ROCO 104 / Mbed 2 deprecated ROCO104_7d5m1

Dependencies:   mbed motor MUSIC board Travel

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 Simple Routine for Nucleo Board for ROCO104 Buggy Motor Control and Microswitches
00003 Heavy edit from previous ROCO103PP code
00004 Motor Class can now be instansiated with all four pins needed to control the H Bridge
00005 with a member functions as follows
00006 
00007 Motor::Speed(float A, Float B) range -0.1 to +1.0 to give full reverse to full forward for A/B motors
00008 Motor::Stop()       STOP
00009 Motor::Fwd(float)   Forward but floating point number (range 0.0 to 1.0)
00010 Motor::Rev(float)   Reverse but floating point number (range 0.0 to 1.0)
00011 
00012 Plymouth University
00013 M.Simpson 31st October 2016
00014 Edited 03/02/2017
00015 Edited 06/12/2018
00016 */
00017 #include "mbed.h"
00018 #include "motor.h"
00019 #include "tunes.h"
00020 #include "Travel.h"
00021 #include "board.h"
00022 
00023 #define TIME_PERIOD 2             //Constant compiler Values here 2 equates to 2ms or 500Hz base Frequency
00024 #define DUTY 0.9                  //DUTY of 1.0=100%, 0.4=40% etc.,
00025 
00026 
00027 
00028 DigitalIn microswitch1(D4);       //Instance of the DigitalIn class called 'microswitch1'
00029 DigitalIn microswitch2(D3);       //Instance of the DigitalIn class called 'microswitch2'
00030 
00031 Motor Wheel(D13,D11,D9,D10);      //Instance of the Motor Class called 'Wheel' see motor.h and motor.cpp
00032 
00033 DigitalIn myButton(USER_BUTTON);  //USER_BUTTON is the Blue Button on the NUCLEO Board
00034 
00035 DigitalOut led(LED3);             //LED1 is the Green LED on the NUCLEO board
00036 //N.B. The RED LED is the POWER Indicator
00037 //and the Multicoloured LED indicates status of the ST-LINK Programming cycle
00038 
00039 Serial pc(USBTX,USBRX);           //Instance of the Serial class to enable much faster BAUD rates then standard 9600 i.e. 115200
00040 //This is Pseudo RS232 over USB the NUCLEO will appear as a COMx Port see device Manager on PC used
00041 //Use PuTTY to monitor check COMx and BAUD rate (115200)
00042 
00043 
00044 //Variable 'duty' for programmer to use to vary speed as required set here to #define compiler constant see above
00045 float duty=DUTY;
00046 
00047   /*
00048 // This is the fwdBack function with acceleration. 
00049 void fwdBack() {
00050 
00051   for (float i = 0.5f; i <= 1; i += 0.01f) //Accelerate  from 50% to 100%
00052   {
00053     Wheel.Speed(i, i);
00054     wait(0.1f);
00055     if (microswitch1 == 1) {
00056       wait(0.2);
00057       break;
00058     }
00059   }
00060   if (microswitch1 == 1){
00061   for (float i = 0.5f; i <= 1; i -= 0.01f) //Accelerate  from 50% to 100% in reverse!
00062   {
00063     Wheel.Speed(i, i);
00064     wait(0.1f);
00065     if (microswitch2 == 1) {
00066       wait(0.2);
00067       break;
00068     }
00069   }
00070   if (microswitch2 == 1) {
00071     wait(0.2);
00072     Wheel.Speed(0.0, 0.0);
00073   }
00074   
00075   }
00076   
00077 */
00078 /*/ //The 3rd version including james toggles and acceleration. 
00079 void fwdBack() {
00080   int fBOn = 1;
00081   while (fBOn == 1) {
00082     for (float i = 0.5f; i <= 1; i += 0.01f) //Accelerate  from 50% to 100%
00083     {
00084       Wheel.Speed(i, i);
00085       wait(0.1f);
00086       if (microswitch1 == 1) {
00087         wait(0.2);
00088         break;
00089       }
00090     if (microswitch1 == 1) {
00091       wait(0.2);
00092       for (float i = 0.5f; i <= 1; i -= 0.01f) //Accelerate  from 50% to 100% in reverse!
00093       {
00094         Wheel.Speed(i, i);
00095         wait(0.1f);
00096         if (microswitch2 == 1) {
00097           wait(0.2);
00098           break;
00099         }
00100       fBOn = 0;
00101     }
00102   }
00103 }
00104 
00105     */
00106 
00107 
00108     
00109 int main ()
00110 {
00111     pc.baud(9600);               //BAUD Rate to 9600
00112     pc.printf("ROCO104 Demonstration Robot Buggy Plymouth University 2018/19\n\r");
00113 
00114     Wheel.Period_in_ms(TIME_PERIOD);//Set frequency of the PWMs
00115 
00116     //
00117     //--------------------------- your strategy goes between the two dashed lines ---------------------------------------------
00118     //
00119     Wheel.Stop();
00120 
00121     megolavania(1);
00122     
00123     blueButton(); 
00124     
00125 // YOUR LINES OF CODE for your stratagy go between HERE! +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00126     Wheel.Period_in_ms(2);//Set frequency of the PWMs 500Hz
00127     
00128     int on = 1;
00129     
00130     while(on == 1){
00131         fwdBack();
00132         turn();
00133     }
00134 }
00135     
00136 
00137 
00138 
00139 // and HERE! +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00140 // go back to start of while loop
00141 //end of int main()
00142 
00143 
00144 
00145 /*
00146 //Consider these lines of code to Accelerate the motors
00147       for (float i=0.5f; i<=1.0f; i+=0.01f) //Accelerate  from 50% to 100%
00148       {
00149         Wheel.Speed(i,i);
00150         wait(0.1f);
00151       }
00152 */