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: Movement mbed tunes motor pwm_tone clickers Ultra
main.cpp@9:1094da23254d, 2020-03-06 (annotated)
- Committer:
- jakesmart
- Date:
- Fri Mar 06 16:56:00 2020 +0000
- Revision:
- 9:1094da23254d
- Parent:
- 8:f78d756832ad
- Child:
- 12:15272c2ae2ed
Ultrasonics working, movement functions created (started).
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" |
| jakesmart | 9:1094da23254d | 19 | #include "UltraSonic.h" |
| martinsimpson | 0:51c12cc34baf | 20 | #include "tunes.h" |
| Mikebob | 5:a0090b8b327b | 21 | #include "clickers.h" |
| martinsimpson | 0:51c12cc34baf | 22 | #define TIME_PERIOD 2 //Constant compiler Values here 2 equates to 2ms or 500Hz base Frequency |
| martinsimpson | 0:51c12cc34baf | 23 | #define DUTY 0.9 //DUTY of 1.0=100%, 0.4=40% etc., |
| martinsimpson | 0:51c12cc34baf | 24 | |
| martinsimpson | 0:51c12cc34baf | 25 | DigitalIn microswitch1(D4); //Instance of the DigitalIn class called 'microswitch1' |
| martinsimpson | 0:51c12cc34baf | 26 | DigitalIn microswitch2(D3); //Instance of the DigitalIn class called 'microswitch2' |
| Mikebob | 7:48bf77d076fc | 27 | Motor Wheel(D13,D11,D9,D10); //Instance of the Motor Class called 'Wheel' see motor.h and motor.cpp |
| martinsimpson | 0:51c12cc34baf | 28 | DigitalIn myButton(USER_BUTTON); //USER_BUTTON is the Blue Button on the NUCLEO Board |
| martinsimpson | 1:3ca91ad8e927 | 29 | DigitalOut led(LED3); //LED1 is the Green LED on the NUCLEO board |
| martinsimpson | 0:51c12cc34baf | 30 | //N.B. The RED LED is the POWER Indicator |
| martinsimpson | 0:51c12cc34baf | 31 | //and the Multicoloured LED indicates status of the ST-LINK Programming cycle |
| martinsimpson | 0:51c12cc34baf | 32 | |
| martinsimpson | 0:51c12cc34baf | 33 | Serial pc(USBTX,USBRX); //Instance of the Serial class to enable much faster BAUD rates then standard 9600 i.e. 115200 |
| martinsimpson | 0:51c12cc34baf | 34 | //This is Pseudo RS232 over USB the NUCLEO will appear as a COMx Port see device Manager on PC used |
| martinsimpson | 0:51c12cc34baf | 35 | //Use PuTTY to monitor check COMx and BAUD rate (115200) |
| Mikebob | 7:48bf77d076fc | 36 | int mm; |
| jakesmart | 9:1094da23254d | 37 | bool fStopped, rStopped = false; |
| jakesmart | 9:1094da23254d | 38 | void dist(){ |
| jakesmart | 9:1094da23254d | 39 | mm = (int)GetDistance(); |
| Mikebob | 7:48bf77d076fc | 40 | } |
| martinsimpson | 0:51c12cc34baf | 41 | int main () |
| martinsimpson | 0:51c12cc34baf | 42 | { |
| Mikebob | 7:48bf77d076fc | 43 | pc.baud(9600); //BAUD Rate to 9600 |
| Mikebob | 7:48bf77d076fc | 44 | pc.printf("ROCO104 Demonstration Robot Buggy Plymouth University 2018/19\n\r"); |
| Mikebob | 7:48bf77d076fc | 45 | Wheel.Period_in_ms(TIME_PERIOD);//Set frequency of the PWMs |
| martinsimpson | 1:3ca91ad8e927 | 46 | Wheel.Stop(); |
| martinsimpson | 0:51c12cc34baf | 47 | |
| martinsimpson | 1:3ca91ad8e927 | 48 | close_encounter(1); //tune to play Announce start! |
| martinsimpson | 1:3ca91ad8e927 | 49 | //twinkle(1); //see tunes.h for alternatives or make your own! |
| martinsimpson | 1:3ca91ad8e927 | 50 | //jingle_bells(1); |
| Mikebob | 7:48bf77d076fc | 51 | |
| martinsimpson | 1:3ca91ad8e927 | 52 | while(myButton==0) |
| martinsimpson | 1:3ca91ad8e927 | 53 | { //Wait here for USER Button (Blue) on Nucleo Board (goes to zero when pressed) |
| martinsimpson | 1:3ca91ad8e927 | 54 | led=0; //and flash green LED whilst waiting |
| martinsimpson | 0:51c12cc34baf | 55 | wait(0.1); |
| martinsimpson | 0:51c12cc34baf | 56 | led=1; |
| martinsimpson | 0:51c12cc34baf | 57 | wait(0.1); |
| martinsimpson | 4:8249fab4d8d3 | 58 | //Test Microswitches with two different tones see tunes.cpp tunes.h or flash (led1 and led2) onboard LEDs |
| martinsimpson | 0:51c12cc34baf | 59 | } |
| Mikebob | 5:a0090b8b327b | 60 | Wheel.Period_in_ms(2);//Set frequency of the PWMs 500Hz |
| Mikebob | 7:48bf77d076fc | 61 | |
| martinsimpson | 1:3ca91ad8e927 | 62 | while(true) //Repeat the following forever NB always true! |
| martinsimpson | 0:51c12cc34baf | 63 | { |
| jakesmart | 9:1094da23254d | 64 | dist(); |
| jakesmart | 9:1094da23254d | 65 | |
| jakesmart | 9:1094da23254d | 66 | ultra_sonic_distance(); |
| Mikebob | 5:a0090b8b327b | 67 | RevStop(); |
| jakesmart | 9:1094da23254d | 68 | FwdStop(); |
| martinsimpson | 4:8249fab4d8d3 | 69 | } // go back to start of while loop |
| martinsimpson | 4:8249fab4d8d3 | 70 | } //end of int main() |