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@64:c2fcf3b349e9, 2017-04-19 (annotated)
- Committer:
- cittecla
- Date:
- Wed Apr 19 08:34:24 2017 +0000
- Revision:
- 64:c2fcf3b349e9
- Parent:
- 62:628f8a4e857c
- Child:
- 67:d1ac034668fc
Init pos
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 | 61:b57577b0072f | 4 | int state = 10; |
| 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 | 61:b57577b0072f | 12 | printf("start...\r\n"); |
| cittecla | 32:777976c4d733 | 13 | |
| cittecla | 34:40d8d29b44b8 | 14 | // while (timer() < 300) { |
| cittecla | 34:40d8d29b44b8 | 15 | while (1) { |
| cittecla | 64:c2fcf3b349e9 | 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 | 61:b57577b0072f | 61 | printf("idel1...\r\n"); |
| cittecla | 38:3526c36e4c73 | 62 | break; |
| cittecla | 38:3526c36e4c73 | 63 | case 11: |
| cittecla | 38:3526c36e4c73 | 64 | state = idel2(); |
| cittecla | 61:b57577b0072f | 65 | printf("idel2...\r\n"); |
| cittecla | 38:3526c36e4c73 | 66 | break; |
| cittecla | 38:3526c36e4c73 | 67 | |
| cittecla | 38:3526c36e4c73 | 68 | |
| cittecla | 38:3526c36e4c73 | 69 | case 15: |
| cittecla | 38:3526c36e4c73 | 70 | state = initialisation(); |
| cittecla | 38:3526c36e4c73 | 71 | break; |
| cittecla | 38:3526c36e4c73 | 72 | case 16: |
| cittecla | 38:3526c36e4c73 | 73 | state = inital_positioning(); |
| cittecla | 38:3526c36e4c73 | 74 | break; |
| cittecla | 32:777976c4d733 | 75 | |
| cittecla | 31:51f52ffa4b51 | 76 | |
| cittecla | 38:3526c36e4c73 | 77 | case 25: |
| cittecla | 58:1a395b6928ee | 78 | state = 26; |
| cittecla | 38:3526c36e4c73 | 79 | positioning_state = 1; |
| cittecla | 38:3526c36e4c73 | 80 | break; |
| cittecla | 38:3526c36e4c73 | 81 | case 26: |
| cittecla | 58:1a395b6928ee | 82 | state = 27; |
| cittecla | 38:3526c36e4c73 | 83 | mapping_state = 1; |
| cittecla | 38:3526c36e4c73 | 84 | break; |
| cittecla | 38:3526c36e4c73 | 85 | case 27: |
| cittecla | 38:3526c36e4c73 | 86 | state = initial_turn(); |
| cittecla | 38:3526c36e4c73 | 87 | break; |
| cittecla | 38:3526c36e4c73 | 88 | |
| cittecla | 40:a46667b62671 | 89 | |
| cittecla | 38:3526c36e4c73 | 90 | case 35: |
| cittecla | 38:3526c36e4c73 | 91 | state = select_target(); |
| cittecla | 38:3526c36e4c73 | 92 | break; |
| cittecla | 38:3526c36e4c73 | 93 | case 36: |
| cittecla | 38:3526c36e4c73 | 94 | state = pathfinding(); |
| cittecla | 38:3526c36e4c73 | 95 | break; |
| cittecla | 38:3526c36e4c73 | 96 | case 37: |
| cittecla | 41:462d379e85c4 | 97 | state = switch_target_red(); |
| cittecla | 41:462d379e85c4 | 98 | break; |
| cittecla | 31:51f52ffa4b51 | 99 | |
| cittecla | 31:51f52ffa4b51 | 100 | |
| cittecla | 38:3526c36e4c73 | 101 | case 40: |
| cittecla | 38:3526c36e4c73 | 102 | state = moving(); |
| cittecla | 38:3526c36e4c73 | 103 | break; |
| cittecla | 38:3526c36e4c73 | 104 | case 41: |
| cittecla | 54:453f24775644 | 105 | state = move_forward_for_distance(0); |
| cittecla | 38:3526c36e4c73 | 106 | break; |
| cittecla | 38:3526c36e4c73 | 107 | case 42: |
| cittecla | 54:453f24775644 | 108 | state = move_backward_for_distance(); |
| cittecla | 38:3526c36e4c73 | 109 | break; |
| cittecla | 62:628f8a4e857c | 110 | |
| cittecla | 54:453f24775644 | 111 | |
| cittecla | 38:3526c36e4c73 | 112 | |
| cittecla | 38:3526c36e4c73 | 113 | case 46: |
| cittecla | 38:3526c36e4c73 | 114 | state = move_to_next_coord(); |
| cittecla | 38:3526c36e4c73 | 115 | break; |
| cittecla | 38:3526c36e4c73 | 116 | case 47: |
| cittecla | 38:3526c36e4c73 | 117 | state = move_in_search_for_brick(); |
| cittecla | 38:3526c36e4c73 | 118 | break; |
| cittecla | 31:51f52ffa4b51 | 119 | |
| cittecla | 31:51f52ffa4b51 | 120 | |
| cittecla | 38:3526c36e4c73 | 121 | case 50: |
| cittecla | 38:3526c36e4c73 | 122 | state = grabbing(); |
| cittecla | 38:3526c36e4c73 | 123 | break; |
| cittecla | 38:3526c36e4c73 | 124 | case 51: |
| cittecla | 38:3526c36e4c73 | 125 | state = arm_position_move(); |
| cittecla | 38:3526c36e4c73 | 126 | break; |
| cittecla | 38:3526c36e4c73 | 127 | case 52: |
| cittecla | 38:3526c36e4c73 | 128 | state = arm_position_grabbing(); |
| cittecla | 38:3526c36e4c73 | 129 | break; |
| cittecla | 38:3526c36e4c73 | 130 | case 53: |
| cittecla | 38:3526c36e4c73 | 131 | state = arm_position_release(); |
| cittecla | 38:3526c36e4c73 | 132 | break; |
| cittecla | 38:3526c36e4c73 | 133 | |
| cittecla | 38:3526c36e4c73 | 134 | case 55: |
| cittecla | 61:b57577b0072f | 135 | state = open_grabber(); |
| cittecla | 38:3526c36e4c73 | 136 | break; |
| cittecla | 38:3526c36e4c73 | 137 | case 56: |
| cittecla | 38:3526c36e4c73 | 138 | state = close_grabber(); |
| cittecla | 38:3526c36e4c73 | 139 | break; |
| cittecla | 61:b57577b0072f | 140 | |
| cittecla | 39:92723f7ea54f | 141 | /* |
| cittecla | 38:3526c36e4c73 | 142 | case 99: |
| cittecla | 38:3526c36e4c73 | 143 | sate = init(); |
| cittecla | 38:3526c36e4c73 | 144 | break; |
| cittecla | 38:3526c36e4c73 | 145 | case 100: |
| cittecla | 38:3526c36e4c73 | 146 | state = turn_left(30); |
| cittecla | 38:3526c36e4c73 | 147 | break; |
| cittecla | 38:3526c36e4c73 | 148 | case 101: |
| cittecla | 38:3526c36e4c73 | 149 | state = turn_right_for_brick; |
| cittecla | 38:3526c36e4c73 | 150 | break; |
| cittecla | 38:3526c36e4c73 | 151 | case 102: |
| cittecla | 38:3526c36e4c73 | 152 | state = move_to_brick; |
| cittecla | 39:92723f7ea54f | 153 | */ |
| cittecla | 19:baa8371d55b4 | 154 | |
| cittecla | 18:a82994e67297 | 155 | |
| cittecla | 38:3526c36e4c73 | 156 | //test states |
| cittecla | 38:3526c36e4c73 | 157 | case 200: |
| cittecla | 34:40d8d29b44b8 | 158 | state = IMU(); |
| cittecla | 32:777976c4d733 | 159 | break; |
| cittecla | 32:777976c4d733 | 160 | default: |
| cittecla | 32:777976c4d733 | 161 | printf("Fatal Error, Unkonwn state!"); |
| cittecla | 32:777976c4d733 | 162 | state = 0; |
| cittecla | 32:777976c4d733 | 163 | break; |
| cittecla | 32:777976c4d733 | 164 | } |
| cittecla | 32:777976c4d733 | 165 | } |
| cittecla | 34:40d8d29b44b8 | 166 | } |
| cittecla | 34:40d8d29b44b8 | 167 | |
| cittecla | 34:40d8d29b44b8 | 168 | int IMU() |
| cittecla | 34:40d8d29b44b8 | 169 | { |
| cittecla | 36:b654afdf886e | 170 | printf("%f\r\n", read_heading()); |
| cittecla | 34:40d8d29b44b8 | 171 | return 200; |
| cittecla | 34:40d8d29b44b8 | 172 | } |
| cittecla | 34:40d8d29b44b8 | 173 | |
| cittecla | 34:40d8d29b44b8 | 174 | int init() |
| cittecla | 34:40d8d29b44b8 | 175 | { |
| cittecla | 34:40d8d29b44b8 | 176 | Robot_init_all(); |
| cittecla | 34:40d8d29b44b8 | 177 | return 100; |
| cittecla | 34:40d8d29b44b8 | 178 | } |
| cittecla | 38:3526c36e4c73 | 179 | |
| cittecla | 38:3526c36e4c73 | 180 | |
| cittecla | 34:40d8d29b44b8 | 181 | /* |
| cittecla | 34:40d8d29b44b8 | 182 | int turn_left(float deg) |
| cittecla | 34:40d8d29b44b8 | 183 | { |
| cittecla | 34:40d8d29b44b8 | 184 | bool in_function = false; |
| cittecla | 34:40d8d29b44b8 | 185 | if(in_function = false) { |
| cittecla | 34:40d8d29b44b8 | 186 | float current_deg = read_heading(); |
| cittecla | 34:40d8d29b44b8 | 187 | set_speed(-50, 50); |
| cittecla | 34:40d8d29b44b8 | 188 | in_function = true; |
| cittecla | 34:40d8d29b44b8 | 189 | } |
| cittecla | 34:40d8d29b44b8 | 190 | if(current_deg - deg < read_heading() { |
| cittecla | 34:40d8d29b44b8 | 191 | set_speed(0, 0); |
| cittecla | 34:40d8d29b44b8 | 192 | return 101; |
| cittecla | 34:40d8d29b44b8 | 193 | in_function = false; |
| cittecla | 34:40d8d29b44b8 | 194 | } |
| cittecla | 34:40d8d29b44b8 | 195 | return 100; |
| cittecla | 34:40d8d29b44b8 | 196 | } |
| cittecla | 34:40d8d29b44b8 | 197 | |
| cittecla | 34:40d8d29b44b8 | 198 | int turn_right_for_brick { |
| cittecla | 34:40d8d29b44b8 | 199 | return 0 |
| cittecla | 34:40d8d29b44b8 | 200 | }*/ |
