Ren Buggy / Mbed 2 deprecated 3-LightSeekingRobot

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /****************************************************************
00002 * LightSeekingRobot                                             *
00003 * Author: Elijah Orr                                            *
00004 *                                                               * 
00005 * This program uses the values of two analog inputs (LDRs) to   *
00006 * steer the RenBuggy via a servo motor.                         *
00007 *****************************************************************/
00008 
00009 /* The library ServoDrive is used in this program to control the 
00010 servo and drive motors. Libraries are a useful tool for creating
00011 code that can be reused in multiple programs. */
00012 #include "mbed.h"
00013 #include "ServoDrive.h"
00014 
00015 AnalogIn LDR1(p15);
00016 AnalogIn LDR2(p16);
00017 
00018 /****************************************************************
00019 * Function: main()                                              *
00020 *                                                               *
00021 * Steers a servo controlled wheel of the Renbuggy according to  *
00022 * the relative light levels of two LDRs                         *
00023 *                                                               *
00024 * Inputs: none                                                  *
00025 *                                                               *
00026 * Returns: none                                                 *
00027 ****************************************************************/
00028 int main()
00029 {
00030     /* variables that will be used in the main are declared */
00031     float angle;
00032     float LDR1value;
00033     float LDR2value;
00034     int i;
00035     
00036     /* these variables are initialised values to be used to 
00037      control certain behaviors of the buggy. tolerance sets
00038      how much the light levels must differ for it to turn,
00039      centreValue is the value in degrees that corresponds to
00040      the wheel pointing directly forward, and difAdjust is 
00041      used make the values of the two LDR inputs more similar */
00042     float tolerance = 0.03;
00043     int centreValue = 45;
00044     float difAdjust = 0.03;
00045     
00046     /* call the function to set up the PWM signal for the servo */
00047     configurePWM(20000, 1500);
00048     
00049     /* the angle of the servo is initially set to centre it */
00050     angle = centreValue;
00051     
00052     /* start the buggy moving */
00053     go();
00054     
00055     /* opening a for loop without any conditions starts an infinite
00056     loop, so that the contained code runs forever */
00057     for(;;)
00058     {
00059         /* the class member function read() in AnalogIn.h reads the
00060         voltage of the of the LDR pins into variables as a floating
00061         point value between 0.0 and 1.0 */       
00062         LDR1value = LDR1.read();
00063         LDR2value = LDR2.read();
00064         
00065         /* LDR2value has difAdjust added to it to make both LDR values
00066         similar enough to compare effectively. The value 0.03 stored in
00067         difAdjust was chosen by printing the LDR values to console via
00068         USB and observing the difference between values (USBSerial.h was 
00069         used for this) */
00070         LDR2value = LDR2value + difAdjust;
00071         
00072         /* the behavior of the RenBuggy is defined by a series of if
00073         statements. If the conditions in the parentheses are true,
00074         then the code in the if statement will run, if not the program
00075         moves on. */
00076         
00077         /* && is known as the logical AND operator. If both operands are 
00078         non zero (i.e. true), then the condition will be true. If one LDR
00079         value is significantly larger than the other, then angle will 
00080         increment (angle++ means that the value of angle will increase
00081         by one) */
00082         if((LDR1value - LDR2value > tolerance) && (angle < 90)){
00083             angle++;
00084         }
00085         
00086         /* if statements can be followed by an optional else if statement, 
00087         which will execute when the above if statement is false, and the 
00088         else if condition is true. */
00089         else if((LDR2value - LDR1value > tolerance) && (angle > 0)){
00090              angle--;   /* angle-- means that angle will decrease by one */
00091         }
00092         
00093         /* If the difference between values is within tolerance, point the 
00094         wheel straight forward */
00095         else if((LDR1value - LDR2value < tolerance) && (LDR2value - LDR1value < tolerance)){
00096             angle = centreValue;
00097         }
00098         
00099         /* a for loop is used to create a delay in the program to make it run
00100         a little slower, so that the servo position is updated less freqently */
00101         for(i=0;i<400000;i++){}
00102         
00103         /* the function setDirection is called and passed the variable angle to
00104         set the servo position to the new value of angle */
00105         setDirection(angle);
00106     }
00107 }
00108         
00109         
00110              
00111         
00112         
00113         
00114         
00115     
00116     
00117