Page 44 - Designing Autonomous Mobile Robots : Inside the Mindo f an Intellegent Machine
P. 44
The Basics of Real-time Software (For Mere Mortals)
flow under the state model already shows signs of having a lot of branching. Experi-
enced programmers have an inherent abhorrence of this type of code, because they
know it leads to chaos.
Now consider the program flow on the right side of Figure 3.2. The elegance of this
program compared to the state-driven model is striking, and it will become more
dramatic as the message protocol becomes more complex. The trick in the context
model is that what appear to be simple Get Character calls are actually points where
task switching occurs. Shortly you will see how simple this can be.
Serial Interrupt Get Character
Save Registers
No
Char = "#"
Get Character
from UART
Yes
Get Character
1st Expected 3rd
Character Save Low
Character
Save High
Character
2nd Get Character
No
Save Low Flag Message
Char = "#"
Character Ready
Save High
Character
Yes
Expected Expected Expected Flag Message
Character=2nd Character=3rd Character=1st Ready
Restore
Registers
Return from
Interrupt
State Driven Context Driven
Message Receiver Message Receiver
Figure 3.2. State-driven vs. context driven
27

