-
Notifications
You must be signed in to change notification settings - Fork 19
/
controllerbase.hh
338 lines (302 loc) · 8.71 KB
/
controllerbase.hh
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
#ifndef __CONTROLLER_HH
#define __CONTROLLER_HH
#include "calibration.hh"
#include "path.hh"
const int BASE = 0;
const int SHOULDER = 1;
const int ELBOW = 2;
const int ROLL = 3;
const int PITCH = 4;
const int WRIST = 5;
const int GRIPPER = 6;
const int DRIVES = 7;
const float ELBOW_RANGE = 60;
class ControllerBase
{
public:
ControllerBase(void): m_number(0), m_fraction(0), m_sign(0), m_teachFun(NULL), m_index(0) {
memset(m_teach, 0, sizeof(m_teach));
memset(m_configuration, 0, sizeof(m_configuration));
}
virtual ~ControllerBase() {}
Path &curve(int drive) { return m_curve[drive]; }
int drive(char c) {
switch (tolower(c)) {
case 's':
return SHOULDER;
case 'e':
return ELBOW;
case 'r':
return ROLL;
case 'p':
return PITCH;
case 'w':
return WRIST;
case 'g':
return GRIPPER;
default:
return BASE;
};
}
float target(int drive) {
return m_curve[drive].target();
}
float limit(float value, float lower, float upper) {
return value < lower ? lower : value > upper ? upper : value;
}
float angleToPWM(int drive, float angle) {
return offset(drive) + angle * resolution(drive);
}
float pwmToAngle(int drive, float pwm) {
return (pwm - offset(drive)) / resolution(drive);
}
float clipPWM(int drive, float value) {
return limit(value, lower(drive), upper(drive));
}
float clipAngle(int drive, float value) {
return pwmToAngle(drive, clipPWM(drive, angleToPWM(drive, value)));
}
float limitJoint(float value, float other) {
return limit(value, -ELBOW_RANGE - other, ELBOW_RANGE - other);
}
float limitArmAngle(int drive, float value)
{
switch (drive) {
case ELBOW:
return limitJoint(value, target(SHOULDER));
case SHOULDER:
return limitJoint(value, target(ELBOW));
default:
return value;
};
}
void saveTeachPoint(int index) {
for (int i=0; i<DRIVES; i++)
m_teach[index][i] = target(i);
}
void loadTeachPoint(int index) {
targetPoint(m_teach[index]);
}
void displayTeachPoint(int index) {
reportTeachPoint(m_teach[index][0], m_teach[index][1], m_teach[index][2],
m_teach[index][3], m_teach[index][4], m_teach[index][5],
m_teach[index][6]);
}
void takeConfigurationValue(void) {
if (m_index < DRIVES) {
float angle = clipAngle(m_index, number());
m_configuration[m_index] = angle;
m_index++;
};
resetNumber();
}
float timeRequired(int drive, float angle) {
return Profile::timeRequired(fabs(angle - target(drive)), MAXJERK);
}
float timeRequired(float point[]) {
float retval = 0;
for (int i=0; i<DRIVES; i++) {
float driveTime = timeRequired(i, point[i]);
retval = retval < driveTime ? driveTime : retval;
};
return retval;
}
void targetAngleUnsafe(int drive, float angle, float time) {
m_curve[drive].retarget(angle, time);
}
void targetPWM(int drive, float pwm) {
float angle = limitArmAngle(drive, pwmToAngle(drive, clipPWM(drive, pwm)));
targetAngleUnsafe(drive, angle, timeRequired(drive, angle));
}
void targetAngle(int drive, float value) {
float angle = limitArmAngle(drive, clipAngle(drive, value));
targetAngleUnsafe(drive, angle, timeRequired(drive, angle));
}
void targetPoint(float point[])
{
float time = timeRequired(point);
for (int i=0; i<DRIVES; i++)
targetAngleUnsafe(i, i == ELBOW ? limitArmAngle(ELBOW, point[i]) : point[i], time);
}
void update(float dt) {
for (int drive=0; drive<DRIVES; drive++)
writePWM(drive, round(angleToPWM(drive, m_curve[drive].update(dt))));
}
void stopDrives(void) {
resetParser();
for (int drive=0; drive<DRIVES; drive++)
m_curve[drive].stop(m_curve[drive].pos());
}
bool hasNumber(void) {
return m_sign != 0;
}
float number(void) {
float fraction = (m_fraction == 0) ? 1 : m_fraction;
return m_number * fraction * m_sign;
}
void resetNumber(void) {
m_number = 0;
m_fraction = 0;
m_sign = 0;
}
void resetParser(void) {
resetNumber();
memset(m_configuration, 0, sizeof(m_configuration));
m_index = 0;
}
bool drivesReady(void) {
bool retval = true;
for (int i=0; i<DRIVES; i++)
retval = retval && m_curve[i].ready();
return retval;
}
void parseChar(char c) {
if (m_teachFun) {
if (c >= 'a' && c <= 'l')
(this->*m_teachFun)(c - 'a');
else
stopDrives();
resetParser();
m_teachFun = NULL;
} else {
switch (c) {
case 'o':
reportReady(drivesReady());
resetParser();
break;
case 't':
if (hasNumber()) {
takeConfigurationValue();
reportRequired(timeRequired(m_configuration));
} else
reportTime();
resetParser();
break;
case 'T':
reportRemaining(m_curve[BASE].timeRemaining());
resetParser();
break;
case '.':
if (m_fraction > 0)
resetParser();
else
m_fraction = 1;
break;
case '-':
m_sign = m_sign == 0 ? -1 : -m_sign;
m_number = 0;
m_fraction = 0;
break;
case 'b':
case 'B':
case 'e':
case 'E':
case 's':
case 'S':
case 'r':
case 'R':
case 'p':
case 'P':
case 'w':
case 'W':
case 'g':
case 'G':
if (hasNumber()) {
if (isupper(c))
targetPWM(drive(c), number());
else
targetAngle(drive(c), number());
} else
if (isupper(c))
reportPWM(round(angleToPWM(drive(c), m_curve[drive(c)].pos())));
else
reportAngle(m_curve[drive(c)].pos());
resetParser();
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
m_number = 10 * m_number + (c - '0');
m_fraction *= 0.1;
if (m_sign == 0) m_sign = 1;
break;
case '\'':
m_teachFun = &ControllerBase::loadTeachPoint;
break;
case 'm':
m_teachFun = &ControllerBase::saveTeachPoint;
break;
case 'd':
m_teachFun = &ControllerBase::displayTeachPoint;
break;
case ' ':
takeConfigurationValue();
break;
case 'c':
if (hasNumber()) {
takeConfigurationValue();
targetPoint(m_configuration);
} else
reportConfiguration(m_curve[0].pos(), m_curve[1].pos(), m_curve[2].pos(),
m_curve[3].pos(), m_curve[4].pos(), m_curve[5].pos(),
m_curve[6].pos());
resetParser();
break;
case 'l':
reportLower((lower(0) - offset(0)) / resolution(0),
(lower(1) - offset(1)) / resolution(1),
(lower(2) - offset(2)) / resolution(2),
(lower(3) - offset(3)) / resolution(3),
(lower(4) - offset(4)) / resolution(4),
(lower(5) - offset(5)) / resolution(5),
(lower(6) - offset(6)) / resolution(6));
resetParser();
break;
case 'u':
reportUpper((upper(0) - offset(0)) / resolution(0),
(upper(1) - offset(1)) / resolution(1),
(upper(2) - offset(2)) / resolution(2),
(upper(3) - offset(3)) / resolution(3),
(upper(4) - offset(4)) / resolution(4),
(upper(5) - offset(5)) / resolution(5),
(upper(6) - offset(6)) / resolution(6));
resetParser();
break;
default:
stopDrives();
};
};
}
virtual int offset(int drive) = 0;
virtual float resolution(int drive) = 0;
virtual int lower(int drive) = 0;
virtual int upper(int drive) = 0;
virtual void reportReady(bool ready) = 0;
virtual void reportTime(void) = 0;
virtual void reportRequired(float time) = 0;
virtual void reportRemaining(float time) = 0;
virtual void reportAngle(float) = 0;
virtual void reportPWM(int) = 0;
virtual void reportConfiguration(float, float, float, float, float, float, float) = 0;
virtual void reportLower(float, float, float, float, float, float, float) = 0;
virtual void reportUpper(float, float, float, float, float, float, float) = 0;
virtual void reportTeachPoint(float, float, float, float, float, float, float) = 0;
virtual void writePWM(int, int) = 0;
protected:
float m_number;
float m_fraction;
char m_sign;
void (ControllerBase::*m_teachFun)(int);
float m_teach[12][DRIVES];
int m_index;
float m_configuration[DRIVES];
Path m_curve[DRIVES];
};
#endif