Chris Anderson
Published © GPL3+

Uso de voz para direccionales(leds) con Arduino BLE Sense

Reconocer las siguientes palabras: Stop, Left y Right Con el fin de que encienda leds, simulando las direccionales y volante de un carro

BeginnerProtip2 hours9
Uso de voz para direccionales(leds) con Arduino BLE Sense

Things used in this project

Story

Read more

Schematics

schematics_06hohhejpo_Er1kZ801tL.jpg

Diagrama conexion

Se pone una imagen de referecia de como conectar los leds, resistencias
ledPin1 = D2;
ledPin2 = D3;
ledPin3 = D4;
ledPin4 = D5;
ledPin5 = D6;
ledPin6 = D7;

Code

Code Arduino

Arduino
Este es el codigo que se pondra en el arduino BLE Sense
/* Edge Impulse ingestion SDK
* Copyright (c) 2022 EdgeImpulse Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

// If your target is limited in memory remove this macro to save 10K RAM
#define EIDSP_QUANTIZE_FILTERBANK   0

/**
* Define the number of slices per model window. E.g. a model window of 1000 ms
* with slices per model window set to 4. Results in a slice size of 250 ms.
* For more info: https://docs.edgeimpulse.com/docs/continuous-audio-sampling
*/
#define EI_CLASSIFIER_SLICES_PER_MODEL_WINDOW 4

/*
** NOTE: If you run into TFLite arena allocation issue.
**
** This may be due to may dynamic memory fragmentation.
** Try defining "-DEI_CLASSIFIER_ALLOCATION_STATIC" in boards.local.txt (create
** if it doesn't exist) and copy this file to
** `<ARDUINO_CORE_INSTALL_PATH>/arduino/hardware/<mbed_core>/<core_version>/`.
**
** See
** (https://support.arduino.cc/hc/en-us/articles/360012076960-Where-are-the-installed-cores-located-)
** to find where Arduino installs cores on your machine.
**
** If the problem persists then there's not enough memory for this model and application.
*/

/* Includes ---------------------------------------------------------------- */
#include <PDM.h>
#include <Inteligencia3.0_inferencing.h>
#include "MadgwickAHRS.h"
#include <Arduino_LSM9DS1.h>
/** Audio buffers, pointers and selectors */
typedef struct {
    signed short *buffers[2];
    unsigned char buf_select;
    unsigned char buf_ready;
    unsigned int buf_count;
    unsigned int n_samples;
} inference_t;

static inference_t inference;
static bool record_ready = false;
static signed short *sampleBuffer;
static bool debug_nn = false; // Set this to true to see e.g. features generated from the raw signal
static int print_results = -(EI_CLASSIFIER_SLICES_PER_MODEL_WINDOW);
Madgwick filter;
float yaw = 0.0;
float yawThreshold = 5.0; // Umbral para detectar movimiento de yaw
float gx,gy,gz;
float ax,ay,az;
float mx,my,mz;
int dire;
int color;
float a,b,c,d,e;
const int ledPin1 = 2;
const int ledPin2 = 3;
const int ledPin3 = 4;
const int ledPin4 = 5;
const int ledPin5 = 6;
const int ledPin6 = 7;
/**
* @brief      Arduino setup function
*/
void setup()
{
    // put your setup code here, to run once:
    Serial.begin(115200);
    // comment out the below line to cancel the wait for USB connection (needed for native USB)
    while (!Serial);
     if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }
    filter.begin(100);
    Serial.println("Edge Impulse Inferencing Demo");
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
    pinMode(ledPin4, OUTPUT);
    pinMode(ledPin5, OUTPUT);
    pinMode(ledPin6, OUTPUT);
    pinMode(LEDR, OUTPUT);
    pinMode(LEDG, OUTPUT);
    pinMode(LEDB, OUTPUT);
    digitalWrite(LEDR, HIGH);               // will turn the LED off
    digitalWrite(LEDG, HIGH);               // will turn the LED off
    digitalWrite(LEDB, HIGH);
    // summary of inferencing settings (from model_metadata.h)
    ei_printf("Inferencing settings:\n");
    ei_printf("\tInterval: %.2f ms.\n", (float)EI_CLASSIFIER_INTERVAL_MS);
    ei_printf("\tFrame size: %d\n", EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE);
    ei_printf("\tSample length: %d ms.\n", EI_CLASSIFIER_RAW_SAMPLE_COUNT / 16);
    ei_printf("\tNo. of classes: %d\n", sizeof(ei_classifier_inferencing_categories) /
                                            sizeof(ei_classifier_inferencing_categories[0]));

    run_classifier_init();
    if (microphone_inference_start(EI_CLASSIFIER_SLICE_SIZE) == false) {
        ei_printf("ERR: Could not allocate audio buffer (size %d), this could be due to the window length of your model\r\n", EI_CLASSIFIER_RAW_SAMPLE_COUNT);
        return;
    }
}

