A program that allows the RenBuggy to detect the direction of a light source and drive towards it.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
RenBuggy
Date:
Fri Mar 11 10:39:05 2016 +0000
Commit message:
version 1

Changed in this revision

ServoDrive.cpp Show annotated file Show diff for this revision Revisions of this file
ServoDrive.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ServoDrive.cpp	Fri Mar 11 10:39:05 2016 +0000
@@ -0,0 +1,112 @@
+/*********************************************************
+*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: The pulsewith of the signal in microseconds          *
+****************************************************************/
+extern int 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); 
+    
+    return 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:39:05 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 int 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:39:05 2016 +0000
@@ -0,0 +1,117 @@
+/****************************************************************
+* LightSeekingRobot                                             *
+* Author: Elijah Orr                                            *
+*                                                               * 
+* This program uses the values of two analog inputs (LDRs) to   *
+* steer the RenBuggy via a servo motor.                         *
+*****************************************************************/
+
+/* The library ServoDrive is used in this program to control the 
+servo and drive motors. Libraries are a useful tool for creating
+code that can be reused in multiple programs. */
+#include "mbed.h"
+#include "ServoDrive.h"
+
+AnalogIn LDR1(p15);
+AnalogIn LDR2(p16);
+
+/****************************************************************
+* Function: main()                                              *
+*                                                               *
+* Steers a servo controlled wheel of the Renbuggy according to  *
+* the relative light levels of two LDRs                         *
+*                                                               *
+* Inputs: none                                                  *
+*                                                               *
+* Returns: none                                                 *
+****************************************************************/
+int main()
+{
+    /* variables that will be used in the main are declared */
+    float angle;
+    float LDR1value;
+    float LDR2value;
+    int i;
+    
+    /* these variables are initialised values to be used to 
+     control certain behaviors of the buggy. tolerance sets
+     how much the light levels must differ for it to turn,
+     centreValue is the value in degrees that corresponds to
+     the wheel pointing directly forward, and difAdjust is 
+     used make the values of the two LDR inputs more similar */
+    float tolerance = 0.03;
+    int centreValue = 45;
+    float difAdjust = 0.03;
+    
+    /* call the function to set up the PWM signal for the servo */
+    configurePWM(20000, 1500);
+    
+    /* the angle of the servo is initially set to centre it */
+    angle = centreValue;
+    
+    /* start the buggy moving */
+    go();
+    
+    /* opening a for loop without any conditions starts an infinite
+    loop, so that the contained code runs forever */
+    for(;;)
+    {
+        /* the class member function read() in AnalogIn.h reads the
+        voltage of the of the LDR pins into variables as a floating
+        point value between 0.0 and 1.0 */       
+        LDR1value = LDR1.read();
+        LDR2value = LDR2.read();
+        
+        /* LDR2value has difAdjust added to it to make both LDR values
+        similar enough to compare effectively. The value 0.03 stored in
+        difAdjust was chosen by printing the LDR values to console via
+        USB and observing the difference between values (USBSerial.h was 
+        used for this) */
+        LDR2value = LDR2value + difAdjust;
+        
+        /* the behavior of the RenBuggy is defined by a series of if
+        statements. If the conditions in the parentheses are true,
+        then the code in the if statement will run, if not the program
+        moves on. */
+        
+        /* && is known as the logical AND operator. If both operands are 
+        non zero (i.e. true), then the condition will be true. If one LDR
+        value is significantly larger than the other, then angle will 
+        increment (angle++ means that the value of angle will increase
+        by one) */
+        if((LDR1value - LDR2value > tolerance) && (angle < 90)){
+            angle++;
+        }
+        
+        /* if statements can be followed by an optional else if statement, 
+        which will execute when the above if statement is false, and the 
+        else if condition is true. */
+        else if((LDR2value - LDR1value > tolerance) && (angle > 0)){
+             angle--;   /* angle-- means that angle will decrease by one */
+        }
+        
+        /* If the difference between values is within tolerance, point the 
+        wheel straight forward */
+        else if((LDR1value - LDR2value < tolerance) && (LDR2value - LDR1value < tolerance)){
+            angle = centreValue;
+        }
+        
+        /* a for loop is used to create a delay in the program to make it run
+        a little slower, so that the servo position is updated less freqently */
+        for(i=0;i<400000;i++){}
+        
+        /* the function setDirection is called and passed the variable angle to
+        set the servo position to the new value of angle */
+        setDirection(angle);
+    }
+}
+        
+        
+             
+        
+        
+        
+        
+    
+    
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Mar 11 10:39:05 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/252557024ec3
\ No newline at end of file