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