Documente Academic
Documente Profesional
Documente Cultură
/*
Arduino obstacle avoider and follower.
Goal in life...
Moves forward looking for obstacles to avoid or
keep-away from them (Force)
/*
Uses the L9110S library. It works with the L9110S h-bridge. See
Step 1 for the library code.
*/
#include <L9110Driver.h>
#include <Servo.h>
// Bot modes
#define MODE_AVOID 0
#define MODE_FORCE 1
// Speed defines
#define MAXFORWARDSPEED 225 // Max speed we want moving forward
#define MAXBACKWARDSPEED 225 // Max reverse speed
#define TOPSPEED 255 // Used to help turn better on carpet and rougher
surfaces.
/*
Globals area.
*/
// Global variables
byte sweep_pos = 0; // Current position of the sensor servo
byte pos_index = 90;
byte mode = MODE_AVOID;
byte mode_change = true;
unsigned long last_pin_change;
unsigned long left_dist, right_dist; // Distance measured left and
right
if (mode_val == HIGH)
mode = MODE_FORCE;
mode_change == true;
}
}
void setup ( )
{
// Uncomment if you need to debug.
//Serial.begin (9600); // Set Serial monitor at 9600 baud
//Serial.println ("My Follow/Avoid bot is starting up!");
void loop ( )
{
checkMode ( ); // Read current switch value
if (mode == MODE_AVOID)
avoid ( ); // Try not to hit anything!
if (mode == MODE_FORCE)
force ( ); // keep-away from the hand!
}
void avoid ( )
{
unsigned long dist_fwd;
void force ( )
{
unsigned long obj_dist;
obj_dist = ping ( );
//Serial.print ("Distance sensor reading: ");
//Serial.println (obj_dist);
delay (250);
}
// Read the sensor after we find something in the way. This helps find
a new
// path
void sensor_read ( )
{
//Serial.println ("Server at 40 deg...");
sensor_servo.write (40);
delay (SERVOMOVEDELAY);
right_dist = ping ( ); //Look to the right
void go_forward ( )
{
//Serial.println ("Going forward...");
// Set to all of the set speed just incase the ramp last vat was
not all of
// it.
motor_left.setSpeed (MAXFORWARDSPEED);
motor_right.setSpeed (MAXFORWARDSPEED);
motor_left.run (FORWARD|RELEASE);
motor_right.run (BACKWARD|RELEASE);
}
void go_backward ( )
{
//Serial.println ("Going backward...");
// Set to all of the set speed just incase the ramp last vat was
not all of
// it.
motor_left.setSpeed (MAXBACKWARDSPEED);
motor_right.setSpeed (MAXBACKWARDSPEED);
motor_left.run (BACKWARD|RELEASE);
motor_right.run (FORWARD|RELEASE);
}
void go_left ( )
{
//Serial.println ("Going left...");
// Set to all of the set speed just incase the ramp last vat was
not all of
// it.
motor_left.setSpeed (TOPSPEED);
motor_right.setSpeed (TOPSPEED);
motor_left.run (BACKWARD|RELEASE);
motor_right.run (BACKWARD|RELEASE);
}
void go_right ( )
{
//Serial.println ("Going right...");
// Set to all of the set speed just incase the ramp last vat was
not all of
// it.
motor_left.setSpeed (TOPSPEED);
motor_right.setSpeed (TOPSPEED);
motor_left.run (FORWARD|RELEASE);
motor_right.run (FORWARD|RELEASE);
}
void halt ( )
{
//Serial.println ("Halt!");
motor_left.setSpeed (0);
motor_right.setSpeed (0);
motor_left.run (BRAKE);
motor_right.run (BRAKE);
}
Traduo
/*
Inibidor de obstculo Arduino e seguidor.
Objetivo na vida...
Movimentos para a frente procurando obstculos evitar ou
mantm afastado deles (fora)
#include <L9110Driver.h>
#include <Servo.h>
Modos de bot
#define MODE_AVOID 0
#define MODE_FORCE 1
Velocidade define
#define MAXFORWARDSPEED 225 / / ns queremos andar para a frente de
velocidade mxima
#define MAXBACKWARDSPEED 225 / / Max velocidade reversa
#define TOPSPEED 255 / / usado para ajudar a virar melhor no tapete e superfcies
mais speros.
/*
rea de Globals.
*/
Variveis globais
byte sweep_pos = 0; Posio atual do servo sensor
pos_index byte = 90;
modo de byte = MODE_AVOID;
byte mode_change = true;
last_pin_change longo no assinado;
left_dist longo no assinado, right_dist; Distncia medida esquerda e direita
se (mode_val = = ALTA)
modo = MODE_FORCE;
mode_change = = true;
}
}
void setup)
{
Descomente se voc precisa depurar.
Serial.Begin (9600); Conjunto monitor Serial a 9600 baud
Serial.println ("My Follow/evitar bot est comeando acima!");
Demora para dar tempo de usurio para fazer ajustes. Retire depois de feito.
atraso (30000);
}
void loop)
{
checkMode (); Ler o valor atual do interruptor
se (modo = = MODE_AVOID)
evitar (); Tente no bater em nada!
se (modo = = MODE_FORCE)
fora (); mantm afastado da mo!
}
evitar o vazio)
{
dist_fwd longo no assinado;
Ir para a frente quando nada est nos sensores de distncia lido rea
se (dist_fwd > MIN_ACTION_DIST | | dist_fwd = = 0)
{
go_forward ();
}
mais / / h algo em sensores ler rea
{
alto (); Pare!
go_backward (); Volta um pouco
atraso (BACKUPDELAY);
alto (); Pare!
fora vazio)
{
obj_dist longo no assinado;
Ir para a frente quando nada est nos sensores de distncia lido rea
se (obj_dist > = (MIN_AWAY_DIST + 20)) {
alto ();
go_forward ();
} mais se (obj_dist < = (MIN_AWAY_DIST + 5)) {
alto ();
go_backward ();