ESE350 project, Spring 2016, University of Pennsylvania
Dependencies: Adafruit9-DOf Receiver mbed-rtos mbed
quadcopter.cpp@12:422963993df5, 2016-04-02 (annotated)
- Committer:
- ivo_david_michelle
- Date:
- Sat Apr 02 21:40:37 2016 +0000
- Revision:
- 12:422963993df5
- Parent:
- 11:5c54826d23a7
- Child:
- 13:291ba30c7806
working stand with x axis control and quadcopter class
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 | 9:f1bd96708a21 | 3 | #include "Adafruit_9DOF.h" |
ivo_david_michelle | 9:f1bd96708a21 | 4 | #include "Serial_base.h" |
ivo_david_michelle | 9:f1bd96708a21 | 5 | |
ivo_david_michelle | 9:f1bd96708a21 | 6 | |
ivo_david_michelle | 10:e7d1801e966a | 7 | // constructor |
ivo_david_michelle | 6:6f3ffd97d808 | 8 | Quadcopter::Quadcopter() |
ivo_david_michelle | 6:6f3ffd97d808 | 9 | { |
ivo_david_michelle | 10:e7d1801e966a | 10 | m_=1; |
ivo_david_michelle | 10:e7d1801e966a | 11 | g_=9.81; |
ivo_david_michelle | 10:e7d1801e966a | 12 | // proportional attitude control gains |
ivo_david_michelle | 10:e7d1801e966a | 13 | kp_phi_ = 1; |
ivo_david_michelle | 10:e7d1801e966a | 14 | kp_theta_ = 1; |
ivo_david_michelle | 10:e7d1801e966a | 15 | kp_psi_ = 1; |
ivo_david_michelle | 10:e7d1801e966a | 16 | |
ivo_david_michelle | 10:e7d1801e966a | 17 | // derivative attitude control gains |
ivo_david_michelle | 10:e7d1801e966a | 18 | kd_phi_ = 0; |
ivo_david_michelle | 10:e7d1801e966a | 19 | kd_theta_ = 0; |
ivo_david_michelle | 10:e7d1801e966a | 20 | kd_psi_ = 0; |
ivo_david_michelle | 10:e7d1801e966a | 21 | |
ivo_david_michelle | 10:e7d1801e966a | 22 | // desired values (will come from joystick) |
ivo_david_michelle | 10:e7d1801e966a | 23 | F_des_ = 0; // desired thrust force (excluding weight compensation) |
ivo_david_michelle | 9:f1bd96708a21 | 24 | |
ivo_david_michelle | 9:f1bd96708a21 | 25 | dof_ = Adafruit_9DOF(); |
ivo_david_michelle | 9:f1bd96708a21 | 26 | accel_ = Adafruit_LSM303_Accel_Unified(30301); |
ivo_david_michelle | 9:f1bd96708a21 | 27 | mag_ = Adafruit_LSM303_Mag_Unified(30302); |
ivo_david_michelle | 9:f1bd96708a21 | 28 | gyro_ = Adafruit_L3GD20_Unified(20); |
ivo_david_michelle | 12:422963993df5 | 29 | |
ivo_david_michelle | 12:422963993df5 | 30 | |
ivo_david_michelle | 12:422963993df5 | 31 | |
ivo_david_michelle | 9:f1bd96708a21 | 32 | } |
ivo_david_michelle | 9:f1bd96708a21 | 33 | |
ivo_david_michelle | 9:f1bd96708a21 | 34 | void Quadcopter::initAllSensors() |
ivo_david_michelle | 9:f1bd96708a21 | 35 | { |
ivo_david_michelle | 9:f1bd96708a21 | 36 | initSensors(accel_, mag_, gyro_,offsetAngRate_); // IMU |
ivo_david_michelle | 6:6f3ffd97d808 | 37 | } |
ivo_david_michelle | 9:f1bd96708a21 | 38 | |
ivo_david_michelle | 9:f1bd96708a21 | 39 | void Quadcopter::readSensorValues() |
ivo_david_michelle | 9:f1bd96708a21 | 40 | { |
ivo_david_michelle | 9:f1bd96708a21 | 41 | accel_.getEvent(&accel_event_); |
ivo_david_michelle | 9:f1bd96708a21 | 42 | if (dof_.accelGetOrientation(&accel_event_, &orientation_)) { |
ivo_david_michelle | 9:f1bd96708a21 | 43 | } |
ivo_david_michelle | 9:f1bd96708a21 | 44 | /* Calculate the heading using the magnetometer */ |
ivo_david_michelle | 9:f1bd96708a21 | 45 | mag_.getEvent(&mag_event_); |
ivo_david_michelle | 9:f1bd96708a21 | 46 | if (dof_.magGetOrientation(SENSOR_AXIS_Z, &mag_event_, &orientation_)) { |
ivo_david_michelle | 9:f1bd96708a21 | 47 | } |
ivo_david_michelle | 9:f1bd96708a21 | 48 | |
ivo_david_michelle | 11:5c54826d23a7 | 49 | gyro_.getEvent(&gyro_event_); |
ivo_david_michelle | 11:5c54826d23a7 | 50 | |
ivo_david_michelle | 11:5c54826d23a7 | 51 | gyro_event_.gyro.x -= offsetAngRate_.x; |
ivo_david_michelle | 11:5c54826d23a7 | 52 | gyro_event_.gyro.y -= offsetAngRate_.y; |
ivo_david_michelle | 11:5c54826d23a7 | 53 | gyro_event_.gyro.z -= offsetAngRate_.z; |
ivo_david_michelle | 11:5c54826d23a7 | 54 | |
ivo_david_michelle | 11:5c54826d23a7 | 55 | |
ivo_david_michelle | 10:e7d1801e966a | 56 | // measured values (will come from IMU/parameter class/Input to function later) |
ivo_david_michelle | 10:e7d1801e966a | 57 | // angles |
ivo_david_michelle | 10:e7d1801e966a | 58 | state_.phi = orientation_.roll; |
ivo_david_michelle | 10:e7d1801e966a | 59 | state_.theta =orientation_.pitch; |
ivo_david_michelle | 10:e7d1801e966a | 60 | state_.psi =orientation_.heading; |
ivo_david_michelle | 10:e7d1801e966a | 61 | // angular velocities in body coordinate system |
ivo_david_michelle | 10:e7d1801e966a | 62 | state_.p = gyro_event_.gyro.x; |
ivo_david_michelle | 10:e7d1801e966a | 63 | state_.q = gyro_event_.gyro.y; |
ivo_david_michelle | 10:e7d1801e966a | 64 | state_.r = gyro_event_.gyro.z; |
ivo_david_michelle | 10:e7d1801e966a | 65 | } |
ivo_david_michelle | 9:f1bd96708a21 | 66 | |
ivo_david_michelle | 10:e7d1801e966a | 67 | // Date member function |
ivo_david_michelle | 10:e7d1801e966a | 68 | void Quadcopter::setState(state *source, state *goal) |
ivo_david_michelle | 10:e7d1801e966a | 69 | { |
ivo_david_michelle | 10:e7d1801e966a | 70 | goal->phi = source->phi; |
ivo_david_michelle | 10:e7d1801e966a | 71 | goal->theta = source->theta; |
ivo_david_michelle | 10:e7d1801e966a | 72 | goal->psi = source->psi; |
ivo_david_michelle | 10:e7d1801e966a | 73 | goal->p = source->p; |
ivo_david_michelle | 10:e7d1801e966a | 74 | goal->q = source->q; |
ivo_david_michelle | 10:e7d1801e966a | 75 | goal->r = source->r; |
ivo_david_michelle | 9:f1bd96708a21 | 76 | |
ivo_david_michelle | 9:f1bd96708a21 | 77 | } |
ivo_david_michelle | 9:f1bd96708a21 | 78 | |
ivo_david_michelle | 10:e7d1801e966a | 79 | void Quadcopter::print(char * myString) |
ivo_david_michelle | 6:6f3ffd97d808 | 80 | { |
ivo_david_michelle | 10:e7d1801e966a | 81 | pcPntr_->printf(myString); |
ivo_david_michelle | 10:e7d1801e966a | 82 | pcPntr_->printf("\n\r"); |
ivo_david_michelle | 9:f1bd96708a21 | 83 | } |
ivo_david_michelle | 9:f1bd96708a21 | 84 | |
ivo_david_michelle | 10:e7d1801e966a | 85 | void Quadcopter::controller() |
ivo_david_michelle | 10:e7d1801e966a | 86 | { |
ivo_david_michelle | 10:e7d1801e966a | 87 | |
ivo_david_michelle | 10:e7d1801e966a | 88 | // compute desired angles (in the case we decide not to set |
ivo_david_michelle | 10:e7d1801e966a | 89 | // the angles, but for instance the velocity with the Joystick |
ivo_david_michelle | 9:f1bd96708a21 | 90 | |
ivo_david_michelle | 10:e7d1801e966a | 91 | // PD controller |
ivo_david_michelle | 10:e7d1801e966a | 92 | controlInput_.f = m_*g_ + F_des_; |
ivo_david_michelle | 10:e7d1801e966a | 93 | controlInput_.mx = kp_phi_*(desiredState_.phi-state_.phi)+kd_phi_*(desiredState_.p-state_.p); |
ivo_david_michelle | 10:e7d1801e966a | 94 | controlInput_.my = kp_theta_*(desiredState_.theta-state_.theta)+kd_theta_*(desiredState_.q-state_.q); |
ivo_david_michelle | 10:e7d1801e966a | 95 | controlInput_.mz = kp_psi_*(desiredState_.psi-state_.psi)+kd_psi_*(desiredState_.r-state_.r); |
ivo_david_michelle | 10:e7d1801e966a | 96 | print("Calculated Control"); |
ivo_david_michelle | 10:e7d1801e966a | 97 | |
ivo_david_michelle | 10:e7d1801e966a | 98 | //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 | 10:e7d1801e966a | 99 | // pc.printf("F: %f\n\r", F); |
ivo_david_michelle | 10:e7d1801e966a | 100 | |
ivo_david_michelle | 10:e7d1801e966a | 101 | |
ivo_david_michelle | 10:e7d1801e966a | 102 | |
ivo_david_michelle | 11:5c54826d23a7 | 103 | } |
ivo_david_michelle | 12:422963993df5 | 104 | motors Quadcopter::getPwm() |
ivo_david_michelle | 11:5c54826d23a7 | 105 | { |
ivo_david_michelle | 12:422963993df5 | 106 | //motors motorPwm_;// weired errror, should not be necessary |
ivo_david_michelle | 11:5c54826d23a7 | 107 | motorPwm_.m1=0; |
ivo_david_michelle | 12:422963993df5 | 108 | motorPwm_.m2=0.65+controlInput_.mx/100; |
ivo_david_michelle | 11:5c54826d23a7 | 109 | motorPwm_.m3=0; |
ivo_david_michelle | 12:422963993df5 | 110 | motorPwm_.m4=0.65-controlInput_.mx/100; |
ivo_david_michelle | 12:422963993df5 | 111 | return motorPwm_; |
ivo_david_michelle | 11:5c54826d23a7 | 112 | } |