Ren Buggy / Mbed 2 deprecated 2-RenBuggyServoCtrl

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ServoDrive.cpp Source File

ServoDrive.cpp

00001 /*********************************************************
00002 *ServoDrive.cpp                                          *
00003 *Author: Elijah Orr                                      *
00004 *                                                        *  
00005 *A library of functions that can be used to control the  * 
00006 *RenBuggy via a servo motor.                             *
00007 *********************************************************/
00008 
00009 #ifndef SERVODRIVE_C
00010 #define SERVODRIVE_C
00011 
00012 #include "mbed.h"
00013 #include "ServoDrive.h"
00014 
00015 
00016 /* A pin is configured as PwmOut to output the PWM signal that will
00017 control the servo. Two pins are configured as DigitalOut to switch
00018 the drive motors on/off. Information about classes used here can be
00019 found in the mbed library. */
00020 PwmOut servo(servoPin);
00021 DigitalOut Lmotor(LeftMotorPin);
00022 DigitalOut Rmotor(RightMotorPin);
00023 
00024 /* functions to start and stop the drive motors simply set the DigitalOut
00025 pins to 1 or 0 to switch the motors on/off */
00026 
00027 /****************************************************************
00028 * Function: go()                                                *
00029 *                                                               *
00030 * Enables the two drive motors of the RenBuggy                  *
00031 *                                                               *
00032 * Inputs: none                                                  *
00033 *                                                               *
00034 * Returns: none                                                 *
00035 ****************************************************************/
00036 extern void go()
00037 {
00038     Lmotor = Rmotor = 1;
00039 }
00040 
00041 /****************************************************************
00042 * Function: stop()                                              *
00043 *                                                               *
00044 * Disables the two drive motors of the RenBuggy                 *
00045 *                                                               *
00046 * Inputs: none                                                  *
00047 *                                                               *
00048 * Returns: none                                                 *
00049 ****************************************************************/
00050 extern void stop()
00051 {
00052     Lmotor = Rmotor = 0;
00053 }
00054 
00055 /* a function is set up to configure the PWM output so that it will have the
00056 correct properties to operate the servo. */
00057 
00058 /****************************************************************
00059 * Function: configurePWM()                                      *
00060 *                                                               *
00061 * Configures a PWM signal for controlling the servo             *
00062 *                                                               *
00063 * Inputs: Period is the period of the signal in microseconds    *
00064 * and Pulsewidth is the pulsewidth of the signal in microseconds*                                                   
00065 *                                                               *
00066 * Returns: none                                                 *
00067 ****************************************************************/
00068 extern void configurePWM(int Period, int Pulsewidth)
00069 {
00070     /* classes that set the properties of the PWM signal are passed the variables 
00071     Period and Pulsewidth to set the frequency and duty cycle of the signal. */
00072     servo.period_us(Period);
00073     servo.pulsewidth_us(Pulsewidth);
00074 }
00075 
00076 /* to make the main function easy to write, setDirection will allow a value in 
00077 degrees to be passed to it, and then do any necessary conversions. angle must be
00078 a float as the conversion involves non integer values. */
00079 
00080 /****************************************************************
00081 * Function: setDirection()                                      *
00082 *                                                               *
00083 * Sets the direction in which the steering servo is pointing    *
00084 *                                                               *
00085 * Inputs: A floating point value representing the angle of the  *
00086 * servo in degrees                                              *                                                   
00087 *                                                               *
00088 * Returns: none                                                 *
00089 ****************************************************************/
00090 extern void setDirection(float angle)
00091 {   
00092     /* it is possible to pass numbers outside the operational range of the servo
00093     to setDirection, to resolve this there are two checks here. If angle is outside 
00094     the boundaries it will be set to either the maximum or minimum value. */
00095     if(angle < 0)
00096     {
00097         angle = 0;
00098     }
00099     if(angle > 90)
00100     {
00101         angle = 90;
00102     }
00103     /* to properly control the servo, the pulsewidth must be between 1000 and 2000 
00104     micro seconds, so a simple conversion is used to translate the angle into a value
00105     within these bounds. The new value is stored in the variable pulse. */
00106     int pulse = 1000 + ((angle/90)*1000);
00107     servo.pulsewidth_us(pulse); 
00108 }
00109 
00110 #endif //SERVODRIVE_C