ESE350 project, Spring 2016, University of Pennsylvania

Dependencies:   Adafruit9-DOf Receiver mbed-rtos mbed

Committer:
ivo_david_michelle
Date:
Thu Apr 07 21:14:27 2016 +0000
Revision:
15:90e07946186f
Parent:
14:64b06476d943
Child:
16:2be2aab63198
communication parses - april 7 17:14

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 15:90e07946186f 38 mrf_ = mrfPntr; // RF tranceiver to link with handheld.
ivo_david_michelle 14:64b06476d943 39 rcIterations_ = 100;
ivo_david_michelle 15:90e07946186f 40 rcLength_ = 250;
ivo_david_michelle 14:64b06476d943 41 rcChannel_ = 3;
ivo_david_michelle 9:f1bd96708a21 42 }
ivo_david_michelle 9:f1bd96708a21 43
ivo_david_michelle 9:f1bd96708a21 44
ivo_david_michelle 9:f1bd96708a21 45 void Quadcopter::readSensorValues()
ivo_david_michelle 9:f1bd96708a21 46 {
ivo_david_michelle 9:f1bd96708a21 47 accel_.getEvent(&accel_event_);
ivo_david_michelle 9:f1bd96708a21 48 if (dof_.accelGetOrientation(&accel_event_, &orientation_)) {
ivo_david_michelle 9:f1bd96708a21 49 }
ivo_david_michelle 9:f1bd96708a21 50 /* Calculate the heading using the magnetometer */
ivo_david_michelle 9:f1bd96708a21 51 mag_.getEvent(&mag_event_);
ivo_david_michelle 9:f1bd96708a21 52 if (dof_.magGetOrientation(SENSOR_AXIS_Z, &mag_event_, &orientation_)) {
ivo_david_michelle 9:f1bd96708a21 53 }
ivo_david_michelle 9:f1bd96708a21 54
ivo_david_michelle 13:291ba30c7806 55 gyro_.getEvent(&gyro_event_);
ivo_david_michelle 11:5c54826d23a7 56
ivo_david_michelle 13:291ba30c7806 57 gyro_event_.gyro.x -= offsetAngRate_.x;
ivo_david_michelle 13:291ba30c7806 58 gyro_event_.gyro.y -= offsetAngRate_.y;
ivo_david_michelle 13:291ba30c7806 59 gyro_event_.gyro.z -= offsetAngRate_.z;
ivo_david_michelle 13:291ba30c7806 60
ivo_david_michelle 13:291ba30c7806 61
ivo_david_michelle 10:e7d1801e966a 62 // measured values (will come from IMU/parameter class/Input to function later)
ivo_david_michelle 10:e7d1801e966a 63 // angles
ivo_david_michelle 10:e7d1801e966a 64 state_.phi = orientation_.roll;
ivo_david_michelle 10:e7d1801e966a 65 state_.theta =orientation_.pitch;
ivo_david_michelle 10:e7d1801e966a 66 state_.psi =orientation_.heading;
ivo_david_michelle 10:e7d1801e966a 67 // angular velocities in body coordinate system
ivo_david_michelle 10:e7d1801e966a 68 state_.p = gyro_event_.gyro.x;
ivo_david_michelle 10:e7d1801e966a 69 state_.q = gyro_event_.gyro.y;
ivo_david_michelle 10:e7d1801e966a 70 state_.r = gyro_event_.gyro.z;
ivo_david_michelle 10:e7d1801e966a 71 }
ivo_david_michelle 9:f1bd96708a21 72
ivo_david_michelle 10:e7d1801e966a 73 // Date member function
ivo_david_michelle 10:e7d1801e966a 74 void Quadcopter::setState(state *source, state *goal)
ivo_david_michelle 10:e7d1801e966a 75 {
ivo_david_michelle 10:e7d1801e966a 76 goal->phi = source->phi;
ivo_david_michelle 10:e7d1801e966a 77 goal->theta = source->theta;
ivo_david_michelle 10:e7d1801e966a 78 goal->psi = source->psi;
ivo_david_michelle 10:e7d1801e966a 79 goal->p = source->p;
ivo_david_michelle 10:e7d1801e966a 80 goal->q = source->q;
ivo_david_michelle 10:e7d1801e966a 81 goal->r = source->r;
ivo_david_michelle 9:f1bd96708a21 82
ivo_david_michelle 9:f1bd96708a21 83 }
ivo_david_michelle 9:f1bd96708a21 84
ivo_david_michelle 9:f1bd96708a21 85
ivo_david_michelle 10:e7d1801e966a 86 void Quadcopter::controller()
ivo_david_michelle 10:e7d1801e966a 87 {
ivo_david_michelle 10:e7d1801e966a 88
ivo_david_michelle 10:e7d1801e966a 89 // compute desired angles (in the case we decide not to set
ivo_david_michelle 10:e7d1801e966a 90 // the angles, but for instance the velocity with the Joystick
ivo_david_michelle 9:f1bd96708a21 91
ivo_david_michelle 10:e7d1801e966a 92 // PD controller
ivo_david_michelle 10:e7d1801e966a 93 controlInput_.f = m_*g_ + F_des_;
ivo_david_michelle 10:e7d1801e966a 94 controlInput_.mx = kp_phi_*(desiredState_.phi-state_.phi)+kd_phi_*(desiredState_.p-state_.p);
ivo_david_michelle 10:e7d1801e966a 95 controlInput_.my = kp_theta_*(desiredState_.theta-state_.theta)+kd_theta_*(desiredState_.q-state_.q);
ivo_david_michelle 10:e7d1801e966a 96 controlInput_.mz = kp_psi_*(desiredState_.psi-state_.psi)+kd_psi_*(desiredState_.r-state_.r);
ivo_david_michelle 14:64b06476d943 97 //print("Calculated Control");
ivo_david_michelle 10:e7d1801e966a 98
ivo_david_michelle 10:e7d1801e966a 99 //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 100 // pc_->printf("F: %f\n\r", F);
ivo_david_michelle 13:291ba30c7806 101 }
ivo_david_michelle 10:e7d1801e966a 102
ivo_david_michelle 12:422963993df5 103 motors Quadcopter::getPwm()
ivo_david_michelle 11:5c54826d23a7 104 {
ivo_david_michelle 12:422963993df5 105 //motors motorPwm_;// weired errror, should not be necessary
ivo_david_michelle 11:5c54826d23a7 106 motorPwm_.m1=0;
ivo_david_michelle 14:64b06476d943 107 motorPwm_.m2=0.5 + controlInput_.mx/100;
ivo_david_michelle 11:5c54826d23a7 108 motorPwm_.m3=0;
ivo_david_michelle 14:64b06476d943 109 motorPwm_.m4=0.5 - controlInput_.mx/100;
ivo_david_michelle 12:422963993df5 110 return motorPwm_;
ivo_david_michelle 11:5c54826d23a7 111 }
ivo_david_michelle 14:64b06476d943 112
ivo_david_michelle 14:64b06476d943 113
ivo_david_michelle 14:64b06476d943 114 void Quadcopter::readRc() {
ivo_david_michelle 14:64b06476d943 115 //write to class variables additional thrust double F_des_, desired state: phi, theta, psi.
ivo_david_michelle 14:64b06476d943 116
ivo_david_michelle 14:64b06476d943 117 // RF tranceiver to link with handheld.
ivo_david_michelle 14:64b06476d943 118
ivo_david_michelle 14:64b06476d943 119 // read from joystick:
ivo_david_michelle 14:64b06476d943 120
ivo_david_michelle 14:64b06476d943 121 //write to class variables additional thrust double F_des_, desired state: phi, theta, psi.
ivo_david_michelle 14:64b06476d943 122
ivo_david_michelle 14:64b06476d943 123 uint8_t zero = 0;
ivo_david_michelle 14:64b06476d943 124 uint8_t *rssi = &zero;
ivo_david_michelle 14:64b06476d943 125
ivo_david_michelle 14:64b06476d943 126 uint8_t receive = 0;
ivo_david_michelle 14:64b06476d943 127 int result = 0;
ivo_david_michelle 14:64b06476d943 128
ivo_david_michelle 14:64b06476d943 129 //Set the Channel. 0 is default, 15 is max
ivo_david_michelle 14:64b06476d943 130 mrf_->SetChannel(rcChannel_);
ivo_david_michelle 14:64b06476d943 131
ivo_david_michelle 14:64b06476d943 132 //Start the timer
ivo_david_michelle 14:64b06476d943 133 rcTimer_.start();
ivo_david_michelle 14:64b06476d943 134
ivo_david_michelle 14:64b06476d943 135 pc_->printf("START\r\n");
ivo_david_michelle 14:64b06476d943 136
ivo_david_michelle 14:64b06476d943 137 // TODO: change default value?
ivo_david_michelle 14:64b06476d943 138 float thrust = 0.5;
ivo_david_michelle 14:64b06476d943 139 float yaw = 0.5;
ivo_david_michelle 14:64b06476d943 140 float pitch = 0.5;
ivo_david_michelle 14:64b06476d943 141 float roll = 0.5;
ivo_david_michelle 14:64b06476d943 142
ivo_david_michelle 14:64b06476d943 143 char rxBuffer[rcLength_];
ivo_david_michelle 14:64b06476d943 144
ivo_david_michelle 14:64b06476d943 145 // TODO: stop doing iterations, just do while recieving from slave
ivo_david_michelle 14:64b06476d943 146 for (int i = 0; i < rcIterations_; i++) {
ivo_david_michelle 14:64b06476d943 147 receive = rf_receive_rssi(*mrf_, rxBuffer, rssi, rcLength_ + 1);
ivo_david_michelle 14:64b06476d943 148 if (receive > 0) {
ivo_david_michelle 15:90e07946186f 149 pc_->printf("RX: %s \r\n", rxBuffer);
ivo_david_michelle 14:64b06476d943 150 result = sscanf(rxBuffer, "%f,%f,%f,%f", &thrust, &yaw, &pitch, &roll);
ivo_david_michelle 14:64b06476d943 151 if (result != 4) {
ivo_david_michelle 14:64b06476d943 152 pc_->printf("Parse failure\r\n");
ivo_david_michelle 14:64b06476d943 153 } else {
ivo_david_michelle 14:64b06476d943 154 pc_->printf("%f,%f,%f,%f\r\n", thrust, yaw, pitch, roll);
ivo_david_michelle 14:64b06476d943 155 }
ivo_david_michelle 14:64b06476d943 156 } else {
ivo_david_michelle 14:64b06476d943 157 pc_->printf("Receive failure\r\n");
ivo_david_michelle 14:64b06476d943 158 }
ivo_david_michelle 14:64b06476d943 159 wait(0.2); // TODO: change this value?
ivo_david_michelle 14:64b06476d943 160 }
ivo_david_michelle 14:64b06476d943 161 pc_->printf("END\r\n");
ivo_david_michelle 14:64b06476d943 162 }
ivo_david_michelle 14:64b06476d943 163