miércoles, 11 de septiembre de 2019

Implementación Proyecto Tecnológico Parte 2

Makeblock control remoto Bluetooth

Recuerde ubicar el modulo bluetooth correctamente en el mBot.




-Código

El código completo del control remoto se muestra a continuación.
recuerde que se utilizo la extensión "bluetooth controller" y una variable nombrada "potencia" para definir la velocidad del motor.




Para realizar el código en Arduino recuerde instalar la librería.


#include <MePS2.h>
#include <MeMCore.h>
#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

float Potencia = 0;

MePS2 MePS2(PORT_5);
MeDCMotor motor_9(9);
MeDCMotor motor_10(10);
void move(int direction, int speed) {
int leftSpeed = 0;
int rightSpeed = 0;
if(direction == 1) {
leftSpeed = speed;
rightSpeed = speed;
} else if(direction == 2) {
leftSpeed = -speed;
rightSpeed = -speed;
} else if(direction == 3) {
leftSpeed = -speed;
rightSpeed = speed;
} else if(direction == 4) {
leftSpeed = speed;
rightSpeed = -speed;
}
motor_9.run((9) == M1 ? -(leftSpeed) : (leftSpeed));
motor_10.run((10) == M1 ? -(rightSpeed) : (rightSpeed));
}


void _delay(float seconds) {
long endTime = millis() + seconds * 1000;
while(millis() < endTime) _loop();
}


void setup() {
MePS2.begin(115200);
Potencia = 100;
while(1) {
if(MePS2.ButtonPressed(11)){
Potencia = 255;


}
if(MePS2.ButtonPressed(10)){
Potencia = 191.25;


}
if(MePS2.ButtonPressed(12)){
Potencia = 127.5;


}
if(MePS2.ButtonPressed(9)){
Potencia = 63.75;


}
if((MePS2.ButtonPressed(18)) || (MePS2.ButtonPressed(20))){
motor_9.run(0);
motor_10.run(0);


}
if(((MePS2.MeAnalog(8) > 0) && (MePS2.MeAnalog(6) == 0.000000)) || ((MePS2.MeAnalog(4) > 0) && (MePS2.MeAnalog(2) == 0.000000))){
move(1, Potencia / 100.0 * 255);


}
if(((MePS2.MeAnalog(8) == 0.000000) && (MePS2.MeAnalog(6) > 0)) || ((MePS2.MeAnalog(4) == 0.000000) && (MePS2.MeAnalog(2) > 0))){
move(4, Potencia / 100.0 * 255);


}
if(((MePS2.MeAnalog(8) < 0) && (MePS2.MeAnalog(6) == 0.000000)) || ((MePS2.MeAnalog(4) < 0) && (MePS2.MeAnalog(2) == 0.000000))){
move(2, Potencia / 100.0 * 255);


}
if(((MePS2.MeAnalog(8) == 0.000000) && (MePS2.MeAnalog(6) < 0)) || ((MePS2.MeAnalog(4) == 0.000000) && (MePS2.MeAnalog(2) < 0))){
move(3, Potencia / 100.0 * 255);


}
if(MePS2.ButtonPressed(14)){
move(1, Potencia / 100.0 * 255);


}
if(MePS2.ButtonPressed(17)){
move(4, Potencia / 100.0 * 255);


}
if(MePS2.ButtonPressed(15)){
move(2, Potencia / 100.0 * 255);


}
if(MePS2.ButtonPressed(16)){
move(3, Potencia / 100.0 * 255);


}
if((MePS2.ButtonPressed(5)) && (MePS2.ButtonPressed(1))){
move(1, Potencia / 100.0 * 255);


}
if(MePS2.ButtonPressed(7)){
move(3, Potencia / 100.0 * 255);
_delay(0.1);
move(3, 0);


}
if(MePS2.ButtonPressed(3)){
move(4, Potencia / 100.0 * 255);
_delay(0.1);
move(4, 0);


}
if(MePS2.ButtonPressed(13)){
while(!(!(0)))
{
_loop();
}
if(Potencia == 255.000000){
Potencia = 255;


}else{
Potencia = (Potencia + 17);


}


}


_loop();
}


}


void _loop() {
MePS2.loop();
}


void loop() {
_loop();
}

Seguidor de linea y evasor de obstáculos

Recuerde conectar los sensores y darle la mejor ubicación para que cumpla correctamente con la función.

En este caso los sensores están ubicados en los puerto 2 y 4.
2 para el sensor de linea y 4 para el sensor de ultrasonido.


- Código
En el cogido creado se realizo un propio código de bloques para el seguidor de linea y utilizamos una variable nombrada "potencia" para definir la velocidad.

Para realizar el código en Arduino recuerde instalar la librería.


#include <MeMCore.h>
#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

float Potencia = 0;

