pabs

Dependencies:   mbed motor

Committer:
martinsimpson
Date:
Fri Dec 14 14:24:52 2018 +0000
Revision:
1:3ca91ad8e927
Parent:
0:51c12cc34baf
Child:
4:8249fab4d8d3
Version Alpha 0.1a

Who changed what in which revision?

UserRevisionLine numberNew 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
martinsimpson 0:51c12cc34baf 32 //N.B. The RED LED is the POWER Indicator
martinsimpson 0:51c12cc34baf 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
martinsimpson 0:51c12cc34baf 36 //This is Pseudo RS232 over USB the NUCLEO will appear as a COMx Port see device Manager on PC used
martinsimpson 0:51c12cc34baf 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 //
martinsimpson 0:51c12cc34baf 43 int main ()
martinsimpson 0:51c12cc34baf 44 {
martinsimpson 1:3ca91ad8e927 45 pc.baud(115200); //BAUD Rate to 115200
martinsimpson 1:3ca91ad8e927 46 pc.printf("ROCO104 Demonstration Robot Buggy Plymouth University 2018/19\n\r");
martinsimpson 0:51c12cc34baf 47
martinsimpson 1:3ca91ad8e927 48 Wheel.Period_in_ms(TIME_PERIOD);//Set frequency of the PWMs
martinsimpson 1:3ca91ad8e927 49
martinsimpson 0:51c12cc34baf 50 //
martinsimpson 0:51c12cc34baf 51 //--------------------------- your strategy goes between the two dashed lines ---------------------------------------------
martinsimpson 0:51c12cc34baf 52 //
martinsimpson 1:3ca91ad8e927 53 Wheel.Stop();
martinsimpson 0:51c12cc34baf 54
martinsimpson 1:3ca91ad8e927 55 close_encounter(1); //tune to play Announce start!
martinsimpson 1:3ca91ad8e927 56 //twinkle(1); //see tunes.h for alternatives or make your own!
martinsimpson 1:3ca91ad8e927 57 //jingle_bells(1);
martinsimpson 1:3ca91ad8e927 58
martinsimpson 1:3ca91ad8e927 59 while(myButton==0)
martinsimpson 1:3ca91ad8e927 60 { //Wait here for USER Button (Blue) on Nucleo Board (goes to zero when pressed)
martinsimpson 1:3ca91ad8e927 61 led=0; //and flash green LED whilst waiting
martinsimpson 0:51c12cc34baf 62 wait(0.1);
martinsimpson 0:51c12cc34baf 63 led=1;
martinsimpson 0:51c12cc34baf 64 wait(0.1);
martinsimpson 1:3ca91ad8e927 65 //Test Microswitches with two different tones see tunes.cpp tunes.h
martinsimpson 1:3ca91ad8e927 66 if(microswitch1==1)
martinsimpson 1:3ca91ad8e927 67 {
martinsimpson 1:3ca91ad8e927 68 pc.printf("Switch1 = %4.2fV\n\r",(float)microswitch1*3.3f);
martinsimpson 1:3ca91ad8e927 69 //printing value of microswitch1 in PuTTy window on PC
martinsimpson 1:3ca91ad8e927 70 //NB this is a Digital Input and so returns a Boolean 1 or 0
martinsimpson 1:3ca91ad8e927 71 //and so 'cast' the result into a 'float' type and multiply by 3.3!
martinsimpson 1:3ca91ad8e927 72 //to indicate voltage applied to pin.
martinsimpson 1:3ca91ad8e927 73 //see the instruction doc on how to install putty.
martinsimpson 1:3ca91ad8e927 74 tone1();
martinsimpson 1:3ca91ad8e927 75 }
martinsimpson 1:3ca91ad8e927 76 if(microswitch2==1)
martinsimpson 1:3ca91ad8e927 77 {
martinsimpson 1:3ca91ad8e927 78 pc.printf("Switch 2 pressed\n\r"); //Another example of how to print a message telling about the program workings.
martinsimpson 1:3ca91ad8e927 79 tone2();
martinsimpson 1:3ca91ad8e927 80 }
martinsimpson 0:51c12cc34baf 81 }
martinsimpson 0:51c12cc34baf 82
martinsimpson 1:3ca91ad8e927 83 while(true) //Repeat the following forever NB always true!
martinsimpson 0:51c12cc34baf 84 {
martinsimpson 1:3ca91ad8e927 85 Wheel.Speed(duty,duty); //Move Foward
martinsimpson 0:51c12cc34baf 86
martinsimpson 1:3ca91ad8e927 87 while(microswitch1==0&&microswitch2==0){wait(0.05);}// Loop here till a microswitch activates
martinsimpson 1:3ca91ad8e927 88 // NB short delay for debounce/noise(Electrical)
martinsimpson 0:51c12cc34baf 89
martinsimpson 1:3ca91ad8e927 90 Wheel.Stop(); //STOP
martinsimpson 1:3ca91ad8e927 91 wait(0.1); //Allow time for motors to stop
martinsimpson 0:51c12cc34baf 92
martinsimpson 1:3ca91ad8e927 93 if(microswitch1==1) //Execute the following code if microswitch1 is activated
martinsimpson 0:51c12cc34baf 94 {
martinsimpson 1:3ca91ad8e927 95 Wheel.Stop(); //STOP
martinsimpson 0:51c12cc34baf 96 tone1();
martinsimpson 1:3ca91ad8e927 97 Wheel.Speed(-duty,-duty); //Move Back
martinsimpson 0:51c12cc34baf 98 wait(2.0f);
martinsimpson 1:3ca91ad8e927 99 Wheel.Stop(); //STOP
martinsimpson 1:3ca91ad8e927 100 wait(0.1f);
martinsimpson 1:3ca91ad8e927 101 Wheel.Speed(-duty,duty); //SPIN anti-clockwise
martinsimpson 1:3ca91ad8e927 102 wait(0.75f);
martinsimpson 1:3ca91ad8e927 103 Wheel.Stop(); //STOP
martinsimpson 1:3ca91ad8e927 104 wait(0.1f);
martinsimpson 0:51c12cc34baf 105 }
martinsimpson 0:51c12cc34baf 106
martinsimpson 1:3ca91ad8e927 107 if(microswitch2==1) //Execute the following code if microswitch2 is activated
martinsimpson 0:51c12cc34baf 108 {
martinsimpson 1:3ca91ad8e927 109 Wheel.Stop(); //STOP
martinsimpson 0:51c12cc34baf 110 tone2();
martinsimpson 1:3ca91ad8e927 111 Wheel.Speed(-duty,-duty); //Move Back
martinsimpson 0:51c12cc34baf 112 wait(2.0f);
martinsimpson 1:3ca91ad8e927 113 Wheel.Stop(); //STOP
martinsimpson 0:51c12cc34baf 114 wait(1.0f);
martinsimpson 1:3ca91ad8e927 115 Wheel.Speed(duty,-duty); //SPIN clockwise
martinsimpson 1:3ca91ad8e927 116 wait(0.75f);
martinsimpson 1:3ca91ad8e927 117 Wheel.Stop(); //STOP
martinsimpson 1:3ca91ad8e927 118 wait(0.1f);
martinsimpson 0:51c12cc34baf 119 }
martinsimpson 1:3ca91ad8e927 120 }// go back to start of while loop
martinsimpson 0:51c12cc34baf 121 }
martinsimpson 0:51c12cc34baf 122 //
martinsimpson 0:51c12cc34baf 123 //----------------------------------------------------------------------------------------------
martinsimpson 0:51c12cc34baf 124 //
martinsimpson 1:3ca91ad8e927 125 //Consider these lines of code to Accelerate the motors
martinsimpson 0:51c12cc34baf 126 // for (float i=0.5f; i<=1.0f; i+=0.01f) //Accelerate from 50% to 100%
martinsimpson 0:51c12cc34baf 127 // {
martinsimpson 1:3ca91ad8e927 128 // Wheel.Speed(i,i);
martinsimpson 0:51c12cc34baf 129 // wait(0.1f);
martinsimpson 0:51c12cc34baf 130 // }
martinsimpson 1:3ca91ad8e927 131