In last few days I was busy with planning, adding and testing of ultrasonic sensor and servo motor. Both of these part is for self driving obstacle avoidance robotic car. Since I had to relearn how to add motors and sensors and control them I choose to add these two sensors. This self driving robotic car uses the servo to move the ultrasonic sensor. When the robot car senses wall or obstructive object, it will stop around, look left and right and go to the direction where there is no obstruction. Using this build exercise will allow me to plan for the movement for the crane body and the arms. Also I have to plan for the bluetooth control vs the automatic control, that is the programming aspect.
After I screwed the tank part of the Arduino RoboMaster in my last blog post, I now had to plan how to fix the servo motor on the car chassis and how to fix the HC-05 ultrasonic sensor onto the servo motor. Below is video showing part of the work of planning and fixing of the ultrasonic sensor and servo motor
After that, I started by searching for an L shaped plastic material. I finally found something from a wall socket box. I used soldering iron and cut to an L shaped object.
As you can see, the motor is fixed with the longer side along the car but later I changed to the longer side of the servo motor along the front car wheels. The reason is that the motor arm movement is along its longer part from 0 to 180. I should have tested the arm movement first and then cut off the part of the chassis. The circuit diagram below shows the connection of Arduino L293D motor shield to the servo motor and to the HC-05 ultrasonic sensor module.
It should be noted that here I have not shown connection to the Bluetooth module because it is not required here in this work. For servo motor control see, servo motor control with arduino motor shield.
I fixed the L shaped part and the glued the ultrasonic sensor on the L shaped using super glue and bit using soldering iron.
#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>
#define TRIG_PIN A1
#define ECHO_PIN A2
#define MAX_DISTANCE 200
#define MAX_SPEED 190 // sets speed of DC motors
#define MAX_SPEED_OFFSET 20
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
Servo myservo;
boolean goesForward=false;
int distance = 100;
int speedSet = 0;
void setup() {
Serial.begin(9600);
myservo.attach(10);
myservo.write(50); //115
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
myservo.write(78);
delay(500);
myservo.write(55);
delay(500);
myservo.write(130); // Move servo arm 180 degrees
delay(500);
Serial.print(distance);
Serial.println(" cm");
}
void loop() {
int distanceR = 0;
int distanceL = 0;
delay(40);
Serial.print(distance);
Serial.println(" cm");
if(distance<=15)
{
moveStop();
delay(100);
moveBackward();
delay(300);
moveStop();
delay(200);
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);
if(distanceR>=distanceL)
{
turnRight();
moveStop();
}else
{
turnLeft();
moveStop();
}
}else
{
moveForward();
}
distance = readPing();
}
int lookRight()
{
myservo.write(146); //70
delay(500);
int distance = readPing();
delay(100);
myservo.write(100); //150
return distance;
}
int lookLeft()
{
myservo.write(100); //150
delay(500);
int distance = readPing();
delay(100);
myservo.write(55);
return distance;
delay(100);
}
int readPing() {
delay(70);
int cm = sonar.ping_cm();
if(cm==0)
{
cm = 250;
}
return cm;
}
void moveStop() {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void moveForward() {
if(!goesForward)
{
goesForward=true;
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
}
}
}
void moveBackward() {
goesForward=false;
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
}
}
void turnRight() {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void turnLeft() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
As you can see I made a big hole to allow the movement of the servo arm and the ultrasonic sensor module.
//simple program to calculate distance with HC-SR04
#define echoPin 11 // Echo pin connected to Arduino 11
#define trigPin 12 // Trig pin connected to Arduino 12
void setup() {
Serial.begin (115200);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
}
void loop() {
//define some constants
int duration, distance;
//send trigger signal
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//calculate distance
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
//print it
Serial.print("distance:");
Serial.print(distance);
Serial.println(" cm");
//add some delay
delay(500);
}
References
# Building RoboMaster with Arduino - Part 2
# Quick HC-05 AT startup guide - Arduino RoboMaster Part 3
# Testing RC Bluetooth Car App and Motor Code -Arduino RoboMaster Part 4
# Lithium Ion Battery and Charger - Arduino RoboMaster Part 5
# My First Arduino RC Car - Arduino RoboMaster part 6
# Android Studio for Wireless Robotic controller -Arduino RoboMaster Part 8