-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparameters.cpp
174 lines (142 loc) · 4.54 KB
/
parameters.cpp
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
/*
* parameters.cpp
*
* Created on: Apr 21, 2012
* Author: jairo
*/
#include "wirish.h"
#include "parameters.h"
#include "utils.h"
#include "MyPID.h"
int interactive_config(paramTable *p) {
static int inputVal_1e5; // input value 1e5 times larger (need to divide by 1e5)
float inputVal;
static int state = 0;
static pidParams *p_p; // pointer, temp pidParams
static float *p_v; // pointer, temp param value
while(SerialUSB.available())
{
uint8 input = SerialUSB.read();
switch(state) {
case 0:
if(input == 't') { // tune PID
SerialUSB.println("\r\ninteractive tuning. select axis [r,p,y]. Current gains:");
state++;
// signal that interactive tuning started
return PARAM_INT_TUNE_FLAG_START;
}
break;
case 1: // select axis
inputVal_1e5 = 0;
switch(input) {
case 'r': // roll
p_p = &(p->roll);
SerialUSB.print("\r\nroll selected.");
state++;
break;
case 'p': // pitch
p_p = &(p->pitch);
SerialUSB.print("\r\npitch selected.");
state++;
break;
case 'y': // yaw
p_p = &(p->yaw);
SerialUSB.print("\r\nyaw selected.");
state++;
break;
default: // reset state
state=0;
break;
}
if(state != 0)
SerialUSB.print(" select gain [p,i,d]:");
break;
case 2:
switch(input) {
case 'p': // Kp
p_v = &(p_p->Kp);
SerialUSB.print("\r\nKp selected.");
state++;
break;
case 'i': // Ki
p_v = &(p_p->Ki);
SerialUSB.print("\r\nKi selected.");
state++;
break;
case 'd': // Kd
p_v = &(p_p->Kd);
SerialUSB.print("\r\nKd selected.");
state++;
break;
default:
state=0;
break;
}
if(state != 0) {
SerialUSB.print(" Curr:");
SerialUSB.print(*p_v,6);
SerialUSB.println(" input value [0-9]:");
}
break;
case 3:
// check that the next input is a number
if(input > 0x2F && input < 0x3A)
{
inputVal_1e5 += (input-0x30);
inputVal_1e5 *= 10;
SerialUSB.print("\r");
SerialUSB.print((float)((float)inputVal_1e5/1000000.0),6);
}else if(input == '\r') {
inputVal_1e5 /= 10;
inputVal = ((float)inputVal_1e5)/100000.0;
// SerialUSB.print("inputVal:");
// SerialUSB.print(inputVal, 8);
//
// SerialUSB.print(" p->roll.Kp=");
// SerialUSB.print(p->roll.Kp, 8);
// SerialUSB.print(" *p_v=");
// SerialUSB.print(*(float *)p_v, 8);
*(float *)(p_v) =inputVal;
SerialUSB.println("Done.");
// done
state=0;
return PARAM_INT_TUNE_FLAG_DONE;
}else{
printkv("ERROR, input:", input);
state=0;
}
break;
default:
state=0;
break;
}
}
if(state == 0)
return PARAM_INT_TUNE_FLAG_IDLE;
else
return PARAM_INT_TUNE_FLAG_ACTIVE;
}
void update_gains(paramTable *p, class MyPID *roll, class MyPID *pitch, class MyPID *yaw)
{
pidParams *p_axis;
p_axis = &(p->roll);
roll->set_gains(p_axis->Kp, p_axis->Ki, p_axis->Kd);
p_axis = &(p->pitch);
pitch->set_gains(p_axis->Kp, p_axis->Ki, p_axis->Kd);
p_axis = &(p->yaw);
yaw->set_gains(p_axis->Kp, p_axis->Ki, p_axis->Kd);
print_gains(roll, pitch, yaw);
}
void print_gains(class MyPID *roll, class MyPID *pitch, class MyPID *yaw)
{
printkv("rkp:", roll->get_gain('p') );
printkv("rki:", roll->get_gain('i') );
printkv("rkd:", roll->get_gain('d') );
printkv("pkp:", pitch->get_gain('p') );
printkv("pki:", pitch->get_gain('i') );
printkv("pkd:", pitch->get_gain('d') );
printkv("ykp:", yaw->get_gain('p') );
printkv("yki:", yaw->get_gain('i') );
printkv("ykd:", yaw->get_gain('d') );
SerialUSB.println("");
}