ESE350 project, Spring 2016, University of Pennsylvania

Dependencies:   Adafruit9-DOf Receiver mbed-rtos mbed

quadcopter.cpp

Committer:
ivo_david_michelle
Date:
2016-04-07
Revision:
15:90e07946186f
Parent:
14:64b06476d943
Child:
16:2be2aab63198

File content as of revision 15:90e07946186f:

#include "quadcopter.h"
#include "sensor.h"
#include "receiver.h"
#include <string>

//#include "mbed.h"

// constructor
Quadcopter::Quadcopter(Serial *pcPntr, MRF24J40 *mrfPntr)
{   
    pc_= pcPntr;  // enable printing
    // initSensors(accel_, mag_, gyro_, offsetAngRate_);  // IMU

    m_=1;
    g_=9.81;
    // proportional attitude control gains
    kp_phi_ = 1;
    kp_theta_ = 1;
    kp_psi_ = 1;

    // derivative attitude control gains
    kd_phi_ = 0;
    kd_theta_ = 0;
    kd_psi_ = 0;

    // desired values (will come from joystick)
    F_des_ = 0; // desired thrust force (excluding weight compensation)

    dof_ = Adafruit_9DOF();
    accel_ = Adafruit_LSM303_Accel_Unified(30301);
    mag_ = Adafruit_LSM303_Mag_Unified(30302);
    gyro_ = Adafruit_L3GD20_Unified(20);
    //motor1_(p21);
    
    // initSensors(accel_, mag_, gyro_, offsetAngRate_);  // IMU
    
    // prepare for communication with remote control   
    mrf_ = mrfPntr;  // RF tranceiver to link with handheld.
    rcIterations_ = 100;
    rcLength_ = 250;
    rcChannel_ = 3;
}


void Quadcopter::readSensorValues()
{
    accel_.getEvent(&accel_event_);
    if (dof_.accelGetOrientation(&accel_event_, &orientation_)) {
    }
    /* Calculate the heading using the magnetometer */
    mag_.getEvent(&mag_event_);
    if (dof_.magGetOrientation(SENSOR_AXIS_Z, &mag_event_, &orientation_)) {
    }

    gyro_.getEvent(&gyro_event_);

    gyro_event_.gyro.x -= offsetAngRate_.x;
    gyro_event_.gyro.y -= offsetAngRate_.y;
    gyro_event_.gyro.z -= offsetAngRate_.z;


    // measured values (will come from IMU/parameter class/Input to function later)
    // angles
    state_.phi = orientation_.roll;
    state_.theta =orientation_.pitch;
    state_.psi =orientation_.heading;
    // angular velocities in body coordinate system
    state_.p = gyro_event_.gyro.x;
    state_.q = gyro_event_.gyro.y;
    state_.r = gyro_event_.gyro.z;
}

// Date member function
void Quadcopter::setState(state *source, state *goal)
{
    goal->phi = source->phi;
    goal->theta = source->theta;
    goal->psi = source->psi;
    goal->p = source->p;
    goal->q = source->q;
    goal->r = source->r;

}


void Quadcopter::controller()
{

    // compute desired angles (in the case we decide not to set
    // the angles, but for instance the velocity with the Joystick

    // PD controller
    controlInput_.f = m_*g_ + F_des_;
    controlInput_.mx = kp_phi_*(desiredState_.phi-state_.phi)+kd_phi_*(desiredState_.p-state_.p);
    controlInput_.my = kp_theta_*(desiredState_.theta-state_.theta)+kd_theta_*(desiredState_.q-state_.q);
    controlInput_.mz = kp_psi_*(desiredState_.psi-state_.psi)+kd_psi_*(desiredState_.r-state_.r);
    //print("Calculated Control");

    //print("F: %f M_x: %f M_y: %f M_z: %f\n\r",  controlInput_.f, controlInput_.mz, controlInput_.my, controlInput_.mz);
    //        pc_->printf("F: %f\n\r",  F);
}

motors Quadcopter::getPwm()
{
    //motors motorPwm_;// weired errror, should not be necessary
    motorPwm_.m1=0;
    motorPwm_.m2=0.5 + controlInput_.mx/100;
    motorPwm_.m3=0;
    motorPwm_.m4=0.5 - controlInput_.mx/100;
    return motorPwm_;
}

    
void Quadcopter::readRc() {
    //write to class variables additional thrust double F_des_, desired state: phi, theta, psi.
    
    // RF tranceiver to link with handheld.

    // read from joystick: 
    
    //write to class variables additional thrust double F_des_, desired state: phi, theta, psi. 
    
    uint8_t zero = 0;
    uint8_t *rssi = &zero;

    uint8_t receive = 0;
    int result = 0;

    //Set the Channel. 0 is default, 15 is max
    mrf_->SetChannel(rcChannel_);

    //Start the timer
    rcTimer_.start();
    
    pc_->printf("START\r\n");
    
    // TODO: change default value?
    float thrust = 0.5;
    float yaw = 0.5;
    float pitch = 0.5;
    float roll = 0.5;

    char rxBuffer[rcLength_];
    
    // TODO: stop doing iterations, just do while recieving from slave
    for (int i = 0; i < rcIterations_; i++) {
        receive = rf_receive_rssi(*mrf_, rxBuffer, rssi, rcLength_ + 1);
        if (receive > 0) {
            pc_->printf("RX: %s \r\n", rxBuffer);
            result = sscanf(rxBuffer, "%f,%f,%f,%f", &thrust, &yaw, &pitch, &roll);
            if (result != 4) {
                pc_->printf("Parse failure\r\n");
            } else {
                pc_->printf("%f,%f,%f,%f\r\n", thrust, yaw, pitch, roll);
            }
        } else {
            pc_->printf("Receive failure\r\n");
        }
        wait(0.2);  // TODO: change this value?
    }
     pc_->printf("END\r\n");    
}