![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ESE350 project, Spring 2016, University of Pennsylvania
Dependencies: Adafruit9-DOf Receiver mbed-rtos mbed
quadcopter.cpp@35:35997980a8ba, 2016-04-26 (annotated)
- Committer:
- ivo_david_michelle
- Date:
- Tue Apr 26 18:50:55 2016 +0000
- Revision:
- 35:35997980a8ba
- Parent:
- 34:eaea0ae92dfa
- Child:
- 36:40b134328376
the code for the demo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ivo_david_michelle | 6:6f3ffd97d808 | 1 | #include "quadcopter.h" |
ivo_david_michelle | 9:f1bd96708a21 | 2 | #include "sensor.h" |
ivo_david_michelle | 14:64b06476d943 | 3 | #include "receiver.h" |
ivo_david_michelle | 14:64b06476d943 | 4 | #include <string> |
ivo_david_michelle | 23:04338a5ef404 | 5 | |
ivo_david_michelle | 23:04338a5ef404 | 6 | #ifndef M_PI |
ivo_david_michelle | 23:04338a5ef404 | 7 | #define M_PI 3.14159265358979323846 |
ivo_david_michelle | 23:04338a5ef404 | 8 | #endif |
ivo_david_michelle | 9:f1bd96708a21 | 9 | |
ivo_david_michelle | 10:e7d1801e966a | 10 | // constructor |
ivo_david_michelle | 34:eaea0ae92dfa | 11 | Quadcopter::Quadcopter(Serial *pcPntr, MRF24J40 *mrfPntr, Timer *timer) |
ivo_david_michelle | 24:e220fbb70ded | 12 | { |
ivo_david_michelle | 14:64b06476d943 | 13 | pc_= pcPntr; // enable printing |
ivo_david_michelle | 23:04338a5ef404 | 14 | g_= 9.81; |
ivo_david_michelle | 23:04338a5ef404 | 15 | l_= 0.25; |
ivo_david_michelle | 23:04338a5ef404 | 16 | gamma_= 1; |
ivo_david_michelle | 24:e220fbb70ded | 17 | |
ivo_david_michelle | 31:d473eacfc271 | 18 | zeroVelPwm = 0.11; |
ivo_david_michelle | 31:d473eacfc271 | 19 | maxPwm = 0.15; |
ivo_david_michelle | 27:11116aa69f32 | 20 | |
ivo_david_michelle | 32:e12b01c94b4a | 21 | // control gains set s.t. 100% joystick results in 15% (actually: (maxPwm-zeroVelPwm+0.1)) duty cycle. |
ivo_david_michelle | 32:e12b01c94b4a | 22 | kp_f_ = (maxPwm - zeroVelPwm) * 4 / 0.5; |
ivo_david_michelle | 35:35997980a8ba | 23 | kp_phi_ = 0.25 * (maxPwm - zeroVelPwm) * l_ / 0.5 * 4 / M_PI; |
ivo_david_michelle | 35:35997980a8ba | 24 | kp_theta_ = 0.2 * (maxPwm - zeroVelPwm) * l_ / 0.5 * 4 / M_PI; |
ivo_david_michelle | 29:ae765492fa8b | 25 | kp_psi_ = 0; |
ivo_david_michelle | 32:e12b01c94b4a | 26 | // kp_phi_ = 0; |
ivo_david_michelle | 31:d473eacfc271 | 27 | // kp_theta_ = 0; |
ivo_david_michelle | 10:e7d1801e966a | 28 | |
ivo_david_michelle | 10:e7d1801e966a | 29 | // derivative attitude control gains |
ivo_david_michelle | 35:35997980a8ba | 30 | kd_phi_ = 0.4 * (maxPwm - zeroVelPwm) * 2 / M_PI; //0.25 good |
ivo_david_michelle | 35:35997980a8ba | 31 | kd_theta_ = 0.0 * (maxPwm - zeroVelPwm) * 2 / M_PI; |
ivo_david_michelle | 29:ae765492fa8b | 32 | kd_psi_ = 0.1; |
ivo_david_michelle | 32:e12b01c94b4a | 33 | // kd_phi_ = 0; |
ivo_david_michelle | 31:d473eacfc271 | 34 | //kd_theta_ = 0; |
ivo_david_michelle | 31:d473eacfc271 | 35 | |
ivo_david_michelle | 32:e12b01c94b4a | 36 | // incresae ki_phi |
ivo_david_michelle | 35:35997980a8ba | 37 | ki_phi_ = 0 * (maxPwm - zeroVelPwm)/(2*M_PI/4); // full control signal after 2s at pi/4 error |
ivo_david_michelle | 32:e12b01c94b4a | 38 | ki_theta_ = 0 * (maxPwm - zeroVelPwm)/(2*M_PI/4); |
ivo_david_michelle | 32:e12b01c94b4a | 39 | |
ivo_david_michelle | 32:e12b01c94b4a | 40 | i_e_phi_ = 0; |
ivo_david_michelle | 31:d473eacfc271 | 41 | i_e_theta_ = 0; |
ivo_david_michelle | 31:d473eacfc271 | 42 | prev_time_ = 0; |
ivo_david_michelle | 32:e12b01c94b4a | 43 | |
ivo_david_michelle | 32:e12b01c94b4a | 44 | |
ivo_david_michelle | 31:d473eacfc271 | 45 | max_integral_phi_ = 0.05/(ki_phi_ * (0.5/l_ + 0.25)); // influence of integral part smaller than 0.05 |
ivo_david_michelle | 32:e12b01c94b4a | 46 | max_integral_theta_ = 0.05/(ki_theta_ * (0.5/l_ + 0.25)); |
ivo_david_michelle | 31:d473eacfc271 | 47 | |
ivo_david_michelle | 10:e7d1801e966a | 48 | |
ivo_david_michelle | 10:e7d1801e966a | 49 | // desired values (will come from joystick) |
ivo_david_michelle | 10:e7d1801e966a | 50 | F_des_ = 0; // desired thrust force (excluding weight compensation) |
ivo_david_michelle | 9:f1bd96708a21 | 51 | |
ivo_david_michelle | 9:f1bd96708a21 | 52 | dof_ = Adafruit_9DOF(); |
ivo_david_michelle | 9:f1bd96708a21 | 53 | accel_ = Adafruit_LSM303_Accel_Unified(30301); |
ivo_david_michelle | 9:f1bd96708a21 | 54 | mag_ = Adafruit_LSM303_Mag_Unified(30302); |
ivo_david_michelle | 9:f1bd96708a21 | 55 | gyro_ = Adafruit_L3GD20_Unified(20); |
ivo_david_michelle | 24:e220fbb70ded | 56 | |
ivo_david_michelle | 24:e220fbb70ded | 57 | // prepare for communication with remote control |
ivo_david_michelle | 16:2be2aab63198 | 58 | rcTimer_.start(); |
ivo_david_michelle | 15:90e07946186f | 59 | mrf_ = mrfPntr; // RF tranceiver to link with handheld. |
ivo_david_michelle | 15:90e07946186f | 60 | rcLength_ = 250; |
ivo_david_michelle | 16:2be2aab63198 | 61 | mrf_->SetChannel(3); //Set the Channel. 0 is default, 15 is max |
ivo_david_michelle | 27:11116aa69f32 | 62 | |
ivo_david_michelle | 24:e220fbb70ded | 63 | initial_offsets_ = (offset*) malloc(sizeof(offset)); |
ivo_david_michelle | 24:e220fbb70ded | 64 | initSensors(*this); // IMU |
ivo_david_michelle | 35:35997980a8ba | 65 | |
ivo_david_michelle | 34:eaea0ae92dfa | 66 | controlTimer = timer; |
ivo_david_michelle | 34:eaea0ae92dfa | 67 | controlTimer->start(); |
ivo_david_michelle | 35:35997980a8ba | 68 | |
ivo_david_michelle | 34:eaea0ae92dfa | 69 | prev_kalman_time = 0; |
ivo_david_michelle | 35:35997980a8ba | 70 | |
ivo_david_michelle | 34:eaea0ae92dfa | 71 | readSensorValues(); |
ivo_david_michelle | 35:35997980a8ba | 72 | |
ivo_david_michelle | 34:eaea0ae92dfa | 73 | kalmanPitch.setAngle(state_.phi); // set initial pitch |
ivo_david_michelle | 34:eaea0ae92dfa | 74 | kalmanRoll.setAngle(state_.theta); // set initial theta |
ivo_david_michelle | 34:eaea0ae92dfa | 75 | compAngleX = state_.phi; |
ivo_david_michelle | 34:eaea0ae92dfa | 76 | compAngleY = state_.theta; |
ivo_david_michelle | 9:f1bd96708a21 | 77 | } |
ivo_david_michelle | 9:f1bd96708a21 | 78 | |
ivo_david_michelle | 9:f1bd96708a21 | 79 | void Quadcopter::readSensorValues() |
ivo_david_michelle | 9:f1bd96708a21 | 80 | { |
ivo_david_michelle | 9:f1bd96708a21 | 81 | accel_.getEvent(&accel_event_); |
ivo_david_michelle | 9:f1bd96708a21 | 82 | mag_.getEvent(&mag_event_); |
ivo_david_michelle | 35:35997980a8ba | 83 | dof_.fusionGetOrientation(&accel_event_, &mag_event_, &orientation_); |
ivo_david_michelle | 9:f1bd96708a21 | 84 | |
ivo_david_michelle | 13:291ba30c7806 | 85 | gyro_.getEvent(&gyro_event_); |
ivo_david_michelle | 11:5c54826d23a7 | 86 | |
ivo_david_michelle | 24:e220fbb70ded | 87 | gyro_event_.gyro.x -= initial_offsets_->gyro_x; |
ivo_david_michelle | 24:e220fbb70ded | 88 | gyro_event_.gyro.y -= initial_offsets_->gyro_y; |
ivo_david_michelle | 24:e220fbb70ded | 89 | gyro_event_.gyro.z -= initial_offsets_->gyro_z; |
ivo_david_michelle | 24:e220fbb70ded | 90 | orientation_.roll -= initial_offsets_->roll; |
ivo_david_michelle | 24:e220fbb70ded | 91 | orientation_.pitch -= initial_offsets_->pitch; |
ivo_david_michelle | 24:e220fbb70ded | 92 | orientation_.heading -= initial_offsets_->heading; |
ivo_david_michelle | 13:291ba30c7806 | 93 | |
ivo_david_michelle | 35:35997980a8ba | 94 | |
ivo_david_michelle | 35:35997980a8ba | 95 | |
ivo_david_michelle | 32:e12b01c94b4a | 96 | static int current_filter = 0; |
ivo_david_michelle | 32:e12b01c94b4a | 97 | filters_.p[current_filter] = gyro_event_.gyro.x * M_PI / 180; |
ivo_david_michelle | 32:e12b01c94b4a | 98 | filters_.q[current_filter] = gyro_event_.gyro.y * M_PI / 180; |
ivo_david_michelle | 32:e12b01c94b4a | 99 | filters_.r[current_filter] = gyro_event_.gyro.z * M_PI / 180; |
ivo_david_michelle | 32:e12b01c94b4a | 100 | filters_.phi[current_filter] = orientation_.roll * M_PI / 180; |
ivo_david_michelle | 32:e12b01c94b4a | 101 | filters_.theta[current_filter] = -orientation_.pitch * M_PI / 180; |
ivo_david_michelle | 32:e12b01c94b4a | 102 | filters_.psi[current_filter] = orientation_.heading * M_PI / 180; |
ivo_david_michelle | 32:e12b01c94b4a | 103 | |
ivo_david_michelle | 32:e12b01c94b4a | 104 | current_filter = (current_filter + 1) % FILTER_SIZE; |
ivo_david_michelle | 32:e12b01c94b4a | 105 | |
ivo_david_michelle | 32:e12b01c94b4a | 106 | double p_sum = 0; |
ivo_david_michelle | 32:e12b01c94b4a | 107 | double q_sum = 0; |
ivo_david_michelle | 32:e12b01c94b4a | 108 | double r_sum = 0; |
ivo_david_michelle | 32:e12b01c94b4a | 109 | double phi_sum = 0; |
ivo_david_michelle | 32:e12b01c94b4a | 110 | double theta_sum = 0; |
ivo_david_michelle | 32:e12b01c94b4a | 111 | double psi_sum = 0; |
ivo_david_michelle | 32:e12b01c94b4a | 112 | for (int i = 0; i < FILTER_SIZE; i++) { |
ivo_david_michelle | 32:e12b01c94b4a | 113 | p_sum += filters_.p[i]; |
ivo_david_michelle | 32:e12b01c94b4a | 114 | q_sum += filters_.q[i]; |
ivo_david_michelle | 32:e12b01c94b4a | 115 | r_sum += filters_.r[i]; |
ivo_david_michelle | 32:e12b01c94b4a | 116 | phi_sum += filters_.phi[i]; |
ivo_david_michelle | 32:e12b01c94b4a | 117 | theta_sum += filters_.theta[i]; |
ivo_david_michelle | 32:e12b01c94b4a | 118 | psi_sum += filters_.psi[i]; |
ivo_david_michelle | 32:e12b01c94b4a | 119 | } |
ivo_david_michelle | 32:e12b01c94b4a | 120 | |
ivo_david_michelle | 35:35997980a8ba | 121 | // double radMaxAngle = M_PI / 180 * 20; |
ivo_david_michelle | 35:35997980a8ba | 122 | double phi_new = phi_sum / FILTER_SIZE; |
ivo_david_michelle | 35:35997980a8ba | 123 | // if (phi_new < radMaxAngle && phi_new > -radMaxAngle) { |
ivo_david_michelle | 35:35997980a8ba | 124 | // state_.phi = phi_new; |
ivo_david_michelle | 35:35997980a8ba | 125 | // }; |
ivo_david_michelle | 35:35997980a8ba | 126 | double theta_new = theta_sum / FILTER_SIZE; |
ivo_david_michelle | 35:35997980a8ba | 127 | // if (theta_new < radMaxAngle && theta_new > -radMaxAngle) { |
ivo_david_michelle | 35:35997980a8ba | 128 | // state_.theta = theta_new; |
ivo_david_michelle | 35:35997980a8ba | 129 | // }; |
ivo_david_michelle | 35:35997980a8ba | 130 | |
ivo_david_michelle | 35:35997980a8ba | 131 | state_.phi = phi_new; |
ivo_david_michelle | 35:35997980a8ba | 132 | state_.theta = theta_new; |
ivo_david_michelle | 32:e12b01c94b4a | 133 | state_.p = p_sum / FILTER_SIZE; |
ivo_david_michelle | 35:35997980a8ba | 134 | |
ivo_david_michelle | 32:e12b01c94b4a | 135 | state_.q = q_sum / FILTER_SIZE; |
ivo_david_michelle | 32:e12b01c94b4a | 136 | state_.r = r_sum / FILTER_SIZE; |
ivo_david_michelle | 24:e220fbb70ded | 137 | |
ivo_david_michelle | 35:35997980a8ba | 138 | //state_.theta = theta_sum / FILTER_SIZE; |
ivo_david_michelle | 32:e12b01c94b4a | 139 | state_.psi = psi_sum / FILTER_SIZE; |
ivo_david_michelle | 35:35997980a8ba | 140 | |
ivo_david_michelle | 34:eaea0ae92dfa | 141 | if (prev_kalman_time == 0) { |
ivo_david_michelle | 34:eaea0ae92dfa | 142 | prev_kalman_time = controlTimer->read(); |
ivo_david_michelle | 34:eaea0ae92dfa | 143 | return; |
ivo_david_michelle | 34:eaea0ae92dfa | 144 | } |
ivo_david_michelle | 35:35997980a8ba | 145 | |
ivo_david_michelle | 35:35997980a8ba | 146 | double raw_phi = state_.phi; |
ivo_david_michelle | 35:35997980a8ba | 147 | double raw_theta = state_.theta; |
ivo_david_michelle | 35:35997980a8ba | 148 | double raw_p = state_.p; |
ivo_david_michelle | 35:35997980a8ba | 149 | double raw_q = state_.q; |
ivo_david_michelle | 34:eaea0ae92dfa | 150 | float dt = controlTimer->read() - prev_kalman_time; |
ivo_david_michelle | 34:eaea0ae92dfa | 151 | state_.phi = kalmanRoll.getAngle(state_.phi * 180 / M_PI, state_.p * 180 / M_PI, dt) * M_PI / 180; |
ivo_david_michelle | 34:eaea0ae92dfa | 152 | state_.theta = kalmanPitch.getAngle(state_.theta * 180 / M_PI, state_.q * 180 / M_PI, dt) * M_PI / 180; |
ivo_david_michelle | 35:35997980a8ba | 153 | state_.p = kalmanRoll.getRate() * M_PI / 180; |
ivo_david_michelle | 35:35997980a8ba | 154 | state_.q = kalmanPitch.getRate() * M_PI / 180; |
ivo_david_michelle | 35:35997980a8ba | 155 | |
ivo_david_michelle | 35:35997980a8ba | 156 | compAngleX = 0.93 * (compAngleX + raw_p * dt) + 0.07 * raw_phi; // Calculate the angle using a Complimentary filter |
ivo_david_michelle | 35:35997980a8ba | 157 | compAngleY = 0.93 * (compAngleY + raw_q * dt) + 0.07 * raw_theta; |
ivo_david_michelle | 34:eaea0ae92dfa | 158 | |
ivo_david_michelle | 35:35997980a8ba | 159 | //state_.phi = compAngleX; |
ivo_david_michelle | 35:35997980a8ba | 160 | //state_.theta = compAngleY; |
ivo_david_michelle | 32:e12b01c94b4a | 161 | |
ivo_david_michelle | 35:35997980a8ba | 162 | prev_kalman_time = controlTimer->read(); |
ivo_david_michelle | 35:35997980a8ba | 163 | |
ivo_david_michelle | 35:35997980a8ba | 164 | pc_->printf("%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\r\n", prev_kalman_time, F_des_, desiredState_.psi, desiredState_.theta, desiredState_.phi, state_.psi, state_.theta, state_.phi,state_.r, state_.p, state_.q, compAngleX, compAngleY, raw_phi, raw_theta); |
ivo_david_michelle | 9:f1bd96708a21 | 165 | } |
ivo_david_michelle | 9:f1bd96708a21 | 166 | |
ivo_david_michelle | 34:eaea0ae92dfa | 167 | void Quadcopter::controller() |
ivo_david_michelle | 10:e7d1801e966a | 168 | { |
ivo_david_michelle | 34:eaea0ae92dfa | 169 | float time = controlTimer->read(); |
ivo_david_michelle | 31:d473eacfc271 | 170 | if (prev_time_ == 0) { |
ivo_david_michelle | 32:e12b01c94b4a | 171 | prev_time_ = time; |
ivo_david_michelle | 31:d473eacfc271 | 172 | return; |
ivo_david_michelle | 32:e12b01c94b4a | 173 | } |
ivo_david_michelle | 32:e12b01c94b4a | 174 | |
ivo_david_michelle | 10:e7d1801e966a | 175 | // PD controller |
ivo_david_michelle | 31:d473eacfc271 | 176 | double e_phi = desiredState_.phi - state_.phi; |
ivo_david_michelle | 31:d473eacfc271 | 177 | double e_theta = desiredState_.theta - state_.theta; |
ivo_david_michelle | 32:e12b01c94b4a | 178 | |
ivo_david_michelle | 32:e12b01c94b4a | 179 | float dt = time - prev_time_; |
ivo_david_michelle | 32:e12b01c94b4a | 180 | i_e_phi_ = i_e_phi_ + e_phi * dt; |
ivo_david_michelle | 32:e12b01c94b4a | 181 | i_e_theta_ = i_e_theta_ + e_theta * dt; |
ivo_david_michelle | 31:d473eacfc271 | 182 | i_e_phi_ = min(max_integral_phi_, i_e_phi_); |
ivo_david_michelle | 31:d473eacfc271 | 183 | i_e_theta_ = min(max_integral_theta_, i_e_theta_); |
ivo_david_michelle | 32:e12b01c94b4a | 184 | i_e_phi_ = max(-max_integral_phi_, i_e_phi_); |
ivo_david_michelle | 32:e12b01c94b4a | 185 | i_e_theta_ = max(-max_integral_theta_, i_e_theta_); |
ivo_david_michelle | 31:d473eacfc271 | 186 | |
ivo_david_michelle | 29:ae765492fa8b | 187 | controlInput_.f = kp_f_ * F_des_;//m_*g_ + F_des_; |
ivo_david_michelle | 31:d473eacfc271 | 188 | controlInput_.mx = kp_phi_ * e_phi + kd_phi_ * (desiredState_.p - state_.p) + ki_phi_ * i_e_phi_; |
ivo_david_michelle | 31:d473eacfc271 | 189 | controlInput_.my = kp_theta_ * e_theta + kd_theta_ * (desiredState_.q - state_.q) + ki_theta_ * i_e_theta_; |
ivo_david_michelle | 29:ae765492fa8b | 190 | controlInput_.mz = kd_psi_ * desiredState_.r; // feedforward desired yaw rate. // kp_psi_*(desiredState_.psi-state_.psi)+kd_psi_*(desiredState_.r-state_.r); |
ivo_david_michelle | 24:e220fbb70ded | 191 | |
ivo_david_michelle | 20:efa15ed008b4 | 192 | // set pwm values |
ivo_david_michelle | 29:ae765492fa8b | 193 | double forcePerMotor = 0.25 * controlInput_.f; |
ivo_david_michelle | 29:ae765492fa8b | 194 | double yawMomentPerMotor = 0.25 / gamma_ * controlInput_.mz; |
ivo_david_michelle | 29:ae765492fa8b | 195 | double rollMomentPerMotor = 0.5 / l_ * controlInput_.mx; |
ivo_david_michelle | 29:ae765492fa8b | 196 | double pitchMomentPerMotor = 0.5 / l_ * controlInput_.my; |
ivo_david_michelle | 29:ae765492fa8b | 197 | motorPwm_.m1 = zeroVelPwm + forcePerMotor - pitchMomentPerMotor - yawMomentPerMotor; |
ivo_david_michelle | 29:ae765492fa8b | 198 | motorPwm_.m2 = zeroVelPwm + forcePerMotor + rollMomentPerMotor + yawMomentPerMotor; |
ivo_david_michelle | 29:ae765492fa8b | 199 | motorPwm_.m3 = zeroVelPwm + forcePerMotor + pitchMomentPerMotor - yawMomentPerMotor; |
ivo_david_michelle | 29:ae765492fa8b | 200 | motorPwm_.m4 = zeroVelPwm + forcePerMotor - rollMomentPerMotor + yawMomentPerMotor; |
ivo_david_michelle | 27:11116aa69f32 | 201 | |
ivo_david_michelle | 29:ae765492fa8b | 202 | // cut off at max PWM |
ivo_david_michelle | 30:4820042e67b5 | 203 | //pc_->printf("m1: %f\tm2: %f\tm3: %f\tm4: %f\r\n", motorPwm_.m1, motorPwm_.m2, motorPwm_.m3, motorPwm_.m4); |
ivo_david_michelle | 32:e12b01c94b4a | 204 | motorPwm_.m1 = min(maxPwm, motorPwm_.m1); |
ivo_david_michelle | 27:11116aa69f32 | 205 | motorPwm_.m2 = min(maxPwm, motorPwm_.m2); |
ivo_david_michelle | 27:11116aa69f32 | 206 | motorPwm_.m3 = min(maxPwm, motorPwm_.m3); |
ivo_david_michelle | 27:11116aa69f32 | 207 | motorPwm_.m4 = min(maxPwm, motorPwm_.m4); |
ivo_david_michelle | 32:e12b01c94b4a | 208 | |
ivo_david_michelle | 32:e12b01c94b4a | 209 | prev_time_ = time; |
ivo_david_michelle | 32:e12b01c94b4a | 210 | |
ivo_david_michelle | 32:e12b01c94b4a | 211 | //pc_->printf("m1: %f\tm2: %f\tm3: %f\tm4: %f\r\n", motorPwm_.m1, motorPwm_.m2, motorPwm_.m3, motorPwm_.m4); |
ivo_david_michelle | 32:e12b01c94b4a | 212 | |
ivo_david_michelle | 33:244dea7a4e81 | 213 | //pc_->printf("%f %f %f %f %f %f %f %f \r\n", time, F_des_, desiredState_.psi, desiredState_.theta, desiredState_.phi, state_.psi, state_.theta, state_.phi); |
ivo_david_michelle | 13:291ba30c7806 | 214 | } |
ivo_david_michelle | 10:e7d1801e966a | 215 | |
ivo_david_michelle | 12:422963993df5 | 216 | motors Quadcopter::getPwm() |
ivo_david_michelle | 11:5c54826d23a7 | 217 | { |
ivo_david_michelle | 22:92401a4fec13 | 218 | return motorPwm_; |
ivo_david_michelle | 22:92401a4fec13 | 219 | } |
ivo_david_michelle | 20:efa15ed008b4 | 220 | |
ivo_david_michelle | 22:92401a4fec13 | 221 | state Quadcopter::getState() |
ivo_david_michelle | 22:92401a4fec13 | 222 | { |
ivo_david_michelle | 22:92401a4fec13 | 223 | return state_; |
ivo_david_michelle | 11:5c54826d23a7 | 224 | } |
ivo_david_michelle | 14:64b06476d943 | 225 | |
ivo_david_michelle | 24:e220fbb70ded | 226 | Adafruit_LSM303_Accel_Unified Quadcopter::getAccel() |
ivo_david_michelle | 24:e220fbb70ded | 227 | { |
ivo_david_michelle | 24:e220fbb70ded | 228 | return accel_; |
ivo_david_michelle | 24:e220fbb70ded | 229 | } |
ivo_david_michelle | 24:e220fbb70ded | 230 | |
ivo_david_michelle | 24:e220fbb70ded | 231 | Adafruit_LSM303_Mag_Unified Quadcopter::getMag() |
ivo_david_michelle | 24:e220fbb70ded | 232 | { |
ivo_david_michelle | 24:e220fbb70ded | 233 | return mag_; |
ivo_david_michelle | 24:e220fbb70ded | 234 | } |
ivo_david_michelle | 24:e220fbb70ded | 235 | |
ivo_david_michelle | 24:e220fbb70ded | 236 | Adafruit_L3GD20_Unified Quadcopter::getGyro() |
ivo_david_michelle | 24:e220fbb70ded | 237 | { |
ivo_david_michelle | 24:e220fbb70ded | 238 | return gyro_; |
ivo_david_michelle | 24:e220fbb70ded | 239 | } |
ivo_david_michelle | 24:e220fbb70ded | 240 | |
ivo_david_michelle | 24:e220fbb70ded | 241 | offset* Quadcopter::getOffset() |
ivo_david_michelle | 24:e220fbb70ded | 242 | { |
ivo_david_michelle | 24:e220fbb70ded | 243 | return initial_offsets_; |
ivo_david_michelle | 24:e220fbb70ded | 244 | } |
ivo_david_michelle | 24:e220fbb70ded | 245 | |
ivo_david_michelle | 24:e220fbb70ded | 246 | Adafruit_9DOF Quadcopter::getIMU() |
ivo_david_michelle | 24:e220fbb70ded | 247 | { |
ivo_david_michelle | 24:e220fbb70ded | 248 | return dof_; |
ivo_david_michelle | 24:e220fbb70ded | 249 | } |
ivo_david_michelle | 24:e220fbb70ded | 250 | |
ivo_david_michelle | 27:11116aa69f32 | 251 | double Quadcopter::getForce() |
ivo_david_michelle | 27:11116aa69f32 | 252 | { |
ivo_david_michelle | 27:11116aa69f32 | 253 | return F_des_; |
ivo_david_michelle | 27:11116aa69f32 | 254 | } |
ivo_david_michelle | 27:11116aa69f32 | 255 | |
ivo_david_michelle | 24:e220fbb70ded | 256 | void Quadcopter::readRc() |
ivo_david_michelle | 24:e220fbb70ded | 257 | { |
ivo_david_michelle | 14:64b06476d943 | 258 | uint8_t zero = 0; |
ivo_david_michelle | 14:64b06476d943 | 259 | uint8_t *rssi = &zero; |
ivo_david_michelle | 14:64b06476d943 | 260 | |
ivo_david_michelle | 14:64b06476d943 | 261 | uint8_t receive = 0; |
ivo_david_michelle | 14:64b06476d943 | 262 | |
ivo_david_michelle | 14:64b06476d943 | 263 | char rxBuffer[rcLength_]; |
ivo_david_michelle | 24:e220fbb70ded | 264 | |
ivo_david_michelle | 27:11116aa69f32 | 265 | float thrust; |
ivo_david_michelle | 27:11116aa69f32 | 266 | float yaw; |
ivo_david_michelle | 27:11116aa69f32 | 267 | float pitch; |
ivo_david_michelle | 27:11116aa69f32 | 268 | float roll; |
ivo_david_michelle | 27:11116aa69f32 | 269 | long long id; |
ivo_david_michelle | 24:e220fbb70ded | 270 | |
ivo_david_michelle | 35:35997980a8ba | 271 | // static int thrust_outliers = 0; |
ivo_david_michelle | 35:35997980a8ba | 272 | |
ivo_david_michelle | 16:2be2aab63198 | 273 | receive = rf_receive_rssi(*mrf_, rxBuffer, rssi, rcLength_ + 1); |
ivo_david_michelle | 16:2be2aab63198 | 274 | if (receive > 0) { |
ivo_david_michelle | 17:96d0c72e413e | 275 | sscanf(rxBuffer, "%lld,%f,%f,%f,%f", &id, &thrust, &yaw, &pitch, &roll); |
ivo_david_michelle | 16:2be2aab63198 | 276 | } else { |
ivo_david_michelle | 16:2be2aab63198 | 277 | pc_->printf("Receive failure\r\n"); |
ivo_david_michelle | 24:e220fbb70ded | 278 | } |
ivo_david_michelle | 24:e220fbb70ded | 279 | |
ivo_david_michelle | 35:35997980a8ba | 280 | // if (thrust < -0.3) { |
ivo_david_michelle | 35:35997980a8ba | 281 | // thrust_outliers++; |
ivo_david_michelle | 35:35997980a8ba | 282 | // if (thrust_outliers < 3) { |
ivo_david_michelle | 35:35997980a8ba | 283 | // thrust = F_des_; |
ivo_david_michelle | 35:35997980a8ba | 284 | // } |
ivo_david_michelle | 35:35997980a8ba | 285 | // } else { |
ivo_david_michelle | 35:35997980a8ba | 286 | // thrust_outliers = 0; |
ivo_david_michelle | 35:35997980a8ba | 287 | // } |
ivo_david_michelle | 35:35997980a8ba | 288 | |
ivo_david_michelle | 33:244dea7a4e81 | 289 | // TODO eliminate the zeros again after testing |
ivo_david_michelle | 25:d44610851105 | 290 | // convert to radians, range is = +-40° or +-0.698132 radians |
ivo_david_michelle | 33:244dea7a4e81 | 291 | desiredState_.phi = 1 * (-((roll - 0.5) * 80) * M_PI / 180); // minus, because joystick to right should result in positive moment |
ivo_david_michelle | 33:244dea7a4e81 | 292 | desiredState_.theta = 1 * ((pitch - 0.5) * 80) * M_PI / 180; |
ivo_david_michelle | 35:35997980a8ba | 293 | desiredState_.r = 1 * (yaw - 0.5); // number between 0 and 1 //((yaw - 0.5) * 80) * M_PI / 180; |
ivo_david_michelle | 35:35997980a8ba | 294 | F_des_ = thrust - 0.5; // number between 0 and 1 -> number between -0.5 and 0.5 |
ivo_david_michelle | 27:11116aa69f32 | 295 | |
ivo_david_michelle | 25:d44610851105 | 296 | // print id with thrust, yaw, pitch, and roll |
ivo_david_michelle | 35:35997980a8ba | 297 | // pc_->printf("%lld: thrust: %f yaw: %f pitch: %f roll: %f outliers: %d\r\n", id, F_des_, desiredState_.psi, desiredState_.theta, desiredState_.phi, thrust_outliers); |
ivo_david_michelle | 33:244dea7a4e81 | 298 | |
ivo_david_michelle | 14:64b06476d943 | 299 | } |
ivo_david_michelle | 14:64b06476d943 | 300 |