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@127:6bde4483ce7b, 2017-05-14 (annotated)
- Committer:
- cittecla
- Date:
- Sun May 14 14:40:12 2017 +0000
- Revision:
- 127:6bde4483ce7b
- Parent:
- 125:d0b2057272d0
- Child:
- 130:0d6c072b23ca
- Child:
- 131:8ae08f41bb43
included path finding into state machine
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 | 127:6bde4483ce7b | 5 | int old_state = 0; |
| cittecla | 32:777976c4d733 | 6 | bool positioning_state = 0; |
| cittecla | 32:777976c4d733 | 7 | bool mapping_state = 0; |
| cittecla | 31:51f52ffa4b51 | 8 | |
| cittecla | 71:ddf4eb5c3081 | 9 | Timer game_timer; |
| cittecla | 71:ddf4eb5c3081 | 10 | |
| cittecla | 93:837a13760026 | 11 | |
| cittecla | 32:777976c4d733 | 12 | int main() |
| cittecla | 32:777976c4d733 | 13 | { |
| cittecla | 61:b57577b0072f | 14 | printf("start...\r\n"); |
| cittecla | 94:0381e8b1beda | 15 | disable_servos(); |
| cittecla | 71:ddf4eb5c3081 | 16 | while (game_timer.read() < 300) { |
| cittecla | 64:c2fcf3b349e9 | 17 | wait(0.01f); |
| cittecla | 34:40d8d29b44b8 | 18 | // 5min |
| cittecla | 33:8a98f8b9d859 | 19 | //******************************************************************************* |
| cittecla | 33:8a98f8b9d859 | 20 | //Non_state machine driven function |
| cittecla | 33:8a98f8b9d859 | 21 | //This functions will be called every cycle, use for safety and sensor functipons |
| cittecla | 33:8a98f8b9d859 | 22 | //******************************************************************************* |
| cittecla | 32:777976c4d733 | 23 | |
| cittecla | 38:3526c36e4c73 | 24 | safety(); |
| cittecla | 31:51f52ffa4b51 | 25 | |
| cittecla | 38:3526c36e4c73 | 26 | |
| cittecla | 32:777976c4d733 | 27 | |
| cittecla | 32:777976c4d733 | 28 | if(positioning_state) { |
| cittecla | 38:3526c36e4c73 | 29 | positioning(); |
| cittecla | 32:777976c4d733 | 30 | } |
| cittecla | 32:777976c4d733 | 31 | if(mapping_state) { |
| cittecla | 38:3526c36e4c73 | 32 | mapping(); |
| cittecla | 32:777976c4d733 | 33 | } |
| cittecla | 18:a82994e67297 | 34 | |
| cittecla | 33:8a98f8b9d859 | 35 | //******************************************************************************* |
| cittecla | 33:8a98f8b9d859 | 36 | //state machine driven function |
| cittecla | 33:8a98f8b9d859 | 37 | //This functions will only be called when their state is active. |
| cittecla | 33:8a98f8b9d859 | 38 | //The state machine will be in the same state until this action is completed or |
| cittecla | 33:8a98f8b9d859 | 39 | //a safty function kicks in and stops the current function. |
| cittecla | 33:8a98f8b9d859 | 40 | //Every function will return the next active state upon its transition table. |
| cittecla | 33:8a98f8b9d859 | 41 | //State and Transition Table can be found in the State_Machine.xlsx |
| cittecla | 33:8a98f8b9d859 | 42 | //******************************************************************************* |
| cittecla | 113:c8ea0cd9b20c | 43 | |
| cittecla | 127:6bde4483ce7b | 44 | if(state != old_state){ |
| cittecla | 127:6bde4483ce7b | 45 | printf("state: %d\r\n",state); |
| cittecla | 127:6bde4483ce7b | 46 | old_state = state; |
| cittecla | 127:6bde4483ce7b | 47 | } |
| cittecla | 32:777976c4d733 | 48 | switch (state) { |
| cittecla | 38:3526c36e4c73 | 49 | case 0: |
| cittecla | 38:3526c36e4c73 | 50 | state = emergency_shutdown(); |
| cittecla | 38:3526c36e4c73 | 51 | break; |
| cittecla | 38:3526c36e4c73 | 52 | case 1: |
| cittecla | 38:3526c36e4c73 | 53 | state = colision_detected(); |
| cittecla | 38:3526c36e4c73 | 54 | break; |
| cittecla | 38:3526c36e4c73 | 55 | case 2: |
| cittecla | 38:3526c36e4c73 | 56 | state = current_to_high(); |
| cittecla | 38:3526c36e4c73 | 57 | break; |
| cittecla | 38:3526c36e4c73 | 58 | case 3: |
| cittecla | 38:3526c36e4c73 | 59 | state = overheating(); |
| cittecla | 38:3526c36e4c73 | 60 | break; |
| cittecla | 31:51f52ffa4b51 | 61 | |
| cittecla | 38:3526c36e4c73 | 62 | case 10: |
| cittecla | 71:ddf4eb5c3081 | 63 | state = idle(); |
| cittecla | 71:ddf4eb5c3081 | 64 | printf("idle1...\r\n"); |
| cittecla | 38:3526c36e4c73 | 65 | break; |
| cittecla | 38:3526c36e4c73 | 66 | case 11: |
| cittecla | 71:ddf4eb5c3081 | 67 | state = idle2(); |
| cittecla | 71:ddf4eb5c3081 | 68 | printf("idle2...\r\n"); |
| cittecla | 38:3526c36e4c73 | 69 | break; |
| cittecla | 38:3526c36e4c73 | 70 | |
| cittecla | 38:3526c36e4c73 | 71 | case 15: |
| aeschsim | 78:d9138c6f82a8 | 72 | state = initialisation(); //init servo is included |
| cittecla | 38:3526c36e4c73 | 73 | break; |
| cittecla | 38:3526c36e4c73 | 74 | case 16: |
| cittecla | 79:92b9d083322d | 75 | state = inital_arm_positioning(); |
| cittecla | 120:cdf7a6751f9e | 76 | //state = 47; // change state here for test cases |
| cittecla | 38:3526c36e4c73 | 77 | break; |
| cittecla | 71:ddf4eb5c3081 | 78 | case 17: |
| cittecla | 79:92b9d083322d | 79 | state = initial_positioning(); |
| cittecla | 71:ddf4eb5c3081 | 80 | break; |
| cittecla | 31:51f52ffa4b51 | 81 | |
| cittecla | 38:3526c36e4c73 | 82 | case 25: |
| cittecla | 58:1a395b6928ee | 83 | state = 26; |
| cittecla | 71:ddf4eb5c3081 | 84 | game_timer.start(); |
| cittecla | 38:3526c36e4c73 | 85 | break; |
| cittecla | 38:3526c36e4c73 | 86 | case 26: |
| cittecla | 58:1a395b6928ee | 87 | state = 27; |
| cittecla | 71:ddf4eb5c3081 | 88 | positioning_state = 1; |
| cittecla | 71:ddf4eb5c3081 | 89 | break; |
| cittecla | 71:ddf4eb5c3081 | 90 | case 27: |
| cittecla | 71:ddf4eb5c3081 | 91 | state = 28; |
| cittecla | 38:3526c36e4c73 | 92 | mapping_state = 1; |
| cittecla | 38:3526c36e4c73 | 93 | break; |
| cittecla | 71:ddf4eb5c3081 | 94 | case 28: |
| cittecla | 38:3526c36e4c73 | 95 | state = initial_turn(); |
| cittecla | 38:3526c36e4c73 | 96 | break; |
| cittecla | 38:3526c36e4c73 | 97 | |
| cittecla | 40:a46667b62671 | 98 | |
| cittecla | 38:3526c36e4c73 | 99 | case 35: |
| cittecla | 38:3526c36e4c73 | 100 | state = select_target(); |
| cittecla | 38:3526c36e4c73 | 101 | break; |
| cittecla | 38:3526c36e4c73 | 102 | case 36: |
| cittecla | 38:3526c36e4c73 | 103 | state = pathfinding(); |
| cittecla | 38:3526c36e4c73 | 104 | break; |
| cittecla | 38:3526c36e4c73 | 105 | case 37: |
| cittecla | 41:462d379e85c4 | 106 | state = switch_target_red(); |
| cittecla | 41:462d379e85c4 | 107 | break; |
| cittecla | 31:51f52ffa4b51 | 108 | |
| cittecla | 31:51f52ffa4b51 | 109 | |
| cittecla | 38:3526c36e4c73 | 110 | case 40: |
| cittecla | 38:3526c36e4c73 | 111 | state = moving(); |
| cittecla | 38:3526c36e4c73 | 112 | break; |
| cittecla | 54:453f24775644 | 113 | |
| cittecla | 38:3526c36e4c73 | 114 | |
| cittecla | 122:8bfb39434fb7 | 115 | case 45: |
| cittecla | 122:8bfb39434fb7 | 116 | state = move_to_brick_by_coordlist(); |
| cittecla | 38:3526c36e4c73 | 117 | break; |
| cittecla | 38:3526c36e4c73 | 118 | case 47: |
| cittecla | 38:3526c36e4c73 | 119 | state = move_in_search_for_brick(); |
| cittecla | 38:3526c36e4c73 | 120 | break; |
| cittecla | 87:df8c869a5a52 | 121 | case 48: |
| cittecla | 87:df8c869a5a52 | 122 | state = generate_fake_target(); |
| cittecla | 87:df8c869a5a52 | 123 | break; |
| cittecla | 31:51f52ffa4b51 | 124 | |
| cittecla | 31:51f52ffa4b51 | 125 | |
| cittecla | 38:3526c36e4c73 | 126 | case 50: |
| cittecla | 38:3526c36e4c73 | 127 | state = grabbing(); |
| cittecla | 38:3526c36e4c73 | 128 | break; |
| cittecla | 38:3526c36e4c73 | 129 | |
| cittecla | 93:837a13760026 | 130 | |
| cittecla | 87:df8c869a5a52 | 131 | |
| cittecla | 87:df8c869a5a52 | 132 | /******************************************************************************/ |
| aeschsim | 80:956f65714207 | 133 | //Testtools |
| aeschsim | 80:956f65714207 | 134 | |
| aeschsim | 80:956f65714207 | 135 | case 100: //Testtool for IR sensors |
| cittecla | 117:66d64dbd1b36 | 136 | //printf("sensor 0: %f \r\n", getDistanceIR(0)); |
| cittecla | 117:66d64dbd1b36 | 137 | // printf("sensor 1: %f \r\n", getDistanceIR(1)); |
| cittecla | 117:66d64dbd1b36 | 138 | printf("sensor oben: %f \r\n", getDistanceIR(2)); |
| cittecla | 117:66d64dbd1b36 | 139 | printf("sensor unten: %f \r\n\n", getDistanceIR(3)); |
| cittecla | 117:66d64dbd1b36 | 140 | // printf("sensor 4: %f \r\n", getDistanceIR(4)); |
| cittecla | 117:66d64dbd1b36 | 141 | // printf("sensor 5: %f \r\n\n", getDistanceIR(5)); |
| cittecla | 117:66d64dbd1b36 | 142 | wait(0.1f); |
| cittecla | 79:92b9d083322d | 143 | state = 100; |
| cittecla | 79:92b9d083322d | 144 | break; |
| aeschsim | 80:956f65714207 | 145 | case 101: //Testtool for get_color |
| aeschsim | 80:956f65714207 | 146 | char str1[] = "GREEN"; |
| aeschsim | 80:956f65714207 | 147 | char str2[] = "RED"; |
| aeschsim | 80:956f65714207 | 148 | printf("Color: %s\r\n",get_color()? str1 : str2); |
| aeschsim | 80:956f65714207 | 149 | wait_ms(500); |
| aeschsim | 80:956f65714207 | 150 | state = 101; |
| cittecla | 87:df8c869a5a52 | 151 | break; |
| cittecla | 94:0381e8b1beda | 152 | case 102: |
| cittecla | 113:c8ea0cd9b20c | 153 | move_for_distance_with_radius(0.5f,-0.4); |
| cittecla | 94:0381e8b1beda | 154 | state = 103; |
| cittecla | 94:0381e8b1beda | 155 | break; |
| cittecla | 94:0381e8b1beda | 156 | case 103: |
| cittecla | 107:02bc5b4e67b7 | 157 | float distance = move_for_distance_with_radius(0,0); |
| cittecla | 107:02bc5b4e67b7 | 158 | if(distance < 0) { |
| cittecla | 107:02bc5b4e67b7 | 159 | //printf("remaining deg %f\r\n", deg); |
| cittecla | 94:0381e8b1beda | 160 | stop_turn(); |
| cittecla | 113:c8ea0cd9b20c | 161 | state = 104; |
| cittecla | 94:0381e8b1beda | 162 | } |
| aeschsim | 96:8da1c57296ff | 163 | break; |
| cittecla | 113:c8ea0cd9b20c | 164 | case 104: |
| cittecla | 113:c8ea0cd9b20c | 165 | move_for_distance_with_radius(0.5f,0.4); |
| cittecla | 113:c8ea0cd9b20c | 166 | state = 105; |
| cittecla | 113:c8ea0cd9b20c | 167 | break; |
| cittecla | 113:c8ea0cd9b20c | 168 | case 105: |
| cittecla | 113:c8ea0cd9b20c | 169 | float distance2 = move_for_distance_with_radius(0,0); |
| cittecla | 113:c8ea0cd9b20c | 170 | if(distance2 < 0) { |
| cittecla | 113:c8ea0cd9b20c | 171 | //printf("remaining deg %f\r\n", deg); |
| cittecla | 113:c8ea0cd9b20c | 172 | stop_turn(); |
| cittecla | 113:c8ea0cd9b20c | 173 | state = 106; |
| cittecla | 113:c8ea0cd9b20c | 174 | } |
| cittecla | 113:c8ea0cd9b20c | 175 | break; |
| cittecla | 113:c8ea0cd9b20c | 176 | case 106: |
| cittecla | 113:c8ea0cd9b20c | 177 | coordinates co = get_current_coord(); |
| cittecla | 113:c8ea0cd9b20c | 178 | printf("current_coord: %f || %f\r\n", co.x, co.y); |
| aeschsim | 99:78d87027c85b | 179 | |
| cittecla | 32:777976c4d733 | 180 | } |
| cittecla | 32:777976c4d733 | 181 | } |
| cittecla | 71:ddf4eb5c3081 | 182 | } |
