Latest version of my quadcopter controller with an LPC1768 and MPU9250.
Currently running on a custom PCB with 30.5 x 30.5mm mounts. There are also 2 PC apps that go with the software; one to set up the PID controller and one to balance the motors and props. If anyone is interested, send me a message and I'll upload them.
main.cpp@3:a49ab9168fb2, 2018-07-10 (annotated)
- Committer:
- Anaesthetix
- Date:
- Tue Jul 10 13:19:53 2018 +0000
- Revision:
- 3:a49ab9168fb2
- Parent:
- 1:53628713ede9
- Child:
- 4:fab65ad01ab4
Added and tested PID controller.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Anaesthetix | 0:0929d3d566cf | 1 | #include "mbed.h" |
Anaesthetix | 0:0929d3d566cf | 2 | #include "MadgwickAHRS.h" |
Anaesthetix | 0:0929d3d566cf | 3 | #include "MAfilter.h" |
Anaesthetix | 0:0929d3d566cf | 4 | #include "MPU9250_SPI.h" |
Anaesthetix | 0:0929d3d566cf | 5 | #include "calccomp.h" |
Anaesthetix | 0:0929d3d566cf | 6 | #include "SerialBuffered.h" |
Anaesthetix | 0:0929d3d566cf | 7 | #include "IAP.h" |
Anaesthetix | 0:0929d3d566cf | 8 | #include "LPfilter.h" |
Anaesthetix | 0:0929d3d566cf | 9 | |
Anaesthetix | 0:0929d3d566cf | 10 | |
Anaesthetix | 0:0929d3d566cf | 11 | #define MEM_SIZE 256 |
Anaesthetix | 0:0929d3d566cf | 12 | #define TARGET_SECTOR 29 // use sector 29 as target sector |
Anaesthetix | 0:0929d3d566cf | 13 | |
Anaesthetix | 0:0929d3d566cf | 14 | DigitalOut led2(LED2); // leds are active low |
Anaesthetix | 0:0929d3d566cf | 15 | DigitalOut ch_sw(p26); |
Anaesthetix | 0:0929d3d566cf | 16 | |
Anaesthetix | 0:0929d3d566cf | 17 | Serial pc(USBTX, USBRX); |
Anaesthetix | 0:0929d3d566cf | 18 | SerialBuffered *pc1 = new SerialBuffered( 100, USBTX, USBRX); |
Anaesthetix | 0:0929d3d566cf | 19 | |
Anaesthetix | 0:0929d3d566cf | 20 | SPI spi(p11, p12, p13); |
Anaesthetix | 0:0929d3d566cf | 21 | mpu9250_spi mpu9250(spi, p14); |
Anaesthetix | 0:0929d3d566cf | 22 | |
Anaesthetix | 0:0929d3d566cf | 23 | Madgwick filter; |
Anaesthetix | 0:0929d3d566cf | 24 | |
Anaesthetix | 0:0929d3d566cf | 25 | Timer timer; |
Anaesthetix | 0:0929d3d566cf | 26 | Timer tijd; |
Anaesthetix | 0:0929d3d566cf | 27 | Timer t; |
Anaesthetix | 0:0929d3d566cf | 28 | Timer print; |
Anaesthetix | 0:0929d3d566cf | 29 | |
Anaesthetix | 0:0929d3d566cf | 30 | MAfilter10 maGX, maGY, maGZ; |
Anaesthetix | 0:0929d3d566cf | 31 | LPfilter2 lp1, lp2, lp3; |
Anaesthetix | 0:0929d3d566cf | 32 | LPfilter2_1 test; |
Anaesthetix | 0:0929d3d566cf | 33 | |
Anaesthetix | 0:0929d3d566cf | 34 | IAP iap; |
Anaesthetix | 0:0929d3d566cf | 35 | |
Anaesthetix | 0:0929d3d566cf | 36 | InterruptIn rx_rud(p5); |
Anaesthetix | 0:0929d3d566cf | 37 | InterruptIn rx_elev(p6); |
Anaesthetix | 0:0929d3d566cf | 38 | InterruptIn rx_thr(p7); |
Anaesthetix | 0:0929d3d566cf | 39 | InterruptIn rx_ail(p8); |
Anaesthetix | 0:0929d3d566cf | 40 | InterruptIn rx_p1(p29); |
Anaesthetix | 0:0929d3d566cf | 41 | InterruptIn rx_p2(p30); |
Anaesthetix | 0:0929d3d566cf | 42 | |
Anaesthetix | 0:0929d3d566cf | 43 | PwmOut motor4(p24); |
Anaesthetix | 0:0929d3d566cf | 44 | PwmOut motor3(p23); |
Anaesthetix | 0:0929d3d566cf | 45 | PwmOut motor1(p22); |
Anaesthetix | 0:0929d3d566cf | 46 | PwmOut motor2(p21); |
Anaesthetix | 0:0929d3d566cf | 47 | |
Anaesthetix | 0:0929d3d566cf | 48 | float Kp = 1.05f; |
Anaesthetix | 0:0929d3d566cf | 49 | float Ki = 0; |
Anaesthetix | 0:0929d3d566cf | 50 | float Kd = 0.45; |
Anaesthetix | 0:0929d3d566cf | 51 | |
Anaesthetix | 0:0929d3d566cf | 52 | Timer trx0, trx1, trx2, trx3, trx4, trx5; |
Anaesthetix | 0:0929d3d566cf | 53 | int rx_data[6] = {0}; |
Anaesthetix | 0:0929d3d566cf | 54 | bool rxd = false; |
Anaesthetix | 0:0929d3d566cf | 55 | |
Anaesthetix | 0:0929d3d566cf | 56 | char mcommand; |
Anaesthetix | 0:0929d3d566cf | 57 | |
Anaesthetix | 0:0929d3d566cf | 58 | void rx0() { |
Anaesthetix | 0:0929d3d566cf | 59 | trx0.start(); |
Anaesthetix | 0:0929d3d566cf | 60 | } |
Anaesthetix | 0:0929d3d566cf | 61 | |
Anaesthetix | 0:0929d3d566cf | 62 | void rx1() { |
Anaesthetix | 0:0929d3d566cf | 63 | trx1.start(); |
Anaesthetix | 0:0929d3d566cf | 64 | trx0.stop(); |
Anaesthetix | 0:0929d3d566cf | 65 | if(trx0.read_us() > 900 && trx0.read_us() < 2200) rx_data[0] = trx0.read_us(); |
Anaesthetix | 0:0929d3d566cf | 66 | trx0.reset(); |
Anaesthetix | 0:0929d3d566cf | 67 | } |
Anaesthetix | 0:0929d3d566cf | 68 | |
Anaesthetix | 0:0929d3d566cf | 69 | void rx2() { |
Anaesthetix | 0:0929d3d566cf | 70 | trx2.start(); |
Anaesthetix | 0:0929d3d566cf | 71 | trx1.stop(); |
Anaesthetix | 0:0929d3d566cf | 72 | if(trx1.read_us() > 900 && trx1.read_us() < 2200) rx_data[1] = trx1.read_us(); |
Anaesthetix | 0:0929d3d566cf | 73 | trx1.reset(); |
Anaesthetix | 0:0929d3d566cf | 74 | } |
Anaesthetix | 0:0929d3d566cf | 75 | |
Anaesthetix | 0:0929d3d566cf | 76 | void rx3() { |
Anaesthetix | 0:0929d3d566cf | 77 | trx3.start(); |
Anaesthetix | 0:0929d3d566cf | 78 | trx2.stop(); |
Anaesthetix | 0:0929d3d566cf | 79 | if(trx2.read_us() > 900 && trx2.read_us() < 2200) rx_data[2] = trx2.read_us(); |
Anaesthetix | 0:0929d3d566cf | 80 | trx2.reset(); |
Anaesthetix | 0:0929d3d566cf | 81 | } |
Anaesthetix | 0:0929d3d566cf | 82 | |
Anaesthetix | 0:0929d3d566cf | 83 | void rx4() { |
Anaesthetix | 0:0929d3d566cf | 84 | trx4.start(); |
Anaesthetix | 0:0929d3d566cf | 85 | trx3.stop(); |
Anaesthetix | 0:0929d3d566cf | 86 | if(trx3.read_us() > 900 && trx3.read_us() < 2200) rx_data[3] = trx3.read_us(); |
Anaesthetix | 0:0929d3d566cf | 87 | trx3.reset(); |
Anaesthetix | 0:0929d3d566cf | 88 | } |
Anaesthetix | 0:0929d3d566cf | 89 | |
Anaesthetix | 0:0929d3d566cf | 90 | void rx5() { |
Anaesthetix | 0:0929d3d566cf | 91 | trx5.start(); |
Anaesthetix | 0:0929d3d566cf | 92 | trx4.stop(); |
Anaesthetix | 0:0929d3d566cf | 93 | if(trx4.read_us() > 900 && trx4.read_us() < 2200) rx_data[4] = trx4.read_us(); |
Anaesthetix | 0:0929d3d566cf | 94 | trx4.reset(); |
Anaesthetix | 0:0929d3d566cf | 95 | } |
Anaesthetix | 0:0929d3d566cf | 96 | |
Anaesthetix | 0:0929d3d566cf | 97 | void rx6() { |
Anaesthetix | 0:0929d3d566cf | 98 | trx5.stop(); |
Anaesthetix | 0:0929d3d566cf | 99 | if(trx5.read_us() > 900 && trx5.read_us() < 2200) rx_data[5] = trx5.read_us(); |
Anaesthetix | 0:0929d3d566cf | 100 | trx5.reset(); |
Anaesthetix | 0:0929d3d566cf | 101 | rxd = true; |
Anaesthetix | 0:0929d3d566cf | 102 | } |
Anaesthetix | 0:0929d3d566cf | 103 | |
Anaesthetix | 0:0929d3d566cf | 104 | |
Anaesthetix | 0:0929d3d566cf | 105 | int main() |
Anaesthetix | 0:0929d3d566cf | 106 | { |
Anaesthetix | 0:0929d3d566cf | 107 | ch_sw = 1; |
Anaesthetix | 0:0929d3d566cf | 108 | led1 = 1; |
Anaesthetix | 0:0929d3d566cf | 109 | led2 = 0; |
Anaesthetix | 0:0929d3d566cf | 110 | pc1->baud(230400); |
Anaesthetix | 0:0929d3d566cf | 111 | pc1->setTimeout(1); |
Anaesthetix | 0:0929d3d566cf | 112 | pc.baud(230400); |
Anaesthetix | 0:0929d3d566cf | 113 | spi.frequency(4000000); |
Anaesthetix | 0:0929d3d566cf | 114 | |
Anaesthetix | 0:0929d3d566cf | 115 | |
Anaesthetix | 0:0929d3d566cf | 116 | //IAP variables |
Anaesthetix | 0:0929d3d566cf | 117 | char* addr = sector_start_adress[TARGET_SECTOR]; |
Anaesthetix | 0:0929d3d566cf | 118 | char mem[ MEM_SIZE ]; // memory, it should be aligned to word boundary |
Anaesthetix | 0:0929d3d566cf | 119 | char PIDsetup = 0; |
Anaesthetix | 0:0929d3d566cf | 120 | int r; |
Anaesthetix | 0:0929d3d566cf | 121 | int tempval; |
Anaesthetix | 0:0929d3d566cf | 122 | |
Anaesthetix | 0:0929d3d566cf | 123 | //IMU variables |
Anaesthetix | 0:0929d3d566cf | 124 | float angles[6] = {0}; |
Anaesthetix | 0:0929d3d566cf | 125 | float time = 0; |
Anaesthetix | 0:0929d3d566cf | 126 | float samplef = 0; |
Anaesthetix | 0:0929d3d566cf | 127 | float gyrodata[3] = {0}; |
Anaesthetix | 0:0929d3d566cf | 128 | float acceldata[3] = {0}; |
Anaesthetix | 0:0929d3d566cf | 129 | float angles_temp[2] = {0}; |
Anaesthetix | 0:0929d3d566cf | 130 | float roll, pitch; |
Anaesthetix | 0:0929d3d566cf | 131 | float tempcomp[6] = {0}; |
Anaesthetix | 0:0929d3d566cf | 132 | float temp = 0; |
Anaesthetix | 0:0929d3d566cf | 133 | float temp2 = 0; |
Anaesthetix | 0:0929d3d566cf | 134 | float temp3 = 0; |
Anaesthetix | 0:0929d3d566cf | 135 | bool exit = true; |
Anaesthetix | 0:0929d3d566cf | 136 | float noise = 0; |
Anaesthetix | 0:0929d3d566cf | 137 | int count = 0; |
Anaesthetix | 0:0929d3d566cf | 138 | |
Anaesthetix | 0:0929d3d566cf | 139 | //Rx variables |
Anaesthetix | 0:0929d3d566cf | 140 | int motor[4] = {0}; |
Anaesthetix | 0:0929d3d566cf | 141 | |
Anaesthetix | 0:0929d3d566cf | 142 | // Init pwm |
Anaesthetix | 0:0929d3d566cf | 143 | motor1.period_ms(10); |
Anaesthetix | 3:a49ab9168fb2 | 144 | motor1.pulsewidth_us(1000); |
Anaesthetix | 3:a49ab9168fb2 | 145 | motor2.pulsewidth_us(1000); |
Anaesthetix | 3:a49ab9168fb2 | 146 | motor3.pulsewidth_us(1000); |
Anaesthetix | 3:a49ab9168fb2 | 147 | motor4.pulsewidth_us(1000); |
Anaesthetix | 0:0929d3d566cf | 148 | |
Anaesthetix | 0:0929d3d566cf | 149 | filter.begin(1500); |
Anaesthetix | 0:0929d3d566cf | 150 | |
Anaesthetix | 0:0929d3d566cf | 151 | pc.putc('c'); |
Anaesthetix | 0:0929d3d566cf | 152 | PIDsetup = pc1->getc(); |
Anaesthetix | 0:0929d3d566cf | 153 | if(PIDsetup == 'c') { |
Anaesthetix | 0:0929d3d566cf | 154 | while(1) { |
Anaesthetix | 0:0929d3d566cf | 155 | PIDsetup = pc1->getc(); |
Anaesthetix | 0:0929d3d566cf | 156 | if(PIDsetup == 'R') { |
Anaesthetix | 0:0929d3d566cf | 157 | for(int i=0; i<6; i++) { |
Anaesthetix | 0:0929d3d566cf | 158 | pc1->putc(addr[i]); |
Anaesthetix | 0:0929d3d566cf | 159 | wait_ms(1); |
Anaesthetix | 0:0929d3d566cf | 160 | } |
Anaesthetix | 0:0929d3d566cf | 161 | } |
Anaesthetix | 0:0929d3d566cf | 162 | |
Anaesthetix | 0:0929d3d566cf | 163 | if(PIDsetup == 'W') { |
Anaesthetix | 0:0929d3d566cf | 164 | for(int i=0; i<6; i++) { |
Anaesthetix | 0:0929d3d566cf | 165 | mem[i] = pc1->getc(); |
Anaesthetix | 0:0929d3d566cf | 166 | } |
Anaesthetix | 0:0929d3d566cf | 167 | iap.prepare( TARGET_SECTOR, TARGET_SECTOR ); |
Anaesthetix | 0:0929d3d566cf | 168 | r = iap.erase( TARGET_SECTOR, TARGET_SECTOR ); |
Anaesthetix | 0:0929d3d566cf | 169 | iap.prepare( TARGET_SECTOR, TARGET_SECTOR ); |
Anaesthetix | 0:0929d3d566cf | 170 | r = iap.write( mem, sector_start_adress[ TARGET_SECTOR ], MEM_SIZE ); |
Anaesthetix | 0:0929d3d566cf | 171 | } |
Anaesthetix | 0:0929d3d566cf | 172 | } |
Anaesthetix | 0:0929d3d566cf | 173 | } |
Anaesthetix | 0:0929d3d566cf | 174 | |
Anaesthetix | 0:0929d3d566cf | 175 | |
Anaesthetix | 0:0929d3d566cf | 176 | if(PIDsetup == 'W') { |
Anaesthetix | 0:0929d3d566cf | 177 | mpu9250.init2(1,BITS_DLPF_CFG_188HZ); |
Anaesthetix | 0:0929d3d566cf | 178 | pc1->setTimeout(0.01); |
Anaesthetix | 0:0929d3d566cf | 179 | |
Anaesthetix | 0:0929d3d566cf | 180 | rx_rud.rise(&rx0); |
Anaesthetix | 0:0929d3d566cf | 181 | rx_elev.rise(&rx1); |
Anaesthetix | 0:0929d3d566cf | 182 | rx_thr.rise(&rx2); |
Anaesthetix | 0:0929d3d566cf | 183 | rx_ail.rise(&rx3); |
Anaesthetix | 0:0929d3d566cf | 184 | rx_p1.rise(&rx4); |
Anaesthetix | 0:0929d3d566cf | 185 | rx_p2.rise(&rx5); |
Anaesthetix | 0:0929d3d566cf | 186 | rx_p2.fall(&rx6); |
Anaesthetix | 0:0929d3d566cf | 187 | mcommand = 'a'; |
Anaesthetix | 0:0929d3d566cf | 188 | |
Anaesthetix | 0:0929d3d566cf | 189 | while(exit) { |
Anaesthetix | 0:0929d3d566cf | 190 | wait_ms(1); |
Anaesthetix | 0:0929d3d566cf | 191 | if(mcommand == '5') { |
Anaesthetix | 0:0929d3d566cf | 192 | motor1.pulsewidth_us(rx_data[2] - 20); |
Anaesthetix | 0:0929d3d566cf | 193 | motor2.pulsewidth_us(1000); |
Anaesthetix | 0:0929d3d566cf | 194 | motor3.pulsewidth_us(1000); |
Anaesthetix | 0:0929d3d566cf | 195 | motor4.pulsewidth_us(1000); |
Anaesthetix | 0:0929d3d566cf | 196 | } |
Anaesthetix | 0:0929d3d566cf | 197 | else if(mcommand == '6') { |
Anaesthetix | 0:0929d3d566cf | 198 | motor1.pulsewidth_us(1000); |
Anaesthetix | 0:0929d3d566cf | 199 | motor2.pulsewidth_us(rx_data[2] - 20); |
Anaesthetix | 0:0929d3d566cf | 200 | motor3.pulsewidth_us(1000); |
Anaesthetix | 0:0929d3d566cf | 201 | motor4.pulsewidth_us(1000); |
Anaesthetix | 0:0929d3d566cf | 202 | } |
Anaesthetix | 0:0929d3d566cf | 203 | else if(mcommand == '3') { |
Anaesthetix | 0:0929d3d566cf | 204 | motor1.pulsewidth_us(1000); |
Anaesthetix | 0:0929d3d566cf | 205 | motor2.pulsewidth_us(1000); |
Anaesthetix | 0:0929d3d566cf | 206 | motor3.pulsewidth_us(rx_data[2] - 20); |
Anaesthetix | 0:0929d3d566cf | 207 | motor4.pulsewidth_us(1000); |
Anaesthetix | 0:0929d3d566cf | 208 | } |
Anaesthetix | 0:0929d3d566cf | 209 | else if(mcommand == '4') { |
Anaesthetix | 0:0929d3d566cf | 210 | motor1.pulsewidth_us(1000); |
Anaesthetix | 0:0929d3d566cf | 211 | motor2.pulsewidth_us(1000); |
Anaesthetix | 0:0929d3d566cf | 212 | motor3.pulsewidth_us(1000); |
Anaesthetix | 0:0929d3d566cf | 213 | motor4.pulsewidth_us(rx_data[2] - 20); |
Anaesthetix | 0:0929d3d566cf | 214 | } |
Anaesthetix | 0:0929d3d566cf | 215 | if(mcommand == 'E') exit = 0; |
Anaesthetix | 0:0929d3d566cf | 216 | |
Anaesthetix | 0:0929d3d566cf | 217 | if(rxd) { |
Anaesthetix | 0:0929d3d566cf | 218 | led2 = !led2; |
Anaesthetix | 0:0929d3d566cf | 219 | rxd = false; |
Anaesthetix | 0:0929d3d566cf | 220 | } |
Anaesthetix | 0:0929d3d566cf | 221 | |
Anaesthetix | 0:0929d3d566cf | 222 | mpu9250.read_all(); |
Anaesthetix | 0:0929d3d566cf | 223 | if(mpu9250.accelerometer_data[0] >= 0) noise = noise*0.99 + 0.01*mpu9250.accelerometer_data[0]; |
Anaesthetix | 0:0929d3d566cf | 224 | |
Anaesthetix | 0:0929d3d566cf | 225 | if(count>100) { |
Anaesthetix | 0:0929d3d566cf | 226 | count = 0; |
Anaesthetix | 0:0929d3d566cf | 227 | pc.printf("%.2f\n", noise); |
Anaesthetix | 0:0929d3d566cf | 228 | mcommand = pc1->getc(); |
Anaesthetix | 0:0929d3d566cf | 229 | } |
Anaesthetix | 0:0929d3d566cf | 230 | count++; |
Anaesthetix | 0:0929d3d566cf | 231 | } |
Anaesthetix | 0:0929d3d566cf | 232 | } |
Anaesthetix | 0:0929d3d566cf | 233 | |
Anaesthetix | 0:0929d3d566cf | 234 | tempval = addr[0]; |
Anaesthetix | 0:0929d3d566cf | 235 | tempval = tempval + (addr[1] << 8); |
Anaesthetix | 0:0929d3d566cf | 236 | Kp = ((float)tempval) / 100; |
Anaesthetix | 0:0929d3d566cf | 237 | tempval = addr[2]; |
Anaesthetix | 0:0929d3d566cf | 238 | tempval = tempval + (addr[3] << 8); |
Anaesthetix | 0:0929d3d566cf | 239 | Ki = ((float)tempval) / 100; |
Anaesthetix | 0:0929d3d566cf | 240 | tempval = addr[4]; |
Anaesthetix | 0:0929d3d566cf | 241 | tempval = tempval + (addr[5] << 8); |
Anaesthetix | 0:0929d3d566cf | 242 | Kd = ((float)tempval) / 100; |
Anaesthetix | 0:0929d3d566cf | 243 | |
Anaesthetix | 0:0929d3d566cf | 244 | mpu9250.init(1,BITS_DLPF_CFG_188HZ); |
Anaesthetix | 0:0929d3d566cf | 245 | |
Anaesthetix | 0:0929d3d566cf | 246 | pc.printf("%.2f %.2f %.2f\r\n", Kp, Ki, Kd); |
Anaesthetix | 0:0929d3d566cf | 247 | |
Anaesthetix | 0:0929d3d566cf | 248 | rx_rud.rise(&rx0); |
Anaesthetix | 0:0929d3d566cf | 249 | rx_elev.rise(&rx1); |
Anaesthetix | 0:0929d3d566cf | 250 | rx_thr.rise(&rx2); |
Anaesthetix | 0:0929d3d566cf | 251 | rx_ail.rise(&rx3); |
Anaesthetix | 0:0929d3d566cf | 252 | rx_p1.rise(&rx4); |
Anaesthetix | 0:0929d3d566cf | 253 | rx_p2.rise(&rx5); |
Anaesthetix | 0:0929d3d566cf | 254 | rx_p2.fall(&rx6); |
Anaesthetix | 0:0929d3d566cf | 255 | |
Anaesthetix | 0:0929d3d566cf | 256 | t.start(); |
Anaesthetix | 0:0929d3d566cf | 257 | |
Anaesthetix | 0:0929d3d566cf | 258 | while(1) { |
Anaesthetix | 0:0929d3d566cf | 259 | print.start(); |
Anaesthetix | 0:0929d3d566cf | 260 | t.stop(); |
Anaesthetix | 0:0929d3d566cf | 261 | time = (float)t.read(); |
Anaesthetix | 0:0929d3d566cf | 262 | t.reset(); |
Anaesthetix | 0:0929d3d566cf | 263 | t.start(); |
Anaesthetix | 0:0929d3d566cf | 264 | filter.invSampleFreq = time; |
Anaesthetix | 0:0929d3d566cf | 265 | samplef = 1/time; |
Anaesthetix | 0:0929d3d566cf | 266 | |
Anaesthetix | 0:0929d3d566cf | 267 | mpu9250.read_all(); |
Anaesthetix | 0:0929d3d566cf | 268 | if(mpu9250.Temperature < 6.0f) temp = 6.0f; |
Anaesthetix | 0:0929d3d566cf | 269 | else if(mpu9250.Temperature > 60.0f) temp = 60.0f; |
Anaesthetix | 0:0929d3d566cf | 270 | else temp = mpu9250.Temperature; |
Anaesthetix | 0:0929d3d566cf | 271 | temp2 = temp*temp; |
Anaesthetix | 0:0929d3d566cf | 272 | temp3 = temp2*temp; |
Anaesthetix | 0:0929d3d566cf | 273 | |
Anaesthetix | 0:0929d3d566cf | 274 | // Temperature compensation |
Anaesthetix | 0:0929d3d566cf | 275 | tempcomp[0] = -1.77e-6*temp2 + 0.000233*temp + 0.02179; |
Anaesthetix | 0:0929d3d566cf | 276 | tempcomp[1] = 0.0005727*temp - 0.01488; |
Anaesthetix | 0:0929d3d566cf | 277 | tempcomp[2] = -2.181e-7*temp3 + 1.754e-5*temp2 - 0.0004955*temp; |
Anaesthetix | 0:0929d3d566cf | 278 | tempcomp[3] = -0.0002814*temp2 + 0.005545*temp - 3.018; |
Anaesthetix | 0:0929d3d566cf | 279 | tempcomp[4] = -3.011e-5*temp3 + 0.002823*temp2 - 0.1073*temp + 3.618; |
Anaesthetix | 0:0929d3d566cf | 280 | tempcomp[5] = 9.364e-5*temp2 + 0.009138*temp - 0.8939; |
Anaesthetix | 0:0929d3d566cf | 281 | |
Anaesthetix | 0:0929d3d566cf | 282 | // Low pass filters on accelerometer data (calculated with the help of Matlab's FDAtool) |
Anaesthetix | 0:0929d3d566cf | 283 | acceldata[0] = lp1.run(mpu9250.accelerometer_data[0] - tempcomp[0]); |
Anaesthetix | 0:0929d3d566cf | 284 | acceldata[1] = lp2.run(mpu9250.accelerometer_data[1] - tempcomp[1]); |
Anaesthetix | 0:0929d3d566cf | 285 | acceldata[2] = lp3.run((mpu9250.accelerometer_data[2] - tempcomp[2])*-1); |
Anaesthetix | 0:0929d3d566cf | 286 | |
Anaesthetix | 0:0929d3d566cf | 287 | // 10-term moving average to remove some noise |
Anaesthetix | 0:0929d3d566cf | 288 | gyrodata[0] = maGX.run((mpu9250.gyroscope_data[0] - tempcomp[3])*-1); |
Anaesthetix | 0:0929d3d566cf | 289 | gyrodata[1] = maGY.run((mpu9250.gyroscope_data[1] - tempcomp[4])*-1); |
Anaesthetix | 0:0929d3d566cf | 290 | gyrodata[2] = maGZ.run((mpu9250.gyroscope_data[2] - tempcomp[5])*-1); |
Anaesthetix | 0:0929d3d566cf | 291 | |
Anaesthetix | 0:0929d3d566cf | 292 | // Madgwick's quaternion algorithm |
Anaesthetix | 0:0929d3d566cf | 293 | filter.updateIMU(gyrodata[0], gyrodata[1], gyrodata[2], acceldata[0], |
Anaesthetix | 0:0929d3d566cf | 294 | acceldata[1], acceldata[2]); |
Anaesthetix | 0:0929d3d566cf | 295 | |
Anaesthetix | 0:0929d3d566cf | 296 | roll = filter.getRoll(); |
Anaesthetix | 0:0929d3d566cf | 297 | pitch = filter.getPitch(); |
Anaesthetix | 0:0929d3d566cf | 298 | |
Anaesthetix | 0:0929d3d566cf | 299 | angles[3] = gyrodata[1]; |
Anaesthetix | 0:0929d3d566cf | 300 | angles[4] = gyrodata[0]; |
Anaesthetix | 0:0929d3d566cf | 301 | angles[5] = gyrodata[2]; |
Anaesthetix | 0:0929d3d566cf | 302 | |
Anaesthetix | 0:0929d3d566cf | 303 | //Simple first order complementary filter -> TODO: CHECK STEP RESPONSE |
Anaesthetix | 0:0929d3d566cf | 304 | angles[0] = 0.99f*(angles[0] + (gyrodata[1]*time)) + 0.01f*pitch; |
Anaesthetix | 0:0929d3d566cf | 305 | angles[1] = 0.99f*(angles[1] + (gyrodata[0]*time)) + 0.01f*roll; |
Anaesthetix | 0:0929d3d566cf | 306 | |
Anaesthetix | 0:0929d3d566cf | 307 | // If [VAR] is NaN use previous value |
Anaesthetix | 0:0929d3d566cf | 308 | if(angles[0] != angles[0]) angles[0] = angles_temp[0]; |
Anaesthetix | 0:0929d3d566cf | 309 | if(angles[1] != angles[1]) angles[1] = angles_temp[1]; |
Anaesthetix | 0:0929d3d566cf | 310 | if(angles[0] == angles[0]) angles_temp[0] = angles[0]; |
Anaesthetix | 0:0929d3d566cf | 311 | if(angles[1] == angles[1]) angles_temp[1] = angles[1]; |
Anaesthetix | 0:0929d3d566cf | 312 | |
Anaesthetix | 0:0929d3d566cf | 313 | tijd.stop(); |
Anaesthetix | 0:0929d3d566cf | 314 | tijd.reset(); |
Anaesthetix | 0:0929d3d566cf | 315 | tijd.start(); |
Anaesthetix | 0:0929d3d566cf | 316 | |
Anaesthetix | 0:0929d3d566cf | 317 | /* |
Anaesthetix | 0:0929d3d566cf | 318 | if(print.read_ms() > 100) { |
Anaesthetix | 0:0929d3d566cf | 319 | print.stop(); |
Anaesthetix | 0:0929d3d566cf | 320 | print.reset(); |
Anaesthetix | 0:0929d3d566cf | 321 | print.start(); |
Anaesthetix | 3:a49ab9168fb2 | 322 | //led2 = !led2; |
Anaesthetix | 0:0929d3d566cf | 323 | // pc.printf("X: %.2f Y: %.2f %.0f\r\n", angles[0], angles[1], samplef); |
Anaesthetix | 0:0929d3d566cf | 324 | pc.printf("%.2f %.0f\r\n", angles[1], samplef); |
Anaesthetix | 0:0929d3d566cf | 325 | } |
Anaesthetix | 0:0929d3d566cf | 326 | */ |
Anaesthetix | 0:0929d3d566cf | 327 | pc.printf("%.2f %.0f\r\n", angles[0], samplef); |
Anaesthetix | 0:0929d3d566cf | 328 | if(rxd) { |
Anaesthetix | 0:0929d3d566cf | 329 | led2 = !led2; |
Anaesthetix | 0:0929d3d566cf | 330 | rxd = false; |
Anaesthetix | 3:a49ab9168fb2 | 331 | // pc.printf("%d %d %d %d\r\n", motor[0], motor[1], motor[2], motor[3]); |
Anaesthetix | 0:0929d3d566cf | 332 | } |
Anaesthetix | 0:0929d3d566cf | 333 | |
Anaesthetix | 0:0929d3d566cf | 334 | if(rx_data[5] > 1650) ch_sw = 0; |
Anaesthetix | 0:0929d3d566cf | 335 | else ch_sw = 1; |
Anaesthetix | 0:0929d3d566cf | 336 | |
Anaesthetix | 0:0929d3d566cf | 337 | calccomp(rx_data, angles, motor); |
Anaesthetix | 0:0929d3d566cf | 338 | |
Anaesthetix | 0:0929d3d566cf | 339 | motor1.pulsewidth_us(motor[0]); |
Anaesthetix | 0:0929d3d566cf | 340 | motor2.pulsewidth_us(motor[1]); |
Anaesthetix | 0:0929d3d566cf | 341 | motor3.pulsewidth_us(motor[2]); |
Anaesthetix | 0:0929d3d566cf | 342 | motor4.pulsewidth_us(motor[3]); |
Anaesthetix | 0:0929d3d566cf | 343 | } |
Anaesthetix | 0:0929d3d566cf | 344 | } |