sábado, 13 de junio de 2015

PROYECTO FINAL.

PROYECTO FINAL:
LABERINTO


  • MATERIALES:
                                                                                                        
                                                                
     
    Y como resultado…
IMG-20150523-WA0008.jpg


  • TAREAS QUE HA REALIZADO CADA MIEMBRO DEL GRUPO:
Entre hicimos todas las tareas; mientras unas cuantas pegábamos los materiales a la tabla, otras soldaban los cables o ponían algunas piezas que necesitaban tornillos y tuercas. El profesor nos puso cada cable conectado al lugar correspondiente.  Entre todas compramos las pilas para que funcionase y cargamos el programa. Finalmente, para la documentación del blog, cada una nos hemos encargado de hacer un trozo: tabla de los precios, subirlo al drive y al blog.


  • PROCESO DE CONSTRUCCIÓN:
1er día: En primer lugar, sujetamos con los tornillos, la rueda giratoria en el soporte blanco y pegamos las ruedas.
2º día: Pegamos el interruptor y el soporte para 4 pilas con la pistola termofusible.
3º día: Sujetamos la placa arduino con tornillos al soporte blanco, quitamos el borde protector de algunos cables y soldamos algunos cables con cobre
4º día: El profe conecta cada cable a su respectivo lugar en la placa arduino y  sujeta con tornillos la tabla perforada.
5º día: Conectamos los cables al interruptor ajustándolos con unos tornillos, lllevamos las pilas y probamos si funciona
6º dÍa: Cargamos el programa seguidor de linea


  • PRESUPUESTO DEL ROBOT:
Está en una carpeta dentro de “Proyecto final”.
Este presupuesto está hecho añadiendo LDR (seguidor de luz), utilizado en el proyecto que presentamos en la feria de Cuenca. El precio final no varía mucho pero sin LDR, el precio total se quedaría en: 238,4.


alkdfadlksfj.png


  • FERIA ARDUINO Y ROBOCAMPEONES:
Feria Arduino Cuenca:
El robot realizado en la feria arduino de la mesa 2, era igual que el realizado para robocampeones, pero su función era seguir la luz, por lo tanto, llevaba incorporados unos LDR.


Nuestro proyecto:



El código que hemos usado como seguidor de luz ha sido:
/*
Light following demo, en pruebas, using
 3 reflective sensors CNY70, h-bridge L293D and motors DC
*/
// Programa que permite controlar el movimiento basico
//de dos motores DC utilizando el puente H-Bridge L293D o similar.
const int switchPin = 2;    // switch input  De momento no se utiliza. Sirve para configuraciones posteriores.
const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
const int motor4Pin = 5;    // H-bridge leg 4 (pin 10, 4A)
const int motor3Pin = 6;    // H-bridge leg 3 (pin 15, 3A)
const int enablePin12 = 9;    // H-bridge enable pin motor M2
const int enablePin34 = 10;    // H-bridge enable pin motor M1
const int leftSensorPin =  A0;    // analog pins with sensors
const int rightSensorPin = A1;
//int sensorThreshold = 0;
void setup() {
 Serial.begin(9600);
 // set the switch as an input .Se utilizara en otros programas, no en este:
   pinMode(switchPin, INPUT);
// set all the other pins you're using as outputs:
   pinMode(motor1Pin, OUTPUT);
   pinMode(motor2Pin, OUTPUT);
   pinMode(motor4Pin, OUTPUT);
   pinMode(motor3Pin, OUTPUT);
        pinMode(enablePin12, OUTPUT);
   pinMode(enablePin34, OUTPUT);
   // set enablePin high so that motor can turn on:
  // digitalWrite(enablePin12, HIGH);
  // digitalWrite(enablePin34, HIGH);
   analogWrite(enablePin12, 127);
   analogWrite(enablePin34, 127);
}
void loop() {
// Read LDR sensors
 int leftVal = analogRead(leftSensorPin);
 delay(10);
 int rightVal = analogRead(rightSensorPin);
 delay(10);
 int colchon = 100;
 int diferencia = abs(leftVal-rightVal);
 Serial.print("leftVal= ");
 Serial.print(leftVal);
 Serial.print("rightVal= ");
 Serial.print(rightVal);
 Serial.print("diferencia= ");
 Serial.println(diferencia);
    if (rightVal > leftVal && diferencia > colchon){       
        line_slipLeft();
    }
    else if (rightVal < leftVal && diferencia > colchon)
       line_slipRight();
    }
    else if (diferencia < colchon){
        line_forward();
    }
    else
 //Este equilibrado, no mover
   line_stop();
   delay(15);
}
//  line_forward();   // on line
 //  delay(4000);
