Self Controlled vehicle

This is a simple vehicle which will change the path/direction by sensing the clear path. If there are some obstruction, the vehicle will change the direction and start moving in another direction.

Self tracking vehicle

  • Actuators / output devices: 2 dc motors
  • Control method: self controlled
  • CPU: Arduino
  • Operating system: none
  • Power source: 9 V DC from adapter
  • Programming language: C
  • Sensors / input devices: Distance measuring sensor

This is a companion discussion topic for the original entry at https://community.robotshop.com/robots/show/self-controlled-vehicle

Swachalit Vahan bhag 2

This video is continuation of my arduino project

After a long break…

Hello All,

It had been a very long break. I left my work due to busy office and home life.

After regaining interest again, I have come up with a more profound and more sensible vehicle.

There are still some small bugs. But that will get fixed with time.

So, just go through the next video now…

Facing issues in power supply without USB

I am looking for a work around to remove the dependency of connecting my vehicle to USB. 

Currently I tried using 4 AA size 1.5 v cells. They are able to run servo, but unable to power motors.

So, I connected with 9v DC battery. That also created some odd behavior in the working of the vehicle.

 

Done…

Though, the work was done much earlier, I am sharing my code with this stuff.

The issue with arduino board is the battery power supply. It’s hard to get a proper portable power supply for arduino+motor shield

 

Here’s the code below:

 

#include <AFMotor.h>

#include <Servo.h>

 

#define MIN_DISTANCE 50

#define RADIUS 4.5

#define PIE 3.14

//The speed of the vehicle is calculated by doing practical. Setting the speed value of motor as 255 for both motors, and running for 10 seconds.

//The distance covered in this case comes out to be 150 cm. So, speed is 15cm/sec.

#define SPEED 15

////////////////////GLOBAL VARIABLES - START////////////////

AF_DCMotor motor1(1);

AF_DCMotor motor2(2);

Servo myservo;

int pos = 0;

int IRpin = 0;

int distance;

int maxDistance;

int turningDirection;

uint8_t maxDistAngle;

uint8_t turningTime;

enum{

LEFT=-1,

BACKSIDE,

RIGHT

};

////////////////////GLOBAL VARIABLES - END/////////////////

 

int readDistance(){

  Serial.println(FUNCTION);

  return (6762/(analogRead(IRpin)-9))-4;

}

 

void runmotor(int dire){

  Serial.print("[");

  Serial.print(TIME);

  Serial.print("]");

  Serial.print(FUNCTION);

  Serial.print(": ");

  Serial.println(dire);

  motor1.run(dire);

  motor2.run(dire);

}

 

void turning(int directionRight, int directionLeft, int turningTime){

  Serial.println(FUNCTION);

motor1.run(directionRight);

motor2.run(directionLeft);

delay(turningTime1000);

motor1.run(RELEASE);

motor2.run(RELEASE);

}

int readMaxDistance(){

  Serial.println(FUNCTION);

myservo.write(0);

int dist,maxDist;

for(int pos = 0; pos<=180; pos++){

myservo.write(pos);

dist = readDistance();

if(dist>maxDist){

maxDist = dist;

maxDistAngle = pos;

}

                delay(50);

        }

    return maxDist;

}

 

int calculateTurningTime(){

  Serial.println(FUNCTION);

//arcLength = (2PIERADIUSmaxDistAngle)/360

//To fasten the process, let’s put the pre-calculated constants’ value

//So, 2PIERADIUS/360 comes out to be 0.55

int arcLength = 0.55maxDistAngle;

//Now, time = distance/velocity

int turningTime = arcLength/15;

  return turningTime;

}

 

 

int setTurningDirection(){

  Serial.println(FUNCTION);

if(maxDistAngle>90)

return LEFT;

else if(maxDistAngle<90)

return RIGHT;

else if(maxDistAngle == 90)

return BACKSIDE;

else{

Serial.println(“Something wrong. Shouldn’t this statement gets printed”);

return -3;

        }

}

 

void findPath(){

runmotor(RELEASE);

         delay(1000);

maxDistance = readMaxDistance();

                Serial.print("maxDistance ");

                Serial.println(maxDistance);

        delay(1000);

turningTime = calculateTurningTime();

                Serial.print("turningTime ");

                Serial.println(turningTime);

        delay(1000);

turningDirection = setTurningDirection();

                Serial.print("turningDirection ");

                Serial.println(turningDirection);

        delay(1000);

if(RIGHT == turningDirection)

turning(FORWARD, BACKWARD,turningTime);

else if(LEFT == turningDirection)

turning(BACKWARD, FORWARD, turningTime);

else if(BACKSIDE == turningDirection){

runmotor(BACKWARD);

delay(turningTime1000);

runmotor(RELEASE);

}

else

Serial.println(“No cases matched for direction. Something wrong”);

 

        myservo.write(90);

        delay(1000);

 

}

//////////////////////////////MAIN FUNCTIONS/////////////////////////////////////

void setup(){

Serial.begin(9600);           // set up Serial library at 9600 bps

    Serial.println(FUNCTION);

myservo.attach(9);

 

  // turn on motor

motor1.setSpeed(255);

motor2.setSpeed(255);

 

motor1.run(RELEASE);

motor2.run(RELEASE);

}

 

void loop() {

  Serial.println(FUNCTION);

distance = readDistance();

Serial.print("Distance: ");

        Serial.println(distance);

        delay(1000);

if(distance<MIN_DISTANCE)

          findPath();

runmotor(FORWARD);

}

 

//////////////////////////////MAIN FUNCTIONS/////////////////////////////////////