Hi, I have a project to transfer IMU data over BLE with C# program.
And I found that if I disconnect BLE through program and without restart the power, the transfer speed would drop.
For example, the transfer speed with first connecting was 50 Hz, the second time would drop to 30 Hz, the third time would drop to 10 Hz and so on.
I wonder know if anybody had the same question and how to solve it.
Below link is my reference with C# program.
https://www.programmersought.com/article/51155122138/
I had modified the function FindService and FindCharacteristic, my program could normal execute.
And below is part of my Arduino code.
#include "Arduino.h"
#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h>
#define BLE_IMUBUFFER_SIZES 45
#define BLE_DEVICE_NAME "NTPU ESA LC BLE"
#define BLE_LOCAL_NAME "NTPU ESA LC BLE"
BLEService BLESensors("a2ea6c2e-9475-4a9e-aa5d-e190a8b8830c");
BLECharacteristic IMUBLE("0001", BLERead | BLENotify | BLEBroadcast, BLE_IMUBUFFER_SIZES);
byte IMUpacket[45] = {IMUheader[0],IMUheader[1],
previousIMUTimes[0],previousIMUTimes[1],previousIMUTimes[2],previousIMUTimes[3],previousIMUTimes[4],
body[1],
AccXb[0],AccXb[1],AccXb[2],AccXb[3],
AccYb[0],AccYb[1],AccYb[2],AccYb[3],
AccZb[0],AccZb[1],AccZb[2],AccZb[3],
GyroXb[0],GyroXb[1],GyroXb[2],GyroXb[3],
GyroYb[0],GyroYb[1],GyroYb[2],GyroYb[3],
GyroZb[0],GyroZb[1],GyroZb[2],GyroZb[3],
MagXb[0],MagXb[1],MagXb[2],MagXb[3],
MagYb[0],MagYb[1],MagYb[2],MagYb[3],
MagZb[0],MagZb[1],MagZb[2],MagZb[3],
(byte)Count
};
void setup()
{
Serial.begin(115200);
if (!BLE.begin())
{
while (1);
}
else
{
BLE.setDeviceName(BLE_DEVICE_NAME);
BLE.setLocalName(BLE_LOCAL_NAME);
BLE.setAdvertisedService(BLESensors);
BLESensors.addCharacteristic(IMUBLE);
BLE.addService(BLESensors);
BLE.advertise();
if (!IMU.begin())
{
Serial.println("Failed to initialize IMU!");
while (1);
}
IMU.setAccelFS(3);
IMU.setAccelODR(3);
IMU.setAccelOffset(-0.017249, 0.017463, -0.02762);
IMU.setAccelSlope (1, 1, 1);
/***********************************************************************************************************************************
******* FS Full Scale range 0:±2g | 1:±24g | 2: ±4g | 3: ±8g (default=2) ******
******* ODR Output Data Rate range 0:off | 1:10Hz | 2:50Hz | 3:119Hz | 4:238Hz | 5:476Hz, (default=3)(not working 6:952Hz) ******
************************************************************************************************************************************/
IMU.setGyroFS(3);
IMU.setGyroODR(3);
IMU.setGyroOffset(-0.266289, -0.21708, 0.528232);
IMU.setGyroSlope (1, 1, 1);
/***********************************************************************************************************************************
******* FS Full Scale setting 0: ±245°/s | 1: ±500°/s | 2: ±1000°/s | 3: ±2000°/s ******
******* ODR Output Data Rate setting 0:off | 1:10Hz | 2:50Hz | 3:119Hz | 4:238Hz | 5:476Hz, (default=3)(not working 6:952Hz) ******
************************************************************************************************************************************/
IMU.setMagnetFS(0);
IMU.setMagnetODR(6);
IMU.setMagnetOffset(0,0,0); // uncalibrated
IMU.setMagnetSlope (1,1,1); // uncalibrated
/******************************************************************************************************************************
**** FS Full Scale range (0=±400 | 1=±800 | 2=±1200 | 3=±1600 (µT) *****
**** ODR Output Data Rate range (6,7,8)=(40,80,400)Hz | not available on all chips (0..5): (0.625,1.25,2.5,5.0,10,20)Hz *****
*******************************************************************************************************************************/
IMU.accelUnit = GRAVITY;
IMU.gyroUnit= DEGREEPERSECOND;
IMU.magnetUnit = MICROTESLA;
}
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
BLEDevice central = BLE.central();
if(central)
{
TimeReset();
bool dataGotFlag = false;
while(central.connected())
{
digitalWrite(LED_BUILTIN, HIGH);
preMicroTime = micros();
Intervalcal();
if(IMU.accelAvailable() && IMU.gyroAvailable())
{
IMU.readAccel(Acc[0], Acc[1], Acc[2]);
IMU.readGyro(Gyro[0], Gyro[1], Gyro[2]);
pointAccXb = (byte *)&Acc[0];
pointAccYb = (byte *)&Acc[1];
pointAccZb = (byte *)&Acc[2];
pointGyroXb = (byte *)&Gyro[0];
pointGyroYb = (byte *)&Gyro[1];
pointGyroZb = (byte *)&Gyro[2];
dataGotFlag = true;
}
if(IMU.magnetAvailable())
{
IMU.readMagnet(Mag[0], Mag[1], Mag[2]);
pointMagXb = (byte *)&Mag[0];
pointMagYb = (byte *)&Mag[1];
pointMagZb = (byte *)&Mag[2];
dataGotFlag = true;
}
if(dataGotFlag)
{
IMUpacket[2] = (byte)Hour;
IMUpacket[3] = (byte)Min;
IMUpacket[4] = (byte)Sec;
IMUpacket[5] = (byte)(Msec/256);
IMUpacket[6] = (byte)(Msec%256);
TurnToByte();
CountNum();
IMUpacket[44] = (byte)Count;
IMUBLE.writeValue(IMUpacket,45);
dataGotFlag = false;
}
proMicroTime = micros();
Timer();
}
digitalWrite(LED_BUILTIN, LOW);
}
}