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@7:a96163893291, 2020-05-07 (annotated)
- Committer:
- james_plym
- Date:
- Thu May 07 16:29:28 2020 +0000
- Revision:
- 7:a96163893291
- Parent:
- 6:eac1e6a55274
- Child:
- 8:5373e5ceab2e
finish product in the basis of time not sensors
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 | } |
| james_plym | 7:a96163893291 | 55 | |
| james_plym | 7:a96163893291 | 56 | void turn(){ |
| james_plym | 7:a96163893291 | 57 | int tOn = 1; |
| james_plym | 7:a96163893291 | 58 | while (tOn == 1){ |
| james_plym | 7:a96163893291 | 59 | |
| james_plym | 7:a96163893291 | 60 | if (microswitch2 == 1){ |
| james_plym | 7:a96163893291 | 61 | wait(0.2); |
| james_plym | 7:a96163893291 | 62 | Wheel.Speed(0.5,0.5); |
| james_plym | 7:a96163893291 | 63 | wait(0.5); |
| james_plym | 7:a96163893291 | 64 | Wheel.Speed(0.0,0.8); |
| james_plym | 7:a96163893291 | 65 | wait(1); |
| james_plym | 7:a96163893291 | 66 | Wheel.Speed(0.8,0.8); |
| james_plym | 7:a96163893291 | 67 | wait(2); |
| james_plym | 7:a96163893291 | 68 | Wheel.Speed(0.8,0.0); |
| james_plym | 7:a96163893291 | 69 | wait(0.5); |
| james_plym | 7:a96163893291 | 70 | tOn = 0; |
| james_plym | 7:a96163893291 | 71 | } |
| james_plym | 7:a96163893291 | 72 | } |
| james_plym | 7:a96163893291 | 73 | } |
| james_plym | 7:a96163893291 | 74 | |
| martinsimpson | 0:51c12cc34baf | 75 | int main () |
| martinsimpson | 0:51c12cc34baf | 76 | { |
| james_plym | 6:eac1e6a55274 | 77 | pc.baud(9600); //BAUD Rate to 9600 |
| james_plym | 6:eac1e6a55274 | 78 | pc.printf("ROCO104 Demonstration Robot Buggy Plymouth University 2018/19\n\r"); |
| martinsimpson | 0:51c12cc34baf | 79 | |
| james_plym | 6:eac1e6a55274 | 80 | Wheel.Period_in_ms(TIME_PERIOD);//Set frequency of the PWMs |
| martinsimpson | 1:3ca91ad8e927 | 81 | |
| james_plym | 6:eac1e6a55274 | 82 | // |
| james_plym | 6:eac1e6a55274 | 83 | //--------------------------- your strategy goes between the two dashed lines --------------------------------------------- |
| james_plym | 6:eac1e6a55274 | 84 | // |
| martinsimpson | 1:3ca91ad8e927 | 85 | Wheel.Stop(); |
| james_plym | 6:eac1e6a55274 | 86 | |
| james_plym | 6:eac1e6a55274 | 87 | //close_encounter(1); //tune to play Announce start! |
| martinsimpson | 1:3ca91ad8e927 | 88 | //twinkle(1); //see tunes.h for alternatives or make your own! |
| martinsimpson | 1:3ca91ad8e927 | 89 | //jingle_bells(1); |
| james_plym | 6:eac1e6a55274 | 90 | megolavania(1); |
| james_plym | 6:eac1e6a55274 | 91 | while(myButton==0) { |
| james_plym | 6:eac1e6a55274 | 92 | //Wait here for USER Button (Blue) on Nucleo Board (goes to zero when pressed) |
| martinsimpson | 1:3ca91ad8e927 | 93 | led=0; //and flash green LED whilst waiting |
| martinsimpson | 0:51c12cc34baf | 94 | wait(0.1); |
| james_plym | 6:eac1e6a55274 | 95 | led=1; |
| martinsimpson | 0:51c12cc34baf | 96 | wait(0.1); |
| martinsimpson | 4:8249fab4d8d3 | 97 | //Test Microswitches with two different tones see tunes.cpp tunes.h or flash (led1 and led2) onboard LEDs |
| james_plym | 6:eac1e6a55274 | 98 | |
| martinsimpson | 0:51c12cc34baf | 99 | } |
| james_plym | 6:eac1e6a55274 | 100 | |
| james_plym | 6:eac1e6a55274 | 101 | //Repeat the following forever NB always true! |
| martinsimpson | 4:8249fab4d8d3 | 102 | |
| james_plym | 5:756dc04a891c | 103 | // YOUR LINES OF CODE for your stratagy go between HERE! +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| james_plym | 6:eac1e6a55274 | 104 | Wheel.Period_in_ms(2);//Set frequency of the PWMs 500Hz |
| james_plym | 6:eac1e6a55274 | 105 | |
| james_plym | 7:a96163893291 | 106 | int on = 1; |
| james_plym | 7:a96163893291 | 107 | |
| james_plym | 7:a96163893291 | 108 | while(on == 1){ |
| james_plym | 7:a96163893291 | 109 | fwdBack(); |
| james_plym | 7:a96163893291 | 110 | turn(); |
| james_plym | 7:a96163893291 | 111 | } |
| james_plym | 7:a96163893291 | 112 | |
| james_plym | 7:a96163893291 | 113 | |
| james_plym | 7:a96163893291 | 114 | |
| james_plym | 7:a96163893291 | 115 | |
| james_plym | 7:a96163893291 | 116 | |
| james_plym | 7:a96163893291 | 117 | |
| james_plym | 7:a96163893291 | 118 | |
| james_plym | 7:a96163893291 | 119 | |
| james_plym | 7:a96163893291 | 120 | |
| james_plym | 7:a96163893291 | 121 | /* while (true) { |
| james_plym | 6:eac1e6a55274 | 122 | |
| james_plym | 6:eac1e6a55274 | 123 | while(microswitch1 == 0); |
| james_plym | 6:eac1e6a55274 | 124 | { |
| james_plym | 6:eac1e6a55274 | 125 | Wheel.Speed(0.8,0.8); |
| james_plym | 6:eac1e6a55274 | 126 | } |
| james_plym | 6:eac1e6a55274 | 127 | |
| james_plym | 5:756dc04a891c | 128 | Wheel.Stop(); |
| james_plym | 6:eac1e6a55274 | 129 | |
| james_plym | 6:eac1e6a55274 | 130 | while(microswitch2 == 0) |
| james_plym | 6:eac1e6a55274 | 131 | { |
| james_plym | 6:eac1e6a55274 | 132 | Wheel.Speed(-0.8,-0.8); |
| james_plym | 6:eac1e6a55274 | 133 | } |
| james_plym | 6:eac1e6a55274 | 134 | |
| james_plym | 6:eac1e6a55274 | 135 | //turn right |
| james_plym | 6:eac1e6a55274 | 136 | Wheel.Speed(0.8,0.0); |
| james_plym | 6:eac1e6a55274 | 137 | wait(0.5); |
| james_plym | 6:eac1e6a55274 | 138 | |
| james_plym | 6:eac1e6a55274 | 139 | //forward |
| james_plym | 6:eac1e6a55274 | 140 | Wheel.Speed(0.8,0.8); |
| james_plym | 6:eac1e6a55274 | 141 | wait(0.5); |
| james_plym | 6:eac1e6a55274 | 142 | |
| james_plym | 6:eac1e6a55274 | 143 | //turn left |
| james_plym | 6:eac1e6a55274 | 144 | Wheel.Speed(0.0,0.8); |
| james_plym | 6:eac1e6a55274 | 145 | wait(0.5); |
| james_plym | 7:a96163893291 | 146 | |
| james_plym | 6:eac1e6a55274 | 147 | |
| james_plym | 6:eac1e6a55274 | 148 | |
| james_plym | 5:756dc04a891c | 149 | } |
| james_plym | 6:eac1e6a55274 | 150 | |
| james_plym | 7:a96163893291 | 151 | */ |
| james_plym | 7:a96163893291 | 152 | } |
| martinsimpson | 0:51c12cc34baf | 153 | |
| martinsimpson | 4:8249fab4d8d3 | 154 | // and HERE! +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| james_plym | 7:a96163893291 | 155 | // go back to start of while loop |
| james_plym | 6:eac1e6a55274 | 156 | //end of int main() |
| martinsimpson | 4:8249fab4d8d3 | 157 | |
| martinsimpson | 4:8249fab4d8d3 | 158 | |
| martinsimpson | 1:3ca91ad8e927 | 159 | |
| martinsimpson | 4:8249fab4d8d3 | 160 | /* |
| martinsimpson | 4:8249fab4d8d3 | 161 | //Consider these lines of code to Accelerate the motors |
| martinsimpson | 4:8249fab4d8d3 | 162 | for (float i=0.5f; i<=1.0f; i+=0.01f) //Accelerate from 50% to 100% |
| james_plym | 6:eac1e6a55274 | 163 | { |
| martinsimpson | 4:8249fab4d8d3 | 164 | Wheel.Speed(i,i); |
| martinsimpson | 4:8249fab4d8d3 | 165 | wait(0.1f); |
| martinsimpson | 4:8249fab4d8d3 | 166 | } |
| martinsimpson | 4:8249fab4d8d3 | 167 | */ |