Mbed demo 1st

Dependencies:   BluetoothSerial SeeedShieldBot mbed

Committer:
otiliaboaghe
Date:
Sun Nov 06 18:26:44 2022 +0000
Revision:
0:3be42f4f1d49
Mbed Demo

Who changed what in which revision?

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