[nano33 ble sense rev2 - cybeorn programming question]

Hi guys, I'm using 'Arduino Nano 33 BLE Sense Rev2' and trying to merge VoiceRecognition example file provided from 'Cyberon' with my own codes.

But when I add my codes to the example file, after compilation is completed, 'LostRecordingFrame!' sentence is printed to the serial monitor.(I attached the image).

I spent two weeks trying almost everything I could, including using multi-threading in the CLI, and found helpful information on forums, Contact Us, and Google. But all was to no avail.

After many attempts, I thought the buffer might be the problem and tried printing the available function, but it kept coming back 0. However, since the header file included in the example file continues to mention memory, I think it is still a memory issue. (I am not familiar with CPP, so I could not interpret the header file correctly.)

I don't think the voice recognition example file is intended to be used alone. I believe that someone has definitely succeeded in integrating the voice recognition example file with the code that implemented what they wanted to create. I hope so too.

I'll attach the code I tried below, and I'd be really happy if you could help me figure out what's causing the problem. It would be a great help if someone who succeeded in what I wanted to try reads this article and offers help.

*This is my first time using Arduino.
*The text may not sound natural due to the use of a translator. If you leave a comment, I will attach additional explanation.

#include <Arduino.h>
#include <Wire.h>
#include "SparkFun_BMI270_Arduino_Library.h"
#include <ArduinoBLE.h>
#include <DSpotterSDK_MakerHL.h>
#include <LED_Control.h>
#include "CybLicense.h"

BMI270 imu;
uint8_t i2cAddress = BMI2_I2C_PRIM_ADDR;
BLEService imuService("180A");
BLECharacteristic accidentChar("2A6E", BLERead | BLENotify, sizeof(float));
BLECharacteristic Impact("3B7D", BLERead | BLENotify, sizeof(float));
float dt;
float accidentDetected = 0;
float magnitudeOfMomentumChange = 0;
const float alpha = 0.5;  // complimentary filter weight
float angleX, angleY;     // filtered angle

#define DSPOTTER_LICENSE g_lpdwLicense
#if defined(TARGET_ARDUINO_NANO33BLE) || defined(TARGET_PORTENTA_H7) || defined(TARGET_NICLA_VISION)
#include "Model_L1.h"
#elif defined(TARGET_NANO_RP2040_CONNECT)
#include "Model_L0.h"
#endif
#define DSPOTTER_MODEL g_lpdwModel
static DSpotterSDKHL g_oDSpotterSDKHL;

void VRCallback(int nFlag, int nID, int nScore, int nSG, int nEnergy) {
  if (nFlag == DSpotterSDKHL::InitSuccess) {
           
     Wire.begin();
    if (imu.beginI2C() != BMI2_OK) {
        Serial.println("Error: BMI270 not connected, check wiring!");
        while (1);  // 
 }
    BLE.begin();
    BLE.setLocalName("CAP_BLE");
    imuService.addCharacteristic(accidentChar);
    imuService.addCharacteristic(Impact);
    BLE.addService(imuService);
    BLE.advertise();
  } else if (nFlag == DSpotterSDKHL::GetResult) {
  } else if (nFlag == DSpotterSDKHL::ChangeStage) {
  } else if (nFlag == DSpotterSDKHL::GetError) {
    if (nID == DSpotterSDKHL::LicenseFailed) {
    }
    g_oDSpotterSDKHL.Release();
    while (1);  //hang loop
  } else if (nFlag == DSpotterSDKHL::LostRecordFrame) {
    Serial.read();
    //while(1);
  }
}

void setup() {
 // LED_Init_All();
  Serial.begin(9600);
  while (!Serial);  // 시리얼 포트 연결 대기

  DSpotterSDKHL::ShowDebugInfo(true);
  if (g_oDSpotterSDKHL.Init(DSPOTTER_LICENSE, sizeof(DSPOTTER_LICENSE), DSPOTTER_MODEL, VRCallback) != DSpotterSDKHL::Success) {
    while(1);
  }
}

void loop() {
  dt = 0.01;  // data sampling rate 

  if (imu.getSensorData() == BMI2_OK) {
    float accelX = imu.data.accelX, accelY = imu.data.accelY, accelZ = imu.data.accelZ;
    float gyroX = imu.data.gyroX, gyroY = imu.data.gyroY;  // gyro data convert 

    float accelAngleX = atan2(accelY, sqrt(sq(accelX) + sq(accelZ))) * 180 / PI;
    float accelAngleY = atan2(-accelX, sqrt(sq(accelY) + sq(accelZ))) * 180 / PI;

    angleX = alpha * (angleX + gyroX * dt) + (1 - alpha) * accelAngleX;
    angleY = alpha * (angleY + gyroY * dt) + (1 - alpha) * accelAngleY;


    if (abs(angleX) > 45 || abs(angleY) > 45) {
      accidentDetected = 1;
    } else {
      accidentDetected = 0;
    }

    magnitudeOfMomentumChange = sqrt(sq(accelX) + sq(accelY) + sq(accelZ));

    // BLE value update
    accidentChar.writeValue((uint8_t*)&accidentDetected, sizeof(accidentDetected));
    Impact.writeValue((uint8_t*)&magnitudeOfMomentumChange, sizeof(magnitudeOfMomentumChange));
  }
  g_oDSpotterSDKHL.DoVR();
}

Hi @katriss122 ,

It seems that the issue occurs within the loop function. I speculate that the 'if' statement in the loop function is frequently evaluated as true, leading to the execution of BLE update code, which slows down VR processing, causing some recorded sound data to be processed too slowly.

DSpotterSDKHL initiates audio recording simultaneously after initialization and continuously collects audio into the audio buffer. It retrieves sound data from the buffer for processing when DoVR is called. When the audio buffer becomes full, the screen displays a lost record frame.

If the BLE judgment and update code within the loop function run frequently, it is advisable to place them in the part running within a MultiThread.

Another minor suggestion is to move the BLE initialization code from the VRCallback to the setup function before initializing DSpotterSDKHL.