Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 00021 #define TIME_PERIOD 2 //Constant compiler Values here 2 equates to 2ms or 500Hz base Frequency 00022 #define DUTY 0.9 //DUTY of 1.0=100%, 0.4=40% etc., 00023 00024 DigitalIn microswitch1(D4); //Instance of the DigitalIn class called 'microswitch1' 00025 DigitalIn microswitch2(D3); //Instance of the DigitalIn class called 'microswitch2' 00026 00027 Motor Wheel(D13,D11,D9,D10); //Instance of the Motor Class called 'Wheel' see motor.h and motor.cpp 00028 00029 DigitalIn myButton(USER_BUTTON); //USER_BUTTON is the Blue Button on the NUCLEO Board 00030 00031 DigitalOut led(LED3); //LED1 is the Green LED on the NUCLEO board 00032 //N.B. The RED LED is the POWER Indicator 00033 //and the Multicoloured LED indicates status of the ST-LINK Programming cycle 00034 00035 Serial pc(USBTX,USBRX); //Instance of the Serial class to enable much faster BAUD rates then standard 9600 i.e. 115200 00036 //This is Pseudo RS232 over USB the NUCLEO will appear as a COMx Port see device Manager on PC used 00037 //Use PuTTY to monitor check COMx and BAUD rate (115200) 00038 00039 00040 //Variable 'duty' for programmer to use to vary speed as required set here to #define compiler constant see above 00041 float duty=DUTY; 00042 // 00043 00044 void fwdBack(){ 00045 int fBOn = 1; 00046 while(fBOn == 1){ 00047 Wheel.Speed(0.8,0.8); 00048 if (microswitch1 == 1){ 00049 wait(0.2); 00050 Wheel.Speed(-0.8,-0.8); 00051 fBOn = 0; 00052 } 00053 } 00054 } 00055 00056 void turn(){ 00057 int tOn = 1; 00058 while (tOn == 1){ 00059 00060 if (microswitch2 == 1){ 00061 wait(0.2); 00062 Wheel.Speed(0.5,0.5); 00063 wait(0.5); 00064 Wheel.Speed(0.0,0.8); 00065 wait(1); 00066 Wheel.Speed(0.8,0.8); 00067 wait(2); 00068 Wheel.Speed(0.8,0.0); 00069 wait(0.5); 00070 tOn = 0; 00071 } 00072 } 00073 } 00074 00075 int main () 00076 { 00077 pc.baud(9600); //BAUD Rate to 9600 00078 pc.printf("ROCO104 Demonstration Robot Buggy Plymouth University 2018/19\n\r"); 00079 00080 Wheel.Period_in_ms(TIME_PERIOD);//Set frequency of the PWMs 00081 00082 // 00083 //--------------------------- your strategy goes between the two dashed lines --------------------------------------------- 00084 // 00085 Wheel.Stop(); 00086 00087 //close_encounter(1); //tune to play Announce start! 00088 //twinkle(1); //see tunes.h for alternatives or make your own! 00089 //jingle_bells(1); 00090 megolavania(1); 00091 while(myButton==0) { 00092 //Wait here for USER Button (Blue) on Nucleo Board (goes to zero when pressed) 00093 led=0; //and flash green LED whilst waiting 00094 wait(0.1); 00095 led=1; 00096 wait(0.1); 00097 //Test Microswitches with two different tones see tunes.cpp tunes.h or flash (led1 and led2) onboard LEDs 00098 00099 } 00100 00101 //Repeat the following forever NB always true! 00102 00103 // YOUR LINES OF CODE for your stratagy go between HERE! +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00104 Wheel.Period_in_ms(2);//Set frequency of the PWMs 500Hz 00105 00106 int on = 1; 00107 00108 while(on == 1){ 00109 fwdBack(); 00110 turn(); 00111 } 00112 00113 00114 00115 00116 00117 00118 00119 00120 00121 /* while (true) { 00122 00123 while(microswitch1 == 0); 00124 { 00125 Wheel.Speed(0.8,0.8); 00126 } 00127 00128 Wheel.Stop(); 00129 00130 while(microswitch2 == 0) 00131 { 00132 Wheel.Speed(-0.8,-0.8); 00133 } 00134 00135 //turn right 00136 Wheel.Speed(0.8,0.0); 00137 wait(0.5); 00138 00139 //forward 00140 Wheel.Speed(0.8,0.8); 00141 wait(0.5); 00142 00143 //turn left 00144 Wheel.Speed(0.0,0.8); 00145 wait(0.5); 00146 00147 00148 00149 } 00150 00151 */ 00152 } 00153 00154 // and HERE! +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00155 // go back to start of while loop 00156 //end of int main() 00157 00158 00159 00160 /* 00161 //Consider these lines of code to Accelerate the motors 00162 for (float i=0.5f; i<=1.0f; i+=0.01f) //Accelerate from 50% to 100% 00163 { 00164 Wheel.Speed(i,i); 00165 wait(0.1f); 00166 } 00167 */
Generated on Fri Jul 15 2022 10:18:33 by
1.7.2