Bluetooth program for the Seeed bot
Dependencies: BluetoothSerial SeeedShieldBot mbed
Fork of Seeed_BlueBot_demo by
main.cpp@4:5dd60bfc3cdd, 2014-10-24 (annotated)
- Committer:
- bcostm
- Date:
- Fri Oct 24 08:27:19 2014 +0000
- Revision:
- 4:5dd60bfc3cdd
- Parent:
- 3:68fe5b9e069a
- Child:
- 5:de5b38436960
Add option to disable the bot front sensors reading (default is enabled, wire A5 to GND to disable it)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 0:fa6bc104fe2d | 1 | #include "mbed.h" |
bcostm | 0:fa6bc104fe2d | 2 | #include "BluetoothSerial.h" |
bcostm | 0:fa6bc104fe2d | 3 | #include "SeeedStudioShieldBot.h" |
bcostm | 0:fa6bc104fe2d | 4 | |
bcostm | 0:fa6bc104fe2d | 5 | // The following configuration must be done on the NUCLEO board: |
bcostm | 0:fa6bc104fe2d | 6 | // - Close SB62/SB63 and open SB13/SB14 solder bridges to enable the D0/D1 pins |
bcostm | 0:fa6bc104fe2d | 7 | // - Open SB21 solder bridge to disconnect the LED |
bcostm | 0:fa6bc104fe2d | 8 | |
bcostm | 0:fa6bc104fe2d | 9 | BluetoothSerial bluetooth(D1, D0); // TX, RX |
bcostm | 0:fa6bc104fe2d | 10 | |
bcostm | 0:fa6bc104fe2d | 11 | SeeedStudioShieldBot bot( |
bcostm | 0:fa6bc104fe2d | 12 | D8, D9, D11, // Right motor pins (PwmOut, DigitalOut, DigitalOut) -> Motor 1 |
bcostm | 0:fa6bc104fe2d | 13 | D12, D10, D13, // Left motor pins (PwmOut, DigitalOut, DigitalOut) -> Motor 2 |
bcostm | 3:68fe5b9e069a | 14 | A0, A1, A2, A3, A4 // Sensors pins (all DigitalIn) |
bcostm | 0:fa6bc104fe2d | 15 | ); |
bcostm | 0:fa6bc104fe2d | 16 | |
bcostm | 4:5dd60bfc3cdd | 17 | // Disable the feature by wiring A5 to GND |
bcostm | 4:5dd60bfc3cdd | 18 | DigitalIn ReadSensorsEnabled(A5, PullUp); |
bcostm | 4:5dd60bfc3cdd | 19 | |
bcostm | 0:fa6bc104fe2d | 20 | // Enable it for debugging on hyperterminal |
bcostm | 4:5dd60bfc3cdd | 21 | #define DEBUG 0 |
bcostm | 0:fa6bc104fe2d | 22 | #if DEBUG == 1 |
bcostm | 0:fa6bc104fe2d | 23 | Serial pc(PC_10, PC_11); |
bcostm | 0:fa6bc104fe2d | 24 | #define PC_DEBUG(args...) pc.printf(args) |
bcostm | 0:fa6bc104fe2d | 25 | #else |
bcostm | 0:fa6bc104fe2d | 26 | #define PC_DEBUG(args...) |
bcostm | 0:fa6bc104fe2d | 27 | #endif |
bcostm | 0:fa6bc104fe2d | 28 | |
bcostm | 0:fa6bc104fe2d | 29 | Ticker tick; |
bcostm | 0:fa6bc104fe2d | 30 | |
bcostm | 3:68fe5b9e069a | 31 | float speed = 1.0; // Used to select the motors speed |
bcostm | 3:68fe5b9e069a | 32 | int stop = 0; // Used to stop the motors when a sensor detects something |
bcostm | 3:68fe5b9e069a | 33 | |
bcostm | 0:fa6bc104fe2d | 34 | void ReadCommand(void) |
bcostm | 0:fa6bc104fe2d | 35 | { |
bcostm | 0:fa6bc104fe2d | 36 | int cmd = 0; |
bcostm | 0:fa6bc104fe2d | 37 | PC_DEBUG(">>> Read command...\n"); |
bcostm | 3:68fe5b9e069a | 38 | |
bcostm | 0:fa6bc104fe2d | 39 | if (bluetooth.readable()) |
bcostm | 0:fa6bc104fe2d | 40 | { |
bcostm | 0:fa6bc104fe2d | 41 | cmd = bluetooth.getc(); |
bcostm | 0:fa6bc104fe2d | 42 | PC_DEBUG(">>> Bluetooth read [%c]\n", cmd); |
bcostm | 3:68fe5b9e069a | 43 | |
bcostm | 3:68fe5b9e069a | 44 | // Ignore the receive command (excepted "Backward") if a sensor has detected something. |
bcostm | 3:68fe5b9e069a | 45 | if ((stop) && (cmd != '2')) return; |
bcostm | 3:68fe5b9e069a | 46 | |
bcostm | 3:68fe5b9e069a | 47 | switch (cmd) |
bcostm | 0:fa6bc104fe2d | 48 | { |
bcostm | 3:68fe5b9e069a | 49 | case '1': // Forward |
bcostm | 3:68fe5b9e069a | 50 | bot.forward(speed); |
bcostm | 3:68fe5b9e069a | 51 | break; |
bcostm | 3:68fe5b9e069a | 52 | case '2': // Backward |
bcostm | 3:68fe5b9e069a | 53 | bot.backward(speed); |
bcostm | 3:68fe5b9e069a | 54 | break; |
bcostm | 3:68fe5b9e069a | 55 | case '3': // Left |
bcostm | 3:68fe5b9e069a | 56 | bot.left(speed); |
bcostm | 3:68fe5b9e069a | 57 | break; |
bcostm | 3:68fe5b9e069a | 58 | case '4': // Right |
bcostm | 3:68fe5b9e069a | 59 | bot.right(speed); |
bcostm | 3:68fe5b9e069a | 60 | break; |
bcostm | 3:68fe5b9e069a | 61 | case '5': // Turn left |
bcostm | 3:68fe5b9e069a | 62 | bot.turn_right(speed); |
bcostm | 3:68fe5b9e069a | 63 | break; |
bcostm | 3:68fe5b9e069a | 64 | case '6': // Turn right |
bcostm | 3:68fe5b9e069a | 65 | bot.turn_left(speed); |
bcostm | 3:68fe5b9e069a | 66 | break; |
bcostm | 3:68fe5b9e069a | 67 | case '7': // Slow |
bcostm | 3:68fe5b9e069a | 68 | speed = 0.4; |
bcostm | 3:68fe5b9e069a | 69 | break; |
bcostm | 3:68fe5b9e069a | 70 | case '8': // Fast |
bcostm | 3:68fe5b9e069a | 71 | speed = 1.0; |
bcostm | 3:68fe5b9e069a | 72 | break; |
bcostm | 3:68fe5b9e069a | 73 | default: // Stop |
bcostm | 3:68fe5b9e069a | 74 | bot.stopAll(); |
bcostm | 3:68fe5b9e069a | 75 | break; |
bcostm | 0:fa6bc104fe2d | 76 | } |
bcostm | 0:fa6bc104fe2d | 77 | } |
bcostm | 0:fa6bc104fe2d | 78 | } |
bcostm | 0:fa6bc104fe2d | 79 | |
bcostm | 0:fa6bc104fe2d | 80 | int main() |
bcostm | 0:fa6bc104fe2d | 81 | { |
bcostm | 0:fa6bc104fe2d | 82 | PC_DEBUG("\n\nSeeed Bluetooth shield test started.\n"); |
bcostm | 0:fa6bc104fe2d | 83 | |
bcostm | 0:fa6bc104fe2d | 84 | // Enable motors |
bcostm | 0:fa6bc104fe2d | 85 | bot.enable_right_motor(); |
bcostm | 0:fa6bc104fe2d | 86 | bot.enable_left_motor(); |
bcostm | 0:fa6bc104fe2d | 87 | |
bcostm | 0:fa6bc104fe2d | 88 | PC_DEBUG(">>> Bluetooth setup..."); |
bcostm | 0:fa6bc104fe2d | 89 | bluetooth.setup(); |
bcostm | 0:fa6bc104fe2d | 90 | PC_DEBUG("done\n"); |
bcostm | 0:fa6bc104fe2d | 91 | |
bcostm | 0:fa6bc104fe2d | 92 | PC_DEBUG(">>> Bluetooth in slave mode..."); |
bcostm | 0:fa6bc104fe2d | 93 | bluetooth.slave("btslave8seeed"); // default PIN code: 0000 |
bcostm | 0:fa6bc104fe2d | 94 | PC_DEBUG("done\n"); |
bcostm | 0:fa6bc104fe2d | 95 | |
bcostm | 0:fa6bc104fe2d | 96 | wait(2); |
bcostm | 0:fa6bc104fe2d | 97 | |
bcostm | 0:fa6bc104fe2d | 98 | PC_DEBUG(">>> Bluetooth connect..."); |
bcostm | 0:fa6bc104fe2d | 99 | bluetooth.connect(); |
bcostm | 0:fa6bc104fe2d | 100 | PC_DEBUG("done\n"); |
bcostm | 0:fa6bc104fe2d | 101 | |
bcostm | 3:68fe5b9e069a | 102 | tick.attach(ReadCommand, 0.3); // Every 300 ms read Bluetooth command |
bcostm | 3:68fe5b9e069a | 103 | |
bcostm | 3:68fe5b9e069a | 104 | // Check if motors are alive |
bcostm | 3:68fe5b9e069a | 105 | bot.left(speed); |
bcostm | 3:68fe5b9e069a | 106 | wait(0.2); |
bcostm | 3:68fe5b9e069a | 107 | bot.right(speed); |
bcostm | 3:68fe5b9e069a | 108 | wait(0.2); |
bcostm | 3:68fe5b9e069a | 109 | bot.stopAll(); |
bcostm | 4:5dd60bfc3cdd | 110 | |
bcostm | 0:fa6bc104fe2d | 111 | while (1) { |
bcostm | 3:68fe5b9e069a | 112 | // Stop the motors if a sensor has detected something. |
bcostm | 4:5dd60bfc3cdd | 113 | if ((!bot.rightSensor || !bot.inRightSensor || !bot.centreSensor || !bot.inLeftSensor || !bot.leftSensor) && ReadSensorsEnabled) |
bcostm | 3:68fe5b9e069a | 114 | { |
bcostm | 3:68fe5b9e069a | 115 | if (!stop) bot.stopAll(); |
bcostm | 3:68fe5b9e069a | 116 | stop = 1; |
bcostm | 3:68fe5b9e069a | 117 | } |
bcostm | 3:68fe5b9e069a | 118 | else |
bcostm | 3:68fe5b9e069a | 119 | { |
bcostm | 3:68fe5b9e069a | 120 | stop = 0; |
bcostm | 3:68fe5b9e069a | 121 | } |
bcostm | 3:68fe5b9e069a | 122 | } |
bcostm | 0:fa6bc104fe2d | 123 | } |