-
-
Save ranchpal/5eed78ceb52b28f7eb0a2583f5d446cf to your computer and use it in GitHub Desktop.
Edge impulse Spectogram snippet
This file contains 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 "user_ai_model.h" | |
#include "user_imu_buffer.h" | |
/* edge impulse */ | |
#include "edge-impulse-sdk/dsp/numpy_types.h" | |
#include "edge-impulse-sdk/porting/ei_classifier_porting.h" | |
#include "edge-impulse-sdk/classifier/ei_classifier_types.h" | |
#include "edge-impulse-sdk/classifier/ei_run_classifier_c.h" | |
#include "model-parameters/model_metadata.h" | |
/* cow_state_buffer, holds the cow state as inferred by the model */ | |
uint8_t cow_state_buffer[MAX_ELEMETS_IN_COW_STATE_BUFFER]; | |
uint16_t cow_state_buffer_count = 0x00; | |
/* cow_state_buffer_ready_flag is set when 'cow_state_buffer' is full */ | |
bool cow_state_buffer_ready_flag = false; | |
uint8_t cow_state_mode_calc_buffer[10]; | |
/* buffer in which the imu data is stored */ | |
extern float imu_buffer[IMU_BUFFER_SIZE]; | |
/* @brief A function to get the feature data buffer */ | |
int get_feature_data(size_t offset, size_t length, float *out_ptr) | |
{ | |
memcpy(out_ptr, imu_buffer + offset, length * sizeof(float)); | |
return 0; | |
} | |
/* @brief A function to run the edgeimpulse model */ | |
/* This function takes in the 50 samples of data and return a label */ | |
void user_ai_model_run(float *p_data, uint8_t *p_label) | |
{ | |
uint8_t i = 0x00; | |
float max_prob = 0.00; | |
/* inferencing model variables*/ | |
signal_t features_signal; | |
ei_impulse_result_t result; | |
EI_IMPULSE_ERROR res; | |
features_signal.total_length = IMU_BUFFER_SIZE; | |
features_signal.get_data = &get_feature_data; | |
res = ei_run_classifier(&features_signal, &result, false); | |
celium_log("Classifier result = %d\r\n", res); | |
for(i = 0; i < EI_CLASSIFIER_LABEL_COUNT; i ++) | |
{ | |
if(max_prob < result.classification[i].value * 100) | |
{ | |
max_prob = result.classification[i].value * 100; | |
*p_label = atoi(result.classification[i].label); | |
} | |
} | |
//celium_log("Cow State = %d\r\n", *p_label); | |
} | |
/* @brief A function to add cow state to a buffer | |
* This buffer would be used to get mode of the states every 60secs and update the adv data */ | |
void user_ai_model_store_cow_state(uint8_t state) | |
{ | |
cow_state_buffer[cow_state_buffer_count] = state; | |
cow_state_buffer_count += 1; | |
if(cow_state_buffer_count == MAX_ELEMETS_IN_COW_STATE_BUFFER) | |
{ | |
cow_state_buffer_ready_flag = true; | |
} | |
} | |
/* @brief A function to check if the state buffer is ready to calc the mode | |
*/ | |
bool is_cow_state_buffer_ready_to_calc_mode(void) | |
{ | |
return cow_state_buffer_ready_flag; | |
} | |
/* @brief A function clear cow state buffer ready flag | |
*/ | |
void user_ai_model_clr_cow_state_buffer_ready_flag(void) | |
{ | |
cow_state_buffer_ready_flag = false; | |
} | |
/* @brief A function to calculate the mode of the cow state | |
*/ | |
uint8_t user_ai_model_calc_mode(void) | |
{ | |
uint16_t i = 0x00; | |
memset(&cow_state_mode_calc_buffer[0], 0x00, 10); | |
uint16_t max_value = 0x00, max_index = 0x00; | |
for(i = 0; i < MAX_ELEMETS_IN_COW_STATE_BUFFER; i++) | |
{ | |
cow_state_mode_calc_buffer[cow_state_buffer[i]] += 1; | |
} | |
for(i = 0; i < 10; i++) | |
{ | |
if(max_value < cow_state_mode_calc_buffer[i]) | |
{ | |
max_value = cow_state_mode_calc_buffer[i]; | |
max_index = i; | |
} | |
} | |
memset(&cow_state_buffer[0], 0x00, MAX_ELEMETS_IN_COW_STATE_BUFFER); | |
cow_state_buffer_count = 0x00; | |
return ((uint8_t)max_index); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The function 'user_ai_model_run(uint8_t *p_data, uint8_t *p_label)' runs every 200ms from the main thread. The 'ei_run_classifier' returns -5 error code.