Rachel Ireland-Jones / Mbed 2 deprecated BuggyDesign

Dependencies:   Movement mbed tunes motor pwm_tone clickers Ultra

Revision:
7:48bf77d076fc
Parent:
5:a0090b8b327b
Child:
8:f78d756832ad
diff -r 9f52f3167cb0 -r 48bf77d076fc main.cpp
--- a/main.cpp	Mon Feb 10 10:57:56 2020 +0000
+++ b/main.cpp	Sat Feb 15 12:46:10 2020 +0000
@@ -18,16 +18,14 @@
 #include "motor.h"
 #include "tunes.h"
 #include "clickers.h"
+#include "ultrasonic.h"
 #define TIME_PERIOD 2             //Constant compiler Values here 2 equates to 2ms or 500Hz base Frequency
 #define DUTY 0.9                  //DUTY of 1.0=100%, 0.4=40% etc.,
 
 DigitalIn microswitch1(D4);       //Instance of the DigitalIn class called 'microswitch1'
 DigitalIn microswitch2(D3);       //Instance of the DigitalIn class called 'microswitch2'
-
-Motor Wheel(D13,D11,D9,D10);      //Instance of the Motor Class called 'Wheel' see motor.h and motor.cpp
-
+Motor Wheel(D13,D11,D9,D10);      //Instance of the Motor Class called 'Wheel' see motor.h and motor.cpp 
 DigitalIn myButton(USER_BUTTON);  //USER_BUTTON is the Blue Button on the NUCLEO Board
-
 DigitalOut led(LED3);             //LED1 is the Green LED on the NUCLEO board
                                   //N.B. The RED LED is the POWER Indicator
                                   //and the Multicoloured LED indicates status of the ST-LINK Programming cycle
@@ -35,27 +33,27 @@
 Serial pc(USBTX,USBRX);           //Instance of the Serial class to enable much faster BAUD rates then standard 9600 i.e. 115200
                                   //This is Pseudo RS232 over USB the NUCLEO will appear as a COMx Port see device Manager on PC used
                                   //Use PuTTY to monitor check COMx and BAUD rate (115200)
-
+int mm;
+void dist(int distance)
+{
+    //put code here to happen when the distance is changed
+    printf("%dmm\r\n", distance);
+    mm = distance;
+}
+ultrasonic mu(PE_9, PF_15, .1, 1, &dist); //Trigger is PE_9, echo is PF_15. have updates every .1 seconds and a timeout after 1 second, and call dist when the distance changes
 
-//Variable 'duty' for programmer to use to vary speed as required set here to #define compiler constant see above
-float duty=DUTY;
-//
 int main ()
 {
-  pc.baud(9600);               //BAUD Rate to 9600
-  pc.printf("ROCO104 Demonstration Robot Buggy Plymouth University 2018/19\n\r");
-
-  Wheel.Period_in_ms(TIME_PERIOD);//Set frequency of the PWMs
-
-  //
-  //--------------------------- your strategy goes between the two dashed lines ---------------------------------------------   
-  //
+    pc.baud(9600);               //BAUD Rate to 9600
+    pc.printf("ROCO104 Demonstration Robot Buggy Plymouth University 2018/19\n\r");
+    mu.startUpdates();//start mesuring the distance
+    Wheel.Period_in_ms(TIME_PERIOD);//Set frequency of the PWMs
     Wheel.Stop();
     
     close_encounter(1);     //tune to play Announce start!
     //twinkle(1);           //see tunes.h for alternatives or make your own!
     //jingle_bells(1);
-
+    
     while(myButton==0)
     {                       //Wait here for USER Button (Blue) on Nucleo Board (goes to zero when pressed)
         led=0;              //and flash green LED whilst waiting
@@ -63,23 +61,13 @@
         led=1; 
         wait(0.1);
         //Test Microswitches with two different tones see tunes.cpp tunes.h or flash (led1 and led2) onboard LEDs
- 
     }
     Wheel.Period_in_ms(2);//Set frequency of the PWMs 500Hz
+    
     while(true)                             //Repeat the following forever NB always true!
     {
-        Wheel.Speed(0.8,0.8);//Forward 80%
+        Wheel.Speed(-0.8,-0.8);//Backwards 80%
+        mu.checkDistance();     //call checkDistance() as much as possible, as this is where
         RevStop();
     } // go back to start of while loop
 }  //end of int main()
-
-
-
-/*
-//Consider these lines of code to Accelerate the motors
-      for (float i=0.5f; i<=1.0f; i+=0.01f) //Accelerate  from 50% to 100%
-      { 
-        Wheel.Speed(i,i);
-        wait(0.1f);
-      }
-*/