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