Rleg  2
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
main.c
Go to the documentation of this file.
1 
7 #include <stdio.h>
8 #include <math.h>
10 #include "main.h"
11 #include "taskScheduler.h"
12 #include "ui.h"
13 //#include "control/control.h"
14 #include "datalogger.h"
16 
17 /*Strutures for tasks:*/
19 
20 /* Task period*/
21 #define TASK_UI_PERIOD 200000 //200ms
22 #define TASK_CONTROL_PERIOD 20000 //20ms
23 
24 /*Strutures for configuration*/
27 
28 /*Strutures for communicate*/
33 
34 /* UI statistics */
35 int total = 0;
36 int failure = 0;
37 int acquire = 0;
38 
39 int quittask = 0;
40 int t0 = 0;
41 
42 int main(void){
43  int return_value = SUCCESS;
44 
45 /*Setting config values:*/
46  // Parameters for IMU
47  imu_param.acc.full_res=1;
48  imu_param.acc.rate=100;
49  imu_param.acc.range=16;
50  imu_param.gyr.rate=100;
51  imu_param.gyr.lpf_bw=188;
52  imu_param.gyr.clk_source='Z';
53  imu_param.gyr.act="XYZ";
54  imu_param.mag.rate=75;
55  imu_param.mag.range=0;
56  imu_param.mag.samples_avg=8;
57  imu_param.mag.meas_mode=0;
58  imu_param.mag.op_mode=0;
59 
60  // Parameters for SPI
61  spi_param.mode=0;
62  spi_param.speed=375000;
63  spi_param.cs=0;
64 
65  // Periodic task :
66  timer_new_task(&task_ui,ui_task);
67  timer_new_task(&task_control,control_task);
68 
69 /*Initialization:*/
70  if(devices_init(&imu_param,&spi_param,&mra_data)!=SUCCESS){
71  perror("Unsuccesful devices initialization");
72  return FAILURE;
73  }
74 
75  if(ui_init()!=SUCCESS){
76  perror("Unsuccesful user interface initialization");
77  return FAILURE;
78  }
79 
80  //timer_start_task(&task1,TASK1_PERIOD);
83 
84 /* Main loop: */
85  while(quittask == 0){
86  //ui_task(0);
87  }
88 
89 /*Shutting Down:*/
90  timer_stop_task(&task_ui);
91  timer_stop_task(&task_control);
92  usleep(2000);
93 
94  if(ui_close()==FAILURE){
95  return_value = FAILURE;
96  }
97 
98  devices_close(&imu_param, &spi_param, &mra_data);
99 
100  return return_value;
101 }
102 
103 static void main_task(int signo){
104  ui_task();
105  control_task();
106 }
107 
108 static void ui_hook(int signo){
109  timer_function_task(&task_ui);
110  //ui_task();
111 }
112 
113 static void control_hook(int signo){
114  timer_function_task(&task_control);
115  //control_task();
116 }
117 
118 static void ui_task(){
119  total++;
120 
121  if(read_all_data(imu_param.i2c_dev, spi_param.spi_dev, &imu_data,&eff_data, &mra_data, &enc_data) != SUCCESS) failure++;
122 
123  ui_update(&imu_data, &eff_data, &mra_data,&enc_data, total, failure);
124 
125 }
126 
127 static void control_task(){
128  static int previous_datalogger_status = DATALOGGER_NOT_RUNNING;
129  int current_datalogger_status; // Used to detect rising edge
130  char user =0;
131 
132 
133  total++;
134 
135 /*Input*/
136  if(read_all_data(imu_param.i2c_dev, spi_param.spi_dev, &imu_data,&eff_data, &mra_data, &enc_data) != SUCCESS) failure++;
137 
138  calibrate_all(&imu_data);
139 
140  current_datalogger_status = datalogger_status();
141  if( (current_datalogger_status == DATALOGGER_RUNNING))
142  {
143  if(previous_datalogger_status == DATALOGGER_NOT_RUNNING) // Rising edge
144  {
145  datalogger_set_Ts(task_control.period_us/1e6);
146  reset_timer();
147  }
148  datalogger_update(task_control.t_global, task_control.T_exec_global, task_ui.T_exec_global, t0, &imu_data, &eff_data, &mra_data /*&imu_measure, &magnetometer_measure, &estimation_data, &control_data*/);
149  }
150  previous_datalogger_status = current_datalogger_status;
151 /* Control */
152  //mra_data.v_ctl= 1275 - (uint8_t)(800*cosf(task_control.t_global*1000));
153  mra_data.v_ctl = ((int)(total*4))%409;
154  //control_test(task1.t_global,&imu_data,&eff_data,&mra_data);
155 /* Actuate */
156  actuate(spi_param.spi_dev,&mra_data);
157 }
158 
159 
160 int reset_timer(void)
161 {
162  t0 = task_control.t_global;
163  return SUCCESS;
164 }
165 
167 int get_time(double *time_control_task_s, double *Ts_control_task_s, double *mean_time_control_task_s, double *t0_control_task_s)
168 {
169  *time_control_task_s = task1.t_global;
170  *Ts_control_task_s = (task1.period_us)/1e6;
171  *mean_time_control_task_s = task1.T_mean_global;
172  *t0_control_task_s = t0;
173 
174  return SUCCESS;
175 }
176 
180 void exit_program(void){
181  quittask = 1;
182  return;
183 }
MRA_DATA_STRUCT mra_data
Definition: main.c:32
volatile double T_mean_global
Definition: taskScheduler.h:24
static void main_task(int signo)
Definition: main.c:103
void timer_function_task(TASK_S *task)
Definition: taskScheduler.c:76
int quittask
Definition: main.c:39
Configs for SPI.
Definition: communication.h:65
volatile double T_exec_global
Definition: taskScheduler.h:23
struct IMU_PARAM_STRUCT::param_mag mag
int t0
Definition: main.c:40
void actuate(int spi_dev, MRA_DATA_STRUCT *mra_data)
Applies the control signal to the actuator.
static void control_task()
Definition: main.c:127
int ui_close(void)
Close UI.
Definition: ui.c:41
int datalogger_set_Ts(double Ts)
Definition: datalogger.c:540
#define DATALOGGER_RUNNING
Definition: datalogger.h:30
ENC_DATA_STRUCT enc_data
Definition: main.c:31
#define TASK_CONTROL_PERIOD
Definition: main.c:22
Struct to control MRA.
Data of IMU structure.
Definition: communication.h:93
SPI_PARAM_STRUCT spi_param
Definition: main.c:26
void calibrate_all(IMU_DATA_STRUCT *imu_data)
Calibrate all sensors.
Definition: calibration.c:8
#define FAILURE
Definition: calibration.h:7
IMU_PARAM_STRUCT imu_param
Definition: main.c:25
int get_time(double *time_control_task_s, double *Ts_control_task_s, double *mean_time_control_task_s, double *t0_control_task_s)
Review of this function:
Definition: main.c:167
TASK_S task_control
Definition: main.c:18
IMU_DATA_STRUCT imu_data
Definition: main.c:30
void timer_stop_task(TASK_S *task)
Stop some task.
struct IMU_PARAM_STRUCT::param_gyr gyr
struct IMU_PARAM_STRUCT::param_acc acc
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)
Definition: datalogger.c:272
void exit_program(void)
Internal function to end program.
Definition: main.c:180
#define TASK_UI_PERIOD
Definition: main.c:21
static void ui_task()
Definition: main.c:118
int failure
Definition: main.c:36
void timer_start_task(TASK_S *task, void(*alertFunction)(int), int period_us)
Start some task.
Definition: taskScheduler.c:27
#define SUCCESS
Definition: calibration.h:6
#define DATALOGGER_NOT_RUNNING
Definition: datalogger.h:29
TASK_S task_ui
Definition: main.c:18
int acquire
Definition: main.c:37
int devices_init(IMU_PARAM_STRUCT *imu_param, SPI_PARAM_STRUCT *spi_param, MRA_DATA_STRUCT *mra_data)
INITIALIZATION OF SENSORS AND DEVICES.
Definition: communication.c:12
static void ui_hook(int signo)
Definition: main.c:108
void timer_new_task(TASK_S *task, void(*runFunction)(void))
Create a task.
Definition: taskScheduler.c:16
Task to schedule definition.
Definition: taskScheduler.h:20
int read_all_data(int i2c_dev, int spi_dev, IMU_DATA_STRUCT *imu_data, EFF_DATA_STRUCT *eff_data, MRA_DATA_STRUCT *mra_data, ENC_DATA_STRUCT *enc_data)
READ ALL DATA FROM SENSORS AND ADC.
Definition: communication.c:94
TASK_S task1
Main rotine for control:
Definition: main.c:18
Configs of IMU.
Definition: communication.h:31
int ui_init(void)
Initialize UI.
Definition: ui.c:26
int devices_close(IMU_PARAM_STRUCT *imu_param, SPI_PARAM_STRUCT *spi_param, MRA_DATA_STRUCT *mra_data)
Fuction to close all communication with the sensors and devices.
Definition: communication.c:82
int reset_timer(void)
Definition: main.c:160
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.
Definition: ui.c:53
EFF_DATA_STRUCT eff_data
Definition: main.c:29
volatile int period_us
Definition: taskScheduler.h:27
static void control_hook(int signo)
Definition: main.c:113
int main(void)
Definition: main.c:42
short int v_ctl
Voltage level for control output.
int datalogger_status(void)
Definition: datalogger.c:571
volatile double t_global
Definition: taskScheduler.h:22
int total
Definition: main.c:35