Ren Buggy
/
3-LightSeekingRobot
A program that allows the RenBuggy to detect the direction of a light source and drive towards it.
ServoDrive.cpp@0:575ce3f4b7ec, 2016-03-11 (annotated)
- Committer:
- RenBuggy
- Date:
- Fri Mar 11 10:39:05 2016 +0000
- Revision:
- 0:575ce3f4b7ec
version 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RenBuggy | 0:575ce3f4b7ec | 1 | /********************************************************* |
RenBuggy | 0:575ce3f4b7ec | 2 | *ServoDrive.cpp * |
RenBuggy | 0:575ce3f4b7ec | 3 | *Author: Elijah Orr * |
RenBuggy | 0:575ce3f4b7ec | 4 | * * |
RenBuggy | 0:575ce3f4b7ec | 5 | *A library of functions that can be used to control the * |
RenBuggy | 0:575ce3f4b7ec | 6 | *RenBuggy via a servo motor. * |
RenBuggy | 0:575ce3f4b7ec | 7 | *********************************************************/ |
RenBuggy | 0:575ce3f4b7ec | 8 | |
RenBuggy | 0:575ce3f4b7ec | 9 | #ifndef SERVODRIVE_C |
RenBuggy | 0:575ce3f4b7ec | 10 | #define SERVODRIVE_C |
RenBuggy | 0:575ce3f4b7ec | 11 | |
RenBuggy | 0:575ce3f4b7ec | 12 | #include "mbed.h" |
RenBuggy | 0:575ce3f4b7ec | 13 | #include "ServoDrive.h" |
RenBuggy | 0:575ce3f4b7ec | 14 | |
RenBuggy | 0:575ce3f4b7ec | 15 | |
RenBuggy | 0:575ce3f4b7ec | 16 | /* A pin is configured as PwmOut to output the PWM signal that will |
RenBuggy | 0:575ce3f4b7ec | 17 | control the servo. Two pins are configured as DigitalOut to switch |
RenBuggy | 0:575ce3f4b7ec | 18 | the drive motors on/off. Information about classes used here can be |
RenBuggy | 0:575ce3f4b7ec | 19 | found in the mbed library. */ |
RenBuggy | 0:575ce3f4b7ec | 20 | PwmOut servo(servoPin); |
RenBuggy | 0:575ce3f4b7ec | 21 | DigitalOut Lmotor(LeftMotorPin); |
RenBuggy | 0:575ce3f4b7ec | 22 | DigitalOut Rmotor(RightMotorPin); |
RenBuggy | 0:575ce3f4b7ec | 23 | |
RenBuggy | 0:575ce3f4b7ec | 24 | /* functions to start and stop the drive motors simply set the DigitalOut |
RenBuggy | 0:575ce3f4b7ec | 25 | pins to 1 or 0 to switch the motors on/off */ |
RenBuggy | 0:575ce3f4b7ec | 26 | |
RenBuggy | 0:575ce3f4b7ec | 27 | /**************************************************************** |
RenBuggy | 0:575ce3f4b7ec | 28 | * Function: go() * |
RenBuggy | 0:575ce3f4b7ec | 29 | * * |
RenBuggy | 0:575ce3f4b7ec | 30 | * Enables the two drive motors of the RenBuggy * |
RenBuggy | 0:575ce3f4b7ec | 31 | * * |
RenBuggy | 0:575ce3f4b7ec | 32 | * Inputs: none * |
RenBuggy | 0:575ce3f4b7ec | 33 | * * |
RenBuggy | 0:575ce3f4b7ec | 34 | * Returns: none * |
RenBuggy | 0:575ce3f4b7ec | 35 | ****************************************************************/ |
RenBuggy | 0:575ce3f4b7ec | 36 | extern void go() |
RenBuggy | 0:575ce3f4b7ec | 37 | { |
RenBuggy | 0:575ce3f4b7ec | 38 | Lmotor = Rmotor = 1; |
RenBuggy | 0:575ce3f4b7ec | 39 | } |
RenBuggy | 0:575ce3f4b7ec | 40 | |
RenBuggy | 0:575ce3f4b7ec | 41 | /**************************************************************** |
RenBuggy | 0:575ce3f4b7ec | 42 | * Function: stop() * |
RenBuggy | 0:575ce3f4b7ec | 43 | * * |
RenBuggy | 0:575ce3f4b7ec | 44 | * Disables the two drive motors of the RenBuggy * |
RenBuggy | 0:575ce3f4b7ec | 45 | * * |
RenBuggy | 0:575ce3f4b7ec | 46 | * Inputs: none * |
RenBuggy | 0:575ce3f4b7ec | 47 | * * |
RenBuggy | 0:575ce3f4b7ec | 48 | * Returns: none * |
RenBuggy | 0:575ce3f4b7ec | 49 | ****************************************************************/ |
RenBuggy | 0:575ce3f4b7ec | 50 | extern void stop() |
RenBuggy | 0:575ce3f4b7ec | 51 | { |
RenBuggy | 0:575ce3f4b7ec | 52 | Lmotor = Rmotor = 0; |
RenBuggy | 0:575ce3f4b7ec | 53 | } |
RenBuggy | 0:575ce3f4b7ec | 54 | |
RenBuggy | 0:575ce3f4b7ec | 55 | /* a function is set up to configure the PWM output so that it will have the |
RenBuggy | 0:575ce3f4b7ec | 56 | correct properties to operate the servo. */ |
RenBuggy | 0:575ce3f4b7ec | 57 | |
RenBuggy | 0:575ce3f4b7ec | 58 | /**************************************************************** |
RenBuggy | 0:575ce3f4b7ec | 59 | * Function: configurePWM() * |
RenBuggy | 0:575ce3f4b7ec | 60 | * * |
RenBuggy | 0:575ce3f4b7ec | 61 | * Configures a PWM signal for controlling the servo * |
RenBuggy | 0:575ce3f4b7ec | 62 | * * |
RenBuggy | 0:575ce3f4b7ec | 63 | * Inputs: Period is the period of the signal in microseconds * |
RenBuggy | 0:575ce3f4b7ec | 64 | * and Pulsewidth is the pulsewidth of the signal in microseconds* |
RenBuggy | 0:575ce3f4b7ec | 65 | * * |
RenBuggy | 0:575ce3f4b7ec | 66 | * Returns: none * |
RenBuggy | 0:575ce3f4b7ec | 67 | ****************************************************************/ |
RenBuggy | 0:575ce3f4b7ec | 68 | extern void configurePWM(int Period, int Pulsewidth) |
RenBuggy | 0:575ce3f4b7ec | 69 | { |
RenBuggy | 0:575ce3f4b7ec | 70 | /* classes that set the properties of the PWM signal are passed the variables |
RenBuggy | 0:575ce3f4b7ec | 71 | Period and Pulsewidth to set the frequency and duty cycle of the signal. */ |
RenBuggy | 0:575ce3f4b7ec | 72 | servo.period_us(Period); |
RenBuggy | 0:575ce3f4b7ec | 73 | servo.pulsewidth_us(Pulsewidth); |
RenBuggy | 0:575ce3f4b7ec | 74 | } |
RenBuggy | 0:575ce3f4b7ec | 75 | |
RenBuggy | 0:575ce3f4b7ec | 76 | /* to make the main function easy to write, setDirection will allow a value in |
RenBuggy | 0:575ce3f4b7ec | 77 | degrees to be passed to it, and then do any necessary conversions. angle must be |
RenBuggy | 0:575ce3f4b7ec | 78 | a float as the conversion involves non integer values. */ |
RenBuggy | 0:575ce3f4b7ec | 79 | |
RenBuggy | 0:575ce3f4b7ec | 80 | /**************************************************************** |
RenBuggy | 0:575ce3f4b7ec | 81 | * Function: setDirection() * |
RenBuggy | 0:575ce3f4b7ec | 82 | * * |
RenBuggy | 0:575ce3f4b7ec | 83 | * Sets the direction in which the steering servo is pointing * |
RenBuggy | 0:575ce3f4b7ec | 84 | * * |
RenBuggy | 0:575ce3f4b7ec | 85 | * Inputs: A floating point value representing the angle of the * |
RenBuggy | 0:575ce3f4b7ec | 86 | * servo in degrees * |
RenBuggy | 0:575ce3f4b7ec | 87 | * * |
RenBuggy | 0:575ce3f4b7ec | 88 | * Returns: The pulsewith of the signal in microseconds * |
RenBuggy | 0:575ce3f4b7ec | 89 | ****************************************************************/ |
RenBuggy | 0:575ce3f4b7ec | 90 | extern int setDirection(float angle) |
RenBuggy | 0:575ce3f4b7ec | 91 | { |
RenBuggy | 0:575ce3f4b7ec | 92 | /* it is possible to pass numbers outside the operational range of the servo |
RenBuggy | 0:575ce3f4b7ec | 93 | to setDirection, to resolve this there are two checks here. If angle is outside |
RenBuggy | 0:575ce3f4b7ec | 94 | the boundaries it will be set to either the maximum or minimum value. */ |
RenBuggy | 0:575ce3f4b7ec | 95 | if(angle < 0) |
RenBuggy | 0:575ce3f4b7ec | 96 | { |
RenBuggy | 0:575ce3f4b7ec | 97 | angle = 0; |
RenBuggy | 0:575ce3f4b7ec | 98 | } |
RenBuggy | 0:575ce3f4b7ec | 99 | if(angle > 90) |
RenBuggy | 0:575ce3f4b7ec | 100 | { |
RenBuggy | 0:575ce3f4b7ec | 101 | angle = 90; |
RenBuggy | 0:575ce3f4b7ec | 102 | } |
RenBuggy | 0:575ce3f4b7ec | 103 | /* to properly control the servo, the pulsewidth must be between 1000 and 2000 |
RenBuggy | 0:575ce3f4b7ec | 104 | micro seconds, so a simple conversion is used to translate the angle into a value |
RenBuggy | 0:575ce3f4b7ec | 105 | within these bounds. The new value is stored in the variable pulse. */ |
RenBuggy | 0:575ce3f4b7ec | 106 | int pulse = 1000 + ((angle/90)*1000); |
RenBuggy | 0:575ce3f4b7ec | 107 | servo.pulsewidth_us(pulse); |
RenBuggy | 0:575ce3f4b7ec | 108 | |
RenBuggy | 0:575ce3f4b7ec | 109 | return pulse; |
RenBuggy | 0:575ce3f4b7ec | 110 | } |
RenBuggy | 0:575ce3f4b7ec | 111 | |
RenBuggy | 0:575ce3f4b7ec | 112 | #endif //SERVODRIVE_C |