GT ECE 4180 Lab Team - Raj Madisetti and Arjun Sonti

Dependencies:   mbed Servo Motordriver X_NUCLEO_53L0A1 HC_SR04_Ultrasonic_Library

Remote Control Car

Georgia Tech ECE 4180 Embedded Systems Design Final Project

Team Members

Raj Madisetti Arjun Sonti

Committer:
rmadisetti3
Date:
Thu Nov 19 21:19:12 2020 +0000
Revision:
4:29839de66eae
Parent:
3:4956cc0efdf3
Child:
5:b1b250500440
Final Code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rmadisetti3 0:20a8dfc396cb 1 #include "mbed.h"
rmadisetti3 0:20a8dfc396cb 2 #include "Servo.h"
rmadisetti3 0:20a8dfc396cb 3 #include "motordriver.h"
rmadisetti3 4:29839de66eae 4 #include "ultrasonic.h"
rmadisetti3 0:20a8dfc396cb 5 #include <stdio.h>
rmadisetti3 1:1c1ad0c1c260 6 Serial pc(USBTX,USBRX);
rmadisetti3 4:29839de66eae 7 Serial blue(p13,p14);
rmadisetti3 0:20a8dfc396cb 8 DigitalOut myled(LED1);
rmadisetti3 0:20a8dfc396cb 9
rmadisetti3 0:20a8dfc396cb 10
rmadisetti3 3:4956cc0efdf3 11 #define AUTOPILOT 10
rmadisetti3 3:4956cc0efdf3 12 #define FORWARD 1
rmadisetti3 3:4956cc0efdf3 13 #define REVERSE -1
rmadisetti3 3:4956cc0efdf3 14 #define STOP 0
rmadisetti3 3:4956cc0efdf3 15
rmadisetti3 4:29839de66eae 16 int state = 0; //global variable stop state
rmadisetti3 4:29839de66eae 17 int status1;
rmadisetti3 4:29839de66eae 18 int status2;
rmadisetti3 4:29839de66eae 19 int distanceCenter;
rmadisetti3 4:29839de66eae 20 int distanceLeft;
rmadisetti3 4:29839de66eae 21 int distanceRight;
rmadisetti3 0:20a8dfc396cb 22 Motor M(p21, p22, p23, 1); // pwm, fwd, rev, can brake
rmadisetti3 1:1c1ad0c1c260 23 Servo S(p24);
rmadisetti3 4:29839de66eae 24
rmadisetti3 3:4956cc0efdf3 25
rmadisetti3 3:4956cc0efdf3 26 void getNewState() {
rmadisetti3 3:4956cc0efdf3 27 //Logic for AdaFruit App
rmadisetti3 3:4956cc0efdf3 28 char bnum=0;
rmadisetti3 3:4956cc0efdf3 29 char bhit=0;
rmadisetti3 3:4956cc0efdf3 30 if (blue.readable()) {
rmadisetti3 3:4956cc0efdf3 31 if (blue.getc()=='!') {
rmadisetti3 3:4956cc0efdf3 32 if (blue.getc()=='B') { //button data packet
rmadisetti3 3:4956cc0efdf3 33 bnum = blue.getc(); //button number
rmadisetti3 3:4956cc0efdf3 34 bhit = blue.getc(); //1=hit, 0=release
rmadisetti3 3:4956cc0efdf3 35 if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
rmadisetti3 3:4956cc0efdf3 36 myled = bnum - '0'; //current button number will appear on LEDs
rmadisetti3 3:4956cc0efdf3 37 switch (bnum) {
rmadisetti3 3:4956cc0efdf3 38 case '1': //AutoPilot Mode
rmadisetti3 3:4956cc0efdf3 39 if (bhit=='1') {
rmadisetti3 3:4956cc0efdf3 40 state = 10; //autopilot state
rmadisetti3 3:4956cc0efdf3 41 }
rmadisetti3 3:4956cc0efdf3 42 break;
rmadisetti3 3:4956cc0efdf3 43 case '5': //forward
rmadisetti3 3:4956cc0efdf3 44 if (bhit=='1') {
rmadisetti3 3:4956cc0efdf3 45 state = 1; //forward state
rmadisetti3 3:4956cc0efdf3 46 } else {
rmadisetti3 3:4956cc0efdf3 47 state = 0; //stop state
rmadisetti3 3:4956cc0efdf3 48 }
rmadisetti3 3:4956cc0efdf3 49 break;
rmadisetti3 3:4956cc0efdf3 50 case '6': //reverse
rmadisetti3 3:4956cc0efdf3 51 if (bhit=='1') {
rmadisetti3 3:4956cc0efdf3 52 state = -1; //reverse state
rmadisetti3 3:4956cc0efdf3 53 } else {
rmadisetti3 3:4956cc0efdf3 54 state = 0; //stop state
rmadisetti3 3:4956cc0efdf3 55 }
rmadisetti3 3:4956cc0efdf3 56 break;
rmadisetti3 3:4956cc0efdf3 57 case '7': //left
rmadisetti3 3:4956cc0efdf3 58 if (bhit=='1') {
rmadisetti3 3:4956cc0efdf3 59 S = S - 0.5; //turn left
rmadisetti3 3:4956cc0efdf3 60 }
rmadisetti3 3:4956cc0efdf3 61 break;
rmadisetti3 3:4956cc0efdf3 62 case '8': //right
rmadisetti3 3:4956cc0efdf3 63 if (bhit=='1') {
rmadisetti3 3:4956cc0efdf3 64 S = S + 0.5; //turn right
rmadisetti3 3:4956cc0efdf3 65 }
rmadisetti3 3:4956cc0efdf3 66 break;
rmadisetti3 3:4956cc0efdf3 67 }
rmadisetti3 3:4956cc0efdf3 68 }
rmadisetti3 3:4956cc0efdf3 69 }
rmadisetti3 3:4956cc0efdf3 70 }
rmadisetti3 3:4956cc0efdf3 71 }
rmadisetti3 3:4956cc0efdf3 72 }
rmadisetti3 3:4956cc0efdf3 73
rmadisetti3 3:4956cc0efdf3 74 void autoPilot() {
rmadisetti3 4:29839de66eae 75 if (distanceCenter >= 250) {
rmadisetti3 3:4956cc0efdf3 76 M.speed(1.0);
rmadisetti3 3:4956cc0efdf3 77 S = 0.5;
rmadisetti3 3:4956cc0efdf3 78 }
rmadisetti3 3:4956cc0efdf3 79 else if (distanceRight > distanceLeft) { //More space on right so turn right
rmadisetti3 3:4956cc0efdf3 80 S = 1.0;
rmadisetti3 3:4956cc0efdf3 81 }
rmadisetti3 3:4956cc0efdf3 82 else { //or turn left
rmadisetti3 3:4956cc0efdf3 83 S = 0;
rmadisetti3 3:4956cc0efdf3 84 }
rmadisetti3 3:4956cc0efdf3 85 }
rmadisetti3 0:20a8dfc396cb 86
rmadisetti3 4:29839de66eae 87 void dist1(int distance)
rmadisetti3 4:29839de66eae 88 {
rmadisetti3 4:29839de66eae 89 distanceCenter = distance;
rmadisetti3 4:29839de66eae 90 }
rmadisetti3 4:29839de66eae 91
rmadisetti3 4:29839de66eae 92 void dist2(int distance)
rmadisetti3 4:29839de66eae 93 {
rmadisetti3 4:29839de66eae 94 distanceLeft = distance;
rmadisetti3 4:29839de66eae 95 }
rmadisetti3 4:29839de66eae 96
rmadisetti3 4:29839de66eae 97 void dist3(int distance)
rmadisetti3 4:29839de66eae 98 {
rmadisetti3 4:29839de66eae 99 distanceRight = distance;
rmadisetti3 4:29839de66eae 100 }
rmadisetti3 4:29839de66eae 101
rmadisetti3 4:29839de66eae 102 ultrasonic muCenter(p6, p7, .1, 1, &dist1); //sonar 1 initialization
rmadisetti3 4:29839de66eae 103 ultrasonic muLeft(p17, p18, .1, 1, &dist2); //sonar 2 initialization
rmadisetti3 4:29839de66eae 104 ultrasonic muRight(p15, p16, .1, 1, &dist3); //sonar 3 initialization
rmadisetti3 4:29839de66eae 105
rmadisetti3 4:29839de66eae 106
rmadisetti3 0:20a8dfc396cb 107
rmadisetti3 0:20a8dfc396cb 108 int main() {
rmadisetti3 4:29839de66eae 109 //SONAR Initializations
rmadisetti3 4:29839de66eae 110 muCenter.startUpdates();//SONAR 1 starts measuring the distance
rmadisetti3 4:29839de66eae 111 muLeft.startUpdates();//SONAR 2 starts measuring the distance
rmadisetti3 4:29839de66eae 112 muRight.startUpdates();//SONAR 3 starts measuring the distance
rmadisetti3 3:4956cc0efdf3 113 while(1) { //main loop for motor control
rmadisetti3 4:29839de66eae 114 pc.printf("Center D=%ld mm\r\n", distanceCenter);
rmadisetti3 4:29839de66eae 115 pc.printf("Right D=%ld mm\r\n", distanceRight);
rmadisetti3 4:29839de66eae 116 pc.printf("Left D=%ld mm\r\n", distanceLeft);
rmadisetti3 4:29839de66eae 117 muCenter.checkDistance(); //SONAR measuring starts
rmadisetti3 4:29839de66eae 118 muLeft.checkDistance();
rmadisetti3 4:29839de66eae 119 muRight.checkDistance();
rmadisetti3 3:4956cc0efdf3 120 getNewState();
rmadisetti3 4:29839de66eae 121 if (distanceCenter >= 250 && state == FORWARD) {
rmadisetti3 3:4956cc0efdf3 122 M.speed(1.0);
rmadisetti3 3:4956cc0efdf3 123 }
rmadisetti3 3:4956cc0efdf3 124 else if (state == REVERSE) {
rmadisetti3 3:4956cc0efdf3 125 M.speed(-1.0);
rmadisetti3 3:4956cc0efdf3 126 }
rmadisetti3 3:4956cc0efdf3 127 else if (state == AUTOPILOT) {
rmadisetti3 3:4956cc0efdf3 128 autoPilot();
rmadisetti3 3:4956cc0efdf3 129 }
rmadisetti3 3:4956cc0efdf3 130 else {
rmadisetti3 3:4956cc0efdf3 131 M.speed(0);
rmadisetti3 2:5c9526ccb055 132 }
rmadisetti3 2:5c9526ccb055 133 }
rmadisetti3 3:4956cc0efdf3 134
rmadisetti3 0:20a8dfc396cb 135 }