Bluetooth program for the Seeed bot

Dependencies:   BluetoothSerial SeeedShieldBot mbed

Fork of Seeed_BlueBot_demo by ST

Committer:
bcostm
Date:
Thu May 21 08:49:34 2015 +0000
Revision:
5:de5b38436960
Parent:
4:5dd60bfc3cdd
Child:
6:a15e7918258f
- Show how to use another Nucleo board (L053R8 for example)
; - Add commands

Who changed what in which revision?

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