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

Dependencies:   mbed

Committer:
RenBuggy
Date:
Fri Mar 11 10:39:05 2016 +0000
Revision:
0:575ce3f4b7ec
version 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RenBuggy 0:575ce3f4b7ec 1 /****************************************************************
RenBuggy 0:575ce3f4b7ec 2 * LightSeekingRobot *
RenBuggy 0:575ce3f4b7ec 3 * Author: Elijah Orr *
RenBuggy 0:575ce3f4b7ec 4 * *
RenBuggy 0:575ce3f4b7ec 5 * This program uses the values of two analog inputs (LDRs) to *
RenBuggy 0:575ce3f4b7ec 6 * steer the RenBuggy via a servo motor. *
RenBuggy 0:575ce3f4b7ec 7 *****************************************************************/
RenBuggy 0:575ce3f4b7ec 8
RenBuggy 0:575ce3f4b7ec 9 /* The library ServoDrive is used in this program to control the
RenBuggy 0:575ce3f4b7ec 10 servo and drive motors. Libraries are a useful tool for creating
RenBuggy 0:575ce3f4b7ec 11 code that can be reused in multiple programs. */
RenBuggy 0:575ce3f4b7ec 12 #include "mbed.h"
RenBuggy 0:575ce3f4b7ec 13 #include "ServoDrive.h"
RenBuggy 0:575ce3f4b7ec 14
RenBuggy 0:575ce3f4b7ec 15 AnalogIn LDR1(p15);
RenBuggy 0:575ce3f4b7ec 16 AnalogIn LDR2(p16);
RenBuggy 0:575ce3f4b7ec 17
RenBuggy 0:575ce3f4b7ec 18 /****************************************************************
RenBuggy 0:575ce3f4b7ec 19 * Function: main() *
RenBuggy 0:575ce3f4b7ec 20 * *
RenBuggy 0:575ce3f4b7ec 21 * Steers a servo controlled wheel of the Renbuggy according to *
RenBuggy 0:575ce3f4b7ec 22 * the relative light levels of two LDRs *
RenBuggy 0:575ce3f4b7ec 23 * *
RenBuggy 0:575ce3f4b7ec 24 * Inputs: none *
RenBuggy 0:575ce3f4b7ec 25 * *
RenBuggy 0:575ce3f4b7ec 26 * Returns: none *
RenBuggy 0:575ce3f4b7ec 27 ****************************************************************/
RenBuggy 0:575ce3f4b7ec 28 int main()
RenBuggy 0:575ce3f4b7ec 29 {
RenBuggy 0:575ce3f4b7ec 30 /* variables that will be used in the main are declared */
RenBuggy 0:575ce3f4b7ec 31 float angle;
RenBuggy 0:575ce3f4b7ec 32 float LDR1value;
RenBuggy 0:575ce3f4b7ec 33 float LDR2value;
RenBuggy 0:575ce3f4b7ec 34 int i;
RenBuggy 0:575ce3f4b7ec 35
RenBuggy 0:575ce3f4b7ec 36 /* these variables are initialised values to be used to
RenBuggy 0:575ce3f4b7ec 37 control certain behaviors of the buggy. tolerance sets
RenBuggy 0:575ce3f4b7ec 38 how much the light levels must differ for it to turn,
RenBuggy 0:575ce3f4b7ec 39 centreValue is the value in degrees that corresponds to
RenBuggy 0:575ce3f4b7ec 40 the wheel pointing directly forward, and difAdjust is
RenBuggy 0:575ce3f4b7ec 41 used make the values of the two LDR inputs more similar */
RenBuggy 0:575ce3f4b7ec 42 float tolerance = 0.03;
RenBuggy 0:575ce3f4b7ec 43 int centreValue = 45;
RenBuggy 0:575ce3f4b7ec 44 float difAdjust = 0.03;
RenBuggy 0:575ce3f4b7ec 45
RenBuggy 0:575ce3f4b7ec 46 /* call the function to set up the PWM signal for the servo */
RenBuggy 0:575ce3f4b7ec 47 configurePWM(20000, 1500);
RenBuggy 0:575ce3f4b7ec 48
RenBuggy 0:575ce3f4b7ec 49 /* the angle of the servo is initially set to centre it */
RenBuggy 0:575ce3f4b7ec 50 angle = centreValue;
RenBuggy 0:575ce3f4b7ec 51
RenBuggy 0:575ce3f4b7ec 52 /* start the buggy moving */
RenBuggy 0:575ce3f4b7ec 53 go();
RenBuggy 0:575ce3f4b7ec 54
RenBuggy 0:575ce3f4b7ec 55 /* opening a for loop without any conditions starts an infinite
RenBuggy 0:575ce3f4b7ec 56 loop, so that the contained code runs forever */
RenBuggy 0:575ce3f4b7ec 57 for(;;)
RenBuggy 0:575ce3f4b7ec 58 {
RenBuggy 0:575ce3f4b7ec 59 /* the class member function read() in AnalogIn.h reads the
RenBuggy 0:575ce3f4b7ec 60 voltage of the of the LDR pins into variables as a floating
RenBuggy 0:575ce3f4b7ec 61 point value between 0.0 and 1.0 */
RenBuggy 0:575ce3f4b7ec 62 LDR1value = LDR1.read();
RenBuggy 0:575ce3f4b7ec 63 LDR2value = LDR2.read();
RenBuggy 0:575ce3f4b7ec 64
RenBuggy 0:575ce3f4b7ec 65 /* LDR2value has difAdjust added to it to make both LDR values
RenBuggy 0:575ce3f4b7ec 66 similar enough to compare effectively. The value 0.03 stored in
RenBuggy 0:575ce3f4b7ec 67 difAdjust was chosen by printing the LDR values to console via
RenBuggy 0:575ce3f4b7ec 68 USB and observing the difference between values (USBSerial.h was
RenBuggy 0:575ce3f4b7ec 69 used for this) */
RenBuggy 0:575ce3f4b7ec 70 LDR2value = LDR2value + difAdjust;
RenBuggy 0:575ce3f4b7ec 71
RenBuggy 0:575ce3f4b7ec 72 /* the behavior of the RenBuggy is defined by a series of if
RenBuggy 0:575ce3f4b7ec 73 statements. If the conditions in the parentheses are true,
RenBuggy 0:575ce3f4b7ec 74 then the code in the if statement will run, if not the program
RenBuggy 0:575ce3f4b7ec 75 moves on. */
RenBuggy 0:575ce3f4b7ec 76
RenBuggy 0:575ce3f4b7ec 77 /* && is known as the logical AND operator. If both operands are
RenBuggy 0:575ce3f4b7ec 78 non zero (i.e. true), then the condition will be true. If one LDR
RenBuggy 0:575ce3f4b7ec 79 value is significantly larger than the other, then angle will
RenBuggy 0:575ce3f4b7ec 80 increment (angle++ means that the value of angle will increase
RenBuggy 0:575ce3f4b7ec 81 by one) */
RenBuggy 0:575ce3f4b7ec 82 if((LDR1value - LDR2value > tolerance) && (angle < 90)){
RenBuggy 0:575ce3f4b7ec 83 angle++;
RenBuggy 0:575ce3f4b7ec 84 }
RenBuggy 0:575ce3f4b7ec 85
RenBuggy 0:575ce3f4b7ec 86 /* if statements can be followed by an optional else if statement,
RenBuggy 0:575ce3f4b7ec 87 which will execute when the above if statement is false, and the
RenBuggy 0:575ce3f4b7ec 88 else if condition is true. */
RenBuggy 0:575ce3f4b7ec 89 else if((LDR2value - LDR1value > tolerance) && (angle > 0)){
RenBuggy 0:575ce3f4b7ec 90 angle--; /* angle-- means that angle will decrease by one */
RenBuggy 0:575ce3f4b7ec 91 }
RenBuggy 0:575ce3f4b7ec 92
RenBuggy 0:575ce3f4b7ec 93 /* If the difference between values is within tolerance, point the
RenBuggy 0:575ce3f4b7ec 94 wheel straight forward */
RenBuggy 0:575ce3f4b7ec 95 else if((LDR1value - LDR2value < tolerance) && (LDR2value - LDR1value < tolerance)){
RenBuggy 0:575ce3f4b7ec 96 angle = centreValue;
RenBuggy 0:575ce3f4b7ec 97 }
RenBuggy 0:575ce3f4b7ec 98
RenBuggy 0:575ce3f4b7ec 99 /* a for loop is used to create a delay in the program to make it run
RenBuggy 0:575ce3f4b7ec 100 a little slower, so that the servo position is updated less freqently */
RenBuggy 0:575ce3f4b7ec 101 for(i=0;i<400000;i++){}
RenBuggy 0:575ce3f4b7ec 102
RenBuggy 0:575ce3f4b7ec 103 /* the function setDirection is called and passed the variable angle to
RenBuggy 0:575ce3f4b7ec 104 set the servo position to the new value of angle */
RenBuggy 0:575ce3f4b7ec 105 setDirection(angle);
RenBuggy 0:575ce3f4b7ec 106 }
RenBuggy 0:575ce3f4b7ec 107 }
RenBuggy 0:575ce3f4b7ec 108
RenBuggy 0:575ce3f4b7ec 109
RenBuggy 0:575ce3f4b7ec 110
RenBuggy 0:575ce3f4b7ec 111
RenBuggy 0:575ce3f4b7ec 112
RenBuggy 0:575ce3f4b7ec 113
RenBuggy 0:575ce3f4b7ec 114
RenBuggy 0:575ce3f4b7ec 115
RenBuggy 0:575ce3f4b7ec 116
RenBuggy 0:575ce3f4b7ec 117