added gy80 dcm
Dependencies: mbed DCM_AHRS_GY80 PID MMA8451Q
Fork of quadCommand by
quadCommand/quadCommand.cpp@16:84c7db5b4464, 2013-06-10 (annotated)
- Committer:
- dereklmc
- Date:
- Mon Jun 10 00:04:56 2013 +0000
- Revision:
- 16:84c7db5b4464
- Parent:
- 9:9e0d0ba5b6b1
- Parent:
- 15:92ecb025fbc5
- Child:
- 17:d73944c3c945
Merge R11, R9
Who changed what in which revision?
User | Revision | Line number | New 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" |
gabdo | 0:8681037b9a18 | 6 | |
dereklmc | 15:92ecb025fbc5 | 7 | const float quadCommand::MOTOR_UPDATE(20.0f); |
dereklmc | 8:72791d8c36b7 | 8 | |
gabdo | 0:8681037b9a18 | 9 | quadCommand::quadCommand() |
gabdo | 0:8681037b9a18 | 10 | { |
gabdo | 0:8681037b9a18 | 11 | myCom = new com( TXPIN, RXPIN ); // Setup com object. |
gabdo | 0:8681037b9a18 | 12 | world = new sensors(); // Setup the sensors. |
gabdo | 0:8681037b9a18 | 13 | |
gabdo | 0:8681037b9a18 | 14 | myMotors[0] = new motor( MOTOR1 ); // Connect motor 0 to PTD4 pin. |
gabdo | 0:8681037b9a18 | 15 | myMotors[1] = new motor( MOTOR2 ); // Connect motor 1 to PTA12 pin. |
gabdo | 0:8681037b9a18 | 16 | myMotors[2] = new motor( MOTOR3 ); // Connect motor 2 to PTA4 pin. |
gabdo | 0:8681037b9a18 | 17 | myMotors[3] = new motor( MOTOR4 ); // Connect motor 3 to PTA5 pin. |
gabdo | 0:8681037b9a18 | 18 | |
gabdo | 0:8681037b9a18 | 19 | rxThrottle = 0; |
gabdo | 0:8681037b9a18 | 20 | rxPitch = 0; |
gabdo | 0:8681037b9a18 | 21 | rxRoll = 0; |
gabdo | 0:8681037b9a18 | 22 | rxYaw = 0; |
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]; |
dereklmc | 8:72791d8c36b7 | 57 | } |
dereklmc | 8:72791d8c36b7 | 58 | |
gabdo | 9:9e0d0ba5b6b1 | 59 | /*************************** updateMotors() ******************************/ |
gabdo | 9:9e0d0ba5b6b1 | 60 | /* */ |
gabdo | 9:9e0d0ba5b6b1 | 61 | /*************************************************************************/ |
gabdo | 9:9e0d0ba5b6b1 | 62 | |
dereklmc | 8:72791d8c36b7 | 63 | void quadCommand::updateMotors() { |
dereklmc | 15:92ecb025fbc5 | 64 | float throttle, pitch, roll, yaw; |
dereklmc | 15:92ecb025fbc5 | 65 | |
dereklmc | 15:92ecb025fbc5 | 66 | throttle = pidThrottle.correct(currentThrottle, targetThrottle, MOTOR_UPDATE); |
dereklmc | 15:92ecb025fbc5 | 67 | pitch = pidPitch.correct(currentPitch, targetPitch, MOTOR_UPDATE); |
dereklmc | 15:92ecb025fbc5 | 68 | roll = pidRoll.correct(currentRoll, targetRoll, MOTOR_UPDATE); |
dereklmc | 15:92ecb025fbc5 | 69 | yaw = 0.0; //pidYaw.correct(currentYaw, targetYaw, MOTOR_UPDATE); |
dereklmc | 15:92ecb025fbc5 | 70 | |
dereklmc | 15:92ecb025fbc5 | 71 | myMotors[ 0 ]->setSpeed( throttle + pitch - roll - yaw); // Motor 1 |
dereklmc | 15:92ecb025fbc5 | 72 | myMotors[ 1 ]->setSpeed( throttle + pitch + roll + yaw); // Motor 2 |
dereklmc | 15:92ecb025fbc5 | 73 | myMotors[ 2 ]->setSpeed( throttle - pitch + roll - yaw); // Motor 3 |
dereklmc | 15:92ecb025fbc5 | 74 | myMotors[ 3 ]->setSpeed( throttle - pitch - roll + yaw); // Motor 4 |
gabdo | 0:8681037b9a18 | 75 | |
gabdo | 0:8681037b9a18 | 76 | delete[] command; |
gabdo | 0:8681037b9a18 | 77 | } |