Ren Buggy
/
2-RenBuggyServoCtrl
A program that allows control of the RenBuggy by steering with a servo controlled wheel.
Revision 0:c95c77611812, committed 2016-03-11
- Comitter:
- RenBuggy
- Date:
- Fri Mar 11 10:37:50 2016 +0000
- Commit message:
- version 1
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ServoDrive.cpp Fri Mar 11 10:37:50 2016 +0000 @@ -0,0 +1,110 @@ +/********************************************************* +*ServoDrive.cpp * +*Author: Elijah Orr * +* * +*A library of functions that can be used to control the * +*RenBuggy via a servo motor. * +*********************************************************/ + +#ifndef SERVODRIVE_C +#define SERVODRIVE_C + +#include "mbed.h" +#include "ServoDrive.h" + + +/* A pin is configured as PwmOut to output the PWM signal that will +control the servo. Two pins are configured as DigitalOut to switch +the drive motors on/off. Information about classes used here can be +found in the mbed library. */ +PwmOut servo(servoPin); +DigitalOut Lmotor(LeftMotorPin); +DigitalOut Rmotor(RightMotorPin); + +/* functions to start and stop the drive motors simply set the DigitalOut +pins to 1 or 0 to switch the motors on/off */ + +/**************************************************************** +* Function: go() * +* * +* Enables the two drive motors of the RenBuggy * +* * +* Inputs: none * +* * +* Returns: none * +****************************************************************/ +extern void go() +{ + Lmotor = Rmotor = 1; +} + +/**************************************************************** +* Function: stop() * +* * +* Disables the two drive motors of the RenBuggy * +* * +* Inputs: none * +* * +* Returns: none * +****************************************************************/ +extern void stop() +{ + Lmotor = Rmotor = 0; +} + +/* a function is set up to configure the PWM output so that it will have the +correct properties to operate the servo. */ + +/**************************************************************** +* Function: configurePWM() * +* * +* Configures a PWM signal for controlling the servo * +* * +* Inputs: Period is the period of the signal in microseconds * +* and Pulsewidth is the pulsewidth of the signal in microseconds* +* * +* Returns: none * +****************************************************************/ +extern void configurePWM(int Period, int Pulsewidth) +{ + /* classes that set the properties of the PWM signal are passed the variables + Period and Pulsewidth to set the frequency and duty cycle of the signal. */ + servo.period_us(Period); + servo.pulsewidth_us(Pulsewidth); +} + +/* to make the main function easy to write, setDirection will allow a value in +degrees to be passed to it, and then do any necessary conversions. angle must be +a float as the conversion involves non integer values. */ + +/**************************************************************** +* Function: setDirection() * +* * +* Sets the direction in which the steering servo is pointing * +* * +* Inputs: A floating point value representing the angle of the * +* servo in degrees * +* * +* Returns: none * +****************************************************************/ +extern void setDirection(float angle) +{ + /* it is possible to pass numbers outside the operational range of the servo + to setDirection, to resolve this there are two checks here. If angle is outside + the boundaries it will be set to either the maximum or minimum value. */ + if(angle < 0) + { + angle = 0; + } + if(angle > 90) + { + angle = 90; + } + /* to properly control the servo, the pulsewidth must be between 1000 and 2000 + micro seconds, so a simple conversion is used to translate the angle into a value + within these bounds. The new value is stored in the variable pulse. */ + int pulse = 1000 + ((angle/90)*1000); + servo.pulsewidth_us(pulse); +} + +#endif //SERVODRIVE_C \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ServoDrive.h Fri Mar 11 10:37:50 2016 +0000 @@ -0,0 +1,33 @@ +/********************************************************* +*ServoDrive.h * +*Author: Elijah Orr * +* * +*A library of functions that can be used to control the * +*RenBuggy via a servo motor. * +*********************************************************/ + +/* include guards are used to prevent problems caused by +multiple definitions */ +#ifndef SERVODRIVE_H +#define SERVODRIVE_H + +/* mbed.h must be included in this file also */ +#include "mbed.h" + +/* #define is used to set the pins that will be used to control the motors. +Using this method means that selecting which pins are used requires only +changing values here, rather than in every function that contains them. */ +#define servoPin p10 +#define LeftMotorPin p5 +#define RightMotorPin p6 + +/* function declarations go here */ +extern void go(); +extern void stop(); +/* setDirection takes a variable of type float (non integer) that will represent an angle in +degrees. */ +extern void setDirection(float); +/* configurePWM expects two variables of type int */ +extern void configurePWM(int, int); + +#endif //SERVODRIVE_H \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Mar 11 10:37:50 2016 +0000 @@ -0,0 +1,60 @@ +/********************************************************* +*RenBuggyServoCtrl * +*Author: Elijah Orr * +* * +*This program demonstates use of a library of functions * +*(ServoDrive) to control the movement of the RenBuggy. * +* * +*********************************************************/ + +/* include the libraries in use in this program. ServoDrive is a library that can +be used to control a steering servo and two drive motors. */ +#include "mbed.h" +#include "ServoDrive.h" + +/* open the main function */ +int main() +{ + /* instead of using wait() to make delays in the program, for loops will be used + as the wait function caused some buggy behaviour. The for loops will require a + variable so it is declared at the top of the main, this is where all variables + used in the main function should be declared. */ + int i; + + /* the first function to be called from ServoDrive.h is used to configure the PWM + output that controls the servo. Both parameters are values in micro seconds that + correspond to the period (20000) and pulsewidth (1500) of the PWM signal. 1500 + corresponds the the servo in centre postion after the line executes. */ + configurePWM(20000, 1500); + + /* function to start the drive motors going is called */ + go(); + + /* the first delay goes here to make the buggy move forward for a time before + turning. The for loop will initialise the variable i to be 0, and enter the + program into a loop that will repeatedly check that i < 25000000 and increment + i as long as the statement is true. When i = 25000000 the program will exit the + loop and move on to the next line. */ + for(i=0;i<25000000;i++){} + + /* the function to set the direction the servo controlled wheel is facing is + called. It takes a single parameter that can be any number between 0 and 90, + which represents a value in degrees where 45 points the wheel directly forward. */ + setDirection(70); + for(i=0;i<25000000;i++){} + + setDirection(10); + for(i=0;i<25000000;i++){} + + setDirection(90); + for(i=0;i<25000000;i++){} + + setDirection(30); + for(i=0;i<25000000;i++){} + + /* function to stop the drive motors is called */ + stop(); + + return 0; +} + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Mar 11 10:37:50 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/252557024ec3 \ No newline at end of file