added gy80 dcm
Dependencies: mbed DCM_AHRS_GY80 PID MMA8451Q
Fork of quadCommand by
quadCommand/quadCommand.cpp@0:8681037b9a18, 2013-06-09 (annotated)
- Committer:
- gabdo
- Date:
- Sun Jun 09 22:13:59 2013 +0000
- Revision:
- 0:8681037b9a18
- Child:
- 1:9b90e7de6e09
quadCommand!
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 | |
gabdo | 0:8681037b9a18 | 7 | quadCommand::quadCommand() |
gabdo | 0:8681037b9a18 | 8 | { |
gabdo | 0:8681037b9a18 | 9 | myCom = new com( TXPIN, RXPIN ); // Setup com object. |
gabdo | 0:8681037b9a18 | 10 | world = new sensors(); // Setup the sensors. |
gabdo | 0:8681037b9a18 | 11 | |
gabdo | 0:8681037b9a18 | 12 | myMotors[0] = new motor( MOTOR1 ); // Connect motor 0 to PTD4 pin. |
gabdo | 0:8681037b9a18 | 13 | myMotors[1] = new motor( MOTOR2 ); // Connect motor 1 to PTA12 pin. |
gabdo | 0:8681037b9a18 | 14 | myMotors[2] = new motor( MOTOR3 ); // Connect motor 2 to PTA4 pin. |
gabdo | 0:8681037b9a18 | 15 | myMotors[3] = new motor( MOTOR4 ); // Connect motor 3 to PTA5 pin. |
gabdo | 0:8681037b9a18 | 16 | |
gabdo | 0:8681037b9a18 | 17 | rxThrottle = 0; |
gabdo | 0:8681037b9a18 | 18 | rxPitch = 0; |
gabdo | 0:8681037b9a18 | 19 | rxRoll = 0; |
gabdo | 0:8681037b9a18 | 20 | rxYaw = 0; |
gabdo | 0:8681037b9a18 | 21 | } |
gabdo | 0:8681037b9a18 | 22 | |
gabdo | 0:8681037b9a18 | 23 | void quadCommand::run() |
gabdo | 0:8681037b9a18 | 24 | { |
gabdo | 0:8681037b9a18 | 25 | while(1) |
gabdo | 0:8681037b9a18 | 26 | { |
gabdo | 0:8681037b9a18 | 27 | if( myCom->isData() ) |
gabdo | 0:8681037b9a18 | 28 | rxInput(); |
gabdo | 0:8681037b9a18 | 29 | |
gabdo | 0:8681037b9a18 | 30 | //myCom->write( 3, world->getAbsoluteY()); |
gabdo | 0:8681037b9a18 | 31 | updatePosition(); |
gabdo | 0:8681037b9a18 | 32 | |
gabdo | 0:8681037b9a18 | 33 | if( globalUpdate ) |
gabdo | 0:8681037b9a18 | 34 | txPosition(); |
gabdo | 0:8681037b9a18 | 35 | } |
gabdo | 0:8681037b9a18 | 36 | } |
gabdo | 0:8681037b9a18 | 37 | |
gabdo | 0:8681037b9a18 | 38 | void quadCommand::rxInput() |
gabdo | 0:8681037b9a18 | 39 | { |
gabdo | 0:8681037b9a18 | 40 | short * command = myCom->read(); |
gabdo | 0:8681037b9a18 | 41 | |
gabdo | 0:8681037b9a18 | 42 | if( command[0] == 1 ) // Throttle command. |
gabdo | 0:8681037b9a18 | 43 | rxThrottle = command[1]; |
gabdo | 0:8681037b9a18 | 44 | |
gabdo | 0:8681037b9a18 | 45 | if( command[0] == 2 ) // Pitch command. |
gabdo | 0:8681037b9a18 | 46 | rxPitch = command[1]; |
gabdo | 0:8681037b9a18 | 47 | |
gabdo | 0:8681037b9a18 | 48 | if( command[0] == 3 ) // Roll command. |
gabdo | 0:8681037b9a18 | 49 | rxRoll = command[1]; |
gabdo | 0:8681037b9a18 | 50 | |
gabdo | 0:8681037b9a18 | 51 | if( command[0] == 4 ) // Yaw command. |
gabdo | 0:8681037b9a18 | 52 | rxYaw = command[1]; |
gabdo | 0:8681037b9a18 | 53 | |
gabdo | 0:8681037b9a18 | 54 | myMotors[ 0 ]->setSpeed( rxThrottle + rxPitch - rxRoll ); // Motor 1 |
gabdo | 0:8681037b9a18 | 55 | myMotors[ 1 ]->setSpeed( rxThrottle + rxPitch + rxRoll ); // Motor 2 |
gabdo | 0:8681037b9a18 | 56 | myMotors[ 2 ]->setSpeed( rxThrottle - rxPitch + rxRoll ); // Motor 3 |
gabdo | 0:8681037b9a18 | 57 | myMotors[ 3 ]->setSpeed( rxThrottle - rxPitch - rxRoll ); // Motor 4 |
gabdo | 0:8681037b9a18 | 58 | |
gabdo | 0:8681037b9a18 | 59 | delete[] command; |
gabdo | 0:8681037b9a18 | 60 | } |
gabdo | 0:8681037b9a18 | 61 | |
gabdo | 0:8681037b9a18 | 62 | void quadCommand::updatePosition() |
gabdo | 0:8681037b9a18 | 63 | { |
gabdo | 0:8681037b9a18 | 64 | float temp; |
gabdo | 0:8681037b9a18 | 65 | |
gabdo | 0:8681037b9a18 | 66 | temp = world->getAbsoluteX(); |
gabdo | 0:8681037b9a18 | 67 | if( temp != currentRoll ) |
gabdo | 0:8681037b9a18 | 68 | { |
gabdo | 0:8681037b9a18 | 69 | currentRoll = temp; |
gabdo | 0:8681037b9a18 | 70 | updatedRoll = true; |
gabdo | 0:8681037b9a18 | 71 | globalUpdate = true; |
gabdo | 0:8681037b9a18 | 72 | } |
gabdo | 0:8681037b9a18 | 73 | |
gabdo | 0:8681037b9a18 | 74 | temp = world->getAbsoluteY(); |
gabdo | 0:8681037b9a18 | 75 | if( temp != currentPitch ) |
gabdo | 0:8681037b9a18 | 76 | { |
gabdo | 0:8681037b9a18 | 77 | currentPitch = temp; |
gabdo | 0:8681037b9a18 | 78 | updatedPitch = true; |
gabdo | 0:8681037b9a18 | 79 | globalUpdate = true; |
gabdo | 0:8681037b9a18 | 80 | } |
gabdo | 0:8681037b9a18 | 81 | } |
gabdo | 0:8681037b9a18 | 82 | |
gabdo | 0:8681037b9a18 | 83 | void quadCommand::txPosition() |
gabdo | 0:8681037b9a18 | 84 | { |
gabdo | 0:8681037b9a18 | 85 | if( updatedRoll ) |
gabdo | 0:8681037b9a18 | 86 | { |
gabdo | 0:8681037b9a18 | 87 | myCom->write( 3, currentRoll); |
gabdo | 0:8681037b9a18 | 88 | updatedRoll = false; |
gabdo | 0:8681037b9a18 | 89 | } |
gabdo | 0:8681037b9a18 | 90 | |
gabdo | 0:8681037b9a18 | 91 | if( updatedPitch ) |
gabdo | 0:8681037b9a18 | 92 | { |
gabdo | 0:8681037b9a18 | 93 | myCom->write( 2, currentPitch); |
gabdo | 0:8681037b9a18 | 94 | updatedPitch = false; |
gabdo | 0:8681037b9a18 | 95 | } |
gabdo | 0:8681037b9a18 | 96 | |
gabdo | 0:8681037b9a18 | 97 | globalUpdate = false; |
gabdo | 0:8681037b9a18 | 98 | } |