Hi everybody,
this is my first post here, hope to be in the right forum section
I have a Genuino 101 measuring yaw, pitch and roll and I'm trying to configure it in order to make it send all of them via Bluetooth LE on an app like nRF Master Tool. The service is correctly advertised but unfortunately nothing is sent to my app
This is my code at the moment [I was trying to start sending at least one of the three but I have to send them all] - any idea on how to fix it? I would like them to get out of the board
Thanks in advance to those who will try to help,
Antonino
#include <BMI160.h>
#include <CurieImu.h>
#include <CurieBle.h>
#include <MadgwickAHRS.h>
// initialise Madgwick object
Madgwick filter;
// accelerometer and gyroscope
int ax, ay, az;
int gx, gy, gz;
// yaw, pitch, roll
float yaw, pitch, roll;
int old_roll = 0;
// variable by which to divide gyroscope values, used to control sensitivity
// NOTE: an increased baud rate requires an increase in value of factor
int factor = 800;
// int to determine whether calibration takes place or not
int calibrateOffsets = 1;
// pin to use for the LED
const int ledPin = 13;
BLEPeripheral blePeripheral;
// custom service
// list of standard services here: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx
// HeartRate details here: https://developer.bluetooth.org/TechnologyOverview/Pages/HRP.aspx
// with nRF UART [also inside nRF toolbox] UUIDs used are - https://www.nordicsemi.com/eng/Products/Nordic-mobile-Apps/nRF-UART-App:
// 6E400001-B5A3-F393-E0A9-E50E24DCCA9E for the Service
// 6E400002-B5A3-F393-E0A9-E50E24DCCA9E for the TX Characteristic
// 6E400003-B5A3-F393-E0A9-E50E24DCCA9E for the RX Characteristic
// IMPORTANT - example: http://www.forward.com.au/pfod/BLE/SampleScreensArduino101.ino.txt
// tutorial: http://www.instructables.com/id/Custom-BLE-No-Programming-Required/step6/Using-ArduinoGenuino-101-BLE-board/
// create Nordic UART service
BLEService uartService = BLEService("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
// create characteristics
BLECharacteristic txCharacteristic = BLECharacteristic("6E400002B5A3F393E0A9E50E24DCCA9E", BLENotify | BLERead , 20); // == RX on central (android app)
// if remote device writes
BLECharacteristic rxCharacteristic = BLECharacteristic("6E400003B5A3F393E0A9E50E24DCCA9E", BLEWriteWithoutResponse, 20); // == TX on central (android app)
long previousMillis = 0; // last time the heart rate was checked, in ms
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
CurieIMU.begin();
while (!Serial);
// verify connection
Serial.println("Testing device connections...");
if (CurieIMU.testConnection())
{
Serial.println("CurieImu connection successful");
Serial.println("");
}
else
{
Serial.println("CurieImu connection failed");
}
// set advertised local name and service UUID
blePeripheral.setLocalName("Gen101");
blePeripheral.setAdvertisedServiceUuid(uartService.uuid());
// add service and characteristic
blePeripheral.addAttribute(uartService);
blePeripheral.addAttribute(rxCharacteristic);
blePeripheral.addAttribute(txCharacteristic);
// set the initial value for the characeristic:
// rxCharacteristic.setValue(0);
blePeripheral.begin();
Serial.println("Bluetooth device active, waiting for connections...");
// accelerometer and gyroscope calibration before and after
if (calibrateOffsets == 1) {
// use the code below to calibrate accel/gyro offset values
Serial.println("Internal sensor offsets BEFORE calibration...");
Serial.print("acc x axis: ");
Serial.print(CurieIMU.getAccelerometerOffset(X_AXIS)); Serial.print("\t");
Serial.print("acc y axis: ");
Serial.print(CurieIMU.getAccelerometerOffset(Y_AXIS)); Serial.print("\t");
Serial.print("acc z axis: ");
Serial.print(CurieIMU.getAccelerometerOffset(Z_AXIS)); Serial.println("\t");
Serial.print("gyr x axis: ");
Serial.print(CurieIMU.getGyroOffset(X_AXIS)); Serial.print("\t");
Serial.print("gyr y axis: ");
Serial.print(CurieIMU.getGyroOffset(Y_AXIS)); Serial.print("\t");
Serial.print("gyr z axis: ");
Serial.print(CurieIMU.getGyroOffset(Z_AXIS)); Serial.println("\t");
Serial.println("");
//IMU device must be resting in a horizontal position for the following calibration procedure to work correctly!
Serial.print("Starting Gyroscope calibration...");
CurieIMU.autoCalibrateGyroOffset();
Serial.println(" Done");
Serial.print("Starting Acceleration calibration...");
CurieIMU.autoCalibrateAccelerometerOffset(X_AXIS, 0);
CurieIMU.autoCalibrateAccelerometerOffset(Y_AXIS, 0);
CurieIMU.autoCalibrateAccelerometerOffset(Z_AXIS, 1);
Serial.println(" Done");
Serial.println("");
Serial.println("Internal sensor offsets AFTER calibration...");
Serial.print("acc x axis: ");
Serial.print(CurieIMU.getAccelerometerOffset(X_AXIS)); Serial.print("\t");
Serial.print("acc y axis: ");
Serial.print(CurieIMU.getAccelerometerOffset(Y_AXIS)); Serial.print("\t");
Serial.print("acc z axis: ");
Serial.print("");
Serial.print(CurieIMU.getAccelerometerOffset(Z_AXIS)); Serial.println("\t");
Serial.print("gyr x axis: ");
Serial.print(CurieIMU.getGyroOffset(X_AXIS)); Serial.print("\t");
Serial.print("gyr y axis: ");
Serial.print(CurieIMU.getGyroOffset(Y_AXIS)); Serial.print("\t");
Serial.print("gyr z axis: ");
Serial.print(CurieIMU.getGyroOffset(Z_AXIS)); Serial.println("\t");
Serial.println("");
}
}
void loop()
{
// listen for BLE peripherals to connect:
BLECentral central = blePeripheral.central();
// if a central is connected to peripheral:
if (central)
{
Serial.print("Connected to central: ");
// print the central's BT address:
Serial.println(central.address());
while (central.connected())
{
long currentMillis = millis();
// if 200ms have passed, check values:
if (currentMillis - previousMillis >= 200)
{
previousMillis = currentMillis;
updateOutputValues();
}
}
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}
void updateOutputValues()
{
// read raw accel/gyro measurements from device
CurieIMU.readMotionSensor(ax, ay, az, gx, gy, gz);
// use function from MagdwickAHRS.h to return quaternions
filter.updateIMU(gx / factor, gy / factor, gz / factor, ax, ay, az);
// functions to find yaw roll and pitch from quaternions
yaw = filter.getYaw();
roll = filter.getRoll();
pitch = filter.getPitch();
if (roll != old_roll) { // if the roll has changed
Serial.print("Roll is now: ");
Serial.println(roll);
const unsigned char rollCharArray[2] = { 0, (char)roll };
txCharacteristic.setValue(rollCharArray, 2); // and update the heart rate measurement characteristic
old_roll = roll; // save the level for next comparison
}
// print gyro and accel values for debugging only, comment out when running Processing
// if there is a request on the serial port
if (Serial.available() > 0)
{
int val = Serial.read();
// if the request is exactly "s" then give back all the values
if (val == 's')
{
//Serial.print("yaw value: ");
//Serial.print(yaw); Serial.print("\t");
//Serial.print("pitch value: ");
//Serial.print(pitch); Serial.print("\t");
//Serial.print("roll value: ");
Serial.print(roll); Serial.println("\t");
//Serial.print("acc x axis: ");
//Serial.print(ax); Serial.print("\t");
//Serial.print("acc y axis: ");
//Serial.print(ay); Serial.print("\t");
//Serial.print("acc z axis: ");
//Serial.print(az); Serial.println("\t");
//Serial.print("gyr x axis: ");
//Serial.print(gx); Serial.print("\t");
//Serial.print("gyr y axis: ");
//Serial.print(gy); Serial.print("\t");
//Serial.print("gyr z axis: ");
//Serial.print(gz); Serial.println("\t");
//Serial.println("");
}
}
}