ESE350 project, Spring 2016, University of Pennsylvania

Dependencies:   Adafruit9-DOf Receiver mbed-rtos mbed

Committer:
ivo_david_michelle
Date:
Thu May 05 14:42:30 2016 +0000
Revision:
45:9f74298eee78
Parent:
44:14bee84cdf59
Child:
46:4bcf2e679f96
before cleaning up

Who changed what in which revision?

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