Angus McDowall / Mbed 2 deprecated ROCO104_Ultrasound_SPC

Dependencies:   mbed motor

Files at this revision

API Documentation at this revision

Comitter:
martinsimpson
Date:
Thu Feb 01 12:59:21 2018 +0000
Child:
1:3ca91ad8e927
Commit message:
First Commit

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
motor.cpp Show annotated file Show diff for this revision Revisions of this file
motor.h Show annotated file Show diff for this revision Revisions of this file
pwm_tone.cpp Show annotated file Show diff for this revision Revisions of this file
pwm_tone.h Show annotated file Show diff for this revision Revisions of this file
tunes.cpp Show annotated file Show diff for this revision Revisions of this file
tunes.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Feb 01 12:59:21 2018 +0000
@@ -0,0 +1,146 @@
+/*
+Simple Routine for Nucleo Board for ROCO103PP Buggy Motor COntrol and Microswitches
+Plymouth University
+M.Simpson 31st October 2016
+Editted 03/02/2017
+*/
+#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
+
+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
+                                  //N.B. The RED LED is the POWER Indicator
+                                  //and the Multicoloured LED indicates status of the ST-LINK Programming cycle
+
+Serial pc(USBTX,USBRX);           //Instance of the Serial class to enable much faster BAUD rates then standard 9600 i.e. 115200
+                                  //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");
+
+  motor_A.Period_in_ms(TIME_PERIOD);    //Set frequency of the PWMs
+  motor_B.Period_in_ms(TIME_PERIOD);
+  //
+  //--------------------------- 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.
+    
+  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
+        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(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
+    {
+      motor(duty,duty);     //Start Moving forward 
+    
+            while(microswitch1==0&&microswitch2==0){wait(0.05);} //short delay for debounce/noise
+
+      motor(0,0);         //STOP Motors
+      wait(0.1);          //Allow time for motors to stop
+      
+            if(microswitch1==1)   //Execute the following code if microswitch1 is activated
+            { 
+                motor(0.0f,0.0f);   //Stop the Motors
+                tone1();
+                motor(-duty,-duty);
+                wait(2.0f);
+                motor(0,0);
+                wait(1.0f);
+                motor(-duty,duty);
+                wait(2.0f);
+                motor(0,0);
+                wait(1.0f);
+            }
+
+            if(microswitch2==1)   //Execute the following code if microswitch2 is activated
+            {
+                motor(0.0f,0.0f);   //Stop the Motors
+                tone2();
+                motor(-duty,-duty);
+                wait(2.0f);
+                motor(0,0);
+                wait(1.0f);
+                motor(duty,-duty);
+                wait(2.0f);
+                motor(0,0);
+                wait(1.0f);
+            }
+  }
+}
+//
+//----------------------------------------------------------------------------------------------
+//
+
+//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
+//      for (float i=0.5f; i<=1.0f; i+=0.01f) //Accelerate  from 50% to 100%
+//      { 
+//        motor(i,i);
+//        wait(0.1f);
+//      }
+*/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Feb 01 12:59:21 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/7130f322cb7e
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/motor.cpp	Thu Feb 01 12:59:21 2018 +0000
@@ -0,0 +1,20 @@
+#include "motor.h"
+
+Motor::Motor(PinName pinName1, PinName pinName2) : pin1(pinName1), pin2(pinName2)
+{
+}
+void Motor::Fwd(float duty)
+{ 
+    this->pin1 = 0.0f;
+    this->pin2 = duty;
+}
+void Motor::Rev(float duty)
+{
+    this->pin1 = duty;
+    this->pin2 = 0.0f;
+}
+void Motor::Period_in_ms(int msPeriod)
+{
+    this->pin1.period_ms(msPeriod);
+    this->pin2.period_ms(msPeriod);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/motor.h	Thu Feb 01 12:59:21 2018 +0000
@@ -0,0 +1,15 @@
+#ifndef _MOTOR_H_
+#define _MOTOR_H_
+#include "mbed.h"
+class Motor
+{
+public:
+    Motor(PinName pinName1, PinName pinName2);
+    void Fwd(float time);
+    void Rev(float time);
+    void Period_in_ms(int msPeriod);
+private:
+    PwmOut pin1;
+    PwmOut pin2;
+};
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pwm_tone.cpp	Thu Feb 01 12:59:21 2018 +0000
@@ -0,0 +1,54 @@
+/* Includes ------------------------------------------------------------------*/
+#include "pwm_tone.h"
+
+/* Private typedef -----------------------------------------------------------*/
+/* Private define ------------------------------------------------------------*/
+/* Private variables ---------------------------------------------------------*/
+/* Private function prototypes -----------------------------------------------*/
+/* Private functions ---------------------------------------------------------*/
+
+/**
+   * @brief     Tune Function
+   * @param  name : Choose the PwmOut
+                    period : this param is tune value. (C_3...B_5)
+                    beat : this param is beat value. (1..16) 1 means 1/16 beat
+   * @retval    None
+   */
+void Tune(PwmOut name, int period, int beat)
+{  
+    int delay;
+    
+    delay = beat*63;
+    name.period_us(period);
+    name.write(0.50f); // 50% duty cycle
+    wait_ms(delay); // 1 beat
+    //name.period_us(0); // Sound off
+    name=0;
+}
+
+/**
+   * @brief     Auto tunes Function
+   * @param  name : Choose the PwmOut
+                    period : this param is tune value. (C_3...B_5)
+                    beat : this param is beat value. (1..16) 1 means 1/16 beat
+   * @retval    None
+   */
+void Auto_tunes(PwmOut name, int period, int beat)
+{    
+    int delay;
+    
+    delay = beat*63;
+    name.period_us(period);
+    name.write(0.50f); // 50% duty cycle
+    wait_ms(delay);
+}
+
+/**
+   * @brief     Stop tunes Function
+   * @param  name : Choose the PwmOut
+   * @retval    None
+   */
+void Stop_tunes(PwmOut name)
+{
+    name.period_us(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pwm_tone.h	Thu Feb 01 12:59:21 2018 +0000
@@ -0,0 +1,112 @@
+/* Includes ------------------------------------------------------------------*/
+#include "mbed.h"
+
+/* Private typedef -----------------------------------------------------------*/
+/* Private define ------------------------------------------------------------*/
+/*
+#define Do3  131 //C octave3
+#define Do3s 139 //C#
+#define Re3  147 //D
+#define Re3s 156//D#
+#define Mi3  165 //E
+#define Fa3  175 //F
+#define Fa3s 185 //F#
+#define So3  196 //G
+#define So3s 208 //G#
+#define La3  220 //A
+#define La3s 233 //A#
+#define Ti3  247 //B
+#define Do4  262 //C octave4
+#define Do4s 277 //C#
+#define Re4  294 //D
+#define Re4s 311//D#
+#define Mi4  330 //E
+#define Fa4  349 //F
+#define Fa4s 370 //F#
+#define So4  392 //G
+#define So4s 415 //G#
+#define La4  440 //A
+#define La4s 466 //A#
+#define Ti4  494 //B
+#define Do5  523 //C octave5
+#define Do5s 554 //C#
+#define Re5  587 //D
+#define Re5s 622//D#
+#define Mi5  659 //E
+#define Fa5  699 //F
+#define Fa5s 740 //F#
+#define So5  784 //G
+#define So5s 831 //G#
+#define La5  880 //A
+#define La5s 932 //A#
+#define Ti5  988 //B
+#define Do6  1047 //C octave6
+#define Do6s 1109 //C#
+#define Re6  1175 //D
+#define Re6s 1245 //D#
+#define Mi6  1319 //E
+#define Fa6  1397 //F
+#define Fa6s 1480 //F#
+#define So6  1568 //G
+#define So6s 1661 //G#
+#define La6  1760 //A
+#define La6s 1865 //A#
+#define Ti6  1976 //B
+*/
+#define C3   131 //C octave3
+#define C3s  139 //C#
+#define Dd3  147 //D
+#define D3s  156//D#
+#define E3   165 //E
+#define F3   175 //F
+#define F3s  185 //F#
+#define G3   196 //G
+#define G3s  208 //G#
+#define Aa3  220 //A
+#define A3s  233 //A#
+#define B3   247 //B
+#define C4   262 //C octave4
+#define C4s  277 //C#
+#define Dd4   294 //D
+#define D4s  311//D#
+#define E4   330 //E
+#define F4   349 //F
+#define F4s  370 //F#
+#define G4   392 //G
+#define G4s  415 //G#
+#define Aa4   440 //A
+#define A4s  466 //A#
+#define B4   494 //B
+#define C5   523 //C octave5
+#define C5s  554 //C#
+#define Dd5   587 //D
+#define D5s  622 //D#
+#define E5   659 //E
+#define F5   699 //F
+#define F5s  740 //F#
+#define G5   784 //G
+#define G5s  831 //G#
+#define Aa5   880 //A
+#define A5s  932 //A#
+#define B5   988 //B
+#define C6   1047 //C octave6
+#define C6s  1109 //C#
+#define Dd6   1175 //D
+#define D6s  1245 //D#
+#define E6   1319 //E
+#define F6   1397 //F
+#define F6s  1480 //F#
+#define G6   1568 //G
+#define G6s  1661 //G#
+#define Aa6   1760 //A
+#define A6s  1865 //A#
+#define B6   1976 //B
+
+
+/* Private variables ---------------------------------------------------------*/
+/* Private function prototypes -----------------------------------------------*/
+void Tune(PwmOut name, int period, int beat);
+void Auto_tunes(PwmOut name, int period, int beat);
+void Stop_tunes(PwmOut name);
+        
+/* Private functions ---------------------------------------------------------*/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tunes.cpp	Thu Feb 01 12:59:21 2018 +0000
@@ -0,0 +1,81 @@
+ #include "mbed.h"
+ #include "pwm_tone.h"
+ #include "motor.h"
+ #define TIME_PERIOD 2             //Constant compiler Values here 2 equates to 2ms or 500Hz base Frequency
+PwmOut buzzer(D5);
+  
+ void tone1(void){
+    Tune(buzzer,1e6/C6,4);
+    buzzer.period_ms(TIME_PERIOD);
+ }
+ 
+void tone2(void){
+    Tune(buzzer,1e6/E6,4);
+    buzzer.period_ms(TIME_PERIOD);
+ }
+
+void close_encounter(int s){
+    Tune(buzzer,1e6/Dd5,s*2); //It's a close encounter of the 3rd kind of!!!
+    Tune(buzzer,1e6/E5,s*2);
+    Tune(buzzer,1e6/C5,s*2);
+    Tune(buzzer,1e6/C4,s*2);
+    Tune(buzzer,1e6/G4,s*4);
+    buzzer.period_ms(TIME_PERIOD);
+ }
+ 
+ void jingle_bells(int s){
+    Tune(buzzer,1e6/E4,s*2); //Jingle Bells!!!
+    Tune(buzzer,1e6/E4,s*2);
+    Tune(buzzer,1e6/E4,s*4);
+  
+    Tune(buzzer,1e6/E4,s*2);
+    Tune(buzzer,1e6/E4,s*2);
+    Tune(buzzer,1e6/E4,s*4);
+    
+    Tune(buzzer,1e6/E4,s*2);
+    Tune(buzzer,1e6/G4,s*2);
+    Tune(buzzer,1e6/C4,s*3);
+    Tune(buzzer,1e6/E4,s*1);
+    
+    Tune(buzzer,1e6/E4,s*8);
+  
+    Tune(buzzer,1e6/F4,s*2);
+    Tune(buzzer,1e6/F4,s*2);
+    Tune(buzzer,1e6/F4,s*3);
+    Tune(buzzer,1e6/F4,s*1);
+    
+    Tune(buzzer,1e6/F4,s*2);
+    Tune(buzzer,1e6/E4,s*2);
+    Tune(buzzer,1e6/E4,s*2);
+    Tune(buzzer,1e6/E4,s*1);
+    Tune(buzzer,1e6/E4,s*1);
+    
+    Tune(buzzer,1e6/E4,s*2);
+    Tune(buzzer,1e6/Dd4,s*2);
+    Tune(buzzer,1e6/Dd4,s*2);
+    Tune(buzzer,1e6/E4,s*2);
+  
+    Tune(buzzer,1e6/Dd4,s*4);
+    Tune(buzzer,1e6/G4,s*4);
+    buzzer.period_ms(TIME_PERIOD);
+  }
+void twinkle(int s){
+    Tune(buzzer,1e6/C4,s*2); //Twinkle Little Star!!!!!
+    Tune(buzzer,1e6/C4,s*2);
+    Tune(buzzer,1e6/G4,s*2);
+    Tune(buzzer,1e6/G4,s*2);
+  
+    Tune(buzzer,1e6/Aa4,s*2);
+    Tune(buzzer,1e6/Aa4,s*2);
+    Tune(buzzer,1e6/G4,s*4);
+    
+    Tune(buzzer,1e6/F4,s*2);
+    Tune(buzzer,1e6/F4,s*2);
+    Tune(buzzer,1e6/E4,s*2);
+    Tune(buzzer,1e6/E4,s*2);
+
+    Tune(buzzer,1e6/Dd4,s*2);
+    Tune(buzzer,1e6/Dd4,s*2);
+    Tune(buzzer,1e6/C4,s*4);
+    buzzer.period_ms(TIME_PERIOD);
+  }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tunes.h	Thu Feb 01 12:59:21 2018 +0000
@@ -0,0 +1,5 @@
+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);