Mechatronics Exercises

Workspace Navigation

Simple IoT Autopilot robot.


Group 12:

Team members: Jonathan Lundström, Alex Norrgård and Oliver Olin

Project Goal:

Our goal is to build a car with wireless steering and which is controlled by smartphone app, with button to turn on a simple autopilot (Works with radar = Servo + Ultrasound sensor).

Demovideo: https://www.youtube.com/watch?v=-O-yBJADFKE


Parts list

Parts needed int the project + Some jumper cables and a battery of choice (5v-12v), Project used 4 x AA batteries

For this project we have 3D-printed the following parts (Bottom and Cover) to use as the body of the robot.


Electronics.

Circuit diagram of the robot

Both the H-bridge and the arduino board are powered by 4x AA 1,5 V batteries. NOTE: RX VOLTAGE ON BLUETOOTH MODULE PIN 3,3 V, VOLTAGE DIVIDER NEEDED


Build process.

The process to build the robot is quite straight forward. First the shell of the robot must be 3D-printed, it will house a major part of the electronics. Meanwhile the shell is being printed it is good to check all the electronic parts one by one, so you know everything is working. Then it is time for some soldering and connecting everything according to the circuit diagram. Jumper cables are a good alternative at least for testing everything and can be used for the final product, but more permanent connections could be done. After everything is connected it is again good to test that everything works. As a last thing the code and the app must be done, either by downloading ours or making them by yourself. Our code for the Arduino contains all the needed functions ready for use. Same goes with the app, it is developed using MIT App Inventor and the required code can be seen in the pictures.


STEP 1: 3D-printing the shell

STEP 2: Putting together the electronics


STEP 3: Compiling Arduino code, and building the app

Available for download, in "CODE", section or viewable by clicking "expand suorce".

Robot2.ino
//Define pins

#define left_dir 12     //Normal
#define right_speed 11  //PWM
#define left_speed 10   //PWM
#define right_dir 9     //PWM
#define echoPin 8       //Normal
#define trigPin 7       //Normal

//Define libaries
#include <SoftwareSerial.h>
#include <ServoEasing.h>

int command = 0,l_dir = 0, r_dir = 0, l_speed = 0, r_speed = 0, velocity = 150;
float min_dist = 20; //(cm)

ServoEasing Servo1;
SoftwareSerial Bluetooth(2, 3); // RX, TX


void drive(int l_dir, int r_dir, int l_speed, int r_speed){
  digitalWrite(left_dir, l_dir);
  digitalWrite(right_dir, r_dir);
  analogWrite(right_speed, r_speed);
  analogWrite(left_speed, l_speed);
}

float readDistance(){
  digitalWrite(trigPin, LOW);   
  delayMicroseconds(2);       
  digitalWrite(trigPin, HIGH);  
  delayMicroseconds(10);      
  digitalWrite(trigPin, LOW);   

  long duration = pulseIn(echoPin, HIGH);   
  float distance = (duration * 0.034) / 2 ;
  
  return distance;
}


void autoPilot(){
  while (command != 6) {
    if (Bluetooth.available()) {
      command = Bluetooth.parseInt();
    }
    drive(1,1, velocity, velocity);
    
    if (readDistance() < (0.8*min_dist) ) {
       drive(0,0,0,0);
       delay(50);
       Servo1.startEaseTo(30);
       delay(500);
       float dis_r = readDistance();
       delay(200);
       Servo1.startEaseTo(150);
       delay(1000);
       float dis_l = readDistance();
       delay(200);
       Servo1.startEaseTo(90);
       delay(1000);
       float dis_f = readDistance();
       if ((dis_l > dis_r) && (dis_l > min_dist)){
          drive(0,1,0,velocity/2);
          delay(500);
          drive(0, 0, 0, 0);
       }
       else if ((dis_r > dis_l) && (dis_r > min_dist)) {
          drive(1,0,velocity/2,0);
          delay(500);
          drive(0, 0, 0, 0);
       }
       else {
          drive(1, 0, velocity/2, 0);
          delay(900);
          drive(0, 0, 0, 0);
        }
     }
  }
  drive(0,0,0,0);
}


