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

Dependencies:   microbit

Files at this revision

API Documentation at this revision

Comitter:
AdrianClarke
Date:
Mon Mar 27 12:14:03 2017 +0000
Commit message:
Initial Code

Changed in this revision

Microbit_function.cpp Show annotated file Show diff for this revision Revisions of this file
Microbit_function.h Show annotated file Show diff for this revision Revisions of this file
buggy_function.cpp Show annotated file Show diff for this revision Revisions of this file
buggy_function.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
microbit.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 4aa6e1498925 Microbit_function.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Microbit_function.cpp	Mon Mar 27 12:14:03 2017 +0000
@@ -0,0 +1,112 @@
+
+#include "Microbit_function.h"
+#include "mbed.h"
+
+MicroBit uBit;
+
+// These MiroBit images are used by the Display function to indicate what direction the buggy will travel in.
+MicroBitImage arrow_Up("0,0,1,0,0\n0,1,1,1,0\n1,0,1,0,1\n0,0,1,0,0\n0,0,1,0,0\n");
+MicroBitImage arrow_Down("0,0,1,0,0\n0,0,1,0,0\n1,0,1,0,1\n0,1,1,1,0\n0,0,1,0,0\n");
+MicroBitImage arrow_Left("0,0,1,0,0\n0,1,0,0,0\n1,1,1,1,1\n0,1,0,0,0\n0,0,1,0,0\n");
+MicroBitImage arrow_Right("0,0,1,0,0\n0,0,0,1,0\n1,1,1,1,1\n0,0,0,1,0\n0,0,1,0,0\n");
+MicroBitImage arrow_TopLeft("1,1,1,0,0\n1,1,0,0,0\n1,0,1,0,0\n0,0,0,1,0\n0,0,0,0,1\n");
+MicroBitImage arrow_TopRight("0,0,1,1,1\n0,0,0,1,1\n0,0,1,0,1\n0,1,0,0,0\n1,0,0,0,0\n");
+MicroBitImage arrow_BottomLeft("0,0,0,0,1\n0,0,0,1,0\n1,0,1,0,0\n1,1,0,0,0\n1,1,1,0,0\n");
+MicroBitImage arrow_BottomRight("1,0,0,0,0\n0,1,0,0,0\n0,0,1,0,1\n0,0,0,1,1\n0,0,1,1,1\n");
+MicroBitImage cross("1,0,0,0,1\n0,1,0,1,0\n0,0,1,0,0\n0,1,0,1,0\n1,0,0,0,1\n");
+MicroBitImage On("1,1,1,1,1\n1,1,1,1,1\n1,1,1,1,1\n1,1,1,1,1\n1,1,1,1,1\n");
+
+//
+MicroBitPin P0(MICROBIT_ID_IO_P0, MICROBIT_PIN_P0, PIN_CAPABILITY_ALL); 
+MicroBitPin P1(MICROBIT_ID_IO_P1, MICROBIT_PIN_P1, PIN_CAPABILITY_ALL); 
+
+extern void microBit_Setup()
+{
+    uBit.init();
+}
+
+extern void Display(Direction Current_Direction) // This function is used to simply display the direction the motors are turning
+{
+    switch(Current_Direction) // Displays the related arrow accoring to which direction the buggy is moving in.
+    {
+        case Up:            uBit.display.printAsync(arrow_Up);           break;
+        case Down:          uBit.display.printAsync(arrow_Down);         break;
+        case Right:         uBit.display.printAsync(arrow_Right);        break;
+        case Left:          uBit.display.printAsync(arrow_Left);         break;
+        case TopLeft:       uBit.display.printAsync(arrow_TopLeft);      break;
+        case BottomLeft:    uBit.display.printAsync(arrow_BottomLeft);   break;
+        case TopRight:      uBit.display.printAsync(arrow_TopRight);     break;
+        case BottomRight:   uBit.display.printAsync(arrow_BottomRight);  break;
+        case Stop:          uBit.display.printAsync(cross);              break;
+        case Clear:         uBit.display.clear();                        break;
+    }
+}
+extern void pointNorth() //This function Will direct the buggy to face North
+{
+    int direction = uBit.compass.heading(); //Declare a variable to store the direction in
+    while((direction > 2) && (direction < 358)) // While it is not facing north
+    {
+        if (direction <= 180) // if it is less then 180 then turn left
+        {
+            left(0.1);
+        }
+        if (direction > 180) // if it is more then 180 then turn right
+        {
+            right(0.1);
+        }
+        direction = uBit.compass.heading(); // check the direction again
+    }
+}
+
+extern void pointSouth() // this function is similar to hedNorth but will point to South instead
+{
+    int direction = uBit.compass.heading(); //Declares a variable which will be used to store the direction
+    while((direction < 178) || (direction > 182)) //While it is not pointing South
+    {
+        if ((direction > 180) && (direction <= 360)) // if it is between 180 and 360 turn left.
+        {
+            left(0.1);
+        }
+        if ((direction > 0) && (direction <=180)) // if it is less than 180 turn right.
+        {
+            right(0.1);
+        }
+        direction = uBit.compass.heading(); // check the direction again
+    }
+}
+
+extern Compass_Dirs Compass() // this function is designed to display the direction the microbit is facing, and returns that value.
+{
+    int direction = uBit.compass.heading(); // declare a variable to be used for storing the direction.
+    if((direction < 45) || (direction >= 325)) // if the direciton is pointing generally north then output North.
+    {
+        uBit.display.print("N");
+        return North;
+    }
+    else if((direction < 135) && (direction >= 45)) // if the direction is generally east then output east.
+    {
+        uBit.display.print("E");
+        return East;
+    }
+    else if((direction < 225) && (direction >= 135)) // if the direction is generally South then output south
+    {
+        uBit.display.print("S");
+        return South;
+    }
+    else if((direction < 325) && (direction >= 225)) // if the direction is generally West then output West.
+    {
+        uBit.display.print("W");
+        return West;
+    }
+    return Default;
+}
+
+extern int left_Sensor()// This function returns the analogue voltage on the left sensor.
+{
+    return P0.getAnalogValue(); //This is the function that actually get the voltage.
+}
+
+extern int right_Sensor() //This function returns the analogue voltage of the right sensor.
+{
+    return P1.getAnalogValue(); //gets the voltage on P1, should be connected to the right sensor.
+}
\ No newline at end of file
diff -r 000000000000 -r 4aa6e1498925 Microbit_function.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Microbit_function.h	Mon Mar 27 12:14:03 2017 +0000
@@ -0,0 +1,32 @@
+
+#ifndef MICROBIT_FUNCTIONS_H
+#define MICROBIT_FUNCTIONS_H
+
+#include "MicroBit.h"
+#include "buggy_function.h"
+
+/*We need to define which pins the Sensors will be on.
+Potentially P0,P1,P2. all other pads 
+*/
+#define Left_Line_Sensor P0
+#define Right_Line_Sensor P1
+
+// This typedef is used to define the values that a compass can be
+typedef enum {
+    North,
+    East,
+    South,
+    West,
+    Default
+}Compass_Dirs;
+
+
+extern Compass_Dirs Compass();
+extern void pointNorth();
+extern void pointSouth();
+extern void Display(Direction Current_Direction);
+extern int left_Sensor();
+extern int right_Sensor();
+extern void microBit_Setup();
+
+#endif 
\ No newline at end of file
diff -r 000000000000 -r 4aa6e1498925 buggy_function.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buggy_function.cpp	Mon Mar 27 12:14:03 2017 +0000
@@ -0,0 +1,169 @@
+/*********************************************************
+*buggy_functions.cpp                                     *
+*Author: Elijah Orr & Dan Argust                         *
+*                                                        *
+*A library of functions that can be used to control the  *
+*RenBuggy.                                               *
+*********************************************************/
+
+#ifndef BUGGY_FUNCTIONS_C
+#define BUGGY_FUNCTIONS_C
+
+/* necessary includes */
+#include "mbed.h"
+#include "buggy_function.h"
+#include "Microbit_function.h"
+
+//Trim is an offset that you can adjust to help the buggy drive straight
+//Trim = -0.2 is a left trim
+//Trim =  0.2 is a right trim
+float trim = 0.0;
+
+//Set up PWM on both left and right motor pins.
+PwmOut Lmotor(LeftMotorPin);
+PwmOut Rmotor(RightMotorPin);
+
+Direction Current_Direction = Up;
+
+//DigitalIn is used as a button
+DigitalIn CurrentButtonState(p7);
+//This bool is used to store the buttons state (pressed/not pressed)
+bool LastButtonState = 0;
+
+
+/* Functions (listed below) contain the code that runs the buggy.
+These functions can be used in the main.cpp file */
+
+
+extern void hold(float time) //waits for (time) seconds
+{
+    //seg.DisplayDigits(0,0); //Displays the digits 0 and 0
+    for (float i = time;i>0;i-=0.01){ //For every hundreth of a second, display the time remaining
+        Display(Current_Direction);
+       wait(0.01);
+    }
+    Display(Clear);
+}
+
+extern void forward(float time) //moves forward for (time) seconds
+{
+    Lmotor = 0.7 + trim;
+    Rmotor = 0.7 - trim; //set the left and right motor to 1.0 (full speed) - the trim offset
+    Current_Direction = Up;
+    hold(time); //wait for (time) seconds while the motors are on
+    stop(); //stops the motors
+}
+
+extern void left(float time) //moves left for (time) seconds
+{
+    Lmotor = 0.0; //set the right motor to full speed
+    Rmotor = 0.7 - trim; //set the left motor to off
+    Current_Direction = Left;
+    hold(time); //waits for (time) seconds
+    stop(); //stops the motors
+}
+
+extern void right(float time) //moves right for (time) seconds
+{
+    Lmotor = 0.7 + trim; //set the left motor to full speed
+    Rmotor = 0.0; //set the right motor to off
+    Current_Direction = Right;
+    hold(time); //waits for (time) seconds
+    stop(); //stops the motors
+}
+
+extern void bear_Right(float time) //keeps both motors turning but will turn right
+{
+    Lmotor =  0.7 + trim; //sets the left motor to full speed
+    Rmotor = 0.1; // sets the right motor to move slowly
+    Current_Direction = TopRight; //Sets the direction to be top right
+    hold(time); // wait for (time) seconds.
+    stop(); // stops both motors.
+}
+
+extern void bear_Left(float time) // keeps both motors turning but it should slowly turn left
+{
+    Lmotor = 0.1; // sets the left motor to move slowly
+    Rmotor = 0.7 - trim; // sets the right motor to move at full speed
+    Current_Direction =  TopLeft; // sets the direction to be the top left.
+    hold(time); // wait for (time) seconds
+    stop(); // stops both motors
+}
+extern void stop() //stops the motors
+{
+    Lmotor = Rmotor = 0; //set the speed of each motor to 0
+    Current_Direction = Stop;
+}
+
+extern void readButton(float time) //checks if the button is pressed for (time) seconds
+{
+    //seg.DisplayDigits(0,0); //displays 0 0 on the seven segment display
+    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)
+    {
+        if (CurrentButtonState == 1 and LastButtonState == 0){
+            rollDice(); //rolls the dice if the button is pressed
+        }
+        LastButtonState = CurrentButtonState;
+        wait(0.01);
+    }
+}
+
+extern int rollDice() //a function that randomly generates a number and displays it on the seven segment display
+{
+    int tens; //declare two variables, tens and units to store the number to be displayed
+    int units;
+    for (int i = 20;i>0;i--){
+        tens = rand()%9; //generate a random number from 0-9 and store it in "tens"
+        units = rand()%9;
+        //seg.DisplayDigits(tens,units); //display the numbers stored in tens and units
+        wait(0.04);
+    }
+    return tens*10+units;
+}
+
+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.
+{
+    int direction_Data; //declares a variable uses to store the direction data.
+    for (float i = time; i > 0 ; i -= follow_Speed) // will repeat until time is up.
+    {
+        direction_Data = left_Sensor() - right_Sensor(); //Compare left sensor and right sensor.
+        //if direction data is positive then the right sensor is on white.
+        //if direction data is negative then the left sensor is on white.
+        if(direction_Data > 100) //data direction is significantly positive.
+        {
+            left(follow_Speed); //turns left for follow_Speed.
+        }
+        else if (direction_Data < -100 ) //data direction is significantly negative
+        {
+            right(follow_Speed); // turns right for follow_Speed
+        }
+        else // else if both sensors are within each other. 
+        {
+            forward(follow_Speed); // heads forward for follow_Speed.
+        }
+    }
+}
+
+extern void follow_Line() // a functoin that will follow a black line forever.
+{
+    int direction_Data; //declares a variable used to store the direction data.
+    for (;;) //This creates a loop which will go on forever.
+    { 
+        direction_Data = left_Sensor() - right_Sensor(); // Combines both sets of direction data into one variable.
+        if(direction_Data > 100) // if direction data is signficantly positive
+        {
+            left(follow_Speed);// Turn left, as the buggy is leaving the black line.
+        }
+        else if (direction_Data < -100 ) // if direction data is significantly negative
+        {
+            right(follow_Speed); //turn right, as the buggy is leaving the black line.
+        }
+        else // if neither other conditions are met then go forward.
+        {
+            forward(follow_Speed); // go forward, as the buggy is on the black line.
+        }
+    }
+}
+
+
+#endif // BUGGY_FUNCTIONS_C
\ No newline at end of file
diff -r 000000000000 -r 4aa6e1498925 buggy_function.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buggy_function.h	Mon Mar 27 12:14:03 2017 +0000
@@ -0,0 +1,63 @@
+/*********************************************************
+*buggy_functions.h                                       *
+*Author: Elijah Orr & Dan Argust                         *
+*                                                        *
+*A library of functions that can be used to control the  *
+*RenBuggy.                                               *
+*********************************************************/
+
+/* include guards are used to prevent problems caused by 
+multiple definitions */
+#ifndef BUGGY_FUNCTIONS_H
+#define BUGGY_FUNCTIONS_H
+
+/* mbed.h must be included in this file also */
+
+
+/* #define LeftMotorPin P0_3 tells the preprocessor to replace
+any mention of LeftMotorPin with P0_3 etc. 
+This currently used the Large pads labled 0 and 1.
+P0_18 is equivelent to pad 8.
+P0_16 is equivelent to pad 16.
+potential pads could be P0,P1,P8,P12,P2,P16
+*/
+#define LeftMotorPin P0_18
+#define RightMotorPin P0_16
+#define follow_Speed 0.3
+
+
+/*This typedef defines what direction the buggy can go in, it is also used to determine what to display.*/
+typedef enum {
+    Up,
+    Down,
+    Left,
+    Right,
+    TopLeft,
+    TopRight,
+    BottomLeft,
+    BottomRight,
+    Stop,
+    Clear,
+}Direction;
+
+/* these are function prototypes that declare all the functions
+in the library.*/
+extern void forward(float); //Move the buggy forward for (float) seconds
+extern void left(float); //Turn left for (float) seconds
+extern void right(float); //Turn right for (float) seconds
+extern void hold(float); //Hold the buggy in an idle state for (float) seconds
+extern void bear_Right(float); // Turns the buggy to the right while moving forward
+extern void bear_Left(float); // Turns the buggy to the left while moving forward
+extern void stop(); //Stop all motors
+extern void follow_Line(float);
+extern void follow_Line();
+
+extern void readButton(float); //Similar to hold, but scans for button presses while waiting
+extern int rollDice(); //Randomly generate a number a display it on the seven segment display
+
+
+/* stop() and rollDice() do not need to be giving a float
+to be run unlike the other functions which use a (float)
+to know how long they should run for */
+
+#endif // BUGGY_FUNCTIONS_H
\ No newline at end of file
diff -r 000000000000 -r 4aa6e1498925 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Mar 27 12:14:03 2017 +0000
@@ -0,0 +1,63 @@
+/*
+The MIT License (MIT)
+
+Copyright (c) 2016 British Broadcasting Corporation.
+This software is provided by Lancaster University by arrangement with the BBC.
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the "Software"),
+to deal in the Software without restriction, including without limitation
+the rights to use, copy, modify, merge, publish, distribute, sublicense,
+and/or sell copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
+*/
+
+#include "buggy_function.h"
+#include "Microbit_function.h"
+
+/* available Commands from buggy_function
+* forward
+* left
+* right
+* hold
+* stop
+* bear_Right
+* bear_Left
+* readButton
+* rolldice
+* follow_Line
+*/
+
+/* available commands from Microbit_function
+* Compass
+* pointNorth
+* pointSouth
+* Display
+*/
+int main()
+{
+    microBit_Setup();
+    stop();
+    hold(3);
+    forward(1000);
+    follow_Line(10);
+    pointNorth();
+    hold(3);
+    pointSouth();
+    while(1)
+    {
+        Compass();
+    }
+}
+
diff -r 000000000000 -r 4aa6e1498925 microbit.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/microbit.lib	Mon Mar 27 12:14:03 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/Lancaster-University/code/microbit/#4b89e7e3494f