ESE350 project, Spring 2016, University of Pennsylvania
Dependencies: Adafruit9-DOf Receiver mbed-rtos mbed
quadcopter.cpp@14:64b06476d943, 2016-04-07 (annotated)
- Committer:
- ivo_david_michelle
- Date:
- Thu Apr 07 02:07:33 2016 +0000
- Revision:
- 14:64b06476d943
- Parent:
- 13:291ba30c7806
- Child:
- 15:90e07946186f
msoch update - compiles!
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 | 14:64b06476d943 | 9 | Quadcopter::Quadcopter(Serial *pcPntr) |
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 | 10:e7d1801e966a | 16 | // proportional attitude control gains |
ivo_david_michelle | 10:e7d1801e966a | 17 | kp_phi_ = 1; |
ivo_david_michelle | 10:e7d1801e966a | 18 | kp_theta_ = 1; |
ivo_david_michelle | 10:e7d1801e966a | 19 | kp_psi_ = 1; |
ivo_david_michelle | 10:e7d1801e966a | 20 | |
ivo_david_michelle | 10:e7d1801e966a | 21 | // derivative attitude control gains |
ivo_david_michelle | 10:e7d1801e966a | 22 | kd_phi_ = 0; |
ivo_david_michelle | 10:e7d1801e966a | 23 | kd_theta_ = 0; |
ivo_david_michelle | 10:e7d1801e966a | 24 | kd_psi_ = 0; |
ivo_david_michelle | 10:e7d1801e966a | 25 | |
ivo_david_michelle | 10:e7d1801e966a | 26 | // desired values (will come from joystick) |
ivo_david_michelle | 10:e7d1801e966a | 27 | F_des_ = 0; // desired thrust force (excluding weight compensation) |
ivo_david_michelle | 9:f1bd96708a21 | 28 | |
ivo_david_michelle | 9:f1bd96708a21 | 29 | dof_ = Adafruit_9DOF(); |
ivo_david_michelle | 9:f1bd96708a21 | 30 | accel_ = Adafruit_LSM303_Accel_Unified(30301); |
ivo_david_michelle | 9:f1bd96708a21 | 31 | mag_ = Adafruit_LSM303_Mag_Unified(30302); |
ivo_david_michelle | 9:f1bd96708a21 | 32 | gyro_ = Adafruit_L3GD20_Unified(20); |
ivo_david_michelle | 14:64b06476d943 | 33 | //motor1_(p21); |
ivo_david_michelle | 14:64b06476d943 | 34 | |
ivo_david_michelle | 14:64b06476d943 | 35 | // initSensors(accel_, mag_, gyro_, offsetAngRate_); // IMU |
ivo_david_michelle | 14:64b06476d943 | 36 | |
ivo_david_michelle | 14:64b06476d943 | 37 | // prepare for communication with remote control |
ivo_david_michelle | 14:64b06476d943 | 38 | MRF24J40 mrf_var(p11, p12, p13, p14, p21); |
ivo_david_michelle | 14:64b06476d943 | 39 | mrf_ = &mrf_var; // RF tranceiver to link with handheld. |
ivo_david_michelle | 14:64b06476d943 | 40 | rcIterations_ = 100; |
ivo_david_michelle | 14:64b06476d943 | 41 | rcLength_ = 50; |
ivo_david_michelle | 14:64b06476d943 | 42 | rcChannel_ = 3; |
ivo_david_michelle | 9:f1bd96708a21 | 43 | } |
ivo_david_michelle | 9:f1bd96708a21 | 44 | |
ivo_david_michelle | 9:f1bd96708a21 | 45 | |
ivo_david_michelle | 9:f1bd96708a21 | 46 | void Quadcopter::readSensorValues() |
ivo_david_michelle | 9:f1bd96708a21 | 47 | { |
ivo_david_michelle | 9:f1bd96708a21 | 48 | accel_.getEvent(&accel_event_); |
ivo_david_michelle | 9:f1bd96708a21 | 49 | if (dof_.accelGetOrientation(&accel_event_, &orientation_)) { |
ivo_david_michelle | 9:f1bd96708a21 | 50 | } |
ivo_david_michelle | 9:f1bd96708a21 | 51 | /* Calculate the heading using the magnetometer */ |
ivo_david_michelle | 9:f1bd96708a21 | 52 | mag_.getEvent(&mag_event_); |
ivo_david_michelle | 9:f1bd96708a21 | 53 | if (dof_.magGetOrientation(SENSOR_AXIS_Z, &mag_event_, &orientation_)) { |
ivo_david_michelle | 9:f1bd96708a21 | 54 | } |
ivo_david_michelle | 9:f1bd96708a21 | 55 | |
ivo_david_michelle | 13:291ba30c7806 | 56 | gyro_.getEvent(&gyro_event_); |
ivo_david_michelle | 11:5c54826d23a7 | 57 | |
ivo_david_michelle | 13:291ba30c7806 | 58 | gyro_event_.gyro.x -= offsetAngRate_.x; |
ivo_david_michelle | 13:291ba30c7806 | 59 | gyro_event_.gyro.y -= offsetAngRate_.y; |
ivo_david_michelle | 13:291ba30c7806 | 60 | gyro_event_.gyro.z -= offsetAngRate_.z; |
ivo_david_michelle | 13:291ba30c7806 | 61 | |
ivo_david_michelle | 13:291ba30c7806 | 62 | |
ivo_david_michelle | 10:e7d1801e966a | 63 | // measured values (will come from IMU/parameter class/Input to function later) |
ivo_david_michelle | 10:e7d1801e966a | 64 | // angles |
ivo_david_michelle | 10:e7d1801e966a | 65 | state_.phi = orientation_.roll; |
ivo_david_michelle | 10:e7d1801e966a | 66 | state_.theta =orientation_.pitch; |
ivo_david_michelle | 10:e7d1801e966a | 67 | state_.psi =orientation_.heading; |
ivo_david_michelle | 10:e7d1801e966a | 68 | // angular velocities in body coordinate system |
ivo_david_michelle | 10:e7d1801e966a | 69 | state_.p = gyro_event_.gyro.x; |
ivo_david_michelle | 10:e7d1801e966a | 70 | state_.q = gyro_event_.gyro.y; |
ivo_david_michelle | 10:e7d1801e966a | 71 | state_.r = gyro_event_.gyro.z; |
ivo_david_michelle | 10:e7d1801e966a | 72 | } |
ivo_david_michelle | 9:f1bd96708a21 | 73 | |
ivo_david_michelle | 10:e7d1801e966a | 74 | // Date member function |
ivo_david_michelle | 10:e7d1801e966a | 75 | void Quadcopter::setState(state *source, state *goal) |
ivo_david_michelle | 10:e7d1801e966a | 76 | { |
ivo_david_michelle | 10:e7d1801e966a | 77 | goal->phi = source->phi; |
ivo_david_michelle | 10:e7d1801e966a | 78 | goal->theta = source->theta; |
ivo_david_michelle | 10:e7d1801e966a | 79 | goal->psi = source->psi; |
ivo_david_michelle | 10:e7d1801e966a | 80 | goal->p = source->p; |
ivo_david_michelle | 10:e7d1801e966a | 81 | goal->q = source->q; |
ivo_david_michelle | 10:e7d1801e966a | 82 | goal->r = source->r; |
ivo_david_michelle | 9:f1bd96708a21 | 83 | |
ivo_david_michelle | 9:f1bd96708a21 | 84 | } |
ivo_david_michelle | 9:f1bd96708a21 | 85 | |
ivo_david_michelle | 9:f1bd96708a21 | 86 | |
ivo_david_michelle | 10:e7d1801e966a | 87 | void Quadcopter::controller() |
ivo_david_michelle | 10:e7d1801e966a | 88 | { |
ivo_david_michelle | 10:e7d1801e966a | 89 | |
ivo_david_michelle | 10:e7d1801e966a | 90 | // compute desired angles (in the case we decide not to set |
ivo_david_michelle | 10:e7d1801e966a | 91 | // the angles, but for instance the velocity with the Joystick |
ivo_david_michelle | 9:f1bd96708a21 | 92 | |
ivo_david_michelle | 10:e7d1801e966a | 93 | // PD controller |
ivo_david_michelle | 10:e7d1801e966a | 94 | controlInput_.f = m_*g_ + F_des_; |
ivo_david_michelle | 10:e7d1801e966a | 95 | controlInput_.mx = kp_phi_*(desiredState_.phi-state_.phi)+kd_phi_*(desiredState_.p-state_.p); |
ivo_david_michelle | 10:e7d1801e966a | 96 | controlInput_.my = kp_theta_*(desiredState_.theta-state_.theta)+kd_theta_*(desiredState_.q-state_.q); |
ivo_david_michelle | 10:e7d1801e966a | 97 | controlInput_.mz = kp_psi_*(desiredState_.psi-state_.psi)+kd_psi_*(desiredState_.r-state_.r); |
ivo_david_michelle | 14:64b06476d943 | 98 | //print("Calculated Control"); |
ivo_david_michelle | 10:e7d1801e966a | 99 | |
ivo_david_michelle | 10:e7d1801e966a | 100 | //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 | 101 | // pc_->printf("F: %f\n\r", F); |
ivo_david_michelle | 13:291ba30c7806 | 102 | } |
ivo_david_michelle | 10:e7d1801e966a | 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 | 14:64b06476d943 | 108 | motorPwm_.m2=0.5 + controlInput_.mx/100; |
ivo_david_michelle | 11:5c54826d23a7 | 109 | motorPwm_.m3=0; |
ivo_david_michelle | 14:64b06476d943 | 110 | motorPwm_.m4=0.5 - controlInput_.mx/100; |
ivo_david_michelle | 12:422963993df5 | 111 | return motorPwm_; |
ivo_david_michelle | 11:5c54826d23a7 | 112 | } |
ivo_david_michelle | 14:64b06476d943 | 113 | |
ivo_david_michelle | 14:64b06476d943 | 114 | |
ivo_david_michelle | 14:64b06476d943 | 115 | void Quadcopter::readRc() { |
ivo_david_michelle | 14:64b06476d943 | 116 | |
ivo_david_michelle | 14:64b06476d943 | 117 | //write to class variables additional thrust double F_des_, desired state: phi, theta, psi. |
ivo_david_michelle | 14:64b06476d943 | 118 | |
ivo_david_michelle | 14:64b06476d943 | 119 | // RF tranceiver to link with handheld. |
ivo_david_michelle | 14:64b06476d943 | 120 | |
ivo_david_michelle | 14:64b06476d943 | 121 | // read from joystick: |
ivo_david_michelle | 14:64b06476d943 | 122 | |
ivo_david_michelle | 14:64b06476d943 | 123 | //write to class variables additional thrust double F_des_, desired state: phi, theta, psi. |
ivo_david_michelle | 14:64b06476d943 | 124 | |
ivo_david_michelle | 14:64b06476d943 | 125 | uint8_t zero = 0; |
ivo_david_michelle | 14:64b06476d943 | 126 | uint8_t *rssi = &zero; |
ivo_david_michelle | 14:64b06476d943 | 127 | |
ivo_david_michelle | 14:64b06476d943 | 128 | uint8_t receive = 0; |
ivo_david_michelle | 14:64b06476d943 | 129 | int result = 0; |
ivo_david_michelle | 14:64b06476d943 | 130 | |
ivo_david_michelle | 14:64b06476d943 | 131 | //Set the Channel. 0 is default, 15 is max |
ivo_david_michelle | 14:64b06476d943 | 132 | mrf_->SetChannel(rcChannel_); |
ivo_david_michelle | 14:64b06476d943 | 133 | |
ivo_david_michelle | 14:64b06476d943 | 134 | //Start the timer |
ivo_david_michelle | 14:64b06476d943 | 135 | rcTimer_.start(); |
ivo_david_michelle | 14:64b06476d943 | 136 | |
ivo_david_michelle | 14:64b06476d943 | 137 | pc_->printf("START\r\n"); |
ivo_david_michelle | 14:64b06476d943 | 138 | |
ivo_david_michelle | 14:64b06476d943 | 139 | // TODO: change default value? |
ivo_david_michelle | 14:64b06476d943 | 140 | float thrust = 0.5; |
ivo_david_michelle | 14:64b06476d943 | 141 | float yaw = 0.5; |
ivo_david_michelle | 14:64b06476d943 | 142 | float pitch = 0.5; |
ivo_david_michelle | 14:64b06476d943 | 143 | float roll = 0.5; |
ivo_david_michelle | 14:64b06476d943 | 144 | |
ivo_david_michelle | 14:64b06476d943 | 145 | char rxBuffer[rcLength_]; |
ivo_david_michelle | 14:64b06476d943 | 146 | |
ivo_david_michelle | 14:64b06476d943 | 147 | // TODO: stop doing iterations, just do while recieving from slave |
ivo_david_michelle | 14:64b06476d943 | 148 | for (int i = 0; i < rcIterations_; i++) { |
ivo_david_michelle | 14:64b06476d943 | 149 | receive = rf_receive_rssi(*mrf_, rxBuffer, rssi, rcLength_ + 1); |
ivo_david_michelle | 14:64b06476d943 | 150 | if (receive > 0) { |
ivo_david_michelle | 14:64b06476d943 | 151 | pc_->printf("%s \r\n", rxBuffer); |
ivo_david_michelle | 14:64b06476d943 | 152 | result = sscanf(rxBuffer, "%f,%f,%f,%f", &thrust, &yaw, &pitch, &roll); |
ivo_david_michelle | 14:64b06476d943 | 153 | if (result != 4) { |
ivo_david_michelle | 14:64b06476d943 | 154 | pc_->printf("Parse failure\r\n"); |
ivo_david_michelle | 14:64b06476d943 | 155 | } else { |
ivo_david_michelle | 14:64b06476d943 | 156 | pc_->printf("%f,%f,%f,%f\r\n", thrust, yaw, pitch, roll); |
ivo_david_michelle | 14:64b06476d943 | 157 | } |
ivo_david_michelle | 14:64b06476d943 | 158 | } else { |
ivo_david_michelle | 14:64b06476d943 | 159 | pc_->printf("Receive failure\r\n"); |
ivo_david_michelle | 14:64b06476d943 | 160 | } |
ivo_david_michelle | 14:64b06476d943 | 161 | wait(0.2); // TODO: change this value? |
ivo_david_michelle | 14:64b06476d943 | 162 | } |
ivo_david_michelle | 14:64b06476d943 | 163 | pc_->printf("END\r\n"); |
ivo_david_michelle | 14:64b06476d943 | 164 | } |
ivo_david_michelle | 14:64b06476d943 | 165 |