/**
* @brief      Arduino main function. Runs the inferencing loop.
*/
void micro()
{
    bool m = microphone_inference_record();
    if (!m) {
        ei_printf("ERR: Failed to record audio...\n");
        return;
    }

    signal_t signal;
    signal.total_length = EI_CLASSIFIER_SLICE_SIZE;
    signal.get_data = &microphone_audio_signal_get_data;
    ei_impulse_result_t result = {0};

    EI_IMPULSE_ERROR r = run_classifier_continuous(&signal, &result, debug_nn);
    if (r != EI_IMPULSE_OK) {
        ei_printf("ERR: Failed to run classifier (%d)\n", r);
        return;
    }

    if (++print_results >= (EI_CLASSIFIER_SLICES_PER_MODEL_WINDOW)) {
        // print the predictions
        ei_printf("Predictions ");
        ei_printf("(DSP: %d ms., Classification: %d ms., Anomaly: %d ms.)",
            result.timing.dsp, result.timing.classification, result.timing.anomaly);
        ei_printf(": \n");
        for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) {
            ei_printf("    %s: %.5f\n", result.classification[ix].label,
                      result.classification[ix].value);
                     
        }
#if EI_CLASSIFIER_HAS_ANOMALY == 1
        ei_printf("    anomaly score: %.3f\n", result.anomaly);
#endif

        print_results = 0;
       
    }

a =  result.classification[0].value;
b =  result.classification[1].value;
c =  result.classification[2].value;
d =  result.classification[3].value;
e =  result.classification[4].value;
}
void direccion(){
     if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable() && IMU.magneticFieldAvailable()) {
    IMU.readAcceleration(ax,ay,az);
    IMU.readGyroscope(gx,gy,gz);
    IMU.readMagneticField(mx,my,mz);

    // Obtener las mediciones de aceleración, giroscopio y magnetómetro
    float accelX = ax;
    float accelY = ay;
    float accelZ = az;
    float gyroX = gx;
    float gyroY = gy;
    float gyroZ = gz;
    float magX = mx;
    float magY = my;
    float magZ = mz;

    // Actualizar el filtro de Madgwick con los datos de los sensores
    filter.update(gyroX, gyroY, gyroZ, accelX, accelY, accelZ, magX, magY, magZ);

    // Obtener el ángulo de yaw del filtro
    float currentYaw = filter.getYaw();

    // Imprimir el ángulo de yaw
    Serial.print("Yaw: ");
    Serial.println(currentYaw);

    // Detectar el movimiento de yaw
    float yawDifference = (currentYaw - yaw)*5;
     //Serial.println(yawDifference);
     //Serial.println(yawThreshold);
    
    if (abs(yawDifference) < yawThreshold) {
      // No se mueve (centro)
      //Serial.println("Centro");
      dire = 0;
    } else if (yawDifference > yawThreshold) {
      // Movimiento a la derecha
      //Serial.println("Izquierda");
      dire = 1;
    } else if (yawDifference < -yawThreshold) {
      // Movimiento a la izquierda
      //Serial.println("Derecha");
      dire = 2;
    }
   

    // Actualizar el valor de yaw
    yaw = currentYaw;
    delay(100);
  }
 
 
  }






void loop(){
  micro();
  direccion();
  if (a >= 0.80) {
  Serial.println("Left LED on");
  digitalWrite(LEDR, LOW);            // will turn the LED on
  digitalWrite(LEDG, HIGH);         // will turn the LED off
  digitalWrite(LEDB, HIGH);         // will turn the LED off
  color = 1;
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin1, HIGH);
 

}
else if (c >= 0.80) {
  Serial.println("Right LED on");
  digitalWrite(LEDR, HIGH);            // will turn the LED on
  digitalWrite(LEDG, LOW);         // will turn the LED off
  digitalWrite(LEDB, HIGH);         // will turn the LED off
  color = 2;
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledPin5, HIGH);
  digitalWrite(ledPin6, HIGH);
}
else if (d >= 0.80) {
  Serial.println("Stop LED on");
  color = 3;
 
}

if (color == 1 & dire == 1){
  digitalWrite(LEDR, HIGH);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin1, HIGH);
  delay(250);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  delay(250);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin1, HIGH);
  delay(250);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  delay(250);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin1, HIGH);
  delay(250);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  delay(250);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin1, HIGH);
  delay(250);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  }
else if (color == 2 & dire == 2){
  digitalWrite(LEDG, LOW);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledPin5, HIGH);
  digitalWrite(ledPin6, HIGH);
  delay(250);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, LOW);
  delay(250);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledPin5, HIGH);
  digitalWrite(ledPin6, HIGH);
  delay(250);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, LOW);
  delay(250);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledPin5, HIGH);
  digitalWrite(ledPin6, HIGH);
  delay(250);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, LOW);
  delay(250);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledPin5, HIGH);
  digitalWrite(ledPin6, HIGH);
  delay(250);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, LOW);
}

