-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch_feb28a.ino
375 lines (325 loc) · 8.94 KB
/
sketch_feb28a.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
#include <DueTimer.h>
#include <RingBuf.h>
#include <AsyncDelay.h>
#include "MOTOR.h"
#include "PENDULUM.h"
#include "CART.h"
#include "VarKV.h"
#include "PIDF.h"
#include "Butter1.h"
#define FSAMP 100
MOTOR Motor(20.0);
CART Cart(Motor, 1.14);
PENDULUM Pendulum;
#define UNUSED(a)
#define VAR(a) {#a, &a}
#define VAR_PID(a) {"Kp_" #a, &a.Kp}, {"Ki_" #a, &a.Ki}, {"Kd_" #a, &a.Kd}, {"sat_" #a, &a.saturation}, {"Kb_" #a, &a.Kb}, {"Cf_" #a, &a.filt.cutoff}
/// !!! Modify code here !!!
float pendulum_length = 0.25;
Butter1 filt1(5, FSAMP);
PIDF pid_cart(50, 0, 0, 0, 20, filt1, FSAMP);
Butter1 filt2(5, FSAMP);
PIDF pid_pendulum(60, 0, 0, 0, 20, filt2, FSAMP);
//float test = 0.0;
//float start = 0.0;
//float tfreq = 0.5;
//float tamp = 0.1;
float lim_pend = 20;
float lim_cart = 20;
//add controllable variables here {"name_string", pointer_to_variable}
VarKV vars({
//VAR(pendulum_length), VAR(test), VAR(start), VAR(tfreq), VAR(tamp),
VAR(lim_pend), VAR(lim_cart),
VAR_PID(pid_cart), VAR_PID(pid_pendulum)
});
//add pointers to PIDs here to reset on variable change and update frequency
PIDF* pids[] = { &pid_cart, &pid_pendulum };
/// Controller
/*
* angle: of pendulum in radians from resting position
* position: of the load on the rail in meters
* target: current position target
* period: controller period
*
* returns: voltage input to motor
*/
float controller(float angle, float position, float target, float /*frequency*/) {
float E_pendulum = -angle;
float E_cart = target - position;
float u_cart = pid_cart.process(E_cart);
float u_pend = pid_pendulum.process(E_pendulum);
u_cart = constrain(u_cart, -lim_cart, lim_cart);
u_pend = constrain(u_pend, -lim_pend, lim_pend);
float out = u_cart + u_pend;
/*float out = 0;
if(test == 1.0){
if(start == 0) start = millis();
out = (millis() > (start + 4000)) ? 0 : 5;
}
else if(test == 2.0){
if(start == 0) start = millis();
out = (millis() > (start + 4000)) ? 0 : -5;
}
else if(test == 3.0){
if(start == 0) start = millis();
out = (millis() > (start + 2300)) ? 0 : 10;
}
else if(test == 4.0){
if(start == 0) start = millis();
out = (millis() > (start + 2300)) ? 0 : -10;
}
else if(test == 5.0){
if(start == 0) start = millis();
out = (millis() > (start + 250)) ? 0 : 20;
}
else if(test == 6.0){
if(start == 0) start = millis();
float curr = millis();
curr -= start;
curr /= 1000;
curr *= 2*PI;
out = cos(curr * tfreq)*tamp;
if(curr*tfreq >= 5*2*PI) //only 5 cycles
out = 0;
}*/
return out;
}
///
struct Sample {
uint32_t time;
float angle, position, voltage;
} __attribute__((packed));
volatile float target = 0;
volatile bool controller_enabled = false;
volatile bool stream_enabled = false;
RingBuf<Sample, 50> buf;
void call_controller() {
float voltage = 0;
float angle = Pendulum.read();
float position = Cart.read();
if(abs(position) > Cart.rail_limit) { //add switch interrupt?
controller_enabled = false;
Motor.write_voltage(0);
}
voltage = controller(angle, position/* - pendulum_length * sin(angle)*/, target, Timer2.getFrequency());
if(stream_enabled)
buf.push({micros(), angle, position, voltage});
if(controller_enabled == false)
return;
Motor.write_voltage(voltage);
}
#define SER SerialUSB
char *send_buf;
int send_buf_size;
int send_buf_elem_size;
int send_buf_index;
AsyncDelay send_timeout(20, AsyncDelay::MILLIS);
void setup() {
SER.begin(115200);
Serial.begin(115200); //debug
SER.setTimeout(100);
Cart.begin();
Motor.begin();
Pendulum.begin();
send_buf_elem_size = sizeof (Sample) + 3;
send_buf_size = SER.availableForWrite() / send_buf_elem_size;
// assert(send_buf_size > 1);
send_buf = new char[send_buf_elem_size * send_buf_size];
send_buf_index = 0;
delay(2000);
Cart.calibrate();
Timer2.stop().attachInterrupt(call_controller).setFrequency(FSAMP);
while(SER.available()) //remove anything received during long calibration
SER.read();
}
#define GETFLOAT(a) if(SER.readBytes((char *)&a, 4) != 4) \
break;;
#define RESPOND if(!stream_enabled) SER.write("RES");
#define GETCHAR(a) a = 0; \
a = SER.readBytes((char *)&a, 1) == 0 ? -1 : a;
void loop() {
Sample samp;
// Load send buffer with new samples as long as there's space and samples
while(buf.pop(samp) && send_buf_index != send_buf_size) {
//char head[] = {'S', 'T','R'};
memcpy(&send_buf[send_buf_index], "STR", 3);
send_buf_index += 3;
memcpy(&send_buf[send_buf_index], &samp, sizeof samp);
send_buf_index += sizeof samp;
// SER.write("STR");
// SER.write((char *)&samp, sizeof samp);
}
// if buffer is full or time since last samples was too long send the buffer
// this part can lock if the previous buffer is still being sent, will result
// in samples being dropped on ring buffer
if((send_buf_index == (send_buf_size * send_buf_elem_size) ||
(send_timeout.isExpired() && send_buf_index != 0)) &&
stream_enabled) {
int bytes = send_buf_index;
send_buf_index = 0;
SER.write((char *) send_buf, bytes);
send_timeout.restart();
}
if(SER.available() == 0)
return;
char req_str[3] = {0, 0, 0};
SER.readBytes(req_str, 3);
if(memcmp(req_str, "REQ", 3) != 0)
return;
int c;
GETCHAR(c);
switch(c) {
//recalibrate
/*case 'R':
Timer2.stop();
Motor.write_voltage(0);
Cart.calibrate();
RESPOND;
break;*/
//controller
case 'C':
GETCHAR(c);
if(c == 'B') {
controller_enabled = true;
for(auto i : pids) {
i->reset();
}
RESPOND;
}
else if(c == 'E') {
controller_enabled = false;
Motor.write_voltage(0);
RESPOND;
}
break;
//move
case 'M':
float pos;
GETFLOAT(pos);
Cart.move_to(pos);
RESPOND;
break;
//voltage
case 'V':
GETCHAR(c);
float voltage;
if(c == 'S') {
GETFLOAT(voltage);
Motor.write_voltage(voltage);
RESPOND;
}
else if(c == 'L') {
GETFLOAT(voltage);
Motor.voltage_limit = voltage;
RESPOND;
}
break;
//stream
case 'S':
GETCHAR(c);
if(c == 'B') {
RESPOND;
stream_enabled = true;
}
else if(c == 'E') {
stream_enabled = false;
buf.clear();
RESPOND;
}
break;
//target
case 'T':
float target_new;
GETFLOAT(target_new);
target = constrain(target_new, -Cart.rail_limit, Cart.rail_limit);
RESPOND;
break;
//sampling freq
case 'F':
float freq;
GETFLOAT(freq);
freq = constrain(freq, 1, 5000);
Timer2.stop().setFrequency(freq);
for(auto i : pids) {
i->frequency = freq;
i->reset();
}
Timer2.start();
RESPOND;
break;
//ping
case 'P':
RESPOND;
break;
case 'K':
GETCHAR(c);
if(c == 'S') {
char name[51];
int len;
float value;
GETCHAR(len);
if(len > 50 || len < 0)
break;
if(SER.readBytes(name, len) != len)
break;
name[len] = 0;
GETFLOAT(value);
String tmp(name);
if(!vars.update(tmp, value))
break;
for(auto i : pids) {
i->reset();
}
RESPOND;
}
else if(c == 'L' && !stream_enabled) {
RESPOND;
byte size = vars.size;
SER.write(&size, 1);
for(int i = 0; i < vars.size; i++) {
String &cur = vars.keys[i];
byte cur_size = cur.length();
SER.write((char *)&cur_size, 1);
SER.write(cur.c_str(), cur_size);
SER.write((char *)vars.values[i], 4);
}
}
break;
case 'Q':
GETCHAR(c);
if(c == 'C') {
RESPOND;
SER.write((char *)&Cart.rail_length, 4);
}
else if(c == 'V') {
RESPOND;
SER.write((char *)&Motor.voltage_max, 4);
}
default:
//ignore
break;
}
}
/*
* Interface:
* packets:
* "REQ",command,params //request
* "RES" //response
* "STR",pendulum(float), cart(float) //stream data
* commands:
* CB(), start controller
* CE(), stop controller
* R(), force recallibration of rail width, stops controller-
* M(float pos), move cart to `pos` on rail, in meters, 0 in center
* VS(float v), set motor voltage input to `v` volts
* VL(float v), limit motor voltage to <-v; v>
* SB(), start streaming pendulum and cart data
* SE(), end data stream
* T(float target), set current target, in m, limited to 80% on each side
* F(float freq), set sampling frequency
* P(), ping-pong
* KS(byte str_len, char[str_len] name, float value), set variable `name` to value `value`
* KL(), list variables; response: RES, num_values, [str_len, name, value_single]
* QC(), get programmed half rail length
* QV(), get programmed max voltage
*/