ESE350 project, Spring 2016, University of Pennsylvania

Dependencies:   Adafruit9-DOf Receiver mbed-rtos mbed

quadcopter.h

Committer:
ivo_david_michelle
Date:
2016-04-13
Revision:
24:e220fbb70ded
Parent:
22:92401a4fec13
Child:
25:d44610851105

File content as of revision 24:e220fbb70ded:

#ifndef QUADCOPTER_H
#define QUADCOPTER_H

#include "mbed.h"
#include "Adafruit_9DOF.h"
#include "Serial_base.h"
#include "MRF24J40.h"


// a few struct declarations. Can possibly eliminated using the vector class in STL
struct state {
    double phi; // roll
    double theta; // pitch
    double psi; // yaw
    double p; // omega x
    double q; // omega y
    double r; // omega z

};

struct controlInput {
    double f;
    double mx;
    double my;
    double mz;
};

typedef struct {
    double gyro_x;
    double gyro_y;
    double gyro_z;
    double roll;
    double pitch;
    double heading;
} offset;

struct motors {
    double m1;
    double m2;
    double m3;
    double m4;
};


class Quadcopter
{
private:
    Serial *pc_; // use for printing

    // allow for remote communication
    MRF24J40 *mrf_;
    int rcIterations_;
    int rcLength_;
    Timer rcTimer_;

    state state_;
    state desiredState_;
    controlInput controlInput_;
    //pwmOut * _pwmOut; // give address to constructor, than change this value
    motors motorPwm_;
    PwmOut *motor1_;

    // declarations for IMU use
    Adafruit_9DOF dof_;
    Adafruit_LSM303_Accel_Unified accel_;
    Adafruit_LSM303_Mag_Unified mag_;
    Adafruit_L3GD20_Unified gyro_;
    sensors_event_t accel_event_;
    sensors_event_t mag_event_;
    sensors_event_t gyro_event_;
    sensors_vec_t   orientation_;
    offset* initial_offsets_;

    Serial *pcPntr_; // used to access serial communication


    double g_;  // gravity [m/s^2]
    double m_; // mass [kg]
    double l_; // arm lenght [m]
    double gamma_; // relation between moment coefficient and force coefficient.

    // proportional attitude control gains
    double kp_f_; // gain converting radio input to thrust.
    double kp_phi_;
    double kp_theta_;
    double kp_psi_;

    // derivative attitude control gains
    double kd_phi_;
    double kd_theta_;
    double kd_psi_;

    // desired force (will come from joystick)
    double F_des_; // desired thrust force (excluding weight compensation)

public:
    Quadcopter(Serial *pcPntr, MRF24J40 *mrfPntr);  // constructor

    void setState(state *source, state *goal); // not sure if needed
    void controller();
    void readSensorValues();

    void readRc();

    motors getPwm();
    state getState();
    Adafruit_LSM303_Accel_Unified getAccel();
    Adafruit_LSM303_Mag_Unified getMag();
    Adafruit_L3GD20_Unified getGyro();
    Adafruit_9DOF getIMU();
    offset* getOffset();

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

    //void readJoystick();
    // not implemented yet
    //void setGains();
    //void setPwm();
    //void initializePwm();
};

#endif