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@10:c8b9362f90d5, 2020-05-14 (annotated)
- Committer:
- james_plym
- Date:
- Thu May 14 16:41:15 2020 +0000
- Revision:
- 10:c8b9362f90d5
- Parent:
- 9:681280a7a416
finished
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; |
| james_plym | 7:a96163893291 | 46 | |
| james_plym | 10:c8b9362f90d5 | 47 | /* |
| Thawedmind | 8:5373e5ceab2e | 48 | // This is the fwdBack function with acceleration. |
| james_plym | 9:681280a7a416 | 49 | void fwdBack() { |
| Thawedmind | 8:5373e5ceab2e | 50 | |
| Thawedmind | 8:5373e5ceab2e | 51 | for (float i = 0.5f; i <= 1; i += 0.01f) //Accelerate from 50% to 100% |
| Thawedmind | 8:5373e5ceab2e | 52 | { |
| Thawedmind | 8:5373e5ceab2e | 53 | Wheel.Speed(i, i); |
| Thawedmind | 8:5373e5ceab2e | 54 | wait(0.1f); |
| Thawedmind | 8:5373e5ceab2e | 55 | if (microswitch1 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 56 | wait(0.2); |
| Thawedmind | 8:5373e5ceab2e | 57 | break; |
| Thawedmind | 8:5373e5ceab2e | 58 | } |
| Thawedmind | 8:5373e5ceab2e | 59 | } |
| Thawedmind | 8:5373e5ceab2e | 60 | if (microswitch1 == 1){ |
| Thawedmind | 8:5373e5ceab2e | 61 | for (float i = 0.5f; i <= 1; i -= 0.01f) //Accelerate from 50% to 100% in reverse! |
| Thawedmind | 8:5373e5ceab2e | 62 | { |
| Thawedmind | 8:5373e5ceab2e | 63 | Wheel.Speed(i, i); |
| Thawedmind | 8:5373e5ceab2e | 64 | wait(0.1f); |
| Thawedmind | 8:5373e5ceab2e | 65 | if (microswitch2 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 66 | wait(0.2); |
| Thawedmind | 8:5373e5ceab2e | 67 | break; |
| Thawedmind | 8:5373e5ceab2e | 68 | } |
| Thawedmind | 8:5373e5ceab2e | 69 | } |
| Thawedmind | 8:5373e5ceab2e | 70 | if (microswitch2 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 71 | wait(0.2); |
| james_plym | 9:681280a7a416 | 72 | Wheel.Speed(0.0, 0.0); |
| Thawedmind | 8:5373e5ceab2e | 73 | } |
| james_plym | 9:681280a7a416 | 74 | |
| Thawedmind | 8:5373e5ceab2e | 75 | } |
| james_plym | 9:681280a7a416 | 76 | |
| james_plym | 9:681280a7a416 | 77 | */ |
| james_plym | 9:681280a7a416 | 78 | /*/ //The 3rd version including james toggles and acceleration. |
| james_plym | 9:681280a7a416 | 79 | void fwdBack() { |
| Thawedmind | 8:5373e5ceab2e | 80 | int fBOn = 1; |
| Thawedmind | 8:5373e5ceab2e | 81 | while (fBOn == 1) { |
| Thawedmind | 8:5373e5ceab2e | 82 | for (float i = 0.5f; i <= 1; i += 0.01f) //Accelerate from 50% to 100% |
| Thawedmind | 8:5373e5ceab2e | 83 | { |
| Thawedmind | 8:5373e5ceab2e | 84 | Wheel.Speed(i, i); |
| Thawedmind | 8:5373e5ceab2e | 85 | wait(0.1f); |
| Thawedmind | 8:5373e5ceab2e | 86 | if (microswitch1 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 87 | wait(0.2); |
| Thawedmind | 8:5373e5ceab2e | 88 | break; |
| Thawedmind | 8:5373e5ceab2e | 89 | } |
| Thawedmind | 8:5373e5ceab2e | 90 | if (microswitch1 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 91 | wait(0.2); |
| Thawedmind | 8:5373e5ceab2e | 92 | for (float i = 0.5f; i <= 1; i -= 0.01f) //Accelerate from 50% to 100% in reverse! |
| Thawedmind | 8:5373e5ceab2e | 93 | { |
| Thawedmind | 8:5373e5ceab2e | 94 | Wheel.Speed(i, i); |
| Thawedmind | 8:5373e5ceab2e | 95 | wait(0.1f); |
| Thawedmind | 8:5373e5ceab2e | 96 | if (microswitch2 == 1) { |
| Thawedmind | 8:5373e5ceab2e | 97 | wait(0.2); |
| Thawedmind | 8:5373e5ceab2e | 98 | break; |
| Thawedmind | 8:5373e5ceab2e | 99 | } |
| Thawedmind | 8:5373e5ceab2e | 100 | fBOn = 0; |
| Thawedmind | 8:5373e5ceab2e | 101 | } |
| Thawedmind | 8:5373e5ceab2e | 102 | } |
| Thawedmind | 8:5373e5ceab2e | 103 | } |
| Thawedmind | 8:5373e5ceab2e | 104 | |
| Thawedmind | 8:5373e5ceab2e | 105 | */ |
| james_plym | 7:a96163893291 | 106 | |
| james_plym | 9:681280a7a416 | 107 | |
| james_plym | 7:a96163893291 | 108 | |
| martinsimpson | 0:51c12cc34baf | 109 | int main () |
| martinsimpson | 0:51c12cc34baf | 110 | { |
| james_plym | 6:eac1e6a55274 | 111 | pc.baud(9600); //BAUD Rate to 9600 |
| james_plym | 6:eac1e6a55274 | 112 | pc.printf("ROCO104 Demonstration Robot Buggy Plymouth University 2018/19\n\r"); |
| martinsimpson | 0:51c12cc34baf | 113 | |
| james_plym | 6:eac1e6a55274 | 114 | Wheel.Period_in_ms(TIME_PERIOD);//Set frequency of the PWMs |
| martinsimpson | 1:3ca91ad8e927 | 115 | |
| james_plym | 6:eac1e6a55274 | 116 | // |
| james_plym | 6:eac1e6a55274 | 117 | //--------------------------- your strategy goes between the two dashed lines --------------------------------------------- |
| james_plym | 6:eac1e6a55274 | 118 | // |
| martinsimpson | 1:3ca91ad8e927 | 119 | Wheel.Stop(); |
| james_plym | 6:eac1e6a55274 | 120 | |
| james_plym | 6:eac1e6a55274 | 121 | megolavania(1); |
| james_plym | 9:681280a7a416 | 122 | |
| james_plym | 9:681280a7a416 | 123 | blueButton(); |
| james_plym | 9:681280a7a416 | 124 | |
| james_plym | 5:756dc04a891c | 125 | // YOUR LINES OF CODE for your stratagy go between HERE! +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| james_plym | 6:eac1e6a55274 | 126 | Wheel.Period_in_ms(2);//Set frequency of the PWMs 500Hz |
| james_plym | 6:eac1e6a55274 | 127 | |
| james_plym | 7:a96163893291 | 128 | int on = 1; |
| james_plym | 7:a96163893291 | 129 | |
| james_plym | 7:a96163893291 | 130 | while(on == 1){ |
| james_plym | 7:a96163893291 | 131 | fwdBack(); |
| james_plym | 7:a96163893291 | 132 | turn(); |
| james_plym | 9:681280a7a416 | 133 | } |
| james_plym | 10:c8b9362f90d5 | 134 | } |
| james_plym | 7:a96163893291 | 135 | |
| james_plym | 6:eac1e6a55274 | 136 | |
| james_plym | 6:eac1e6a55274 | 137 | |
| martinsimpson | 0:51c12cc34baf | 138 | |
| martinsimpson | 4:8249fab4d8d3 | 139 | // and HERE! +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| james_plym | 7:a96163893291 | 140 | // go back to start of while loop |
| james_plym | 6:eac1e6a55274 | 141 | //end of int main() |
| martinsimpson | 4:8249fab4d8d3 | 142 | |
| martinsimpson | 4:8249fab4d8d3 | 143 | |
| martinsimpson | 1:3ca91ad8e927 | 144 | |
| martinsimpson | 4:8249fab4d8d3 | 145 | /* |
| martinsimpson | 4:8249fab4d8d3 | 146 | //Consider these lines of code to Accelerate the motors |
| martinsimpson | 4:8249fab4d8d3 | 147 | for (float i=0.5f; i<=1.0f; i+=0.01f) //Accelerate from 50% to 100% |
| james_plym | 6:eac1e6a55274 | 148 | { |
| martinsimpson | 4:8249fab4d8d3 | 149 | Wheel.Speed(i,i); |
| martinsimpson | 4:8249fab4d8d3 | 150 | wait(0.1f); |
| martinsimpson | 4:8249fab4d8d3 | 151 | } |
| martinsimpson | 4:8249fab4d8d3 | 152 | */ |