Derek McLean / Mbed 2 deprecated uwb-quadcopter

Dependencies:   mbed ESC SR04 TSI

Committer:
gabdo
Date:
Thu Jun 06 00:18:29 2013 +0000
Revision:
16:5f736b955d53
Motor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gabdo 16:5f736b955d53 1 /****************************** motor.cpp ********************************/
gabdo 16:5f736b955d53 2 /* Version: 1.0 */
gabdo 16:5f736b955d53 3 /* Last Updated: June 1, 2013 */
gabdo 16:5f736b955d53 4 /* */
gabdo 16:5f736b955d53 5 /* The motor class is used for motor control using a PWM ECS. When a */
gabdo 16:5f736b955d53 6 /*a motor object is created you must pass the PWM pin as the only */
gabdo 16:5f736b955d53 7 /*argument. */
gabdo 16:5f736b955d53 8 /*************************************************************************/
gabdo 16:5f736b955d53 9
gabdo 16:5f736b955d53 10 #include "motor.h"
gabdo 16:5f736b955d53 11
gabdo 16:5f736b955d53 12 /************************** motor( PinName ) *****************************/
gabdo 16:5f736b955d53 13 /* The motor constructor takes the pin to be used as for PWM and sets it */
gabdo 16:5f736b955d53 14 /*to default safe valuse. */
gabdo 16:5f736b955d53 15 /*************************************************************************/
gabdo 16:5f736b955d53 16
gabdo 16:5f736b955d53 17 motor::motor( PinName pin ) : _pwm(pin)
gabdo 16:5f736b955d53 18 {
gabdo 16:5f736b955d53 19 _pwm.period( 0.020 ); // Set the period to 20ms.
gabdo 16:5f736b955d53 20 setPulseMin( 0.001150 ); // Set default min pulse.
gabdo 16:5f736b955d53 21 setPulseMax( 0.002 ); // Set default max pulse.
gabdo 16:5f736b955d53 22 setSpeed( 0 ); // Set motor to stopped.
gabdo 16:5f736b955d53 23 }
gabdo 16:5f736b955d53 24
gabdo 16:5f736b955d53 25 /************************** setSpeed( int ) ******************************/
gabdo 16:5f736b955d53 26 /* Set speed takes an int value between 0 and 100 and sets the speed of */
gabdo 16:5f736b955d53 27 /*the motor based on passed in percent value of speed. */
gabdo 16:5f736b955d53 28 /*************************************************************************/
gabdo 16:5f736b955d53 29
gabdo 16:5f736b955d53 30 void motor::setSpeed( int value )
gabdo 16:5f736b955d53 31 {
gabdo 16:5f736b955d53 32 // Is the value to small?
gabdo 16:5f736b955d53 33 if( value < 0 ) // Yup, just set to 0.
gabdo 16:5f736b955d53 34 currentSpeed = 0;
gabdo 16:5f736b955d53 35
gabdo 16:5f736b955d53 36 // Is the value to large?
gabdo 16:5f736b955d53 37 else if( value > 100 ) // Yup, just set to 100.
gabdo 16:5f736b955d53 38 currentSpeed = 100;
gabdo 16:5f736b955d53 39
gabdo 16:5f736b955d53 40 // Value must be in the correct range.
gabdo 16:5f736b955d53 41 else
gabdo 16:5f736b955d53 42 currentSpeed = value; // Set the new value.
gabdo 16:5f736b955d53 43
gabdo 16:5f736b955d53 44 // Calculate the value based on pulseMin, pulseMax and currentSpeed.
gabdo 16:5f736b955d53 45 pulse = ((pulseMax - pulseMin ) / 100 * currentSpeed ) + pulseMin;
gabdo 16:5f736b955d53 46 _pwm.pulsewidth( pulse ); // Write the pulse to the pin.
gabdo 16:5f736b955d53 47 }
gabdo 16:5f736b955d53 48
gabdo 16:5f736b955d53 49 /************************** setPulseMin( float ) *************************/
gabdo 16:5f736b955d53 50 /* */
gabdo 16:5f736b955d53 51 /*************************************************************************/
gabdo 16:5f736b955d53 52
gabdo 16:5f736b955d53 53 void motor::setPulseMin( float value )
gabdo 16:5f736b955d53 54 {
gabdo 16:5f736b955d53 55 pulseMin = value;
gabdo 16:5f736b955d53 56 }
gabdo 16:5f736b955d53 57
gabdo 16:5f736b955d53 58 /************************** setPulseMax( float ) *************************/
gabdo 16:5f736b955d53 59 /* */
gabdo 16:5f736b955d53 60 /*************************************************************************/
gabdo 16:5f736b955d53 61
gabdo 16:5f736b955d53 62 void motor::setPulseMax( float value )
gabdo 16:5f736b955d53 63 {
gabdo 16:5f736b955d53 64 pulseMax = value;
gabdo 16:5f736b955d53 65 }
gabdo 16:5f736b955d53 66
gabdo 16:5f736b955d53 67 /*************************** float getPulse( ) ***************************/
gabdo 16:5f736b955d53 68 /* Get the current pulse value. */
gabdo 16:5f736b955d53 69 /*************************************************************************/
gabdo 16:5f736b955d53 70
gabdo 16:5f736b955d53 71 float motor::getPulse( void )
gabdo 16:5f736b955d53 72 {
gabdo 16:5f736b955d53 73 return pulse;
gabdo 16:5f736b955d53 74 }