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.
Revision 1:3ca91ad8e927, committed 2018-12-14
- Comitter:
- martinsimpson
- Date:
- Fri Dec 14 14:24:52 2018 +0000
- Parent:
- 0:51c12cc34baf
- Child:
- 2:bc41daf2b0ce
- Commit message:
- Version Alpha 0.1a
Changed in this revision
--- a/main.cpp Thu Feb 01 12:59:21 2018 +0000
+++ b/main.cpp Fri Dec 14 14:24:52 2018 +0000
@@ -1,24 +1,34 @@
/*
-Simple Routine for Nucleo Board for ROCO103PP Buggy Motor COntrol and Microswitches
+Simple Routine for Nucleo Board for ROCO104 Buggy Motor Control and Microswitches
+Heavy edit from previous ROCO103PP code
+Motor Class can now be instansiated with all four pins needed to control the H Bridge
+with a member functions as follows
+
+Motor::Speed(float A, Float B) range -0.1 to +1.0 to give full reverse to full forward for A/B motors
+Motor::Stop() STOP
+Motor::Fwd(float) Forward but floating point number (range 0.0 to 1.0)
+Motor::Rev(float) Reverse but floating point number (range 0.0 to 1.0)
+
Plymouth University
M.Simpson 31st October 2016
-Editted 03/02/2017
+Edited 03/02/2017
+Edited 06/12/2018
*/
#include "mbed.h"
#include "motor.h"
#include "tunes.h"
+
#define TIME_PERIOD 2 //Constant compiler Values here 2 equates to 2ms or 500Hz base Frequency
#define DUTY 0.9 //DUTY of 1.0=100%, 0.4=40% etc.,
DigitalIn microswitch1(D4); //Instance of the DigitalIn class called 'microswitch1'
DigitalIn microswitch2(D3); //Instance of the DigitalIn class called 'microswitch2'
-Motor motor_A(D7,D8); //Instances of the Motor Class see motor.h anf motor.cpp
-Motor motor_B(D9,D10); //They must be connected to these ports D7,D8 & D9,D10
+Motor Wheel(D13,D11,D9,D10); //Instance of the Motor Class called 'Wheel' see motor.h and motor.cpp
DigitalIn myButton(USER_BUTTON); //USER_BUTTON is the Blue Button on the NUCLEO Board
-DigitalOut led(LED1); //LED1 is the Green LED on the NUCLEO board
+DigitalOut led(LED3); //LED1 is the Green LED on the NUCLEO board
//N.B. The RED LED is the POWER Indicator
//and the Multicoloured LED indicates status of the ST-LINK Programming cycle
@@ -26,121 +36,96 @@
//This is Pseudo RS232 over USB the NUCLEO will appear as a COMx Port see device Manager on PC used
//Use PuTTY to monitor check COMx and BAUD rate (115200)
-//The Following line is a Function Prototype
-int motor(float speedA, float speedB); //call as motor('Speed of MotorA Left','Speed of MotorB Right')
- //Where speed ranges from -1.0 to +1.0 inclusive rto give full reverse to full forward
- //And of course 0.0 will stop the Motor
//Variable 'duty' for programmer to use to vary speed as required set here to #define compiler constant see above
float duty=DUTY;
//
int main ()
{
- pc.baud(115200); //BAUD Rate to 115200
- pc.printf("ROCO103PP Demonstration Robot Buggy Plymouth University 2016/17\n\r");
+ pc.baud(115200); //BAUD Rate to 115200
+ pc.printf("ROCO104 Demonstration Robot Buggy Plymouth University 2018/19\n\r");
- motor_A.Period_in_ms(TIME_PERIOD); //Set frequency of the PWMs
- motor_B.Period_in_ms(TIME_PERIOD);
+ Wheel.Period_in_ms(TIME_PERIOD);//Set frequency of the PWMs
+
//
//--------------------------- your strategy goes between the two dashed lines ---------------------------------------------
//
- motor(0.0f,0.0f); // Ensure Motors are stopped - For the curious, this function is defined at the end of this file.
+ Wheel.Stop();
- close_encounter(1); //tune to play Announce start!
- //twinkle(1); //see tunes.h for alternatives or make your own!
- //jingle_bells(1);
- while(myButton==1)
- { //Wait here for USER Button (Blue) on Nucleo Board (goes to zero when pressed)
- led=0; //and flash green LED whilst waiting
+ close_encounter(1); //tune to play Announce start!
+ //twinkle(1); //see tunes.h for alternatives or make your own!
+ //jingle_bells(1);
+
+ while(myButton==0)
+ { //Wait here for USER Button (Blue) on Nucleo Board (goes to zero when pressed)
+ led=0; //and flash green LED whilst waiting
wait(0.1);
led=1;
wait(0.1);
- if(microswitch1==1)
- {
- pc.printf("Switch1 = %4.2fV\n\r",(float)microswitch1*3.3f);//printing value of microswitch1 in PuTTy window on PC
- //NB this is a Digital Input and so returns a Boolean 1 or 0
- //and so 'cast' the result into a 'float' type and multiply by 3.3!
- // see the instruction doc on how to install putty.
- tone1();
+ //Test Microswitches with two different tones see tunes.cpp tunes.h
+ if(microswitch1==1)
+ {
+ pc.printf("Switch1 = %4.2fV\n\r",(float)microswitch1*3.3f);
+ //printing value of microswitch1 in PuTTy window on PC
+ //NB this is a Digital Input and so returns a Boolean 1 or 0
+ //and so 'cast' the result into a 'float' type and multiply by 3.3!
+ //to indicate voltage applied to pin.
+ //see the instruction doc on how to install putty.
+ tone1();
+ }
+ if(microswitch2==1)
+ {
+ pc.printf("Switch 2 pressed\n\r"); //Another example of how to print a message telling about the program workings.
+ tone2();
+ }
}
- //Test Microswitches with two different tones see tunes.cpp tunes.h
- if(microswitch2==1)
- {
- pc.printf("Switch 2 pressed\n\r"); //Another example of how to print a message telling about the program workings.
- tone2();
- }
- }
- while(true) //Repeat the following forever
+ while(true) //Repeat the following forever NB always true!
{
- motor(duty,duty); //Start Moving forward
+ Wheel.Speed(duty,duty); //Move Foward
- while(microswitch1==0&µswitch2==0){wait(0.05);} //short delay for debounce/noise
+ while(microswitch1==0&µswitch2==0){wait(0.05);}// Loop here till a microswitch activates
+ // NB short delay for debounce/noise(Electrical)
- motor(0,0); //STOP Motors
- wait(0.1); //Allow time for motors to stop
+ Wheel.Stop(); //STOP
+ wait(0.1); //Allow time for motors to stop
- if(microswitch1==1) //Execute the following code if microswitch1 is activated
+ if(microswitch1==1) //Execute the following code if microswitch1 is activated
{
- motor(0.0f,0.0f); //Stop the Motors
+ Wheel.Stop(); //STOP
tone1();
- motor(-duty,-duty);
+ Wheel.Speed(-duty,-duty); //Move Back
wait(2.0f);
- motor(0,0);
- wait(1.0f);
- motor(-duty,duty);
- wait(2.0f);
- motor(0,0);
- wait(1.0f);
+ Wheel.Stop(); //STOP
+ wait(0.1f);
+ Wheel.Speed(-duty,duty); //SPIN anti-clockwise
+ wait(0.75f);
+ Wheel.Stop(); //STOP
+ wait(0.1f);
}
- if(microswitch2==1) //Execute the following code if microswitch2 is activated
+ if(microswitch2==1) //Execute the following code if microswitch2 is activated
{
- motor(0.0f,0.0f); //Stop the Motors
+ Wheel.Stop(); //STOP
tone2();
- motor(-duty,-duty);
+ Wheel.Speed(-duty,-duty); //Move Back
wait(2.0f);
- motor(0,0);
+ Wheel.Stop(); //STOP
wait(1.0f);
- motor(duty,-duty);
- wait(2.0f);
- motor(0,0);
- wait(1.0f);
+ Wheel.Speed(duty,-duty); //SPIN clockwise
+ wait(0.75f);
+ Wheel.Stop(); //STOP
+ wait(0.1f);
}
- }
+ }// go back to start of while loop
}
//
//----------------------------------------------------------------------------------------------
//
-
-//Small function to control motors use as motor(1.0,-0.5) Motor A full speed forward Motor B half speed reversed
-int motor(float speedA, float speedB){
- if(speedA>1.0f||speedA<-1.0f){ //CHECK speedA Value is in Range!
- return -1; //return ERROR code -1=speedA Value out of range! EXIT Function
- }
- if(speedB>1.0f||speedA<-1.0f){ //CHECK speedB Value is in Range!
- return -2; //return ERROR code -2=speedB Value out of range! EXIT Function
- }
- //If speed values have passed the checks above then the following code will be executed
- if(speedA<0.0f){
- motor_A.Rev(-speedA);
- }
- else{
- motor_A.Fwd(speedA);
- }
- if(speedB<0.0f){
- motor_B.Rev(-speedB);
- }
- else{
- motor_B.Fwd(speedB);
- }
- return 0; //Return ERROR code Zero i.e. NO ERROR success!
-}
-
-/* //Consider these lines of code to Accelerate the motors
+ //Consider these lines of code to Accelerate the motors
// for (float i=0.5f; i<=1.0f; i+=0.01f) //Accelerate from 50% to 100%
// {
-// motor(i,i);
+// Wheel.Speed(i,i);
// wait(0.1f);
// }
-*/
+
--- a/mbed.bld Thu Feb 01 12:59:21 2018 +0000 +++ b/mbed.bld Fri Dec 14 14:24:52 2018 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/mbed_official/code/mbed/builds/7130f322cb7e \ No newline at end of file +https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc \ No newline at end of file
--- a/motor.cpp Thu Feb 01 12:59:21 2018 +0000
+++ b/motor.cpp Fri Dec 14 14:24:52 2018 +0000
@@ -1,20 +1,69 @@
#include "motor.h"
-Motor::Motor(PinName pinName1, PinName pinName2) : pin1(pinName1), pin2(pinName2)
+Motor::Motor(PinName pinName1, PinName pinName2, PinName pinName3, PinName pinName4) : pin1(pinName1), pin2(pinName2), pin3(pinName3), pin4(pinName4)
{
}
void Motor::Fwd(float duty)
{
this->pin1 = 0.0f;
this->pin2 = duty;
+ this->pin3 = 0.0f;
+ this->pin4 = duty;
}
void Motor::Rev(float duty)
{
this->pin1 = duty;
this->pin2 = 0.0f;
+ this->pin3 = duty;
+ this->pin4 = 0.0f;
}
+void Motor::Stop(void)
+{
+ this->pin1 = 0.0f;
+ this->pin2 = 0.0f;
+ this->pin3 = 0.0f;
+ this->pin4 = 0.0f;
+}
+int Motor::Speed(float speedA, float speedB)
+{
+ if(speedA>1.0f||speedA<-1.0f){ //CHECK speedA Value is in Range!
+ return -1; //return ERROR code -1=speedA Value out of range! EXIT Function
+ }
+ if(speedB>1.0f||speedA<-1.0f){ //CHECK speedB Value is in Range!
+ return -2; //return ERROR code -2=speedB Value out of range! EXIT Function
+ }
+
+ //If speed values have passed the checks above then the following code will be executed
+
+ if(speedA<0.0f)
+ { //Reverse A motor
+ this->pin1 = -speedA;
+ this->pin2 = 0.0f;
+ }
+
+ else
+ { //Forward A motor
+ this->pin1 = 0.0f;
+ this->pin2 = speedA;
+ }
+
+ if(speedB<0.0f)
+ { //Reverse B motor
+ this->pin3 = -speedB;
+ this->pin4 = 0.0f;
+ }
+ else
+ { //Forward B motor
+ this->pin3 = 0.0f;
+ this->pin4 = speedB;
+ }
+ return 0; //Return ERROR code Zero i.e. NO ERROR success!
+}
+
void Motor::Period_in_ms(int msPeriod)
{
this->pin1.period_ms(msPeriod);
this->pin2.period_ms(msPeriod);
+ this->pin3.period_ms(msPeriod);
+ this->pin4.period_ms(msPeriod);
}
--- a/motor.h Thu Feb 01 12:59:21 2018 +0000
+++ b/motor.h Fri Dec 14 14:24:52 2018 +0000
@@ -4,12 +4,19 @@
class Motor
{
public:
- Motor(PinName pinName1, PinName pinName2);
+ Motor(PinName pinName1, PinName pinName2, PinName pinName3, PinName pinName4);
void Fwd(float time);
void Rev(float time);
+ void Stop(void);
+ int Speed(float speedA, float speedB);
void Period_in_ms(int msPeriod);
private:
PwmOut pin1;
PwmOut pin2;
+ PwmOut pin3;
+ PwmOut pin4;
};
+
+//int motor(float speedA, float speedB);
+
#endif
--- a/pwm_tone.h Thu Feb 01 12:59:21 2018 +0000
+++ b/pwm_tone.h Fri Dec 14 14:24:52 2018 +0000
@@ -1,6 +1,8 @@
+#ifndef _PWM_TONE_H_
+#define _PWM_TONE_H_
+
/* Includes ------------------------------------------------------------------*/
#include "mbed.h"
-
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/*
@@ -110,3 +112,5 @@
void Stop_tunes(PwmOut name);
/* Private functions ---------------------------------------------------------*/
+
+#endif
--- a/tunes.h Thu Feb 01 12:59:21 2018 +0000 +++ b/tunes.h Fri Dec 14 14:24:52 2018 +0000 @@ -1,5 +1,10 @@ +#ifndef _TUNES_H_ +#define _TUNES_H_ + extern void close_encounter(int s); extern void jingle_bells(int s); extern void twinkle(int s); extern void tone1(void); extern void tone2(void); + +#endif