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