added gy80 dcm

Dependencies:   mbed DCM_AHRS_GY80 PID MMA8451Q

Fork of quadCommand by Greg Abdo

quadCommand/quadCommand.cpp

Committer:
dereklmc
Date:
2013-06-10
Revision:
16:84c7db5b4464
Parent:
9:9e0d0ba5b6b1
Parent:
15:92ecb025fbc5
Child:
17:d73944c3c945

File content as of revision 16:84c7db5b4464:

/************************ quadCommand.cpp ********************************/
/*                                                                       */
/*************************************************************************/

#include "quadCommand.h"

const float quadCommand::MOTOR_UPDATE(20.0f);

quadCommand::quadCommand()
{
        myCom = new com( TXPIN, RXPIN );        // Setup com object.       
        world = new sensors();                  // Setup the sensors.
        
        myMotors[0] = new motor( MOTOR1 );      // Connect motor 0 to PTD4 pin.
        myMotors[1] = new motor( MOTOR2 );      // Connect motor 1 to PTA12 pin.
        myMotors[2] = new motor( MOTOR3 );      // Connect motor 2 to PTA4 pin.
        myMotors[3] = new motor( MOTOR4 );      // Connect motor 3 to PTA5 pin.
        
        rxThrottle    = 0;
        rxPitch       = 0;
        rxRoll        = 0;
        rxYaw         = 0;
}

/******************************* run() ***********************************/
/*                                                                       */
/*************************************************************************/

void quadCommand::run() 
{   
    while(1)
    {
        if( myCom->isData() )
            rxInput();
    }
}

/***************************** rxInput() *********************************/
/*                                                                       */
/*************************************************************************/

void quadCommand::rxInput()
{
    short * command = myCom->read();
            
    if( command[0] == 1 )       // Throttle command.
        targetThrottle = command[1];
                
    if( command[0] == 2 )       // Pitch command.
        targetPitch = command[1];
                
    if( command[0] == 3 )       // Roll command.
        targetRoll = command[1];            
            
     if( command[0] == 4 )      // Yaw command.
        targetYaw = command[1];
}

/*************************** updateMotors() ******************************/
/*                                                                       */
/*************************************************************************/

void quadCommand::updateMotors() {
    float throttle, pitch, roll, yaw;
    
    throttle = pidThrottle.correct(currentThrottle, targetThrottle, MOTOR_UPDATE);
    pitch = pidPitch.correct(currentPitch, targetPitch, MOTOR_UPDATE);
    roll = pidRoll.correct(currentRoll, targetRoll, MOTOR_UPDATE);
    yaw = 0.0; //pidYaw.correct(currentYaw, targetYaw, MOTOR_UPDATE);

     myMotors[ 0 ]->setSpeed( throttle + pitch - roll - yaw);    // Motor 1
     myMotors[ 1 ]->setSpeed( throttle + pitch + roll + yaw);    // Motor 2
     myMotors[ 2 ]->setSpeed( throttle - pitch + roll - yaw);    // Motor 3
     myMotors[ 3 ]->setSpeed( throttle - pitch - roll + yaw);    // Motor 4
                         
     delete[] command;
}