void setup() {
  
  pinMode(left_dir, OUTPUT);
  pinMode(right_dir, OUTPUT);
  pinMode(left_speed, OUTPUT);
  pinMode(right_speed, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(trigPin, OUTPUT);

  Servo1.attach(5);
  Servo1.write(90);
  Servo1.setSpeed(120);
  
  Bluetooth.begin(9600);
  Serial.begin(9600);
  
  digitalWrite(right_dir,LOW);
  digitalWrite(left_dir,LOW);
  analogWrite(right_speed,LOW);
  analogWrite(left_speed,LOW);
  
}

void loop() {

  if (Bluetooth.available()) {
    command = Bluetooth.parseInt();
  }
  switch (command) {
    case 0: // STOP
      drive(0, 0, 0, 0);
      break;
    case 1: // Forward
      if (readDistance() > min_dist){
      drive(1,1, velocity, velocity);
      }
      else {
        drive(0, 0, 0, 0); // Stop if close to object
      }
      break;
    case 2: // Turn right
      drive(1, 0, velocity/2, 0);
      delay(250);
      drive(0, 0, 0, 0);
      break;
    case 3: // Turn left
      drive(0, 1, 0, velocity/2);
      delay(250);
      drive(0, 0, 0, 0);
      break;
    case 4: // Reverse
      drive(1, 0, velocity/2, 0);
      delay(900);
      drive(0, 0, 0, 0);
      break;
    case 5: // Autopilot on
      autoPilot();
      break;
    default:
      if (readDistance() < min_dist){
        drive(0,0,0,0);
      }
      break;
  }
}

NOTE: Group found use of the ServoEasing.h for smooth Servo movements between angels. (Also helped with servo randomly twitching at times)

Pictures of our MIT App inventor app, controlling our robot.



STEP 4: Completed project

Demovideo: https://www.youtube.com/watch?v=-O-yBJADFKE

Conclusion

In the begining, the group was quite sure that building a project, as simple as a Bluetooth controlled car, would be easy. This however was proven wrong when the group spent about 4 weeks troubleshooting components that where broken or incompatable with Blynk (Our first option for a controller). It also did not help that blynk somehow shortcircuted/broke our original H-bridge. A word of advise from our group would be to avoid Blynk, and use MIT app Inventor instead (Might look complicated at first, but in the end easier to manage than blynks coding requirements).

Our original intent was to first build a car that could be remote controlled and put on Autopilot and then add on features, since in the begining it did not seem to be that hard to implement. But as we countered problems with those functions, we never where able to implement functions such as: Always on "radar scanning" that prints out into console screen on app (Example: https://www.youtube.com/watch?v=kQRYIH2HwfY).

Also the gruop would like to note that using Ultrasound sensors in a moving project caused alot of headache, since sensor readings could fluctuate alot during movement (+ Diffrent surface materials would dampen the ultrasound waves).

Code (INO and .APK)

Robot2.0.inoRobot.apk

  • No labels
  File Modified
JPEG File 20210311_164611472_iOS.jpg Mar 14, 2021 by Alex Norrgård
JPEG File 20210311_170330026_iOS.jpg Mar 14, 2021 by Alex Norrgård
PNG File blocks(1).png Apr 05, 2021 by Jonathan Lundström
PNG File image2021-3-11_16-47-25.png Mar 11, 2021 by Alex Norrgård
PNG File image2021-3-11_16-47-44.png Mar 11, 2021 by Alex Norrgård
PNG File image2021-3-11_16-50-56.png Mar 11, 2021 by Alex Norrgård
PNG File image2021-4-5_12-27-53.png Apr 05, 2021 by Alex Norrgård
PNG File image2021-4-5_12-28-46.png Apr 05, 2021 by Alex Norrgård
PNG File MIT_components.png Apr 05, 2021 by Jonathan Lundström
JPEG File photo_2021-03-11_16-17-47.jpg Mar 11, 2021 by Alex Norrgård
JPEG File photo_2021-03-11_16-28-41.jpg Mar 11, 2021 by Alex Norrgård
JPEG File photo_2021-03-11_16-50-41.jpg Mar 11, 2021 by Alex Norrgård
JPEG File photo_2021-04-05_12-58-27.jpg Apr 05, 2021 by Jonathan Lundström
JPEG File photo_2021-04-05_12-59-22.jpg Apr 05, 2021 by Jonathan Lundström
File robot_alusta.3mf Apr 05, 2021 by Jonathan Lundström
File robot_kansi.3mf Apr 05, 2021 by Jonathan Lundström
File Robot.apk Apr 05, 2021 by Jonathan Lundström
File Robot2.0.ino Apr 05, 2021 by Jonathan Lundström