Download presentation
Presentation is loading. Please wait.
1
CMPE 118 MECHATRONICS Event-Driven Programming and State Machines
2
CMPE 118 MECHATRONICS Here
3
CMPE 118 MECHATRONICS Traditional Program Structures
4
CMPE 118 MECHATRONICS Traditional Program Structure Often characterized by: Input from keyboard or data file Begins, performs operations, then ends (linear program) Decisions made don’t change fundamental program structure Output to monitor or data file
5
CMPE 118 MECHATRONICS Programming Embedded Systems often asynchronous “simultaneous” inputs & outputs sequences unknowable, re-orderable no “end” or “exit” sensors (switches, light sensors, voltages, etc.) timers user inputs (keypad, push-buttons) update a display move something switch something on or off in general CHANGE SOMETHING PHYSICAL Characteristics: Inputs: Outputs:
6
CMPE 118 MECHATRONICS The Events and Services Framework Conceptual framework An excellent method for implementing Event-driven programming & state machines Emphasizes design first Gets you to use structures that: 1) make it clear how to define the low-levelfunctions 2) make debugging code simpler
7
CMPE 118 MECHATRONICS Events and Services Framework RULE #1: Recognize that tasks break downONLY into two fundamental classes: a. Event Detectors b. Services
8
CMPE 118 MECHATRONICS So, What’s an Event ?
9
CMPE 118 MECHATRONICS What Happens with Noise
10
CMPE 118 MECHATRONICS With Hysteresis
11
CMPE 118 MECHATRONICS Events and Services Framework CORROLARY TO RULE #1: Keep Event-Detector and Service routines as short as possible. Must implement “Non-Blocking” routines
12
CMPE 118 MECHATRONICS Events and Services Framework Complete program structure: Initialize hardware and software Continually test for Events –Round-robin scanning –“Non-blocking” code Perform Service(s) when Event detected –“Non-blocking” code Repeat
13
CMPE 118 MECHATRONICS State Machines Description of an abstract Machine At any given time, it can only be in one of a fixed number of discrete states The next state in a progression depends on inputs and the current state Idealized, instantaneous transitions from one state to the next
14
CMPE 118 MECHATRONICS State Machines Useful tool for describing the behavior of Event-Driven Programming tasks. Allows you to explore the behavior of the program before you implement anything. Naturally fits with the Event-Driven Programming.
15
CMPE 118 MECHATRONICS State Diagram Conventions Pumping Spraying Event1 Action1 (Qual) Optional (!Qual) Event1 Action2
16
CMPE 118 MECHATRONICS State Diagram Conventions
17
CMPE 118 MECHATRONICS Example: Smart Combination Lock Combination = 1-1-8 None Right 1 Right 2 Right 3 Right Open
18
CMPE 118 MECHATRONICS Example: Smart Combination Lock Combination = 1-1-8
19
CMPE 118 MECHATRONICS
20
SES - Software Events and Services Initialize SES by calling: SES_Init(); Event-Checking Functions prototyped with the parameter EVENT_PARAM MyEventChecker(EVENT_PARAM) return unsigned char = 0 if event not detected return unsigned char 0 if event detected to pass data from the Event-Checking Function to its Service Function, use SET_SHARED_BYTE_TO(foo); or SET_SHARED_WORD_TO(foo); Data passed between functions must be static Service Function prototyped with the parameter: SERVICE_PARAM MyServiceFunction(SERVICE_PARAM) no return value to read the data passed from the Event-Checking Function, use GET_SHARED_BYTE() if it’s 8-bit data, or GET_SHARED_WORD() if it’s 16-bit data.
21
CMPE 118 MECHATRONICS Register each Event Function and Service Functions in pairs: SES_Register(MyEventChecker, MyServiceFunction); Start the process by calling SES_HandleEvents() in an infinite loop. while(1) { SES_HandleEvents(); } SES - Software Events and Services
22
CMPE 118 MECHATRONICS Timer Library TIMER: 8 timers available to you (0-7) Initialize timer functionality by calling the function: TMR_Init() Initialize a timer by calling the function: TMR_InitTimer(0,TIME_INTERVAL); TIME_INTERVAL = number of timer ticks (1 tick = 4.1ms) Check to see if the timer has expired by calling: TMR_IsTimerExpired(timer number); Clear the timer flags by calling: TMR_ClearTimerExpired(timer number);
23
CMPE 118 MECHATRONICS Roach Library ROACHLIB: You need to initialize the functions by calling RoachInit(); Functions available for controlling the motors (see documentation for full details): LeftMtrSpeed(x);RightMtrSpeed(x); x is a number from -10 (reverse) to 10 (forward) Functions available for checking the bumpers: uchar ReadBumpers(); Function available for reading the light level: uchar LightLevel();
24
CMPE 118 MECHATRONICS Pseudo-Code (PDL) PDL = Program Design Language Pseudo-code is written in ENGLISH Doesn’t use the syntax of any particular programming language It is a written, low-level exploration of an implementation of an algorithm. It can form the first level of comments for your code
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.