![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Code to drive Team 1's robot for the 2016 R5 robotics competition.
Dependencies: mbed navigation R5_StepperDrive LongRangeSensor DistanceSensor scanner Gripper ColorSensor
r5driver.cpp@28:17c17157e728, 2016-03-22 (annotated)
- Committer:
- j_j205
- Date:
- Tue Mar 22 18:11:56 2016 +0000
- Revision:
- 28:17c17157e728
- Parent:
- 26:d99d078921a0
- Child:
- 29:e77e8891d985
3/22/16 jj
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j_j205 | 8:90af8914ce03 | 1 | #include "navigation.h" |
j_j205 | 15:d8940756d5d5 | 2 | #include "scanner.h" |
j_j205 | 12:e9f878ced6e7 | 3 | #include "StepperDrive.h" |
j_j205 | 15:d8940756d5d5 | 4 | #include "VL6180x.h" |
j_j205 | 8:90af8914ce03 | 5 | #include "stdint.h" |
j_j205 | 2:3b162e764c9d | 6 | #include "mbed.h" |
j_j205 | 0:a3c39d3359ac | 7 | |
j_j205 | 0:a3c39d3359ac | 8 | Serial pc(USBTX, USBRX); |
j_j205 | 28:17c17157e728 | 9 | // Serial pc(PTE16,PTE17); // bluetooth |
j_j205 | 12:e9f878ced6e7 | 10 | InterruptIn start(SW1); |
j_j205 | 12:e9f878ced6e7 | 11 | DigitalOut led_red(LED_RED); |
j_j205 | 12:e9f878ced6e7 | 12 | DigitalOut led_green(LED_GREEN); |
j_j205 | 12:e9f878ced6e7 | 13 | bool active = false; |
j_j205 | 12:e9f878ced6e7 | 14 | |
j_j205 | 12:e9f878ced6e7 | 15 | void activate(); |
j_j205 | 0:a3c39d3359ac | 16 | |
j_j205 | 0:a3c39d3359ac | 17 | int main() |
j_j205 | 0:a3c39d3359ac | 18 | { |
j_j205 | 0:a3c39d3359ac | 19 | pc.baud(115200); |
j_j205 | 28:17c17157e728 | 20 | // pc.baud(9600); /* interface via Bluetooth at 9600 */ |
j_j205 | 12:e9f878ced6e7 | 21 | start.mode(PullUp); /* Start/Stop button is active low needing PullUp */ |
j_j205 | 12:e9f878ced6e7 | 22 | start.fall(&activate); |
j_j205 | 20:53ab414d7b33 | 23 | |
j_j205 | 12:e9f878ced6e7 | 24 | led_red = 0; |
j_j205 | 12:e9f878ced6e7 | 25 | led_green = 1; |
j_j205 | 20:53ab414d7b33 | 26 | |
j_j205 | 25:670a59096bf8 | 27 | pc.printf("\nWaiting for START BUTTON\n"); |
j_j205 | 25:670a59096bf8 | 28 | while(!active) // wait for start_button |
j_j205 | 25:670a59096bf8 | 29 | { |
j_j205 | 25:670a59096bf8 | 30 | wait(1e-6); |
j_j205 | 25:670a59096bf8 | 31 | } |
j_j205 | 25:670a59096bf8 | 32 | |
j_j205 | 25:670a59096bf8 | 33 | StepperDrive drive(pc, PTE19, PTE18, 0, PTE3, PTE2, 1, 10.0625, 8.3750, 500); |
j_j205 | 25:670a59096bf8 | 34 | //(serial &, stepPinLeft, dirPinLeft, invertLeft, stepPinRight, dirPinRight, invertRight, wheelCircum, wheelSepar, periodUs) |
j_j205 | 25:670a59096bf8 | 35 | |
j_j205 | 25:670a59096bf8 | 36 | VL6180x shortRangeL(PTE0, PTE1, 0x52); // I2C1, 0x29 left-shifted by 1 bit |
j_j205 | 25:670a59096bf8 | 37 | VL6180x shortRangeR(PTC9, PTC8, 0x52); // I2C0, 0x29 left-shifted by 1 bit |
j_j205 | 26:d99d078921a0 | 38 | LongRangeSensor longRangeL(pc, PTB2); |
j_j205 | 26:d99d078921a0 | 39 | LongRangeSensor longRangeR(pc, PTB3); |
j_j205 | 25:670a59096bf8 | 40 | |
j_j205 | 25:670a59096bf8 | 41 | Scanner scanner(pc, PTB0, PTB1, shortRangeL, shortRangeR, longRangeL, longRangeR, 0.075); |
j_j205 | 25:670a59096bf8 | 42 | // (Serial &, PinName servoL, PinName servoR, shortRangeL, shortRangeR, longRangeL, longRangeR, period) |
j_j205 | 25:670a59096bf8 | 43 | |
j_j205 | 28:17c17157e728 | 44 | Navigation r5nav(scanner, 11); |
j_j205 | 8:90af8914ce03 | 45 | wait(0.1); |
j_j205 | 20:53ab414d7b33 | 46 | |
j_j205 | 25:670a59096bf8 | 47 | r5nav.addGraphNode(0, 1, 10.0625, 0); |
j_j205 | 25:670a59096bf8 | 48 | r5nav.addGraphNode(1, 0, 10.0625, 180); |
j_j205 | 25:670a59096bf8 | 49 | r5nav.addGraphNode(1, 2, 10.0625, 270); |
j_j205 | 25:670a59096bf8 | 50 | r5nav.addGraphNode(2, 1, 10.0625, 90); |
j_j205 | 25:670a59096bf8 | 51 | r5nav.addGraphNode(2, 3, 10.0625, 180); |
j_j205 | 25:670a59096bf8 | 52 | r5nav.addGraphNode(3, 2, 10.0625, 0); |
j_j205 | 25:670a59096bf8 | 53 | r5nav.addGraphNode(3, 0, 10.0625, 90); |
j_j205 | 25:670a59096bf8 | 54 | r5nav.addGraphNode(0, 3, 10.0625, 270); |
j_j205 | 1:1958e6db41fc | 55 | |
j_j205 | 15:d8940756d5d5 | 56 | /* |
j_j205 | 20:53ab414d7b33 | 57 | // loading r5 map |
j_j205 | 8:90af8914ce03 | 58 | r5nav.addGraphNode(0, 1, 18, 0); |
j_j205 | 8:90af8914ce03 | 59 | r5nav.addGraphNode(1, 0, 18, 180); |
j_j205 | 12:e9f878ced6e7 | 60 | r5nav.addGraphNode(1, 2, 72, 0); |
j_j205 | 12:e9f878ced6e7 | 61 | r5nav.addGraphNode(1, 3, 13.5, 90); |
j_j205 | 8:90af8914ce03 | 62 | r5nav.addGraphNode(2, 1, 72, 180); |
j_j205 | 12:e9f878ced6e7 | 63 | r5nav.addGraphNode(3, 1, 13.5, 270); |
j_j205 | 8:90af8914ce03 | 64 | r5nav.addGraphNode(3, 4, 12, 180); |
j_j205 | 8:90af8914ce03 | 65 | r5nav.addGraphNode(3, 5, 23, 0); |
j_j205 | 8:90af8914ce03 | 66 | r5nav.addGraphNode(4, 3, 12, 0); |
j_j205 | 8:90af8914ce03 | 67 | r5nav.addGraphNode(5, 3, 23, 180); |
j_j205 | 8:90af8914ce03 | 68 | r5nav.addGraphNode(5, 6, 37, 0); |
j_j205 | 8:90af8914ce03 | 69 | r5nav.addGraphNode(5, 8, 12, 90); |
j_j205 | 8:90af8914ce03 | 70 | r5nav.addGraphNode(6, 5, 37, 180); |
j_j205 | 8:90af8914ce03 | 71 | r5nav.addGraphNode(6, 7, 12, 0); |
j_j205 | 8:90af8914ce03 | 72 | r5nav.addGraphNode(7, 6, 12, 180); |
j_j205 | 8:90af8914ce03 | 73 | r5nav.addGraphNode(8, 5, 12, 270); |
j_j205 | 8:90af8914ce03 | 74 | r5nav.addGraphNode(8, 9, 35, 180); |
j_j205 | 8:90af8914ce03 | 75 | r5nav.addGraphNode(9, 8, 35, 0); |
j_j205 | 28:17c17157e728 | 76 | r5nav.addGraphNode(8, 10, 37, 0); |
j_j205 | 28:17c17157e728 | 77 | r5nav.addGraphNode(6, 10, 12, 90); |
j_j205 | 28:17c17157e728 | 78 | r5nav.addGraphNode(10, 6, 12, 270); |
j_j205 | 28:17c17157e728 | 79 | r5nav.addGraphNode(8, 11, 12, 90); |
j_j205 | 28:17c17157e728 | 80 | r5nav.addGraphNode(11, 8, 12, 270); |
j_j205 | 28:17c17157e728 | 81 | r5nav.addGraphNode(11, 12, 35, 180); |
j_j205 | 28:17c17157e728 | 82 | r5nav.addGraphNode(12, 11, 35, 0); |
j_j205 | 28:17c17157e728 | 83 | r5nav.addGraphNode(11, 13, 37, 0); |
j_j205 | 28:17c17157e728 | 84 | r5nav.addGraphNode(13, 11, 37, 180); |
j_j205 | 28:17c17157e728 | 85 | r5nav.addGraphNode(13, 14, 12, 0); |
j_j205 | 28:17c17157e728 | 86 | r5nav.addGraphNode(14, 13, 12, 180); |
j_j205 | 28:17c17157e728 | 87 | |
j_j205 | 8:90af8914ce03 | 88 | const uint16_t YELLOW_VICTIM = 7; |
j_j205 | 8:90af8914ce03 | 89 | const uint16_t RED_VICTIM = 9; |
j_j205 | 8:90af8914ce03 | 90 | const uint16_t YELLOW_DROP_ZONE = 4; |
j_j205 | 8:90af8914ce03 | 91 | const uint16_t RED_DROP_ZONE = 2; |
j_j205 | 15:d8940756d5d5 | 92 | */ |
j_j205 | 20:53ab414d7b33 | 93 | |
j_j205 | 15:d8940756d5d5 | 94 | r5nav.getShortestPath(1); |
j_j205 | 15:d8940756d5d5 | 95 | pc.printf("\n\nDistance from 0 to 1: %i\n", r5nav.getMinDist(1) ); |
j_j205 | 15:d8940756d5d5 | 96 | pc.printf("Route:\n"); |
j_j205 | 15:d8940756d5d5 | 97 | r5nav.executeRoute(pc, drive); |
j_j205 | 15:d8940756d5d5 | 98 | wait(0.1); |
j_j205 | 20:53ab414d7b33 | 99 | |
j_j205 | 15:d8940756d5d5 | 100 | r5nav.getShortestPath(2); |
j_j205 | 15:d8940756d5d5 | 101 | pc.printf("\n\nDistance from 1 to 2: %i\n", r5nav.getMinDist(2) ); |
j_j205 | 15:d8940756d5d5 | 102 | pc.printf("Route:\n"); |
j_j205 | 15:d8940756d5d5 | 103 | r5nav.executeRoute(pc, drive); |
j_j205 | 15:d8940756d5d5 | 104 | wait(0.1); |
j_j205 | 20:53ab414d7b33 | 105 | |
j_j205 | 15:d8940756d5d5 | 106 | r5nav.getShortestPath(3); |
j_j205 | 15:d8940756d5d5 | 107 | pc.printf("\n\nDistance from 2 to 3: %i\n", r5nav.getMinDist(3) ); |
j_j205 | 8:90af8914ce03 | 108 | pc.printf("Route:\n"); |
j_j205 | 15:d8940756d5d5 | 109 | r5nav.executeRoute(pc, drive); |
j_j205 | 15:d8940756d5d5 | 110 | wait(0.1); |
j_j205 | 20:53ab414d7b33 | 111 | |
j_j205 | 15:d8940756d5d5 | 112 | r5nav.getShortestPath(0); |
j_j205 | 15:d8940756d5d5 | 113 | pc.printf("\n\nDistance from 3 to 0: %i\n", r5nav.getMinDist(0) ); |
j_j205 | 15:d8940756d5d5 | 114 | pc.printf("Route:\n"); |
j_j205 | 15:d8940756d5d5 | 115 | r5nav.executeRoute(pc, drive); |
j_j205 | 15:d8940756d5d5 | 116 | wait(0.1); |
j_j205 | 20:53ab414d7b33 | 117 | |
j_j205 | 15:d8940756d5d5 | 118 | /* |
j_j205 | 15:d8940756d5d5 | 119 | r5nav.getShortestPath(YELLOW_VICTIM); |
j_j205 | 15:d8940756d5d5 | 120 | pc.printf("\n\nDistance from 0 to YELLOW_VICTIM: %i\n", r5nav.getMinDist(YELLOW_VICTIM) ); |
j_j205 | 15:d8940756d5d5 | 121 | pc.printf("Route:\n"); |
j_j205 | 15:d8940756d5d5 | 122 | //r5nav.printRoute(pc); |
j_j205 | 15:d8940756d5d5 | 123 | //r5nav.setVertex(YELLOW_VICTIM) |
j_j205 | 12:e9f878ced6e7 | 124 | r5nav.executeRoute(pc, drive); |
j_j205 | 8:90af8914ce03 | 125 | wait(0.1); |
j_j205 | 1:1958e6db41fc | 126 | |
j_j205 | 8:90af8914ce03 | 127 | r5nav.getShortestPath(YELLOW_DROP_ZONE); |
j_j205 | 15:d8940756d5d5 | 128 | pc.printf("\n\nDistance from %i to YELLOW_DROP_ZONE: %i\n", r5nav.getVertex(), r5nav.getMinDist(YELLOW_DROP_ZONE) ); |
j_j205 | 8:90af8914ce03 | 129 | pc.printf("Route:\n"); |
j_j205 | 15:d8940756d5d5 | 130 | //r5nav.printRoute(pc); |
j_j205 | 15:d8940756d5d5 | 131 | //r5nav.setVertex(YELLOW_DROP_ZONE); |
j_j205 | 12:e9f878ced6e7 | 132 | r5nav.executeRoute(pc, drive); |
j_j205 | 8:90af8914ce03 | 133 | wait(0.1); |
j_j205 | 1:1958e6db41fc | 134 | |
j_j205 | 8:90af8914ce03 | 135 | r5nav.getShortestPath(RED_VICTIM); |
j_j205 | 15:d8940756d5d5 | 136 | pc.printf("\n\nDistance from %i to RED_VICTIM: %i\n", r5nav.getVertex(), r5nav.getMinDist(RED_VICTIM) ); |
j_j205 | 8:90af8914ce03 | 137 | pc.printf("Route:\n"); |
j_j205 | 15:d8940756d5d5 | 138 | //r5nav.printRoute(pc); |
j_j205 | 15:d8940756d5d5 | 139 | //r5nav.setVertex(RED_VICTIM); |
j_j205 | 12:e9f878ced6e7 | 140 | r5nav.executeRoute(pc, drive); |
j_j205 | 8:90af8914ce03 | 141 | wait(0.1); |
j_j205 | 1:1958e6db41fc | 142 | |
j_j205 | 8:90af8914ce03 | 143 | r5nav.getShortestPath(RED_DROP_ZONE); |
j_j205 | 15:d8940756d5d5 | 144 | pc.printf("\n\nDistance from %i to RED_DROP_ZONE: %i\n", r5nav.getVertex(), r5nav.getMinDist(RED_DROP_ZONE) ); |
j_j205 | 8:90af8914ce03 | 145 | pc.printf("Route:\n"); |
j_j205 | 15:d8940756d5d5 | 146 | //r5nav.printRoute(pc); |
j_j205 | 15:d8940756d5d5 | 147 | //r5nav.setVertex(RED_DROP_ZONE); |
j_j205 | 12:e9f878ced6e7 | 148 | r5nav.executeRoute(pc, drive); |
j_j205 | 8:90af8914ce03 | 149 | wait(0.1); |
j_j205 | 15:d8940756d5d5 | 150 | */ |
j_j205 | 20:53ab414d7b33 | 151 | |
j_j205 | 15:d8940756d5d5 | 152 | drive.move(0, ((90.0)*(3.14159 / 180.0))); // return to initial angle |
j_j205 | 15:d8940756d5d5 | 153 | // wait for move to complete |
j_j205 | 15:d8940756d5d5 | 154 | while(!drive.isMoveDone()) |
j_j205 | 15:d8940756d5d5 | 155 | { |
j_j205 | 15:d8940756d5d5 | 156 | wait(1e-6); |
j_j205 | 15:d8940756d5d5 | 157 | } |
j_j205 | 20:53ab414d7b33 | 158 | |
j_j205 | 15:d8940756d5d5 | 159 | pc.printf("\n\nExercise Complete"); |
j_j205 | 0:a3c39d3359ac | 160 | return 0; |
j_j205 | 12:e9f878ced6e7 | 161 | } |
j_j205 | 12:e9f878ced6e7 | 162 | |
j_j205 | 12:e9f878ced6e7 | 163 | void activate() |
j_j205 | 12:e9f878ced6e7 | 164 | { |
j_j205 | 12:e9f878ced6e7 | 165 | led_red = 1; |
j_j205 | 12:e9f878ced6e7 | 166 | led_green = 0; |
j_j205 | 12:e9f878ced6e7 | 167 | active = true; |
j_j205 | 25:670a59096bf8 | 168 | } |