forked from lsantens/S-Blimp-XIAO-S3-DC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpinning_Blimp_Control_v1.ino
313 lines (259 loc) · 7.66 KB
/
Spinning_Blimp_Control_v1.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
#include <crazyflieComplementary.h>
#include "WiFi.h"
#include "AsyncUDP.h"
#include <ESP32Servo.h>
#define THRUST1 D0
#define THRUST2 D1
// min and max high signal of thruster PWMs
int minUs = 1100;
int maxUs = 1900;
// Wi-Fi access details
const char * ssid = "AIRLab-BigLab";
const char * password = "Airlabrocks2022";
// using servo lib to control brushless motors
Servo thrust1;
Servo thrust2;
// c.c. Edward
SensFusion sensorSuite;
AsyncUDP udp;
float joy_data[8] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
volatile bool joy_ready = false;
volatile unsigned long time_now, time_loop;
// P.I.D. Values
float roll, pitch, yaw;
float rollrate, pitchrate, yawrate;
float estimatedZ, velocityZ, groundZ;
float abz = 0.0;
float kpz = 0.01*3.0; // N/meter
float kiz = 0.02;
float kdz = 0.2*0.7;
float kpx = 0.035;
float kdx = 0.2;
float kptz = 0.3;
float kdtz = -0.025;
float kptx = 0.01;
float kdtx = 0.01;
float lx = 0.25;
float m1 = 0.0;
float m2 = 0.0;
float heading = 0.0;
float massthrust = 0.15;
long dt, last_time, time_nw;
void setup() {
Serial.begin(9600);
delay(500);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed");
while (1) {
delay(1000);
}
}
// Allocate timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
// Standard 50hz PWM for thrusters
thrust1.setPeriodHertz(50);
thrust2.setPeriodHertz(50);
thrust1.attach(THRUST1, minUs, maxUs);
thrust2.attach(THRUST2, minUs, maxUs);
delay(100);
// ESC arm
escarm();
// Access sensor suite c.c. Edward
sensorSuite.initSensors();
sensorSuite.updateKp(5, -1, 0.3); // 20, -1, 0
groundZ = sensorSuite.returnZ();
// sensorSuite.recordData();
// magnetometer calibration
float transformationMatrix[3][3] = {
{1.0f, 9.693f, 0.6187f},
{9.6624f, -0.6822f, 0.3864f},
{-0.4155f, 0.6628f, -10.7386f}
};
float offsets[3] = {11.98f, 7.01f, 21.77f};
sensorSuite.enterTransform(offsets, transformationMatrix);
getSensorValues();
time_now = millis();
// time_loop = millis();
if(udp.listen(1234)) {
Serial.print("UDP Listening on IP: ");
Serial.println(WiFi.localIP());
// setup callback functions of the udp
udp.onPacket([](AsyncUDPPacket packet) {
joy_ready = false;
time_now = millis();
unsigned char *buffer = packet.data();
unpack_joystick(joy_data, buffer);
joy_ready = true;
//reply to the server
// packet.printf("Got %u bytes of data", packet.length());
});
}
// Yaw heading setup
heading = sensorSuite.getYaw();
}
void loop() {
//gyro, acc, mag, euler, z
float cfx, cfy, cfz, ctx, cty, ctz;
// Runs the sensor fusion loop
sensorSuite.sensfusionLoop(false, 5);
if (joy_data[7] != 1){
// Serial.println("Initialization") //debug;
thrust1.writeMicroseconds(minUs);
thrust2.writeMicroseconds(minUs);
// Debug joystick input
// thrust1.writeMicroseconds((int) (1000 + joy_data[0]));
// thrust2.writeMicroseconds((int) (1000 + joy_data[0]));
} else if (joy_ready && millis() - time_now <= 1000){ //&& millis() - time_loop > 50) {kdz
// Call sensor suite to update 10-DOF values
getSensorValues();
getControllerInputs(&cfx, &cfy, &cfz, &ctx, &cty, &ctz, &abz);
addFeedback(&cfx, &cfy, &cfz, &ctx, &cty, &ctz, abz);
controlOutputs(cfx, cfy, cfz, ctx, cty, ctz);
// Convert motor input to 1000-2000 Us values
int m1us = (minUs + (maxUs - minUs)*m1*3.33*(0.5));
int m2us = (minUs + (maxUs - minUs)*m2*3.33*(0.5));
// Write motor thrust values to the pins
m1us = clamp(m1us, 1200, 1800);
m2us = clamp(m2us, 1200, 1800);
// m1us = clamp(m1us, 1125, 1550);
// m2us = clamp(m2us, 1125, 1550);
thrust1.write((int) m1us);
thrust2.write((int) m2us);
// Else statement for if the Wi-Fi signal is lost
} else {
thrust1.writeMicroseconds((int) minUs);
thrust2.writeMicroseconds((int) minUs);
}
}
//Enter arming sequence for ESC
void escarm(){
// ESC arming sequence for BLHeli S
thrust1.writeMicroseconds(1000);
delay(10);
thrust2.writeMicroseconds(1000);
delay(10);
// Sweep up
for(int i=1100; i<1500; i++) {
thrust1.writeMicroseconds(i);
delay(5);
thrust2.writeMicroseconds(i);
delay(5);
}
// Sweep down
for(int i=1500; i<1100; i--) {
thrust1.writeMicroseconds(i);
delay(5);
thrust2.writeMicroseconds(i);
delay(5);
}
// Back to minimum value
thrust1.writeMicroseconds(1000);
delay(10);
thrust2.writeMicroseconds(1000);
delay(10);
}
void getSensorValues(){
//all in radians or meters or meters per second
// roll = sensorSuite.getRoll();
// pitch = -1*sensorSuite.getPitch();
yaw = sensorSuite.getYaw();
// rollrate = sensorSuite.getRollRate();
// pitchrate = sensorSuite.getPitchRate();
yawrate = sensorSuite.getYawRate();
estimatedZ = sensorSuite.returnZ();
velocityZ = sensorSuite.returnVZ();
}
float valtz = 0;
void getControllerInputs(float *fx, float *fy, float *fz, float *tx, float *ty, float *tz, float *abz){
if (false) {
*fx = 0;//joy_data[0];
*fy = 0;//joy_data[1];
*fz = 0;//joy_data[2];
*tx = 0;//joy_data[3];
*ty = 0;//joy_data[4];
*tz = 0;//joy_data[5];
*abz = 0;//joy_data[6];
if (valtz > 1){
valtz = -1;
} else {
valtz += .01;
}
} else{
*fx = joy_data[0];
*fy = joy_data[1];
*fz = joy_data[2];
*tx = joy_data[3];
*ty = joy_data[4];
*tz = joy_data[5];
*abz = joy_data[6];
}
}
void addFeedback(float *fx, float *fy, float *fz, float *tx, float *ty, float *tz, float abz){
// *fz = 0;
float err = estimatedZ-groundZ;
delta_time();
float int_err =+ dt * err;
// *fz = (*fz - (estimatedZ-groundZ))*kpz - (int_err * kiz) - (velocityZ)*kdz + abz;//*fz = *fz + abz;//
*fz = (*fz - (estimatedZ-groundZ))*kpz - (velocityZ)*kdz + abz;//*fz = *fz + abz;//
Serial.println(int_err*kiz);
}
long delta_time(){
time_nw = millis();
dt = (time_nw - last_time);
last_time = time_nw;
return dt;
}
float clamp(float in, float min, float max){
if (in< min){
return min;
} else if (in > max){
return max;
} else {
return in;
}
}
void controlOutputs(float ifx, float ify, float ifz, float itx, float ity, float itz) {
// Heading added to the control loop
heading = heading + radians(ity * 180);
// Serial.println(yaw);
// Convert joystick input to theta and magnitude for cyclic input
float joytheta = atan2(ify,-ifx) + PI;
float joymag = sqrt(pow(ifx,2) + pow(ify,2));
// Cyclic pitch input
// float lhs = joymag*(coscos - sincos);
// float rhs = joymag*(-sincos - coscos);
float pitch = joymag * sin(joytheta) * cos(yaw);
float roll = joymag * cos(joytheta) * sin(yaw);
// float pitch = joymag * sin(joytheta) * cos(yaw + heading);
// float roll = joymag * cos(joytheta) * sin(yaw + heading);
// TODO: bring back x-y feedback
// Mass Thrust it a proportional debug gain
float mt = massthrust;
float f1 = ifz - (mt * (pitch + roll)); // LHS motor
float f2 = ifz + (mt * (pitch + roll)); // RHS motor
// Clamp to ensure motor doesn't stop spinning at minimum
// and motor doesn't draw too much current
// m1 = clamp(f1, 0, 0.25);
// m2 = clamp(f2, 0, 0.25);
m1 = clamp(f1, 0.01, 0.15);
m2 = clamp(f2, 0.01, 0.15);
}
// Having questions? Ask Jiawei!
void unpack_joystick(float *dat, const unsigned char *buffer) {
int num_floats = 8;
int num_bytes = 4;
int i, j;
for(i = 0; i < num_floats; i++) {
char temp[4] = {0, 0, 0, 0};
for(j = 0; j < num_bytes; j++) {
temp[j] = buffer[num_bytes*i + j];
}
dat[i] = *((float*) temp);
}
;
}