forked from cgwood/ArdustationMega
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagePID.ino
612 lines (546 loc) · 13.9 KB
/
pagePID.ino
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// Created 2011 By Colin G http://www.diydrones.com/profile/ColinG
// Modified 2013 by Colin G for use with Ardustation Mega http://www.diydrones.com/profiles/blogs/ardustation-mega-with-graphic-lcd
////////////////////////////////////////////////////////////////////////////////
// PID Setup page
////////////////////////////////////////////////////////////////////////////////
#define PIDFIELDWIDTH 4
#define PIDDECPOS 2
/// @todo Could make this a generic parameter editing page?
PagePID::PagePID() //const prog_char *textHeader, const uint8_t *pidTypes, const uint8_t *pid_p, const uint8_t *pid_i, const uint8_t *pid_d)
{
// /// Copy the header and types to internal storage
// _textHeader = textHeader;
// _pidTypes = pidTypes;
// _pid_p = pid_p;
// _pid_i = pid_i;
// _pid_d = pid_d;
//
_pid_p[0] = RATE_RLL_P;
_pid_p[1] = RATE_PIT_P;
_pid_p[2] = RATE_YAW_P;
_pid_i[0] = RATE_RLL_I;
_pid_i[1] = RATE_PIT_I;
_pid_i[2] = RATE_YAW_I;
_pid_d[0] = RATE_RLL_D;
_pid_d[1] = RATE_PIT_D;
_pid_d[2] = RATE_YAW_D;
// uav.param[RATE_RLL_P] = 0.15;
/// XXX Initially, all values are available (will require attention)
_avail[0] = 1;
_avail[1] = 1;
_avail[2] = 1;
_textHeader = pidHeaderRPY;
_state = 0;
// lcd.ClearArea();
// _refresh_med();
}
uint8_t
PagePID::_enter() {
// gcs3.params_request();
_drawLocal();
return 0;
}
uint8_t
PagePID::_forceUpdate(uint8_t reason) {
if (reason == R_PARAM) {
// Exit edit mode
_state = 0;
// Redraw the entire page
_drawLocal();
}
return 0;
}
//void
//PagePID::_enter(uint8_t fromPage)
//{
//// if (fromPage == P_PIDCONFIRM || fromPage == P_NAVPIDCONFIRM)
//// _uploadLocal();
//
// uint8_t i;
//
// for (i=0;i<3;i++) {
// //if (!_avail[i])
// // comm.send_msg_pid_request(_pidTypes[i]);
// }
//
// _state = 0;
// lcd.ClearArea();
// _render();
//}
//
//void
//PagePID::notify(void *arg, mavlink_message_t *buf)
//{
//// // Put the message to the protected message function
//// ((PagePID *)arg)->_message(messageID, messageVersion, buf);
// ((PagePID *)arg)->_message();
//
//}
//
//void
//PagePID::_message(void)
//{
//
// // Exit edit mode
// _state = 0;
//
// // Notify the update function
// _updated = true;
//
//}
//
//void
//PagePID::_update(void)
//{
// // don't waste time redrawing if we have no changes to announce
// if (!_updated)
// return;
//
// // don't redraw too often
// if ((millis() - _lastRedraw) < STATUS_UPDATE_INTERVAL)
// return;
//
// // redraw the page
// _render();
// _updated = false;
// _lastRedraw = millis();
//}
//
//
uint8_t
PagePID::_refresh_med()
{
}
uint8_t
PagePID::_refresh_slow()
{
return 0;
}
void
PagePID::_drawLocal()
{
uint8_t c;
uint8_t i;
uint8_t j;
uint8_t lineno;
char decBuf[PIDFIELDWIDTH];
uint32_t value;
float value_local;
lcd.CursorTo(0, 0);
for (i=0;i<19;i++) {
// Write the header, e.g. roll pitch yaw
c = pgm_read_byte_near(_textHeader + i);
if (0 == c)
break;
else
lcd.write(c);
}
// lcd.print(" roll pitch yaw");
for (lineno=1;lineno<4;lineno++) {
lcd.CursorTo(0, lineno);
// Write the left hand labels for PID
if (lineno==1)
lcd.print("P:");
if (lineno==2)
lcd.print("I:");
if (lineno==3)
lcd.print("D:");
// Write the values
for (i=0;i<3;i++) {
value_local = 0;
if (_avail[i] == 1) {
// Load the PID into value, either editing or live val
if (lineno==1) {
// if (i == (_state-101)%3)
if (_state == 100 + (lineno-1)*3 + i + 1)
value_local = _value_temp; //_pidtemp.p;
else {
j = _pid_p[i];
// nvram.load_param(&j,&value_local);
value_local = uav.rate_pid_copter[j];
}
}
if (lineno==2) {
// if (i == (_state-101)%3)
if (_state == 100 + (lineno-1)*3 + i + 1)
value_local = _value_temp; //_pidtemp.i;
else {
j = _pid_i[i];
// nvram.load_param(&j,&value_local);
value_local = uav.rate_pid_copter[j];
}
}
if (lineno==3) {
// if (i == (_state-101)%3)
if (_state == 100 + (lineno-1)*3 + i + 1)
value_local = _value_temp; //_pidtemp.d;
else {
j = _pid_d[i];
// nvram.load_param(&j,&value_local);
value_local = uav.rate_pid_copter[j];
}
}
// Scale the PID value and fix for the number of decimal places
value_local *= pow(10,PIDDECPOS);
value = (uint32_t)(floor(value_local+0.5));
decBuf[0] = '0' + (value % 10);
value /= 10;
for (j=1;j<PIDFIELDWIDTH;j++) {
if (j == PIDDECPOS) {
decBuf[j] = '.';
}
else if ((0 == value) && (j > (PIDDECPOS + 1))) {
decBuf[j] = ' ';
}
else {
decBuf[j] = '0' + (value % 10);
value /= 10;
}
}
}
else {
// Data unavailable, display dashes
for (j=0;j<PIDFIELDWIDTH;j++)
decBuf[j] = '-';
}
// Display the data
lcd.write(' ');
while (j-- > 0)
lcd.write(decBuf[j]);
lcd.write(' ');
}
}
// redraw the "choosing" marker
_paintMarker();
}
/// Draw the "choosing" marker
void
PagePID::_paintMarker(void)
{
if (_state > 0 && _state < 100) {
lcd.CursorTo(((_state-1)%3+1)*6-4,(_state-1)/3+1);
lcd.write('>');
}
else if (_state > 100 && _state < 200)
{
lcd.CursorTo(((_state-101)%3+1)*6-4,(_state-101)/3+1);
lcd.write('#');//LCD_CHAR_MODIFY);
}
}
/// remove the "choosing" marker
void
PagePID::_clearMarker(void)
{
if (_state > 0 && _state < 200) {
if (_state > 100)
lcd.CursorTo(((_state-101)%3+1)*6-4,(_state-101)/3+1);
else
lcd.CursorTo(((_state-1)%3+1)*6-4,(_state-1)/3+1);
lcd.write(' ');
}
}
void
PagePID::_alterLocal(float alterMag)
{
// uint8_t i,j;
// i = (_state-101)%3; // Column, i.e. Roll/pitch/yaw
// j = (_state-101)/3; // Row, i.e. PID
_value_temp = constrain(_value_temp + alterMag, 0, 3);
_value_encoder = _value_temp*100;
// //@bug sometimes this makes the value wrap around
// //@{
// switch ((_state-101)/3) {
// case 0:
// _pidtemp.p = constrain(_pidtemp.p + alterMag, 0, 3000000);
// break;
// case 1:
// _pidtemp.i = constrain(_pidtemp.i + alterMag, 0, 3000000);
// break;
// case 2:
// _pidtemp.d = constrain(_pidtemp.d + alterMag, 0, 3000000);
// break;
// }
// //@}
// kick the update function
_updated = true;
}
void
PagePID::_redrawLocal(void)
{
// uint8_t c;
uint8_t i;
uint8_t j;
// uint8_t lineno;
char decBuf[PIDFIELDWIDTH];
uint32_t value;
float value_local;
// Check that the state is not in confirmation mode and a value is being pointed at
if (_state < 200 && _state > 0) {
if (_state > 100) {
value_local = _value_temp;
lcd.CursorTo(((_state-101)%3+1)*6-3,(_state-101)/3+1);
}
else {
lcd.CursorTo(((_state-1)%3+1)*6-3,(_state-1)/3+1);
Serial.println(_state);
Serial.println(((_state-1)%3+1)*6-3);
Serial.println((_state-1)/3+1);
}
// Scale the PID value and fix for the number of decimal places
value_local *= pow(10,PIDDECPOS);
value = (uint32_t)(floor(value_local+0.5));
decBuf[0] = '0' + (value % 10);
value /= 10;
for (j=1;j<PIDFIELDWIDTH;j++) {
if (j == PIDDECPOS) {
decBuf[j] = '.';
}
else if ((0 == value) && (j > (PIDDECPOS + 1))) {
decBuf[j] = ' ';
}
else {
decBuf[j] = '0' + (value % 10);
value /= 10;
}
}
//j = PIDFIELDWIDTH;
// Display the data
while (j-- > 0)
lcd.write(decBuf[j]);
}
}
void
PagePID::_voidLocal(void)
{
uint8_t j;
// Overwrite value
switch((_state-101)/3) {
case 0:
j = _pid_p[(_state-101)%3];
break;
case 1:
j = _pid_i[(_state-101)%3];
break;
case 2:
j = _pid_d[(_state-101)%3];
break;
default:
return;
}
_value_temp = uav.rate_pid_copter[j];
_redrawLocal();
// Reset _state
_state = 0;
// kick the update function
_updated = true;
}
void
PagePID::_uploadConfirm(void)
{
uint8_t c;
uint8_t row;
const prog_char *str;
str = confirmMessage;
// lcd.CursorTo(0, 0);
lcd.ClearArea();
row = 0;
for (;;) {
c = pgm_read_byte_near(str++);
if (0 == c)
break;
if ('\n' == c) {
lcd.CursorTo(0, ++row);
continue;
}
// emit
lcd.write(c);
}
}
void
PagePID::_uploadLocal(void)
{
// uint8_t i;
uint8_t j;
// i = (_state-101)%3; // Column, i.e. Roll/pitch/yaw
switch((_state-201)/3) {
case 0:
j = _pid_p[(_state-201)%3];
break;
case 1:
j = _pid_i[(_state-201)%3];
break;
case 2:
j = _pid_d[(_state-201)%3];
break;
default:
return;
}
Serial.println("Uploading value");
char str_param_id[15];
// First initialise the string to be empty
for (uint8_t i=0;i<15;i++)
str_param_id[i] = 0;
// Copy the relevant one into memory
strcpy_P(str_param_id, (char*)pgm_read_word(&(paramTable_rate_pid_copter[j])));
// Construct the packet
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
//static inline uint16_t mavlink_msg_param_set_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg,
// uint8_t target_system, uint8_t target_component, const char *param_id, float param_value, uint8_t param_type)
mavlink_msg_param_set_pack(0xFF, 0x00, &msg, uav.sysid, apm_mav_component, (const char*)&str_param_id, _value_temp, MAV_PARAM_TYPE_REAL32);
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
Serial3.write(buf, len);
// params.set_param(j, _value_temp); // want this one, but need params
// params.set_param(_pid_p[i], _value_temp);
// Send the value that we edited
//comm.send_msg_pid_set(_pidTypes[(_state-201)%3], _pidtemp.p, _pidtemp.i, _pidtemp.d, _pidtemp.integratorMax);
// Reset _state
_state = 0;
// kick the update function
_updated = true;
}
uint8_t
PagePID::_interact(uint8_t buttonid)
{
/// State positions are:
/// 1 2 3
/// 4 5 6
/// 7 8 9
_clearMarker();
switch(buttonid) {
case B_UP:
// Navigation
if (_state > 3 && _state < 10)
_state -= 3;
// Editing
else if (_state > 100 && _state < 200)
_alterLocal(0.01);
// Confirming
else if (_state > 200)
return 0;
break;
case B_DOWN:
// Navigation
if (_state > 0 && _state < 7)
_state += 3;
// Editing
else if (_state > 100 && _state < 200)
_alterLocal(-0.01);
// Confirming
else if (_state > 200)
return 0;
break;
case B_OK:
if (_state == 0) {
// Allow selection if the value is available
// if (_avail[(_state-1)%3])
if (_avail[0])
_state = 1;
}
else if(_state < 100) {
// Allow editing if the value is available
if (_avail[(_state-1)%3]) {
// Copy the PID values to temp for editing
uint8_t j;
switch((_state-1)/3) {
case 0:
j = _pid_p[(_state-1)%3];
break;
case 1:
j = _pid_i[(_state-1)%3];
break;
case 2:
j = _pid_d[(_state-1)%3];
break;
default:
return 0;
}
// nvram.load_param(&j,&_value_temp);
_value_temp = uav.rate_pid_copter[j];
_value_encoder = _value_temp * 100;
rotary.configure(&_value_encoder, 300, 0, -4);
//rotary.configure(&encoderval, &_value_temp, 0, 10, -4);
// _pidtemp.pidSet = _pidlive[(_state-1)%3].pidSet;
// _pidtemp.p = _pidlive[(_state-1)%3].p;
// _pidtemp.i = _pidlive[(_state-1)%3].i;
// _pidtemp.d = _pidlive[(_state-1)%3].d;
// _pidtemp.integratorMax = _pidlive[(_state-1)%3].integratorMax;
// Update the state
_state += 100;
}
}
else if(_state > 100 && _state < 200) {
// Save the value
//_leave(OK);
_state += 100;
_uploadConfirm();
return 0; // Leave before we draw the marker again
}
else if(_state > 200) {
// Save the value
//_leave(OK);
_uploadLocal();
_drawLocal();
return 0; // Leave before we draw the marker again
}
break;
case B_ENCODER:
if (_state > 100 && _state < 200) {
_value_temp = _value_encoder / 100.0;
_redrawLocal();
}
break;
case B_CANCEL:
if (_state == 0) {
Pages::move(0);
return 0; // avoid drawing the cursor
}
else {
if (_state > 100) {
if (_state > 200) {
_drawLocal();
}
// Don't save the changes to local variable
_voidLocal();
// Ensure state has returned to nothing selected
_state = 0;
}
else
_state = 0;
}
break;
case B_LEFT:
// Navigation
if (_state == 0)
Pages::move(-1);
else if (_state < 100 && _state % 3 != 1)
_state -= 1;
// Editing
else if (_state > 100 && _state < 200)
_alterLocal(-0.1);
// Confirming
else if (_state > 200)
return 0;
break;
case B_RIGHT:
// Navigation
if (_state == 0)
Pages::move(1);
else if (_state < 100 && _state % 3 != 0)
_state += 1;
// Editing
else if (_state > 100 && _state < 200)
_alterLocal(0.1);
// Confirming
else if (_state > 200)
return 0;
break;
// case B_LEFT:
// Pages::move(-1);
// break;
// case B_RIGHT:
// Pages::move(1);
// break;
}
_paintMarker();
}