ESE350 project, Spring 2016, University of Pennsylvania

Dependencies:   Adafruit9-DOf Receiver mbed-rtos mbed

Committer:
ivo_david_michelle
Date:
Fri Apr 22 00:52:32 2016 +0000
Revision:
32:e12b01c94b4a
Parent:
31:d473eacfc271
Child:
33:244dea7a4e81
reasonably ok control gains

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 15:90e07946186f 11 Quadcopter::Quadcopter(Serial *pcPntr, MRF24J40 *mrfPntr)
ivo_david_michelle 24:e220fbb70ded 12 {
ivo_david_michelle 14:64b06476d943 13 pc_= pcPntr; // enable printing
ivo_david_michelle 23:04338a5ef404 14 g_= 9.81;
ivo_david_michelle 23:04338a5ef404 15 l_= 0.25;
ivo_david_michelle 23:04338a5ef404 16 gamma_= 1;
ivo_david_michelle 24:e220fbb70ded 17
ivo_david_michelle 31:d473eacfc271 18 zeroVelPwm = 0.11;
ivo_david_michelle 31:d473eacfc271 19 maxPwm = 0.15;
ivo_david_michelle 27:11116aa69f32 20
ivo_david_michelle 32:e12b01c94b4a 21 // control gains set s.t. 100% joystick results in 15% (actually: (maxPwm-zeroVelPwm+0.1)) duty cycle.
ivo_david_michelle 32:e12b01c94b4a 22 kp_f_ = (maxPwm - zeroVelPwm) * 4 / 0.5;
ivo_david_michelle 32:e12b01c94b4a 23 kp_phi_ = 0.16 * (maxPwm - zeroVelPwm) * l_ / 0.5 * 4 / M_PI;
ivo_david_michelle 32:e12b01c94b4a 24 kp_theta_ = 0.12 * (maxPwm - zeroVelPwm) * l_ / 0.5 * 4 / M_PI;
ivo_david_michelle 29:ae765492fa8b 25 kp_psi_ = 0;
ivo_david_michelle 32:e12b01c94b4a 26 // kp_phi_ = 0;
ivo_david_michelle 31:d473eacfc271 27 // kp_theta_ = 0;
ivo_david_michelle 10:e7d1801e966a 28
ivo_david_michelle 10:e7d1801e966a 29 // derivative attitude control gains
ivo_david_michelle 32:e12b01c94b4a 30 kd_phi_ = 0.025 * (maxPwm - zeroVelPwm) * 2 / M_PI;
ivo_david_michelle 32:e12b01c94b4a 31 kd_theta_ = 0.025 * (maxPwm - zeroVelPwm) * 2 / M_PI;
ivo_david_michelle 29:ae765492fa8b 32 kd_psi_ = 0.1;
ivo_david_michelle 32:e12b01c94b4a 33 // kd_phi_ = 0;
ivo_david_michelle 31:d473eacfc271 34 //kd_theta_ = 0;
ivo_david_michelle 31:d473eacfc271 35
ivo_david_michelle 32:e12b01c94b4a 36 // incresae ki_phi
ivo_david_michelle 32:e12b01c94b4a 37 ki_phi_ = 0.1 * (maxPwm - zeroVelPwm)/(2*M_PI/4); // full control signal after 2s at pi/4 error
ivo_david_michelle 32:e12b01c94b4a 38 ki_theta_ = 0 * (maxPwm - zeroVelPwm)/(2*M_PI/4);
ivo_david_michelle 32:e12b01c94b4a 39
ivo_david_michelle 32:e12b01c94b4a 40 i_e_phi_ = 0;
ivo_david_michelle 31:d473eacfc271 41 i_e_theta_ = 0;
ivo_david_michelle 31:d473eacfc271 42 prev_time_ = 0;
ivo_david_michelle 32:e12b01c94b4a 43
ivo_david_michelle 32:e12b01c94b4a 44
ivo_david_michelle 31:d473eacfc271 45 max_integral_phi_ = 0.05/(ki_phi_ * (0.5/l_ + 0.25)); // influence of integral part smaller than 0.05
ivo_david_michelle 32:e12b01c94b4a 46 max_integral_theta_ = 0.05/(ki_theta_ * (0.5/l_ + 0.25));
ivo_david_michelle 31:d473eacfc271 47
ivo_david_michelle 10:e7d1801e966a 48
ivo_david_michelle 10:e7d1801e966a 49 // desired values (will come from joystick)
ivo_david_michelle 10:e7d1801e966a 50 F_des_ = 0; // desired thrust force (excluding weight compensation)
ivo_david_michelle 9:f1bd96708a21 51
ivo_david_michelle 9:f1bd96708a21 52 dof_ = Adafruit_9DOF();
ivo_david_michelle 9:f1bd96708a21 53 accel_ = Adafruit_LSM303_Accel_Unified(30301);
ivo_david_michelle 9:f1bd96708a21 54 mag_ = Adafruit_LSM303_Mag_Unified(30302);
ivo_david_michelle 9:f1bd96708a21 55 gyro_ = Adafruit_L3GD20_Unified(20);
ivo_david_michelle 24:e220fbb70ded 56
ivo_david_michelle 24:e220fbb70ded 57 // prepare for communication with remote control
ivo_david_michelle 16:2be2aab63198 58 rcTimer_.start();
ivo_david_michelle 15:90e07946186f 59 mrf_ = mrfPntr; // RF tranceiver to link with handheld.
ivo_david_michelle 15:90e07946186f 60 rcLength_ = 250;
ivo_david_michelle 16:2be2aab63198 61 mrf_->SetChannel(3); //Set the Channel. 0 is default, 15 is max
ivo_david_michelle 27:11116aa69f32 62
ivo_david_michelle 24:e220fbb70ded 63 initial_offsets_ = (offset*) malloc(sizeof(offset));
ivo_david_michelle 24:e220fbb70ded 64 initSensors(*this); // IMU
ivo_david_michelle 9:f1bd96708a21 65 }
ivo_david_michelle 9:f1bd96708a21 66
ivo_david_michelle 9:f1bd96708a21 67 void Quadcopter::readSensorValues()
ivo_david_michelle 9:f1bd96708a21 68 {
ivo_david_michelle 9:f1bd96708a21 69 accel_.getEvent(&accel_event_);
ivo_david_michelle 9:f1bd96708a21 70 if (dof_.accelGetOrientation(&accel_event_, &orientation_)) {
ivo_david_michelle 9:f1bd96708a21 71 }
ivo_david_michelle 9:f1bd96708a21 72 /* Calculate the heading using the magnetometer */
ivo_david_michelle 9:f1bd96708a21 73 mag_.getEvent(&mag_event_);
ivo_david_michelle 9:f1bd96708a21 74 if (dof_.magGetOrientation(SENSOR_AXIS_Z, &mag_event_, &orientation_)) {
ivo_david_michelle 9:f1bd96708a21 75 }
ivo_david_michelle 9:f1bd96708a21 76
ivo_david_michelle 13:291ba30c7806 77 gyro_.getEvent(&gyro_event_);
ivo_david_michelle 11:5c54826d23a7 78
ivo_david_michelle 24:e220fbb70ded 79 gyro_event_.gyro.x -= initial_offsets_->gyro_x;
ivo_david_michelle 24:e220fbb70ded 80 gyro_event_.gyro.y -= initial_offsets_->gyro_y;
ivo_david_michelle 24:e220fbb70ded 81 gyro_event_.gyro.z -= initial_offsets_->gyro_z;
ivo_david_michelle 24:e220fbb70ded 82 orientation_.roll -= initial_offsets_->roll;
ivo_david_michelle 24:e220fbb70ded 83 orientation_.pitch -= initial_offsets_->pitch;
ivo_david_michelle 24:e220fbb70ded 84 orientation_.heading -= initial_offsets_->heading;
ivo_david_michelle 13:291ba30c7806 85
ivo_david_michelle 32:e12b01c94b4a 86 static int current_filter = 0;
ivo_david_michelle 32:e12b01c94b4a 87 filters_.p[current_filter] = gyro_event_.gyro.x * M_PI / 180;
ivo_david_michelle 32:e12b01c94b4a 88 filters_.q[current_filter] = gyro_event_.gyro.y * M_PI / 180;
ivo_david_michelle 32:e12b01c94b4a 89 filters_.r[current_filter] = gyro_event_.gyro.z * M_PI / 180;
ivo_david_michelle 32:e12b01c94b4a 90 filters_.phi[current_filter] = orientation_.roll * M_PI / 180;
ivo_david_michelle 32:e12b01c94b4a 91 filters_.theta[current_filter] = -orientation_.pitch * M_PI / 180;
ivo_david_michelle 32:e12b01c94b4a 92 filters_.psi[current_filter] = orientation_.heading * M_PI / 180;
ivo_david_michelle 32:e12b01c94b4a 93
ivo_david_michelle 32:e12b01c94b4a 94 current_filter = (current_filter + 1) % FILTER_SIZE;
ivo_david_michelle 32:e12b01c94b4a 95
ivo_david_michelle 32:e12b01c94b4a 96 double p_sum = 0;
ivo_david_michelle 32:e12b01c94b4a 97 double q_sum = 0;
ivo_david_michelle 32:e12b01c94b4a 98 double r_sum = 0;
ivo_david_michelle 32:e12b01c94b4a 99 double phi_sum = 0;
ivo_david_michelle 32:e12b01c94b4a 100 double theta_sum = 0;
ivo_david_michelle 32:e12b01c94b4a 101 double psi_sum = 0;
ivo_david_michelle 32:e12b01c94b4a 102 for (int i = 0; i < FILTER_SIZE; i++) {
ivo_david_michelle 32:e12b01c94b4a 103 p_sum += filters_.p[i];
ivo_david_michelle 32:e12b01c94b4a 104 q_sum += filters_.q[i];
ivo_david_michelle 32:e12b01c94b4a 105 r_sum += filters_.r[i];
ivo_david_michelle 32:e12b01c94b4a 106 phi_sum += filters_.phi[i];
ivo_david_michelle 32:e12b01c94b4a 107 theta_sum += filters_.theta[i];
ivo_david_michelle 32:e12b01c94b4a 108 psi_sum += filters_.psi[i];
ivo_david_michelle 32:e12b01c94b4a 109 }
ivo_david_michelle 32:e12b01c94b4a 110
ivo_david_michelle 10:e7d1801e966a 111 // angular velocities in body coordinate system
ivo_david_michelle 32:e12b01c94b4a 112 state_.p = p_sum / FILTER_SIZE;
ivo_david_michelle 32:e12b01c94b4a 113 state_.q = q_sum / FILTER_SIZE;
ivo_david_michelle 32:e12b01c94b4a 114 state_.r = r_sum / FILTER_SIZE;
ivo_david_michelle 24:e220fbb70ded 115
ivo_david_michelle 32:e12b01c94b4a 116 state_.phi = phi_sum / FILTER_SIZE;
ivo_david_michelle 32:e12b01c94b4a 117 state_.theta = theta_sum / FILTER_SIZE;
ivo_david_michelle 32:e12b01c94b4a 118 state_.psi = psi_sum / FILTER_SIZE;
ivo_david_michelle 32:e12b01c94b4a 119
ivo_david_michelle 29:ae765492fa8b 120 //pc_->printf("Roll: %f\tPitch: %f\tYaw: %f\tVel x: %f\tVel y: %f\tVel z: %f\r\n", state_.phi, state_.theta, state_.psi, state_.p, state_.q, state_.r);
ivo_david_michelle 9:f1bd96708a21 121 }
ivo_david_michelle 9:f1bd96708a21 122
ivo_david_michelle 31:d473eacfc271 123 void Quadcopter::controller(float time)
ivo_david_michelle 10:e7d1801e966a 124 {
ivo_david_michelle 32:e12b01c94b4a 125
ivo_david_michelle 31:d473eacfc271 126 if (prev_time_ == 0) {
ivo_david_michelle 32:e12b01c94b4a 127 prev_time_ = time;
ivo_david_michelle 31:d473eacfc271 128 return;
ivo_david_michelle 32:e12b01c94b4a 129 }
ivo_david_michelle 32:e12b01c94b4a 130
ivo_david_michelle 32:e12b01c94b4a 131
ivo_david_michelle 10:e7d1801e966a 132 // PD controller
ivo_david_michelle 31:d473eacfc271 133 double e_phi = desiredState_.phi - state_.phi;
ivo_david_michelle 31:d473eacfc271 134 double e_theta = desiredState_.theta - state_.theta;
ivo_david_michelle 32:e12b01c94b4a 135
ivo_david_michelle 32:e12b01c94b4a 136 float dt = time - prev_time_;
ivo_david_michelle 32:e12b01c94b4a 137 i_e_phi_ = i_e_phi_ + e_phi * dt;
ivo_david_michelle 32:e12b01c94b4a 138 i_e_theta_ = i_e_theta_ + e_theta * dt;
ivo_david_michelle 31:d473eacfc271 139 i_e_phi_ = min(max_integral_phi_, i_e_phi_);
ivo_david_michelle 31:d473eacfc271 140 i_e_theta_ = min(max_integral_theta_, i_e_theta_);
ivo_david_michelle 32:e12b01c94b4a 141 i_e_phi_ = max(-max_integral_phi_, i_e_phi_);
ivo_david_michelle 32:e12b01c94b4a 142 i_e_theta_ = max(-max_integral_theta_, i_e_theta_);
ivo_david_michelle 31:d473eacfc271 143
ivo_david_michelle 29:ae765492fa8b 144 controlInput_.f = kp_f_ * F_des_;//m_*g_ + F_des_;
ivo_david_michelle 31:d473eacfc271 145 controlInput_.mx = kp_phi_ * e_phi + kd_phi_ * (desiredState_.p - state_.p) + ki_phi_ * i_e_phi_;
ivo_david_michelle 31:d473eacfc271 146 controlInput_.my = kp_theta_ * e_theta + kd_theta_ * (desiredState_.q - state_.q) + ki_theta_ * i_e_theta_;
ivo_david_michelle 29:ae765492fa8b 147 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 148
ivo_david_michelle 20:efa15ed008b4 149 // set pwm values
ivo_david_michelle 29:ae765492fa8b 150 double forcePerMotor = 0.25 * controlInput_.f;
ivo_david_michelle 29:ae765492fa8b 151 double yawMomentPerMotor = 0.25 / gamma_ * controlInput_.mz;
ivo_david_michelle 29:ae765492fa8b 152 double rollMomentPerMotor = 0.5 / l_ * controlInput_.mx;
ivo_david_michelle 29:ae765492fa8b 153 double pitchMomentPerMotor = 0.5 / l_ * controlInput_.my;
ivo_david_michelle 29:ae765492fa8b 154 motorPwm_.m1 = zeroVelPwm + forcePerMotor - pitchMomentPerMotor - yawMomentPerMotor;
ivo_david_michelle 29:ae765492fa8b 155 motorPwm_.m2 = zeroVelPwm + forcePerMotor + rollMomentPerMotor + yawMomentPerMotor;
ivo_david_michelle 29:ae765492fa8b 156 motorPwm_.m3 = zeroVelPwm + forcePerMotor + pitchMomentPerMotor - yawMomentPerMotor;
ivo_david_michelle 29:ae765492fa8b 157 motorPwm_.m4 = zeroVelPwm + forcePerMotor - rollMomentPerMotor + yawMomentPerMotor;
ivo_david_michelle 27:11116aa69f32 158
ivo_david_michelle 29:ae765492fa8b 159 // cut off at max PWM
ivo_david_michelle 30:4820042e67b5 160 //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 161 motorPwm_.m1 = min(maxPwm, motorPwm_.m1);
ivo_david_michelle 27:11116aa69f32 162 motorPwm_.m2 = min(maxPwm, motorPwm_.m2);
ivo_david_michelle 27:11116aa69f32 163 motorPwm_.m3 = min(maxPwm, motorPwm_.m3);
ivo_david_michelle 27:11116aa69f32 164 motorPwm_.m4 = min(maxPwm, motorPwm_.m4);
ivo_david_michelle 32:e12b01c94b4a 165
ivo_david_michelle 32:e12b01c94b4a 166 prev_time_ = time;
ivo_david_michelle 32:e12b01c94b4a 167
ivo_david_michelle 32:e12b01c94b4a 168 //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 169
ivo_david_michelle 13:291ba30c7806 170 }
ivo_david_michelle 10:e7d1801e966a 171
ivo_david_michelle 12:422963993df5 172 motors Quadcopter::getPwm()
ivo_david_michelle 11:5c54826d23a7 173 {
ivo_david_michelle 22:92401a4fec13 174 return motorPwm_;
ivo_david_michelle 22:92401a4fec13 175 }
ivo_david_michelle 20:efa15ed008b4 176
ivo_david_michelle 22:92401a4fec13 177 state Quadcopter::getState()
ivo_david_michelle 22:92401a4fec13 178 {
ivo_david_michelle 22:92401a4fec13 179 return state_;
ivo_david_michelle 11:5c54826d23a7 180 }
ivo_david_michelle 14:64b06476d943 181
ivo_david_michelle 24:e220fbb70ded 182 Adafruit_LSM303_Accel_Unified Quadcopter::getAccel()
ivo_david_michelle 24:e220fbb70ded 183 {
ivo_david_michelle 24:e220fbb70ded 184 return accel_;
ivo_david_michelle 24:e220fbb70ded 185 }
ivo_david_michelle 24:e220fbb70ded 186
ivo_david_michelle 24:e220fbb70ded 187 Adafruit_LSM303_Mag_Unified Quadcopter::getMag()
ivo_david_michelle 24:e220fbb70ded 188 {
ivo_david_michelle 24:e220fbb70ded 189 return mag_;
ivo_david_michelle 24:e220fbb70ded 190 }
ivo_david_michelle 24:e220fbb70ded 191
ivo_david_michelle 24:e220fbb70ded 192 Adafruit_L3GD20_Unified Quadcopter::getGyro()
ivo_david_michelle 24:e220fbb70ded 193 {
ivo_david_michelle 24:e220fbb70ded 194 return gyro_;
ivo_david_michelle 24:e220fbb70ded 195 }
ivo_david_michelle 24:e220fbb70ded 196
ivo_david_michelle 24:e220fbb70ded 197 offset* Quadcopter::getOffset()
ivo_david_michelle 24:e220fbb70ded 198 {
ivo_david_michelle 24:e220fbb70ded 199 return initial_offsets_;
ivo_david_michelle 24:e220fbb70ded 200 }
ivo_david_michelle 24:e220fbb70ded 201
ivo_david_michelle 24:e220fbb70ded 202 Adafruit_9DOF Quadcopter::getIMU()
ivo_david_michelle 24:e220fbb70ded 203 {
ivo_david_michelle 24:e220fbb70ded 204 return dof_;
ivo_david_michelle 24:e220fbb70ded 205 }
ivo_david_michelle 24:e220fbb70ded 206
ivo_david_michelle 27:11116aa69f32 207 double Quadcopter::getForce()
ivo_david_michelle 27:11116aa69f32 208 {
ivo_david_michelle 27:11116aa69f32 209 return F_des_;
ivo_david_michelle 27:11116aa69f32 210 }
ivo_david_michelle 27:11116aa69f32 211
ivo_david_michelle 24:e220fbb70ded 212 void Quadcopter::readRc()
ivo_david_michelle 24:e220fbb70ded 213 {
ivo_david_michelle 14:64b06476d943 214 uint8_t zero = 0;
ivo_david_michelle 14:64b06476d943 215 uint8_t *rssi = &zero;
ivo_david_michelle 14:64b06476d943 216
ivo_david_michelle 14:64b06476d943 217 uint8_t receive = 0;
ivo_david_michelle 14:64b06476d943 218
ivo_david_michelle 14:64b06476d943 219 char rxBuffer[rcLength_];
ivo_david_michelle 24:e220fbb70ded 220
ivo_david_michelle 27:11116aa69f32 221 float thrust;
ivo_david_michelle 27:11116aa69f32 222 float yaw;
ivo_david_michelle 27:11116aa69f32 223 float pitch;
ivo_david_michelle 27:11116aa69f32 224 float roll;
ivo_david_michelle 27:11116aa69f32 225 long long id;
ivo_david_michelle 24:e220fbb70ded 226
ivo_david_michelle 16:2be2aab63198 227 receive = rf_receive_rssi(*mrf_, rxBuffer, rssi, rcLength_ + 1);
ivo_david_michelle 16:2be2aab63198 228 if (receive > 0) {
ivo_david_michelle 17:96d0c72e413e 229 sscanf(rxBuffer, "%lld,%f,%f,%f,%f", &id, &thrust, &yaw, &pitch, &roll);
ivo_david_michelle 16:2be2aab63198 230 } else {
ivo_david_michelle 16:2be2aab63198 231 pc_->printf("Receive failure\r\n");
ivo_david_michelle 24:e220fbb70ded 232 }
ivo_david_michelle 24:e220fbb70ded 233
ivo_david_michelle 25:d44610851105 234 // convert to radians, range is = +-40° or +-0.698132 radians
ivo_david_michelle 28:61f7356325c3 235 desiredState_.phi = -((roll - 0.5) * 80) * M_PI / 180; // minus, because joystick to right should result in positive moment
ivo_david_michelle 25:d44610851105 236 desiredState_.theta = ((pitch - 0.5) * 80) * M_PI / 180;
ivo_david_michelle 27:11116aa69f32 237 desiredState_.r = yaw-0.5; // number between 0 and 1 //((yaw - 0.5) * 80) * M_PI / 180;
ivo_david_michelle 30:4820042e67b5 238 F_des_ = thrust-0.5; // number between 0 and 1 -> number between -0.5 and 0.5. //((thrust - 0.5) * 80) * M_PI / 180;
ivo_david_michelle 27:11116aa69f32 239
ivo_david_michelle 25:d44610851105 240 // print id with thrust, yaw, pitch, and roll
ivo_david_michelle 28:61f7356325c3 241 //pc_->printf("%lld: thrust: %f, yaw: %f, pitch: %f, roll: %f\r\n", id, F_des_, desiredState_.psi, desiredState_.theta, desiredState_.phi);
ivo_david_michelle 14:64b06476d943 242 }
ivo_david_michelle 14:64b06476d943 243