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@81:a6f0f827dcb0, 2017-05-01 (annotated)
- Committer:
- aeschsim
- Date:
- Mon May 01 09:22:29 2017 +0000
- Revision:
- 81:a6f0f827dcb0
- Parent:
- 80:956f65714207
- Child:
- 87:df8c869a5a52
servo and sensors are working now, to do: optimize move for relaese;
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 | |
| aeschsim | 81:a6f0f827dcb0 | 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 | 71:ddf4eb5c3081 | 8 | Timer game_timer; |
| cittecla | 71:ddf4eb5c3081 | 9 | |
| cittecla | 32:777976c4d733 | 10 | int main() |
| cittecla | 32:777976c4d733 | 11 | { |
| cittecla | 61:b57577b0072f | 12 | printf("start...\r\n"); |
| cittecla | 71:ddf4eb5c3081 | 13 | while (game_timer.read() < 300) { |
| cittecla | 64:c2fcf3b349e9 | 14 | wait(0.01f); |
| cittecla | 34:40d8d29b44b8 | 15 | // 5min |
| cittecla | 33:8a98f8b9d859 | 16 | //******************************************************************************* |
| cittecla | 33:8a98f8b9d859 | 17 | //Non_state machine driven function |
| cittecla | 33:8a98f8b9d859 | 18 | //This functions will be called every cycle, use for safety and sensor functipons |
| cittecla | 33:8a98f8b9d859 | 19 | //******************************************************************************* |
| cittecla | 32:777976c4d733 | 20 | |
| cittecla | 38:3526c36e4c73 | 21 | safety(); |
| cittecla | 31:51f52ffa4b51 | 22 | |
| cittecla | 38:3526c36e4c73 | 23 | //scanning(); |
| cittecla | 71:ddf4eb5c3081 | 24 | |
| cittecla | 38:3526c36e4c73 | 25 | |
| cittecla | 32:777976c4d733 | 26 | |
| cittecla | 32:777976c4d733 | 27 | if(positioning_state) { |
| cittecla | 38:3526c36e4c73 | 28 | positioning(); |
| cittecla | 32:777976c4d733 | 29 | } |
| cittecla | 32:777976c4d733 | 30 | if(mapping_state) { |
| cittecla | 38:3526c36e4c73 | 31 | mapping(); |
| cittecla | 32:777976c4d733 | 32 | } |
| cittecla | 18:a82994e67297 | 33 | |
| cittecla | 33:8a98f8b9d859 | 34 | //******************************************************************************* |
| cittecla | 33:8a98f8b9d859 | 35 | //state machine driven function |
| cittecla | 33:8a98f8b9d859 | 36 | //This functions will only be called when their state is active. |
| cittecla | 33:8a98f8b9d859 | 37 | //The state machine will be in the same state until this action is completed or |
| cittecla | 33:8a98f8b9d859 | 38 | //a safty function kicks in and stops the current function. |
| cittecla | 33:8a98f8b9d859 | 39 | //Every function will return the next active state upon its transition table. |
| cittecla | 33:8a98f8b9d859 | 40 | //State and Transition Table can be found in the State_Machine.xlsx |
| cittecla | 33:8a98f8b9d859 | 41 | //******************************************************************************* |
| cittecla | 72:4e8a151d804e | 42 | //printf("state: %d\r\n",state); |
| aeschsim | 81:a6f0f827dcb0 | 43 | printf("State - %d\r\n",state); |
| 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 | 71:ddf4eb5c3081 | 60 | state = idle(); |
| cittecla | 71:ddf4eb5c3081 | 61 | printf("idle1...\r\n"); |
| cittecla | 38:3526c36e4c73 | 62 | break; |
| cittecla | 38:3526c36e4c73 | 63 | case 11: |
| cittecla | 71:ddf4eb5c3081 | 64 | state = idle2(); |
| cittecla | 71:ddf4eb5c3081 | 65 | printf("idle2...\r\n"); |
| cittecla | 38:3526c36e4c73 | 66 | break; |
| cittecla | 38:3526c36e4c73 | 67 | |
| cittecla | 38:3526c36e4c73 | 68 | |
| cittecla | 38:3526c36e4c73 | 69 | case 15: |
| aeschsim | 78:d9138c6f82a8 | 70 | state = initialisation(); //init servo is included |
| cittecla | 38:3526c36e4c73 | 71 | break; |
| cittecla | 38:3526c36e4c73 | 72 | case 16: |
| cittecla | 79:92b9d083322d | 73 | state = inital_arm_positioning(); |
| aeschsim | 81:a6f0f827dcb0 | 74 | state = 11; |
| cittecla | 38:3526c36e4c73 | 75 | break; |
| cittecla | 71:ddf4eb5c3081 | 76 | case 17: |
| cittecla | 79:92b9d083322d | 77 | state = initial_positioning(); |
| cittecla | 71:ddf4eb5c3081 | 78 | break; |
| cittecla | 31:51f52ffa4b51 | 79 | |
| cittecla | 38:3526c36e4c73 | 80 | case 25: |
| cittecla | 58:1a395b6928ee | 81 | state = 26; |
| cittecla | 71:ddf4eb5c3081 | 82 | game_timer.start(); |
| cittecla | 38:3526c36e4c73 | 83 | break; |
| cittecla | 38:3526c36e4c73 | 84 | case 26: |
| cittecla | 58:1a395b6928ee | 85 | state = 27; |
| cittecla | 71:ddf4eb5c3081 | 86 | positioning_state = 1; |
| cittecla | 71:ddf4eb5c3081 | 87 | break; |
| cittecla | 71:ddf4eb5c3081 | 88 | case 27: |
| cittecla | 71:ddf4eb5c3081 | 89 | state = 28; |
| cittecla | 38:3526c36e4c73 | 90 | mapping_state = 1; |
| cittecla | 38:3526c36e4c73 | 91 | break; |
| cittecla | 71:ddf4eb5c3081 | 92 | case 28: |
| cittecla | 38:3526c36e4c73 | 93 | state = initial_turn(); |
| cittecla | 38:3526c36e4c73 | 94 | break; |
| cittecla | 38:3526c36e4c73 | 95 | |
| cittecla | 40:a46667b62671 | 96 | |
| cittecla | 38:3526c36e4c73 | 97 | case 35: |
| cittecla | 38:3526c36e4c73 | 98 | state = select_target(); |
| cittecla | 38:3526c36e4c73 | 99 | break; |
| cittecla | 38:3526c36e4c73 | 100 | case 36: |
| cittecla | 38:3526c36e4c73 | 101 | state = pathfinding(); |
| cittecla | 38:3526c36e4c73 | 102 | break; |
| cittecla | 38:3526c36e4c73 | 103 | case 37: |
| cittecla | 41:462d379e85c4 | 104 | state = switch_target_red(); |
| cittecla | 41:462d379e85c4 | 105 | break; |
| cittecla | 31:51f52ffa4b51 | 106 | |
| cittecla | 31:51f52ffa4b51 | 107 | |
| cittecla | 38:3526c36e4c73 | 108 | case 40: |
| cittecla | 38:3526c36e4c73 | 109 | state = moving(); |
| cittecla | 38:3526c36e4c73 | 110 | break; |
| cittecla | 38:3526c36e4c73 | 111 | case 41: |
| cittecla | 54:453f24775644 | 112 | state = move_forward_for_distance(0); |
| cittecla | 38:3526c36e4c73 | 113 | break; |
| cittecla | 38:3526c36e4c73 | 114 | case 42: |
| cittecla | 54:453f24775644 | 115 | state = move_backward_for_distance(); |
| cittecla | 38:3526c36e4c73 | 116 | break; |
| cittecla | 62:628f8a4e857c | 117 | |
| cittecla | 54:453f24775644 | 118 | |
| cittecla | 38:3526c36e4c73 | 119 | |
| cittecla | 38:3526c36e4c73 | 120 | case 46: |
| cittecla | 38:3526c36e4c73 | 121 | state = move_to_next_coord(); |
| cittecla | 38:3526c36e4c73 | 122 | break; |
| cittecla | 38:3526c36e4c73 | 123 | case 47: |
| cittecla | 38:3526c36e4c73 | 124 | state = move_in_search_for_brick(); |
| cittecla | 38:3526c36e4c73 | 125 | break; |
| cittecla | 31:51f52ffa4b51 | 126 | |
| cittecla | 31:51f52ffa4b51 | 127 | |
| cittecla | 38:3526c36e4c73 | 128 | case 50: |
| cittecla | 38:3526c36e4c73 | 129 | state = grabbing(); |
| cittecla | 38:3526c36e4c73 | 130 | break; |
| cittecla | 38:3526c36e4c73 | 131 | case 51: |
| cittecla | 38:3526c36e4c73 | 132 | state = arm_position_move(); |
| cittecla | 38:3526c36e4c73 | 133 | break; |
| cittecla | 38:3526c36e4c73 | 134 | case 52: |
| aeschsim | 63:87b615e4a842 | 135 | arm_position_grabbing(); |
| aeschsim | 63:87b615e4a842 | 136 | state = 56; |
| aeschsim | 63:87b615e4a842 | 137 | wait_ms(1000); |
| cittecla | 38:3526c36e4c73 | 138 | break; |
| cittecla | 38:3526c36e4c73 | 139 | case 53: |
| cittecla | 38:3526c36e4c73 | 140 | state = arm_position_release(); |
| aeschsim | 63:87b615e4a842 | 141 | state = 55; |
| aeschsim | 63:87b615e4a842 | 142 | wait_ms(1000); |
| cittecla | 38:3526c36e4c73 | 143 | break; |
| cittecla | 38:3526c36e4c73 | 144 | |
| cittecla | 38:3526c36e4c73 | 145 | case 55: |
| cittecla | 61:b57577b0072f | 146 | state = open_grabber(); |
| aeschsim | 63:87b615e4a842 | 147 | state = 52; |
| aeschsim | 63:87b615e4a842 | 148 | wait_ms(1000); |
| cittecla | 38:3526c36e4c73 | 149 | break; |
| cittecla | 38:3526c36e4c73 | 150 | case 56: |
| cittecla | 38:3526c36e4c73 | 151 | state = close_grabber(); |
| aeschsim | 63:87b615e4a842 | 152 | state = 53; |
| aeschsim | 63:87b615e4a842 | 153 | wait_ms(1000); |
| cittecla | 38:3526c36e4c73 | 154 | break; |
| aeschsim | 80:956f65714207 | 155 | |
| aeschsim | 80:956f65714207 | 156 | /******************************************************************************/ |
| aeschsim | 80:956f65714207 | 157 | //Testtools |
| aeschsim | 80:956f65714207 | 158 | |
| aeschsim | 80:956f65714207 | 159 | case 100: //Testtool for IR sensors |
| cittecla | 79:92b9d083322d | 160 | printf("sensor 0: %f \r\n", getDistanceIR(0)); |
| cittecla | 79:92b9d083322d | 161 | printf("sensor 1: %f \r\n", getDistanceIR(1)); |
| cittecla | 79:92b9d083322d | 162 | printf("sensor 2: %f \r\n", getDistanceIR(2)); |
| cittecla | 79:92b9d083322d | 163 | printf("sensor 3: %f \r\n", getDistanceIR(3)); |
| cittecla | 79:92b9d083322d | 164 | printf("sensor 4: %f \r\n", getDistanceIR(4)); |
| cittecla | 79:92b9d083322d | 165 | printf("sensor 5: %f \r\n\n", getDistanceIR(5)); |
| cittecla | 79:92b9d083322d | 166 | state = 100; |
| cittecla | 79:92b9d083322d | 167 | break; |
| aeschsim | 80:956f65714207 | 168 | case 101: //Testtool for get_color |
| aeschsim | 80:956f65714207 | 169 | char str1[] = "GREEN"; |
| aeschsim | 80:956f65714207 | 170 | char str2[] = "RED"; |
| aeschsim | 80:956f65714207 | 171 | printf("Color: %s\r\n",get_color()? str1 : str2); |
| aeschsim | 80:956f65714207 | 172 | wait_ms(500); |
| aeschsim | 80:956f65714207 | 173 | state = 101; |
| aeschsim | 80:956f65714207 | 174 | |
| aeschsim | 80:956f65714207 | 175 | case 102: //Testtool for grabbing functions |
| aeschsim | 81:a6f0f827dcb0 | 176 | arm_position_release(); |
| aeschsim | 81:a6f0f827dcb0 | 177 | wait_ms(500); |
| aeschsim | 80:956f65714207 | 178 | open_grabber(); |
| aeschsim | 80:956f65714207 | 179 | wait_ms(500); |
| aeschsim | 80:956f65714207 | 180 | arm_position_grabbing(); |
| aeschsim | 81:a6f0f827dcb0 | 181 | wait_ms(1000); |
| aeschsim | 80:956f65714207 | 182 | //move_forward_for_distance(5.0); |
| aeschsim | 80:956f65714207 | 183 | close_grabber(); |
| aeschsim | 80:956f65714207 | 184 | wait_ms(500); |
| aeschsim | 81:a6f0f827dcb0 | 185 | if (get_color() == 0) { |
| aeschsim | 81:a6f0f827dcb0 | 186 | open_grabber(); |
| aeschsim | 81:a6f0f827dcb0 | 187 | } |
| aeschsim | 80:956f65714207 | 188 | wait_ms(500); |
| aeschsim | 80:956f65714207 | 189 | state = 102; |
| cittecla | 32:777976c4d733 | 190 | } |
| cittecla | 32:777976c4d733 | 191 | } |
| cittecla | 71:ddf4eb5c3081 | 192 | } |
