Construccion de la clase robot, todo movido allí

parent 778d5818
Showing with 92 additions and 73 deletions
...@@ -2,4 +2,5 @@ __pycache__/ ...@@ -2,4 +2,5 @@ __pycache__/
*.pyc *.pyc
.venv/ .venv/
.vscode/ .vscode/
.idea/
#!/usr/bin/env pybricks-micropython #!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick from robot import Robot
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,
InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import Port, Stop, Direction, Button, Color
from pybricks.tools import wait, StopWatch, DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile, ImageFile
# Create your objects here.
ev3 = EV3Brick()
colorSensor = ColorSensor(Port.S4)
left_motor = Motor(Port.A)
right_motor = Motor(Port.D)
giroSensor = GyroSensor(Port.S1)
robot = DriveBase(left_motor, right_motor, wheel_diameter=55.5, axle_track=124)
# Variables para almacenar los valores de calibración de color.
green_rgb = (35, 55, 39)
black_rgb = (8, 4, 19)
tolerancia = 5
# Función para determinar si un color es verde basado en la calibración.
def is_green(rgb):
if (rgb[0] >= (green_rgb[0]-tolerancia) and rgb[0] <= (green_rgb[0]+tolerancia)) and (rgb[1] >= (green_rgb[1]-tolerancia) and rgb[1] <= (green_rgb[1]+tolerancia)) and (rgb[2] >= (green_rgb[2]-tolerancia) and rgb[2] <= (green_rgb[2]+tolerancia)):
return True
return False
def is_black(rgb):
if (rgb[0] >= (black_rgb[0]-tolerancia) and rgb[0] <= (black_rgb[0]+tolerancia)) and (rgb[1] >= (black_rgb[1]-tolerancia) and rgb[1] <= (black_rgb[1]+tolerancia)) and (rgb[2] >= (black_rgb[2]-tolerancia) and rgb[2] <= (black_rgb[2]+tolerancia)):
return True
return False
# Contador de líneas negras.
black_line_count = 0
# Función para seguir la línea verde.
def follow_green_line():
while True:
rgb = colorSensor.rgb()
# Si detecta verde, sigue adelante.
if is_green(rgb):
robot.drive(100, 0)
elif is_black(rgb):
robot.drive(50,0)
# Si detecta otro color (no verde), entonces buscará la línea verde girando.
else:
robot.drive(0, 50)
# Comienza a seguir la línea verde.
follow_green_line()
robot = Robot()
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor,InfraredSensor, UltrasonicSensor, GyroSensor)
from pybricks.parameters import Port, Stop, Direction, Button, Color
from pybricks.tools import wait, StopWatch, DataLog
from pybricks.robotics import DriveBase
from pybricks.media.ev3dev import SoundFile, ImageFile
class Robot: class Robot:
def __init__(self,robot,ev3,giroSensor,colorSensor): green_rgb = (35, 55, 39)
self.robot = robot black_rgb = (8, 4, 19)
self.ev3 = ev3 blue_rgb = (0,0,0)
self.giroSensor = giroSensor tolerancia = 5
self.colorSensor = colorSensor contadorNegros = 0
#Metodo para el giro def __init__(self):
def giro(grados): self.ev3 = EV3Brick()
robot.reset() left_motor = Motor(Port.A)
giroSensor.reset_angle(0) right_motor = Motor(Port.D)
robot.turn(grados) self.giroSensor = GyroSensor(Port.S1)
while(giroSensor.angle() != grados): self.colorSensor = ColorSensor(Port.S4)
diferencia = giroSensor.angle() - grados self.robot = DriveBase(left_motor, right_motor, wheel_diameter=55.5, axle_track=124)
#Metodos gestion negros
def queNegroEs(self):
if self.contadorNegros % 2 == 0:
return 0
return 1
def resetContadorNegros(self):
self.contadorNegros = 0
def incrementaContNegros(self):
self.contadorNegros = self.contadorNegros + 1
#metodos deteccion de colores
def is_green(rgb):
if (rgb[0] >= (Robot.green_rgb[0]-Robot.tolerancia) and rgb[0] <= (Robot.green_rgb[0]+Robot.tolerancia)) and (rgb[1] >= (Robot.green_rgb[1]-Robot.tolerancia) and rgb[1] <= (Robot.green_rgb[1]+Robot.tolerancia)) and (rgb[2] >= (Robot.green_rgb[2]-Robot.tolerancia) and rgb[2] <= (Robot.green_rgb[2]+Robot.tolerancia)):
return True
return False
def is_black(rgb):
if (rgb[0] >= (Robot.black_rgb[0]-Robot.tolerancia) and rgb[0] <= (Robot.black_rgb[0]+Robot.tolerancia)) and (rgb[1] >= (Robot.black_rgb[1]-Robot.tolerancia) and rgb[1] <= (Robot.black_rgb[1]+Robot.tolerancia)) and (rgb[2] >= (Robot.black_rgb[2]-Robot.tolerancia) and rgb[2] <= (Robot.black_rgb[2]+Robot.tolerancia)):
return True
return False
def is_blue(rgb):
if (rgb[0] >= (Robot.blue_rgb[0]-Robot.tolerancia) and rgb[0] <= (Robot.blue_rgb[0]+Robot.tolerancia)) and (rgb[1] >= (Robot.blue_rgb[1]-Robot.tolerancia) and rgb[1] <= (Robot.blue_rgb[1]+Robot.tolerancia)) and (rgb[2] >= (Robot.blue_rgb[2]-Robot.tolerancia) and rgb[2] <= (Robot.blue_rgb[2]+Robot.tolerancia)):
return True
return False
#Metodos para el movimiento
def giro(self,grados):
self.robot.reset()
self.giroSensor.reset_angle(0)
self.robot.turn(grados)
while(self.giroSensor.angle() != grados):
diferencia = self.giroSensor.angle() - grados
ajuste = diferencia * -1 ajuste = diferencia * -1
for i in range(abs(ajuste)): for i in range(abs(ajuste)):
if(ajuste > 0): if(ajuste > 0):
robot.turn(1) self.robot.turn(1)
else: else:
if (ajuste < 0): if (ajuste < 0):
robot.turn(-1) self.robot.turn(-1)
def seguir_linea(self,color="verde"):
while True:
rgb = self.colorSensor.rgb()
#caso en que detecta linea verde
if self.is_green(rgb):
self.robot.drive(100,0)
#caso detecta negro
elif self.is_black(rgb):
#incremento el contador
self.incrementaContNegros()
#miro si es par o impar
if self.queNegroEs() == 0:
#si es par es que es linea por lo que avanzo para sobrepasar la linea
self.robot.drive(100,0)
else:
#caso en que encuentro cuadrado
self.ev3.speaker.beep()
self.robot.stop()
#caso en que se detecta otro color
else:
self.robot.drive(0,50)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment