A program designed to run on the microbit. Used for driving a buggy.

Dependencies:   microbit

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers buggy_function.cpp Source File

buggy_function.cpp

00001 /*********************************************************
00002 *buggy_functions.cpp                                     *
00003 *Author: Elijah Orr & Dan Argust                         *
00004 *                                                        *
00005 *A library of functions that can be used to control the  *
00006 *RenBuggy.                                               *
00007 *********************************************************/
00008 
00009 #ifndef BUGGY_FUNCTIONS_C
00010 #define BUGGY_FUNCTIONS_C
00011 
00012 /* necessary includes */
00013 #include "mbed.h"
00014 #include "buggy_function.h"
00015 #include "Microbit_function.h"
00016 
00017 //Trim is an offset that you can adjust to help the buggy drive straight
00018 //Trim = -0.2 is a left trim
00019 //Trim =  0.2 is a right trim
00020 float trim = 0.0;
00021 
00022 //Set up PWM on both left and right motor pins.
00023 PwmOut Lmotor(LeftMotorPin);
00024 PwmOut Rmotor(RightMotorPin);
00025 
00026 Direction Current_Direction = Up;
00027 
00028 //DigitalIn is used as a button
00029 DigitalIn CurrentButtonState(p7);
00030 //This bool is used to store the buttons state (pressed/not pressed)
00031 bool LastButtonState = 0;
00032 
00033 
00034 /* Functions (listed below) contain the code that runs the buggy.
00035 These functions can be used in the main.cpp file */
00036 
00037 
00038 extern void hold(float time) //waits for (time) seconds
00039 {
00040     //seg.DisplayDigits(0,0); //Displays the digits 0 and 0
00041     for (float i = time;i>0;i-=0.01){ //For every hundreth of a second, display the time remaining
00042         Display(Current_Direction);
00043        wait(0.01);
00044     }
00045     Display(Clear);
00046 }
00047 
00048 extern void forward(float time) //moves forward for (time) seconds
00049 {
00050     Lmotor = 0.7 + trim;
00051     Rmotor = 0.7 - trim; //set the left and right motor to 1.0 (full speed) - the trim offset
00052     Current_Direction = Up;
00053     hold(time); //wait for (time) seconds while the motors are on
00054     stop(); //stops the motors
00055 }
00056 
00057 extern void left(float time) //moves left for (time) seconds
00058 {
00059     Lmotor = 0.0; //set the right motor to full speed
00060     Rmotor = 0.7 - trim; //set the left motor to off
00061     Current_Direction = Left;
00062     hold(time); //waits for (time) seconds
00063     stop(); //stops the motors
00064 }
00065 
00066 extern void right(float time) //moves right for (time) seconds
00067 {
00068     Lmotor = 0.7 + trim; //set the left motor to full speed
00069     Rmotor = 0.0; //set the right motor to off
00070     Current_Direction = Right;
00071     hold(time); //waits for (time) seconds
00072     stop(); //stops the motors
00073 }
00074 
00075 extern void bear_Right(float time) //keeps both motors turning but will turn right
00076 {
00077     Lmotor =  0.7 + trim; //sets the left motor to full speed
00078     Rmotor = 0.1; // sets the right motor to move slowly
00079     Current_Direction = TopRight; //Sets the direction to be top right
00080     hold(time); // wait for (time) seconds.
00081     stop(); // stops both motors.
00082 }
00083 
00084 extern void bear_Left(float time) // keeps both motors turning but it should slowly turn left
00085 {
00086     Lmotor = 0.1; // sets the left motor to move slowly
00087     Rmotor = 0.7 - trim; // sets the right motor to move at full speed
00088     Current_Direction =  TopLeft; // sets the direction to be the top left.
00089     hold(time); // wait for (time) seconds
00090     stop(); // stops both motors
00091 }
00092 extern void stop() //stops the motors
00093 {
00094     Lmotor = Rmotor = 0; //set the speed of each motor to 0
00095     Current_Direction = Stop;
00096 }
00097 
00098 extern void readButton(float time) //checks if the button is pressed for (time) seconds
00099 {
00100     //seg.DisplayDigits(0,0); //displays 0 0 on the seven segment display
00101     for (float i = time;i>0;i-=0.01) //for each hundreth of a seconds check if the button state goes from low to high (eg. pressed)
00102     {
00103         if (CurrentButtonState == 1 and LastButtonState == 0){
00104             rollDice(); //rolls the dice if the button is pressed
00105         }
00106         LastButtonState = CurrentButtonState;
00107         wait(0.01);
00108     }
00109 }
00110 
00111 extern int rollDice() //a function that randomly generates a number and displays it on the seven segment display
00112 {
00113     int tens; //declare two variables, tens and units to store the number to be displayed
00114     int units;
00115     for (int i = 20;i>0;i--){
00116         tens = rand()%9; //generate a random number from 0-9 and store it in "tens"
00117         units = rand()%9;
00118         //seg.DisplayDigits(tens,units); //display the numbers stored in tens and units
00119         wait(0.04);
00120     }
00121     return tens*10+units;
00122 }
00123 
00124 extern void follow_Line(float time) //a function that uses the built in ADCs to command the buggy to follow a black line for time.
00125 {
00126     int direction_Data; //declares a variable uses to store the direction data.
00127     for (float i = time; i > 0 ; i -= follow_Speed) // will repeat until time is up.
00128     {
00129         direction_Data = left_Sensor() - right_Sensor(); //Compare left sensor and right sensor.
00130         //if direction data is positive then the right sensor is on white.
00131         //if direction data is negative then the left sensor is on white.
00132         if(direction_Data > 100) //data direction is significantly positive.
00133         {
00134             left(follow_Speed); //turns left for follow_Speed.
00135         }
00136         else if (direction_Data < -100 ) //data direction is significantly negative
00137         {
00138             right(follow_Speed); // turns right for follow_Speed
00139         }
00140         else // else if both sensors are within each other. 
00141         {
00142             forward(follow_Speed); // heads forward for follow_Speed.
00143         }
00144     }
00145 }
00146 
00147 extern void follow_Line() // a functoin that will follow a black line forever.
00148 {
00149     int direction_Data; //declares a variable used to store the direction data.
00150     for (;;) //This creates a loop which will go on forever.
00151     { 
00152         direction_Data = left_Sensor() - right_Sensor(); // Combines both sets of direction data into one variable.
00153         if(direction_Data > 100) // if direction data is signficantly positive
00154         {
00155             left(follow_Speed);// Turn left, as the buggy is leaving the black line.
00156         }
00157         else if (direction_Data < -100 ) // if direction data is significantly negative
00158         {
00159             right(follow_Speed); //turn right, as the buggy is leaving the black line.
00160         }
00161         else // if neither other conditions are met then go forward.
00162         {
00163             forward(follow_Speed); // go forward, as the buggy is on the black line.
00164         }
00165     }
00166 }
00167 
00168 
00169 #endif // BUGGY_FUNCTIONS_C