我看到了Arduino101的一个随机奇怪的行为。如果我不移动电路板一切正常,但当我开始摇动并不断移动电路板时,它会随机卡住。这意味着您在扫描时无法再看到BLE Arduino设备,并且只能再次使用它来重启电路板。面对这个问题,arduino的电源仍然可以。我再说一遍,这似乎与晃动董事会一段时间有关。
这是我的草图,希望你可以帮助我理解可能的原因(可能是关于性能的一些问题或我处理中断的方式)。
#include "CurieIMU.h"
#include "CurieBLE.h"
#include <stdio.h> /* puts, printf */
bool motion;
int aix, aiy, aiz; // raw accelerometer values
int gix, giy, giz; // raw gyro values
BLEPeripheral blePeripheral;
BLEService sensorService("19B10010-E8F2-537E-4F6C-D104768A1214");
BLEUnsignedLongCharacteristic stepCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead); // stepcount
BLEFloatCharacteristic axCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // acc.in x in g
BLEFloatCharacteristic ayCharacteristic("19B10013-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // acc.in y in g
BLEFloatCharacteristic azCharacteristic("19B10014-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // acc.in z in g
BLEFloatCharacteristic gxCharacteristic("19B10015-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // rot.in x in deg/s
BLEFloatCharacteristic gyCharacteristic("19B10016-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // rot.in y in deg/s
BLEFloatCharacteristic gzCharacteristic("19B10017-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // rot.in z in deg/s
BLEIntCharacteristic shockCharacteristic("19B10018-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify); // true if shock sensed
BLEIntCharacteristic modeCharacteristic("19B10019-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); // sensing mode
BLEIntCharacteristic srangeCharacteristic("19B10020-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); // shock range
BLEIntCharacteristic arangeCharacteristic("19B10021-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); // acc. range
BLEIntCharacteristic grangeCharacteristic("19B100122-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); // gyro range
BLEIntCharacteristic calibCharacteristic("19B100123-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); // 1 if calibration is on, otherwise 0
void setup()
{
CurieIMU.begin();
blePeripheral.setLocalName("MOOVBIT");
blePeripheral.setAdvertisedServiceUuid(sensorService.uuid());
blePeripheral.addAttribute(sensorService);
blePeripheral.addAttribute(stepCharacteristic);
blePeripheral.addAttribute(axCharacteristic);
blePeripheral.addAttribute(ayCharacteristic);
blePeripheral.addAttribute(azCharacteristic);
blePeripheral.addAttribute(gxCharacteristic);
blePeripheral.addAttribute(gyCharacteristic);
blePeripheral.addAttribute(gzCharacteristic);
blePeripheral.addAttribute(shockCharacteristic);
blePeripheral.addAttribute(modeCharacteristic);
blePeripheral.addAttribute(arangeCharacteristic);
blePeripheral.addAttribute(srangeCharacteristic);
blePeripheral.addAttribute(grangeCharacteristic);
blePeripheral.addAttribute(calibCharacteristic);
// set initial characteristics values
stepCharacteristic.setValue(0);
shockCharacteristic.setValue(0);
modeCharacteristic.setValue(0);
arangeCharacteristic.setValue(4);
srangeCharacteristic.setValue(4);
grangeCharacteristic.setValue(250);
calibCharacteristic.setValue(1);
initPeriph();
CurieIMU.setAccelerometerRange(arangeCharacteristic.value());
CurieIMU.setGyroRange(grangeCharacteristic.value());
CurieIMU.setStepDetectionMode(CURIE_IMU_STEP_MODE_SENSITIVE);
CurieIMU.setStepCountEnabled(true);
CurieIMU.setDetectionThreshold(CURIE_IMU_SHOCK, srangeCharacteristic.value() * 500); // 1g = 1000 mg
CurieIMU.setDetectionDuration(CURIE_IMU_SHOCK, 50); // 50ms or 75ms
CurieIMU.interrupts(CURIE_IMU_SHOCK);
CurieIMU.interrupts(CURIE_IMU_MOTION);
CurieIMU.attachInterrupt(eventCallback);
motion = false;
}
void loop()
{
if (motion) {
if (!axCharacteristic.setValue(convertRawAcceleration(aix)) || !ayCharacteristic.setValue(convertRawAcceleration(aiy)) ||
!azCharacteristic.setValue(convertRawAcceleration(aiz)) || !gxCharacteristic.setValue(convertRawGyro(gix)) ||
!gyCharacteristic.setValue(convertRawGyro(giy)) || !gzCharacteristic.setValue(convertRawGyro(giz))) {
//Serial.println("Erorr BLE");
}
stepCharacteristic.setValue(CurieIMU.getStepCount());
motion = false;
}
delay(2000);
shockCharacteristic.setValue(0);
}
void eventCallback() {
if (CurieIMU.getInterruptStatus(CURIE_IMU_SHOCK)) {
if (shockCharacteristic.value() == 0) {
//Serial.println("Shock sensed");
shockCharacteristic.setValue(1);
}
//CurieIMU.readMotionSensor(aix, aiy, aiz, gix, giy, giz);
//motion = true;
}
else if (CurieIMU.getInterruptStatus(CURIE_IMU_MOTION)) {
CurieIMU.readMotionSensor(aix, aiy, aiz, gix, giy, giz);
motion = true;
}
}