marți, 26 noiembrie 2013

Arduino autonomous robot

   This is a simple project, developed around an Arduino Nano board, wich is almost the same as Arduino Uno, but much smaller.

   THE HARDWARE  

   The chasis was taken from a Spyder Rock Crawler RC car, so the performance and further development posibilities are allmost endless. The following information can be used to build your own autonomous robot using standard RC platforms wich allready have an ESC(electronic speed control) and a servo for steering.
    Having an ESC makes the life a bit easier since it can be controlled using the same <Servo.h> library, the only thing diffenrend from servos is that the ESC must be initialized with a specific value for a specific amount of time.
    As example, my ESC needed the value 100 for 1 second.
 
Front view of the robot. You can nottice the ultrasonic range sensor (HC-SR04) mounted on top of regular servo.

The right side anntena is from the RC receiver, wich is not used for the moment.








   Top view of the robot. Here you can observe starting from left to right, the turret (servo + ultrasonic), the mainboard containing the Arduino board, a 5V voltage regulator and some pin connectors and finally the ESC.

   THE CODE

   The code is using only the main loop, no interrupts and no timers. It's quite simple, but effective. There are also some downsights. The intense use of delay function slows down the robot, but with a little care each delay can be set to a minimum value.
   Care must be taken to not go under the minimum time required by the ultrasonic sensor for send-receive process. For the HC-SR04 this is around 30ms, but I strongly advice not to go below 50ms.
   For any suggestions just leave a repply below ... and feel free to use the code for your own projects.

#include <Servo.h>
#include <NewPing.h>

Servo turret;
Servo dir;
Servo esc;
int Left = 140;
int Right = 40;
int Center = 90;
static int L;
static int R;

#define TRIGGER_PIN  7
#define ECHO_PIN     6
#define LED_PIN 13
#define MAX_DISTANCE 400
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup()
{
  esc.attach(3);
  esc.write(100);
  delay(1500);
  turret.attach(12);
  dir.attach(8);
  dir.write(Center);
  turret.write(90);
  pinMode(13, OUTPUT);
}

void loop()
{
  int ping = sonar.ping_cm();
   
  if (ping != 0 && ping <= 70)
  {
    digitalWrite(LED_PIN,HIGH);
    brake();
    delay(20);
 
    go_look();
    delay(200);

    reverse();
    delay(800);
    if (L > R)
    {turn_left();
     delay(1000);}
    else
    {
    if (L < R)
    {turn_right();
     delay(1000);}
    }
   
   }
   else
   {
     digitalWrite(LED_PIN,LOW);
     forward();
   }
}

void go_look()
{
  turret.write(80);
  int R1 = sonar.ping_cm();
  delay(200);
  turret.write(60);
  int R2 = sonar.ping_cm();
  delay(200);
  turret.write(40);
  int R3 = sonar.ping_cm();
  delay(200);
  turret.write(20);
  int R4 = sonar.ping_cm();
  delay(1000);

  turret.write(110);
  int L1 = sonar.ping_cm();
  delay(200);
  turret.write(130);
  int L2 = sonar.ping_cm();
  delay(200);
  turret.write(150);
  int L3 = sonar.ping_cm();
  delay(200);
  turret.write(170);
  int L4 = sonar.ping_cm();
  delay(200);

  turret.write(90);
  delay(200);

  L = (L1+L2+L3+L4)/4;
  R = (R1+R2+R3+R4)/4;
}

void forward()
{
  esc.write(89);
  dir.write(Center);
}

void brake()
{
  esc.write(103);
}

void reverse()
{
  esc.write(112);
  dir.write(Center);
}

void turn_left()
{
  esc.write(89);
  dir.write(Left);
  delay(100);
}

void turn_right()
{
  esc.write(89);
  dir.write(Right);
  delay(100);
}

IN ACTION



   

Un comentariu:

  1. How To Make A Casino With Gambling - drmcd
    Gambling games are an incredibly simple concept to make money, and most people do not understand 밀양 출장마사지 how 광주광역 출장샵 to play casino games. The casino software and 제주도 출장안마 the casino games that 보령 출장샵 you 거제 출장샵

    RăspundețiȘtergere