(DA) Internet of Things and Smart Electronics- ELE3006M2122
/
ObjectDetection_LiamCox
First Draft, serial print change based on distance
main.cpp
- Committer:
- liam94
- Date:
- 2022-01-28
- Revision:
- 9:ada61082bbaa
- Parent:
- 8:7e48229d678c
- Child:
- 10:be53044119d1
File content as of revision 9:ada61082bbaa:
#include "mbed.h" #include "ultrasonic.h" #include "N5110.h" #include "Joystick.h" #include "main.h" /***************************************************************************//** * * the main code below sets all the interrupts to pull down and also for the start * and back buttons it is sent to the specific start and back functions which sets * the flags which are then used in other functions. * * the mu.startUpdates() is where the ultrasonic sensor is told to start measuring * the distances. * the joystick and lcd are then initialised and the man menu function is then * called. * *******************************************************************************/ int main() { R.mode(PullDown); L.mode(PullDown); Start.mode(PullDown); Start.rise(Start_isr); Back.mode(PullDown); Back.rise(Back_isr); // printf("set buttons"); mu.startUpdates(); // printf ("Ultrasonic Updates started"); joystick.init(); // printf("initialise joystick") init_display(); main_menu(); } /***************************************************************************//** * * added function which initalise the display and sets the contrast, this is * called from the int main and so the LCD boots up on startup. * *******************************************************************************/ void init_display(){ // printf("initialise display"); lcd.init(); lcd.setContrast(0.4); lcd.clear(); } /***************************************************************************//** * * this function is the main menu for the program, it is called at the start and * depending on what string is selected, clicking the start uton will send the * user to the specified function. * *******************************************************************************/ void main_menu(){ // printf("main menu"); lcd.clear(); Start_flag = 0; int select = 0; output = 63; // 63 on the bus output sets all LEDs to off while (1) { Direction d = joystick.get_direction(); // printf("Direction = %i\n",d); switch(select) { case 0: switch(d) { case N: select = 1; // printf("UP"); break; case S: select = 1; // printf("Down"); break; } break; case 1: switch(d) { case N: select = 0; // printf("UP"); break; case S: select = 0; // printf("Down"); break; } break; } wait(0.1); if (select == 0) { // printf("main menu - sense object"); lcd.clear(); lcd.printString("Detect",27,0); lcd.printString("Sense Object",7,2); lcd.printString("Calibration",7,3); lcd.drawCircle(3,19,2,FILL_TRANSPARENT); lcd.refresh(); wait(0.15); if (Start_flag == 1) { Start_flag = 0; sense_object(); } } else if (select == 1) { // printf("main menu - calibration"); lcd.clear(); lcd.printString("Detect",27,0); lcd.printString("Sense Object", 7,2); lcd.printString("Calibration",7,3); lcd.drawCircle(3,27,2,FILL_TRANSPARENT); lcd.refresh(); wait(0.15); if (Start_flag == 1) { Start_flag = 0; calibrate_object(); } } } } /***************************************************************************//** * * this function is for sensing the object * *******************************************************************************/ void sense_object(){ // printf("sense object"); // set inital state int state = 0; while(1) { output = fsm[state]; // output current state // printf("state = %d\r\n",state); // check which state we are in and see which the next state should be switch(state) { case 0:{ int sense = 1; lcd.clear(); lcd.printString(" object at 0'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString(" L - 60'",0,4); lcd.refresh(); if (R == 1){ state = 1;} else state = 0; if (Start_flag == 1) { Start_flag = 0; object_sense1(); } break;} case 1:{ lcd.clear(); lcd.printString(" object at 60'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString(" L - 60'",0,4); lcd.refresh(); if (R == 1){ state = 2;} else state = 1; break;} if (Start_flag == 1) { Start_flag = 0; object_sense2(); } case 2:{ lcd.clear(); lcd.printString(" object at 120'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString(" L - 60'",0,4); lcd.refresh(); if (R == 1){ state = 3;} else state = 2; break;} if (Start_flag == 1) { Start_flag = 0; object_sense3(); } case 3:{ lcd.clear(); lcd.printString(" object at 180'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString(" L - 60'",0,4); lcd.refresh(); if (R == 1){ state = 4;} else state = 3; break;} if (Start_flag == 1) { Start_flag = 0; object_sense4(); } case 4:{ lcd.clear(); lcd.printString(" object at 240'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString(" L - 60'",0,4); lcd.refresh(); if (R == 1){ state = 5;} else state = 4; break;} if (Start_flag == 1) { Start_flag = 0; object_sense5(); } case 5:{ lcd.clear(); lcd.printString(" object at 300'",0,0); lcd.printString(" R + 60'",0,2); lcd.printString(" L - 60'",0,4); lcd.refresh(); if (R == 1){ state = 6;} else state = 5; break;} if (Start_flag == 1) { Start_flag = 0; object_sense6(); } case 6:{ if (buffer1 - 10 <= glass[1] >= buffer1 + 10 and buffer2 - 10 <= glass[2] >= buffer2 + 10 and buffer3 - 10 <= glass[3] >= buffer3 + 10 and buffer4 - 10 <= glass[4] >= buffer4 + 10 and buffer5 - 10 <= glass[5] >= buffer5 + 10 and buffer6 - 10 <= glass[6] >= buffer6 + 10){ printf("glass");} else{ printf("nothing");} } default:{ error("Invalid state"); //invalid state - call error routine // or could jump to starting state i.e. state = 0 break;} } if (Back_flag == 1) { Back_flag = 0; main_menu();} ThisThread::sleep_for(200); } } /***************************************************************************//** * * this function is for calibrating the distance in which the object centre point * should be away from the sensor, it checks the current distance and then though * the function "void dist" displays the current distance on the screen. * this function also tells the user the distance away from the target centre * point this should be, this is so that the board is the right distance away and * will be able to detect the object better. * * in order to print the distance onto the LCD the current distance is printed to * a buffer which is a max length of 14 (the amount of characters that can fit * width ways on the screen) this can then be printed on the screen using print * string. * *******************************************************************************/ void calibrate_object(){ sense = 0; while(1) { mu.checkDistance(); if (Back_flag == 1) { Back_flag = 0; main_menu();} wait (1); } } void object_sense1(){ sense = 1; mu.checkDistance(); } void object_sense2(){ sense = 2; mu.checkDistance(); } void object_sense3(){ sense = 3; mu.checkDistance(); } void object_sense4(){ sense = 4; mu.checkDistance(); } void object_sense5(){ sense = 5; mu.checkDistance(); } void object_sense6(){ sense = 6; mu.checkDistance(); } void dist(int distance) { printf("sense = %d", sense); // printf("Distance changed to %dmm\r\n", distance); switch(sense){ case 0:{ lcd.clear(); char buffer[14]; int length = sprintf(buffer,"%dmm", distance); if (length <= 14) lcd.printString("set object to",0,0); lcd.printString(" 200mm",0,1); lcd.printString(buffer,24,3); lcd.refresh(); break;} case 1:{ printf("sens1"); char buffer1[14]; int length1 = sprintf(buffer1,"%dmm", distance); if (length1 <= 14) //printf("distance = %d", distance); break;} case 2:{ printf("sens2"); char buffer2[14]; int length2 = sprintf(buffer2,"%dmm", distance); if (length2 <= 14) //printf("distance = %d", distance); break;} case 3:{ printf("sens3"); char buffer3[14]; int length3 = sprintf(buffer3,"%dmm", distance); if (length3 <= 14) //printf("distance = %d", distance); break;} case 4:{ printf("sens4"); char buffer4[14]; int length4 = sprintf(buffer4,"%dmm", distance); if (length4 <= 14) //printf("distance = %d", distance); break;} case 5:{ printf("sens5"); char buffer5[14]; int length5 = sprintf(buffer5,"%dmm", distance); if (length5 <= 14) //printf("distance = %d", distance); break;} case 6:{ printf("sens6"); char buffer6[14]; int length6 = sprintf(buffer6,"%dmm", distance); if (length6 <= 14) //printf("distance = %d", distance); break;} } } /***************************************************************************//** * * these functions are for setting the flags for the start and back butons which * are used in the other functions. * *******************************************************************************/ void Start_isr() { // printf("start button pressed"); Start_flag = 1; } void Back_isr() { // printf("back button pressed"); Back_flag = 1; }