Angus McDowall / Mbed 2 deprecated ROCO104_Ultrasound_SPC

Dependencies:   mbed motor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 Simple Routine for Nucleo Board for ROCO104 Buggy Motor Control and Microswitches
00003 Heavy edit from previous ROCO103PP code
00004 Motor Class can now be instansiated with all four pins needed to control the H Bridge
00005 with a member functions as follows
00006 
00007 Motor::Speed(float A, Float B) range -0.1 to +1.0 to give full reverse to full forward for A/B motors
00008 Motor::Stop()       STOP
00009 Motor::Fwd(float)   Forward but floating point number (range 0.0 to 1.0)
00010 Motor::Rev(float)   Reverse but floating point number (range 0.0 to 1.0)
00011 
00012 Plymouth University
00013 M.Simpson 31st October 2016
00014 Edited 03/02/2017
00015 Edited 06/12/2018
00016 */
00017 #include "mbed.h"
00018 #include "motor.h"
00019 #include "UltraSonic.h"
00020 #include "spc.h"
00021 
00022 #define TIME_PERIOD 2             //Constant compiler Values here 2 equates to 2ms or 500Hz base Frequency
00023 #define DUTY 0.9                  //DUTY of 1.0=100%, 0.4=40% etc.,
00024 
00025 DigitalIn microswitch1(D4);       //Instance of the DigitalIn class called 'microswitch1'
00026 DigitalIn microswitch2(D3);       //Instance of the DigitalIn class called 'microswitch2'
00027 
00028 Motor Wheel(D13,D11,D9,D10);      //Instance of the Motor Class called 'Wheel' see motor.h and motor.cpp
00029 
00030 
00031 DigitalIn myButton(USER_BUTTON);  //USER_BUTTON is the Blue Button on the NUCLEO Board
00032 
00033 DigitalOut led(LED3);             //LED1 is the Green LED on the NUCLEO board
00034                                   //N.B. The RED LED is the POWER Indicator
00035                                   //and the Multicoloured LED indicates status of the ST-LINK Programming cycle
00036 
00037 Serial terminal(USBTX,USBRX);           //Instance of the Serial class to enable much faster BAUD rates then standard 9600 i.e. 115200
00038                                   //This is Pseudo RS232 over USB the NUCLEO will appear as a COMx Port see device Manager on PC used
00039                                   //Use PuTTY to monitor check COMx and BAUD rate (115200)
00040                                   
00041 DigitalOut red(D6);               //Three coloured LEDs for the single Pixel Camera
00042 DigitalOut green(D12);
00043 DigitalOut blue(D8);
00044 
00045 AnalogIn ldr(A1);                 //Instance of the AnalogIn class called 'ldr'
00046 
00047 void SPCoff(void){red=0;green=0;blue=0;}
00048 
00049 void SPCtest(void)
00050 {
00051     float rlight,glight,blight;
00052         
00053     red=1;green=0;blue=0;   //ILLUMINATE WITH RED ONLY!
00054     wait_ms(50);            //Wait for LDR to respond leave this
00055     rlight=ldr/RCOR;        //Read the Analogue input MBED Normalised 0.0 to 1.0f Multiply by 3.3f to Calibrate to a Voltage read
00056     printf("RED %4.2fV ",rlight);  //Send LDR response in Volts to PC via PuTTY
00057     wait(0.1f);
00058     
00059     red=0;green=1;blue=0;   //ILLUMINATE WITH GREEN ONLY!
00060     wait_ms(50);            //Wait for LDR to respond leave this
00061     glight=ldr/GCOR;        //Read the Analogue input MBED Normalised 0.0 to 1.0f Multiply by 3.3f to Calibrate to a Voltage read
00062     printf("GRN %4.2fV ",glight);  //Send LDR response in Volts to PC via PuTTY
00063     wait(0.1f);
00064     
00065     red=0;green=0;blue=1;   //ILLUMINATE WITH BLUE ONLY!
00066     wait_ms(50);            //Wait for LDR to respond leave this
00067     blight=ldr/BCOR;        //Read the Analogue input MBED Normalised 0.0 to 1.0f Multiply by 3.3f to Calibrate to a Voltage read
00068     printf("BLU %4.2fV\n\r",blight);  //Send LDR response in Volts to PC via PuTTY
00069     wait(0.1f);
00070     
00071     red=0;green=0;blue=0;
00072     if(rlight<glight && rlight<blight){printf("\n\rRED!\n\n\r");  SPCflash(RED,5);}
00073     if(glight<rlight && glight<blight){printf("\n\rGREEN!\n\n\r");SPCflash(GRN,5);}
00074     if(blight<glight && blight<rlight){printf("\n\rBLUE!\n\n\r"); SPCflash(BLU,5);}
00075     wait(1.0f);
00076 }
00077 
00078 void SPCflash(int colour,int numberOfFlashes)
00079 {
00080     switch (colour)
00081     {
00082         case RED:for(int i=0;i<numberOfFlashes*2;i++){red=!red;wait(0.15f);}break;
00083         case GRN:for(int i=0;i<numberOfFlashes*2;i++){green=!green;wait(0.15f);}break;
00084         case BLU:for(int i=0;i<numberOfFlashes*2;i++){blue=!blue;wait(0.15f);}break;
00085         default:break;
00086     }
00087 }
00088                                   
00089 
00090 DigitalOut Trigger(D7);           //Instance of the DigitalInOut class called 'TriggerEcho' WAS D6
00091 DigitalIn  Echo(D2);              //Instance of the DigitalInOut class called 'TriggerEcho'
00092 Timer pulse;                      //Instance of the Timer class called 'pulse' so we can measure timed events
00093 
00094 
00095 // Function ultra_sonic_distance() will load the global variable distance with Ultra Sonic Sensor value in mm
00096 // and then send the value to the stdio ouput i.e serial over USB
00097 void ultra_sonic_distance(void)
00098 {
00099    printf("%dmm \n\r",(int)GetDistance());   
00100 }
00101   
00102 // Function GetDistance() will return a float value of the Distance from the Ultrasonic Sensor in millimetres typically use as: ‘(float)myDistance=GetDistance();’
00103 float GetDistance()
00104 {                                                       //Function Name to be called
00105     int EchoPulseWidth=0,EchoStart=0,EchoEnd=0;         //Assign and set to zero the local variables for this function
00106     Trigger = 1;                                        //Signal goes High i.e. 3V3
00107     wait_us(100);                                       //Wait 100us to give a pulse width Triggering the Ultrasonic Module
00108     Trigger = 0;                                        //Signal goes Low i.e. 0V
00109     pulse.start();                        //Start the instance of the class timer
00110     pulse.reset();                                      //Reset the instance of the Timer Class
00111     while(Echo == 0 && EchoStart < 25000){              //wait for Echo to go high
00112         EchoStart=pulse.read_us();                      //Conditional 'AND' with timeout to prevent blocking!      
00113     }
00114     while(Echo == 1 && ((EchoEnd - EchoStart) < 25000)){//wait for echo to return low
00115         EchoEnd=pulse.read_us();                        //Conditional 'OR' with timeout to prevent blocking!   
00116     }
00117     EchoPulseWidth = EchoEnd - EchoStart;               //time period in us
00118     pulse.stop();                                       //Stop the instance of the class timer
00119     return (float)EchoPulseWidth/5.8f;                  //calculate distance in mm and return the value as a float
00120 }
00121 
00122 
00123 //Variable 'duty' for programmer to use to vary speed as required set here to #define compiler constant see above
00124 float duty=DUTY;
00125 //
00126 int main(){
00127    terminal.baud(115200);
00128    while(1){
00129        SPCoff();
00130        SPCtest();
00131        wait(0.5);
00132 }
00133 } 
00134 
00135 
00136 
00137 /*
00138 //Consider these lines of code to Accelerate the motors
00139       for (float i=0.5f; i<=1.0f; i+=0.01f) //Accelerate  from 50% to 100%
00140       { 
00141         Wheel.Speed(i,i);
00142         wait(0.1f);
00143       }
00144 */