ESE350 project, Spring 2016, University of Pennsylvania

Dependencies:   Adafruit9-DOf Receiver mbed-rtos mbed

Committer:
ivo_david_michelle
Date:
Thu May 05 15:58:49 2016 +0000
Revision:
46:4bcf2e679f96
Parent:
45:9f74298eee78
Child:
47:ae478c380136
best stand so far. hovering see video

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