ESE350 project, Spring 2016, University of Pennsylvania

Dependencies:   Adafruit9-DOf Receiver mbed-rtos mbed

quadcopter.cpp

Committer:
ivo_david_michelle
Date:
2016-04-20
Revision:
30:4820042e67b5
Parent:
29:ae765492fa8b
Child:
31:d473eacfc271

File content as of revision 30:4820042e67b5:

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

#ifndef M_PI
#define M_PI           3.14159265358979323846
#endif

// constructor
Quadcopter::Quadcopter(Serial *pcPntr, MRF24J40 *mrfPntr)
{
    pc_= pcPntr;  // enable printing
    g_= 9.81;
    l_= 0.25;
    gamma_= 1;

    zeroVelPwm=0.11;
    maxPwm=0.15;

    // control gains set s.t. 100% joystick results in 15% (actually: (maxPwm-zeroVelPwm+0.1)) duty cycle. 
    kp_f_     = (maxPwm - zeroVelPwm) * 4 / 0.5; 
    kp_phi_   = 0.2 * (maxPwm - zeroVelPwm) * l_ / 0.5 * 4 / M_PI;
    kp_theta_ = 0.2 * (maxPwm - zeroVelPwm) * l_ / 0.5 * 4 / M_PI;
    kp_psi_   = 0;
    //kp_phi_ = 0;
    //kp_theta_ = 0;

    // derivative attitude control gains
    kd_phi_   = 0.3 * (maxPwm - zeroVelPwm) * 2 / M_PI;
    kd_theta_ = 0.3 * (maxPwm - zeroVelPwm) * 2 / M_PI;
    kd_psi_   = 0.1;

    // 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);

    // prepare for communication with remote control
    rcTimer_.start();
    mrf_ = mrfPntr;  // RF tranceiver to link with handheld.
    rcLength_ = 250;
    mrf_->SetChannel(3);  //Set the Channel. 0 is default, 15 is max

    initial_offsets_ = (offset*) malloc(sizeof(offset));
    initSensors(*this);  // IMU
}

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   -= initial_offsets_->gyro_x;
    gyro_event_.gyro.y   -= initial_offsets_->gyro_y;
    gyro_event_.gyro.z   -= initial_offsets_->gyro_z;
    orientation_.roll    -= initial_offsets_->roll;
    orientation_.pitch   -= initial_offsets_->pitch;
    orientation_.heading -= initial_offsets_->heading;

    // angular velocities in body coordinate system
    state_.p = gyro_event_.gyro.x * M_PI / 180;
    state_.q = gyro_event_.gyro.y * M_PI / 180;
    state_.r = gyro_event_.gyro.z * M_PI / 180;

    state_.phi = orientation_.roll * M_PI / 180;
    state_.theta = -orientation_.pitch * M_PI / 180;
    state_.psi = orientation_.heading * M_PI / 180;
    //pc_->printf("Roll: %f\tPitch: %f\tYaw: %f\tVel x: %f\tVel y: %f\tVel z: %f\r\n", state_.phi, state_.theta, state_.psi, state_.p, state_.q, state_.r);
}

void Quadcopter::controller()
{
    // PD controller
    controlInput_.f  = kp_f_ * F_des_;//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 = kd_psi_ * desiredState_.r; // feedforward desired yaw rate.  // kp_psi_*(desiredState_.psi-state_.psi)+kd_psi_*(desiredState_.r-state_.r);

    // set pwm values
    double forcePerMotor = 0.25 * controlInput_.f;
    double yawMomentPerMotor = 0.25 / gamma_ * controlInput_.mz;
    double rollMomentPerMotor = 0.5 / l_ * controlInput_.mx;
    double pitchMomentPerMotor = 0.5 / l_ * controlInput_.my;
    motorPwm_.m1 = zeroVelPwm + forcePerMotor - pitchMomentPerMotor - yawMomentPerMotor;
    motorPwm_.m2 = zeroVelPwm + forcePerMotor + rollMomentPerMotor + yawMomentPerMotor;
    motorPwm_.m3 = zeroVelPwm + forcePerMotor + pitchMomentPerMotor - yawMomentPerMotor;
    motorPwm_.m4 = zeroVelPwm + forcePerMotor - rollMomentPerMotor + yawMomentPerMotor;

    // cut off at max PWM
    //pc_->printf("m1: %f\tm2: %f\tm3: %f\tm4: %f\r\n", motorPwm_.m1, motorPwm_.m2, motorPwm_.m3, motorPwm_.m4);
    motorPwm_.m1 = min(maxPwm, motorPwm_.m1);
    motorPwm_.m2 = min(maxPwm, motorPwm_.m2);
    motorPwm_.m3 = min(maxPwm, motorPwm_.m3);
    motorPwm_.m4 = min(maxPwm, motorPwm_.m4);
}

motors Quadcopter::getPwm()
{
    return motorPwm_;
}

state Quadcopter::getState()
{
    return state_;
}

Adafruit_LSM303_Accel_Unified Quadcopter::getAccel()
{
    return accel_;
}

Adafruit_LSM303_Mag_Unified Quadcopter::getMag()
{
    return mag_;
}

Adafruit_L3GD20_Unified Quadcopter::getGyro()
{
    return gyro_;
}

offset* Quadcopter::getOffset()
{
    return initial_offsets_;
}

Adafruit_9DOF Quadcopter::getIMU()
{
    return dof_;
}

double Quadcopter::getForce()
{
    return F_des_;
}

void Quadcopter::readRc()
{
    uint8_t zero = 0;
    uint8_t *rssi = &zero;

    uint8_t receive = 0;

    char rxBuffer[rcLength_];

    float thrust;
    float yaw;
    float pitch;
    float roll;
    long long id;

    receive = rf_receive_rssi(*mrf_, rxBuffer, rssi, rcLength_ + 1);
    if (receive > 0) {
        sscanf(rxBuffer, "%lld,%f,%f,%f,%f", &id, &thrust, &yaw, &pitch, &roll);
    } else {
        pc_->printf("Receive failure\r\n");
    }

    // convert to radians, range is = +-40° or +-0.698132 radians
    desiredState_.phi = -((roll - 0.5) * 80) * M_PI / 180; // minus, because joystick to right should result in positive moment
    desiredState_.theta = ((pitch - 0.5) * 80) * M_PI / 180;
    desiredState_.r = yaw-0.5; // number between 0 and 1 //((yaw - 0.5) * 80) * M_PI / 180;
    F_des_ = thrust-0.5; // number between 0 and 1 -> number between -0.5 and 0.5.  //((thrust - 0.5) * 80) * M_PI / 180;

    // print id with thrust, yaw, pitch, and roll
    //pc_->printf("%lld: thrust: %f, yaw: %f, pitch: %f, roll: %f\r\n", id, F_des_, desiredState_.psi, desiredState_.theta, desiredState_.phi);
}