ESE350 project, Spring 2016, University of Pennsylvania
Dependencies: Adafruit9-DOf Receiver mbed-rtos mbed
quadcopter.cpp@30:4820042e67b5, 2016-04-20 (annotated)
- Committer:
- ivo_david_michelle
- Date:
- Wed Apr 20 02:29:37 2016 +0000
- Revision:
- 30:4820042e67b5
- Parent:
- 29:ae765492fa8b
- Child:
- 31:d473eacfc271
began tuning the proportional and derivative 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 | 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 | 30:4820042e67b5 | 18 | zeroVelPwm=0.11; |
ivo_david_michelle | 27:11116aa69f32 | 19 | maxPwm=0.15; |
ivo_david_michelle | 27:11116aa69f32 | 20 | |
ivo_david_michelle | 27:11116aa69f32 | 21 | // control gains set s.t. 100% joystick results in 15% (actually: (maxPwm-zeroVelPwm+0.1)) duty cycle. |
ivo_david_michelle | 29:ae765492fa8b | 22 | kp_f_ = (maxPwm - zeroVelPwm) * 4 / 0.5; |
ivo_david_michelle | 30:4820042e67b5 | 23 | kp_phi_ = 0.2 * (maxPwm - zeroVelPwm) * l_ / 0.5 * 4 / M_PI; |
ivo_david_michelle | 30:4820042e67b5 | 24 | kp_theta_ = 0.2 * (maxPwm - zeroVelPwm) * l_ / 0.5 * 4 / M_PI; |
ivo_david_michelle | 29:ae765492fa8b | 25 | kp_psi_ = 0; |
ivo_david_michelle | 30:4820042e67b5 | 26 | //kp_phi_ = 0; |
ivo_david_michelle | 30:4820042e67b5 | 27 | //kp_theta_ = 0; |
ivo_david_michelle | 10:e7d1801e966a | 28 | |
ivo_david_michelle | 10:e7d1801e966a | 29 | // derivative attitude control gains |
ivo_david_michelle | 30:4820042e67b5 | 30 | kd_phi_ = 0.3 * (maxPwm - zeroVelPwm) * 2 / M_PI; |
ivo_david_michelle | 30:4820042e67b5 | 31 | kd_theta_ = 0.3 * (maxPwm - zeroVelPwm) * 2 / M_PI; |
ivo_david_michelle | 29:ae765492fa8b | 32 | kd_psi_ = 0.1; |
ivo_david_michelle | 10:e7d1801e966a | 33 | |
ivo_david_michelle | 10:e7d1801e966a | 34 | // desired values (will come from joystick) |
ivo_david_michelle | 10:e7d1801e966a | 35 | F_des_ = 0; // desired thrust force (excluding weight compensation) |
ivo_david_michelle | 9:f1bd96708a21 | 36 | |
ivo_david_michelle | 9:f1bd96708a21 | 37 | dof_ = Adafruit_9DOF(); |
ivo_david_michelle | 9:f1bd96708a21 | 38 | accel_ = Adafruit_LSM303_Accel_Unified(30301); |
ivo_david_michelle | 9:f1bd96708a21 | 39 | mag_ = Adafruit_LSM303_Mag_Unified(30302); |
ivo_david_michelle | 9:f1bd96708a21 | 40 | gyro_ = Adafruit_L3GD20_Unified(20); |
ivo_david_michelle | 24:e220fbb70ded | 41 | |
ivo_david_michelle | 24:e220fbb70ded | 42 | // prepare for communication with remote control |
ivo_david_michelle | 16:2be2aab63198 | 43 | rcTimer_.start(); |
ivo_david_michelle | 15:90e07946186f | 44 | mrf_ = mrfPntr; // RF tranceiver to link with handheld. |
ivo_david_michelle | 15:90e07946186f | 45 | rcLength_ = 250; |
ivo_david_michelle | 16:2be2aab63198 | 46 | mrf_->SetChannel(3); //Set the Channel. 0 is default, 15 is max |
ivo_david_michelle | 27:11116aa69f32 | 47 | |
ivo_david_michelle | 24:e220fbb70ded | 48 | initial_offsets_ = (offset*) malloc(sizeof(offset)); |
ivo_david_michelle | 24:e220fbb70ded | 49 | initSensors(*this); // IMU |
ivo_david_michelle | 9:f1bd96708a21 | 50 | } |
ivo_david_michelle | 9:f1bd96708a21 | 51 | |
ivo_david_michelle | 9:f1bd96708a21 | 52 | void Quadcopter::readSensorValues() |
ivo_david_michelle | 9:f1bd96708a21 | 53 | { |
ivo_david_michelle | 9:f1bd96708a21 | 54 | accel_.getEvent(&accel_event_); |
ivo_david_michelle | 9:f1bd96708a21 | 55 | if (dof_.accelGetOrientation(&accel_event_, &orientation_)) { |
ivo_david_michelle | 9:f1bd96708a21 | 56 | } |
ivo_david_michelle | 9:f1bd96708a21 | 57 | /* Calculate the heading using the magnetometer */ |
ivo_david_michelle | 9:f1bd96708a21 | 58 | mag_.getEvent(&mag_event_); |
ivo_david_michelle | 9:f1bd96708a21 | 59 | if (dof_.magGetOrientation(SENSOR_AXIS_Z, &mag_event_, &orientation_)) { |
ivo_david_michelle | 9:f1bd96708a21 | 60 | } |
ivo_david_michelle | 9:f1bd96708a21 | 61 | |
ivo_david_michelle | 13:291ba30c7806 | 62 | gyro_.getEvent(&gyro_event_); |
ivo_david_michelle | 11:5c54826d23a7 | 63 | |
ivo_david_michelle | 24:e220fbb70ded | 64 | gyro_event_.gyro.x -= initial_offsets_->gyro_x; |
ivo_david_michelle | 24:e220fbb70ded | 65 | gyro_event_.gyro.y -= initial_offsets_->gyro_y; |
ivo_david_michelle | 24:e220fbb70ded | 66 | gyro_event_.gyro.z -= initial_offsets_->gyro_z; |
ivo_david_michelle | 24:e220fbb70ded | 67 | orientation_.roll -= initial_offsets_->roll; |
ivo_david_michelle | 24:e220fbb70ded | 68 | orientation_.pitch -= initial_offsets_->pitch; |
ivo_david_michelle | 24:e220fbb70ded | 69 | orientation_.heading -= initial_offsets_->heading; |
ivo_david_michelle | 13:291ba30c7806 | 70 | |
ivo_david_michelle | 10:e7d1801e966a | 71 | // angular velocities in body coordinate system |
ivo_david_michelle | 30:4820042e67b5 | 72 | state_.p = gyro_event_.gyro.x * M_PI / 180; |
ivo_david_michelle | 30:4820042e67b5 | 73 | state_.q = gyro_event_.gyro.y * M_PI / 180; |
ivo_david_michelle | 30:4820042e67b5 | 74 | state_.r = gyro_event_.gyro.z * M_PI / 180; |
ivo_david_michelle | 24:e220fbb70ded | 75 | |
ivo_david_michelle | 23:04338a5ef404 | 76 | state_.phi = orientation_.roll * M_PI / 180; |
ivo_david_michelle | 28:61f7356325c3 | 77 | state_.theta = -orientation_.pitch * M_PI / 180; |
ivo_david_michelle | 24:e220fbb70ded | 78 | state_.psi = orientation_.heading * M_PI / 180; |
ivo_david_michelle | 29:ae765492fa8b | 79 | //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 | 80 | } |
ivo_david_michelle | 9:f1bd96708a21 | 81 | |
ivo_david_michelle | 10:e7d1801e966a | 82 | void Quadcopter::controller() |
ivo_david_michelle | 10:e7d1801e966a | 83 | { |
ivo_david_michelle | 10:e7d1801e966a | 84 | // PD controller |
ivo_david_michelle | 29:ae765492fa8b | 85 | controlInput_.f = kp_f_ * F_des_;//m_*g_ + F_des_; |
ivo_david_michelle | 29:ae765492fa8b | 86 | controlInput_.mx = kp_phi_ * (desiredState_.phi - state_.phi) + kd_phi_ * (desiredState_.p - state_.p); |
ivo_david_michelle | 29:ae765492fa8b | 87 | controlInput_.my = kp_theta_ * (desiredState_.theta - state_.theta) + kd_theta_ * (desiredState_.q - state_.q); |
ivo_david_michelle | 29:ae765492fa8b | 88 | 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 | 89 | |
ivo_david_michelle | 20:efa15ed008b4 | 90 | // set pwm values |
ivo_david_michelle | 29:ae765492fa8b | 91 | double forcePerMotor = 0.25 * controlInput_.f; |
ivo_david_michelle | 29:ae765492fa8b | 92 | double yawMomentPerMotor = 0.25 / gamma_ * controlInput_.mz; |
ivo_david_michelle | 29:ae765492fa8b | 93 | double rollMomentPerMotor = 0.5 / l_ * controlInput_.mx; |
ivo_david_michelle | 29:ae765492fa8b | 94 | double pitchMomentPerMotor = 0.5 / l_ * controlInput_.my; |
ivo_david_michelle | 29:ae765492fa8b | 95 | motorPwm_.m1 = zeroVelPwm + forcePerMotor - pitchMomentPerMotor - yawMomentPerMotor; |
ivo_david_michelle | 29:ae765492fa8b | 96 | motorPwm_.m2 = zeroVelPwm + forcePerMotor + rollMomentPerMotor + yawMomentPerMotor; |
ivo_david_michelle | 29:ae765492fa8b | 97 | motorPwm_.m3 = zeroVelPwm + forcePerMotor + pitchMomentPerMotor - yawMomentPerMotor; |
ivo_david_michelle | 29:ae765492fa8b | 98 | motorPwm_.m4 = zeroVelPwm + forcePerMotor - rollMomentPerMotor + yawMomentPerMotor; |
ivo_david_michelle | 27:11116aa69f32 | 99 | |
ivo_david_michelle | 29:ae765492fa8b | 100 | // cut off at max PWM |
ivo_david_michelle | 30:4820042e67b5 | 101 | //pc_->printf("m1: %f\tm2: %f\tm3: %f\tm4: %f\r\n", motorPwm_.m1, motorPwm_.m2, motorPwm_.m3, motorPwm_.m4); |
ivo_david_michelle | 27:11116aa69f32 | 102 | motorPwm_.m1 = min(maxPwm, motorPwm_.m1); |
ivo_david_michelle | 27:11116aa69f32 | 103 | motorPwm_.m2 = min(maxPwm, motorPwm_.m2); |
ivo_david_michelle | 27:11116aa69f32 | 104 | motorPwm_.m3 = min(maxPwm, motorPwm_.m3); |
ivo_david_michelle | 27:11116aa69f32 | 105 | motorPwm_.m4 = min(maxPwm, motorPwm_.m4); |
ivo_david_michelle | 13:291ba30c7806 | 106 | } |
ivo_david_michelle | 10:e7d1801e966a | 107 | |
ivo_david_michelle | 12:422963993df5 | 108 | motors Quadcopter::getPwm() |
ivo_david_michelle | 11:5c54826d23a7 | 109 | { |
ivo_david_michelle | 22:92401a4fec13 | 110 | return motorPwm_; |
ivo_david_michelle | 22:92401a4fec13 | 111 | } |
ivo_david_michelle | 20:efa15ed008b4 | 112 | |
ivo_david_michelle | 22:92401a4fec13 | 113 | state Quadcopter::getState() |
ivo_david_michelle | 22:92401a4fec13 | 114 | { |
ivo_david_michelle | 22:92401a4fec13 | 115 | return state_; |
ivo_david_michelle | 11:5c54826d23a7 | 116 | } |
ivo_david_michelle | 14:64b06476d943 | 117 | |
ivo_david_michelle | 24:e220fbb70ded | 118 | Adafruit_LSM303_Accel_Unified Quadcopter::getAccel() |
ivo_david_michelle | 24:e220fbb70ded | 119 | { |
ivo_david_michelle | 24:e220fbb70ded | 120 | return accel_; |
ivo_david_michelle | 24:e220fbb70ded | 121 | } |
ivo_david_michelle | 24:e220fbb70ded | 122 | |
ivo_david_michelle | 24:e220fbb70ded | 123 | Adafruit_LSM303_Mag_Unified Quadcopter::getMag() |
ivo_david_michelle | 24:e220fbb70ded | 124 | { |
ivo_david_michelle | 24:e220fbb70ded | 125 | return mag_; |
ivo_david_michelle | 24:e220fbb70ded | 126 | } |
ivo_david_michelle | 24:e220fbb70ded | 127 | |
ivo_david_michelle | 24:e220fbb70ded | 128 | Adafruit_L3GD20_Unified Quadcopter::getGyro() |
ivo_david_michelle | 24:e220fbb70ded | 129 | { |
ivo_david_michelle | 24:e220fbb70ded | 130 | return gyro_; |
ivo_david_michelle | 24:e220fbb70ded | 131 | } |
ivo_david_michelle | 24:e220fbb70ded | 132 | |
ivo_david_michelle | 24:e220fbb70ded | 133 | offset* Quadcopter::getOffset() |
ivo_david_michelle | 24:e220fbb70ded | 134 | { |
ivo_david_michelle | 24:e220fbb70ded | 135 | return initial_offsets_; |
ivo_david_michelle | 24:e220fbb70ded | 136 | } |
ivo_david_michelle | 24:e220fbb70ded | 137 | |
ivo_david_michelle | 24:e220fbb70ded | 138 | Adafruit_9DOF Quadcopter::getIMU() |
ivo_david_michelle | 24:e220fbb70ded | 139 | { |
ivo_david_michelle | 24:e220fbb70ded | 140 | return dof_; |
ivo_david_michelle | 24:e220fbb70ded | 141 | } |
ivo_david_michelle | 24:e220fbb70ded | 142 | |
ivo_david_michelle | 27:11116aa69f32 | 143 | double Quadcopter::getForce() |
ivo_david_michelle | 27:11116aa69f32 | 144 | { |
ivo_david_michelle | 27:11116aa69f32 | 145 | return F_des_; |
ivo_david_michelle | 27:11116aa69f32 | 146 | } |
ivo_david_michelle | 27:11116aa69f32 | 147 | |
ivo_david_michelle | 24:e220fbb70ded | 148 | void Quadcopter::readRc() |
ivo_david_michelle | 24:e220fbb70ded | 149 | { |
ivo_david_michelle | 14:64b06476d943 | 150 | uint8_t zero = 0; |
ivo_david_michelle | 14:64b06476d943 | 151 | uint8_t *rssi = &zero; |
ivo_david_michelle | 14:64b06476d943 | 152 | |
ivo_david_michelle | 14:64b06476d943 | 153 | uint8_t receive = 0; |
ivo_david_michelle | 14:64b06476d943 | 154 | |
ivo_david_michelle | 14:64b06476d943 | 155 | char rxBuffer[rcLength_]; |
ivo_david_michelle | 24:e220fbb70ded | 156 | |
ivo_david_michelle | 27:11116aa69f32 | 157 | float thrust; |
ivo_david_michelle | 27:11116aa69f32 | 158 | float yaw; |
ivo_david_michelle | 27:11116aa69f32 | 159 | float pitch; |
ivo_david_michelle | 27:11116aa69f32 | 160 | float roll; |
ivo_david_michelle | 27:11116aa69f32 | 161 | long long id; |
ivo_david_michelle | 24:e220fbb70ded | 162 | |
ivo_david_michelle | 16:2be2aab63198 | 163 | receive = rf_receive_rssi(*mrf_, rxBuffer, rssi, rcLength_ + 1); |
ivo_david_michelle | 16:2be2aab63198 | 164 | if (receive > 0) { |
ivo_david_michelle | 17:96d0c72e413e | 165 | sscanf(rxBuffer, "%lld,%f,%f,%f,%f", &id, &thrust, &yaw, &pitch, &roll); |
ivo_david_michelle | 16:2be2aab63198 | 166 | } else { |
ivo_david_michelle | 16:2be2aab63198 | 167 | pc_->printf("Receive failure\r\n"); |
ivo_david_michelle | 24:e220fbb70ded | 168 | } |
ivo_david_michelle | 24:e220fbb70ded | 169 | |
ivo_david_michelle | 25:d44610851105 | 170 | // convert to radians, range is = +-40° or +-0.698132 radians |
ivo_david_michelle | 28:61f7356325c3 | 171 | desiredState_.phi = -((roll - 0.5) * 80) * M_PI / 180; // minus, because joystick to right should result in positive moment |
ivo_david_michelle | 25:d44610851105 | 172 | desiredState_.theta = ((pitch - 0.5) * 80) * M_PI / 180; |
ivo_david_michelle | 27:11116aa69f32 | 173 | desiredState_.r = yaw-0.5; // number between 0 and 1 //((yaw - 0.5) * 80) * M_PI / 180; |
ivo_david_michelle | 30:4820042e67b5 | 174 | 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 | 175 | |
ivo_david_michelle | 25:d44610851105 | 176 | // print id with thrust, yaw, pitch, and roll |
ivo_david_michelle | 28:61f7356325c3 | 177 | //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 | 178 | } |
ivo_david_michelle | 14:64b06476d943 | 179 |