ESE350 project, Spring 2016, University of Pennsylvania

Dependencies:   Adafruit9-DOf Receiver mbed-rtos mbed

Committer:
ivo_david_michelle
Date:
Sun May 01 18:45:02 2016 +0000
Revision:
37:a983eb9fd9c5
Parent:
36:40b134328376
Child:
38:14bf11115f9f
david's changes and printing diagnostics from readRC

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