Table of Contents

Object Avoidance Robot

Autor: Topală Andrei-Tudor

Introducere

Descriere generala + schema bloc

Hardware Design

Lista de componente

Componente auxiliare pentru montaj

Schema electrica

Software Design

Motoare

enum Direction {
  LEFT, RIGHT
};
 
/* Motor control pins of 
 *   
 * enAPin: Left motor PWM speed control
 * enBPin: Right motor PWM speed control
 * in1Pin: Left motor Forward
 * in2Pin: Left motor Backwards
 * in3Pin: Right motor Forward
 * in4pin: Right motor Backwards
 */
const int enAPin = 6;
const int in1Pin = 7;
const int in2Pin = 5;
const int in3Pin = 4;
const int in4Pin = 2;
const int enBPin = 3;
 
/* Set motor speed: 255 full ahead, −255 full reverse , 0 stop */
void go(enum Direction m, int speed)
{
  digitalWrite(m == LEFT ? in1Pin : in3Pin, speed > 0 ? HIGH : LOW);
  digitalWrite(m == LEFT ? in2Pin : in4Pin, speed <= 0 ? HIGH : LOW);
  analogWrite(m == LEFT ? enAPin : enBPin, speed < 0 ? -speed : speed);
}
 
void moveForward() {
  go(LEFT, 200);
  go(RIGHT, 180);
}
 
void moveLeft() {
  go(LEFT, -200);
  go(RIGHT, 0);
  delay(600);
}
 
void moveRight() {
  go(LEFT, 0);
  go(RIGHT, -180);
  delay(700);
}
 
void moveStop() {
  go(LEFT, 0);
  go(RIGHT, 0);
}

Servo

#include <Servo.h>
 
#define START_ANGLE 2
#define NUM_ANGLES 5
 
Servo servo;
/* Servo motor that aims ultrasonic sensor.
 *
 * servoPin: PWM output for servomotor
 */
const int servoPin = 11;
 
int centimeter = 0;
 
/* Servomotor angles to masure best potential path
 * 
 * sensorAngle: arrya of angles
 * distance: array of distances between sensor and object for each angle
 */
unsigned char sensorAngle[NUM_ANGLES] = {20, 60, 90, 130, 160};
unsigned int distance[NUM_ANGLES] = {0};
 
/* Moves ultrasonic sensor in 5 different angles
 * to get a better view of the field.
 * 
 * @return: Direction (LEFT/RIGHT) for the next path
 */
unsigned int useServo() {
  static unsigned char angleIndex = START_ANGLE;
  static signed char step = 1;
  int maxDistance = 0, nextDirection = 0;
  int distanceLeft = 0, distanceRight = 0;
 
  for (int i = 0; i < 8; ++i) {
    angleIndex += step;
    if (angleIndex == NUM_ANGLES - 1) {
      step = -1;
    } else if (angleIndex == 0) {
      step = 1;
    }
    servo.write(sensorAngle[angleIndex]);
    distance[angleIndex] = readUltrasonicDistance() / CM_FACTOR;
 
    delay(500);
  }
 
  /* Distance mean for both directions. */
  distanceLeft = (distance[0] + distance[1]) / 2;
  distanceRight = (distance[3] + distance[4]) / 2;
 
  return (distanceLeft > distanceRight) ? LEFT : RIGHT;
}

Senzor ultrasonic

#define CM_FACTOR 58.00
 
/* Ultrasonic Module pins 
 * 
 * triggerPin: 10 microsecond high pulse causes sharp sound , wait 50 us
 * echoPin: Width of high pulse indicates distance
 */
const int triggerPin = 12;
const int echoPin = 13;
 
long readUltrasonicDistance() {
  /* Clear the trigger */
  pinMode(triggerPin, OUTPUT);
 
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
 
  return pulseIn(echoPin, HIGH) / CM_FACTOR;
}

Loop

#define MIN_COLLISION_DISTANCE 10
 
void loop() {
    centimeter = readUltrasonicDistance();
    if (centimeter < MIN_COLLISION_DISTANCE) {
      moveStop();
 
      int nextDirection = useServo();
 
      if (nextDirection == LEFT) {
        moveLeft();
      } else {
        moveRight();
      }
      moveStop();
    } else {
      moveForward();
    }
}

Rezultate

Poze dispozitiv

top_oar.jpg Placuta Arduino este alimentata de la baterii prin pinul Vin si este conectata la driver-ul de motoare, la servomotor si la senzorul ultrasonic.

front_oar.jpg Senzorul ultrasonic este plasat pe un suport de camera controlat de un servomotor capabil sa se roteasca pe planul orizontal.

back_oar.jpg Bateriile si motoarele sunt lipite pe spatele sasiului pentru a economisi spatiu. Bateriile alimenteaza atat motoarele, cat si microcontrolerul.

Videoclip de prezentare

Download

Cod Sursa (arhiva)

Jurnal

Concluzii

Bibiliografie/Resurse

Export to PDF