ESE350 project, Spring 2016, University of Pennsylvania
Dependencies: Adafruit9-DOf Receiver mbed-rtos mbed
quadcopter.cpp@50:c9831bb5a660, 2016-05-06 (annotated)
- Committer:
- ivo_david_michelle
- Date:
- Fri May 06 15:05:26 2016 +0000
- Revision:
- 50:c9831bb5a660
- Parent:
- 49:59c3427e6838
demo stand, tuned control gains
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 | 48:0346cdff12a7 | 10 | DigitalOut initializationLED(LED3); |
ivo_david_michelle | 48:0346cdff12a7 | 11 | |
ivo_david_michelle | 10:e7d1801e966a | 12 | // constructor |
ivo_david_michelle | 46:4bcf2e679f96 | 13 | Quadcopter::Quadcopter(Serial *pcPntr, MRF24J40 *mrfPntr, Timer *timer) |
ivo_david_michelle | 24:e220fbb70ded | 14 | { |
ivo_david_michelle | 14:64b06476d943 | 15 | pc_= pcPntr; // enable printing |
ivo_david_michelle | 23:04338a5ef404 | 16 | g_= 9.81; |
ivo_david_michelle | 23:04338a5ef404 | 17 | l_= 0.25; |
ivo_david_michelle | 23:04338a5ef404 | 18 | gamma_= 1; |
ivo_david_michelle | 24:e220fbb70ded | 19 | |
ivo_david_michelle | 46:4bcf2e679f96 | 20 | //desired_mutex = desired; |
ivo_david_michelle | 27:11116aa69f32 | 21 | |
ivo_david_michelle | 32:e12b01c94b4a | 22 | // control gains set s.t. 100% joystick results in 15% (actually: (maxPwm-zeroVelPwm+0.1)) duty cycle. |
ivo_david_michelle | 42:d09dec5bb184 | 23 | kp_f_ = (MAX_PWM - OFF_PWM) * 4 / 0.5; |
ivo_david_michelle | 50:c9831bb5a660 | 24 | kp_phi_ = 0.5 * (MAX_PWM - OFF_PWM) * l_ / 0.5 * 4 / M_PI; //0.3 quite slow |
ivo_david_michelle | 50:c9831bb5a660 | 25 | kp_theta_ = 0.5 * (MAX_PWM - OFF_PWM) * l_ / 0.5 * 4 / M_PI; |
ivo_david_michelle | 29:ae765492fa8b | 26 | kp_psi_ = 0; |
ivo_david_michelle | 10:e7d1801e966a | 27 | |
ivo_david_michelle | 10:e7d1801e966a | 28 | // derivative attitude control gains |
ivo_david_michelle | 45:9f74298eee78 | 29 | // TODO 0.15 for both |
ivo_david_michelle | 50:c9831bb5a660 | 30 | kd_phi_ = 0.3 * (MAX_PWM - OFF_PWM) * 2 / M_PI; // 0.25 maybe a good |
ivo_david_michelle | 50:c9831bb5a660 | 31 | kd_theta_ = 0.3 * (MAX_PWM - OFF_PWM) * 2 / M_PI; |
ivo_david_michelle | 29:ae765492fa8b | 32 | kd_psi_ = 0.1; |
ivo_david_michelle | 31:d473eacfc271 | 33 | |
ivo_david_michelle | 39:fff0a72633ee | 34 | // incresae ki_phi |
ivo_david_michelle | 42:d09dec5bb184 | 35 | ki_phi_ = 0 * (MAX_PWM - OFF_PWM)/(2*M_PI/4); // full control signal after 2s at pi/4 error |
ivo_david_michelle | 42:d09dec5bb184 | 36 | ki_theta_ = 0 * (MAX_PWM - OFF_PWM)/(2*M_PI/4); |
ivo_david_michelle | 32:e12b01c94b4a | 37 | |
ivo_david_michelle | 32:e12b01c94b4a | 38 | i_e_phi_ = 0; |
ivo_david_michelle | 31:d473eacfc271 | 39 | i_e_theta_ = 0; |
ivo_david_michelle | 48:0346cdff12a7 | 40 | |
ivo_david_michelle | 48:0346cdff12a7 | 41 | comp_x_offset = 0; |
ivo_david_michelle | 48:0346cdff12a7 | 42 | comp_y_offset = 0; |
ivo_david_michelle | 32:e12b01c94b4a | 43 | |
ivo_david_michelle | 31:d473eacfc271 | 44 | 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 | 45 | max_integral_theta_ = 0.05/(ki_theta_ * (0.5/l_ + 0.25)); |
ivo_david_michelle | 31:d473eacfc271 | 46 | |
ivo_david_michelle | 10:e7d1801e966a | 47 | |
ivo_david_michelle | 10:e7d1801e966a | 48 | // desired values (will come from joystick) |
ivo_david_michelle | 10:e7d1801e966a | 49 | F_des_ = 0; // desired thrust force (excluding weight compensation) |
ivo_david_michelle | 9:f1bd96708a21 | 50 | |
ivo_david_michelle | 9:f1bd96708a21 | 51 | dof_ = Adafruit_9DOF(); |
ivo_david_michelle | 9:f1bd96708a21 | 52 | accel_ = Adafruit_LSM303_Accel_Unified(30301); |
ivo_david_michelle | 46:4bcf2e679f96 | 53 | //mag_ = Adafruit_LSM303_Mag_Unified(30302); |
ivo_david_michelle | 9:f1bd96708a21 | 54 | gyro_ = Adafruit_L3GD20_Unified(20); |
ivo_david_michelle | 24:e220fbb70ded | 55 | |
ivo_david_michelle | 24:e220fbb70ded | 56 | // prepare for communication with remote control |
ivo_david_michelle | 16:2be2aab63198 | 57 | rcTimer_.start(); |
ivo_david_michelle | 15:90e07946186f | 58 | mrf_ = mrfPntr; // RF tranceiver to link with handheld. |
ivo_david_michelle | 15:90e07946186f | 59 | rcLength_ = 250; |
ivo_david_michelle | 16:2be2aab63198 | 60 | mrf_->SetChannel(3); //Set the Channel. 0 is default, 15 is max |
ivo_david_michelle | 27:11116aa69f32 | 61 | |
ivo_david_michelle | 46:4bcf2e679f96 | 62 | //initial_offsets_ = (offset*) malloc(sizeof(offset)); |
ivo_david_michelle | 24:e220fbb70ded | 63 | initSensors(*this); // IMU |
ivo_david_michelle | 35:35997980a8ba | 64 | |
ivo_david_michelle | 46:4bcf2e679f96 | 65 | // define timer |
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 | 46:4bcf2e679f96 | 69 | // initialze previous values to zero |
ivo_david_michelle | 46:4bcf2e679f96 | 70 | prev_controller_time_ = 0; |
ivo_david_michelle | 46:4bcf2e679f96 | 71 | prev_sensor_time = 0; |
ivo_david_michelle | 48:0346cdff12a7 | 72 | start_sampling_time = 0; |
ivo_david_michelle | 35:35997980a8ba | 73 | |
ivo_david_michelle | 48:0346cdff12a7 | 74 | //readSensorValues(); |
ivo_david_michelle | 35:35997980a8ba | 75 | |
ivo_david_michelle | 46:4bcf2e679f96 | 76 | //kalmanPitch.setAngle(state_.phi); // set initial pitch |
ivo_david_michelle | 46:4bcf2e679f96 | 77 | //§kalmanRoll.setAngle(state_.theta); // set initial theta |
ivo_david_michelle | 46:4bcf2e679f96 | 78 | //compAngleX = state_.phi; |
ivo_david_michelle | 46:4bcf2e679f96 | 79 | //compAngleY = state_.theta; // constant offset at props spinning in hover velocity /2 because of overcorrection |
ivo_david_michelle | 43:0e98d5488bf2 | 80 | |
ivo_david_michelle | 9:f1bd96708a21 | 81 | } |
ivo_david_michelle | 9:f1bd96708a21 | 82 | |
ivo_david_michelle | 9:f1bd96708a21 | 83 | void Quadcopter::readSensorValues() |
ivo_david_michelle | 9:f1bd96708a21 | 84 | { |
ivo_david_michelle | 41:d103f9aa44f0 | 85 | // precomputed gyro offsets |
ivo_david_michelle | 46:4bcf2e679f96 | 86 | double offset_gyro_x = -4.793711656441709e-04; |
ivo_david_michelle | 44:14bee84cdf59 | 87 | double offset_gyro_y = 4.481595092024531e-06; |
ivo_david_michelle | 44:14bee84cdf59 | 88 | double offset_gyro_z = -2.371472392638039e-05; |
ivo_david_michelle | 44:14bee84cdf59 | 89 | double offset_acc_x = -0.270495085889571; |
ivo_david_michelle | 44:14bee84cdf59 | 90 | double offset_acc_y = -0.053425306748466; |
ivo_david_michelle | 44:14bee84cdf59 | 91 | double offset_acc_z = -0.520254558282190; |
ivo_david_michelle | 44:14bee84cdf59 | 92 | |
ivo_david_michelle | 46:4bcf2e679f96 | 93 | if (prev_sensor_time == 0) { |
ivo_david_michelle | 46:4bcf2e679f96 | 94 | prev_sensor_time = controlTimer->read(); |
ivo_david_michelle | 48:0346cdff12a7 | 95 | start_sampling_time = prev_sensor_time; |
ivo_david_michelle | 36:40b134328376 | 96 | return; |
ivo_david_michelle | 36:40b134328376 | 97 | } |
ivo_david_michelle | 46:4bcf2e679f96 | 98 | // get acceleration values |
ivo_david_michelle | 9:f1bd96708a21 | 99 | accel_.getEvent(&accel_event_); |
ivo_david_michelle | 46:4bcf2e679f96 | 100 | // detract offsets form raw acceleromater data |
ivo_david_michelle | 44:14bee84cdf59 | 101 | accel_event_.acceleration.x -= offset_acc_x; |
ivo_david_michelle | 44:14bee84cdf59 | 102 | accel_event_.acceleration.y -= offset_acc_y; |
ivo_david_michelle | 44:14bee84cdf59 | 103 | accel_event_.acceleration.z -= offset_acc_z; |
ivo_david_michelle | 46:4bcf2e679f96 | 104 | // compute orientation with correctet acc data |
ivo_david_michelle | 37:a983eb9fd9c5 | 105 | dof_.accelGetOrientation(&accel_event_, &orientation_); |
ivo_david_michelle | 46:4bcf2e679f96 | 106 | // compute roll and pitch values |
ivo_david_michelle | 47:ae478c380136 | 107 | double raw_phi = orientation_.roll * M_PI / 180; |
ivo_david_michelle | 47:ae478c380136 | 108 | double raw_theta = orientation_.pitch * M_PI / 180; |
ivo_david_michelle | 11:5c54826d23a7 | 109 | |
ivo_david_michelle | 46:4bcf2e679f96 | 110 | // get gyro values |
ivo_david_michelle | 46:4bcf2e679f96 | 111 | gyro_.getEvent(&gyro_event_); |
ivo_david_michelle | 46:4bcf2e679f96 | 112 | // detract offsets form raw gyro data |
ivo_david_michelle | 44:14bee84cdf59 | 113 | gyro_event_.gyro.x -= offset_gyro_x; |
ivo_david_michelle | 44:14bee84cdf59 | 114 | gyro_event_.gyro.y -= offset_gyro_y; |
ivo_david_michelle | 44:14bee84cdf59 | 115 | gyro_event_.gyro.z -= offset_gyro_z; |
ivo_david_michelle | 46:4bcf2e679f96 | 116 | // compute rates |
ivo_david_michelle | 46:4bcf2e679f96 | 117 | double raw_p = gyro_event_.gyro.x * M_PI / 180; |
ivo_david_michelle | 46:4bcf2e679f96 | 118 | double raw_q = gyro_event_.gyro.y * M_PI / 180; |
ivo_david_michelle | 46:4bcf2e679f96 | 119 | double raw_r = gyro_event_.gyro.z * M_PI / 180; |
ivo_david_michelle | 32:e12b01c94b4a | 120 | |
ivo_david_michelle | 46:4bcf2e679f96 | 121 | float time = controlTimer->read(); |
ivo_david_michelle | 46:4bcf2e679f96 | 122 | float dt = time - prev_sensor_time; // TODO problems with this? |
ivo_david_michelle | 32:e12b01c94b4a | 123 | |
ivo_david_michelle | 46:4bcf2e679f96 | 124 | // Kalman Filter |
ivo_david_michelle | 46:4bcf2e679f96 | 125 | // state_.phi = kalmanRoll.getAngle(state_.phi * 180 / M_PI, state_.p * 180 / M_PI, dt) * M_PI / 180; |
ivo_david_michelle | 46:4bcf2e679f96 | 126 | // state_.theta = kalmanPitch.getAngle(state_.theta * 180 / M_PI, state_.q * 180 / M_PI, dt) * M_PI / 180; |
ivo_david_michelle | 46:4bcf2e679f96 | 127 | // state_.p = kalmanRoll.getRate() * M_PI / 180; |
ivo_david_michelle | 46:4bcf2e679f96 | 128 | // state_.q = kalmanPitch.getRate() * M_PI / 180; |
ivo_david_michelle | 46:4bcf2e679f96 | 129 | |
ivo_david_michelle | 46:4bcf2e679f96 | 130 | // complementary filter parameters |
ivo_david_michelle | 45:9f74298eee78 | 131 | double alphaX = 0.0002;//0.002; |
ivo_david_michelle | 47:ae478c380136 | 132 | double alphaY = 0.0001; //maybe 0.0002; |
ivo_david_michelle | 46:4bcf2e679f96 | 133 | // different version of complementary filter |
ivo_david_michelle | 40:09a59d5b7944 | 134 | //compAngleX = (1 - alphaX) * (compAngleX + raw_p * dt) + alphaX * raw_phi; // Calculate the angle using a Complimentary filter |
ivo_david_michelle | 47:ae478c380136 | 135 | //compAngleY = (1 - alphaY) * (com pAngleY + raw_q * dt) + alphaY * raw_theta; |
ivo_david_michelle | 46:4bcf2e679f96 | 136 | compAngleX = (1 - alphaX) * (compAngleX + (raw_p + 8.446703927263016e-04) * dt) + alphaX * raw_phi; // hardcoded value compensates drift with alpha == 0. |
ivo_david_michelle | 44:14bee84cdf59 | 137 | compAngleY = (1 - alphaY) * (compAngleY + (raw_q - 0.008936763695439) * dt) + alphaY * raw_theta; |
ivo_david_michelle | 48:0346cdff12a7 | 138 | |
ivo_david_michelle | 48:0346cdff12a7 | 139 | static int comp_avg_count = 0; |
ivo_david_michelle | 48:0346cdff12a7 | 140 | static double comp_sum_x = 0.0; |
ivo_david_michelle | 48:0346cdff12a7 | 141 | static double comp_sum_y = 0.0; |
ivo_david_michelle | 48:0346cdff12a7 | 142 | static bool sample_finished = false; |
ivo_david_michelle | 48:0346cdff12a7 | 143 | if (time - start_sampling_time < 5.0) { |
ivo_david_michelle | 48:0346cdff12a7 | 144 | comp_sum_x += compAngleX; |
ivo_david_michelle | 48:0346cdff12a7 | 145 | comp_sum_y += compAngleY; |
ivo_david_michelle | 48:0346cdff12a7 | 146 | comp_avg_count++; |
ivo_david_michelle | 48:0346cdff12a7 | 147 | } else if (!sample_finished) { |
ivo_david_michelle | 48:0346cdff12a7 | 148 | comp_x_offset = comp_sum_x / comp_avg_count; |
ivo_david_michelle | 48:0346cdff12a7 | 149 | comp_y_offset = comp_sum_y / comp_avg_count; |
ivo_david_michelle | 48:0346cdff12a7 | 150 | sample_finished = true; |
ivo_david_michelle | 48:0346cdff12a7 | 151 | initializationLED = 1; |
ivo_david_michelle | 48:0346cdff12a7 | 152 | } |
ivo_david_michelle | 48:0346cdff12a7 | 153 | |
ivo_david_michelle | 46:4bcf2e679f96 | 154 | // assign values to state |
ivo_david_michelle | 48:0346cdff12a7 | 155 | state_.phi = compAngleX - comp_x_offset; |
ivo_david_michelle | 48:0346cdff12a7 | 156 | state_.theta = compAngleY - comp_y_offset; |
ivo_david_michelle | 46:4bcf2e679f96 | 157 | state_.p = raw_p; |
ivo_david_michelle | 46:4bcf2e679f96 | 158 | state_.q = raw_q; |
ivo_david_michelle | 46:4bcf2e679f96 | 159 | state_.r = raw_r; |
ivo_david_michelle | 46:4bcf2e679f96 | 160 | |
ivo_david_michelle | 46:4bcf2e679f96 | 161 | prev_sensor_time = time; |
ivo_david_michelle | 32:e12b01c94b4a | 162 | |
ivo_david_michelle | 46:4bcf2e679f96 | 163 | // Plotting |
ivo_david_michelle | 42:d09dec5bb184 | 164 | /*pc_->printf("%f %f %f %f %f %f %f \r\n", |
ivo_david_michelle | 46:4bcf2e679f96 | 165 | prev_sensor_time, |
ivo_david_michelle | 40:09a59d5b7944 | 166 | accel_event_.acceleration.x, accel_event_.acceleration.y, accel_event_.acceleration.z, |
ivo_david_michelle | 40:09a59d5b7944 | 167 | gyro_event_.gyro.x, gyro_event_.gyro.y, gyro_event_.gyro.z); |
ivo_david_michelle | 42:d09dec5bb184 | 168 | */ |
ivo_david_michelle | 39:fff0a72633ee | 169 | // static int count = 0; |
ivo_david_michelle | 39:fff0a72633ee | 170 | // if (count % 100 == 0) { |
ivo_david_michelle | 39:fff0a72633ee | 171 | // pc_->printf("%d\r\n", count); |
ivo_david_michelle | 39:fff0a72633ee | 172 | // } |
ivo_david_michelle | 39:fff0a72633ee | 173 | // count++; |
ivo_david_michelle | 48:0346cdff12a7 | 174 | //pc_->printf("%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\r\n", prev_sensor_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,accel_event_.acceleration.x, accel_event_.acceleration.y, accel_event_.acceleration.z, comp_x_offset, comp_y_offset); |
ivo_david_michelle | 9:f1bd96708a21 | 175 | } |
ivo_david_michelle | 9:f1bd96708a21 | 176 | |
ivo_david_michelle | 34:eaea0ae92dfa | 177 | void Quadcopter::controller() |
ivo_david_michelle | 10:e7d1801e966a | 178 | { |
ivo_david_michelle | 34:eaea0ae92dfa | 179 | float time = controlTimer->read(); |
ivo_david_michelle | 46:4bcf2e679f96 | 180 | if (prev_controller_time_ == 0) { |
ivo_david_michelle | 46:4bcf2e679f96 | 181 | prev_controller_time_ = time; |
ivo_david_michelle | 31:d473eacfc271 | 182 | return; |
ivo_david_michelle | 32:e12b01c94b4a | 183 | } |
ivo_david_michelle | 32:e12b01c94b4a | 184 | |
ivo_david_michelle | 10:e7d1801e966a | 185 | // PD controller |
ivo_david_michelle | 31:d473eacfc271 | 186 | double e_phi = desiredState_.phi - state_.phi; |
ivo_david_michelle | 31:d473eacfc271 | 187 | double e_theta = desiredState_.theta - state_.theta; |
ivo_david_michelle | 32:e12b01c94b4a | 188 | |
ivo_david_michelle | 46:4bcf2e679f96 | 189 | float dt = time - prev_controller_time_; |
ivo_david_michelle | 46:4bcf2e679f96 | 190 | // integrate errors |
ivo_david_michelle | 32:e12b01c94b4a | 191 | i_e_phi_ = i_e_phi_ + e_phi * dt; |
ivo_david_michelle | 32:e12b01c94b4a | 192 | i_e_theta_ = i_e_theta_ + e_theta * dt; |
ivo_david_michelle | 46:4bcf2e679f96 | 193 | // limit integrals |
ivo_david_michelle | 31:d473eacfc271 | 194 | i_e_phi_ = min(max_integral_phi_, i_e_phi_); |
ivo_david_michelle | 31:d473eacfc271 | 195 | i_e_theta_ = min(max_integral_theta_, i_e_theta_); |
ivo_david_michelle | 32:e12b01c94b4a | 196 | i_e_phi_ = max(-max_integral_phi_, i_e_phi_); |
ivo_david_michelle | 32:e12b01c94b4a | 197 | i_e_theta_ = max(-max_integral_theta_, i_e_theta_); |
ivo_david_michelle | 46:4bcf2e679f96 | 198 | |
ivo_david_michelle | 46:4bcf2e679f96 | 199 | // compute control forces |
ivo_david_michelle | 46:4bcf2e679f96 | 200 | controlInput_.f = kp_f_ * F_des_; |
ivo_david_michelle | 31:d473eacfc271 | 201 | controlInput_.mx = kp_phi_ * e_phi + kd_phi_ * (desiredState_.p - state_.p) + ki_phi_ * i_e_phi_; |
ivo_david_michelle | 31:d473eacfc271 | 202 | controlInput_.my = kp_theta_ * e_theta + kd_theta_ * (desiredState_.q - state_.q) + ki_theta_ * i_e_theta_; |
ivo_david_michelle | 49:59c3427e6838 | 203 | controlInput_.mz = kd_psi_ * (desiredState_.r); //- state_.q); // feedforward desired yaw rate. // kp_psi_*(desiredState_.psi-state_.psi)+kd_psi_*(desiredState_.r-state_.r); |
ivo_david_michelle | 24:e220fbb70ded | 204 | |
ivo_david_michelle | 20:efa15ed008b4 | 205 | // set pwm values |
ivo_david_michelle | 29:ae765492fa8b | 206 | double forcePerMotor = 0.25 * controlInput_.f; |
ivo_david_michelle | 29:ae765492fa8b | 207 | double yawMomentPerMotor = 0.25 / gamma_ * controlInput_.mz; |
ivo_david_michelle | 29:ae765492fa8b | 208 | double rollMomentPerMotor = 0.5 / l_ * controlInput_.mx; |
ivo_david_michelle | 29:ae765492fa8b | 209 | double pitchMomentPerMotor = 0.5 / l_ * controlInput_.my; |
ivo_david_michelle | 42:d09dec5bb184 | 210 | motorPwm_.m1 = MIN_PWM_1 + forcePerMotor - pitchMomentPerMotor - yawMomentPerMotor; |
ivo_david_michelle | 42:d09dec5bb184 | 211 | motorPwm_.m2 = MIN_PWM_2 + forcePerMotor + rollMomentPerMotor + yawMomentPerMotor; |
ivo_david_michelle | 42:d09dec5bb184 | 212 | motorPwm_.m3 = MIN_PWM_3 + forcePerMotor + pitchMomentPerMotor - yawMomentPerMotor; |
ivo_david_michelle | 42:d09dec5bb184 | 213 | motorPwm_.m4 = MIN_PWM_4 + forcePerMotor - rollMomentPerMotor + yawMomentPerMotor; |
ivo_david_michelle | 27:11116aa69f32 | 214 | |
ivo_david_michelle | 29:ae765492fa8b | 215 | // cut off at max PWM |
ivo_david_michelle | 42:d09dec5bb184 | 216 | motorPwm_.m1 = min(MAX_PWM, motorPwm_.m1); |
ivo_david_michelle | 42:d09dec5bb184 | 217 | motorPwm_.m2 = min(MAX_PWM, motorPwm_.m2); |
ivo_david_michelle | 42:d09dec5bb184 | 218 | motorPwm_.m3 = min(MAX_PWM, motorPwm_.m3); |
ivo_david_michelle | 42:d09dec5bb184 | 219 | motorPwm_.m4 = min(MAX_PWM, motorPwm_.m4); |
ivo_david_michelle | 42:d09dec5bb184 | 220 | |
ivo_david_michelle | 42:d09dec5bb184 | 221 | motorPwm_.m1 = max(MIN_PWM_1, motorPwm_.m1); |
ivo_david_michelle | 42:d09dec5bb184 | 222 | motorPwm_.m2 = max(MIN_PWM_2, motorPwm_.m2); |
ivo_david_michelle | 42:d09dec5bb184 | 223 | motorPwm_.m3 = max(MIN_PWM_3, motorPwm_.m3); |
ivo_david_michelle | 42:d09dec5bb184 | 224 | motorPwm_.m4 = max(MIN_PWM_4, motorPwm_.m4); |
ivo_david_michelle | 32:e12b01c94b4a | 225 | |
ivo_david_michelle | 46:4bcf2e679f96 | 226 | prev_controller_time_ = time; |
ivo_david_michelle | 32:e12b01c94b4a | 227 | |
ivo_david_michelle | 46:4bcf2e679f96 | 228 | // Printing |
ivo_david_michelle | 46:4bcf2e679f96 | 229 | //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 | 32:e12b01c94b4a | 230 | //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 | 231 | |
ivo_david_michelle | 13:291ba30c7806 | 232 | } |
ivo_david_michelle | 10:e7d1801e966a | 233 | |
ivo_david_michelle | 12:422963993df5 | 234 | motors Quadcopter::getPwm() |
ivo_david_michelle | 11:5c54826d23a7 | 235 | { |
ivo_david_michelle | 22:92401a4fec13 | 236 | return motorPwm_; |
ivo_david_michelle | 22:92401a4fec13 | 237 | } |
ivo_david_michelle | 20:efa15ed008b4 | 238 | |
ivo_david_michelle | 22:92401a4fec13 | 239 | state Quadcopter::getState() |
ivo_david_michelle | 22:92401a4fec13 | 240 | { |
ivo_david_michelle | 22:92401a4fec13 | 241 | return state_; |
ivo_david_michelle | 11:5c54826d23a7 | 242 | } |
ivo_david_michelle | 14:64b06476d943 | 243 | |
ivo_david_michelle | 24:e220fbb70ded | 244 | Adafruit_LSM303_Accel_Unified Quadcopter::getAccel() |
ivo_david_michelle | 24:e220fbb70ded | 245 | { |
ivo_david_michelle | 24:e220fbb70ded | 246 | return accel_; |
ivo_david_michelle | 24:e220fbb70ded | 247 | } |
ivo_david_michelle | 24:e220fbb70ded | 248 | |
ivo_david_michelle | 46:4bcf2e679f96 | 249 | //Adafruit_LSM303_Mag_Unified Quadcopter::getMag() |
ivo_david_michelle | 46:4bcf2e679f96 | 250 | //{ |
ivo_david_michelle | 46:4bcf2e679f96 | 251 | // return mag_; |
ivo_david_michelle | 46:4bcf2e679f96 | 252 | //} |
ivo_david_michelle | 24:e220fbb70ded | 253 | |
ivo_david_michelle | 24:e220fbb70ded | 254 | Adafruit_L3GD20_Unified Quadcopter::getGyro() |
ivo_david_michelle | 24:e220fbb70ded | 255 | { |
ivo_david_michelle | 24:e220fbb70ded | 256 | return gyro_; |
ivo_david_michelle | 24:e220fbb70ded | 257 | } |
ivo_david_michelle | 24:e220fbb70ded | 258 | |
ivo_david_michelle | 24:e220fbb70ded | 259 | offset* Quadcopter::getOffset() |
ivo_david_michelle | 24:e220fbb70ded | 260 | { |
ivo_david_michelle | 24:e220fbb70ded | 261 | return initial_offsets_; |
ivo_david_michelle | 24:e220fbb70ded | 262 | } |
ivo_david_michelle | 24:e220fbb70ded | 263 | |
ivo_david_michelle | 24:e220fbb70ded | 264 | Adafruit_9DOF Quadcopter::getIMU() |
ivo_david_michelle | 24:e220fbb70ded | 265 | { |
ivo_david_michelle | 24:e220fbb70ded | 266 | return dof_; |
ivo_david_michelle | 24:e220fbb70ded | 267 | } |
ivo_david_michelle | 24:e220fbb70ded | 268 | |
ivo_david_michelle | 27:11116aa69f32 | 269 | double Quadcopter::getForce() |
ivo_david_michelle | 27:11116aa69f32 | 270 | { |
ivo_david_michelle | 37:a983eb9fd9c5 | 271 | return F_des_; |
ivo_david_michelle | 27:11116aa69f32 | 272 | } |
ivo_david_michelle | 27:11116aa69f32 | 273 | |
ivo_david_michelle | 24:e220fbb70ded | 274 | void Quadcopter::readRc() |
ivo_david_michelle | 24:e220fbb70ded | 275 | { |
ivo_david_michelle | 14:64b06476d943 | 276 | uint8_t zero = 0; |
ivo_david_michelle | 14:64b06476d943 | 277 | uint8_t *rssi = &zero; |
ivo_david_michelle | 14:64b06476d943 | 278 | |
ivo_david_michelle | 14:64b06476d943 | 279 | uint8_t receive = 0; |
ivo_david_michelle | 14:64b06476d943 | 280 | |
ivo_david_michelle | 14:64b06476d943 | 281 | char rxBuffer[rcLength_]; |
ivo_david_michelle | 24:e220fbb70ded | 282 | |
ivo_david_michelle | 27:11116aa69f32 | 283 | float thrust; |
ivo_david_michelle | 37:a983eb9fd9c5 | 284 | float yaw = 0; |
ivo_david_michelle | 37:a983eb9fd9c5 | 285 | float pitch = 0; |
ivo_david_michelle | 37:a983eb9fd9c5 | 286 | float roll = 0; |
ivo_david_michelle | 27:11116aa69f32 | 287 | long long id; |
ivo_david_michelle | 24:e220fbb70ded | 288 | |
ivo_david_michelle | 37:a983eb9fd9c5 | 289 | //static int thrust_outliers = 0; |
ivo_david_michelle | 35:35997980a8ba | 290 | |
ivo_david_michelle | 16:2be2aab63198 | 291 | receive = rf_receive_rssi(*mrf_, rxBuffer, rssi, rcLength_ + 1); |
ivo_david_michelle | 37:a983eb9fd9c5 | 292 | if (receive > 10) { |
ivo_david_michelle | 37:a983eb9fd9c5 | 293 | int written = sscanf(rxBuffer, "%lld,%f,%f,%f,%f", &id, &thrust, &yaw, &pitch, &roll); |
ivo_david_michelle | 38:14bf11115f9f | 294 | // pc_->printf("%d\r\n", written); |
ivo_david_michelle | 37:a983eb9fd9c5 | 295 | if (written != 5) { |
ivo_david_michelle | 38:14bf11115f9f | 296 | // pc_->printf("%s\r\n", rxBuffer); |
ivo_david_michelle | 37:a983eb9fd9c5 | 297 | return; |
ivo_david_michelle | 37:a983eb9fd9c5 | 298 | } |
ivo_david_michelle | 16:2be2aab63198 | 299 | } else { |
ivo_david_michelle | 16:2be2aab63198 | 300 | pc_->printf("Receive failure\r\n"); |
ivo_david_michelle | 37:a983eb9fd9c5 | 301 | return; |
ivo_david_michelle | 24:e220fbb70ded | 302 | } |
ivo_david_michelle | 39:fff0a72633ee | 303 | |
ivo_david_michelle | 39:fff0a72633ee | 304 | // test for outliers (can remove when fixed for sure) |
ivo_david_michelle | 39:fff0a72633ee | 305 | // float temp_thrust = thrust - 0.5; |
ivo_david_michelle | 39:fff0a72633ee | 306 | // |
ivo_david_michelle | 37:a983eb9fd9c5 | 307 | // if (temp_thrust < -0.3) { |
ivo_david_michelle | 35:35997980a8ba | 308 | // thrust_outliers++; |
ivo_david_michelle | 35:35997980a8ba | 309 | // if (thrust_outliers < 3) { |
ivo_david_michelle | 35:35997980a8ba | 310 | // thrust = F_des_; |
ivo_david_michelle | 35:35997980a8ba | 311 | // } |
ivo_david_michelle | 35:35997980a8ba | 312 | // } else { |
ivo_david_michelle | 35:35997980a8ba | 313 | // thrust_outliers = 0; |
ivo_david_michelle | 35:35997980a8ba | 314 | // } |
ivo_david_michelle | 35:35997980a8ba | 315 | |
ivo_david_michelle | 25:d44610851105 | 316 | // convert to radians, range is = +-40° or +-0.698132 radians |
ivo_david_michelle | 49:59c3427e6838 | 317 | desiredState_.phi = 1 * (-(roll * 20) * M_PI / 180); // minus, because joystick to right should result in positive moment |
ivo_david_michelle | 49:59c3427e6838 | 318 | desiredState_.theta = 1 * (pitch * 20) * M_PI / 180; |
ivo_david_michelle | 38:14bf11115f9f | 319 | desiredState_.r = 1 * yaw; // number between 0 and 1 //((yaw - 0.5) * 80) * M_PI / 180; |
ivo_david_michelle | 38:14bf11115f9f | 320 | F_des_ = thrust; // number between 0 and 1 -> number between -0.5 and 0.5 |
ivo_david_michelle | 39:fff0a72633ee | 321 | |
ivo_david_michelle | 39:fff0a72633ee | 322 | /* test for outliers (can remove when fixed for sure) |
ivo_david_michelle | 38:14bf11115f9f | 323 | if (abs(F_des_) > 0.01 || abs(desiredState_.psi) > 0.01 || abs(desiredState_.theta) > 0.02 || abs(desiredState_.phi) > 0.01) { |
ivo_david_michelle | 38:14bf11115f9f | 324 | pc_->printf("%lld: thrust: %f yaw: %f pitch: %f roll: %f\r\n", id, F_des_, desiredState_.psi, desiredState_.theta, desiredState_.phi); //, thrust_outliers); |
ivo_david_michelle | 38:14bf11115f9f | 325 | } |
ivo_david_michelle | 39:fff0a72633ee | 326 | */ |
ivo_david_michelle | 14:64b06476d943 | 327 | } |
ivo_david_michelle | 14:64b06476d943 | 328 |