practica1

Dependencies:   mbed SeeedShieldBot BluetoothSerial

Committer:
jlandresh
Date:
Fri Nov 27 14:26:46 2020 +0000
Revision:
0:04b0267b59eb
practica1

Who changed what in which revision?

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