Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of Robocode by
source/main.cpp@32:777976c4d733, 2017-03-16 (annotated)
- Committer:
- cittecla
- Date:
- Thu Mar 16 12:52:41 2017 +0000
- Revision:
- 32:777976c4d733
- Parent:
- 31:51f52ffa4b51
- Child:
- 33:8a98f8b9d859
first iteration of the state machine implemented
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| cittecla | 32:777976c4d733 | 1 | #include "mbed.h" |
| cittecla | 32:777976c4d733 | 2 | #include "pathfinding.h" |
| cittecla | 27:df11ab63cda4 | 3 | |
| cittecla | 18:a82994e67297 | 4 | |
| cittecla | 18:a82994e67297 | 5 | |
| cittecla | 32:777976c4d733 | 6 | int state = 10; |
| cittecla | 32:777976c4d733 | 7 | bool positioning_state = 0; |
| cittecla | 32:777976c4d733 | 8 | bool mapping_state = 0; |
| cittecla | 31:51f52ffa4b51 | 9 | |
| cittecla | 32:777976c4d733 | 10 | int main() |
| cittecla | 32:777976c4d733 | 11 | { |
| cittecla | 31:51f52ffa4b51 | 12 | |
| cittecla | 32:777976c4d733 | 13 | |
| cittecla | 32:777976c4d733 | 14 | while (timer() < 300) { // 5min |
| cittecla | 31:51f52ffa4b51 | 15 | /********************************************************************************* |
| cittecla | 31:51f52ffa4b51 | 16 | Non_state machine driven function |
| cittecla | 31:51f52ffa4b51 | 17 | This functions will be called every cycle, use for safety and sensor functipons |
| cittecla | 31:51f52ffa4b51 | 18 | **********************************************************************************/ |
| cittecla | 32:777976c4d733 | 19 | |
| cittecla | 32:777976c4d733 | 20 | safty() { |
| cittecla | 31:51f52ffa4b51 | 21 | |
| cittecla | 31:51f52ffa4b51 | 22 | } |
| cittecla | 31:51f52ffa4b51 | 23 | |
| cittecla | 31:51f52ffa4b51 | 24 | scanning() { |
| cittecla | 32:777976c4d733 | 25 | |
| cittecla | 31:51f52ffa4b51 | 26 | } |
| cittecla | 32:777976c4d733 | 27 | |
| cittecla | 32:777976c4d733 | 28 | if(positioning_state) { |
| cittecla | 32:777976c4d733 | 29 | positioning(); |
| cittecla | 32:777976c4d733 | 30 | } |
| cittecla | 32:777976c4d733 | 31 | if(mapping_state) { |
| cittecla | 32:777976c4d733 | 32 | mapping(); |
| cittecla | 32:777976c4d733 | 33 | } |
| cittecla | 18:a82994e67297 | 34 | |
| cittecla | 32:777976c4d733 | 35 | /********************************************************************************* |
| cittecla | 32:777976c4d733 | 36 | state machine driven function |
| cittecla | 32:777976c4d733 | 37 | This functions will only be called when their state is active. |
| cittecla | 32:777976c4d733 | 38 | The state machine will be in the same state until this action is completed or |
| cittecla | 32:777976c4d733 | 39 | a safty function kicks in and stops the current function. |
| cittecla | 32:777976c4d733 | 40 | Every function will return the next active state upon its transition table. |
| cittecla | 32:777976c4d733 | 41 | State and Transition Table can be found in the State_Machine.xlsx |
| cittecla | 32:777976c4d733 | 42 | **********************************************************************************/ |
| cittecla | 32:777976c4d733 | 43 | switch (state) { |
| cittecla | 32:777976c4d733 | 44 | case 0: |
| cittecla | 32:777976c4d733 | 45 | state = emergency_shutdown(); |
| cittecla | 32:777976c4d733 | 46 | break; |
| cittecla | 32:777976c4d733 | 47 | case 1: |
| cittecla | 32:777976c4d733 | 48 | state = colision_detected(); |
| cittecla | 32:777976c4d733 | 49 | break; |
| cittecla | 32:777976c4d733 | 50 | case 2: |
| cittecla | 32:777976c4d733 | 51 | state = current_to_high(); |
| cittecla | 32:777976c4d733 | 52 | break; |
| cittecla | 32:777976c4d733 | 53 | case 3: |
| cittecla | 32:777976c4d733 | 54 | state = overheating(); |
| cittecla | 32:777976c4d733 | 55 | break; |
| cittecla | 31:51f52ffa4b51 | 56 | |
| cittecla | 31:51f52ffa4b51 | 57 | |
| cittecla | 32:777976c4d733 | 58 | case 10: |
| cittecla | 32:777976c4d733 | 59 | state = idel(); |
| cittecla | 32:777976c4d733 | 60 | break; |
| cittecla | 32:777976c4d733 | 61 | case 11: |
| cittecla | 32:777976c4d733 | 62 | state = idel2(); |
| cittecla | 32:777976c4d733 | 63 | break; |
| cittecla | 32:777976c4d733 | 64 | |
| cittecla | 31:51f52ffa4b51 | 65 | |
| cittecla | 32:777976c4d733 | 66 | case 15: |
| cittecla | 32:777976c4d733 | 67 | state = initialisation(); |
| cittecla | 32:777976c4d733 | 68 | break; |
| cittecla | 32:777976c4d733 | 69 | case 16: |
| cittecla | 32:777976c4d733 | 70 | state = engage_motors(); |
| cittecla | 32:777976c4d733 | 71 | break; |
| cittecla | 32:777976c4d733 | 72 | case 17: |
| cittecla | 32:777976c4d733 | 73 | state = test_servos(); |
| cittecla | 32:777976c4d733 | 74 | break; |
| cittecla | 32:777976c4d733 | 75 | case 18: |
| cittecla | 32:777976c4d733 | 76 | state = inital_positioning(); |
| cittecla | 32:777976c4d733 | 77 | break; |
| cittecla | 31:51f52ffa4b51 | 78 | |
| cittecla | 31:51f52ffa4b51 | 79 | |
| cittecla | 32:777976c4d733 | 80 | case 25: |
| cittecla | 32:777976c4d733 | 81 | state = start_positioning(); |
| cittecla | 32:777976c4d733 | 82 | break; |
| cittecla | 32:777976c4d733 | 83 | case 26: |
| cittecla | 32:777976c4d733 | 84 | state = start_mapping(); |
| cittecla | 32:777976c4d733 | 85 | break; |
| cittecla | 32:777976c4d733 | 86 | case 27: |
| cittecla | 32:777976c4d733 | 87 | state = initial_turn(); |
| cittecla | 32:777976c4d733 | 88 | break; |
| cittecla | 31:51f52ffa4b51 | 89 | |
| cittecla | 31:51f52ffa4b51 | 90 | |
| cittecla | 32:777976c4d733 | 91 | case 35: |
| cittecla | 32:777976c4d733 | 92 | state = select_target(); |
| cittecla | 32:777976c4d733 | 93 | break; |
| cittecla | 32:777976c4d733 | 94 | case 36: |
| cittecla | 32:777976c4d733 | 95 | state = pathfinding(); |
| cittecla | 32:777976c4d733 | 96 | break; |
| cittecla | 32:777976c4d733 | 97 | case 37: |
| cittecla | 32:777976c4d733 | 98 | state = remove_target(); |
| cittecla | 32:777976c4d733 | 99 | break; |
| cittecla | 25:08ee4525155b | 100 | |
| cittecla | 19:baa8371d55b4 | 101 | |
| cittecla | 18:a82994e67297 | 102 | |
| cittecla | 32:777976c4d733 | 103 | case 40: |
| cittecla | 32:777976c4d733 | 104 | state = moving(); |
| cittecla | 32:777976c4d733 | 105 | break; |
| cittecla | 32:777976c4d733 | 106 | case 41: |
| cittecla | 32:777976c4d733 | 107 | state = moving_forward_for_distance(); |
| cittecla | 32:777976c4d733 | 108 | break; |
| cittecla | 32:777976c4d733 | 109 | case 42: |
| cittecla | 32:777976c4d733 | 110 | state = moving_backward_for_distance(); |
| cittecla | 32:777976c4d733 | 111 | break; |
| cittecla | 32:777976c4d733 | 112 | case 43: |
| cittecla | 32:777976c4d733 | 113 | state = turn_left_for_deg(); |
| cittecla | 32:777976c4d733 | 114 | break; |
| cittecla | 32:777976c4d733 | 115 | case 44: |
| cittecla | 32:777976c4d733 | 116 | state = turn_right_for_deg(); |
| cittecla | 32:777976c4d733 | 117 | break; |
| cittecla | 32:777976c4d733 | 118 | |
| cittecla | 32:777976c4d733 | 119 | case 46: |
| cittecla | 32:777976c4d733 | 120 | state = move_to_next_coord(); |
| cittecla | 32:777976c4d733 | 121 | break; |
| cittecla | 32:777976c4d733 | 122 | case 47: |
| cittecla | 32:777976c4d733 | 123 | state = move_in_search_for_brick(); |
| cittecla | 32:777976c4d733 | 124 | break; |
| cittecla | 18:a82994e67297 | 125 | |
| cittecla | 18:a82994e67297 | 126 | |
| cittecla | 32:777976c4d733 | 127 | case 50: |
| cittecla | 32:777976c4d733 | 128 | state = grabbing(); |
| cittecla | 32:777976c4d733 | 129 | break; |
| cittecla | 32:777976c4d733 | 130 | case 51: |
| cittecla | 32:777976c4d733 | 131 | state = arm_move_up(); |
| cittecla | 32:777976c4d733 | 132 | break; |
| cittecla | 32:777976c4d733 | 133 | case 52: |
| cittecla | 32:777976c4d733 | 134 | state = arm_move_down(); |
| cittecla | 32:777976c4d733 | 135 | break; |
| cittecla | 32:777976c4d733 | 136 | case 53: |
| cittecla | 32:777976c4d733 | 137 | state = arm_move_to_hold(); |
| cittecla | 32:777976c4d733 | 138 | break; |
| cittecla | 24:6c2fec64f890 | 139 | |
| cittecla | 32:777976c4d733 | 140 | case 55: |
| cittecla | 32:777976c4d733 | 141 | state = oben_grabber(); |
| cittecla | 32:777976c4d733 | 142 | break; |
| cittecla | 32:777976c4d733 | 143 | case 56: |
| cittecla | 32:777976c4d733 | 144 | state = close_grabber(); |
| cittecla | 32:777976c4d733 | 145 | break; |
| cittecla | 32:777976c4d733 | 146 | case 57: |
| cittecla | 32:777976c4d733 | 147 | state = test_color(); |
| cittecla | 25:08ee4525155b | 148 | |
| cittecla | 32:777976c4d733 | 149 | |
| cittecla | 32:777976c4d733 | 150 | default: |
| cittecla | 32:777976c4d733 | 151 | printf("Fatal Error, Unkonwn state!"); |
| cittecla | 32:777976c4d733 | 152 | state = 0; |
| cittecla | 32:777976c4d733 | 153 | break; |
| cittecla | 32:777976c4d733 | 154 | } |
| cittecla | 32:777976c4d733 | 155 | } |
| cittecla | 31:51f52ffa4b51 | 156 | } |
