added gy80 dcm
Dependencies: mbed DCM_AHRS_GY80 PID MMA8451Q
Fork of quadCommand by
quadCommand/quadCommand.cpp@15:92ecb025fbc5, 2013-06-10 (annotated)
- Committer:
- dereklmc
- Date:
- Mon Jun 10 00:03:12 2013 +0000
- Revision:
- 15:92ecb025fbc5
- Parent:
- 8:72791d8c36b7
- Child:
- 16:84c7db5b4464
Called pid controller in motors.
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 | 0:8681037b9a18 | 25 | void quadCommand::run() |
gabdo | 0:8681037b9a18 | 26 | { |
gabdo | 0:8681037b9a18 | 27 | while(1) |
gabdo | 0:8681037b9a18 | 28 | { |
gabdo | 0:8681037b9a18 | 29 | if( myCom->isData() ) |
gabdo | 0:8681037b9a18 | 30 | rxInput(); |
gabdo | 0:8681037b9a18 | 31 | } |
gabdo | 0:8681037b9a18 | 32 | } |
gabdo | 0:8681037b9a18 | 33 | |
gabdo | 0:8681037b9a18 | 34 | void quadCommand::rxInput() |
gabdo | 0:8681037b9a18 | 35 | { |
gabdo | 0:8681037b9a18 | 36 | short * command = myCom->read(); |
gabdo | 0:8681037b9a18 | 37 | |
gabdo | 0:8681037b9a18 | 38 | if( command[0] == 1 ) // Throttle command. |
gabdo | 0:8681037b9a18 | 39 | rxThrottle = command[1]; |
gabdo | 0:8681037b9a18 | 40 | |
gabdo | 0:8681037b9a18 | 41 | if( command[0] == 2 ) // Pitch command. |
gabdo | 0:8681037b9a18 | 42 | rxPitch = command[1]; |
gabdo | 0:8681037b9a18 | 43 | |
gabdo | 0:8681037b9a18 | 44 | if( command[0] == 3 ) // Roll command. |
gabdo | 0:8681037b9a18 | 45 | rxRoll = command[1]; |
gabdo | 0:8681037b9a18 | 46 | |
gabdo | 0:8681037b9a18 | 47 | if( command[0] == 4 ) // Yaw command. |
dereklmc | 8:72791d8c36b7 | 48 | rxYaw = command[1]; |
dereklmc | 8:72791d8c36b7 | 49 | } |
dereklmc | 8:72791d8c36b7 | 50 | |
dereklmc | 8:72791d8c36b7 | 51 | void quadCommand::updateMotors() { |
dereklmc | 15:92ecb025fbc5 | 52 | float throttle, pitch, roll, yaw; |
dereklmc | 15:92ecb025fbc5 | 53 | |
dereklmc | 15:92ecb025fbc5 | 54 | throttle = pidThrottle.correct(currentThrottle, targetThrottle, MOTOR_UPDATE); |
dereklmc | 15:92ecb025fbc5 | 55 | pitch = pidPitch.correct(currentPitch, targetPitch, MOTOR_UPDATE); |
dereklmc | 15:92ecb025fbc5 | 56 | roll = pidRoll.correct(currentRoll, targetRoll, MOTOR_UPDATE); |
dereklmc | 15:92ecb025fbc5 | 57 | yaw = 0.0; //pidYaw.correct(currentYaw, targetYaw, MOTOR_UPDATE); |
dereklmc | 15:92ecb025fbc5 | 58 | |
dereklmc | 15:92ecb025fbc5 | 59 | myMotors[ 0 ]->setSpeed( throttle + pitch - roll - yaw); // Motor 1 |
dereklmc | 15:92ecb025fbc5 | 60 | myMotors[ 1 ]->setSpeed( throttle + pitch + roll + yaw); // Motor 2 |
dereklmc | 15:92ecb025fbc5 | 61 | myMotors[ 2 ]->setSpeed( throttle - pitch + roll - yaw); // Motor 3 |
dereklmc | 15:92ecb025fbc5 | 62 | myMotors[ 3 ]->setSpeed( throttle - pitch - roll + yaw); // Motor 4 |
gabdo | 0:8681037b9a18 | 63 | |
gabdo | 0:8681037b9a18 | 64 | delete[] command; |
gabdo | 0:8681037b9a18 | 65 | } |