-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
269 lines (205 loc) · 10.9 KB
/
Copy pathmain.c
File metadata and controls
269 lines (205 loc) · 10.9 KB
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
#include<delay/delay.h>
#include<d_serializer/d_serializer.h>
#include<regs/rcc.h>
#include<regs/gpio.h>
#include<sysclock/sysclock.h>
#include<uart/uart.h>
#include<i2c/i2c.h>
#include<backup_data/backup_data.h>
#include<pid/pid.h>
#include<gy86/gy86.h>
#include<bldc/quad_bldc.h>
#include<rc_receiver/rc_receiver.h>
//#define STABILIZE_MODE
#define map(val, a_min, a_max, b_min, b_max) (b_min) + ((((float)(val)) - (a_min)) * ((b_max) - (b_min))) / ((a_max) - (a_min))
#define GYRO_ACCL_MIX 0.98
#define LOOP_EVERY_MICROS 2500
#define THROTTLE_MIN_VALUE 0.0
#define THROTTLE_PID_ACTIVATE 200.0
#define THROTTLE_MAX_VALUE 800.0
#ifdef STABILIZE_MODE
#define ATTITUDE_INPUT_LIMIT 30.0
#else // it is a rate controlled quadcopter
#define ATTITUDE_INPUT_LIMIT 120.0
#endif
#define ANGULAR_RATE_INPUT_LIMIT 120.0
#define MOTOR_MIN_PWM 110.0
#define MOTOR_MAX_PWM 990.0
//#define CALIBRATE_ESC_ON_START_UP
//#define DEBUG_OVER_UART
//#define PID_TO_TUNE
void main(void)
{
// setup clock to run at 72 MHz
change_sys_clock_source(HSE, 8000000);
setup_pll_module(HSE, 72000000);
change_sys_clock_source(PLL, 72000000);
// to enable port c GPIO
RCC->RCC_APB2ENR |= (1<<4);
// GPIO Port C pin 13 setup as output, it is the blink led on the bluepill board
GPIOC->GPIO_CRH &= 0xFF0FFFFF;
GPIOC->GPIO_CRH |= 0x00200000;
GPIOC->GPIO_ODR |= (1 << 13);
// init rtc clock
rtc_init();
// setup communications
i2c_init();
uart_init(2000000);
// initialize rc receiver
init_rc_receiver();
// initialize motors
init_bldc();
#if defined CALIBRATE_ESC_ON_START_UP
set_motors(1000, 1000, 1000, 1000);
delay_for_ms(3000);
#endif
set_motors(0, 0, 0, 0);
delay_for_ms(1000);
#if defined DEBUG_OVER_UART
if(i2c_detect(0x68))
uart_write_blocking("MPU detected\n", 13);
else
uart_write_blocking("MPU not detected\n", 17);
#endif
// initialize all necessary sensors
const MPUdatascaled* mpuInit = mpu_init();
// initialize MPUdatascaled variable used inside the loop
MPUdatascaled mpuData = *mpuInit;
// initialize pid variables
// angular rate control pids, these cause differential motor corrections to attain required angular rates along local axis
pid_state x_ang_rate_pid; pid_init(&x_ang_rate_pid, 2.7, 0.015, 0.0, 400);
pid_state y_ang_rate_pid; pid_init(&y_ang_rate_pid, 2.7, 0.015, 0.0, 400);
pid_state z_ang_rate_pid; pid_init(&z_ang_rate_pid, 5.4, 0.030, 0.0, 400);
// altitude rate pid will mainly work to make 0 rate of change of altitude
pid_state z_alt_rate_pid; pid_init(&z_alt_rate_pid, 0, 0, 0, 400);
// flyable values
/*
pid_state x_ang_rate_pid; pid_init(&x_ang_rate_pid, 2.4, 0.017, 0.000030, 400);
pid_state y_ang_rate_pid; pid_init(&y_ang_rate_pid, 2.4, 0.017, 0.000030, 400);
pid_state z_ang_rate_pid; pid_init(&z_ang_rate_pid, 5.2, 0.037, 0.000065, 400);
*/
uint64_t begin_micros = get_now_micros() - LOOP_EVERY_MICROS;
uint64_t print_iter = 0;
char print_str[256];
float abs_roll_init = atanf( mpuInit->accl.yj/mpuInit->accl.zk) * 180 / M_PI;
float abs_pitch_init = atanf(-mpuInit->accl.xi/mpuInit->accl.zk) * 180 / M_PI;
float abs_roll = abs_roll_init;
float abs_pitch = abs_pitch_init;
float alt_rate = 0;
while(1)
{
float time_delta_in_seconds = ((float)(get_now_micros() - begin_micros))/1000000.0;
begin_micros = get_now_micros();
GPIOC->GPIO_ODR ^= (1 << 13);
get_scaled_MPUdata(&mpuData);
abs_roll = (abs_roll + mpuData.gyro.xi * time_delta_in_seconds) * (GYRO_ACCL_MIX)
+ (atanf( mpuData.accl.yj/mpuData.accl.zk) * 180/M_PI) * (1.0 - GYRO_ACCL_MIX);
abs_pitch = (abs_pitch + mpuData.gyro.yj * time_delta_in_seconds) * (GYRO_ACCL_MIX)
+ (atanf(-mpuData.accl.xi/mpuData.accl.zk) * 180/M_PI) * (1.0 - GYRO_ACCL_MIX);
uint32_t chan_ret[6];
int is_rc_active = get_rc_channels(chan_ret);
float throttle = map(chan_ret[3], 0.0, 1000.0, THROTTLE_MIN_VALUE, THROTTLE_MAX_VALUE);
float x_rc_req = map(chan_ret[5], 0.0, 1000.0, -ATTITUDE_INPUT_LIMIT, ATTITUDE_INPUT_LIMIT);
float y_rc_req = map(chan_ret[4], 0.0, 1000.0, -ATTITUDE_INPUT_LIMIT, ATTITUDE_INPUT_LIMIT);
float z_rc_req = map(chan_ret[2], 0.0, 1000.0, ANGULAR_RATE_INPUT_LIMIT, -ANGULAR_RATE_INPUT_LIMIT);
chan_ret[1] = (chan_ret[1] < 3) ? 0 : chan_ret[1];
float aux1 = map(chan_ret[1], 0.0, 1000.0, 0.0, 10.0);
chan_ret[0] = (chan_ret[0] < 3) ? 0 : chan_ret[0];
float aux2 = map(chan_ret[0], 0.0, 1000.0, 0.0, 40.0);
#if defined PID_TO_TUNE
pid_update_constants(&x_ang_rate_pid, aux1, 0.015, aux2);
pid_update_constants(&y_ang_rate_pid, aux1, 0.015, aux2);
//pid_update_constants(&z_ang_rate_pid, 7.0 + aux1, aux2, 0);
#endif
float motor_LF = throttle, motor_RF = throttle, motor_LB = throttle, motor_RB = throttle;
#if defined STABILIZE_MODE
float x_rate_req = aux1 * (x_rc_req - (abs_roll - abs_roll_init));
float y_rate_req = aux1 * (y_rc_req - (abs_pitch - abs_pitch_init));
#else
float x_rate_req = x_rc_req;
float y_rate_req = y_rc_req;
#endif
float z_rate_req = z_rc_req;
if(throttle < THROTTLE_PID_ACTIVATE)
{
pid_reinit(&x_ang_rate_pid);
pid_reinit(&y_ang_rate_pid);
pid_reinit(&z_ang_rate_pid);
pid_reinit(&z_alt_rate_pid);
}
else
{
float x_motor_corr = pid_update(&x_ang_rate_pid, mpuData.gyro.xi, x_rate_req);
float y_motor_corr = pid_update(&y_ang_rate_pid, mpuData.gyro.yj, y_rate_req);
float z_motor_corr = pid_update(&z_ang_rate_pid, mpuData.gyro.zk, z_rate_req);
// we always strive to make 0 altitude rate
float comm_motor_corr = pid_update(&z_alt_rate_pid, 0, 0);
motor_LF += (comm_motor_corr + x_motor_corr - y_motor_corr - z_motor_corr);
motor_RF += (comm_motor_corr - x_motor_corr - y_motor_corr + z_motor_corr);
motor_LB += (comm_motor_corr + x_motor_corr + y_motor_corr + z_motor_corr);
motor_RB += (comm_motor_corr - x_motor_corr + y_motor_corr - z_motor_corr);
if(motor_LF < MOTOR_MIN_PWM) motor_LF = MOTOR_MIN_PWM;
if(motor_RF < MOTOR_MIN_PWM) motor_RF = MOTOR_MIN_PWM;
if(motor_LB < MOTOR_MIN_PWM) motor_LB = MOTOR_MIN_PWM;
if(motor_RB < MOTOR_MIN_PWM) motor_RB = MOTOR_MIN_PWM;
if(motor_LF > MOTOR_MAX_PWM) motor_LF = MOTOR_MAX_PWM;
if(motor_RF > MOTOR_MAX_PWM) motor_RF = MOTOR_MAX_PWM;
if(motor_LB > MOTOR_MAX_PWM) motor_LB = MOTOR_MAX_PWM;
if(motor_RB > MOTOR_MAX_PWM) motor_RB = MOTOR_MAX_PWM;
}
set_motors(motor_LF, motor_RF, motor_LB, motor_RB);
#if defined DEBUG_OVER_UART
if(print_iter == 100)
{
print_iter = 0;
char* end_ps = print_str;
//end_ps = stringify_integer(end_ps, is_rc_active); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_integer(end_ps, motor_LF); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_integer(end_ps, motor_RF); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_integer(end_ps, motor_LB); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_integer(end_ps, motor_RB); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
end_ps = stringify_float(end_ps, aux1); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
end_ps = stringify_float(end_ps, aux2); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_integer(end_ps, chan_ret[0]); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_integer(end_ps, chan_ret[1]); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_integer(end_ps, chan_ret[2]); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_integer(end_ps, chan_ret[3]); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_integer(end_ps, chan_ret[4]); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_integer(end_ps, chan_ret[5]); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, x_ang_rate_pid.constants.Kp); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, x_ang_rate_pid.constants.Ki); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, x_ang_rate_pid.constants.Kd); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, y_ang_rate_pid.constants.Kp); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, y_ang_rate_pid.constants.Ki); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, y_ang_rate_pid.constants.Kd); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, z_ang_rate_pid.constants.Kp); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, z_ang_rate_pid.constants.Ki); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, z_ang_rate_pid.constants.Kd); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, mpuData.gyro.xi); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, mpuData.gyro.yj); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, mpuData.gyro.zk); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, mpuInit->gyro.xi); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, mpuInit->gyro.yj); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, mpuInit->gyro.zk); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, mpuData.accl.xi); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, mpuData.accl.yj); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, mpuData.accl.zk); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, mpuInit->accl.xi); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, mpuInit->accl.yj); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, mpuInit->accl.zk); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, abs_roll); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, abs_pitch); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, x_rc_req); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, y_rc_req); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, x_rate_req); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, y_rate_req); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
//end_ps = stringify_float(end_ps, time_delta_in_seconds); *end_ps = ' '; end_ps++; *end_ps = '\t'; end_ps++;
*end_ps = '\n'; end_ps++;
uart_write_through_dma(print_str, end_ps - print_str);
}
else
print_iter++;
#endif
delay_until_us(begin_micros + LOOP_EVERY_MICROS);
}
}