Bluetooth program for the Seeed bot

Dependencies:   BluetoothSerial SeeedShieldBot mbed

Fork of Seeed_BlueBot_demo by ST

Committer:
bcostm
Date:
Mon Sep 08 08:22:07 2014 +0000
Revision:
0:fa6bc104fe2d
Child:
1:6a67ad238815
Initial version.

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 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 0:fa6bc104fe2d 14 A0, A1, A2, A3, A4 // Sensors pins (all DigitalIn)
bcostm 0:fa6bc104fe2d 15 );
bcostm 0:fa6bc104fe2d 16
bcostm 0:fa6bc104fe2d 17 // Enable it for debugging on hyperterminal
bcostm 0:fa6bc104fe2d 18 #define DEBUG 1
bcostm 0:fa6bc104fe2d 19 #if DEBUG == 1
bcostm 0:fa6bc104fe2d 20 Serial pc(PC_10, PC_11);
bcostm 0:fa6bc104fe2d 21 #define PC_DEBUG(args...) pc.printf(args)
bcostm 0:fa6bc104fe2d 22 #else
bcostm 0:fa6bc104fe2d 23 #define PC_DEBUG(args...)
bcostm 0:fa6bc104fe2d 24 #endif
bcostm 0:fa6bc104fe2d 25
bcostm 0:fa6bc104fe2d 26 Ticker tick;
bcostm 0:fa6bc104fe2d 27
bcostm 0:fa6bc104fe2d 28 float speed = 1.0; // Motors speed
bcostm 0:fa6bc104fe2d 29
bcostm 0:fa6bc104fe2d 30 void ReadCommand(void)
bcostm 0:fa6bc104fe2d 31 {
bcostm 0:fa6bc104fe2d 32 int cmd = 0;
bcostm 0:fa6bc104fe2d 33 PC_DEBUG(">>> Read command...\n");
bcostm 0:fa6bc104fe2d 34 if (bluetooth.readable())
bcostm 0:fa6bc104fe2d 35 {
bcostm 0:fa6bc104fe2d 36 cmd = bluetooth.getc();
bcostm 0:fa6bc104fe2d 37 PC_DEBUG(">>> Bluetooth read [%c]\n", cmd);
bcostm 0:fa6bc104fe2d 38 switch(cmd)
bcostm 0:fa6bc104fe2d 39 {
bcostm 0:fa6bc104fe2d 40 case 1: // Forward
bcostm 0:fa6bc104fe2d 41 bot.forward(speed);
bcostm 0:fa6bc104fe2d 42 break;
bcostm 0:fa6bc104fe2d 43 case 2: // Backward
bcostm 0:fa6bc104fe2d 44 bot.backward(speed);
bcostm 0:fa6bc104fe2d 45 break;
bcostm 0:fa6bc104fe2d 46 case 3: // Left
bcostm 0:fa6bc104fe2d 47 bot.left(speed);
bcostm 0:fa6bc104fe2d 48 break;
bcostm 0:fa6bc104fe2d 49 case 4: // Right
bcostm 0:fa6bc104fe2d 50 bot.right(speed);
bcostm 0:fa6bc104fe2d 51 break;
bcostm 0:fa6bc104fe2d 52 case 5: // Turn left
bcostm 0:fa6bc104fe2d 53 bot.turn_right(speed);
bcostm 0:fa6bc104fe2d 54 break;
bcostm 0:fa6bc104fe2d 55 case 6: // Turn right
bcostm 0:fa6bc104fe2d 56 bot.turn_left(speed);
bcostm 0:fa6bc104fe2d 57 break;
bcostm 0:fa6bc104fe2d 58 case 7: // Slow
bcostm 0:fa6bc104fe2d 59 speed = 0.4;
bcostm 0:fa6bc104fe2d 60 break;
bcostm 0:fa6bc104fe2d 61 case 8: // Fast
bcostm 0:fa6bc104fe2d 62 speed = 1.0;
bcostm 0:fa6bc104fe2d 63 break;
bcostm 0:fa6bc104fe2d 64 default: // Stop
bcostm 0:fa6bc104fe2d 65 bot.stopAll();
bcostm 0:fa6bc104fe2d 66 break;
bcostm 0:fa6bc104fe2d 67 }
bcostm 0:fa6bc104fe2d 68 }
bcostm 0:fa6bc104fe2d 69 }
bcostm 0:fa6bc104fe2d 70
bcostm 0:fa6bc104fe2d 71 int main()
bcostm 0:fa6bc104fe2d 72 {
bcostm 0:fa6bc104fe2d 73 PC_DEBUG("\n\nSeeed Bluetooth shield test started.\n");
bcostm 0:fa6bc104fe2d 74
bcostm 0:fa6bc104fe2d 75 // Enable motors
bcostm 0:fa6bc104fe2d 76 bot.enable_right_motor();
bcostm 0:fa6bc104fe2d 77 bot.enable_left_motor();
bcostm 0:fa6bc104fe2d 78
bcostm 0:fa6bc104fe2d 79 // Check if they are alive
bcostm 0:fa6bc104fe2d 80 bot.left(speed);
bcostm 0:fa6bc104fe2d 81 wait(0.2);
bcostm 0:fa6bc104fe2d 82 bot.right(speed);
bcostm 0:fa6bc104fe2d 83 wait(0.2);
bcostm 0:fa6bc104fe2d 84 bot.stopAll();
bcostm 0:fa6bc104fe2d 85
bcostm 0:fa6bc104fe2d 86 PC_DEBUG(">>> Bluetooth setup...");
bcostm 0:fa6bc104fe2d 87 bluetooth.setup();
bcostm 0:fa6bc104fe2d 88 PC_DEBUG("done\n");
bcostm 0:fa6bc104fe2d 89
bcostm 0:fa6bc104fe2d 90 PC_DEBUG(">>> Bluetooth in slave mode...");
bcostm 0:fa6bc104fe2d 91 bluetooth.slave("btslave8seeed"); // default PIN code: 0000
bcostm 0:fa6bc104fe2d 92 PC_DEBUG("done\n");
bcostm 0:fa6bc104fe2d 93
bcostm 0:fa6bc104fe2d 94 wait(2);
bcostm 0:fa6bc104fe2d 95
bcostm 0:fa6bc104fe2d 96 PC_DEBUG(">>> Bluetooth connect...");
bcostm 0:fa6bc104fe2d 97 bluetooth.connect();
bcostm 0:fa6bc104fe2d 98 PC_DEBUG("done\n");
bcostm 0:fa6bc104fe2d 99
bcostm 0:fa6bc104fe2d 100 tick.attach_us(ReadCommand, 500000); // Every 500ms read Bluetooth command
bcostm 0:fa6bc104fe2d 101
bcostm 0:fa6bc104fe2d 102 bot.forward(speed);
bcostm 0:fa6bc104fe2d 103 wait(0.4);
bcostm 0:fa6bc104fe2d 104 bot.backward(speed);
bcostm 0:fa6bc104fe2d 105 wait(0.4);
bcostm 0:fa6bc104fe2d 106 bot.stopAll();
bcostm 0:fa6bc104fe2d 107
bcostm 0:fa6bc104fe2d 108 while (1) {
bcostm 0:fa6bc104fe2d 109 wait(1);
bcostm 0:fa6bc104fe2d 110 }
bcostm 0:fa6bc104fe2d 111 }