Nano 33 BLE Sense Machine Learning Problem

I have successfully run gesture example using machine learning. But I when I want to run the machine learning for my project, I got some problems. My model has 10 inputs and 10 outputs. When I add code to run inferencing, the board goes into the error mode with yellow light flushing. The code that causes the error is

TfLiteStatus invokeStatus = tflInterpreter->Invoke();

Here is my entire code which also has I2C communication. Thanks.

// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>
#include "powerTest100.h"
#include "30Neurons.h"

#include <Arduino_LSM9DS1.h>
#include <TensorFlowLite.h>
#include <tensorflow/lite/micro/all_ops_resolver.h>
#include <tensorflow/lite/micro/micro_error_reporter.h>
#include <tensorflow/lite/micro/micro_interpreter.h>
#include <tensorflow/lite/schema/schema_generated.h>
#include <tensorflow/lite/version.h>

// global variables used for TensorFlow Lite (Micro)
tflite::MicroErrorReporter tflErrorReporter;

// pull in all the TFLM ops, you can remove this line and
// only pull in the TFLM ops you need, if would like to reduce
// the compiled size of the sketch.
tflite::AllOpsResolver tflOpsResolver;

const tflite::Model* tflModel = nullptr;
tflite::MicroInterpreter* tflInterpreter = nullptr;
TfLiteTensor* tflInputTensor = nullptr;
TfLiteTensor* tflOutputTensor = nullptr;

// Create a static memory buffer for TFLM, the size may need to
// be adjusted based on the model you are using
constexpr int tensorArenaSize = 8 * 1024;
byte tensorArena[tensorArenaSize];
//byte tensorArena[tensorArenaSize] __attribute__((aligned(16)));

#define slaveAddress 0x50  //you have to assign an 8-bit address to Slave

byte dataArraySend[10];
byte dataArrayReceive[10];
int phaseNum = 0;
int powerNum = 0;
float phaseArray[10];
float powerArray[10];
bool transOn = true;
bool secondTime = false;
int dataNum = 10;


void setup() {
  Serial.begin(9600);
  Wire.begin(slaveAddress);                 // join i2c bus with address #50
  Wire.onReceive(receiveEvent); 
  Wire.onRequest(requestEvent); // register event

  // get the TFL representation of the model byte array
  tflModel = tflite::GetModel(model);
  if (tflModel->version() != TFLITE_SCHEMA_VERSION) {
    Serial.println("Model schema mismatch!");
    while (1);
  }

  // Create an interpreter to run the model
  tflInterpreter = new tflite::MicroInterpreter(tflModel, tflOpsResolver, tensorArena, tensorArenaSize, &tflErrorReporter);

  // Allocate memory for the model's input and output tensors
  tflInterpreter->AllocateTensors();

  // Get pointers for the model's input and output tensors
  tflInputTensor = tflInterpreter->input(0);
  tflOutputTensor = tflInterpreter->output(0);
}

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {

  if (transOn == true)
  {
    // Get required power information
    // Input power to tensorflow model
    for(int i=0; i<10; i++)
    {
//      powerArray[i] = powerSet[powerNum*10+i];
      powerArray[i] = powerSet[i];
      tflInputTensor->data.f[i] = powerArray[i];
    }
    
    //Run inferencing
    TfLiteStatus invokeStatus = tflInterpreter->Invoke();
    if (invokeStatus != kTfLiteOk) {
      Serial.println("Invoke failed!");
      while (1);
      return;
    }
    
    for(int i=0; i<10; i++)
    {
      phaseArray[i] = tflOutputTensor->data.f[i];
      dataArraySend[i] = int ((phaseArray[i]+23)*5.54348);
      if (dataArraySend[i]>255)
      {
        dataArraySend[i] = 255;  
      }
      else if(dataArraySend[i]<=0)
      {
        dataArraySend[i] = 0;  
      }
    }
    Serial.print(powerArray[9]);
    Serial.print(" ");
    Serial.println(phaseArray[9]);

    // Send two times to make sure DSP receive the right data
    if (secondTime == true)
    {
      powerNum++;
      secondTime = false;
    }
    else if (secondTime == false)
    {
      secondTime = true;  
    }

    // Stop sending data when 100 data sets are completed sent
    if (powerNum == 100)
    {
//      transOn = false;
      powerNum = 0;
    }
    Serial.print("Data Sent: ");
    for(int i=0; i<dataNum; i++)
    {
      Wire.write(dataArraySend[i]); 
      Serial.print(dataArraySend[i]);
      Serial.print(" "); 
    }
    Serial.println(" ");  
    
    if (secondTime == false)
    {
      Serial.print("Phase Predicted: ");
      for(int i=0; i<dataNum; i++)
      {
        Serial.print(phaseArray[i]);
        Serial.print(" "); 
      }
      Serial.println(" "); 
      Serial.println(" ");
    }
  }
   delay(100);
}


void receiveEvent(int howMany)
{
  int nth = 0;
  Serial.print("Data Received: ");
  while(Wire.available()) // loop through all but the last
  {
    int x = Wire.read();    // receive byte as an integer
    dataArrayReceive[nth] = x;
    nth++;
    Serial.print(x);         // print the integer
    Serial.print(" ");         // print the integer
  }
  Serial.println(" ");  
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.