513
edits
No edit summary |
|||
Line 3: | Line 3: | ||
== Introduction == | == Introduction == | ||
The state machine is an important concept in theoretical Computer Science. | The state machine is an important concept in theoretical Computer Science. <br> | ||
The following article explains how this concept can be used to program and organize different actions of simple robots. | The following article explains how this concept can be used to program and organize different actions of simple robots. | ||
Line 19: | Line 19: | ||
We can imagine that only one possible output (action) is not enough to react to a diversity of inputs (stimuli) - | We can imagine that only one possible output (action) is not enough to react to a diversity of inputs (stimuli) -<br> | ||
It might be a good idea to move towards the closest wall if we are looking for a power plug. At the same time this would be rather bad if we are being chased by a prosecutor. | It might be a good idea to move towards the closest wall if we are looking for a power plug. At the same time this would be rather bad if we are being chased by a prosecutor. <br> | ||
We can also imagine that a suitable program can easily get quite complex and messy. | We can also imagine that a suitable program can easily get quite complex and messy. <br> | ||
Furthermore there is a good chance that some important relations between situations and reactions get lost in the programming process. | Furthermore there is a good chance that some important relations between situations and reactions get lost in the programming process. | ||
Line 31: | Line 31: | ||
A good way to deal with the possible complexity of such a program is to draw a corresponding diagram. | A good way to deal with the possible complexity of such a program is to draw a corresponding diagram. | ||
There is another useful concept in theoretical Computer Science that can help us doing that: | There is another useful concept in theoretical Computer Science that can help us doing that:<br> | ||
It says that it is enough to conduct only one predefined action (output) in reaction to the current situation (state) and to then look at the input data in order to analyze and decide the next situation (and so forth..). | It says that it is enough to conduct only one predefined action (output) in reaction to the current situation (state) and to then look at the input data in order to analyze and decide the next situation (and so forth..). <br> | ||
This kind of action pattern ([https://en.wikipedia.org/wiki/Moore_machine Moore Machine]) can be used in order to disassemble, restructure and consequently simplify similar sequences of much higher complexity ([https://en.wikipedia.org/wiki/Mealy_machine Mealy Machine]). | This kind of action pattern ([https://en.wikipedia.org/wiki/Moore_machine Moore Machine]) can be used in order to disassemble, restructure and consequently simplify similar sequences of much higher complexity ([https://en.wikipedia.org/wiki/Mealy_machine Mealy Machine]). | ||
Line 53: | Line 53: | ||
==Example: developing the control mechanism of an autonomous vacuum cleaner== | ==Example: developing the control mechanism of an autonomous vacuum cleaner== | ||
All of the above will become much clearer if we look at an practical example of an autonomous vacuum cleaner robot. | All of the above will become much clearer if we look at an practical example of an autonomous vacuum cleaner robot. <br> | ||
Step by step we will think of everything the robot has to do, put it in the right order and draw an appropriate diagram. | Step by step we will think of everything the robot has to do, put it in the right order and draw an appropriate diagram. | ||
Line 59: | Line 59: | ||
1) Turned on | 1) Turned on | ||
The robot stands still and waits for further instructions. | The robot stands still and waits for further instructions.<br> | ||
It is now in standby mode. | It is now in standby mode. | ||
Line 67: | Line 67: | ||
2) On / Off - button pushed | 2) On / Off - button pushed | ||
The robot starts to clean the room. | The robot starts to clean the room.<br> | ||
It moves forward and activates the air pump. | It moves forward and activates the air pump. | ||
Line 75: | Line 75: | ||
3) Approaching a wall | 3) Approaching a wall | ||
The robot would bump into the next wall if it would only be able to move forward. | The robot would bump into the next wall if it would only be able to move forward.<br> | ||
It therefore needs a function that recognizes walls and makes the robot turn around. | It therefore needs a function that recognizes walls and makes the robot turn around. | ||
Line 83: | Line 83: | ||
4) Turning around | 4) Turning around | ||
So far the robot turns randomly in any direction. | So far the robot turns randomly in any direction.<br> | ||
We have to define a parameter that tells the robot if it turned enough in order to continue cleaning. | We have to define a parameter that tells the robot if it turned enough in order to continue cleaning. <br> | ||
This parameter can be a time delay, a certain amount of wheel revolutions or the value of a (distance, ..) sensor. | This parameter can be a time delay, a certain amount of wheel revolutions or the value of a (distance, ..) sensor. | ||
Line 105: | Line 105: | ||
'''Step 1:''' saving the current state into a variable | '''Step 1:''' saving the current state into a variable | ||
First of all we need a variable that saves the current state. It represents our State Machines memory. | First of all we need a variable that saves the current state. It represents our State Machines memory. <br> | ||
The most suitable type for such a variable is in this case an „[https://en.wikipedia.org/wiki/Enumerated_type enum]“. | The most suitable type for such a variable is in this case an „[https://en.wikipedia.org/wiki/Enumerated_type enum]“.<br> | ||
This enum assigns different names to values: | This enum assigns different names to values: | ||
[[File:robot_enum.jpg]] | [[File:robot_enum.jpg]] | ||
Note (enum + Processing): the definition of an enum has to be saved in a separate file (tab) with an .java extension. ([http://stackoverflow.com/questions/13370090/enums-in-processing-2-0 more info] ) | Note (enum + Processing): the definition of an enum has to be saved in a separate file (tab) with an .java extension. <br>([http://stackoverflow.com/questions/13370090/enums-in-processing-2-0 more info] ) | ||
'''Step 2:''' choosing alternative actions | '''Step 2:''' choosing alternative actions | ||
Now we need something that executes different code depending on the current state. | Now we need something that executes different code depending on the current state.<br> | ||
For this purpose we will have a closer look at the [https://www.arduino.cc/en/Reference/SwitchCase switch/case] statement. | For this purpose we will have a closer look at the [https://www.arduino.cc/en/Reference/SwitchCase switch/case] statement. <br> | ||
It operates similar to an if-condition whereas it can choose from more than two (if… else..) alternatives. | It operates similar to an if-condition whereas it can choose from more than two (if… else..) alternatives. <br> | ||
It could for instance run over and over again in the main loop-function of our program: | It could for instance run over and over again in the main loop-function of our program: | ||
Line 126: | Line 126: | ||
'''Step 3:''' defining the practical behavior of each state | '''Step 3:''' defining the practical behavior of each state | ||
From now on it makes sense to use functions (aggregation of code) in order to keep a clean overview of our program. | From now on it makes sense to use functions (aggregation of code) in order to keep a clean overview of our program.<br> | ||
Each function includes the particular code that defines the behavior of a certain state. | Each function includes the particular code that defines the behavior of a certain state. <br> | ||
The internal structure of these functions is always the same: | The internal structure of these functions is always the same: | ||
Line 143: | Line 143: | ||
'''Step 4:''' assembling everything | '''Step 4:''' assembling everything | ||
We can now call each particular function in the appropriate part of the switch/case statement. | We can now call each particular function in the appropriate part of the switch/case statement.<br> | ||
The functions return value is saved in the state variable for the next loop. | The functions return value is saved in the state variable for the next loop. | ||
Line 151: | Line 151: | ||
'''Step 5:''' extending the program | '''Step 5:''' extending the program | ||
This kind of program structure can easily be extended by additional states without disturbing the overall clarity. | This kind of program structure can easily be extended by additional states without disturbing the overall clarity.<br> | ||
Therefore it makes sense to begin with a simple structure and to then add functions that are more complex. | Therefore it makes sense to begin with a simple structure and to then add functions that are more complex. |
edits