How to operate the buzzer in the desired behavior after machine learning

#include <kimyoungmoon-project-1_inference.h>
#include <Arduino_LSM9DS1.h>

#define CONVERT_G_TO_MS2 9.80665f

static bool debug_nn = false; // Set this to true to see e.g. features generated from the raw signal

void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println(“Edge Impulse Inferencing Demo”);

if (!IMU.begin()) {
    ei_printf("Failed to initialize IMU!\r\n");
}
else {
    ei_printf("IMU initialized\r\n");
}

if (EI_CLASSIFIER_RAW_SAMPLES_PER_FRAME != 3) {
    ei_printf("ERR: EI_CLASSIFIER_RAW_SAMPLES_PER_FRAME should be equal to 3 (the 3 sensor axes)\n");
    return;
}

}

void ei_printf(const char *format, …) {
static char print_buf[1024] = { 0 };

va_list args;
va_start(args, format);
int r = vsnprintf(print_buf, sizeof(print_buf), format, args);
va_end(args);

if (r > 0) {
Serial.write(print_buf);
}
}

void loop()
{
ei_printf("\nStarting inferencing in 2 seconds…\n");

delay(2000);

ei_printf("Sampling...\n");

// Allocate a buffer here for the values we'll read from the IMU
float buffer[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE] = { 0 };

for (size_t ix = 0; ix < EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE; ix += 3) {
    // Determine the next tick (and then sleep later)
    uint64_t next_tick = micros() + (EI_CLASSIFIER_INTERVAL_MS * 1000);

    IMU.readAcceleration(buffer[ix], buffer[ix + 1], buffer[ix + 2]);

    buffer[ix + 0] *= CONVERT_G_TO_MS2;
    buffer[ix + 1] *= CONVERT_G_TO_MS2;
    buffer[ix + 2] *= CONVERT_G_TO_MS2;

    delayMicroseconds(next_tick - micros());
}

// Turn the raw buffer in a signal which we can the classify
signal_t signal;
int err = numpy::signal_from_buffer(buffer, EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE, &signal);
if (err != 0) {
    ei_printf("Failed to create signal from buffer (%d)\n", err);
    return;
}

// Run the classifier
ei_impulse_result_t result = { 0 };

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

// 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
}

#if !defined(EI_CLASSIFIER_SENSOR) || EI_CLASSIFIER_SENSOR != EI_CLASSIFIER_SENSOR_ACCELEROMETER
#error “Invalid model for current sensor”
#endif

I want to know how to operate the buzzer in the desired motion after machine learning.

Hi @KIMYOUNGMOON,

Can you provide more details about your project? Which buzzer are you referring to?

Aurelien

If you detect the vibration of the 3D printer and detect a defect, the buzzer may sound.

The buzzer I use is Passive Buzzer.

You can write your buzzer code after printing the predictions. The result.classification array contains the predicted values, you could add an if statement based on a threshold to activate the buzzer for instance.

Aurelien

1 Like

Oh, thank you.
Could you write an example if you have time?
I don’t quite understand.

You can do something like this when “yourlabel” is detected with a prediction confidence more than 70%:

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 (result.classification[ix].label == "yourlabel" && result.classification[ix].value > 0.7) {
        // Add buzzer code here
    }
}

Hello!

I do not have a passive buzzer but I guess you could right something similar to this code (change the #define buzzerPin according to your GPIO pinout):

#define buzzerPin 7
#define anomaly_threshold 0.5

void setup() {

  pinMode(buzzerPin,OUTPUT);

}

And then add something similar to this in your code:


 err = run_classifier(&signal, &result, debug_nn);
 if (err != EI_IMPULSE_OK) {
    ei_printf("ERR: Failed to run classifier (%d)\n", err);
    return;
 }
 if(result.anomaly > anomaly_threshold){
    ei_printf(" anomaly score: %.3f\n", result.anomaly);
    digitalWrite(buzzerPin, HIGH);
 }

Regards,

1 Like

Oh, thank you. :slight_smile:
What does yourlable mean?

yourlabel in @aurel suggestion means “your label” (it can up, down, idle_state or whatever your decided to call your labels in the studio) :wink:

Regards

I don’t know well, can you show me one example?

Hello @KIMYOUNGMOON,

Unfortunately, I do not have the hardware to test and to provide you the exact piece of code that will work for you.

Regards,

Louis

Can you give me an example from the code I wrote?

You have to solve :slight_smile: Thank you.

Hi @KIMYOUNGMOON,

This is more an Arduino related question. Which buzzer and library are you using? I would suggest to look at the Arduino forum if you need help on programming using their IDE.

Aurelien

1 Like