else if (color == 3){
  digitalWrite(LEDR, HIGH);            // will turn the LED on
  digitalWrite(LEDG, HIGH);         // will turn the LED off
  digitalWrite(LEDB, LOW);         // will turn the LED off
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledPin5, HIGH);
  digitalWrite(ledPin6, HIGH);
  delay(500);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, LOW);
  delay(500);
 
 
 
  }
  }
/**
* @brief      PDM buffer full callback
*             Get data and call audio thread callback
*/
static void pdm_data_ready_inference_callback(void)
{
    int bytesAvailable = PDM.available();

    // read into the sample buffer
    int bytesRead = PDM.read((char *)&sampleBuffer[0], bytesAvailable);

    if (record_ready == true) {
        for (int i = 0; i<bytesRead>> 1; i++) {
            inference.buffers[inference.buf_select][inference.buf_count++] = sampleBuffer[i];

            if (inference.buf_count >= inference.n_samples) {
                inference.buf_select ^= 1;
                inference.buf_count = 0;
                inference.buf_ready = 1;
            }
        }
    }
}

/**
* @brief      Init inferencing struct and setup/start PDM
*
* @param[in]  n_samples  The n samples
*
* @return     { description_of_the_return_value }
*/
static bool microphone_inference_start(uint32_t n_samples)
{
    inference.buffers[0] = (signed short *)malloc(n_samples * sizeof(signed short));

    if (inference.buffers[0] == NULL) {
        return false;
    }

    inference.buffers[1] = (signed short *)malloc(n_samples * sizeof(signed short));

    if (inference.buffers[1] == NULL) {
        free(inference.buffers[0]);
        return false;
    }

    sampleBuffer = (signed short *)malloc((n_samples >> 1) * sizeof(signed short));

    if (sampleBuffer == NULL) {
        free(inference.buffers[0]);
        free(inference.buffers[1]);
        return false;
    }

    inference.buf_select = 0;
    inference.buf_count = 0;
    inference.n_samples = n_samples;
    inference.buf_ready = 0;

    // configure the data receive callback
    PDM.onReceive(&pdm_data_ready_inference_callback);

    PDM.setBufferSize((n_samples >> 1) * sizeof(int16_t));

    // initialize PDM with:
    // - one channel (mono mode)
    // - a 16 kHz sample rate
    if (!PDM.begin(1, EI_CLASSIFIER_FREQUENCY)) {
        ei_printf("Failed to start PDM!");
    }

    // set the gain, defaults to 20
    PDM.setGain(127);

    record_ready = true;

    return true;
}

/**
* @brief      Wait on new data
*
* @return     True when finished
*/
static bool microphone_inference_record(void)
{
    bool ret = true;

    if (inference.buf_ready == 1) {
        ei_printf(
            "Error sample buffer overrun. Decrease the number of slices per model window "
            "(EI_CLASSIFIER_SLICES_PER_MODEL_WINDOW)\n");
        ret = false;
    }

    while (inference.buf_ready == 0) {
        delay(1);
    }

    inference.buf_ready = 0;

    return ret;
}

/**
* Get raw audio signal data
*/
static int microphone_audio_signal_get_data(size_t offset, size_t length, float *out_ptr)
{
    numpy::int16_to_float(&inference.buffers[inference.buf_select ^ 1][offset], out_ptr, length);

    return 0;
}

/**
* @brief      Stop PDM and release buffers
*/
static void microphone_inference_end(void)
{
    PDM.end();
    free(inference.buffers[0]);
    free(inference.buffers[1]);
    free(sampleBuffer);
}

#if !defined(EI_CLASSIFIER_SENSOR) || EI_CLASSIFIER_SENSOR != EI_CLASSIFIER_SENSOR_MICROPHONE
#error "Invalid model for current sensor."
#endif

Codigo Arduino

Arduino
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><title>/* Edge Impulse ingestion SDK</title></head><body><p dir="ltr">/* Edge Impulse ingestion SDK<br>
 * Copyright (c) 2022 EdgeImpulse Inc.<br>
 *<br>
 * Licensed under the Apache License, Version 2.0 (the "License");<br>
 * you may not use this file except in compliance with the License.<br>
 * You may obtain a copy of the License at<br>
 * http://www.apache.org/licenses/LICENSE-2.0<br>
 *<br>
 * Unless required by applicable law or agreed to in writing, software<br>
 * distributed under the License is distributed on an "AS IS" BASIS,<br>
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<br>
 * See the License for the specific language governing permissions and<br>
 * limitations under the License.<br>
 *<br>
 */</p>
