(DA) Internet of Things and Smart Electronics- ELE3006M2122 / Mbed 2 deprecated Project-Target-Localization

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**Ciaran Kane-18689005-- Target Localisation---- Awkknowledgements Dr Edmond Nurellari Joystick Library. Craig Evans N5110 Library. Mbed HCSR04 Liibrary .
00002 enji Arai Piezo library.
00003 
00004 /**----Libraries*/
00005 #include "mbed.h"
00006 #include "Joystick.h"
00007 #include "N5110.h"  
00008 #include "ultrasonic.h"  
00009 #include "piezo_bz.h" 
00010 
00011 /**Setting interrupts for buttons*/
00012 //There is only 4 butons due to my HCSR04 sensor being wired into two of the buttons channels
00013 volatile int g_bumperL_flag = 0;
00014 volatile int g_bumperR_flag = 0;
00015 volatile int g_BACK_flag = 0;
00016 volatile int g_START_flag = 0; 
00017 int Target;
00018 
00019 /**Voids for N5110 Menu Display*/
00020 void initialiseMbed();
00021 void menu();
00022 void targetLocate();
00023 void initUltrasonic();
00024 void dist(int distance);
00025 void techSupport();
00026 void TargetDistance();
00027 
00028 
00029 /**I/O definition*/
00030 InterruptIn bumperL_btn(PTB18);
00031 InterruptIn bumperR_btn(PTB3);
00032 InterruptIn BACK_btn(PTB19);
00033 InterruptIn START_btn(PTC5);
00034 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
00035 Serial pc(USBTX, USBRX);                                                              
00036 BusOut redleds(PTA1,PTA2,PTC2);
00037 BusOut greenleds(PTC3,PTC4,PTD3);
00038 PwmOut piezo(PTC10);                                                                                     
00039 DigitalOut trigPin(PTD0);
00040 DigitalIn echoPin(PTC12);
00041 Joystick joystick(PTB10,PTB11,PTC16);
00042 ultrasonic Sensor(PTD0, PTC12, .5, 1, &dist);
00043 
00044 
00045 /** Design and thought process goes into more detail in the associated report written for this project.
00046 But i will give a brief breakdown of what this simple code entails below:
00047 
00048 *Calling for initialisation of mbedded controller on startup whilst all LEDS's on
00049 when inialisation is complete turn off red LED's
00050 
00051 *all for menu where you have an option to initialise the sensor or 
00052 locate targets whilst red LED's stay on until one is selected using the left and right bumper
00053 then green LEDS on
00054 
00055 *If target locate is selected call for targetLocate void to show that a target is being located whilst green LEDS on
00056 
00057 *If sensor intialisation is selected then sound piezo buzzer for a period of 10 seconds until sensor is initialised
00058 
00059 *If target is found call for Located void with string text and turn on redLEDS and sound buzzer for 5 seconds to indicate target
00060 
00061 *After target is located call for results void to display distance from sensor 
00062 */
00063 //Main Program
00064 int main()
00065 {
00066     lcd.init();
00067     // printf ("LCD intialise");
00068     lcd.setContrast(0.4);
00069     // printf ("LCD Contrast set");
00070     // printf ("LCD Brightness Set");
00071     initialiseMbed();
00072     // printf ("MBED K64f Initialise");
00073     Sensor.startUpdates();
00074     // printf ("Ultrasonic Updates started");
00075     }
00076 
00077 void initialiseMbed() {
00078     //printf ( Initialise MBED)
00079     greenleds= 1;
00080     redleds=0;
00081     //printf(green LED on)
00082     //printf(red led off)
00083     lcd.clear();
00084     lcd.printString("MBED",0,0);
00085     lcd.printString("Controller",0,1);
00086     lcd.printString("Initialising",0,2);
00087     lcd.printString("Ciaran Kane",0,5);
00088     lcd.refresh();
00089     wait(1); 
00090     lcd.printString("Initialising.",0,2);
00091     lcd.refresh();
00092     wait(1);
00093     lcd.printString("Initialising..",0,2);
00094     lcd.refresh();
00095     wait(5.0);
00096     lcd.clear();
00097     //printf (Intialised. Call for menu void)
00098     lcd.printString("Initialised",0,0);
00099     lcd.printString("Press Start",0,1);
00100     lcd.printString("For",0,2);
00101     lcd.printString("Main Menu",0,3);
00102     lcd.printString("",0,4);
00103     lcd.printString("Ciaran Kane",0,5);
00104     lcd.refresh();
00105     greenleds=0;
00106     //printf (green led off)
00107     redleds=1;
00108     //printf (red led on)
00109     wait(5.0);
00110     
00111     while(1)
00112     {
00113         wait(0.5);
00114         if (g_START_flag==1) {
00115             //printf (start flag)
00116             menu();
00117             }
00118             }
00119             }
00120 
00121            
00122 void menu()
00123 {
00124     lcd.clear();
00125     lcd.printString("Main Menu",0,0);
00126     lcd.printString("Localise - Start",0,2);
00127     lcd.printString("Sensor Initialise - LB",0,4);
00128     lcd.printString("Technical Support - RB ",0,5);
00129         while(1)
00130     {
00131         wait(0.5);
00132         if (g_bumperL_flag==1) {
00133             //printf (Left bumper flag)
00134             initUltrasonic();
00135             }
00136         else if(g_START_flag==1) { 
00137         //printf (Start flag)
00138           targetLocate();
00139             }
00140             else if(g_bumperR_flag==1) { 
00141             //printf (right bumper flag)
00142           techSupport();
00143             }
00144             }
00145            }
00146         
00147            
00148 void initUltrasonic()
00149 {
00150       
00151     greenleds= 1;
00152     //printf ( green led on)
00153     redleds=0;
00154     //printf(red led off)
00155     lcd.clear();
00156     lcd.printString("Ultrasonic",0,0);
00157     lcd.printString("Sesnor",0,1);
00158     lcd.printString("Initialising",0,2);
00159     lcd.printString("Ciaran Kane",0,5);
00160     lcd.refresh();
00161     wait(1); 
00162     lcd.printString("Initialising.",0,2);
00163     lcd.refresh();
00164     wait(1);
00165     lcd.printString("Initialising..",0,2);
00166     lcd.refresh();
00167     wait(5.0);
00168     lcd.clear();
00169     //printf(Sensor initialised)
00170     lcd.printString("Initialised",0,0);
00171     lcd.printString("Press Start",0,1);
00172     lcd.printString("For",0,2);
00173     lcd.printString("Main Menu",0,3);
00174     lcd.printString("",0,4);
00175     lcd.printString("Ciaran Kane",0,5);
00176     lcd.refresh();
00177     greenleds=0;
00178     //printf( green led off)
00179     redleds=1;
00180     //printf( red led on)
00181     wait(5.0);
00182     
00183     while(1)
00184     {
00185         wait(0.5);
00186         if (g_START_flag==1) {
00187             //printf( start flag)
00188             menu();
00189             }
00190             }
00191             }
00192 void dist(int distance)
00193 {
00194     // printf("sense = %d\n", sense);
00195      //printf("Distance changed to %dmm\r\n", distance);
00196     
00197             lcd.clear();
00198      
00199             char targetD[14];
00200             int length = sprintf(targetD,"%dmm", distance);
00201             if (length <= 14)
00202     
00203             lcd.printString("Localising Target",0,0);
00204             lcd.printString(targetD,24,3);
00205             lcd.refresh();
00206      
00207     }
00208 
00209 void targetLocate()
00210 {  
00211 //printf( Target locating)
00212     lcd.printString("Searching",0,1);
00213     lcd.printString("For",0,2);
00214     lcd.printString("Target",0,3);
00215     TargetDistance();
00216     //printf( Target distance)
00217     lcd.clear();
00218     lcd.printString("Target",0,0); 
00219     lcd.printString("Found!",0,1);
00220     }
00221 
00222 void TargetDistance()
00223 {
00224         //sense;
00225      
00226         Target= Sensor.getCurrentDistance();
00227         
00228         //printf("distance at sense 1 = %d\r\n", Distance1);
00229 
00230         }
00231 void techSupport()
00232     {  
00233     //printf(Tech support screen)
00234     lcd.printString("Ciaran Kane",0,1);
00235     lcd.printString("Main engineer",0,2);
00236     lcd.printString("Ciaran.kane@se.com",0,3);
00237     lcd.clear();
00238     lcd.printString("Contact for queries",0,0); 
00239     lcd.printString("+44 758604400",0,1);
00240     }
00241