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