Are there any obvious issues in the code? [code]
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Are there any obvious issues in the code?
#include "mbed.h"
#include "drivers/DigitalOut.h"
#include <events/mbed_events.h>
#include "/home/user/EE2405/14_5_BBCar_BLE/mbed-os/connectivity/FEATURE_BLE/include/ble/BLE.h"
#include "gatt_server_process.h"
#include "mbed-trace/mbed_trace.h"
// Uncomment for actual BB Car operations
// #include "bbcar.h"
// Ticker servo_ticker;
// Ticker servo_feedback_ticker;
// PwmIn servo0_f(D9), servo1_f(D10);
// PwmOut servo0_c(D11), servo1_c(D12);
// BBCar car(servo0_c, servo0_f, servo1_c, servo1_f, servo_ticker, servo_feedback_ticker);
mbed::DigitalOut led1(LED1, 1);
mbed::DigitalOut led2(LED2, 1);
mbed::DigitalOut led3(LED3, 1);
mbed::DigitalOut *leds[] = {&led1, &led2, &led3};
// Uncomment for actual BB Car operations
// BBCar* cars[] = {&car}; //Control only one car
void stop(uint8_t car)
{
if (car == 1)
{ // there is only one car
*leds[car - 1] = 0;
// Uncomment for actual BB Car operations
// (*cars[car -1]).stop();
printf("Car %d stop.\n", car);
}
}
void goStraight(uint8_t car, int32_t speed)
{
if (car == 1)
{ // there is only one car
*leds[car - 1] = 0;
// Uncomment for actual BB Car operations
// (*cars[car -1]).goStraight(speed);
printf("Car %d go straight at speed %d.\n", car, speed);
}
}
void turn(uint8_t car, int32_t speed, double factor)
{
if (car == 1)
{ // there is only one car
*leds[car - 1] = 0;
// Uncomment for actual BB Car operations
// (*cars[car -1]).turn(speed, factor);
printf("Car %d turn at speed %d with a factor of %f.\n", car, speed, factor);
}
}
static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE);
class GattServerDemo : ble::GattServer::EventHandler {
const static uint16_t EXAMPLE_SERVICE_UUID = 0xA080;
const static uint16_t WRITABLE_CHARACTERISTIC_UUID = 0xA081;
public:
GattServerDemo()
{
const UUID uuid = WRITABLE_CHARACTERISTIC_UUID;
_writable_characteristic = new ReadWriteGattCharacteristic<uint8_t> (uuid, &_characteristic_value);
if (!_writable_characteristic) {
printf("Allocation of ReadWriteGattCharacteristic failed\r\n");
}
}
~GattServerDemo()
{
}
void start(BLE &ble, events::EventQueue &event_queue)
{
const UUID uuid = EXAMPLE_SERVICE_UUID;
GattCharacteristic* charTable[] = { _writable_characteristic };
GattService example_service(uuid, charTable, 1);
ble.gattServer().addService(example_service);
ble.gattServer().setEventHandler(this);
printf("Example service added with UUID 0xA000\r\n");
printf("Connect and write to characteristic 0xA001\r\n");
}
private:
/**
* This callback allows the LEDService to receive updates to the ledState Characteristic.
*
* @param[in] params Information about the characteristic being updated.
*/
virtual void onDataWritten(const GattWriteCallbackParams ¶ms)
{
if ((params.handle == _writable_characteristic->getValueHandle()) && (params.len == 1)) {
printf("New characteristic value written: %x\r\n", *(params.data));
char command=*(params.data);
switch(command) {
case 0x69: //i
printf("Initialization received\r");
break;
case 0x66: //f
goStraight(1, 100);
break;
case 0x6C: //l
turn(1, 100, 0.3);
break;
case 0x72: //r
turn(1, 100, -0.3);
break;
case 0x62: //b
goStraight(1, -100);
break;
case 0x73: //s
stop(1);
break;
default:
break;
}
}
}
private:
ReadWriteGattCharacteristic<uint8_t> *_writable_characteristic = nullptr;
uint8_t _characteristic_value = 0;
};
int main()
{
mbed_trace_init();
BLE &ble = BLE::Instance();
printf("GattServer for BB Car.\r\n");
GattServerDemo demo;
/* this process will handle basic setup and advertising for us */
GattServerProcess ble_process(event_queue, ble);
/* once it's done it will let us continue with our demo*/
ble_process.on_init(callback(&demo, &GattServerDemo::start));
ble_process.start();
return 0;
}
Below are the “head-slappers” I spotted after a quick code-review.
The first two will actually STOP the code from compiling; the rest are logical / style / portability problems you’ll probably want to fix before you flash the board.
class GattServerDemo : ble::GattServer::EventHandler { … }
class defaults to private inheritance.
Later you do
ble.gattServer().setEventHandler(this);
setEventHandler() expects a ble::GattServer::EventHandler*.
A pointer-to-derived only converts to pointer-to-base when the inheritance is public.
→ Change to
class GattServerDemo : public ble::GattServer::EventHandler {
#include "/home/user/EE2405/14_5_BBCar_BLE/mbed-os/connectivity/FEATURE_BLE/include/ble/BLE.h"
Hard-coding a local, absolute path makes the project non-portable and will break on every other machine (and sometimes even after you update mbed-os). Replace with the normal relative include that mbed already provides:
#include "ble/BLE.h"
You allocate the characteristic with new but never delete it:
_writable_characteristic = new ReadWriteGattCharacteristic<uint8_t>(…);
Add a delete in the destructor or, better, make it a direct member, e.g.
ReadWriteGattCharacteristic<uint8_t> _writable_characteristic;
and initialise it in the member-initialiser list.
const static uint16_t EXAMPLE_SERVICE_UUID = 0xA080;
const static uint16_t WRITABLE_CHARACTERISTIC_UUID = 0xA081;
…
printf("Example service added with UUID 0xA000\r\n");
printf("Connect and write to characteristic 0xA001\r\n");
The strings printed (0xA000 / 0xA001) don’t match the actual constants (0xA080 / 0xA081). This will confuse you when you try to connect from the phone/computer.
ReadWriteGattCharacteristic has several constructors.
Most examples pass an explicit flag, e.g.
ReadWriteGattCharacteristic<uint8_t>(
uuid,
&_characteristic_value,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ |
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE
);
With your 2-arg constructor the characteristic becomes read-only on some versions of mbed-os. If the phone app can’t write, nothing in onDataWritten() will ever be called.
static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE);
The first constructor parameter is byte size, not “event count”.
If you really want to hold 10 events use:
static EventQueue event_queue(10 * EVENTS_EVENT_SIZE);
Your comment says that, but if you thought the parameter was “10 events” it’s actually “10×eventSize bytes”.
Using hex literals (0x69, 0x66 …) is correct, but readability improves if you add comments or constants:
enum : uint8_t {
CMD_INIT = 'i',
CMD_FORWARD = 'f',
CMD_LEFT = 'l',
CMD_RIGHT = 'r',
CMD_BACKWARD = 'b',
CMD_STOP = 's'
};
You set the LED “off” before each car action:
*leds[car - 1] = 0;
If you want a visual cue while the car is moving, consider turning it on (= 1) and setting it back to 0 inside stop().
Small, but serial monitors often need \r\n to jump to a new line:
printf("Initialization received\r\n");
All car-control lines are commented. If you forget to un-comment them when you’re on actual hardware you’ll wonder why the wheels don’t move. A #define DRY_RUN style switch is cleaner:
#ifdef DRY_RUN
#define CAR(x) /* do nothing */
#else
#define CAR(x) x
#endif
CAR(car.goStraight(speed));
class GattServerDemo : public ble::GattServer::EventHandler.new on the GATT characteristic.printf messages.EventQueue size.\n to that lone printf.Address 1 & 2 and the code will compile; fix the rest for happier debugging on the actual BB Car 🚗💨