10 #include "magnetometer.h"
16 #include "estimation.h"
20 #include "mavlink_module.h"
21 #include "threads_linux.h"
32 #define TASK1_PERIOD_US 20000 //20ms
33 #define TASK2_PERIOD_US 100000 //100ms
53 volatile double t0 = 0.0;
98 int return_value = THREADS_LINUX_SUCCESS;
102 if(status != SENSORS_SUCCESS)
104 return_value = THREADS_LINUX_FAILURE;
107 if(return_value != THREADS_LINUX_FAILURE)
110 if(status != CALIBRATION_SUCCESS)
112 return_value = THREADS_LINUX_FAILURE;
116 if(return_value != THREADS_LINUX_FAILURE)
121 return_value = THREADS_LINUX_FAILURE;
125 if(return_value != THREADS_LINUX_FAILURE)
130 return_value = THREADS_LINUX_FAILURE;
134 if(return_value != THREADS_LINUX_FAILURE)
139 return_value = THREADS_LINUX_FAILURE;
143 if(return_value != THREADS_LINUX_FAILURE)
148 return_value = THREADS_LINUX_FAILURE;
152 if(return_value != THREADS_LINUX_FAILURE)
154 status = estimation_init(ESTIMATION_DO_NOT_ESTIMATE_ACCEL_BIAS, &
estimation_data);
155 if(status != ESTIMATION_SUCCESS)
157 return_value = THREADS_LINUX_FAILURE;
161 if(return_value != THREADS_LINUX_FAILURE)
164 if(status != CONTROL_SUCCESS)
166 return_value = THREADS_LINUX_FAILURE;
170 if(return_value != THREADS_LINUX_FAILURE)
172 status = protocol_init();
173 if(status != PROTOCOL_SUCCESS)
175 return_value = THREADS_LINUX_FAILURE;
179 if(return_value != THREADS_LINUX_FAILURE)
182 if(status != UI_SUCCESS)
184 return_value = THREADS_LINUX_FAILURE;
188 if(return_value != THREADS_LINUX_FAILURE)
193 return_value = THREADS_LINUX_FAILURE;
198 if(return_value != THREADS_LINUX_FAILURE)
207 #if ANU_COMPILE_FOR_OVERO
228 return_value = THREADS_LINUX_FAILURE;
232 if(status != UI_SUCCESS)
234 return_value = THREADS_LINUX_FAILURE;
237 status = protocol_close();
238 if(status != PROTOCOL_SUCCESS)
240 return_value = THREADS_LINUX_FAILURE;
243 status = control_close();
244 if(status != CONTROL_SUCCESS)
246 return_value = THREADS_LINUX_FAILURE;
250 if(status != ESTIMATION_SUCCESS)
252 return_value = THREADS_LINUX_FAILURE;
258 return_value = THREADS_LINUX_FAILURE;
264 return_value = THREADS_LINUX_FAILURE;
269 return_value = THREADS_LINUX_FAILURE;
273 if(status != CALIBRATION_SUCCESS)
275 return_value = THREADS_LINUX_FAILURE;
278 status = sensors_close();
279 if(status != SENSORS_SUCCESS)
281 return_value = THREADS_LINUX_FAILURE;
289 static int previous_calibration_status = CALIBRATION_NOT_RUNNING;
290 int current_calibration_status;
292 int current_datalogger_status;
304 calibration_calibrate_all(&
pwm_read_data, &
scp1000_data, &
battery_data, &
gps_data, &imu_data, &
pitot_data, &
sonar_data, &
calibration_local_coordinate_system_data, &
calibration_altimeter_data, &
calibration_local_fields_data, &
gps_measure, &
imu_measure, &
magnetometer_measure);
325 datalogger_update(
t_task_1_global,
T_task_1_exec_global,
T_task_2_exec_global,
t0, &
battery_data, &
gps_data, &imu_data, &
pitot_data, &
pwm_read_data, &
pwm_write_data, &
scp1000_data, &
sonar_data, &
gps_measure, &
imu_measure, &
magnetometer_measure, &
estimation_data, &
control_data);
327 previous_datalogger_status = current_datalogger_status;
329 current_calibration_status = calibration_get_status();
330 if(current_calibration_status == CALIBRATION_RUNNING)
332 calibration_imu_add_sample_bias_estimate(&imu_data);
337 if(previous_calibration_status == CALIBRATION_RUNNING)
339 calibration_imu_finish_bias_estimate(&imu_data);
343 previous_calibration_status = current_calibration_status;
345 return THREADS_LINUX_SUCCESS;
353 ui_update(&
battery_data, &
gps_data, &imu_data, &
pitot_data, &
pwm_read_data, &
pwm_write_data, &
scp1000_data, &
sonar_data, &
gps_measure, &
imu_measure, &
magnetometer_measure, &
calibration_local_coordinate_system_data, &
calibration_local_fields_data, &
calibration_altimeter_data, &
estimation_data, &
control_data,
total,
failure);
357 mavlink_module_update(
t_task_1_global,
t0, &
battery_data, &
gps_data, &imu_data, &
pitot_data, &
pwm_read_data, &
pwm_write_data, &
scp1000_data, &
sonar_data, &
gps_measure, &
imu_measure, &
magnetometer_measure, &
estimation_data, &
control_data);
360 return THREADS_LINUX_SUCCESS;
365 sigval.sival_int = 0;
371 sigval.sival_int = 0;
377 struct itimerspec itimer;
378 struct sigevent sigev;
383 itimer.it_interval.tv_sec=0;
385 itimer.it_value=itimer.it_interval;
387 memset(&sigev, 0,
sizeof (
struct sigevent));
388 sigev.sigev_value.sival_int = 0;
389 sigev.sigev_notify = SIGEV_THREAD;
390 sigev.sigev_notify_attributes = NULL;
393 if (timer_create(CLOCK_REALTIME, &sigev, &
timer_task_1) < 0)
395 fprintf(stderr,
"[%d]: %s\n", __LINE__, strerror(errno));
401 fprintf(stderr,
"[%d]: %s\n", __LINE__, strerror(errno));
408 struct itimerspec itimer;
409 struct sigevent sigev;
414 itimer.it_interval.tv_sec=0;
416 itimer.it_value=itimer.it_interval;
418 memset (&sigev, 0,
sizeof (
struct sigevent));
419 sigev.sigev_value.sival_int = 0;
420 sigev.sigev_notify = SIGEV_THREAD;
421 sigev.sigev_notify_attributes = NULL;
424 if(timer_create(CLOCK_REALTIME, &sigev, &
timer_task_2) < 0)
426 fprintf(stderr,
"[%d]: %s\n", __LINE__, strerror(errno));
432 fprintf(stderr,
"[%d]: %s\n", __LINE__, strerror(errno));
442 fprintf(stderr,
"[%d]: %s\n", __LINE__, strerror(errno));
452 fprintf(stderr,
"[%d]: %s\n", __LINE__, strerror(errno));
460 static struct timeval timereset;
461 static struct timeval time;
462 static struct timeval time_exec_start;
463 static struct timeval time_exec_end;
464 static double T_task,t_task,t_task_previous;
465 static double T_task_mean,T_task_min,T_task_max,T_task_exec;
467 gettimeofday(&time_exec_start, NULL);
472 gettimeofday(&timereset, NULL);
480 gettimeofday(&time, NULL);
481 t_task = ((time.tv_sec - timereset.tv_sec) + (time.tv_usec - timereset.tv_usec)*1e-6);
482 T_task = t_task - t_task_previous;
483 t_task_previous = t_task;
487 T_task_mean = T_task;
494 T_task_mean = 0.05*T_task + 0.95*T_task_mean;
495 if(T_task < T_task_min) T_task_min = T_task;
496 if(T_task > T_task_max) T_task_max = T_task;
502 gettimeofday(&time_exec_end, NULL);
503 T_task_exec = ((time_exec_end.tv_sec - time_exec_start.tv_sec) + (time_exec_end.tv_usec - time_exec_start.tv_usec)*1e-6);
517 static struct timeval timereset;
518 static struct timeval time;
519 static struct timeval time_exec_start;
520 static struct timeval time_exec_end;
521 static double T_task,t_task,t_task_previous;
522 static double T_task_mean,T_task_min,T_task_max,T_task_exec;
524 gettimeofday(&time_exec_start, NULL);
529 gettimeofday(&timereset, NULL);
537 gettimeofday(&time, NULL);
538 t_task = ((time.tv_sec - timereset.tv_sec) + (time.tv_usec - timereset.tv_usec)*1e-6);
539 T_task = t_task - t_task_previous;
540 t_task_previous = t_task;
544 T_task_mean = T_task;
550 T_task_mean = 0.05*T_task + 0.95*T_task_mean;
551 if(T_task < T_task_min) T_task_min = T_task;
552 if(T_task > T_task_max) T_task_max = T_task;
558 gettimeofday(&time_exec_end, NULL);
559 T_task_exec = ((time_exec_end.tv_sec - time_exec_start.tv_sec) + (time_exec_end.tv_usec - time_exec_start.tv_usec)*1e-6);
573 return THREADS_LINUX_SUCCESS;
576 int threads_get_time(
double *time_control_task_s,
double *Ts_control_task_s,
double *mean_time_control_task_s,
double *t0_control_task_s)
581 *t0_control_task_s =
t0;
582 return THREADS_LINUX_SUCCESS;
volatile double T_task_2_max_global
ESTIMATION_DATA_STRUCT estimation_data
volatile double T_task_1_min_global
volatile double T_task_1_max_global
void threads_linux_timer_function_task_1(void)
volatile double t_task_1_global
volatile int task_1_period_us
volatile int flag_task_1_firstexecution
int ui_close(void)
Close UI.
int datalogger_set_Ts(double Ts)
volatile double T_task_1_mean_global
BATTERY_DATA_STRUCT battery_data
void threads_linux_timer_task_2(union sigval sigval)
#define DATALOGGER_RUNNING
unsigned int telemetry_mode
volatile double T_task_2_exec_global
int threads_linux_periodic_task_2(void)
int datalogger_update_IPC(void)
CALIBRATION_LOCAL_COORDINATE_SYSTEM_STRUCT calibration_local_coordinate_system_data
CALIBRATION_LOCAL_FIELDS_STRUCT calibration_local_fields_data
int threads_reset_timer(void)
int datalogger_close(void)
MAGNETOMETERMEASURE magnetometer_measure
int datalogger_write_file(void)
volatile double T_task_1_exec_global
int datalogger_init(void)
SONARMEASURE sonar_measure
int datalogger_update(double t_s, double t_control_exec_s, double t_ui_exec_s, double t0_s, IMU_DATA_STRUCT *pimu_data, EFF_DATA_STRUCT *peff_data, MRA_DATA_STRUCT *pmra_data)
volatile int flag_task_2_firstexecution
void threads_linux_timer_start_task_1(void)
void threads_linux_timer_stop_task_2(void)
int threads_linux_init(void)
volatile double T_task_2_min_global
volatile double T_task_2_mean_global
#define DATALOGGER_NOT_RUNNING
#define DATALOGGER_SUCCESS
int threads_get_time(double *time_control_task_s, double *Ts_control_task_s, double *mean_time_control_task_s, double *t0_control_task_s)
CALIBRATION_ALTIMETER_STRUCT calibration_altimeter_data
int threads_linux_periodic_task_1(void)
PITOT_DATA_STRUCT pitot_data
volatile int task_2_period_us
int ui_init(void)
Initialize UI.
int ui_update(IMU_DATA_STRUCT *pimu_data, EFF_DATA_STRUCT *peff_data, MRA_DATA_STRUCT *pmra_data, ENC_DATA_STRUCT *enc_data, int total, int failure)
Update Screen with new data of sensors.
void threads_linux_timer_task_1(union sigval sigval)
volatile double t_task_2_global
void threads_linux_timer_start_task_2(void)
void threads_linux_timer_stop_task_1(void)
SONAR_DATA_STRUCT sonar_data
PWM_READ_DATA_STRUCT pwm_read_data
SCP1000_DATA_STRUCT scp1000_data
int datalogger_status(void)
PWM_WRITE_DATA_STRUCT pwm_write_data
CONTROL_DATA_STRUCT control_data
void threads_linux_timer_function_task_2(void)