<p dir="ltr">// If your target is limited in memory remove this macro to save 10K RAM<br>
#define EIDSP_QUANTIZE_FILTERBANK&nbsp;&nbsp; 0</p>
<p dir="ltr">/**<br>
 * Define the number of slices per model window. E.g. a model window of 1000 ms<br>
 * with slices per model window set to 4. Results in a slice size of 250 ms.<br>
 * For more info: https://docs.edgeimpulse.com/docs/continuous-audio-sampling<br>
 */<br>
#define EI_CLASSIFIER_SLICES_PER_MODEL_WINDOW 4</p>
<p dir="ltr">/*<br>
 ** NOTE: If you run into TFLite arena allocation issue.<br>
 **<br>
 ** This may be due to may dynamic memory fragmentation.<br>
 ** Try defining "-DEI_CLASSIFIER_ALLOCATION_STATIC" in boards.local.txt (create<br>
 ** if it doesn't exist) and copy this file to<br>
 ** `&lt;ARDUINO_CORE_INSTALL_PATH&gt;/arduino/hardware/&lt;mbed_core&gt;/&lt;core_version&gt;/`.<br>
 **<br>
 ** See<br>
 ** (https://support.arduino.cc/hc/en-us/articles/360012076960-Where-are-the-installed-cores-located-)<br>
 ** to find where Arduino installs cores on your machine.<br>
 **<br>
 ** If the problem persists then there's not enough memory for this model and application.<br>
 */</p>
<p dir="ltr">/* Includes ---------------------------------------------------------------- */<br>
#include &lt;PDM.h&gt;<br>
#include &lt;Inteligencia3.0_inferencing.h&gt;<br>
#include "MadgwickAHRS.h"<br>
#include &lt;Arduino_LSM9DS1.h&gt;<br>
/** Audio buffers, pointers and selectors */<br>
typedef struct {<br>
&nbsp;&nbsp;&nbsp; signed short *buffers[2];<br>
&nbsp;&nbsp;&nbsp; unsigned char buf_select;<br>
&nbsp;&nbsp;&nbsp; unsigned char buf_ready;<br>
&nbsp;&nbsp;&nbsp; unsigned int buf_count;<br>
&nbsp;&nbsp;&nbsp; unsigned int n_samples;<br>
} inference_t;</p>
<p dir="ltr">static inference_t inference;<br>
static bool record_ready = false;<br>
static signed short *sampleBuffer;<br>
static bool debug_nn = false; // Set this to true to see e.g. features generated from the raw signal<br>
static int print_results = -(EI_CLASSIFIER_SLICES_PER_MODEL_WINDOW);<br>
Madgwick filter;<br>
float yaw = 0.0;<br>
float yawThreshold = 5.0; // Umbral para detectar movimiento de yaw<br>
float gx,gy,gz;<br>
float ax,ay,az;<br>
float mx,my,mz;<br>
int dire;<br>
int color;<br>
float a,b,c,d,e;<br>
const int ledPin1 = 2;<br>
const int ledPin2 = 3;<br>
const int ledPin3 = 4;<br>
const int ledPin4 = 5;<br>
const int ledPin5 = 6;<br>
const int ledPin6 = 7;<br>
/**<br>
 * @brief&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Arduino setup function<br>
 */<br>
void setup()<br>
{<br>
&nbsp;&nbsp;&nbsp; // put your setup code here, to run once:<br>
&nbsp;&nbsp;&nbsp; Serial.begin(115200);<br>
&nbsp;&nbsp;&nbsp; // comment out the below line to cancel the wait for USB connection (needed for native USB)<br>
&nbsp;&nbsp;&nbsp; while (!Serial);<br>
&nbsp;&nbsp;&nbsp;&nbsp; if (!IMU.begin()) {<br>
&nbsp;&nbsp;&nbsp; Serial.println("Failed to initialize IMU!");<br>
&nbsp;&nbsp;&nbsp; while (1);<br>
&nbsp; }<br>
&nbsp;&nbsp;&nbsp; filter.begin(100);<br>
&nbsp;&nbsp;&nbsp; Serial.println("Edge Impulse Inferencing Demo");<br>
&nbsp;&nbsp;&nbsp; pinMode(ledPin1, OUTPUT);<br>
&nbsp;&nbsp;&nbsp; pinMode(ledPin2, OUTPUT);<br>
&nbsp;&nbsp;&nbsp; pinMode(ledPin3, OUTPUT);<br>
&nbsp;&nbsp;&nbsp; pinMode(ledPin4, OUTPUT);<br>
&nbsp;&nbsp;&nbsp; pinMode(ledPin5, OUTPUT);<br>
&nbsp;&nbsp;&nbsp; pinMode(ledPin6, OUTPUT);<br>
&nbsp;&nbsp;&nbsp; pinMode(LEDR, OUTPUT);<br>
&nbsp;&nbsp;&nbsp; pinMode(LEDG, OUTPUT);<br>
&nbsp;&nbsp;&nbsp; pinMode(LEDB, OUTPUT);<br>
&nbsp;&nbsp;&nbsp; digitalWrite(LEDR, HIGH);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // will turn the LED off<br>
&nbsp;&nbsp;&nbsp; digitalWrite(LEDG, HIGH);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // will turn the LED off<br>
&nbsp;&nbsp;&nbsp; digitalWrite(LEDB, HIGH);<br>
&nbsp;&nbsp;&nbsp; // summary of inferencing settings (from model_metadata.h)<br>
&nbsp;&nbsp;&nbsp; ei_printf("Inferencing settings:\n");<br>
&nbsp;&nbsp;&nbsp; ei_printf("\tInterval: %.2f ms.\n", (float)EI_CLASSIFIER_INTERVAL_MS);<br>
&nbsp;&nbsp;&nbsp; ei_printf("\tFrame size: %d\n", EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE);<br>
&nbsp;&nbsp;&nbsp; ei_printf("\tSample length: %d ms.\n", EI_CLASSIFIER_RAW_SAMPLE_COUNT / 16);<br>
&nbsp;&nbsp;&nbsp; ei_printf("\tNo. of classes: %d\n", sizeof(ei_classifier_inferencing_categories) /<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sizeof(ei_classifier_inferencing_categories[0]));</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; run_classifier_init();<br>
&nbsp;&nbsp;&nbsp; if (microphone_inference_start(EI_CLASSIFIER_SLICE_SIZE) == false) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ei_printf("ERR: Could not allocate audio buffer (size %d), this could be due to the window length of your model\r\n", EI_CLASSIFIER_RAW_SAMPLE_COUNT);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br>
&nbsp;&nbsp;&nbsp; }<br>
}</p>
<p dir="ltr">/**<br>
 * @brief&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Arduino main function. Runs the inferencing loop.<br>
 */<br>
