added gy80 dcm

Dependencies:   mbed DCM_AHRS_GY80 PID MMA8451Q

Fork of quadCommand by Greg Abdo

Committer:
dereklmc
Date:
Mon Jun 10 01:12:52 2013 +0000
Revision:
33:4f62a7a46e71
Parent:
32:3d180aa938ba
Child:
34:d6696dc977a2
Added debug info to update motors

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gabdo 0:8681037b9a18 1 /************************ quadCommand.cpp ********************************/
gabdo 0:8681037b9a18 2 /* */
gabdo 0:8681037b9a18 3 /*************************************************************************/
gabdo 0:8681037b9a18 4
gabdo 0:8681037b9a18 5 #include "quadCommand.h"
dereklmc 32:3d180aa938ba 6 #include "debug.h"
gabdo 0:8681037b9a18 7
dereklmc 15:92ecb025fbc5 8 const float quadCommand::MOTOR_UPDATE(20.0f);
dereklmc 8:72791d8c36b7 9
dereklmc 17:d73944c3c945 10 quadCommand::quadCommand() :
dereklmc 17:d73944c3c945 11 pidThrottle(1.5,0.1,1.20,DEFAULT_WINDUP_GUARD),
dereklmc 17:d73944c3c945 12 pidPitch(1.5,0.1,1.20,DEFAULT_WINDUP_GUARD),
dereklmc 17:d73944c3c945 13 pidRoll(1.5,0.1,1.20,DEFAULT_WINDUP_GUARD),
dereklmc 17:d73944c3c945 14 pidYaw(1.5,0.1,1.20,DEFAULT_WINDUP_GUARD)
gabdo 0:8681037b9a18 15 {
gabdo 0:8681037b9a18 16 myCom = new com( TXPIN, RXPIN ); // Setup com object.
gabdo 0:8681037b9a18 17 world = new sensors(); // Setup the sensors.
gabdo 0:8681037b9a18 18
gabdo 0:8681037b9a18 19 myMotors[0] = new motor( MOTOR1 ); // Connect motor 0 to PTD4 pin.
gabdo 0:8681037b9a18 20 myMotors[1] = new motor( MOTOR2 ); // Connect motor 1 to PTA12 pin.
gabdo 0:8681037b9a18 21 myMotors[2] = new motor( MOTOR3 ); // Connect motor 2 to PTA4 pin.
gabdo 0:8681037b9a18 22 myMotors[3] = new motor( MOTOR4 ); // Connect motor 3 to PTA5 pin.
gabdo 0:8681037b9a18 23 }
gabdo 0:8681037b9a18 24
gabdo 9:9e0d0ba5b6b1 25 /******************************* run() ***********************************/
gabdo 9:9e0d0ba5b6b1 26 /* */
gabdo 9:9e0d0ba5b6b1 27 /*************************************************************************/
gabdo 9:9e0d0ba5b6b1 28
gabdo 0:8681037b9a18 29 void quadCommand::run()
gabdo 0:8681037b9a18 30 {
gabdo 0:8681037b9a18 31 while(1)
gabdo 0:8681037b9a18 32 {
gabdo 0:8681037b9a18 33 if( myCom->isData() )
gabdo 0:8681037b9a18 34 rxInput();
gabdo 0:8681037b9a18 35 }
gabdo 0:8681037b9a18 36 }
gabdo 0:8681037b9a18 37
gabdo 9:9e0d0ba5b6b1 38 /***************************** rxInput() *********************************/
gabdo 9:9e0d0ba5b6b1 39 /* */
gabdo 9:9e0d0ba5b6b1 40 /*************************************************************************/
gabdo 9:9e0d0ba5b6b1 41
gabdo 0:8681037b9a18 42 void quadCommand::rxInput()
gabdo 0:8681037b9a18 43 {
gabdo 0:8681037b9a18 44 short * command = myCom->read();
gabdo 0:8681037b9a18 45
gabdo 0:8681037b9a18 46 if( command[0] == 1 ) // Throttle command.
gabdo 9:9e0d0ba5b6b1 47 targetThrottle = command[1];
gabdo 0:8681037b9a18 48
gabdo 0:8681037b9a18 49 if( command[0] == 2 ) // Pitch command.
gabdo 9:9e0d0ba5b6b1 50 targetPitch = command[1];
gabdo 0:8681037b9a18 51
gabdo 0:8681037b9a18 52 if( command[0] == 3 ) // Roll command.
gabdo 9:9e0d0ba5b6b1 53 targetRoll = command[1];
gabdo 0:8681037b9a18 54
gabdo 0:8681037b9a18 55 if( command[0] == 4 ) // Yaw command.
gabdo 9:9e0d0ba5b6b1 56 targetYaw = command[1];
gabdo 12:15d129d681e9 57
gabdo 12:15d129d681e9 58 delete[] command;
dereklmc 8:72791d8c36b7 59 }
dereklmc 8:72791d8c36b7 60
gabdo 9:9e0d0ba5b6b1 61 /*************************** updateMotors() ******************************/
gabdo 9:9e0d0ba5b6b1 62 /* */
gabdo 9:9e0d0ba5b6b1 63 /*************************************************************************/
gabdo 9:9e0d0ba5b6b1 64
dereklmc 8:72791d8c36b7 65 void quadCommand::updateMotors() {
dereklmc 15:92ecb025fbc5 66 float throttle, pitch, roll, yaw;
dereklmc 15:92ecb025fbc5 67
dereklmc 15:92ecb025fbc5 68 throttle = pidThrottle.correct(currentThrottle, targetThrottle, MOTOR_UPDATE);
dereklmc 15:92ecb025fbc5 69 pitch = pidPitch.correct(currentPitch, targetPitch, MOTOR_UPDATE);
dereklmc 15:92ecb025fbc5 70 roll = pidRoll.correct(currentRoll, targetRoll, MOTOR_UPDATE);
dereklmc 15:92ecb025fbc5 71 yaw = 0.0; //pidYaw.correct(currentYaw, targetYaw, MOTOR_UPDATE);
dereklmc 32:3d180aa938ba 72
dereklmc 33:4f62a7a46e71 73 DEBUG(pc.printf("att : { t : %f, p : %f, r : %f, y : %f }", throttle, pitch, roll, yaw);)
dereklmc 15:92ecb025fbc5 74
dereklmc 33:4f62a7a46e71 75 int speed0 = throttle + pitch - roll - yaw;
dereklmc 33:4f62a7a46e71 76 int speed1 = throttle + pitch + roll + yaw;
dereklmc 33:4f62a7a46e71 77 int speed2 = throttle - pitch + roll - yaw;
dereklmc 33:4f62a7a46e71 78 int speed3 = throttle - pitch - roll + yaw;
dereklmc 33:4f62a7a46e71 79
dereklmc 33:4f62a7a46e71 80 DEBUG(pc.printf("motor : [ %f , %f , %f , %f ]", speed0, speed1, speed2, speed3);)
dereklmc 33:4f62a7a46e71 81
dereklmc 33:4f62a7a46e71 82 myMotors[ 0 ]->setSpeed(speed0); // Motor 1
dereklmc 33:4f62a7a46e71 83 myMotors[ 1 ]->setSpeed(speed1); // Motor 2
dereklmc 33:4f62a7a46e71 84 myMotors[ 2 ]->setSpeed(speed2); // Motor 3
dereklmc 33:4f62a7a46e71 85 myMotors[ 3 ]->setSpeed(speed3); // Motor 4
gabdo 0:8681037b9a18 86 }