forked from CRAWLER-T800/Robot_Crawling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_interface.c
457 lines (381 loc) · 10.8 KB
/
command_interface.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include <allegro.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include "ptask.h"
#include "qlearn.h"
#include "matrices.h"
// Filename for saving/loading Q-Matrix
#define Q_MATRIX_FILE "./q_matrix.txt"
// System State Constants
#define RESET 0
#define PLAY 1
#define PAUSE 2
#define STOP 3
// Task identifier Constants
#define INTERPRETER 1
#define GRAPHIC 2
#define CRAWLER 3
#define MODEL 4
// Q-learning Parameters Change Constants
#define NPARAM 5 // Total Number of Possible Learning Parameters
#define STEP 0.01 // Increase/Decrease Step of Learning Parameters
#define ALPHA 0
#define GAMMA 1
#define DECAY 2
#define EPS_MAX 3
#define EPS_MIN 4
// Dynamics task period and integration step constants
// Remember for setting crawler period: PER_C = T/DT * PER_D
#define PER_STD 1 //[ms]
#define PER_FAST 0.6 //[ms]
#define DT_STD 0.001 //[s]
#define DT_FAST 0.001 //[s]
#define T 0.1 //[s] time to go from a state to the next
// Functions from other modules
extern int next_desired_state(int a); //from crawler.c
extern void init_state(); //from model.c
extern void set_dyn_dt(float dt); //from model.c
extern float get_dyn_dt(); //from model.c
// Mutexes
static pthread_mutex_t mux_sys_state = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mux_parameter_values = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mux_parameter_selected = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mux_pause_graphic = PTHREAD_MUTEX_INITIALIZER;
// Static Variables
static int sys_state;
static int pause_graphic;
static int parameter_selected; // Selected QLearning Parameter
static float values[5]; // Values of QLearning Parameters
// value[0] -> alpha
// value[1] -> gamma
// value[2] -> decay
// value[3] -> initial epsilon
// value[4] -> final epsilon
//-----------------------------------------------------
// The following Functions manage the Qlearning
// Parameters Selection from user
//-----------------------------------------------------
void inc_parameter_selected()
{
pthread_mutex_lock(&mux_parameter_selected);
parameter_selected = (parameter_selected + 1) % NPARAM;
pthread_mutex_unlock(&mux_parameter_selected);
}
void dec_parameter_selected()
{
pthread_mutex_lock(&mux_parameter_selected);
parameter_selected = (parameter_selected + 4) % NPARAM;
pthread_mutex_unlock(&mux_parameter_selected);
}
int get_parameter_selected()
{
int ret;
pthread_mutex_lock(&mux_parameter_selected);
ret = parameter_selected;
pthread_mutex_unlock(&mux_parameter_selected);
return ret;
}
//-----------------------------------------------------
// The following Functions manage the change of
// Qlearning Parameters from user
//-----------------------------------------------------
void inc_parameter_value(int p)
{
pthread_mutex_lock(&mux_parameter_values);
//printf("GRAPHIC: il parametro %d valeva %f ", p, values[p]);
values[p] += STEP;
if(values[p] > 1)
values[p] = 1;
if(p == EPS_MIN){ //epsilon min
if(values[p] > values[EPS_MAX])
values[p] = values[EPS_MAX];
}
//printf("e ora vale %f\n", values[p]);
pthread_mutex_unlock(&mux_parameter_values);
}
void dec_parameter_value(int p)
{
pthread_mutex_lock(&mux_parameter_values);
//printf("il parametro %d valeva %f ", p, values[p]);
values[p] -= STEP;
if(values[p] < 0)
values[p] = 0;
if(p == EPS_MAX){//epsilon max
if(values[p] < values[EPS_MIN])
values[p] = values[EPS_MIN];
}
//printf("e ora vale %f\n", values[p]);
pthread_mutex_unlock(&mux_parameter_values);
}
void get_parameter_values(float *buff)
{
pthread_mutex_lock(&mux_parameter_values);
vector_copy(values, buff, 5);
pthread_mutex_unlock(&mux_parameter_values);
}
void init_parameter_values()
{
values[ALPHA] = ql_get_learning_rate();
values[GAMMA] = ql_get_discount_factor();
values[DECAY] = ql_get_expl_decay();
values[EPS_MAX] = ql_get_epsini();
values[EPS_MIN] = ql_get_epsfin();
}
void set_qlearning_values()
{
pthread_mutex_lock(&mux_parameter_values);
ql_set_learning_rate(values[ALPHA]);
ql_set_discount_factor(values[GAMMA]);
ql_set_expl_decay(values[DECAY]);
ql_set_epsini(values[EPS_MAX]);
ql_set_epsfin(values[EPS_MIN]);
ql_set_epsilon(values[EPS_MAX]);
pthread_mutex_unlock(&mux_parameter_values);
}
//-----------------------------------------------------
// The following Functions manage the
// application acceleration
//-----------------------------------------------------
void change_pause_graphic()
{
pthread_mutex_lock(&mux_pause_graphic);
pause_graphic = (pause_graphic)?0:1;
pthread_mutex_unlock(&mux_pause_graphic);
}
int get_pause_graphic()
{
int temp;
pthread_mutex_lock(&mux_pause_graphic);
temp = pause_graphic;
pthread_mutex_unlock(&mux_pause_graphic);
return temp;
}
//-----------------------------------------------------
// The following Functions manage and track
// the System State
//-----------------------------------------------------
void set_sys_state(int i)
{
if(i >= 0 && i < 4){
pthread_mutex_lock(&mux_sys_state);
sys_state = i;
pthread_mutex_unlock(&mux_sys_state);
}
}
int get_sys_state(int *s)
{
pthread_mutex_lock(&mux_sys_state);
*s = sys_state;
pthread_mutex_unlock(&mux_sys_state);
return *s;
}
//------------------------------------------------------
// The following function waits for a key pressed and
// extracts the corresponding ascii code and scan code
//------------------------------------------------------
char get_scancode()
{
if(keypressed())
return readkey()>>8;
else
return 0;
}
//-------------------------------------------------------------
// The following Function manages the interaction with the user
// and the state of the system.
//-------------------------------------------------------------
void key_manager(int exec)
{
int p; // Tracks Qlearning parameters change
int pg; // Tracks pause_graphics
char cm;
cm = get_scancode();
switch(cm){
/* Writing/Reading Q-Matrix to/from file */
case KEY_F:
if(!get_pause_graphic()){
printf("INTERPRETER: hai premuto il tasto F\n");
if(exec == PAUSE){
ql_Q_to_file(Q_MATRIX_FILE);
printf("INTERPRETER: Salvata matrice Q su file\n");
}
}
break;
case KEY_L:
if(!get_pause_graphic()){
printf("INTERPRETER: hai premuto il tasto L\n");
if(exec == RESET){
if(ql_Q_from_file(Q_MATRIX_FILE) == 1)
printf("INTERPRETER: Letta matrice Q da file\n");
}
}
break;
/*---------------------------------------*/
case KEY_R:
if(!get_pause_graphic()){
printf("INTERPRETER: hai premuto il tasto R\n");
set_sys_state(RESET);
init_state();
}
break;
case KEY_S:
if(!get_pause_graphic()){
printf("INTERPRETER: hai premuto il tasto S\n");
if(exec == RESET)
set_sys_state(PLAY);
}
break;
case KEY_P:
if(!get_pause_graphic()){
printf("INTERPRETER: hai premuto il tasto P\n");
if(exec == PLAY)
set_sys_state(PAUSE);
else if(exec == PAUSE)
set_sys_state(PLAY);
}
break;
case KEY_E:
printf("INTERPRETER: hai premuto il tasto E\n");
set_sys_state(STOP);
break;
case KEY_UP:
if(sys_state == RESET){
printf("INTERPRETER: hai premuto il tasto UP\n");
dec_parameter_selected();
}
break;
case KEY_DOWN:
if(sys_state == RESET){
printf("INTERPRETER: hai premuto il tasto DOWN\n");
inc_parameter_selected();
}
break;
case KEY_RIGHT:
if(sys_state == RESET){
printf("INTERPRETER: hai premuto il tasto RIGHT\n");
p = get_parameter_selected();
inc_parameter_value(p);
}
break;
case KEY_LEFT:
if(sys_state == RESET){
printf("INTERPRETER: hai premuto il tasto LEFT\n");
p = get_parameter_selected();
dec_parameter_value(p);
}
break;
case KEY_B:
printf("INTERPRETER: hai premuto il tasto G\n");
change_pause_graphic();
pg = get_pause_graphic();
if(pg == 0){ // accelerator disabled
set_dyn_dt(DT_STD); // setting dynamic integration dt
// setting qlearn task parameters
pt_set_period(CRAWLER, T/DT_STD*PER_STD*1000);
pt_set_deadline(CRAWLER, T/DT_STD*PER_STD*1000);
// setting model task parameters
pt_set_period(MODEL, PER_STD*1000);
pt_set_deadline(MODEL, PER_STD*1000);
} else{ // accelerator enabled
set_dyn_dt(DT_FAST); // setting dynamic integration dt
// setting qlearn task parameters
pt_set_period(CRAWLER, T/DT_FAST*PER_FAST*1000);
pt_set_deadline(CRAWLER, T/DT_FAST*PER_FAST*1000);
// setting model task parameters
pt_set_period(MODEL, PER_FAST*1000);
pt_set_deadline(MODEL, PER_FAST*1000);
}
break;
default: break;
}
}
//---------------------------------------
// User Interface Task
// --------------------------------------
void* interpreter(void * arg)
{
printf("INTERPRETER: task started\n");
int i, exec;
i = pt_get_index(arg);
pt_set_activation(i);
set_sys_state(RESET);
init_parameter_values();
while(get_sys_state(&exec) != STOP){
key_manager(exec);
pt_deadline_miss(i);
pt_wait_for_period(i);
}
printf("INTERPRETER: task finished\n");
return NULL;
}
//---------------------------------------
// Manual Mode Functions
// --------------------------------------
#define TH1UP 0 // Action move up link 1
#define TH1DW 1 // Action move down link 1
#define TH2UP 2 // Action move up link 2
#define TH2DW 3 // Action move down link 2
void key_manager_manual(int exec)
{
char cm;
cm = get_scancode();
switch(cm){
case KEY_R:
printf("INTERPRETER: hai premuto il tasto R\n");
set_sys_state(RESET);
break;
case KEY_S:
printf("INTERPRETER: hai premuto il tasto S\n");
if(exec == RESET){
init_state();
set_sys_state(1);
}
break;
case KEY_P:
printf("INTERPRETER: hai premuto il tasto P\n");
if(exec == PAUSE)
set_sys_state(PLAY);
else if(exec == PLAY)
set_sys_state(PAUSE);
break;
case KEY_E:
printf("INTERPRETER: hai premuto il tasto E\n");
set_sys_state(STOP);
break;
case KEY_UP:
printf("INTERPRETER: hai premuto il tasto UP\n");
next_desired_state(TH1UP);
break;
case KEY_DOWN:
printf("INTERPRETER: hai premuto il tasto DOWN\n");
next_desired_state(TH1DW);
break;
case KEY_RIGHT:
printf("INTERPRETER: hai premuto il tasto RIGHT\n");
next_desired_state(TH2UP);
break;
case KEY_LEFT:
printf("INTERPRETER: hai premuto il tasto LEFT\n");
next_desired_state(TH2DW);
break;
default: break;
}
}
void* manual_interpreter(void* arg)
{
int i, exec;
printf("INTERPRETER: task started\n");
i = pt_get_index(arg);
pt_set_activation(i);
set_sys_state(RESET);
init_parameter_values();
while(get_sys_state(&exec) != STOP){
key_manager_manual(exec);
pt_deadline_miss(i);
pt_wait_for_period(i);
}
printf("INTERPRETER: task finished\n");
return NULL;
}