void micro()<br>
{<br>
&nbsp;&nbsp;&nbsp; bool m = microphone_inference_record();<br>
&nbsp;&nbsp;&nbsp; if (!m) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ei_printf("ERR: Failed to record audio...\n");<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br>
&nbsp;&nbsp;&nbsp; }</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; signal_t signal;<br>
&nbsp;&nbsp;&nbsp; signal.total_length = EI_CLASSIFIER_SLICE_SIZE;<br>
&nbsp;&nbsp;&nbsp; signal.get_data = &amp;microphone_audio_signal_get_data;<br>
&nbsp;&nbsp;&nbsp; ei_impulse_result_t result = {0};</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; EI_IMPULSE_ERROR r = run_classifier_continuous(&amp;signal, &amp;result, debug_nn);<br>
&nbsp;&nbsp;&nbsp; if (r != EI_IMPULSE_OK) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ei_printf("ERR: Failed to run classifier (%d)\n", r);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br>
&nbsp;&nbsp;&nbsp; }</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; if (++print_results &gt;= (EI_CLASSIFIER_SLICES_PER_MODEL_WINDOW)) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // print the predictions<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ei_printf("Predictions ");<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ei_printf("(DSP: %d ms., Classification: %d ms., Anomaly: %d ms.)",<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result.timing.dsp, result.timing.classification, result.timing.anomaly);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ei_printf(": \n");<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (size_t ix = 0; ix &lt; EI_CLASSIFIER_LABEL_COUNT; ix++) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ei_printf("&nbsp;&nbsp;&nbsp; %s: %.5f\n", result.classification[ix].label,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result.classification[ix].value);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
#if EI_CLASSIFIER_HAS_ANOMALY == 1<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ei_printf("&nbsp;&nbsp;&nbsp; anomaly score: %.3f\n", result.anomaly);<br>
#endif</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print_results = 0;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; }</p>
<p dir="ltr">a =&nbsp; result.classification[0].value;<br>
b =&nbsp; result.classification[1].value;<br>
c =&nbsp; result.classification[2].value;<br>
d =&nbsp; result.classification[3].value;<br>
e =&nbsp; result.classification[4].value;<br>
}<br>
void direccion(){<br>
&nbsp;&nbsp;&nbsp;&nbsp; if (IMU.accelerationAvailable() &amp;&amp; IMU.gyroscopeAvailable() &amp;&amp; IMU.magneticFieldAvailable()) {<br>
&nbsp;&nbsp;&nbsp; IMU.readAcceleration(ax,ay,az);<br>
&nbsp;&nbsp;&nbsp; IMU.readGyroscope(gx,gy,gz);<br>
&nbsp;&nbsp;&nbsp; IMU.readMagneticField(mx,my,mz);</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; // Obtener las mediciones de aceleraci&#243;n, giroscopio y magnet&#243;metro<br>
&nbsp;&nbsp;&nbsp; float accelX = ax;<br>
&nbsp;&nbsp;&nbsp; float accelY = ay;<br>
&nbsp;&nbsp;&nbsp; float accelZ = az;<br>
&nbsp;&nbsp;&nbsp; float gyroX = gx;<br>
&nbsp;&nbsp;&nbsp; float gyroY = gy;<br>
&nbsp;&nbsp;&nbsp; float gyroZ = gz;<br>
&nbsp;&nbsp;&nbsp; float magX = mx;<br>
&nbsp;&nbsp;&nbsp; float magY = my;<br>
&nbsp;&nbsp;&nbsp; float magZ = mz;</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; // Actualizar el filtro de Madgwick con los datos de los sensores<br>
&nbsp;&nbsp;&nbsp; filter.update(gyroX, gyroY, gyroZ, accelX, accelY, accelZ, magX, magY, magZ);</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; // Obtener el &#225;ngulo de yaw del filtro<br>
&nbsp;&nbsp;&nbsp; float currentYaw = filter.getYaw();</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; // Imprimir el &#225;ngulo de yaw<br>
&nbsp;&nbsp;&nbsp; Serial.print("Yaw: ");<br>
&nbsp;&nbsp;&nbsp; Serial.println(currentYaw);</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; // Detectar el movimiento de yaw<br>
&nbsp;&nbsp;&nbsp; float yawDifference = (currentYaw - yaw)*5;<br>
&nbsp;&nbsp;&nbsp;&nbsp; //Serial.println(yawDifference);<br>
&nbsp;&nbsp;&nbsp;&nbsp; //Serial.println(yawThreshold);<br>
&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; if (abs(yawDifference) &lt; yawThreshold) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // No se mueve (centro)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Serial.println("Centro");<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dire = 0;<br>
&nbsp;&nbsp;&nbsp; } else if (yawDifference &gt; yawThreshold) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Movimiento a la derecha<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Serial.println("Izquierda");<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dire = 1;<br>
&nbsp;&nbsp;&nbsp; } else if (yawDifference &lt; -yawThreshold) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Movimiento a la izquierda<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Serial.println("Derecha");<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dire = 2;<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; </p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; // Actualizar el valor de yaw<br>
&nbsp;&nbsp;&nbsp; yaw = currentYaw;<br>
&nbsp;&nbsp;&nbsp; delay(100);<br>
&nbsp; }<br>
&nbsp; <br>
&nbsp; <br>
&nbsp; }<br><br><br><br><br><br></p>
<p dir="ltr">void loop(){<br>
&nbsp; micro();<br>
&nbsp; direccion();<br>
&nbsp; if (a &gt;= 0.80) {<br>
&nbsp; Serial.println("Left LED on");<br>
&nbsp; digitalWrite(LEDR, LOW);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // will turn the LED on<br>
&nbsp; digitalWrite(LEDG, HIGH);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // will turn the LED off<br>
&nbsp; digitalWrite(LEDB, HIGH);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // will turn the LED off<br>
&nbsp; color = 1;<br>
&nbsp; digitalWrite(ledPin3, HIGH);<br>
&nbsp; digitalWrite(ledPin2, HIGH);<br>
&nbsp; digitalWrite(ledPin1, HIGH);<br>
&nbsp; </p>
<p dir="ltr">}<br>
else if (c &gt;= 0.80) {<br>
&nbsp; Serial.println("Right LED on");<br>
&nbsp; digitalWrite(LEDR, HIGH);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // will turn the LED on<br>
&nbsp; digitalWrite(LEDG, LOW);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // will turn the LED off<br>
&nbsp; digitalWrite(LEDB, HIGH);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // will turn the LED off<br>
&nbsp; color = 2;<br>
&nbsp; digitalWrite(ledPin4, HIGH);<br>
&nbsp; digitalWrite(ledPin5, HIGH);<br>
&nbsp; digitalWrite(ledPin6, HIGH);<br>
}<br>
else if (d &gt;= 0.80) {<br>
&nbsp; Serial.println("Stop LED on");<br>
&nbsp; color = 3;<br>
&nbsp; <br>
}</p>
<p dir="ltr">if (color == 1 &amp; dire == 1){<br>
&nbsp; digitalWrite(LEDR, HIGH); <br>
&nbsp; digitalWrite(ledPin3, HIGH);<br>
&nbsp; digitalWrite(ledPin2, HIGH);<br>
&nbsp; digitalWrite(ledPin1, HIGH);<br>
&nbsp; delay(250);<br>
&nbsp; digitalWrite(ledPin1, LOW);<br>
&nbsp; digitalWrite(ledPin2, LOW);<br>
&nbsp; digitalWrite(ledPin3, LOW);<br>
&nbsp; delay(250);<br>
&nbsp; digitalWrite(ledPin3, HIGH);<br>
&nbsp; digitalWrite(ledPin2, HIGH);<br>
&nbsp; digitalWrite(ledPin1, HIGH);<br>
&nbsp; delay(250);<br>
&nbsp; digitalWrite(ledPin1, LOW);<br>
&nbsp; digitalWrite(ledPin2, LOW);<br>
&nbsp; digitalWrite(ledPin3, LOW);<br>
&nbsp; delay(250);<br>
&nbsp; digitalWrite(ledPin3, HIGH);<br>
&nbsp; digitalWrite(ledPin2, HIGH);<br>
&nbsp; digitalWrite(ledPin1, HIGH);<br>
&nbsp; delay(250);<br>
&nbsp; digitalWrite(ledPin1, LOW);<br>
&nbsp; digitalWrite(ledPin2, LOW);<br>
&nbsp; digitalWrite(ledPin3, LOW);<br>
&nbsp; delay(250);<br>
&nbsp; digitalWrite(ledPin3, HIGH);<br>
&nbsp; digitalWrite(ledPin2, HIGH);<br>
&nbsp; digitalWrite(ledPin1, HIGH);<br>
&nbsp; delay(250);<br>
&nbsp; digitalWrite(ledPin1, LOW);<br>
&nbsp; digitalWrite(ledPin2, LOW);<br>
&nbsp; digitalWrite(ledPin3, LOW);<br>
&nbsp; }<br>
else if (color == 2 &amp; dire == 2){<br>
&nbsp; digitalWrite(LEDG, LOW);<br>
&nbsp; digitalWrite(ledPin4, HIGH);<br>
&nbsp; digitalWrite(ledPin5, HIGH);<br>
&nbsp; digitalWrite(ledPin6, HIGH);<br>
&nbsp; delay(250);<br>
&nbsp; digitalWrite(ledPin4, LOW);<br>
&nbsp; digitalWrite(ledPin5, LOW);<br>
&nbsp; digitalWrite(ledPin6, LOW);<br>
&nbsp; delay(250);<br>
&nbsp; digitalWrite(ledPin4, HIGH);<br>
&nbsp; digitalWrite(ledPin5, HIGH);<br>
&nbsp; digitalWrite(ledPin6, HIGH);<br>
&nbsp; delay(250);<br>
&nbsp; digitalWrite(ledPin4, LOW);<br>
&nbsp; digitalWrite(ledPin5, LOW);<br>
&nbsp; digitalWrite(ledPin6, LOW);<br>
&nbsp; delay(250);<br>
&nbsp; digitalWrite(ledPin4, HIGH);<br>
&nbsp; digitalWrite(ledPin5, HIGH);<br>
&nbsp; digitalWrite(ledPin6, HIGH);<br>
&nbsp; delay(250);<br>
&nbsp; digitalWrite(ledPin4, LOW);<br>
&nbsp; digitalWrite(ledPin5, LOW);<br>
&nbsp; digitalWrite(ledPin6, LOW);<br>
&nbsp; delay(250);<br>
&nbsp; digitalWrite(ledPin4, HIGH);<br>
&nbsp; digitalWrite(ledPin5, HIGH);<br>
&nbsp; digitalWrite(ledPin6, HIGH);<br>
&nbsp; delay(250);<br>
&nbsp; digitalWrite(ledPin4, LOW);<br>
&nbsp; digitalWrite(ledPin5, LOW);<br>
&nbsp; digitalWrite(ledPin6, LOW);<br>
}</p>
<p dir="ltr">else if (color == 3){<br>
&nbsp; digitalWrite(LEDR, HIGH);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // will turn the LED on<br>
&nbsp; digitalWrite(LEDG, HIGH);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // will turn the LED off<br>
&nbsp; digitalWrite(LEDB, LOW);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // will turn the LED off<br>
&nbsp; digitalWrite(ledPin1, HIGH);<br>
&nbsp; digitalWrite(ledPin2, HIGH);<br>
&nbsp; digitalWrite(ledPin3, HIGH);<br>
&nbsp; digitalWrite(ledPin4, HIGH);<br>
&nbsp; digitalWrite(ledPin5, HIGH);<br>
&nbsp; digitalWrite(ledPin6, HIGH);<br>
&nbsp; delay(500);<br>
&nbsp; digitalWrite(ledPin1, LOW);<br>
&nbsp; digitalWrite(ledPin2, LOW);<br>
&nbsp; digitalWrite(ledPin3, LOW);<br>
&nbsp; digitalWrite(ledPin4, LOW);<br>
&nbsp; digitalWrite(ledPin5, LOW);<br>
&nbsp; digitalWrite(ledPin6, LOW);<br>
&nbsp; delay(500);<br>
&nbsp; <br>
&nbsp; <br>
&nbsp; <br>
&nbsp; }<br>
&nbsp; }<br>
/**<br>
 * @brief&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PDM buffer full callback<br>
 *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Get data and call audio thread callback<br>
 */<br>
