40 #include "physical_constants.h"
45 #include "modulesettings.h"
46 #include "vibrationanalysisoutput.h"
47 #include "vibrationanalysissettings.h"
52 #define MAX_QUEUE_SIZE 2
54 #define STACK_SIZE_BYTES (200 + 448 + 16 + (2*3*window_size)*0) // The memory requirement grows linearly
61 #define TASK_PRIORITY PIOS_THREAD_PRIO_LOW
62 #define SETTINGS_THROTTLING_MS 100
64 #define MAX_ACCEL_RANGE 16 // Maximum accelerometer resolution in [g]
65 #define FLOAT_TO_FIXED (32768/(MAX_ACCEL_RANGE*2)-1) // This is the scaling constant that scales input floats
66 #define VIBRATION_ELEMENTS_COUNT 16 // Number of elements per object Instance
68 #define MAX_WINDOW_SIZE 1024
71 #define USE_SINGLE_INSTANCE_BUFFERS 1
78 static TaskInfoRunningElem
task;
121 #ifdef PIOS_FREE_IMPLEMENTED
157 uint16_t window_size;
158 uint16_t instances = 1;
174 VibrationAnalysisSettingsFFTWindowSizeOptions window_size_enum;
175 VibrationAnalysisSettingsFFTWindowSizeGet(&window_size_enum);
176 switch (window_size_enum) {
177 case VIBRATIONANALYSISSETTINGS_FFTWINDOWSIZE_16:
180 case VIBRATIONANALYSISSETTINGS_FFTWINDOWSIZE_64:
183 case VIBRATIONANALYSISSETTINGS_FFTWINDOWSIZE_256:
186 case VIBRATIONANALYSISSETTINGS_FFTWINDOWSIZE_1024:
202 #ifndef USE_SINGLE_INSTANCE_BUFFERS
204 uint16_t existing_instances = VibrationAnalysisOutputGetNumInstances();
205 if(existing_instances < instances) {
208 uint16_t ret = VibrationAnalysisOutputCreateInstance();
218 if (VibrationAnalysisOutputGetNumInstances() < instances) {
223 #ifdef PIOS_FREE_IMPLEMENTED
241 #ifdef USE_SINGLE_INSTANCE_BUFFERS
245 #ifdef PIOS_FREE_IMPLEMENTED
302 if (ModuleSettingsInitialize() == -1) {
307 #ifdef MODULE_VibrationAnalysis_BUILTIN
310 uint8_t module_state[MODULESETTINGS_ADMINSTATE_NUMELEM];
311 ModuleSettingsAdminStateGet(module_state);
312 if (module_state[MODULESETTINGS_ADMINSTATE_VIBRATIONANALYSIS] == MODULESETTINGS_ADMINSTATE_ENABLED) {
323 if (VibrationAnalysisSettingsInitialize() == -1 || VibrationAnalysisOutputInitialize() == -1) {
340 uint32_t lastSettingsUpdateTime;
341 uint8_t runAnalysisFlag = VIBRATIONANALYSISSETTINGS_TESTINGSTATUS_OFF;
342 uint16_t sampleRate_ms = 100;
343 uint16_t sample_count;
348 AccelsConnectQueue(queue);
351 VibrationAnalysisOutputData vibrationAnalysisOutputData;
359 #ifdef USE_SINGLE_INSTANCE_BUFFERS
360 uint16_t instance_number = 0;
363 uint8_t runningAcquisition = 0;
373 VibrationAnalysisSettingsTestingStatusGet(&runAnalysisFlag);
376 if (runAnalysisFlag == VIBRATIONANALYSISSETTINGS_TESTINGSTATUS_OFF) {
381 VibrationAnalysisSettingsSampleRateGet(&sampleRate_ms);
382 sampleRate_ms = sampleRate_ms > 0 ? sampleRate_ms : 1;
391 runningAcquisition = 1;
403 AccelsData accels_data;
404 AccelsGet(&accels_data);
447 #ifdef USE_SINGLE_INSTANCE_BUFFERS
452 vibrationAnalysisOutputData.index = instance_number;
458 VibrationAnalysisOutputInstSet(0, &vibrationAnalysisOutputData);
459 VibrationAnalysisOutputInstUpdated(0);
462 instance_number = (instance_number + 1) %
vtd->
instances;
469 vibrationAnalysisOutputData.index =
i;
470 vibrationAnalysisOutputData.x[k] =
vtd->
accel_buffer_x[k + VIBRATION_ELEMENTS_COUNT *
i];
471 vibrationAnalysisOutputData.y[k] =
vtd->
accel_buffer_y[k + VIBRATION_ELEMENTS_COUNT *
i];
472 vibrationAnalysisOutputData.z[k] =
vtd->
accel_buffer_z[k + VIBRATION_ELEMENTS_COUNT *
i];
475 VibrationAnalysisOutputInstSet(
i, &vibrationAnalysisOutputData);
476 VibrationAnalysisOutputInstUpdated(
i);
486 runningAcquisition = 0;
uint16_t accels_sum_count
uint32_t PIOS_Thread_Systime(void)
float accels_static_bias_x
struct pios_queue * PIOS_Queue_Create(size_t queue_length, size_t item_size)
void * PIOS_malloc(size_t size)
static int32_t VibrationAnalysisInitialize(void)
bool PIOS_Queue_Receive(struct pios_queue *queuep, void *itemp, uint32_t timeout_ms)
float accels_static_bias_y
static struct pios_thread * taskHandle
int32_t TaskMonitorRemove(TaskInfoRunningElem task)
static struct VibrationAnalysis_data * vtd
#define PIOS_QUEUE_TIMEOUT_MAX
#define MODULE_INITCALL(ifn, sfn)
struct pios_thread * PIOS_Thread_Create(void(*fp)(void *), const char *namep, size_t stack_bytes, void *argp, enum pios_thread_prio_e prio)
static TaskInfoRunningElem task
int32_t TaskMonitorAdd(TaskInfoRunningElem task, struct pios_thread *threadp)
static void VibrationAnalysisTask(void *parameters)
#define SETTINGS_THROTTLING_MS
float accels_static_bias_z
static int32_t VibrationAnalysisStart(void)
void PIOS_free(void *buf)
void PIOS_Thread_Sleep(uint32_t time_ms)
Includes PiOS and core architecture components.
static struct pios_queue * queue
static uint32_t lastSysTime
void PIOS_Thread_Delete(struct pios_thread *threadp)
if(BaroAltitudeHandle()!=NULL)
static void VibrationAnalysisCleanup(void)
static bool module_enabled
#define VIBRATION_ELEMENTS_COUNT