ESE350 project, Spring 2016, University of Pennsylvania

Dependencies:   Adafruit9-DOf Receiver mbed-rtos mbed

Committer:
ivo_david_michelle
Date:
Sat Apr 02 18:01:36 2016 +0000
Revision:
11:5c54826d23a7
Parent:
10:e7d1801e966a
Child:
12:422963993df5
still one compiler error;

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 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 9:f1bd96708a21 29 }
ivo_david_michelle 9:f1bd96708a21 30
ivo_david_michelle 9:f1bd96708a21 31 void Quadcopter::initAllSensors()
ivo_david_michelle 9:f1bd96708a21 32 {
ivo_david_michelle 9:f1bd96708a21 33 initSensors(accel_, mag_, gyro_,offsetAngRate_); // IMU
ivo_david_michelle 6:6f3ffd97d808 34 }
ivo_david_michelle 9:f1bd96708a21 35
ivo_david_michelle 9:f1bd96708a21 36 void Quadcopter::readSensorValues()
ivo_david_michelle 9:f1bd96708a21 37 {
ivo_david_michelle 9:f1bd96708a21 38 accel_.getEvent(&accel_event_);
ivo_david_michelle 9:f1bd96708a21 39 if (dof_.accelGetOrientation(&accel_event_, &orientation_)) {
ivo_david_michelle 9:f1bd96708a21 40 }
ivo_david_michelle 9:f1bd96708a21 41 /* Calculate the heading using the magnetometer */
ivo_david_michelle 9:f1bd96708a21 42 mag_.getEvent(&mag_event_);
ivo_david_michelle 9:f1bd96708a21 43 if (dof_.magGetOrientation(SENSOR_AXIS_Z, &mag_event_, &orientation_)) {
ivo_david_michelle 9:f1bd96708a21 44 }
ivo_david_michelle 9:f1bd96708a21 45
ivo_david_michelle 11:5c54826d23a7 46 gyro_.getEvent(&gyro_event_);
ivo_david_michelle 11:5c54826d23a7 47
ivo_david_michelle 11:5c54826d23a7 48 gyro_event_.gyro.x -= offsetAngRate_.x;
ivo_david_michelle 11:5c54826d23a7 49 gyro_event_.gyro.y -= offsetAngRate_.y;
ivo_david_michelle 11:5c54826d23a7 50 gyro_event_.gyro.z -= offsetAngRate_.z;
ivo_david_michelle 11:5c54826d23a7 51
ivo_david_michelle 11:5c54826d23a7 52
ivo_david_michelle 10:e7d1801e966a 53 // measured values (will come from IMU/parameter class/Input to function later)
ivo_david_michelle 10:e7d1801e966a 54 // angles
ivo_david_michelle 10:e7d1801e966a 55 state_.phi = orientation_.roll;
ivo_david_michelle 10:e7d1801e966a 56 state_.theta =orientation_.pitch;
ivo_david_michelle 10:e7d1801e966a 57 state_.psi =orientation_.heading;
ivo_david_michelle 10:e7d1801e966a 58 // angular velocities in body coordinate system
ivo_david_michelle 10:e7d1801e966a 59 state_.p = gyro_event_.gyro.x;
ivo_david_michelle 10:e7d1801e966a 60 state_.q = gyro_event_.gyro.y;
ivo_david_michelle 10:e7d1801e966a 61 state_.r = gyro_event_.gyro.z;
ivo_david_michelle 10:e7d1801e966a 62 }
ivo_david_michelle 9:f1bd96708a21 63
ivo_david_michelle 10:e7d1801e966a 64 // Date member function
ivo_david_michelle 10:e7d1801e966a 65 void Quadcopter::setState(state *source, state *goal)
ivo_david_michelle 10:e7d1801e966a 66 {
ivo_david_michelle 10:e7d1801e966a 67 goal->phi = source->phi;
ivo_david_michelle 10:e7d1801e966a 68 goal->theta = source->theta;
ivo_david_michelle 10:e7d1801e966a 69 goal->psi = source->psi;
ivo_david_michelle 10:e7d1801e966a 70 goal->p = source->p;
ivo_david_michelle 10:e7d1801e966a 71 goal->q = source->q;
ivo_david_michelle 10:e7d1801e966a 72 goal->r = source->r;
ivo_david_michelle 9:f1bd96708a21 73
ivo_david_michelle 9:f1bd96708a21 74 }
ivo_david_michelle 9:f1bd96708a21 75
ivo_david_michelle 10:e7d1801e966a 76 void Quadcopter::print(char * myString)
ivo_david_michelle 6:6f3ffd97d808 77 {
ivo_david_michelle 10:e7d1801e966a 78 pcPntr_->printf(myString);
ivo_david_michelle 10:e7d1801e966a 79 pcPntr_->printf("\n\r");
ivo_david_michelle 9:f1bd96708a21 80 }
ivo_david_michelle 9:f1bd96708a21 81
ivo_david_michelle 10:e7d1801e966a 82 void Quadcopter::controller()
ivo_david_michelle 10:e7d1801e966a 83 {
ivo_david_michelle 10:e7d1801e966a 84
ivo_david_michelle 10:e7d1801e966a 85 // compute desired angles (in the case we decide not to set
ivo_david_michelle 10:e7d1801e966a 86 // the angles, but for instance the velocity with the Joystick
ivo_david_michelle 9:f1bd96708a21 87
ivo_david_michelle 10:e7d1801e966a 88 // PD controller
ivo_david_michelle 10:e7d1801e966a 89 controlInput_.f = m_*g_ + F_des_;
ivo_david_michelle 10:e7d1801e966a 90 controlInput_.mx = kp_phi_*(desiredState_.phi-state_.phi)+kd_phi_*(desiredState_.p-state_.p);
ivo_david_michelle 10:e7d1801e966a 91 controlInput_.my = kp_theta_*(desiredState_.theta-state_.theta)+kd_theta_*(desiredState_.q-state_.q);
ivo_david_michelle 10:e7d1801e966a 92 controlInput_.mz = kp_psi_*(desiredState_.psi-state_.psi)+kd_psi_*(desiredState_.r-state_.r);
ivo_david_michelle 10:e7d1801e966a 93 print("Calculated Control");
ivo_david_michelle 10:e7d1801e966a 94
ivo_david_michelle 10:e7d1801e966a 95 //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 96 // pc.printf("F: %f\n\r", F);
ivo_david_michelle 10:e7d1801e966a 97
ivo_david_michelle 10:e7d1801e966a 98
ivo_david_michelle 10:e7d1801e966a 99
ivo_david_michelle 11:5c54826d23a7 100 }
ivo_david_michelle 11:5c54826d23a7 101
ivo_david_michelle 11:5c54826d23a7 102 motors getPwm()
ivo_david_michelle 11:5c54826d23a7 103 {
ivo_david_michelle 11:5c54826d23a7 104 motors motorPwm_;
ivo_david_michelle 11:5c54826d23a7 105 motorPwm_.m1=0;
ivo_david_michelle 11:5c54826d23a7 106 motorPwm_.m2=0.6+controlInput_.mx/100;
ivo_david_michelle 11:5c54826d23a7 107 motorPwm_.m3=0;
ivo_david_michelle 11:5c54826d23a7 108 motorPwm_.m4=0.6-controlInput_.mx/100;
ivo_david_michelle 11:5c54826d23a7 109 return motorPwm;
ivo_david_michelle 11:5c54826d23a7 110 }