static void pdm_data_ready_inference_callback(void)<br>
{<br>
&nbsp;&nbsp;&nbsp; int bytesAvailable = PDM.available();</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; // read into the sample buffer<br>
&nbsp;&nbsp;&nbsp; int bytesRead = PDM.read((char *)&amp;sampleBuffer[0], bytesAvailable);</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; if (record_ready == true) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i&lt;bytesRead&gt;&gt; 1; i++) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inference.buffers[inference.buf_select][inference.buf_count++] = sampleBuffer[i];</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (inference.buf_count &gt;= inference.n_samples) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inference.buf_select ^= 1;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inference.buf_count = 0;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inference.buf_ready = 1;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; }<br>
}</p>
<p dir="ltr">/**<br>
 * @brief&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Init inferencing struct and setup/start PDM<br>
 *<br>
 * @param[in]&nbsp; n_samples&nbsp; The n samples<br>
 *<br>
 * @return&nbsp;&nbsp;&nbsp;&nbsp; { description_of_the_return_value }<br>
 */<br>
static bool microphone_inference_start(uint32_t n_samples)<br>
{<br>
&nbsp;&nbsp;&nbsp; inference.buffers[0] = (signed short *)malloc(n_samples * sizeof(signed short));</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; if (inference.buffers[0] == NULL) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<br>
&nbsp;&nbsp;&nbsp; }</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; inference.buffers[1] = (signed short *)malloc(n_samples * sizeof(signed short));</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; if (inference.buffers[1] == NULL) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; free(inference.buffers[0]);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<br>
&nbsp;&nbsp;&nbsp; }</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; sampleBuffer = (signed short *)malloc((n_samples &gt;&gt; 1) * sizeof(signed short));</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; if (sampleBuffer == NULL) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; free(inference.buffers[0]);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; free(inference.buffers[1]);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<br>
&nbsp;&nbsp;&nbsp; }</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; inference.buf_select = 0;<br>
&nbsp;&nbsp;&nbsp; inference.buf_count = 0;<br>
&nbsp;&nbsp;&nbsp; inference.n_samples = n_samples;<br>
&nbsp;&nbsp;&nbsp; inference.buf_ready = 0;</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; // configure the data receive callback<br>
&nbsp;&nbsp;&nbsp; PDM.onReceive(&amp;pdm_data_ready_inference_callback);</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; PDM.setBufferSize((n_samples &gt;&gt; 1) * sizeof(int16_t));</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; // initialize PDM with:<br>
&nbsp;&nbsp;&nbsp; // - one channel (mono mode)<br>
&nbsp;&nbsp;&nbsp; // - a 16 kHz sample rate<br>
&nbsp;&nbsp;&nbsp; if (!PDM.begin(1, EI_CLASSIFIER_FREQUENCY)) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ei_printf("Failed to start PDM!");<br>
&nbsp;&nbsp;&nbsp; }</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; // set the gain, defaults to 20<br>
&nbsp;&nbsp;&nbsp; PDM.setGain(127);</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; record_ready = true;</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; return true;<br>
}</p>
<p dir="ltr">/**<br>
 * @brief&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Wait on new data<br>
 *<br>
 * @return&nbsp;&nbsp;&nbsp;&nbsp; True when finished<br>
 */<br>
