Bluetooth program for the Seeed bot

Dependencies:   BluetoothSerial SeeedShieldBot mbed

Fork of Seeed_BlueBot_demo by ST

Committer:
omarrasheedk
Date:
Thu Aug 20 21:36:56 2015 +0000
Revision:
6:a15e7918258f
Parent:
5:de5b38436960
Seeed bot bluetooth program

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
omarrasheedk 6:a15e7918258f 11 /**SeeedStudioShieldBot bot(
bcostm 5:de5b38436960 12 PWM1, D9, D11, // Right motor pins (PwmOut, DigitalOut, DigitalOut) -> Motor 1
bcostm 5:de5b38436960 13 PWM2, 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 );
omarrasheedk 6:a15e7918258f 16 */
omarrasheedk 6:a15e7918258f 17 SeeedStudioShieldBot bot(
omarrasheedk 6:a15e7918258f 18 D8, D9, D11, // Left motor pins
omarrasheedk 6:a15e7918258f 19 D12, D10, D13, // Right motor pins
omarrasheedk 6:a15e7918258f 20 A0, A1, A2, A3, D4 // Sensors pins
omarrasheedk 6:a15e7918258f 21 );
bcostm 0:fa6bc104fe2d 22
omarrasheedk 6:a15e7918258f 23 DigitalOut led(D7);
bcostm 4:5dd60bfc3cdd 24 // Disable the feature by wiring A5 to GND
bcostm 4:5dd60bfc3cdd 25 DigitalIn ReadSensorsEnabled(A5, PullUp);
bcostm 4:5dd60bfc3cdd 26
bcostm 0:fa6bc104fe2d 27 // Enable it for debugging on hyperterminal
bcostm 4:5dd60bfc3cdd 28 #define DEBUG 0
bcostm 0:fa6bc104fe2d 29 #if DEBUG == 1
bcostm 5:de5b38436960 30 Serial pc(PC_10, PC_11); // Connect PC10 and CN3-RX
bcostm 0:fa6bc104fe2d 31 #define PC_DEBUG(args...) pc.printf(args)
bcostm 0:fa6bc104fe2d 32 #else
bcostm 0:fa6bc104fe2d 33 #define PC_DEBUG(args...)
bcostm 0:fa6bc104fe2d 34 #endif
bcostm 0:fa6bc104fe2d 35
bcostm 0:fa6bc104fe2d 36 Ticker tick;
bcostm 0:fa6bc104fe2d 37
bcostm 3:68fe5b9e069a 38 float speed = 1.0; // Used to select the motors speed
bcostm 3:68fe5b9e069a 39 int stop = 0; // Used to stop the motors when a sensor detects something
bcostm 3:68fe5b9e069a 40
bcostm 0:fa6bc104fe2d 41 void ReadCommand(void)
bcostm 0:fa6bc104fe2d 42 {
bcostm 0:fa6bc104fe2d 43 int cmd = 0;
bcostm 0:fa6bc104fe2d 44 PC_DEBUG(">>> Read command...\n");
bcostm 3:68fe5b9e069a 45
bcostm 0:fa6bc104fe2d 46 if (bluetooth.readable())
bcostm 0:fa6bc104fe2d 47 {
bcostm 0:fa6bc104fe2d 48 cmd = bluetooth.getc();
bcostm 0:fa6bc104fe2d 49 PC_DEBUG(">>> Bluetooth read [%c]\n", cmd);
bcostm 3:68fe5b9e069a 50
bcostm 3:68fe5b9e069a 51 // Ignore the receive command (excepted "Backward") if a sensor has detected something.
bcostm 3:68fe5b9e069a 52 if ((stop) && (cmd != '2')) return;
bcostm 3:68fe5b9e069a 53
bcostm 3:68fe5b9e069a 54 switch (cmd)
bcostm 0:fa6bc104fe2d 55 {
bcostm 3:68fe5b9e069a 56 case '1': // Forward
bcostm 3:68fe5b9e069a 57 bot.forward(speed);
bcostm 3:68fe5b9e069a 58 break;
bcostm 3:68fe5b9e069a 59 case '2': // Backward
bcostm 3:68fe5b9e069a 60 bot.backward(speed);
bcostm 3:68fe5b9e069a 61 break;
bcostm 3:68fe5b9e069a 62 case '3': // Left
bcostm 3:68fe5b9e069a 63 bot.left(speed);
bcostm 3:68fe5b9e069a 64 break;
bcostm 3:68fe5b9e069a 65 case '4': // Right
bcostm 3:68fe5b9e069a 66 bot.right(speed);
bcostm 3:68fe5b9e069a 67 break;
bcostm 5:de5b38436960 68 case '5': // Turn left forward
bcostm 3:68fe5b9e069a 69 bot.turn_right(speed);
bcostm 3:68fe5b9e069a 70 break;
bcostm 5:de5b38436960 71 case '6': // Turn right forward
bcostm 3:68fe5b9e069a 72 bot.turn_left(speed);
bcostm 3:68fe5b9e069a 73 break;
bcostm 5:de5b38436960 74 case '7': // Turn left backward
bcostm 5:de5b38436960 75 bot.turn_right(-speed);
bcostm 3:68fe5b9e069a 76 break;
bcostm 5:de5b38436960 77 case '8': // Turn right backward
bcostm 5:de5b38436960 78 bot.turn_left(-speed);
bcostm 5:de5b38436960 79 break;
bcostm 5:de5b38436960 80 case '9': // Slow
bcostm 5:de5b38436960 81 speed = 0.5;
bcostm 5:de5b38436960 82 break;
bcostm 5:de5b38436960 83 case 'A': // Fast
bcostm 3:68fe5b9e069a 84 speed = 1.0;
bcostm 3:68fe5b9e069a 85 break;
bcostm 3:68fe5b9e069a 86 default: // Stop
bcostm 3:68fe5b9e069a 87 bot.stopAll();
bcostm 3:68fe5b9e069a 88 break;
bcostm 0:fa6bc104fe2d 89 }
bcostm 0:fa6bc104fe2d 90 }
bcostm 0:fa6bc104fe2d 91 }
bcostm 0:fa6bc104fe2d 92
bcostm 0:fa6bc104fe2d 93 int main()
bcostm 0:fa6bc104fe2d 94 {
bcostm 0:fa6bc104fe2d 95 PC_DEBUG("\n\nSeeed Bluetooth shield test started.\n");
bcostm 0:fa6bc104fe2d 96
bcostm 0:fa6bc104fe2d 97 // Enable motors
bcostm 0:fa6bc104fe2d 98 bot.enable_right_motor();
bcostm 0:fa6bc104fe2d 99 bot.enable_left_motor();
omarrasheedk 6:a15e7918258f 100
bcostm 0:fa6bc104fe2d 101 PC_DEBUG(">>> Bluetooth setup...");
bcostm 0:fa6bc104fe2d 102 bluetooth.setup();
bcostm 0:fa6bc104fe2d 103 PC_DEBUG("done\n");
omarrasheedk 6:a15e7918258f 104
omarrasheedk 6:a15e7918258f 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");
omarrasheedk 6:a15e7918258f 115 while(1){led = !led;wait(0.1);}
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 }
omarrasheedk 6:a15e7918258f 137
omarrasheedk 6:a15e7918258f 138 }