//  line_back();   // on line
 //  delay(4000);
 //    line_spinRight();   // on line
 //    delay(4000);
   // line_spinLeft();   // on line
  // delay(4000);
   //line_slipRight();   // on line
 // delay(4000);
   //line_slipLeft();   // on line
  // delay(4000);
   //line_stop();  // veering off left
  //delay(4000);
/*
 // If line is lost try to reacquire
 if (irReflectL < thresh && irReflectR < thresh) {
   line_spinRight();
   delay(20);
 }
 */
// Motion routines for line following
// Avance hacia adelante
void line_forward() {
 digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge high M2
 digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge low M2
 digitalWrite(motor4Pin, LOW);   // set leg 4 of the H-bridge high M1
 digitalWrite(motor3Pin, HIGH);  // set leg 3 of the H-bridge low M1
}
// Retroceso
void line_back() {
 digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge low M2
 digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high M2
 digitalWrite(motor4Pin, HIGH);   // set leg 4 of the H-bridge low M1
 digitalWrite(motor3Pin, LOW);  // set leg 3 of the H-bridge high M1
}
// Paro
void line_stop() {
 digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low M2
 digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge low M2
 digitalWrite(motor4Pin, LOW);   // set leg 14 of the H-bridge low M1
 digitalWrite(motor3Pin, LOW);  // set leg 3 of the H-bridge low M1
}
// Avance hacia a la derecha, motor derecho M1 parado
void line_slipRight() {
digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge high M2
digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge low M2
digitalWrite(motor4Pin, LOW);   // set leg 4 of the H-bridge low M1
digitalWrite(motor3Pin, LOW);  // set leg 3 of the H-bridge low M1
}
//avance hacia la izquierda, motor izquierdo M2 parado
void line_slipLeft() {
 digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low M2
 digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge low M2
 digitalWrite(motor4Pin, LOW);   // set leg 4 of the H-bridge high M1
 digitalWrite(motor3Pin, HIGH);  // set leg 3 of the H-bridge low M1
}
//Giro derecha
void line_spinRight() {
 digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge high M2
 digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge low M2
 digitalWrite(motor4Pin, HIGH);   // set leg 4 of the H-bridge low M1
 digitalWrite(motor3Pin, LOW);  // set leg 3 of the H-bridge high M1
}
//Giro izquierda
void line_spinLeft() {
 digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge low M2
 digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high M2
 digitalWrite(motor4Pin, LOW);   // set leg 4 of the H-bridge high M1
 digitalWrite(motor3Pin, HIGH);  // set leg 3 of the H-bridge low M1
}


Otros proyectos de la feria:
2015-05-12 12.05.15.jpg                            
Este proyecto consistía en que
cuando los fantasmas se levantaban,
tenías que poner la mano encima
para que bajaran.

(Este era un juego con bombillas verdes que se iban encendiendo muy rápido y para ganar debias parar la luz en la bombilla roja)


Robocampeones:
En esta feria de Majadahonda (Madrid), nuestro robot, era el mismo que presentamos en la feria de Cuenca pero sin LDR por lo tanto no era un seguidor de luz, sino un seguidor de línea. El proyecto era “Laberinto”. Tuvimos que ir a una sala donde competimos todos los robots de este estilo. Aunque el nuestro tuvo algunos fallos, hubo laberintos del I.E.S Pedro Mercedes que llegaron hasta la meta.
Aparte del robot, también era necesario un tablero con líneas negras de celo y cuadrados que marcaban la salida del robot.
Nuestro robot:


El código que se utilizó para que el robot funcionase fue el siguiente:
/*
Maze-Solving program adapted from
http://www.instructables.com/id/Maze-Solving-Robot/?lang=es&ALLSTEPS
*/
const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
const int motor4Pin = 5;    // H-bridge leg 4 (pin 10, 4A)
const int motor3Pin = 6; // H-bridge leg 3 (pin 15, 3A)
const int enablePin12 = 9; // H-bridge enable pin motor M2
const int enablePin34 = 10; // H-bridge enable pin motor M1
const int led = 7;
const int lineLLSense = A0;
const int lineLSense = A1;
const int lineCSense = A2;
const int lineRSense = A3;
const int lineRRSense = A4;
// Lecturas de los sensores
int irReflectRR = 0;
int irReflectR = 0;
int irReflectC = 0;
int irReflectL = 0;
int irReflectLL = 0;
// Umbral de sensibilidad
int thresh = 500;
int leapTime = 400;
void setup(){
 //Serial.begin(9600);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(motor4Pin, OUTPUT);
pinMode(motor3Pin, OUTPUT);
pinMode(enablePin12, OUTPUT);
pinMode(enablePin34, OUTPUT);
// set enablePin high so that motor can turn on:
digitalWrite(enablePin12, HIGH);
digitalWrite(enablePin34, HIGH);
delay(1000);
}
void loop(){
readSensors();                                                                                    
// Menor que thresh es lectura sobre blanco.
if(irReflectLL<thresh && irReflectRR<thresh && irReflectC>thresh ){
straight();                                                                                     
 }
 else{                                                                                             
leftHandWall();                                                                               
 }
void readSensors(){
 // Lectura de sensores
 irReflectLL = analogRead(lineLLSense);
 irReflectL = analogRead(lineLSense);
 irReflectC = analogRead(lineCSense);
 irReflectR = analogRead(lineRSense);
 irReflectRR = analogRead(lineRRSense);
 // Para calibrar
 /*
 Serial.print("irReflectLL= ");
 Serial.print(irReflectLL);
 Serial.print("irReflectL= ");
 Serial.print(irReflectL);
 Serial.print("irReflectC= ");
 Serial.print(irReflectC);
 Serial.print("irReflectR= ");
 Serial.print(irReflectR);
 Serial.print("irReflectRR= ");
 Serial.println(irReflectRR);
 delay(200);
*/
}
void leftHandWall(){
// Mayor que 200 es lectura sobre negro.
 if( irReflectLL>thresh && irReflectRR>thresh){
line_forward();  //avanza hacia adelante
delay(leapTime);
readSensors();
if(irReflectLL>thresh || irReflectRR>thresh){
  line_forward();
  delay(200);
  done();
}
if(irReflectLL<thresh && irReflectRR<thresh){
  turnLeft();
}
}
  if(irReflectLL>thresh){ // if you can turn left then turn left
line_forward();  //avanza un poco hacia adelante
delay(leapTime);
readSensors();
  if(irReflectLL<thresh && irReflectRR<thresh){
    line_forward();
    delay(200);
    turnLeft();
  }
  else
line_forward();
    delay(200);
    done();
  }
 }
 if(irReflectRR>thresh){
line_forward();
delay(30);
readSensors();
if(irReflectLL>thresh){
  delay(leapTime-30);
  readSensors();
  if(irReflectRR>thresh && irReflectLL>thresh){
    done();
  }
  else{
    turnLeft();
    return;
  }
}
   delay(leapTime-30);
readSensors();
if(irReflectLL<thresh && irReflectC<thresh && irReflectRR<thresh){
  turnRight();
  return;
}
 }
 readSensors();
 if(irReflectLL<thresh && irReflectC<thresh &&  irReflectRR<thresh && irReflectL<thresh && irReflectR<thresh){
turnAround();
 }
}
void done(){
 line_stop();
 delay(4000);
}
void turnLeft(){
 while(analogRead(lineCSense)>thresh){
line_spinLeft();
delay(2);
line_stop();
delay(1);
 }
 while(analogRead(lineCSense)<thresh){
line_spinLeft();
delay(2);
line_stop();
delay(1);
 }
}
void turnRight(){
 while(analogRead(lineCSense)>thresh){
line_spinRight();
delay(2);
line_stop();
delay(1);
 }
  while(analogRead(lineLSense)<thresh){
line_spinRight();
delay(2);
 }
}
void straight(){
 if( analogRead(lineCSense)<thresh){
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
digitalWrite(motor4Pin, HIGH);
digitalWrite(motor3Pin, LOW);
delay(1);
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
digitalWrite(motor4Pin, LOW);
digitalWrite(motor3Pin, LOW);
delay(5);
return;
 }
follow_line();
delay(4);
}
void turnAround(){
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
digitalWrite(motor4Pin, HIGH);
digitalWrite(motor3Pin, LOW);
delay(150);
  while(analogRead(lineCSense)<thresh){
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
digitalWrite(motor4Pin, HIGH);
digitalWrite(motor3Pin, LOW);
delay(2);
line_stop();
delay(1);
 }
}
void endMotion(){
digitalWrite(led, LOW);
delay(500);
digitalWrite(led, HIGH);
delay(200);
  digitalWrite(led, LOW);
delay(200);
digitalWrite(led, HIGH);
delay(500);
 endMotion();
}
void follow_line() {
if (irReflectC >= thresh && irReflectR <= thresh && irReflectL <= thresh) {
line_forward();   // on line
 }
 if (irReflectL >= thresh && irReflectR <= thresh) {
line_slipLeft();  // veering off right
delay(1);
 }
 if (irReflectL <= thresh && irReflectR >= thresh) {
line_slipRight();  // veering off left
delay(1);
 }
}
//**********************************************************
// Motion routines
//**********************************************************
// Motion routines for line following
// Avance hacia adelante
void line_forward() {
 digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge high M2
 digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge low M2
 digitalWrite(motor4Pin, HIGH);   // set leg 4 of the H-bridge high M1
 digitalWrite(motor3Pin, LOW);  // set leg 3 of the H-bridge low M1
}
// Retroceso
void line_back() {
 digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low M2
 digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high M2
 digitalWrite(motor4Pin, LOW);   // set leg 4 of the H-bridge low M1
 digitalWrite(motor3Pin, HIGH);  // set leg 3 of the H-bridge high M1
}
// Paro
void line_stop() {
 digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low M2
 digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge low M2
 digitalWrite(motor4Pin, LOW);   // set leg 14 of the H-bridge low M1
 digitalWrite(motor3Pin, LOW);  // set leg 3 of the H-bridge low M1
}
// Avance hacia a la derecha, motor derecho M1 parado
void line_slipLeft() {
digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge high M2
digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge low M2
digitalWrite(motor4Pin, LOW);   // set leg 4 of the H-bridge low M1
digitalWrite(motor3Pin, LOW);  // set leg 3 of the H-bridge low M1
}
//avance hacia la izquierda, motor izquierdo M2 parado
void line_slipRight() {
 digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low M2
 digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge low M2
 digitalWrite(motor4Pin, HIGH);   // set leg 4 of the H-bridge high M1
 digitalWrite(motor3Pin, LOW);  // set leg 3 of the H-bridge low M1
}
//Giro derecha
void line_spinRight() {
 digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge high M2
 digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge low M2
 digitalWrite(motor4Pin, HIGH);   // set leg 4 of the H-bridge low M1
 digitalWrite(motor3Pin, LOW);  // set leg 3 of the H-bridge high M1
}
//Giro izquierda
void line_spinLeft() {
 digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge low M2
 digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high M2
 digitalWrite(motor4Pin, LOW);   // set leg 4 of the H-bridge high M1
 digitalWrite(motor3Pin, HIGH);  // set leg 3 of the H-bridge low M1
}