static bool microphone_inference_record(void)<br>
{<br>
&nbsp;&nbsp;&nbsp; bool ret = true;</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; if (inference.buf_ready == 1) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ei_printf(<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "Error sample buffer overrun. Decrease the number of slices per model window "<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "(EI_CLASSIFIER_SLICES_PER_MODEL_WINDOW)\n");<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ret = false;<br>
&nbsp;&nbsp;&nbsp; }</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; while (inference.buf_ready == 0) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(1);<br>
&nbsp;&nbsp;&nbsp; }</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; inference.buf_ready = 0;</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; return ret;<br>
}</p>
<p dir="ltr">/**<br>
 * Get raw audio signal data<br>
 */<br>
static int microphone_audio_signal_get_data(size_t offset, size_t length, float *out_ptr)<br>
{<br>
&nbsp;&nbsp;&nbsp; numpy::int16_to_float(&amp;inference.buffers[inference.buf_select ^ 1][offset], out_ptr, length);</p>
<p dir="ltr">&nbsp;&nbsp;&nbsp; return 0;<br>
}</p>
<p dir="ltr">/**<br>
 * @brief&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Stop PDM and release buffers<br>
 */<br>
static void microphone_inference_end(void)<br>
{<br>
&nbsp;&nbsp;&nbsp; PDM.end();<br>
&nbsp;&nbsp;&nbsp; free(inference.buffers[0]);<br>
&nbsp;&nbsp;&nbsp; free(inference.buffers[1]);<br>
&nbsp;&nbsp;&nbsp; free(sampleBuffer);<br>
}</p>
<p dir="ltr">#if !defined(EI_CLASSIFIER_SENSOR) || EI_CLASSIFIER_SENSOR != EI_CLASSIFIER_SENSOR_MICROPHONE<br>
#error "Invalid model for current sensor."<br>
#endif</p>
</body></html>

Credits

Chris Anderson
1 project • 0 followers

Comments