ESE350 project, Spring 2016, University of Pennsylvania

Dependencies:   Adafruit9-DOf Receiver mbed-rtos mbed

Committer:
ivo_david_michelle
Date:
Thu Apr 07 22:13:17 2016 +0000
Revision:
17:96d0c72e413e
Parent:
16:2be2aab63198
Child:
18:a00d6b065c6b
18:12 access remote control variables from main loop!

Who changed what in which revision?

UserRevisionLine numberNew 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 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 16:2be2aab63198 38 rcTimer_.start();
ivo_david_michelle 15:90e07946186f 39 mrf_ = mrfPntr; // RF tranceiver to link with handheld.
ivo_david_michelle 15:90e07946186f 40 rcLength_ = 250;
ivo_david_michelle 16:2be2aab63198 41 mrf_->SetChannel(3); //Set the Channel. 0 is default, 15 is max
ivo_david_michelle 17:96d0c72e413e 42 thrust = 0.5;
ivo_david_michelle 17:96d0c72e413e 43 yaw = 0.5;
ivo_david_michelle 17:96d0c72e413e 44 pitch = 0.5;
ivo_david_michelle 17:96d0c72e413e 45 roll = 0.5;
ivo_david_michelle 17:96d0c72e413e 46 id = -1;
ivo_david_michelle 9:f1bd96708a21 47 }
ivo_david_michelle 9:f1bd96708a21 48
ivo_david_michelle 9:f1bd96708a21 49
ivo_david_michelle 9:f1bd96708a21 50 void Quadcopter::readSensorValues()
ivo_david_michelle 9:f1bd96708a21 51 {
ivo_david_michelle 9:f1bd96708a21 52 accel_.getEvent(&accel_event_);
ivo_david_michelle 9:f1bd96708a21 53 if (dof_.accelGetOrientation(&accel_event_, &orientation_)) {
ivo_david_michelle 9:f1bd96708a21 54 }
ivo_david_michelle 9:f1bd96708a21 55 /* Calculate the heading using the magnetometer */
ivo_david_michelle 9:f1bd96708a21 56 mag_.getEvent(&mag_event_);
ivo_david_michelle 9:f1bd96708a21 57 if (dof_.magGetOrientation(SENSOR_AXIS_Z, &mag_event_, &orientation_)) {
ivo_david_michelle 9:f1bd96708a21 58 }
ivo_david_michelle 9:f1bd96708a21 59
ivo_david_michelle 13:291ba30c7806 60 gyro_.getEvent(&gyro_event_);
ivo_david_michelle 11:5c54826d23a7 61
ivo_david_michelle 13:291ba30c7806 62 gyro_event_.gyro.x -= offsetAngRate_.x;
ivo_david_michelle 13:291ba30c7806 63 gyro_event_.gyro.y -= offsetAngRate_.y;
ivo_david_michelle 13:291ba30c7806 64 gyro_event_.gyro.z -= offsetAngRate_.z;
ivo_david_michelle 13:291ba30c7806 65
ivo_david_michelle 13:291ba30c7806 66
ivo_david_michelle 10:e7d1801e966a 67 // measured values (will come from IMU/parameter class/Input to function later)
ivo_david_michelle 10:e7d1801e966a 68 // angles
ivo_david_michelle 10:e7d1801e966a 69 state_.phi = orientation_.roll;
ivo_david_michelle 10:e7d1801e966a 70 state_.theta =orientation_.pitch;
ivo_david_michelle 10:e7d1801e966a 71 state_.psi =orientation_.heading;
ivo_david_michelle 10:e7d1801e966a 72 // angular velocities in body coordinate system
ivo_david_michelle 10:e7d1801e966a 73 state_.p = gyro_event_.gyro.x;
ivo_david_michelle 10:e7d1801e966a 74 state_.q = gyro_event_.gyro.y;
ivo_david_michelle 10:e7d1801e966a 75 state_.r = gyro_event_.gyro.z;
ivo_david_michelle 10:e7d1801e966a 76 }
ivo_david_michelle 9:f1bd96708a21 77
ivo_david_michelle 10:e7d1801e966a 78 // Date member function
ivo_david_michelle 10:e7d1801e966a 79 void Quadcopter::setState(state *source, state *goal)
ivo_david_michelle 10:e7d1801e966a 80 {
ivo_david_michelle 10:e7d1801e966a 81 goal->phi = source->phi;
ivo_david_michelle 10:e7d1801e966a 82 goal->theta = source->theta;
ivo_david_michelle 10:e7d1801e966a 83 goal->psi = source->psi;
ivo_david_michelle 10:e7d1801e966a 84 goal->p = source->p;
ivo_david_michelle 10:e7d1801e966a 85 goal->q = source->q;
ivo_david_michelle 10:e7d1801e966a 86 goal->r = source->r;
ivo_david_michelle 9:f1bd96708a21 87
ivo_david_michelle 9:f1bd96708a21 88 }
ivo_david_michelle 9:f1bd96708a21 89
ivo_david_michelle 9:f1bd96708a21 90
ivo_david_michelle 10:e7d1801e966a 91 void Quadcopter::controller()
ivo_david_michelle 10:e7d1801e966a 92 {
ivo_david_michelle 10:e7d1801e966a 93
ivo_david_michelle 10:e7d1801e966a 94 // compute desired angles (in the case we decide not to set
ivo_david_michelle 10:e7d1801e966a 95 // the angles, but for instance the velocity with the Joystick
ivo_david_michelle 9:f1bd96708a21 96
ivo_david_michelle 10:e7d1801e966a 97 // PD controller
ivo_david_michelle 10:e7d1801e966a 98 controlInput_.f = m_*g_ + F_des_;
ivo_david_michelle 10:e7d1801e966a 99 controlInput_.mx = kp_phi_*(desiredState_.phi-state_.phi)+kd_phi_*(desiredState_.p-state_.p);
ivo_david_michelle 10:e7d1801e966a 100 controlInput_.my = kp_theta_*(desiredState_.theta-state_.theta)+kd_theta_*(desiredState_.q-state_.q);
ivo_david_michelle 10:e7d1801e966a 101 controlInput_.mz = kp_psi_*(desiredState_.psi-state_.psi)+kd_psi_*(desiredState_.r-state_.r);
ivo_david_michelle 14:64b06476d943 102 //print("Calculated Control");
ivo_david_michelle 10:e7d1801e966a 103
ivo_david_michelle 10:e7d1801e966a 104 //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 105 // pc_->printf("F: %f\n\r", F);
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 12:422963993df5 110 //motors motorPwm_;// weired errror, should not be necessary
ivo_david_michelle 11:5c54826d23a7 111 motorPwm_.m1=0;
ivo_david_michelle 14:64b06476d943 112 motorPwm_.m2=0.5 + controlInput_.mx/100;
ivo_david_michelle 11:5c54826d23a7 113 motorPwm_.m3=0;
ivo_david_michelle 14:64b06476d943 114 motorPwm_.m4=0.5 - controlInput_.mx/100;
ivo_david_michelle 12:422963993df5 115 return motorPwm_;
ivo_david_michelle 11:5c54826d23a7 116 }
ivo_david_michelle 14:64b06476d943 117
ivo_david_michelle 14:64b06476d943 118
ivo_david_michelle 16:2be2aab63198 119 void Quadcopter::readRc() {
ivo_david_michelle 14:64b06476d943 120 uint8_t zero = 0;
ivo_david_michelle 14:64b06476d943 121 uint8_t *rssi = &zero;
ivo_david_michelle 14:64b06476d943 122
ivo_david_michelle 14:64b06476d943 123 uint8_t receive = 0;
ivo_david_michelle 14:64b06476d943 124
ivo_david_michelle 14:64b06476d943 125 char rxBuffer[rcLength_];
ivo_david_michelle 14:64b06476d943 126
ivo_david_michelle 16:2be2aab63198 127 receive = rf_receive_rssi(*mrf_, rxBuffer, rssi, rcLength_ + 1);
ivo_david_michelle 16:2be2aab63198 128 if (receive > 0) {
ivo_david_michelle 17:96d0c72e413e 129 sscanf(rxBuffer, "%lld,%f,%f,%f,%f", &id, &thrust, &yaw, &pitch, &roll);
ivo_david_michelle 16:2be2aab63198 130 } else {
ivo_david_michelle 16:2be2aab63198 131 pc_->printf("Receive failure\r\n");
ivo_david_michelle 16:2be2aab63198 132 }
ivo_david_michelle 14:64b06476d943 133 }
ivo_david_michelle 14:64b06476d943 134