Bluetooth program for the Seeed bot

Dependencies:   BluetoothSerial SeeedShieldBot mbed

Fork of Seeed_BlueBot_demo by ST

main.cpp

Committer:
bcostm
Date:
2014-09-08
Revision:
0:fa6bc104fe2d
Child:
1:6a67ad238815

File content as of revision 0:fa6bc104fe2d:

#include "mbed.h"
#include "BluetoothSerial.h"
#include "SeeedStudioShieldBot.h"

// The following configuration must be done on the NUCLEO board:
// - Close SB62/SB63 and open SB13/SB14 solder bridges to enable the D0/D1 pins
// - Open SB21 solder bridge to disconnect the LED

BluetoothSerial bluetooth(D1, D0); // TX, RX

SeeedStudioShieldBot bot(
    D8, D9, D11,       // Right motor pins (PwmOut, DigitalOut, DigitalOut) -> Motor 1
    D12, D10, D13,     // Left motor pins (PwmOut, DigitalOut, DigitalOut) -> Motor 2
    A0, A1, A2, A3, A4 // Sensors pins (all DigitalIn)
);

// Enable it for debugging on hyperterminal
#define DEBUG 1
#if DEBUG == 1
Serial pc(PC_10, PC_11);
#define PC_DEBUG(args...) pc.printf(args)
#else
#define PC_DEBUG(args...)
#endif

Ticker tick;

float speed = 1.0; // Motors speed
    
void ReadCommand(void)
{
    int cmd = 0;
    PC_DEBUG(">>> Read command...\n");
    if (bluetooth.readable())
    {
        cmd = bluetooth.getc();
        PC_DEBUG(">>> Bluetooth read [%c]\n", cmd);
        switch(cmd)
        {
        case 1: // Forward
            bot.forward(speed);
            break;
        case 2: // Backward
            bot.backward(speed);
            break;              
        case 3: // Left
            bot.left(speed);
            break;
        case 4: // Right
            bot.right(speed);
            break;              
        case 5: // Turn left
            bot.turn_right(speed);
            break;
         case 6: // Turn right
            bot.turn_left(speed);
            break; 
         case 7: // Slow
            speed = 0.4;
            break;
         case 8: // Fast
            speed = 1.0;
            break;
         default: // Stop
            bot.stopAll();
            break;
         }
    }    
}
    
int main()
{
    PC_DEBUG("\n\nSeeed Bluetooth shield test started.\n");
      
    // Enable motors
    bot.enable_right_motor();
    bot.enable_left_motor();
    
    // Check if they are alive
    bot.left(speed);
    wait(0.2);
    bot.right(speed);
    wait(0.2);
    bot.stopAll();
  
    PC_DEBUG(">>> Bluetooth setup...");
    bluetooth.setup();
    PC_DEBUG("done\n");
  
    PC_DEBUG(">>> Bluetooth in slave mode...");
    bluetooth.slave("btslave8seeed");  // default PIN code: 0000
    PC_DEBUG("done\n");
    
    wait(2);
    
    PC_DEBUG(">>> Bluetooth connect...");
    bluetooth.connect();
    PC_DEBUG("done\n");
    
    tick.attach_us(ReadCommand, 500000); // Every 500ms read Bluetooth command

    bot.forward(speed);
    wait(0.4);
    bot.backward(speed);
    wait(0.4);
    bot.stopAll();
      
    while (1) {
        wait(1);
    } 
}