Stima V4 Slave RAIN  4.2
accelerometer_task.cpp
Go to the documentation of this file.
1 
30 #define TRACE_LEVEL ACCELEROMETER_TASK_TRACE_LEVEL
31 #define LOCAL_TASK_ID ACCELEROMETER_TASK_ID
32 
34 
35 #if (ENABLE_ACCELEROMETER)
41 AccelerometerTask::AccelerometerTask(const char *taskName, uint16_t stackSize, uint8_t priority, AccelerometerParam_t accelerometerParam) : Thread(taskName, stackSize, priority), param(accelerometerParam)
42 {
43  // Start WDT controller and TaskState Flags
46 
47  // Setup register mode && Load or Init configuration
49 
50  // Starting Class
52 
54  Start();
55 };
56 
57 #if (ENABLE_STACK_USAGE)
59 void AccelerometerTask::TaskMonitorStack()
60 {
61  uint16_t stackUsage = (uint16_t)uxTaskGetStackHighWaterMark( NULL );
62  if((stackUsage) && (stackUsage < param.system_status->tasks[LOCAL_TASK_ID].stack)) {
63  param.systemStatusLock->Take();
65  param.systemStatusLock->Give();
66  }
67 }
68 #endif
69 
72 void AccelerometerTask::TaskWatchDog(uint32_t millis_standby)
73 {
74  // Local TaskWatchDog update
75  param.systemStatusLock->Take();
76  // Update WDT Signal (Direct or Long function Timered)
77  if(millis_standby)
78  {
79  // Check 1/2 Freq. controller ready to WDT only SET flag
80  if((millis_standby) < WDT_CONTROLLER_MS / 2) {
82  } else {
84  // Add security milimal Freq to check
86  }
87  }
88  else
90  param.systemStatusLock->Give();
91 }
92 
97 void AccelerometerTask::TaskState(uint8_t state_position, uint8_t state_subposition, task_flag state_operation)
98 {
99  // Local TaskWatchDog update
100  param.systemStatusLock->Take();
101  // Signal Task sleep/disabled mode from request (Auto SET WDT on Resume)
103  (state_operation==task_flag::normal))
105  param.system_status->tasks[LOCAL_TASK_ID].state = state_operation;
107  param.system_status->tasks[LOCAL_TASK_ID].running_sub = state_subposition;
108  param.systemStatusLock->Give();
109 }
110 
113 {
114  // Local parameter for command and state accelerometer
115  bool is_module_ready;
116  uint8_t hardware_check_attempt;
117  bool start_calibration = false;
118 
119  // System message data queue structured
120  system_message_t system_message;
121 
122  // Start Running Monitor and First WDT normal state
123  #if (ENABLE_STACK_USAGE)
124  TaskMonitorStack();
125  #endif
127 
128  while (true)
129  {
130 
131  // ********* SYSTEM QUEUE MESSAGE ***********
132  // enqueud system message from caller task
133  if (!param.systemMessageQueue->IsEmpty()) {
134  // Read queue in test mode
135  if (param.systemMessageQueue->Peek(&system_message, 0))
136  {
137  // Its request addressed into this TASK... -> pull
138  if(system_message.task_dest == ACCELEROMETER_TASK_ID)
139  {
140  // Pull && elaborate command, after response if...
141  param.systemMessageQueue->Dequeue(&system_message);
142  if(system_message.command.do_calib) // == Calibrate && Save {
143  {
144  start_calibration = true;
145  }
146  }
147  // Its request addressed into ALL TASK... -> no pull (only SUPERVISOR or exernal gestor)
148  else if(system_message.task_dest == ALL_TASK_ID)
149  {
150  // Pull && elaborate command,
151  if(system_message.command.do_sleep)
152  {
153  // Start module sleep procedure
154  powerDownModule();
155  // Enter sleep module OK and update WDT
158  Delay(Ticks::MsToTicks(ACCELEROMETER_TASK_SLEEP_DELAY_MS));
160  // Restore module from Sleep
162  }
163  }
164  }
165  }
166 
167  // Standard TASK switch main
168  switch (state)
169  {
171  TRACE_VERBOSE_F(F("ACCELEROMETER_STATE_INIT -> ACCELEROMETER_STATE_CHECK_HARDWARE\r\n"));
173  // Signal reset error error and status to system state
174  param.systemStatusLock->Take();
178  param.systemStatusLock->Give();
179  Delay(Ticks::MsToTicks(ACCELEROMETER_WAIT_CHECK_HARDWARE));
180  hardware_check_attempt = 0;
181  is_module_ready = false;
182  break;
183 
185  if (!is_module_ready)
186  {
187  if (checkModule()) {
188  TRACE_VERBOSE_F(F("ACCELEROMETER_STATE_CHECK_HARDWARE -> ACCELEROMETER_STATE_SETUP_MODULE\r\n"));
190  break;
191  }
192  // Wait for HW Check
193  hardware_check_attempt++;
194  Delay(Ticks::MsToTicks(ACCELEROMETER_WAIT_CHECK_HARDWARE));
195  if(hardware_check_attempt >= ACCELEROMETER_MAX_CHECK_ATTEMPT)
197  } else {
199  }
200  break;
201 
203  setupModule();
204  is_module_ready = true;
205  // System inform Accelerometer ready hardware OK
206  param.systemStatusLock->Take();
208  param.systemStatusLock->Give();
209  TRACE_VERBOSE_F(F("ACCELEROMETER_STATE_SETUP_MODULE -> ACCELEROMETER_STATE_CHECK_OPERATION\r\n"));
211  break;
212 
214  if(readModule()) {
215  // Checking bubble error limit (with Mirror Error "Z" become 0.001 -> 0.999 X,Y 0.001 -> -0.001)
216  if(((abs(value_x) > BUBBLE_ANGLE_ERROR) && (abs(value_x) < BUBBLE_ANGLE_MIRROR)) ||
217  ((abs(value_y) > BUBBLE_ANGLE_ERROR) && (abs(value_y) < BUBBLE_ANGLE_MIRROR)) ||
218  ((abs(value_z) > BUBBLE_ANGLE_ERROR) && (abs(value_z) < BUBBLE_ANGLE_MIRROR)))
219  {
221  // Start alert flags
222  param.systemStatusLock->Take();
225  param.systemStatusLock->Give();
226  } else {
227  // Check event for real alarm set value
230  }
231  }
232  } else {
233  // Restore alert flag
235  param.systemStatusLock->Take();
238  param.systemStatusLock->Give();
239  }
240  }
241  TRACE_INFO_F(F("X[ 0.%03d ] | Y[ 0.%03d ] | Z[ 0.%03d ]\r\n"), (int)(abs(value_x)*1000), (int)(abs(value_y)*1000), (int)(abs(value_z)*1000), OK_STRING);
242  if(start_calibration) {
243  TRACE_INFO_F(F("ACCELEROMETER Start calibration\r\n"));
244  calibrate(false, true);
246  start_calibration = false;
247  }
248  }
249  break;
250 
252  TRACE_VERBOSE_F(F("ACCELEROMETER_STATE_END -> SUSPEND()\r\n"));
253  // Disable control TASK
255  Suspend();
257  // On Restore Next INIT
259  break;
260 
262  TRACE_VERBOSE_F(F("ACCELEROMETER_STATE_FAIL -> SUSPEND()\r\n"));
263  // Disable control TASK
265  Suspend();
267  // On Restore Next INIT
269  break;
270  }
271 
272  #if (ENABLE_STACK_USAGE)
273  TaskMonitorStack();
274  #endif
275 
276  // Local WatchDog update;
278 
279  // MAX One switch step for Task Waiting Next Step
280  DelayUntil(Ticks::MsToTicks(ACCELEROMETER_TASK_WAIT_DELAY_MS));
281  }
282 }
283 
286 {
287  // Verify config valid param
288  bool register_config_valid = true;
289  // Param Reading
290  static uavcan_register_Value_1_0 val = {0};
291 
292  TRACE_INFO_F(F("ACCELEROMETER: Load configuration...\r\n"));
293 
295  // Reading RMAP Module identify Param -> (READ/WRITE)
296  // uint8_t config_valid; TYPE ELEMENT ID
297  // uint8_t module_power; Frequency get data
298  if(register_config_valid) {
299  // Select type register (uint_8)
300  uavcan_register_Value_1_0_select_natural8_(&val);
301  val.natural8.value.count = 2;
302  // Loading Default
303  val.natural8.value.elements[0] = IIS328DQ_ID;
304  val.natural8.value.elements[1] = Accelerometer::IIS328DQ_ODR_5Hz2;
305  param.registerAccessLock->Take();
306  param.clRegister->read("rmap.accelerometer.config", &val);
307  param.registerAccessLock->Give();
308  if(uavcan_register_Value_1_0_is_natural8_(&val) && (val.natural8.value.count != 2)) {
309  register_config_valid = false;
310  } else {
311  accelerometer_configuration.config_valid = val.natural8.value.elements[0];
312  accelerometer_configuration.module_power = (Accelerometer::iis328dq_dr_t) val.natural8.value.elements[1];
313  }
314  }
315 
316  // Reading RMAP Module Offset Config -> (READ/WRITE)
317  // float_t x; Offset (X)
318  // float_t y; Offset (Y)
319  // float_t z; Offset (Z)
320  if(register_config_valid) {
321  // Select type register (float_)
322  uavcan_register_Value_1_0_select_real32_(&val);
323  val.real32.value.count = 3;
324  // Loading Default
325  val.real32.value.elements[0] = 0.0;
326  val.real32.value.elements[1] = 0.0;
327  val.real32.value.elements[2] = 0.0;
328  param.registerAccessLock->Take();
329  param.clRegister->read("rmap.accelerometer.offset", &val);
330  param.registerAccessLock->Give();
331  if(uavcan_register_Value_1_0_is_real32_(&val) && (val.real32.value.count != 3)) {
332  register_config_valid = false;
333  } else {
334  accelerometer_configuration.offset_x = val.real32.value.elements[0];
335  accelerometer_configuration.offset_y = val.real32.value.elements[1];
336  accelerometer_configuration.offset_z = val.real32.value.elements[2];
337  }
338  }
339 
340  // Validation Byte Config (Init Defualt Value)
341  if (!register_config_valid || (accelerometer_configuration.config_valid != IIS328DQ_ID))
342  {
343  // Reset configuration...
344  saveConfiguration(true);
345  }
347 }
348 
351 {
352  TRACE_INFO_F(F("--> accelerometer config: \r\n"));
353  TRACE_INFO_F(F("--> config flag: %d\r\n"), accelerometer_configuration.config_valid);
354  TRACE_INFO_F(F("--> power mode: %d\r\n"), (int)accelerometer_configuration.module_power);
355  TRACE_INFO_F(F("--> offset X value: %d\r\n"), (int)(accelerometer_configuration.offset_x * 1000));
356  TRACE_INFO_F(F("--> offset Y value: %d\r\n"), (int)(accelerometer_configuration.offset_y * 1000));
357  TRACE_INFO_F(F("--> offset Z value: %d\r\n"), (int)(accelerometer_configuration.offset_z * 1000));
358 }
359 
363 {
364  // Param Writing
365  static uavcan_register_Value_1_0 val = {0};
366 
367  TRACE_INFO_F(F("Attempt to write accelerometer configuration... [ %s ]\r\n"), OK_STRING);
368 
369  // Loading defualt request
370  if (is_default)
371  {
377  }
378 
379  // Writing RMAP Module identify Param -> (READ/WRITE)
380  // uint8_t config_valid; TYPE ELEMENT ID
381  // uint8_t module_power; Frequency get data
382  // Select type register (uint_8)
383  uavcan_register_Value_1_0_select_natural8_(&val);
384  val.natural8.value.count = 2;
385  val.natural8.value.elements[0] = IIS328DQ_ID;
386  val.natural8.value.elements[1] = accelerometer_configuration.module_power;
387  param.registerAccessLock->Take();
388  param.clRegister->write("rmap.accelerometer.config", &val);
389  param.registerAccessLock->Give();
390 
391  // Reading RMAP Module Offset Config -> (READ/WRITE)
392  // float_t x; Offset (X)
393  // float_t y; Offset (Y)
394  // float_t z; Offset (Z)
395  // Select type register (float_)
396  uavcan_register_Value_1_0_select_real32_(&val);
397  val.real32.value.count = 3;
398  // Loading Default
399  val.real32.value.elements[0] = accelerometer_configuration.offset_x;
400  val.real32.value.elements[1] = accelerometer_configuration.offset_y;
401  val.real32.value.elements[2] = accelerometer_configuration.offset_z;
402  param.registerAccessLock->Take();
403  param.clRegister->write("rmap.accelerometer.offset", &val);
404  param.registerAccessLock->Give();
405 }
406 
410 void AccelerometerTask::calibrate(bool is_default, bool save_register)
411 {
412  if (is_default)
413  {
414  // Init offset to 0
418  } else {
419  // Set offset to direct realtime Read Value
423  }
424 
425  // Save Register Eeprom
426  if(save_register) {
427  // Param Writing
428  static uavcan_register_Value_1_0 val = {0};
429  // Reading RMAP Module Offset Config -> (READ/WRITE)
430  // float_t x; Offset (X)
431  // float_t y; Offset (Y)
432  // float_t z; Offset (Z)
433  // Select type register (float_)
434  uavcan_register_Value_1_0_select_real32_(&val);
435  val.real32.value.count = 3;
436  // Loading Default
437  val.real32.value.elements[0] = accelerometer_configuration.offset_x;
438  val.real32.value.elements[1] = accelerometer_configuration.offset_y;
439  val.real32.value.elements[2] = accelerometer_configuration.offset_z;
440  param.registerAccessLock->Take();
441  param.clRegister->write("rmap.accelerometer.offset", &val);
442  param.registerAccessLock->Give();
443  }
444 }
445 
449 {
450  bool hw_check = false;
451 
452  TRACE_INFO_F(F("Check hardware module... [ %s ]\r\n"), OK_STRING);
453  /* Try whoamI response from device */
454  uint8_t whoamI = 0;
455  // Read ID Device
457  if (whoamI == IIS328DQ_ID) hw_check = true;
458 
459  return hw_check;
460 }
461 
464 {
465  TRACE_INFO_F(F("Setup hardware module... [ %s ]\r\n"), OK_STRING);
466  /* Enable Block Data Update */
468  /* Set full scale */
470  /* Configure filtering chain */
471  /* Accelerometer - High Pass / Slope path */
473  /* Set Output Data Rate */
475 }
476 
480 {
481  bool status = false;
482  /* Read output only if new value is available */
485  // Is New Data avaiable
486  if (reg.status_reg.zyxda) {
487  status = true;
488  /* Read acceleration data */
489  int16_t data_raw_acceleration[3];
490  memset(data_raw_acceleration, 0x00, 3 * sizeof(int16_t));
491  accelerometer.iis328dq_acceleration_raw_get(data_raw_acceleration);
492  accelerometer.push_raw_data(data_raw_acceleration);
496  }
497  // True if data ready
498  return status;
499 }
500 
503 {
504  TRACE_INFO_F(F("Power down accelerometer... [ %s ]\r\n"), OK_STRING);
505  /* Set Output Data Rate to OFF */
507  /* Disable Block Data Update */
509 }
510 
511 #endif
#define PROPERTY_ENABLE
Definition: accelerometer.h:59
#define ACCELEROMETER_MAX_CHECK_ATTEMPT
Definition: accelerometer.h:50
#define ACCELEROMETER_WAIT_CHECK_HARDWARE
Definition: accelerometer.h:49
#define PROPERTY_DISABLE
Definition: accelerometer.h:58
#define IIS328DQ_ID
Definition: accelerometer.h:52
#define LOCAL_TASK_ID
accelerometer cpp_Freertos header file
#define BUBBLE_TIMINGS_VERIFY
#define ACCELEROMETER_TASK_WAIT_DELAY_MS
#define BUBBLE_ANGLE_MIRROR
#define BUBBLE_ANGLE_ERROR
#define ACCELEROMETER_TASK_SLEEP_DELAY_MS
accelerometer_t accelerometer_configuration
void printConfiguration(void)
Print configuration Accelerometer.
void TaskState(uint8_t state_position, uint8_t state_subposition, task_flag state_operation)
local suspend flag and positor running state Task (optional)
void loadConfiguration(void)
Load configuration accelleration module.
void setupModule(void)
Setup hardware configuration.
AccelerometerState_t state
void calibrate(bool is_default, bool save_register)
Calibrate accelereometer position X-Y-Z to actual value (set offset from 0)
void saveConfiguration(bool is_default)
Init/Save configuration param Accelerometer.
void TaskWatchDog(uint32_t millis_standby)
local watchDog and Sleep flag Task (optional)
virtual void Run()
RUN Task.
AccelerometerParam_t param
bool readModule(void)
Read data from module accelerometer.
Accelerometer accelerometer
AccelerometerTask(const char *taskName, uint16_t stackSize, uint8_t priority, AccelerometerParam_t AccelerometerParam)
Construct the Accelerometer Task::AccelerometerTask object.
void powerDownModule(void)
Activate power saving hardware module.
bool checkModule(void)
Check hardware module.
void read(const char *const register_name, uavcan_register_Value_1_0 *const inout_value)
Legge un registro Cypal/Uavcan wrapper UAVCAN (Imposta Default su Set inout_value su value se non esi...
void write(const char *const register_name, const uavcan_register_Value_1_0 *const value)
Store the given register value into the persistent storage.
#define WDT_CONTROLLER_MS
Task milliseconds minimal check.
Definition: config.h:90
#define ALL_TASK_ID
All task ID. Send message to ALL Task.
Definition: config.h:166
#define WDT_STARTING_TASK_MS
Init WatchDog Task local milliseconds.
Definition: config.h:88
#define ACCELEROMETER_TASK_ID
Accelerometer task ID.
Definition: config.h:176
#define UNUSED_SUB_POSITION
Monitor Sub Position not used flag.
Definition: config.h:94
#define OK_STRING
Definition: debug.h:33
#define TRACE_INFO_F(...)
Definition: debug_F.h:57
#define TRACE_VERBOSE_F(...)
Definition: debug_F.h:71
int32_t iis328dq_device_id_get(uint8_t *buff)
Device Who am I.[get].
int32_t iis328dq_block_data_update_set(uint8_t val)
Block data update.[set].
int32_t iis328dq_full_scale_set(iis328dq_fs_t val)
Accelerometer full-scale selection.[set].
int32_t iis328dq_status_reg_get(iis328dq_status_reg_t *val)
The STATUS_REG register is read by the interface.[get].
int32_t iis328dq_data_rate_set(iis328dq_dr_t val)
Accelerometer data rate selection.[set].
int32_t iis328dq_acceleration_raw_get(int16_t *val)
Linear acceleration output register. The value is expressed as a 16-bit word in two’s complement....
int32_t iis328dq_hp_path_set(iis328dq_hpen_t val)
Select High Pass filter path.[set].
void push_raw_data(int16_t *data_raw)
Push data row for scroll mean sensibility data.
float_t iis328dq_from_fsx_to_inc(int16_t lsb)
Read istant value accelerometer scaled 0-100% to inclinometer value.
@ set
Set WDT (From Application TASK... All OK)
Definition: local_typedef.h:60
@ timer
Set Timered WDT (From Application long function WDT...)
Definition: local_typedef.h:61
task_flag
Task state Flag type.
Definition: local_typedef.h:65
@ suspended
Task is excluded from WDT Controller or Suspended complete.
Definition: local_typedef.h:68
@ sleepy
Task is in sleep mode or longer wait (Inform WDT controller)
Definition: local_typedef.h:67
@ normal
Normal operation Task controller.
Definition: local_typedef.h:66
struct local elaborate data parameter
system_status_t * system_status
system status pointer struct
TwoWire * wire
Local Wire access for sensor accelerometer.
cpp_freertos::Queue * systemMessageQueue
Queue for system message.
cpp_freertos::BinarySemaphore * systemStatusLock
Semaphore to system status access.
cpp_freertos::BinarySemaphore * wireLock
Semaphore to Wire access for sensor accelerometer.
EERegister * clRegister
Object Register C++ access.
cpp_freertos::BinarySemaphore * registerAccessLock
Semaphore to register Cyphal access.
Accelerometer::iis328dq_dr_t module_power
module updating frequency (enabled)
uint8_t config_valid
Inizialization Byte Config.
float offset_y
offset_y to 0
float offset_x
offset_x to 0
float offset_z
offset_z to 0
System message for queue.
uint8_t task_dest
Command struct.
uint8_t do_sleep
Optional param for difference level Sleep.
uint8_t do_calib
Calibrate accelerometr.
struct system_message_t::@7 command
bool bubble_level_error
Bubble software accelerometer error.
struct system_status_t::@6 running
struct system_status_t::@5 events
Module error or alert timings continuos verify.
task_t tasks[TOTAL_INFO_TASK]
Info Task && WDT.
Definition: local_typedef.h:92
bool is_bubble_level_error
Bubble software accelerometer error.
bool is_accelerometer_error
Accelerometer error.
uint32_t epoch_bubble_level_error
Bubble software accelerometer error epoch event start.
int32_t watch_dog_ms
WatchDog of Task Timer.
Definition: local_typedef.h:75
uint8_t running_pos
!=0 (CREATE) Task Started (Generic state of Task)
Definition: local_typedef.h:78
task_flag state
Long sleep Task.
Definition: local_typedef.h:77
wdt_flag watch_dog
WatchDog of Task.
Definition: local_typedef.h:74
uint16_t stack
Stack Max Usage Monitor.
Definition: local_typedef.h:76
uint8_t running_sub
Optional SubState of Task.
Definition: local_typedef.h:79
iis328dq_status_reg_t status_reg