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.
Dependencies: mbed motor MUSIC board Travel
main.cpp@8:5373e5ceab2e, 2020-05-08 (annotated)
- Committer:
- Thawedmind
- Date:
- Fri May 08 10:26:06 2020 +0000
- Revision:
- 8:5373e5ceab2e
- Parent:
- 7:a96163893291
- Child:
- 9:681280a7a416
added two fwdBack functions (commented out) needs testing by JF
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| martinsimpson | 0:51c12cc34baf | 1 | /* |
| martinsimpson | 1:3ca91ad8e927 | 2 | Simple Routine for Nucleo Board for ROCO104 Buggy Motor Control and Microswitches |
| martinsimpson | 1:3ca91ad8e927 | 3 | Heavy edit from previous ROCO103PP code |
| martinsimpson | 1:3ca91ad8e927 | 4 | Motor Class can now be instansiated with all four pins needed to control the H Bridge |
| martinsimpson | 1:3ca91ad8e927 | 5 | with a member functions as follows |
| martinsimpson | 1:3ca91ad8e927 | 6 | |
| martinsimpson | 1:3ca91ad8e927 | 7 | Motor::Speed(float A, Float B) range -0.1 to +1.0 to give full reverse to full forward for A/B motors |
| martinsimpson | 1:3ca91ad8e927 | 8 | Motor::Stop() STOP |
| martinsimpson | 1:3ca91ad8e927 | 9 | Motor::Fwd(float) Forward but floating point number (range 0.0 to 1.0) |
| martinsimpson | 1:3ca91ad8e927 | 10 | Motor::Rev(float) Reverse but floating point number (range 0.0 to 1.0) |
| martinsimpson | 1:3ca91ad8e927 | 11 | |
| martinsimpson | 0:51c12cc34baf | 12 | Plymouth University |
| martinsimpson | 0:51c12cc34baf | 13 | M.Simpson 31st October 2016 |
| martinsimpson | 1:3ca91ad8e927 | 14 | Edited 03/02/2017 |
| martinsimpson | 1:3ca91ad8e927 | 15 | Edited 06/12/2018 |
| martinsimpson | 0:51c12cc34baf | 16 | */ |
| martinsimpson | 0:51c12cc34baf | 17 | #include "mbed.h" |
| martinsimpson | 0:51c12cc34baf | 18 | #include "motor.h" |
| martinsimpson | 0:51c12cc34baf | 19 | #include "tunes.h" |
| martinsimpson | 1:3ca91ad8e927 | 20 | |
| martinsimpson | 0:51c12cc34baf | 21 | #define TIME_PERIOD 2 //Constant compiler Values here 2 equates to 2ms or 500Hz base Frequency |
| martinsimpson | 0:51c12cc34baf | 22 | #define DUTY 0.9 //DUTY of 1.0=100%, 0.4=40% etc., |
| martinsimpson | 0:51c12cc34baf | 23 | |
| martinsimpson | 0:51c12cc34baf | 24 | DigitalIn microswitch1(D4); //Instance of the DigitalIn class called 'microswitch1' |
| martinsimpson | 0:51c12cc34baf | 25 | DigitalIn microswitch2(D3); //Instance of the DigitalIn class called 'microswitch2' |
| martinsimpson | 0:51c12cc34baf | 26 | |
| martinsimpson | 1:3ca91ad8e927 | 27 | Motor Wheel(D13,D11,D9,D10); //Instance of the Motor Class called 'Wheel' see motor.h and motor.cpp |
| martinsimpson | 0:51c12cc34baf | 28 | |
| martinsimpson | 0:51c12cc34baf | 29 | DigitalIn myButton(USER_BUTTON); //USER_BUTTON is the Blue Button on the NUCLEO Board |
| martinsimpson | 0:51c12cc34baf | 30 | |
| martinsimpson | 1:3ca91ad8e927 | 31 | DigitalOut led(LED3); //LED1 is the Green LED on the NUCLEO board |
| james_plym | 6:eac1e6a55274 | 32 | //N.B. The RED LED is the POWER Indicator |
| james_plym | 6:eac1e6a55274 | 33 | //and the Multicoloured LED indicates status of the ST-LINK Programming cycle |
| martinsimpson | 0:51c12cc34baf | 34 | |
| martinsimpson | 0:51c12cc34baf | 35 | Serial pc(USBTX,USBRX); //Instance of the Serial class to enable much faster BAUD rates then standard 9600 i.e. 115200 |
| james_plym | 6:eac1e6a55274 | 36 | //This is Pseudo RS232 over USB the NUCLEO will appear as a COMx Port see device Manager on PC used |
| james_plym | 6:eac1e6a55274 | 37 | //Use PuTTY to monitor check COMx and BAUD rate (115200) |
| martinsimpson | 0:51c12cc34baf | 38 | |
| martinsimpson | 0:51c12cc34baf | 39 | |
| martinsimpson | 0:51c12cc34baf | 40 | //Variable 'duty' for programmer to use to vary speed as required set here to #define compiler constant see above |
| martinsimpson | 0:51c12cc34baf | 41 | float duty=DUTY; |
| martinsimpson | 0:51c12cc34baf | 42 | // |
| james_plym | 7:a96163893291 | 43 | |
| james_plym | 7:a96163893291 | 44 | void fwdBack(){ |
| james_plym | 7:a96163893291 | 45 | int fBOn = 1; |
| james_plym | 7:a96163893291 | 46 | while(fBOn == 1){ |
| james_plym | 7:a96163893291 | 47 | Wheel.Speed(0.8,0.8); |
| james_plym | 7:a96163893291 | 48 | if (microswitch1 == 1){ |
| james_plym | 7:a96163893291 | 49 | wait(0.2); |
| james_plym | 7:a96163893291 | 50 | Wheel.Speed(-0.8,-0.8); |
| james_plym | 7:a96163893291 | 51 | fBOn = 0; |
| james_plym | 7:a96163893291 | 52 | } |
| james_plym | 7:a96163893291 | 53 | } |
| james_plym | 7:a96163893291 | 54 | } |
| Thawedmind | 8:5373e5ceab2e | 55 | |
| Thawedmind | 8:5373e5ceab2e | 56 | /* |
| Thawedmind | 8:5373e5ceab2e | 57 | // This is the fwdBack function with acceleration. |
| Thawedmind | 8:5373e5ceab2e | 58 | void fwdBack2() { |
| Thawedmind | 8:5373e5ceab2e | 59 | |
| Thawedmind | 8:5373e5ceab2e | 60 | for (float i = 0.5f; i <= 1; i += 0.01f) //Accelerate from 50% to 100% |
| Thawedmind | 8:5373e5ceab2e | 61 | { |
| Thawedmind | 8:5373e5ceab2e | 62 | Wheel.Speed(i, i); |
| Thawedmind | 8:5373e5ceab2e | 63 | wait(0.1f); |
| Thawedmind | 8:5373e5ceab2e | 64 | if (microswitch1 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 65 | wait(0.2); |
| Thawedmind | 8:5373e5ceab2e | 66 | break; |
| Thawedmind | 8:5373e5ceab2e | 67 | } |
| Thawedmind | 8:5373e5ceab2e | 68 | } |
| Thawedmind | 8:5373e5ceab2e | 69 | if (microswitch1 == 1){ |
| Thawedmind | 8:5373e5ceab2e | 70 | for (float i = 0.5f; i <= 1; i -= 0.01f) //Accelerate from 50% to 100% in reverse! |
| Thawedmind | 8:5373e5ceab2e | 71 | { |
| Thawedmind | 8:5373e5ceab2e | 72 | Wheel.Speed(i, i); |
| Thawedmind | 8:5373e5ceab2e | 73 | wait(0.1f); |
| Thawedmind | 8:5373e5ceab2e | 74 | if (microswitch2 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 75 | wait(0.2); |
| Thawedmind | 8:5373e5ceab2e | 76 | break; |
| Thawedmind | 8:5373e5ceab2e | 77 | } |
| Thawedmind | 8:5373e5ceab2e | 78 | } |
| Thawedmind | 8:5373e5ceab2e | 79 | if (microswitch2 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 80 | wait(0.2); |
| Thawedmind | 8:5373e5ceab2e | 81 | wheel.Speed(0.0, 0.0); |
| Thawedmind | 8:5373e5ceab2e | 82 | } |
| Thawedmind | 8:5373e5ceab2e | 83 | return 0; |
| Thawedmind | 8:5373e5ceab2e | 84 | } |
| Thawedmind | 8:5373e5ceab2e | 85 | */ |
| Thawedmind | 8:5373e5ceab2e | 86 | |
| Thawedmind | 8:5373e5ceab2e | 87 | /*/ //The 3rd version including jaimes toggles and acceleration. |
| Thawedmind | 8:5373e5ceab2e | 88 | void fwdBack3() { |
| Thawedmind | 8:5373e5ceab2e | 89 | int fBOn = 1; |
| Thawedmind | 8:5373e5ceab2e | 90 | while (fBOn == 1) { |
| Thawedmind | 8:5373e5ceab2e | 91 | for (float i = 0.5f; i <= 1; i += 0.01f) //Accelerate from 50% to 100% |
| Thawedmind | 8:5373e5ceab2e | 92 | { |
| Thawedmind | 8:5373e5ceab2e | 93 | Wheel.Speed(i, i); |
| Thawedmind | 8:5373e5ceab2e | 94 | wait(0.1f); |
| Thawedmind | 8:5373e5ceab2e | 95 | if (microswitch1 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 96 | wait(0.2); |
| Thawedmind | 8:5373e5ceab2e | 97 | break; |
| Thawedmind | 8:5373e5ceab2e | 98 | } |
| Thawedmind | 8:5373e5ceab2e | 99 | if (microswitch1 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 100 | wait(0.2); |
| Thawedmind | 8:5373e5ceab2e | 101 | for (float i = 0.5f; i <= 1; i -= 0.01f) //Accelerate from 50% to 100% in reverse! |
| Thawedmind | 8:5373e5ceab2e | 102 | { |
| Thawedmind | 8:5373e5ceab2e | 103 | Wheel.Speed(i, i); |
| Thawedmind | 8:5373e5ceab2e | 104 | wait(0.1f); |
| Thawedmind | 8:5373e5ceab2e | 105 | if (microswitch2 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 106 | wait(0.2); |
| Thawedmind | 8:5373e5ceab2e | 107 | break; |
| Thawedmind | 8:5373e5ceab2e | 108 | } |
| Thawedmind | 8:5373e5ceab2e | 109 | fBOn = 0; |
| Thawedmind | 8:5373e5ceab2e | 110 | } |
| Thawedmind | 8:5373e5ceab2e | 111 | } |
| Thawedmind | 8:5373e5ceab2e | 112 | } |
| Thawedmind | 8:5373e5ceab2e | 113 | |
| Thawedmind | 8:5373e5ceab2e | 114 | */ |
| james_plym | 7:a96163893291 | 115 | |
| james_plym | 7:a96163893291 | 116 | void turn(){ |
| james_plym | 7:a96163893291 | 117 | int tOn = 1; |
| james_plym | 7:a96163893291 | 118 | while (tOn == 1){ |
| james_plym | 7:a96163893291 | 119 | |
| james_plym | 7:a96163893291 | 120 | if (microswitch2 == 1){ |
| james_plym | 7:a96163893291 | 121 | wait(0.2); |
| james_plym | 7:a96163893291 | 122 | Wheel.Speed(0.5,0.5); |
| james_plym | 7:a96163893291 | 123 | wait(0.5); |
| james_plym | 7:a96163893291 | 124 | Wheel.Speed(0.0,0.8); |
| james_plym | 7:a96163893291 | 125 | wait(1); |
| james_plym | 7:a96163893291 | 126 | Wheel.Speed(0.8,0.8); |
| james_plym | 7:a96163893291 | 127 | wait(2); |
| james_plym | 7:a96163893291 | 128 | Wheel.Speed(0.8,0.0); |
| james_plym | 7:a96163893291 | 129 | wait(0.5); |
| james_plym | 7:a96163893291 | 130 | tOn = 0; |
| james_plym | 7:a96163893291 | 131 | } |
| james_plym | 7:a96163893291 | 132 | } |
| james_plym | 7:a96163893291 | 133 | } |
| james_plym | 7:a96163893291 | 134 | |
| martinsimpson | 0:51c12cc34baf | 135 | int main () |
| martinsimpson | 0:51c12cc34baf | 136 | { |
| james_plym | 6:eac1e6a55274 | 137 | pc.baud(9600); //BAUD Rate to 9600 |
| james_plym | 6:eac1e6a55274 | 138 | pc.printf("ROCO104 Demonstration Robot Buggy Plymouth University 2018/19\n\r"); |
| martinsimpson | 0:51c12cc34baf | 139 | |
| james_plym | 6:eac1e6a55274 | 140 | Wheel.Period_in_ms(TIME_PERIOD);//Set frequency of the PWMs |
| martinsimpson | 1:3ca91ad8e927 | 141 | |
| james_plym | 6:eac1e6a55274 | 142 | // |
| james_plym | 6:eac1e6a55274 | 143 | //--------------------------- your strategy goes between the two dashed lines --------------------------------------------- |
| james_plym | 6:eac1e6a55274 | 144 | // |
| martinsimpson | 1:3ca91ad8e927 | 145 | Wheel.Stop(); |
| james_plym | 6:eac1e6a55274 | 146 | |
| james_plym | 6:eac1e6a55274 | 147 | //close_encounter(1); //tune to play Announce start! |
| martinsimpson | 1:3ca91ad8e927 | 148 | //twinkle(1); //see tunes.h for alternatives or make your own! |
| martinsimpson | 1:3ca91ad8e927 | 149 | //jingle_bells(1); |
| james_plym | 6:eac1e6a55274 | 150 | megolavania(1); |
| james_plym | 6:eac1e6a55274 | 151 | while(myButton==0) { |
| james_plym | 6:eac1e6a55274 | 152 | //Wait here for USER Button (Blue) on Nucleo Board (goes to zero when pressed) |
| martinsimpson | 1:3ca91ad8e927 | 153 | led=0; //and flash green LED whilst waiting |
| martinsimpson | 0:51c12cc34baf | 154 | wait(0.1); |
| james_plym | 6:eac1e6a55274 | 155 | led=1; |
| martinsimpson | 0:51c12cc34baf | 156 | wait(0.1); |
| martinsimpson | 4:8249fab4d8d3 | 157 | //Test Microswitches with two different tones see tunes.cpp tunes.h or flash (led1 and led2) onboard LEDs |
| james_plym | 6:eac1e6a55274 | 158 | |
| martinsimpson | 0:51c12cc34baf | 159 | } |
| james_plym | 6:eac1e6a55274 | 160 | |
| james_plym | 6:eac1e6a55274 | 161 | //Repeat the following forever NB always true! |
| martinsimpson | 4:8249fab4d8d3 | 162 | |
| james_plym | 5:756dc04a891c | 163 | // YOUR LINES OF CODE for your stratagy go between HERE! +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| james_plym | 6:eac1e6a55274 | 164 | Wheel.Period_in_ms(2);//Set frequency of the PWMs 500Hz |
| james_plym | 6:eac1e6a55274 | 165 | |
| james_plym | 7:a96163893291 | 166 | int on = 1; |
| james_plym | 7:a96163893291 | 167 | |
| james_plym | 7:a96163893291 | 168 | while(on == 1){ |
| james_plym | 7:a96163893291 | 169 | fwdBack(); |
| james_plym | 7:a96163893291 | 170 | turn(); |
| james_plym | 7:a96163893291 | 171 | } |
| james_plym | 7:a96163893291 | 172 | |
| james_plym | 7:a96163893291 | 173 | |
| james_plym | 7:a96163893291 | 174 | |
| james_plym | 7:a96163893291 | 175 | |
| james_plym | 7:a96163893291 | 176 | |
| james_plym | 7:a96163893291 | 177 | |
| james_plym | 7:a96163893291 | 178 | |
| james_plym | 7:a96163893291 | 179 | |
| james_plym | 7:a96163893291 | 180 | |
| james_plym | 7:a96163893291 | 181 | /* while (true) { |
| james_plym | 6:eac1e6a55274 | 182 | |
| james_plym | 6:eac1e6a55274 | 183 | while(microswitch1 == 0); |
| james_plym | 6:eac1e6a55274 | 184 | { |
| james_plym | 6:eac1e6a55274 | 185 | Wheel.Speed(0.8,0.8); |
| james_plym | 6:eac1e6a55274 | 186 | } |
| james_plym | 6:eac1e6a55274 | 187 | |
| james_plym | 5:756dc04a891c | 188 | Wheel.Stop(); |
| james_plym | 6:eac1e6a55274 | 189 | |
| james_plym | 6:eac1e6a55274 | 190 | while(microswitch2 == 0) |
| james_plym | 6:eac1e6a55274 | 191 | { |
| james_plym | 6:eac1e6a55274 | 192 | Wheel.Speed(-0.8,-0.8); |
| james_plym | 6:eac1e6a55274 | 193 | } |
| james_plym | 6:eac1e6a55274 | 194 | |
| james_plym | 6:eac1e6a55274 | 195 | //turn right |
| james_plym | 6:eac1e6a55274 | 196 | Wheel.Speed(0.8,0.0); |
| james_plym | 6:eac1e6a55274 | 197 | wait(0.5); |
| james_plym | 6:eac1e6a55274 | 198 | |
| james_plym | 6:eac1e6a55274 | 199 | //forward |
| james_plym | 6:eac1e6a55274 | 200 | Wheel.Speed(0.8,0.8); |
| james_plym | 6:eac1e6a55274 | 201 | wait(0.5); |
| james_plym | 6:eac1e6a55274 | 202 | |
| james_plym | 6:eac1e6a55274 | 203 | //turn left |
| james_plym | 6:eac1e6a55274 | 204 | Wheel.Speed(0.0,0.8); |
| james_plym | 6:eac1e6a55274 | 205 | wait(0.5); |
| james_plym | 7:a96163893291 | 206 | |
| james_plym | 6:eac1e6a55274 | 207 | |
| james_plym | 6:eac1e6a55274 | 208 | |
| james_plym | 5:756dc04a891c | 209 | } |
| james_plym | 6:eac1e6a55274 | 210 | |
| james_plym | 7:a96163893291 | 211 | */ |
| james_plym | 7:a96163893291 | 212 | } |
| martinsimpson | 0:51c12cc34baf | 213 | |
| martinsimpson | 4:8249fab4d8d3 | 214 | // and HERE! +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| james_plym | 7:a96163893291 | 215 | // go back to start of while loop |
| james_plym | 6:eac1e6a55274 | 216 | //end of int main() |
| martinsimpson | 4:8249fab4d8d3 | 217 | |
| martinsimpson | 4:8249fab4d8d3 | 218 | |
| martinsimpson | 1:3ca91ad8e927 | 219 | |
| martinsimpson | 4:8249fab4d8d3 | 220 | /* |
| martinsimpson | 4:8249fab4d8d3 | 221 | //Consider these lines of code to Accelerate the motors |
| martinsimpson | 4:8249fab4d8d3 | 222 | for (float i=0.5f; i<=1.0f; i+=0.01f) //Accelerate from 50% to 100% |
| james_plym | 6:eac1e6a55274 | 223 | { |
| martinsimpson | 4:8249fab4d8d3 | 224 | Wheel.Speed(i,i); |
| martinsimpson | 4:8249fab4d8d3 | 225 | wait(0.1f); |
| martinsimpson | 4:8249fab4d8d3 | 226 | } |
| martinsimpson | 4:8249fab4d8d3 | 227 | */ |