Академический Документы
Профессиональный Документы
Культура Документы
com Control de un servo mendiante envo de caracteres a travs de un puerto serie bluetooth La posicion del servo se controla manualmente mediante una de las entradas analogicas del arduino, en este caso "turnPostionPinIN" . Mediante dos seales digitales de salida del arduino, se controlan las puertas (gate) de los transistores del puente H que controla el motor que mueve el "servo" (motor) de la direccin de las ruedas. turnLeftPinOUT turnRightPinOUT Con seales digitales a los pines forwardPinOUT del puente H del motor princiapl El movimiento que queremos hacer se recibe por el puerto serie mediante caracteres, que indican hacia que posicion nos queremos mover. En nuestro caso el puerto serie esta conectado a un mdulo bluetooth el cual recibe los commandos (caracteres) desde un telefono android con un app de puerto serie bluetooth. En nuestro caso usamos el app Bluetooth serial controller disponible en el playstore. backwardPinOUT controlamos los "gates"
TODO: Se podria usar una variable target direction para transmittir directamente por serie el valor deseado de giro, de momento nos desplazaremos a izq, der, centro TODO: Buscar un app que use el acelerometro del movil para trasmitir direcciones y/o velocidad.
**/ #include <Arduino.h> const int turnPostionPinIN = 0; //VERDER : GND const int turnLeftPinOUT = 5; const int turnRightPinOUT = 3; const int forwardPinOUT = 9; const int backwardPinOUT = 10; //DOWN //UP -- Amarillo: 5V -- Blanco: ref
//naranja //NEGRO
const int marginalDirection = 30; // Margen al cual si nos acercamos suficiente dejamos de alimentar el servo
char command; // variable to receive data from the serial port const char CMD_RIGHT = 'R'; const char CMD_LEFT = 'L'; const char CMD_CENTER = 'S'; const char CMD_FORWARD = 'F'; const char CMD_BACKWARD = 'B'; const int POS_MAX_RIGHT = 1024; const int POS_CENTER = POS_MAX_RIGHT / 2; const int POS_MIN_LEFT = 0; const int MAX_SPEED = 255; const int SPEED_STEP = 16; int currentSpeed = 0; int safetyTime = 7000; long timeMark,timeMark2; //Speed goes from -255 to 255
void setup() { pinMode(turnLeftPinOUT,OUTPUT); pinMode(turnRightPinOUT,OUTPUT); pinMode(11, OUTPUT); analogWrite(11,130); Serial.begin(9600); // start serial communication at 9600bps Serial.println("OK __________________________ START"); analogWrite( forwardPinOUT , 0 ); timeMark = millis(); } void loop() { //Parpadear cada byte recibido if( Serial.available() ) // if data is available to read { command = Serial.read(); //Serial.print( command ); debuggin purposes // read it and store it in 'val' // echo the command back for
Serial.print( getTurnPosition ( turnPostionPinIN ) ); timeMark2 = timeMark; timeMark = millis(); if( command == CMD_RIGHT ) { } else } else moveRight(); if ( command == CMD_LEFT ){ moveLeft(); if ( command == CMD_CENTER ){ moveCenter(); stop(); } else if ( command == CMD_FORWARD ) { accelerate(); } else if ( command == CMD_BACKWARD ) { decelerate(); } else { timeMark = timeMark2; } } //Si no llega ningun comando vamos a la posicion central si no estamos ya ah //O NO hacemos nada ? else { if ( inCenter( ) != 1 ) { //moveCenter(); } stopTurn();
if ( timeMark + safetyTime < millis() ) { slowDown(); } } //Serial.println(currentSpeed); delay(25); } void stop () { currentSpeed = 0; setSpeed ( currentSpeed ); }
} setSpeed ( currentSpeed ); } void decelerate() { if ( currentSpeed > -MAX_SPEED ) { currentSpeed = currentSpeed - SPEED_STEP;
} setSpeed ( currentSpeed ); }
void slowDown(){ if (currentSpeed > 0 ) { currentSpeed -= 1; } else { currentSpeed += 1; } setSpeed(currentSpeed); //Serial.println(currentSpeed); } void setSpeed(int speed ) { if (speed > MAX_SPEED ) speed = MAX_SPEED; if (speed < -MAX_SPEED ) speed = -MAX_SPEED;
//Serial.println(speed); if ( speed > SPEED_STEP ) { digitalWrite( backwardPinOUT , LOW ); analogWrite( forwardPinOUT , speed ); } else if (speed < -SPEED_STEP ) { digitalWrite( forwardPinOUT , LOW ); analogWrite( backwardPinOUT , -speed ); } else { digitalWrite( forwardPinOUT , LOW ); digitalWrite( backwardPinOUT , LOW ); } } void moveRight( ) { if ( ! isRightMost () ){ right(); }
Serial.println("CENTER "); if ( ! inCenter () ){ if ( inLeftSide() ) { right(); Serial.println("right "); } else{ left(); Serial.println("left "); } } } int inCenter () { if ( getTurnPosition ( turnPostionPinIN ) - marginalDirection < POS_CENTER && getTurnPosition ( turnPostionPinIN ) + marginalDirection > POS_CENTER ) { return 1; } else { return 0; } }
int isRightMost () { if ( getTurnPosition ( turnPostionPinIN ) + marginalDirection > POS_MAX_RIGHT ) { return 1; } else { return 0; } }
else { return 0; } } //Posicion actual del servo ( 0 - MAX_RIGHT) int getTurnPosition (int pin ) { int raw =analogRead( pin ); int directionServoReading = map ( raw , 260 , 870 ,POS_MIN_LEFT , POS_MAX_RIGHT) ; Serial.print("RAW: " ); Serial.print(raw); Serial.print(" Pos:"); Serial.println(directionServoReading); return directionServoReading; }
int inLeftSide ( ) { return getTurnPosition ( turnPostionPinIN ) < POS_CENTER ; } void right () { Serial.println("RIGHT"); digitalWrite( turnLeftPinOUT , LOW); delay(1); digitalWrite( turnRightPinOUT , HIGH); delay(40); } void left () { Serial.println("LEFT"); digitalWrite( turnRightPinOUT , LOW); delay(1); digitalWrite( turnLeftPinOUT , HIGH); delay(40); } void stopTurn() { //Serial.println("STOP"); digitalWrite( turnRightPinOUT , LOW); digitalWrite( turnLeftPinOUT , LOW); }