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@54:453f24775644, 2017-04-18 (annotated)
- Committer:
- cittecla
- Date:
- Tue Apr 18 13:11:55 2017 +0000
- Revision:
- 54:453f24775644
- Parent:
- 47:728502df3cb6
- Child:
- 58:1a395b6928ee
movement improved
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| cittecla | 38:3526c36e4c73 | 1 | #include "Main.h" |
| cittecla | 18:a82994e67297 | 2 | |
| cittecla | 18:a82994e67297 | 3 | |
| cittecla | 36:b654afdf886e | 4 | int state = 200; |
| cittecla | 32:777976c4d733 | 5 | bool positioning_state = 0; |
| cittecla | 32:777976c4d733 | 6 | bool mapping_state = 0; |
| cittecla | 31:51f52ffa4b51 | 7 | |
| cittecla | 34:40d8d29b44b8 | 8 | int IMU(); |
| cittecla | 34:40d8d29b44b8 | 9 | |
| cittecla | 32:777976c4d733 | 10 | int main() |
| cittecla | 32:777976c4d733 | 11 | { |
| cittecla | 31:51f52ffa4b51 | 12 | |
| cittecla | 32:777976c4d733 | 13 | |
| cittecla | 34:40d8d29b44b8 | 14 | // while (timer() < 300) { |
| cittecla | 34:40d8d29b44b8 | 15 | while (1) { |
| cittecla | 36:b654afdf886e | 16 | wait(0.01f); |
| cittecla | 34:40d8d29b44b8 | 17 | // 5min |
| cittecla | 33:8a98f8b9d859 | 18 | //******************************************************************************* |
| cittecla | 33:8a98f8b9d859 | 19 | //Non_state machine driven function |
| cittecla | 33:8a98f8b9d859 | 20 | //This functions will be called every cycle, use for safety and sensor functipons |
| cittecla | 33:8a98f8b9d859 | 21 | //******************************************************************************* |
| cittecla | 32:777976c4d733 | 22 | |
| cittecla | 38:3526c36e4c73 | 23 | safety(); |
| cittecla | 31:51f52ffa4b51 | 24 | |
| cittecla | 38:3526c36e4c73 | 25 | //scanning(); |
| cittecla | 38:3526c36e4c73 | 26 | |
| cittecla | 38:3526c36e4c73 | 27 | |
| cittecla | 32:777976c4d733 | 28 | |
| cittecla | 32:777976c4d733 | 29 | if(positioning_state) { |
| cittecla | 38:3526c36e4c73 | 30 | positioning(); |
| cittecla | 32:777976c4d733 | 31 | } |
| cittecla | 32:777976c4d733 | 32 | if(mapping_state) { |
| cittecla | 38:3526c36e4c73 | 33 | mapping(); |
| cittecla | 32:777976c4d733 | 34 | } |
| cittecla | 18:a82994e67297 | 35 | |
| cittecla | 33:8a98f8b9d859 | 36 | //******************************************************************************* |
| cittecla | 33:8a98f8b9d859 | 37 | //state machine driven function |
| cittecla | 33:8a98f8b9d859 | 38 | //This functions will only be called when their state is active. |
| cittecla | 33:8a98f8b9d859 | 39 | //The state machine will be in the same state until this action is completed or |
| cittecla | 33:8a98f8b9d859 | 40 | //a safty function kicks in and stops the current function. |
| cittecla | 33:8a98f8b9d859 | 41 | //Every function will return the next active state upon its transition table. |
| cittecla | 33:8a98f8b9d859 | 42 | //State and Transition Table can be found in the State_Machine.xlsx |
| cittecla | 33:8a98f8b9d859 | 43 | //******************************************************************************* |
| cittecla | 32:777976c4d733 | 44 | switch (state) { |
| cittecla | 38:3526c36e4c73 | 45 | case 0: |
| cittecla | 38:3526c36e4c73 | 46 | state = emergency_shutdown(); |
| cittecla | 38:3526c36e4c73 | 47 | break; |
| cittecla | 38:3526c36e4c73 | 48 | case 1: |
| cittecla | 38:3526c36e4c73 | 49 | state = colision_detected(); |
| cittecla | 38:3526c36e4c73 | 50 | break; |
| cittecla | 38:3526c36e4c73 | 51 | case 2: |
| cittecla | 38:3526c36e4c73 | 52 | state = current_to_high(); |
| cittecla | 38:3526c36e4c73 | 53 | break; |
| cittecla | 38:3526c36e4c73 | 54 | case 3: |
| cittecla | 38:3526c36e4c73 | 55 | state = overheating(); |
| cittecla | 38:3526c36e4c73 | 56 | break; |
| cittecla | 31:51f52ffa4b51 | 57 | |
| cittecla | 31:51f52ffa4b51 | 58 | |
| cittecla | 38:3526c36e4c73 | 59 | case 10: |
| cittecla | 38:3526c36e4c73 | 60 | state = idel(); |
| cittecla | 38:3526c36e4c73 | 61 | break; |
| cittecla | 38:3526c36e4c73 | 62 | case 11: |
| cittecla | 38:3526c36e4c73 | 63 | state = idel2(); |
| cittecla | 38:3526c36e4c73 | 64 | break; |
| cittecla | 38:3526c36e4c73 | 65 | |
| cittecla | 38:3526c36e4c73 | 66 | |
| cittecla | 38:3526c36e4c73 | 67 | case 15: |
| cittecla | 38:3526c36e4c73 | 68 | state = initialisation(); |
| cittecla | 38:3526c36e4c73 | 69 | break; |
| cittecla | 38:3526c36e4c73 | 70 | case 16: |
| cittecla | 38:3526c36e4c73 | 71 | state = engage_motors(); |
| cittecla | 38:3526c36e4c73 | 72 | break; |
| cittecla | 38:3526c36e4c73 | 73 | case 17: |
| cittecla | 38:3526c36e4c73 | 74 | state = test_servos(); |
| cittecla | 38:3526c36e4c73 | 75 | break; |
| cittecla | 38:3526c36e4c73 | 76 | case 18: |
| cittecla | 38:3526c36e4c73 | 77 | state = inital_positioning(); |
| cittecla | 38:3526c36e4c73 | 78 | break; |
| cittecla | 32:777976c4d733 | 79 | |
| cittecla | 31:51f52ffa4b51 | 80 | |
| cittecla | 38:3526c36e4c73 | 81 | case 25: |
| cittecla | 38:3526c36e4c73 | 82 | state = start_positioning(); |
| cittecla | 38:3526c36e4c73 | 83 | positioning_state = 1; |
| cittecla | 38:3526c36e4c73 | 84 | break; |
| cittecla | 38:3526c36e4c73 | 85 | case 26: |
| cittecla | 38:3526c36e4c73 | 86 | state = start_mapping(); |
| cittecla | 38:3526c36e4c73 | 87 | mapping_state = 1; |
| cittecla | 38:3526c36e4c73 | 88 | break; |
| cittecla | 38:3526c36e4c73 | 89 | case 27: |
| cittecla | 38:3526c36e4c73 | 90 | state = initial_turn(); |
| cittecla | 38:3526c36e4c73 | 91 | break; |
| cittecla | 38:3526c36e4c73 | 92 | |
| cittecla | 40:a46667b62671 | 93 | |
| cittecla | 38:3526c36e4c73 | 94 | case 35: |
| cittecla | 38:3526c36e4c73 | 95 | state = select_target(); |
| cittecla | 38:3526c36e4c73 | 96 | break; |
| cittecla | 38:3526c36e4c73 | 97 | case 36: |
| cittecla | 38:3526c36e4c73 | 98 | state = pathfinding(); |
| cittecla | 38:3526c36e4c73 | 99 | break; |
| cittecla | 38:3526c36e4c73 | 100 | case 37: |
| cittecla | 41:462d379e85c4 | 101 | state = switch_target_red(); |
| cittecla | 41:462d379e85c4 | 102 | break; |
| cittecla | 31:51f52ffa4b51 | 103 | |
| cittecla | 31:51f52ffa4b51 | 104 | |
| cittecla | 38:3526c36e4c73 | 105 | case 40: |
| cittecla | 38:3526c36e4c73 | 106 | state = moving(); |
| cittecla | 38:3526c36e4c73 | 107 | break; |
| cittecla | 38:3526c36e4c73 | 108 | case 41: |
| cittecla | 54:453f24775644 | 109 | state = move_forward_for_distance(0); |
| cittecla | 38:3526c36e4c73 | 110 | break; |
| cittecla | 38:3526c36e4c73 | 111 | case 42: |
| cittecla | 54:453f24775644 | 112 | state = move_backward_for_distance(); |
| cittecla | 38:3526c36e4c73 | 113 | break; |
| cittecla | 38:3526c36e4c73 | 114 | case 43: |
| cittecla | 54:453f24775644 | 115 | state = turn_for_deg(0); |
| cittecla | 38:3526c36e4c73 | 116 | break; |
| cittecla | 54:453f24775644 | 117 | |
| cittecla | 38:3526c36e4c73 | 118 | |
| cittecla | 38:3526c36e4c73 | 119 | case 46: |
| cittecla | 38:3526c36e4c73 | 120 | state = move_to_next_coord(); |
| cittecla | 38:3526c36e4c73 | 121 | break; |
| cittecla | 38:3526c36e4c73 | 122 | case 47: |
| cittecla | 38:3526c36e4c73 | 123 | state = move_in_search_for_brick(); |
| cittecla | 38:3526c36e4c73 | 124 | break; |
| cittecla | 31:51f52ffa4b51 | 125 | |
| cittecla | 31:51f52ffa4b51 | 126 | |
| cittecla | 38:3526c36e4c73 | 127 | case 50: |
| cittecla | 38:3526c36e4c73 | 128 | state = grabbing(); |
| cittecla | 38:3526c36e4c73 | 129 | break; |
| cittecla | 38:3526c36e4c73 | 130 | case 51: |
| cittecla | 38:3526c36e4c73 | 131 | state = arm_position_move(); |
| cittecla | 38:3526c36e4c73 | 132 | break; |
| cittecla | 38:3526c36e4c73 | 133 | case 52: |
| cittecla | 38:3526c36e4c73 | 134 | state = arm_position_grabbing(); |
| cittecla | 38:3526c36e4c73 | 135 | break; |
| cittecla | 38:3526c36e4c73 | 136 | case 53: |
| cittecla | 38:3526c36e4c73 | 137 | state = arm_position_release(); |
| cittecla | 38:3526c36e4c73 | 138 | break; |
| cittecla | 38:3526c36e4c73 | 139 | |
| cittecla | 38:3526c36e4c73 | 140 | case 55: |
| cittecla | 38:3526c36e4c73 | 141 | state = oben_grabber(); |
| cittecla | 38:3526c36e4c73 | 142 | break; |
| cittecla | 38:3526c36e4c73 | 143 | case 56: |
| cittecla | 38:3526c36e4c73 | 144 | state = close_grabber(); |
| cittecla | 38:3526c36e4c73 | 145 | break; |
| cittecla | 38:3526c36e4c73 | 146 | case 57: |
| cittecla | 38:3526c36e4c73 | 147 | state = test_color(); |
| cittecla | 39:92723f7ea54f | 148 | /* |
| cittecla | 38:3526c36e4c73 | 149 | case 99: |
| cittecla | 38:3526c36e4c73 | 150 | sate = init(); |
| cittecla | 38:3526c36e4c73 | 151 | break; |
| cittecla | 38:3526c36e4c73 | 152 | case 100: |
| cittecla | 38:3526c36e4c73 | 153 | state = turn_left(30); |
| cittecla | 38:3526c36e4c73 | 154 | break; |
| cittecla | 38:3526c36e4c73 | 155 | case 101: |
| cittecla | 38:3526c36e4c73 | 156 | state = turn_right_for_brick; |
| cittecla | 38:3526c36e4c73 | 157 | break; |
| cittecla | 38:3526c36e4c73 | 158 | case 102: |
| cittecla | 38:3526c36e4c73 | 159 | state = move_to_brick; |
| cittecla | 39:92723f7ea54f | 160 | */ |
| cittecla | 19:baa8371d55b4 | 161 | |
| cittecla | 18:a82994e67297 | 162 | |
| cittecla | 38:3526c36e4c73 | 163 | //test states |
| cittecla | 38:3526c36e4c73 | 164 | case 200: |
| cittecla | 34:40d8d29b44b8 | 165 | state = IMU(); |
| cittecla | 32:777976c4d733 | 166 | break; |
| cittecla | 32:777976c4d733 | 167 | default: |
| cittecla | 32:777976c4d733 | 168 | printf("Fatal Error, Unkonwn state!"); |
| cittecla | 32:777976c4d733 | 169 | state = 0; |
| cittecla | 32:777976c4d733 | 170 | break; |
| cittecla | 32:777976c4d733 | 171 | } |
| cittecla | 32:777976c4d733 | 172 | } |
| cittecla | 34:40d8d29b44b8 | 173 | } |
| cittecla | 34:40d8d29b44b8 | 174 | |
| cittecla | 34:40d8d29b44b8 | 175 | int IMU() |
| cittecla | 34:40d8d29b44b8 | 176 | { |
| cittecla | 36:b654afdf886e | 177 | printf("%f\r\n", read_heading()); |
| cittecla | 34:40d8d29b44b8 | 178 | return 200; |
| cittecla | 34:40d8d29b44b8 | 179 | } |
| cittecla | 34:40d8d29b44b8 | 180 | |
| cittecla | 34:40d8d29b44b8 | 181 | int init() |
| cittecla | 34:40d8d29b44b8 | 182 | { |
| cittecla | 34:40d8d29b44b8 | 183 | Robot_init_all(); |
| cittecla | 34:40d8d29b44b8 | 184 | return 100; |
| cittecla | 34:40d8d29b44b8 | 185 | } |
| cittecla | 38:3526c36e4c73 | 186 | |
| cittecla | 38:3526c36e4c73 | 187 | |
| cittecla | 34:40d8d29b44b8 | 188 | /* |
| cittecla | 34:40d8d29b44b8 | 189 | int turn_left(float deg) |
| cittecla | 34:40d8d29b44b8 | 190 | { |
| cittecla | 34:40d8d29b44b8 | 191 | bool in_function = false; |
| cittecla | 34:40d8d29b44b8 | 192 | if(in_function = false) { |
| cittecla | 34:40d8d29b44b8 | 193 | float current_deg = read_heading(); |
| cittecla | 34:40d8d29b44b8 | 194 | set_speed(-50, 50); |
| cittecla | 34:40d8d29b44b8 | 195 | in_function = true; |
| cittecla | 34:40d8d29b44b8 | 196 | } |
| cittecla | 34:40d8d29b44b8 | 197 | if(current_deg - deg < read_heading() { |
| cittecla | 34:40d8d29b44b8 | 198 | set_speed(0, 0); |
| cittecla | 34:40d8d29b44b8 | 199 | return 101; |
| cittecla | 34:40d8d29b44b8 | 200 | in_function = false; |
| cittecla | 34:40d8d29b44b8 | 201 | } |
| cittecla | 34:40d8d29b44b8 | 202 | return 100; |
| cittecla | 34:40d8d29b44b8 | 203 | } |
| cittecla | 34:40d8d29b44b8 | 204 | |
| cittecla | 34:40d8d29b44b8 | 205 | int turn_right_for_brick { |
| cittecla | 34:40d8d29b44b8 | 206 | return 0 |
| cittecla | 34:40d8d29b44b8 | 207 | }*/ |
