Академический Документы
Профессиональный Документы
Культура Документы
Page 1/5
Lets start with writing the software first. First, recall the very simple state diagram
I drew in the last video module.
Youll remember that your Arduino code always consists of two functions:
setup(): Called once after power-up or reset.
loop(): Called over and over again.
This is where well put implementing our FSM.
A hint about coding style: Try to use functions as much as possible. Try to avoid
cluttering up setup() and loop() with a lot of lines of code that would be better put
in a function.
Driving
ObstacleDetected
ObstacleGone
Waiting
Entry/start timer
Exit/stop timer
Do: look for obstacle
Do: check timer
TimerExpired
Page 2/5
loop(){
switch(State){
case DRIVING:
ObstaclePresent = CheckForObstacle();
break;
case WAITING:
ObstaclePresent = CheckForObstacle();
TimerExpired = CheckTimer();
break;
case TERMINATE:
break;
}
Page 3/5
StartDriving();
}
if(TimerExpired){
StopTimer();
State = TERMINATE;
}
break;
case TERMINATE:
break;
}
Tools for Describing Software
Once your code is complete, you need to provide documentation that will allow others
to use, modify and build on your software.
This documentation comes in a variety of sources:
Diagrams and tools for describing your software modules. The UML system that
we used for our state diagrams have a number of other systems for describing
software structure. These are quite focused on OO software design, though.
Automatic tools that parse the comments of the code itself to produce documentation. The doxygen system is an example of this.
Simply producing readable code. Good comments, variable names and function
names.
We will be using the final system in this class.
Readable Code
Follow these guidelines:
Use descriptive variable names.
Always give a variable a name that describes the data it stores or the function it
performs.
If you have an integer that is 0 if there is no obstacle or 1 if there is an obstacle,
call it ObstacleDetected instead of A or x1.
Note how this makes the code itself start to read more like a sentence: if(ObstacleDetected){
..} instead of if(A){..}
Use descriptive function names.
Always give a function a name that describes what it does.
If the function checks for an obstacle, call it CheckForObstacle() rather than
f1().
ENEL 300 Supplementary Module 2: Detailed Design Diagrams
c Geoffrey Messier, 2012
Page 4/5
Page 5/5