MeLineFollower linefollower_2(2);
MeDCMotor motor_9(9);
MeDCMotor motor_10(10);
void move(int direction, int speed) {
int leftSpeed = 0;
int rightSpeed = 0;
if(direction == 1) {
leftSpeed = speed;
rightSpeed = speed;
} else if(direction == 2) {
leftSpeed = -speed;
rightSpeed = -speed;
} else if(direction == 3) {
leftSpeed = -speed;
rightSpeed = speed;
} else if(direction == 4) {
leftSpeed = speed;
rightSpeed = -speed;
}
motor_9.run((9) == M1 ? -(leftSpeed) : (leftSpeed));
motor_10.run((10) == M1 ? -(rightSpeed) : (rightSpeed));
}
void Linea (){
if(((1 ? (2 == 0 ? linefollower_2.readSensors() == 0 :
(linefollower_2.readSensors() & 2) == 2) :
(2 == 0 ? linefollower_2.readSensors() == 3 :
(linefollower_2.readSensors() & 2) == 0))) && ((1 ? (1 == 0 ? linefollower_2.readSensors() == 0 :
(linefollower_2.readSensors() & 1) == 1) :
(1 == 0 ? linefollower_2.readSensors() == 3 :
(linefollower_2.readSensors() & 1) == 0)))){
move(1, Potencia / 100.0 * 255);

}else{
if((1 ? (2 == 0 ? linefollower_2.readSensors() == 0 :
(linefollower_2.readSensors() & 2) == 2) :
(2 == 0 ? linefollower_2.readSensors() == 3 :
(linefollower_2.readSensors() & 2) == 0))){
move(3, Potencia / 100.0 * 255);

}else{
if((1 ? (1 == 0 ? linefollower_2.readSensors() == 0 :
(linefollower_2.readSensors() & 1) == 1) :
(1 == 0 ? linefollower_2.readSensors() == 3 :
(linefollower_2.readSensors() & 1) == 0))){
move(4, Potencia / 100.0 * 255);

}else{
move(1, 50 / 100.0 * 255);
_delay(0.5);
move(1, 0);

}

}

}

}

MeUltrasonicSensor ultrasonic_4(4);

void _delay(float seconds) {
long endTime = millis() + seconds * 1000;
while(millis() < endTime) _loop();
}

void setup() {
pinMode(A7, INPUT);

Potencia = 150;
while(!((0 ^ (analogRead(A7) > 10 ? 0 : 1))))
{
_loop();
}
while(!((1 ^ (analogRead(A7) > 10 ? 0 : 1))))
{
_loop();
}
while(1) {
if(ultrasonic_4.distanceCm() < 10){
move(3, 50 / 100.0 * 255);
_delay(0.25);
move(3, 0);
move(1, 50 / 100.0 * 255);
_delay(0.5);
move(1, 0);
move(1, 50 / 100.0 * 255);
_delay(0.5);
move(1, 0);
move(4, 50 / 100.0 * 255);
_delay(0.25);
move(4, 0);
move(1, 50 / 100.0 * 255);
while(!((((1 ? (2 == 0 ? linefollower_2.readSensors() == 0 :
(linefollower_2.readSensors() & 2) == 2) :
(2 == 0 ? linefollower_2.readSensors() == 3 :
(linefollower_2.readSensors() & 2) == 0))) && ((1 ? (1 == 0 ? linefollower_2.readSensors() == 0 :
(linefollower_2.readSensors() & 1) == 1) :
(1 == 0 ? linefollower_2.readSensors() == 3 :
(linefollower_2.readSensors() & 1) == 0)))) || ((((1 ? (2 == 0 ? linefollower_2.readSensors() == 0 :
(linefollower_2.readSensors() & 2) == 2) :
(2 == 0 ? linefollower_2.readSensors() == 3 :
(linefollower_2.readSensors() & 2) == 0))) && ((0 ? (1 == 0 ? linefollower_2.readSensors() == 0 :
(linefollower_2.readSensors() & 1) == 1) :
(1 == 0 ? linefollower_2.readSensors() == 3 :
(linefollower_2.readSensors() & 1) == 0)))) || (((0 ? (2 == 0 ? linefollower_2.readSensors() == 0 :
(linefollower_2.readSensors() & 2) == 2) :
(2 == 0 ? linefollower_2.readSensors() == 3 :
(linefollower_2.readSensors() & 2) == 0))) && ((1 ? (1 == 0 ? linefollower_2.readSensors() == 0 :
(linefollower_2.readSensors() & 1) == 1) :
(1 == 0 ? linefollower_2.readSensors() == 3 :
(linefollower_2.readSensors() & 1) == 0)))))))
{
_loop();
}
move(4, 50 / 100.0 * 255);
_delay(0.25);
move(4, 0);

}else{
Linea();

}

_loop();
}

}

void _loop() {
}


No hay comentarios:

Publicar un comentario