Created
June 27, 2022 18:37
-
-
Save charlie2951/91509e9c42c730c987f54353cc1b238a to your computer and use it in GitHub Desktop.
Number gesture detection main C++ source file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <Numer_detection_inferencing.h> | |
#include "ei_classifier_porting.h" | |
#include "pico/stdlib.h" | |
#include "ei_run_classifier.h" | |
#include "hardware/gpio.h" | |
#include "hardware/adc.h" | |
/* Private variables ------------------------------------------------------- */ | |
static bool debug_nn = false; // Set this to true to see e.g. features generated from the raw signal | |
const uint LED_PIN = 25; | |
float readAxisAccelation (int adc_n) { | |
adc_select_input(adc_n); | |
unsigned int axis_raw = 0; | |
axis_raw=adc_read(); | |
float axis_g=axis_raw*16; //convert 12 bit data to 16 bit using zero padding | |
return axis_g; | |
} | |
uint64_t ei_read_timer_ms() { | |
return to_ms_since_boot(get_absolute_time()); | |
} | |
uint64_t ei_read_timer_us() { | |
return to_us_since_boot(get_absolute_time()); | |
} | |
int main() | |
{ | |
stdio_init_all(); | |
gpio_init(LED_PIN); | |
gpio_set_dir(LED_PIN, GPIO_OUT); | |
gpio_put(LED_PIN, 0); | |
adc_init(); | |
adc_gpio_init(26); | |
adc_gpio_init(27); | |
adc_gpio_init(28); | |
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 1; | |
} | |
while (true){ | |
ei_printf("\nStarting inferencing in 2 seconds...\n"); | |
sleep_ms(2000); | |
gpio_put(LED_PIN, 1); | |
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 = ei_read_timer_us() + (EI_CLASSIFIER_INTERVAL_MS * 1000); | |
buffer[ix] = readAxisAccelation (0); | |
buffer[ix + 1] = readAxisAccelation (1); | |
buffer[ix + 2] = readAxisAccelation (2); | |
sleep_us(next_tick - ei_read_timer_us()); | |
} | |
// 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 1; | |
} | |
// 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 1; | |
} | |
// 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); | |
float val = result.classification[ix].value; | |
if(val > 0.5) | |
ei_printf("Predicted Number is -> %s", result.classification[ix].label); | |
//else | |
//ei_printf("%s: %.5f\n", result.classification[ix].label, 0); | |
} | |
gpio_put(LED_PIN, 0); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment