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@9:681280a7a416, 2020-05-13 (annotated)
- Committer:
- james_plym
- Date:
- Wed May 13 17:09:57 2020 +0000
- Revision:
- 9:681280a7a416
- Parent:
- 8:5373e5ceab2e
- Child:
- 10:c8b9362f90d5
dw
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" |
| james_plym | 9:681280a7a416 | 20 | #include "Travel.h" |
| james_plym | 9:681280a7a416 | 21 | #include "board.h" |
| martinsimpson | 1:3ca91ad8e927 | 22 | |
| martinsimpson | 0:51c12cc34baf | 23 | #define TIME_PERIOD 2 //Constant compiler Values here 2 equates to 2ms or 500Hz base Frequency |
| martinsimpson | 0:51c12cc34baf | 24 | #define DUTY 0.9 //DUTY of 1.0=100%, 0.4=40% etc., |
| martinsimpson | 0:51c12cc34baf | 25 | |
| james_plym | 9:681280a7a416 | 26 | |
| james_plym | 9:681280a7a416 | 27 | |
| martinsimpson | 0:51c12cc34baf | 28 | DigitalIn microswitch1(D4); //Instance of the DigitalIn class called 'microswitch1' |
| martinsimpson | 0:51c12cc34baf | 29 | DigitalIn microswitch2(D3); //Instance of the DigitalIn class called 'microswitch2' |
| martinsimpson | 0:51c12cc34baf | 30 | |
| martinsimpson | 1:3ca91ad8e927 | 31 | Motor Wheel(D13,D11,D9,D10); //Instance of the Motor Class called 'Wheel' see motor.h and motor.cpp |
| martinsimpson | 0:51c12cc34baf | 32 | |
| martinsimpson | 0:51c12cc34baf | 33 | DigitalIn myButton(USER_BUTTON); //USER_BUTTON is the Blue Button on the NUCLEO Board |
| martinsimpson | 0:51c12cc34baf | 34 | |
| martinsimpson | 1:3ca91ad8e927 | 35 | DigitalOut led(LED3); //LED1 is the Green LED on the NUCLEO board |
| james_plym | 6:eac1e6a55274 | 36 | //N.B. The RED LED is the POWER Indicator |
| james_plym | 6:eac1e6a55274 | 37 | //and the Multicoloured LED indicates status of the ST-LINK Programming cycle |
| martinsimpson | 0:51c12cc34baf | 38 | |
| martinsimpson | 0:51c12cc34baf | 39 | 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 | 40 | //This is Pseudo RS232 over USB the NUCLEO will appear as a COMx Port see device Manager on PC used |
| james_plym | 6:eac1e6a55274 | 41 | //Use PuTTY to monitor check COMx and BAUD rate (115200) |
| martinsimpson | 0:51c12cc34baf | 42 | |
| martinsimpson | 0:51c12cc34baf | 43 | |
| martinsimpson | 0:51c12cc34baf | 44 | //Variable 'duty' for programmer to use to vary speed as required set here to #define compiler constant see above |
| martinsimpson | 0:51c12cc34baf | 45 | float duty=DUTY; |
| martinsimpson | 0:51c12cc34baf | 46 | // |
| james_plym | 7:a96163893291 | 47 | |
| james_plym | 9:681280a7a416 | 48 | |
| Thawedmind | 8:5373e5ceab2e | 49 | |
| james_plym | 9:681280a7a416 | 50 | /* |
| Thawedmind | 8:5373e5ceab2e | 51 | // This is the fwdBack function with acceleration. |
| james_plym | 9:681280a7a416 | 52 | void fwdBack() { |
| Thawedmind | 8:5373e5ceab2e | 53 | |
| Thawedmind | 8:5373e5ceab2e | 54 | for (float i = 0.5f; i <= 1; i += 0.01f) //Accelerate from 50% to 100% |
| Thawedmind | 8:5373e5ceab2e | 55 | { |
| Thawedmind | 8:5373e5ceab2e | 56 | Wheel.Speed(i, i); |
| Thawedmind | 8:5373e5ceab2e | 57 | wait(0.1f); |
| Thawedmind | 8:5373e5ceab2e | 58 | if (microswitch1 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 59 | wait(0.2); |
| Thawedmind | 8:5373e5ceab2e | 60 | break; |
| Thawedmind | 8:5373e5ceab2e | 61 | } |
| Thawedmind | 8:5373e5ceab2e | 62 | } |
| Thawedmind | 8:5373e5ceab2e | 63 | if (microswitch1 == 1){ |
| Thawedmind | 8:5373e5ceab2e | 64 | for (float i = 0.5f; i <= 1; i -= 0.01f) //Accelerate from 50% to 100% in reverse! |
| Thawedmind | 8:5373e5ceab2e | 65 | { |
| Thawedmind | 8:5373e5ceab2e | 66 | Wheel.Speed(i, i); |
| Thawedmind | 8:5373e5ceab2e | 67 | wait(0.1f); |
| Thawedmind | 8:5373e5ceab2e | 68 | if (microswitch2 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 69 | wait(0.2); |
| Thawedmind | 8:5373e5ceab2e | 70 | break; |
| Thawedmind | 8:5373e5ceab2e | 71 | } |
| Thawedmind | 8:5373e5ceab2e | 72 | } |
| Thawedmind | 8:5373e5ceab2e | 73 | if (microswitch2 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 74 | wait(0.2); |
| james_plym | 9:681280a7a416 | 75 | Wheel.Speed(0.0, 0.0); |
| Thawedmind | 8:5373e5ceab2e | 76 | } |
| james_plym | 9:681280a7a416 | 77 | |
| Thawedmind | 8:5373e5ceab2e | 78 | } |
| james_plym | 9:681280a7a416 | 79 | |
| james_plym | 9:681280a7a416 | 80 | */ |
| james_plym | 9:681280a7a416 | 81 | /*/ //The 3rd version including james toggles and acceleration. |
| james_plym | 9:681280a7a416 | 82 | void fwdBack() { |
| Thawedmind | 8:5373e5ceab2e | 83 | int fBOn = 1; |
| Thawedmind | 8:5373e5ceab2e | 84 | while (fBOn == 1) { |
| Thawedmind | 8:5373e5ceab2e | 85 | for (float i = 0.5f; i <= 1; i += 0.01f) //Accelerate from 50% to 100% |
| Thawedmind | 8:5373e5ceab2e | 86 | { |
| Thawedmind | 8:5373e5ceab2e | 87 | Wheel.Speed(i, i); |
| Thawedmind | 8:5373e5ceab2e | 88 | wait(0.1f); |
| Thawedmind | 8:5373e5ceab2e | 89 | if (microswitch1 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 90 | wait(0.2); |
| Thawedmind | 8:5373e5ceab2e | 91 | break; |
| Thawedmind | 8:5373e5ceab2e | 92 | } |
| Thawedmind | 8:5373e5ceab2e | 93 | if (microswitch1 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 94 | wait(0.2); |
| Thawedmind | 8:5373e5ceab2e | 95 | for (float i = 0.5f; i <= 1; i -= 0.01f) //Accelerate from 50% to 100% in reverse! |
| Thawedmind | 8:5373e5ceab2e | 96 | { |
| Thawedmind | 8:5373e5ceab2e | 97 | Wheel.Speed(i, i); |
| Thawedmind | 8:5373e5ceab2e | 98 | wait(0.1f); |
| Thawedmind | 8:5373e5ceab2e | 99 | if (microswitch2 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 100 | wait(0.2); |
| Thawedmind | 8:5373e5ceab2e | 101 | break; |
| Thawedmind | 8:5373e5ceab2e | 102 | } |
| Thawedmind | 8:5373e5ceab2e | 103 | fBOn = 0; |
| Thawedmind | 8:5373e5ceab2e | 104 | } |
| Thawedmind | 8:5373e5ceab2e | 105 | } |
| Thawedmind | 8:5373e5ceab2e | 106 | } |
| Thawedmind | 8:5373e5ceab2e | 107 | |
| Thawedmind | 8:5373e5ceab2e | 108 | */ |
| james_plym | 7:a96163893291 | 109 | |
| james_plym | 9:681280a7a416 | 110 | |
| james_plym | 7:a96163893291 | 111 | |
| martinsimpson | 0:51c12cc34baf | 112 | int main () |
| martinsimpson | 0:51c12cc34baf | 113 | { |
| james_plym | 6:eac1e6a55274 | 114 | pc.baud(9600); //BAUD Rate to 9600 |
| james_plym | 6:eac1e6a55274 | 115 | pc.printf("ROCO104 Demonstration Robot Buggy Plymouth University 2018/19\n\r"); |
| martinsimpson | 0:51c12cc34baf | 116 | |
| james_plym | 6:eac1e6a55274 | 117 | Wheel.Period_in_ms(TIME_PERIOD);//Set frequency of the PWMs |
| martinsimpson | 1:3ca91ad8e927 | 118 | |
| james_plym | 6:eac1e6a55274 | 119 | // |
| james_plym | 6:eac1e6a55274 | 120 | //--------------------------- your strategy goes between the two dashed lines --------------------------------------------- |
| james_plym | 6:eac1e6a55274 | 121 | // |
| martinsimpson | 1:3ca91ad8e927 | 122 | Wheel.Stop(); |
| james_plym | 6:eac1e6a55274 | 123 | |
| james_plym | 6:eac1e6a55274 | 124 | megolavania(1); |
| james_plym | 9:681280a7a416 | 125 | |
| james_plym | 9:681280a7a416 | 126 | blueButton(); |
| james_plym | 9:681280a7a416 | 127 | |
| james_plym | 5:756dc04a891c | 128 | // YOUR LINES OF CODE for your stratagy go between HERE! +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| james_plym | 6:eac1e6a55274 | 129 | Wheel.Period_in_ms(2);//Set frequency of the PWMs 500Hz |
| james_plym | 6:eac1e6a55274 | 130 | |
| james_plym | 7:a96163893291 | 131 | int on = 1; |
| james_plym | 7:a96163893291 | 132 | |
| james_plym | 7:a96163893291 | 133 | while(on == 1){ |
| james_plym | 7:a96163893291 | 134 | fwdBack(); |
| james_plym | 7:a96163893291 | 135 | turn(); |
| james_plym | 7:a96163893291 | 136 | } |
| james_plym | 9:681280a7a416 | 137 | } |
| james_plym | 7:a96163893291 | 138 | |
| james_plym | 6:eac1e6a55274 | 139 | |
| james_plym | 6:eac1e6a55274 | 140 | |
| martinsimpson | 0:51c12cc34baf | 141 | |
| martinsimpson | 4:8249fab4d8d3 | 142 | // and HERE! +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| james_plym | 7:a96163893291 | 143 | // go back to start of while loop |
| james_plym | 6:eac1e6a55274 | 144 | //end of int main() |
| martinsimpson | 4:8249fab4d8d3 | 145 | |
| martinsimpson | 4:8249fab4d8d3 | 146 | |
| martinsimpson | 1:3ca91ad8e927 | 147 | |
| martinsimpson | 4:8249fab4d8d3 | 148 | /* |
| martinsimpson | 4:8249fab4d8d3 | 149 | //Consider these lines of code to Accelerate the motors |
| martinsimpson | 4:8249fab4d8d3 | 150 | for (float i=0.5f; i<=1.0f; i+=0.01f) //Accelerate from 50% to 100% |
| james_plym | 6:eac1e6a55274 | 151 | { |
| martinsimpson | 4:8249fab4d8d3 | 152 | Wheel.Speed(i,i); |
| martinsimpson | 4:8249fab4d8d3 | 153 | wait(0.1f); |
| martinsimpson | 4:8249fab4d8d3 | 154 | } |
| martinsimpson | 4:8249fab4d8d3 | 155 | */ |