Autito 360º
Principio de funcionamiento
Conexiones
Ø
Servo 360º 1º à
Pin D9
Ø
Servo 360º 2º à
Pin D6
Ø
Sensor B/N à
Pin D2
Programa 1 – Control por serial
// Habilitamos variables |
#include
<Servo.h> Servo
_ruedaizq, _ruedader ; String
_pc ; #define
VELO(x) x*90+90 |
// Función setup() |
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("arduino:ok") ;
Serial.setTimeout(10) ;
_ruedaizq.attach(9) ;
_ruedader.attach(6) ; } |
// Loop, esperando mensajes |
void loop()
{
// put your main code here, to run repeatedly:
_pc = "" ;
if( Serial.available() ) {
_pc = Serial.readString() ;
_pc.trim() ; |
// Si es adelante |
if( _pc == "adelante" ) {
_ruedaizq.write( VELO(.5) ) ;
_ruedader.write( VELO(-.5) ) ;
} |
// Si es atrás |
if( _pc == "atras" ) {
_ruedaizq.write( VELO(-.5) ) ;
_ruedader.write( VELO(.5) ) ;
} |
// Si es un costado |
if( _pc == "izq" ) {
_ruedaizq.write( VELO(.5) ) ;
_ruedader.write( VELO(.5) ) ;
}
if( _pc == "der" ) {
_ruedaizq.write( VELO(-.5) ) ;
_ruedader.write( VELO(-.5) ) ;
} |
// Si es detener |
if( _pc == "detener" ) {
_ruedaizq.write( VELO(0) ) ;
_ruedader.write( VELO(0) ) ;
} |
// Fin del programa |
} } |
Programa 2 – Automóvil autónomo
// Definiciones útiles |
#define
function #define
VELOCIDAD(x) x*90 + 90 |
// Variables de estado |
int
_estado = 0 ; float _tiempoEstado
= 0 ; String
_pc ; |
// Variables de componentes |
#include
<Servo.h> Servo
_rueda1, _rueda2 ; |
// Para el setup |
void
function fnSetupSerial() {
Serial.begin( 9600 ) ;
Serial.setTimeout(10) ;
Serial.println("arduino:conectado") ; } void
function fnSetupComponentes() {
_rueda1.attach( 6 ) ;
_rueda2.attach( 9 ) ;
pinMode( 2, INPUT ) ;
randomSeed( analogRead(A4) ) ; } void
function setup() {
fnSetupSerial() ;
fnSetupComponentes() ; } |
// Máquina de estados finitos |
void
function loop() {
_tiempoEstado += 100 ;
delay( 100 ) ;
_pc = "" ;
if( Serial.available() ) {
_pc = Serial.readString() ;
_pc.trim() ;
}
if( _estado == 0 ) return fnEstado0() ;
if( _estado == 1 ) return fnEstado1() ;
if( _estado == 2 ) return fnEstado2() ;
if( _estado == 3 ) return fnEstado3() ; } |
// Transiciones de los estados |
void
function fnCambiarEstado( int _nuevoEstado) {
_estado = _nuevoEstado ;
_tiempoEstado = -100 ;
Serial.print( "estado:" ) ; Serial.println(_nuevoEstado)
;
if( _estado == 0 ) fnTransicionEstado0() ;
if( _estado == 1 ) fnTransicionEstado1() ;
if( _estado == 2 ) fnTransicionEstado2() ;
if( _estado == 3 ) fnTransicionEstado3() ; } // Hacia
el estado deshabilitado void
function fnTransicionEstado0() {
Serial.println("desactivado") ;
_rueda1.write( VELOCIDAD(0) ) ;
_rueda2.write( VELOCIDAD(0) ) ; } //
Hacia el estado habilitado void function
fnTransicionEstado1() {
Serial.println("Activado, Esperando 1") ;
_rueda1.write( VELOCIDAD(0) ) ;
_rueda2.write( VELOCIDAD(0) ) ; } //
Habilitando el vehículo autónomo void
function fnTransicionEstado2() {
Serial.println("Avanzando....") ;
_rueda1.write( VELOCIDAD(0.3) ) ;
_rueda2.write( VELOCIDAD(-0.3) ) ; } // Obstáculo
detectado, retroceder void
function fnTransicionEstado3() {
Serial.println("Obstáculo! Rebotando") ; } |
// Estado 0 – Estado deshabilitado – No hace
nada |
void
function fnEstado0() {
if( _pc == "activar" ) return fnCambiarEstado(1) ; } |
// Estado 1 – Estado habilitado, esperando
1 para adelantar |
void
function fnEstado1() {
if( _pc == "desactivar" ) return fnCambiarEstado(0) ;
if( _pc == "1" ) return fnCambiarEstado(2) ; } |
// Estado 2 – Avanzando. 0 para detener. Si
sensor detecta, rebotar |
void function
fnEstado2() {
if( _pc == "0" ) return fnCambiarEstado(1) ;
int _medida = !digitalRead(2) ;
Serial.print("medida:") ; Serial.println(_medida);
if( _medida ) return fnCambiarEstado(3) ; } |
// Estado 3 – Forma de rebote: retroceder y
elegir lado al azar |
void
function fnEstado3() {
_rueda1.write( VELOCIDAD(-0.3) ) ;
_rueda2.write( VELOCIDAD(0.3) ) ;
delay( 1000 ) ;
float _aleatorio = random(0,2) ;
if( _aleatorio ) { _rueda1.write( VELOCIDAD(-0.3) ) ;
_rueda2.write( VELOCIDAD(-0.3) ) ;
}
else {
_rueda1.write( VELOCIDAD(0.3) ) ;
_rueda2.write( VELOCIDAD(0.3) ) ;
}
delay( 2000 ) ;
fnCambiarEstado(2) ; } |
Comentarios
Publicar un comentario