Rleg  2
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
ui.c
Go to the documentation of this file.
1 #include <ncurses.h> //NCURSES
2 #include <math.h>
3 #include <string.h>
4 //#include "gmatrix.h"
6 //#include "calibration/calibration.h"
7 //#include "estimation.h"
8 //#include "control.h"
9 #include "datalogger.h"
10 //#if ANU_COMPILE_FOR_XENOMAI // Compile for Xenomai
11 // #include "threads_xenomai.h"
12 //#else // Compile for Linux
13 // #include "threads_linux.h"
14 //#endif
15 #include "main2.h"
16 #include "ui.h"
17 
18 // From main
19 extern void exit_program(void);
20 
21 // From threads
22 extern unsigned int telemetry_mode;
23 
24 //WINDOW *scr;
25 
26 int ui_init(void)
27 {
28  #if UI_MODULE_ENABLED
29  //NCURSES
30  initscr();
31  scrollok(stdscr,TRUE);
32  //resizeterm(33,100);
33  //wresize(scr,32,100);
34  //erase();
35  timeout(0); //No delay for getch();
36  #endif
37 
38  return SUCCESS;
39 }
40 
41 int ui_close(void)
42 {
43  #if UI_MODULE_ENABLED
44  clear();
45  printw("Cleaning up...\n");
46  refresh();
47  endwin();
48  #endif
49 
50  return SUCCESS;
51 }
52 
54 {
55  unsigned char user_char = 0;
56 
57  static enum {UI_OVERVIEW = 0, UI_IMU, UI_EFF, UI_MRA , UI_ENC} ui_state;
58 
59  erase();
60  //wresize(scr,32,100);
61  attron(A_STANDOUT);
62 
63  mvaddstr(0,15,"RLEG Data");
64  attroff(A_STANDOUT);
65 
66  switch(ui_state)
67  {
68  case UI_IMU:
69  if(ui_imu_data(pimu_data) != SUCCESS) return FAILURE;
70  break;
71  case UI_EFF:
72  if(ui_eff_data(peff_data) != SUCCESS) return FAILURE;
73  break;
74  case UI_MRA:
75  if(ui_mra_data(pmra_data) != SUCCESS) return FAILURE;
76  break;
77  case UI_ENC:
78  if(ui_enc_data(enc_data) != SUCCESS) return FAILURE;
79  break;
80  case UI_OVERVIEW:
81  default:
82  if(ui_overview_data(total, failure, pimu_data, peff_data, pmra_data, enc_data) != SUCCESS) return FAILURE;
83  break;
84  }
85 
86  ui_menu();
87 
88  refresh();
89 
90  user_char = getch(); //Getting a char typed by the user
91  switch(user_char)
92  {
93  case 'Q':
94  case 'q': //Quit
95  exit_program();
96  break;
97  case 'I':
98  case 'i': //IMU view
99  ui_state = UI_IMU;
100  break;
101  case 'F':
102  case 'f': //EFF view
103  ui_state = UI_EFF;
104  break;
105  case 'E':
106  case 'e': // ENCODER view
107  ui_state = UI_ENC;
108  break;
109  case 'O':
110  case 'o': //Overview
111  ui_state = UI_OVERVIEW;
112  break;
113  case 'M':
114  case 'm': //MRA
115  ui_state = UI_MRA;
116  break;
117  case 'D':
118  case 'd': //Datalogger start/stop
120  {
121  datalogger_stop();
122  }
123  else
124  {
126  }
127  break;
128  default:
129  break;
130  }
131 
132  return SUCCESS;
133 }
134 
136 {
137  mvprintw(2, 0, "Accelerometer X (raw): %d", pimu_data->acc.x);
138  mvprintw(2, 40, "Accelerometer X (g): %lf", pimu_data->calib.acc.x);
139  mvprintw(3, 0, "Accelerometer Y (raw): %d", pimu_data->acc.y);
140  mvprintw(3, 40, "Accelerometer Y (g): %lf", pimu_data->calib.acc.y);
141  mvprintw(4, 0, "Accelerometer Z (raw): %d", pimu_data->acc.z);
142  mvprintw(4, 40, "Accelerometer Z (g): %lf", pimu_data->calib.acc.z);
143  mvprintw(5, 0, "Gyrometer X (raw): %d", pimu_data->gyr.x);
144  mvprintw(5, 40, "Gyrometer X (rad/sec): %lf", pimu_data->calib.gyr.x);
145  mvprintw(6, 0, "Gyrometer Y (raw): %d", pimu_data->gyr.y);
146  mvprintw(6, 40, "Gyrometer Y (rad/sec): %lf", pimu_data->calib.gyr.y);
147  mvprintw(7, 0, "Gyrometer Z (raw): %d", pimu_data->gyr.z);
148  mvprintw(7, 40, "Gyrometer Z (rad/sec): %lf", pimu_data->calib.gyr.z);
149  mvprintw(8, 0, "Magnetometer X (raw): %d", pimu_data->mag.x);
150  mvprintw(8, 40, "Magnetometer X (B): %lf", pimu_data->calib.mag.x);
151  mvprintw(9, 0, "Magnetometer Y (raw): %d", pimu_data->mag.y);
152  mvprintw(9, 40, "Magnetometer Y (B): %lf", pimu_data->calib.mag.y);
153  mvprintw(10,0, "Magnetometer Z (raw): %d", pimu_data->mag.z);
154  mvprintw(10,40, "Magnetometer Z (B): %lf", pimu_data->calib.mag.z);
155  mvprintw(12,0,"Total Acceleromter: %lf",sqrt(pimu_data->calib.acc.x*pimu_data->calib.acc.x+pimu_data->calib.acc.y*pimu_data->calib.acc.y+pimu_data->calib.acc.z*pimu_data->calib.acc.z));
156  mvprintw(13,0,"Total Gyrometer: %lf",sqrt(pimu_data->calib.gyr.x*pimu_data->calib.gyr.x+pimu_data->calib.gyr.y*pimu_data->calib.gyr.y+pimu_data->calib.gyr.z*pimu_data->calib.gyr.z));
157  mvprintw(14,0,"Total Magnetometer: %lf",sqrt(pimu_data->calib.mag.x*pimu_data->calib.mag.x+pimu_data->calib.mag.y*pimu_data->calib.mag.y+pimu_data->calib.mag.z*pimu_data->calib.mag.z));
158  //mvprintw(15, 0, "Encoder (raw): %lf", enc.data);
159 
160  return SUCCESS;
161 }
162 
164 {
165  mvprintw(2,0,"Fx (bits): %d",peff_data->F.x);
166  //mvprintw(2,40,"Fx (N): %lf",peff_data->??);
167  mvprintw(3,0,"Fy (bits): %d",peff_data->F.y);
168  //mvprintw(3,40,"Fx (N): %lf",peff_data->??);
169  mvprintw(4,0,"Fz (bits): %d",peff_data->F.z);
170  //mvprintw(4,40,"Fx (N): %lf",peff_data->??);
171  mvprintw(5,0,"Mx (bits): %d",peff_data->M.x);
172  //mvprintw(5,40,"Mx (Nm): %lf",peff_data->??);
173  mvprintw(6,0,"My (bits): %d",peff_data->M.y);
174  //mvprintw(6,40,"My (Nm): %lf",peff_data->??);
175  mvprintw(7,0,"Mz (bits): %d",peff_data->M.z);
176  //mvprintw(7,40,"Mz (Nm): %lf",peff_data->??);
177 
178  return SUCCESS;
179 }
180 
182 {
183  mvprintw(2,0,"Position (raw): %lf",enc_data->position);
184  mvprintw(3,0,"Position (degree): %lf",enc_data->calib.position);
185  return SUCCESS;
186 }
187 
189 {
190  mvprintw(2,0,"V_ctl (bits): %d",pmra_data->v_ctl);
191  mvprintw(3,0,"V_ctl_read (bits): %d",pmra_data->v_ctl_read);
192  //mvprintw(2,40,"Fx (N): %lf",peff_data->??);
193 
194  return SUCCESS;
195 }
196 
197 int ui_overview_data(int total, int failures, IMU_DATA_STRUCT *pimu_data, EFF_DATA_STRUCT *peff_data, MRA_DATA_STRUCT *pmra_data, ENC_DATA_STRUCT *enc_data)
198 {
199  float error_rate = 0.0;
200  double t = 0.0;
201  double Ts = 0.0;
202  double mean_exec_time = 0.0;
203  double t0 = 0.0;
204 
205  get_time(&t, &Ts, &mean_exec_time, &t0);
206 
207  error_rate = (float)(((float)failures/(float)total)*100.0);
208  mvprintw(2, 0, "Communication Statistics: Total = %d Failures = %d Error Rate = %3.3f",total, failures, error_rate);
209 
210  mvprintw(4, 0, "IMU Accelerometer (bits):\tX:%4d\tY:%4d\tZ:%4d", pimu_data->acc.x, pimu_data->acc.y, pimu_data->acc.z);
211  mvprintw(5, 0, "IMU Gyrometer (bits):\t\tX:%4d\tY:%4d\tZ:%4d", pimu_data->gyr.x, pimu_data->gyr.y, pimu_data->gyr.z);
212  mvprintw(6, 0, "IMU Magnetometer (bits):\tX:%4d\tY:%4d\tZ:%4d", pimu_data->mag.x, pimu_data->mag.y, pimu_data->mag.z);
213 
214  mvprintw(8, 0, "IMU Accelerometer (g):\t\tX:%8.5lf\tY:%8.5lf\tZ:%8.5lf", pimu_data->calib.acc.x, pimu_data->calib.acc.y, pimu_data->calib.acc.z);
215  mvprintw(9, 0, "IMU Gyrometer (rad/s):\t\tX:%8.5lf\tY:%8.5lf\tZ:%8.5lf", pimu_data->calib.gyr.x, pimu_data->calib.gyr.y, pimu_data->calib.gyr.z);
216  mvprintw(10, 0, "IMU Magnetometer (B):\t\tX:%8.5lf\tY:%8.5lf\tZ:%8.5lf", pimu_data->calib.mag.x, pimu_data->calib.mag.y, pimu_data->calib.mag.z);
217 
218  mvprintw(12, 0, "Temp (bits): %d", pimu_data->temp);
219  mvprintw(12,40, "Temp (ÂșC): %lf", pimu_data->calib_temp);
220 
221  mvprintw(13,0, "Encoder (bits):\t%d",enc_data->position);
222  mvprintw(13,40, "Encoder (degree):\t%d",enc_data->calib.position);
223  //mvprintw(15,0,"Fx (bits): %d",peff_data->F.x);
224 
225  //mvprintw(2,40,"Fx (N): %lf",peff_data->??);
226  //mvprintw(15,0,"Fy (bits): %d",peff_data->F.y);
227  //mvprintw(3,40,"Fx (N): %lf",peff_data->??);
228  //mvprintw(16,0,"Fz (bits): %d",peff_data->F.z);
229  //mvprintw(4,40,"Fx (N): %lf",peff_data->??);
230  //mvprintw(17,0,"Mx (bits): %d",peff_data->M.x);
231  //mvprintw(5,40,"Mx (Nm): %lf",peff_data->??);
232  //mvprintw(18,0,"My (bits): %d",peff_data->M.y);
233  //mvprintw(6,40,"My (Nm): %lf",peff_data->??);
234  //mvprintw(19,0,"Mz (bits): %d",peff_data->M.z);
235  //mvprintw(7,40,"Mz (Nm): %lf",peff_data->??);
236 
237  mvprintw(15, 0, "Voltage Control Written (bits):\t%4d", pmra_data->v_ctl);
238  mvprintw(16, 0, "Voltage Control Read (bits):\t%4d", pmra_data->v_ctl_read);
239 
240  mvprintw(18, 0, "Runtime: %4.2lf", t);
241 
243  {
244  mvprintw(18, 40, "Datalogger stopped");
245  }
246  else
247  {
248  mvprintw(18, 40, "Datalogger runtime: %4.2lf", (t-t0));
249  }
250 /*
251  if(calibration_get_status() == CALIBRATION_NOT_RUNNING)
252  {
253  mvprintw(15, 0, "Calibration stopped");
254  }
255  else
256  {
257  mvprintw(15, 0, "Calibration running");
258  }
259 */
260 
261  return SUCCESS;
262 }
263 
264 int ui_menu(){
265  mvprintw(20, 0, "I: IMU");
266  mvprintw(20, 20, "F: Efforts");
267  mvprintw(20, 40, "M: Magneto-rheological Actuator");
268  mvprintw(21, 0, "E: Encoder");
269  mvprintw(21, 20, "O: Overview");
270  mvprintw(21, 40, "D: Datalogger Start/Stop");
271  mvprintw(22, 0, "Q: Quit\n");
272 
273  return SUCCESS;
274 }
int t0
Definition: main.c:40
int ui_eff_data(EFF_DATA_STRUCT *peff_data)
Definition: ui.c:163
int ui_close(void)
Close UI.
Definition: ui.c:41
int ui_mra_data(MRA_DATA_STRUCT *pmra_data)
Print MRA data.
Definition: ui.c:188
int datalogger_start(void)
Definition: datalogger.c:584
#define DATALOGGER_RUNNING
Definition: datalogger.h:30
ENC_DATA_STRUCT enc_data
Definition: main.c:31
Struct to control MRA.
Data of IMU structure.
Definition: communication.h:93
short int y
Definition: communication.h:77
#define FAILURE
Definition: calibration.h:7
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
unsigned int telemetry_mode
Definition: main2.c:65
DATA_XYZ mag
Magnetormeter Vector.
Definition: communication.h:96
int ui_enc_data(ENC_DATA_STRUCT *enc_data)
Print ENCODER data.
Definition: ui.c:181
#define TRUE
int failure
Definition: main.c:36
void exit_program(void)
Internal function to end program.
Definition: main.c:180
short int v_ctl_read
Voltage level read from the actuator.
DATA_XYZ acc
Accel Vector.
Definition: communication.h:94
#define SUCCESS
Definition: calibration.h:6
struct ENC_DATA_STRUCT::calibrate calib
int ui_overview_data(int total, int failures, IMU_DATA_STRUCT *pimu_data, EFF_DATA_STRUCT *peff_data, MRA_DATA_STRUCT *pmra_data, ENC_DATA_STRUCT *enc_data)
Print ALL sensors data.
Definition: ui.c:197
#define DATALOGGER_NOT_RUNNING
Definition: datalogger.h:29
short int z
Definition: communication.h:78
unsigned short int position
int ui_menu()
Print the menu.
Definition: ui.c:264
unsigned short int position
struct IMU_DATA_STRUCT::calibrated calib
int ui_imu_data(IMU_DATA_STRUCT *pimu_data)
Print IMU data.
Definition: ui.c:135
int ui_init(void)
Initialize UI.
Definition: ui.c:26
DATA_XYZ gyr
Gyrometer Vector.
Definition: communication.h:95
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
short int x
Definition: communication.h:76
short int v_ctl
Voltage level for control output.
int datalogger_status(void)
Definition: datalogger.c:571
int datalogger_stop(void)
Definition: datalogger.c:601
int total
Definition: main.c:35