ESE350 project, Spring 2016, University of Pennsylvania
Dependencies: Adafruit9-DOf Receiver mbed-rtos mbed
quadcopter.cpp@25:d44610851105, 2016-04-13 (annotated)
- Committer:
- ivo_david_michelle
- Date:
- Wed Apr 13 19:16:03 2016 +0000
- Revision:
- 25:d44610851105
- Parent:
- 24:e220fbb70ded
- Child:
- 26:7f50323c0c0d
convert desired state values to radians ;
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 | 14:64b06476d943 | 10 | //#include "mbed.h" |
ivo_david_michelle | 9:f1bd96708a21 | 11 | |
ivo_david_michelle | 10:e7d1801e966a | 12 | // constructor |
ivo_david_michelle | 15:90e07946186f | 13 | Quadcopter::Quadcopter(Serial *pcPntr, MRF24J40 *mrfPntr) |
ivo_david_michelle | 24:e220fbb70ded | 14 | { |
ivo_david_michelle | 14:64b06476d943 | 15 | pc_= pcPntr; // enable printing |
ivo_david_michelle | 24:e220fbb70ded | 16 | //initSensors(accel_, mag_, gyro_, offsetAngRate_); // IMUm_= 1; |
ivo_david_michelle | 23:04338a5ef404 | 17 | g_= 9.81; |
ivo_david_michelle | 23:04338a5ef404 | 18 | l_= 0.25; |
ivo_david_michelle | 23:04338a5ef404 | 19 | gamma_= 1; |
ivo_david_michelle | 24:e220fbb70ded | 20 | |
ivo_david_michelle | 24:e220fbb70ded | 21 | |
ivo_david_michelle | 10:e7d1801e966a | 22 | // proportional attitude control gains |
ivo_david_michelle | 24:e220fbb70ded | 23 | // TODO change gains so that joystick deflection never produces pwm duty cycle >10%. |
ivo_david_michelle | 24:e220fbb70ded | 24 | |
ivo_david_michelle | 21:336faf452989 | 25 | kp_f_ =2.5; |
ivo_david_michelle | 21:336faf452989 | 26 | kp_phi_ = 0.2; |
ivo_david_michelle | 21:336faf452989 | 27 | kp_theta_ = 0.2; |
ivo_david_michelle | 21:336faf452989 | 28 | kp_psi_ = 0.2; |
ivo_david_michelle | 10:e7d1801e966a | 29 | |
ivo_david_michelle | 10:e7d1801e966a | 30 | // derivative attitude control gains |
ivo_david_michelle | 10:e7d1801e966a | 31 | kd_phi_ = 0; |
ivo_david_michelle | 10:e7d1801e966a | 32 | kd_theta_ = 0; |
ivo_david_michelle | 10:e7d1801e966a | 33 | kd_psi_ = 0; |
ivo_david_michelle | 10:e7d1801e966a | 34 | |
ivo_david_michelle | 10:e7d1801e966a | 35 | // desired values (will come from joystick) |
ivo_david_michelle | 10:e7d1801e966a | 36 | F_des_ = 0; // desired thrust force (excluding weight compensation) |
ivo_david_michelle | 9:f1bd96708a21 | 37 | |
ivo_david_michelle | 9:f1bd96708a21 | 38 | dof_ = Adafruit_9DOF(); |
ivo_david_michelle | 9:f1bd96708a21 | 39 | accel_ = Adafruit_LSM303_Accel_Unified(30301); |
ivo_david_michelle | 9:f1bd96708a21 | 40 | mag_ = Adafruit_LSM303_Mag_Unified(30302); |
ivo_david_michelle | 9:f1bd96708a21 | 41 | gyro_ = Adafruit_L3GD20_Unified(20); |
ivo_david_michelle | 14:64b06476d943 | 42 | //motor1_(p21); |
ivo_david_michelle | 24:e220fbb70ded | 43 | |
ivo_david_michelle | 14:64b06476d943 | 44 | // initSensors(accel_, mag_, gyro_, offsetAngRate_); // IMU |
ivo_david_michelle | 24:e220fbb70ded | 45 | |
ivo_david_michelle | 24:e220fbb70ded | 46 | // prepare for communication with remote control |
ivo_david_michelle | 16:2be2aab63198 | 47 | rcTimer_.start(); |
ivo_david_michelle | 15:90e07946186f | 48 | mrf_ = mrfPntr; // RF tranceiver to link with handheld. |
ivo_david_michelle | 15:90e07946186f | 49 | rcLength_ = 250; |
ivo_david_michelle | 16:2be2aab63198 | 50 | mrf_->SetChannel(3); //Set the Channel. 0 is default, 15 is max |
ivo_david_michelle | 17:96d0c72e413e | 51 | thrust = 0.5; |
ivo_david_michelle | 17:96d0c72e413e | 52 | yaw = 0.5; |
ivo_david_michelle | 17:96d0c72e413e | 53 | pitch = 0.5; |
ivo_david_michelle | 17:96d0c72e413e | 54 | roll = 0.5; |
ivo_david_michelle | 17:96d0c72e413e | 55 | id = -1; |
ivo_david_michelle | 24:e220fbb70ded | 56 | initial_offsets_ = (offset*) malloc(sizeof(offset)); |
ivo_david_michelle | 24:e220fbb70ded | 57 | initSensors(*this); // IMU |
ivo_david_michelle | 9:f1bd96708a21 | 58 | } |
ivo_david_michelle | 9:f1bd96708a21 | 59 | |
ivo_david_michelle | 9:f1bd96708a21 | 60 | |
ivo_david_michelle | 9:f1bd96708a21 | 61 | void Quadcopter::readSensorValues() |
ivo_david_michelle | 9:f1bd96708a21 | 62 | { |
ivo_david_michelle | 9:f1bd96708a21 | 63 | accel_.getEvent(&accel_event_); |
ivo_david_michelle | 9:f1bd96708a21 | 64 | if (dof_.accelGetOrientation(&accel_event_, &orientation_)) { |
ivo_david_michelle | 9:f1bd96708a21 | 65 | } |
ivo_david_michelle | 9:f1bd96708a21 | 66 | /* Calculate the heading using the magnetometer */ |
ivo_david_michelle | 9:f1bd96708a21 | 67 | mag_.getEvent(&mag_event_); |
ivo_david_michelle | 9:f1bd96708a21 | 68 | if (dof_.magGetOrientation(SENSOR_AXIS_Z, &mag_event_, &orientation_)) { |
ivo_david_michelle | 9:f1bd96708a21 | 69 | } |
ivo_david_michelle | 9:f1bd96708a21 | 70 | |
ivo_david_michelle | 13:291ba30c7806 | 71 | gyro_.getEvent(&gyro_event_); |
ivo_david_michelle | 11:5c54826d23a7 | 72 | |
ivo_david_michelle | 24:e220fbb70ded | 73 | gyro_event_.gyro.x -= initial_offsets_->gyro_x; |
ivo_david_michelle | 24:e220fbb70ded | 74 | gyro_event_.gyro.y -= initial_offsets_->gyro_y; |
ivo_david_michelle | 24:e220fbb70ded | 75 | gyro_event_.gyro.z -= initial_offsets_->gyro_z; |
ivo_david_michelle | 24:e220fbb70ded | 76 | orientation_.roll -= initial_offsets_->roll; |
ivo_david_michelle | 24:e220fbb70ded | 77 | orientation_.pitch -= initial_offsets_->pitch; |
ivo_david_michelle | 24:e220fbb70ded | 78 | orientation_.heading -= initial_offsets_->heading; |
ivo_david_michelle | 13:291ba30c7806 | 79 | |
ivo_david_michelle | 10:e7d1801e966a | 80 | // measured values (will come from IMU/parameter class/Input to function later) |
ivo_david_michelle | 10:e7d1801e966a | 81 | // angles |
ivo_david_michelle | 10:e7d1801e966a | 82 | state_.phi = orientation_.roll; |
ivo_david_michelle | 10:e7d1801e966a | 83 | state_.theta =orientation_.pitch; |
ivo_david_michelle | 10:e7d1801e966a | 84 | state_.psi =orientation_.heading; |
ivo_david_michelle | 10:e7d1801e966a | 85 | // angular velocities in body coordinate system |
ivo_david_michelle | 10:e7d1801e966a | 86 | state_.p = gyro_event_.gyro.x; |
ivo_david_michelle | 10:e7d1801e966a | 87 | state_.q = gyro_event_.gyro.y; |
ivo_david_michelle | 10:e7d1801e966a | 88 | state_.r = gyro_event_.gyro.z; |
ivo_david_michelle | 24:e220fbb70ded | 89 | |
ivo_david_michelle | 23:04338a5ef404 | 90 | // TODO print values to check what they are |
ivo_david_michelle | 23:04338a5ef404 | 91 | // TODO convert to Radians (*pi/180) |
ivo_david_michelle | 24:e220fbb70ded | 92 | |
ivo_david_michelle | 25:d44610851105 | 93 | // 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 | 23:04338a5ef404 | 94 | state_.phi = orientation_.roll * M_PI / 180; |
ivo_david_michelle | 24:e220fbb70ded | 95 | state_.theta = orientation_.pitch * M_PI / 180; |
ivo_david_michelle | 24:e220fbb70ded | 96 | state_.psi = orientation_.heading * M_PI / 180; |
ivo_david_michelle | 24:e220fbb70ded | 97 | //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 | 10:e7d1801e966a | 98 | } |
ivo_david_michelle | 9:f1bd96708a21 | 99 | |
ivo_david_michelle | 10:e7d1801e966a | 100 | // Date member function |
ivo_david_michelle | 10:e7d1801e966a | 101 | void Quadcopter::setState(state *source, state *goal) |
ivo_david_michelle | 10:e7d1801e966a | 102 | { |
ivo_david_michelle | 10:e7d1801e966a | 103 | goal->phi = source->phi; |
ivo_david_michelle | 10:e7d1801e966a | 104 | goal->theta = source->theta; |
ivo_david_michelle | 10:e7d1801e966a | 105 | goal->psi = source->psi; |
ivo_david_michelle | 10:e7d1801e966a | 106 | goal->p = source->p; |
ivo_david_michelle | 10:e7d1801e966a | 107 | goal->q = source->q; |
ivo_david_michelle | 10:e7d1801e966a | 108 | goal->r = source->r; |
ivo_david_michelle | 9:f1bd96708a21 | 109 | } |
ivo_david_michelle | 9:f1bd96708a21 | 110 | |
ivo_david_michelle | 10:e7d1801e966a | 111 | void Quadcopter::controller() |
ivo_david_michelle | 10:e7d1801e966a | 112 | { |
ivo_david_michelle | 10:e7d1801e966a | 113 | // compute desired angles (in the case we decide not to set |
ivo_david_michelle | 10:e7d1801e966a | 114 | // the angles, but for instance the velocity with the Joystick |
ivo_david_michelle | 9:f1bd96708a21 | 115 | |
ivo_david_michelle | 10:e7d1801e966a | 116 | // PD controller |
ivo_david_michelle | 21:336faf452989 | 117 | controlInput_.f = kp_f_*F_des_;//m_*g_ + F_des_; |
ivo_david_michelle | 10:e7d1801e966a | 118 | controlInput_.mx = kp_phi_*(desiredState_.phi-state_.phi)+kd_phi_*(desiredState_.p-state_.p); |
ivo_david_michelle | 10:e7d1801e966a | 119 | controlInput_.my = kp_theta_*(desiredState_.theta-state_.theta)+kd_theta_*(desiredState_.q-state_.q); |
ivo_david_michelle | 10:e7d1801e966a | 120 | controlInput_.mz = kp_psi_*(desiredState_.psi-state_.psi)+kd_psi_*(desiredState_.r-state_.r); |
ivo_david_michelle | 14:64b06476d943 | 121 | //print("Calculated Control"); |
ivo_david_michelle | 10:e7d1801e966a | 122 | |
ivo_david_michelle | 10:e7d1801e966a | 123 | //print("F: %f M_x: %f M_y: %f M_z: %f\n\r", controlInput_.f, controlInput_.mz, controlInput_.my, controlInput_.mz); |
ivo_david_michelle | 14:64b06476d943 | 124 | // pc_->printf("F: %f\n\r", F); |
ivo_david_michelle | 24:e220fbb70ded | 125 | |
ivo_david_michelle | 20:efa15ed008b4 | 126 | // set pwm values |
ivo_david_michelle | 20:efa15ed008b4 | 127 | // make code faster by precomputing all the components that are used multiple times and hardcode 0.25/gamma... |
ivo_david_michelle | 22:92401a4fec13 | 128 | double zeroVeloPwm=0.1; |
ivo_david_michelle | 22:92401a4fec13 | 129 | motorPwm_.m1=zeroVeloPwm+ 0.25*controlInput_.f-0.5/l_*controlInput_.my-0.25/gamma_*controlInput_.mz; |
ivo_david_michelle | 24:e220fbb70ded | 130 | motorPwm_.m2=zeroVeloPwm +0.25*controlInput_.f-0.5/l_*controlInput_.mx+0.25/gamma_*controlInput_.mz; |
ivo_david_michelle | 22:92401a4fec13 | 131 | motorPwm_.m3=zeroVeloPwm + 0.25*controlInput_.f+0.5/l_*controlInput_.my-0.25/gamma_*controlInput_.mz; |
ivo_david_michelle | 22:92401a4fec13 | 132 | motorPwm_.m4=zeroVeloPwm + 0.25*controlInput_.f+0.5/l_*controlInput_.mx+0.25/gamma_*controlInput_.mz; |
ivo_david_michelle | 13:291ba30c7806 | 133 | } |
ivo_david_michelle | 10:e7d1801e966a | 134 | |
ivo_david_michelle | 12:422963993df5 | 135 | motors Quadcopter::getPwm() |
ivo_david_michelle | 11:5c54826d23a7 | 136 | { |
ivo_david_michelle | 22:92401a4fec13 | 137 | return motorPwm_; |
ivo_david_michelle | 22:92401a4fec13 | 138 | } |
ivo_david_michelle | 20:efa15ed008b4 | 139 | |
ivo_david_michelle | 22:92401a4fec13 | 140 | state Quadcopter::getState() |
ivo_david_michelle | 22:92401a4fec13 | 141 | { |
ivo_david_michelle | 22:92401a4fec13 | 142 | return state_; |
ivo_david_michelle | 11:5c54826d23a7 | 143 | } |
ivo_david_michelle | 14:64b06476d943 | 144 | |
ivo_david_michelle | 24:e220fbb70ded | 145 | Adafruit_LSM303_Accel_Unified Quadcopter::getAccel() |
ivo_david_michelle | 24:e220fbb70ded | 146 | { |
ivo_david_michelle | 24:e220fbb70ded | 147 | return accel_; |
ivo_david_michelle | 24:e220fbb70ded | 148 | } |
ivo_david_michelle | 24:e220fbb70ded | 149 | |
ivo_david_michelle | 24:e220fbb70ded | 150 | Adafruit_LSM303_Mag_Unified Quadcopter::getMag() |
ivo_david_michelle | 24:e220fbb70ded | 151 | { |
ivo_david_michelle | 24:e220fbb70ded | 152 | return mag_; |
ivo_david_michelle | 24:e220fbb70ded | 153 | } |
ivo_david_michelle | 24:e220fbb70ded | 154 | |
ivo_david_michelle | 24:e220fbb70ded | 155 | Adafruit_L3GD20_Unified Quadcopter::getGyro() |
ivo_david_michelle | 24:e220fbb70ded | 156 | { |
ivo_david_michelle | 24:e220fbb70ded | 157 | return gyro_; |
ivo_david_michelle | 24:e220fbb70ded | 158 | } |
ivo_david_michelle | 24:e220fbb70ded | 159 | |
ivo_david_michelle | 24:e220fbb70ded | 160 | offset* Quadcopter::getOffset() |
ivo_david_michelle | 24:e220fbb70ded | 161 | { |
ivo_david_michelle | 24:e220fbb70ded | 162 | return initial_offsets_; |
ivo_david_michelle | 24:e220fbb70ded | 163 | } |
ivo_david_michelle | 24:e220fbb70ded | 164 | |
ivo_david_michelle | 24:e220fbb70ded | 165 | Adafruit_9DOF Quadcopter::getIMU() |
ivo_david_michelle | 24:e220fbb70ded | 166 | { |
ivo_david_michelle | 24:e220fbb70ded | 167 | return dof_; |
ivo_david_michelle | 24:e220fbb70ded | 168 | } |
ivo_david_michelle | 24:e220fbb70ded | 169 | |
ivo_david_michelle | 24:e220fbb70ded | 170 | void Quadcopter::readRc() |
ivo_david_michelle | 24:e220fbb70ded | 171 | { |
ivo_david_michelle | 14:64b06476d943 | 172 | uint8_t zero = 0; |
ivo_david_michelle | 14:64b06476d943 | 173 | uint8_t *rssi = &zero; |
ivo_david_michelle | 14:64b06476d943 | 174 | |
ivo_david_michelle | 14:64b06476d943 | 175 | uint8_t receive = 0; |
ivo_david_michelle | 14:64b06476d943 | 176 | |
ivo_david_michelle | 14:64b06476d943 | 177 | char rxBuffer[rcLength_]; |
ivo_david_michelle | 24:e220fbb70ded | 178 | |
ivo_david_michelle | 24:e220fbb70ded | 179 | |
ivo_david_michelle | 16:2be2aab63198 | 180 | receive = rf_receive_rssi(*mrf_, rxBuffer, rssi, rcLength_ + 1); |
ivo_david_michelle | 16:2be2aab63198 | 181 | if (receive > 0) { |
ivo_david_michelle | 17:96d0c72e413e | 182 | sscanf(rxBuffer, "%lld,%f,%f,%f,%f", &id, &thrust, &yaw, &pitch, &roll); |
ivo_david_michelle | 16:2be2aab63198 | 183 | } else { |
ivo_david_michelle | 16:2be2aab63198 | 184 | pc_->printf("Receive failure\r\n"); |
ivo_david_michelle | 24:e220fbb70ded | 185 | } |
ivo_david_michelle | 24:e220fbb70ded | 186 | |
ivo_david_michelle | 25:d44610851105 | 187 | // convert to radians, range is = +-40° or +-0.698132 radians |
ivo_david_michelle | 25:d44610851105 | 188 | desiredState_.phi = ((roll - 0.5) * 80) * M_PI / 180; |
ivo_david_michelle | 25:d44610851105 | 189 | desiredState_.theta = ((pitch - 0.5) * 80) * M_PI / 180; |
ivo_david_michelle | 25:d44610851105 | 190 | desiredState_.psi = ((yaw - 0.5) * 80) * M_PI / 180; |
ivo_david_michelle | 25:d44610851105 | 191 | F_des_ = ((thrust - 0.5) * 80) * M_PI / 180; |
ivo_david_michelle | 25:d44610851105 | 192 | |
ivo_david_michelle | 25:d44610851105 | 193 | // print id with thrust, yaw, pitch, and roll |
ivo_david_michelle | 25:d44610851105 | 194 | 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 | 195 | } |
ivo_david_michelle | 14:64b06476d943 | 196 |