Nucleo board with Seeed bot and bluetooth shields demo.

Dependencies:   BluetoothSerial SeeedShieldBot mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "BluetoothSerial.h"
00003 #include "SeeedStudioShieldBot.h"
00004 
00005 // The following configuration must be done on the NUCLEO board:
00006 // - Close SB62/SB63 and open SB13/SB14 solder bridges to enable the D0/D1 pins
00007 // - Open SB21 solder bridge to disconnect the LED
00008 
00009 BluetoothSerial bluetooth(D1, D0); // TX, RX
00010 
00011 #ifdef TARGET_NUCLEO_L053R8
00012 #define PWM1 D6 // Connect D6 and D8
00013 #define PWM2 D12
00014 #else // NUCLEO_F072RB
00015 #define PWM1 D8
00016 #define PWM2 D12
00017 #endif
00018 
00019 SeeedStudioShieldBot bot(
00020     PWM1, D9, D11,     // Right motor pins (PwmOut, DigitalOut, DigitalOut) -> Motor 1
00021     PWM2, D10, D13,    // Left motor pins (PwmOut, DigitalOut, DigitalOut) -> Motor 2
00022     A0, A1, A2, A3, A4 // Sensors pins (all DigitalIn)
00023 );
00024 
00025 // Disable the feature by wiring A5 to GND
00026 DigitalIn ReadSensorsEnabled(A5, PullUp);
00027 
00028 // Enable it for debugging on hyperterminal
00029 #define DEBUG 0
00030 #if DEBUG == 1
00031 Serial pc(PC_10, PC_11); // Connect PC10 and CN3-RX
00032 #define PC_DEBUG(args...) pc.printf(args)
00033 #else
00034 #define PC_DEBUG(args...)
00035 #endif
00036 
00037 Ticker tick;
00038 
00039 float speed = 1.0; // Used to select the motors speed
00040 int stop = 0; // Used to stop the motors when a sensor detects something
00041 
00042 void ReadCommand(void)
00043 {
00044     int cmd = 0;
00045     PC_DEBUG(">>> Read command...\n");
00046 
00047     if (bluetooth.readable())
00048     {
00049         cmd = bluetooth.getc();
00050         PC_DEBUG(">>> Bluetooth read [%c]\n", cmd);
00051       
00052         // Ignore the receive command (excepted "Backward") if a sensor has detected something.
00053         if ((stop) && (cmd != '2')) return;
00054 
00055         switch (cmd)
00056         {
00057             case '1': // Forward
00058                 bot.forward(speed);
00059                 break;
00060             case '2': // Backward
00061                 bot.backward(speed);
00062                 break;              
00063             case '3': // Left
00064                 bot.left(speed);
00065                 break;
00066             case '4': // Right
00067                 bot.right(speed);
00068                 break;              
00069             case '5': // Turn left forward
00070                 bot.turn_right(speed);
00071                 break;
00072             case '6': // Turn right forward
00073                 bot.turn_left(speed);
00074                 break; 
00075             case '7': // Turn left backward
00076                 bot.turn_right(-speed);
00077                 break;
00078             case '8': // Turn right backward
00079                 bot.turn_left(-speed);
00080                 break; 
00081             case '9': // Slow
00082                 speed = 0.5;
00083                 break;
00084             case 'A': // Fast
00085                 speed = 1.0;
00086                 break;
00087             default: // Stop
00088                 bot.stopAll();
00089                 break;
00090          }
00091     }    
00092 }
00093     
00094 int main()
00095 {
00096     PC_DEBUG("\n\nSeeed Bluetooth shield test started.\n");
00097       
00098     // Enable motors
00099     bot.enable_right_motor();
00100     bot.enable_left_motor();
00101   
00102     PC_DEBUG(">>> Bluetooth setup...");
00103     bluetooth.setup();
00104     PC_DEBUG("done\n");
00105   
00106     PC_DEBUG(">>> Bluetooth in slave mode...");
00107     bluetooth.slave("bt_seeed_1");  // default PIN code: 0000
00108     PC_DEBUG("done\n");
00109     
00110     wait(2);
00111     
00112     PC_DEBUG(">>> Bluetooth connect...");
00113     bluetooth.connect();
00114     PC_DEBUG("done\n");
00115     
00116     tick.attach(ReadCommand, 0.3); // Every 300 ms read Bluetooth command
00117 
00118     // Check if motors are alive
00119     bot.left(speed);
00120     wait(0.2);
00121     bot.right(speed);
00122     wait(0.2);
00123     bot.stopAll();
00124     
00125     while (1) {
00126         // Stop the motors if a sensor has detected something.
00127         if ((!bot.rightSensor || !bot.inRightSensor || !bot.centreSensor || !bot.inLeftSensor || !bot.leftSensor) && ReadSensorsEnabled)
00128         {
00129             if (!stop) bot.stopAll();
00130             stop = 1;
00131         }
00132         else
00133         { 
00134             stop = 0;
00135         }
00136     }
00137 }