Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Vicente Castellano Gómez
/
robotAmbientales
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
26af2877
authored
Apr 29, 2024
by
Vicente Castellano Gómez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Construccion de la clase robot, todo movido allí
parent
778d5818
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
92 additions
and
73 deletions
.gitignore
main.py
robot.py
.gitignore
View file @
26af2877
...
...
@@ -2,4 +2,5 @@ __pycache__/
*.pyc
.venv/
.vscode/
.idea/
main.py
View file @
26af2877
#!/usr/bin/env pybricks-micropython
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
# 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
()
from
robot
import
Robot
robot
=
Robot
()
robot.py
View file @
26af2877
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
:
def
__init__
(
self
,
robot
,
ev3
,
giroSensor
,
colorSensor
):
self
.
robot
=
robot
self
.
ev3
=
ev3
self
.
giroSensor
=
giroSensor
self
.
colorSensor
=
colorSensor
#Metodo para el giro
def
giro
(
grados
):
robot
.
reset
()
giroSensor
.
reset_angle
(
0
)
robot
.
turn
(
grados
)
while
(
giroSensor
.
angle
()
!=
grados
):
diferencia
=
giroSensor
.
angle
()
-
grados
green_rgb
=
(
35
,
55
,
39
)
black_rgb
=
(
8
,
4
,
19
)
blue_rgb
=
(
0
,
0
,
0
)
tolerancia
=
5
contadorNegros
=
0
def
__init__
(
self
):
self
.
ev3
=
EV3Brick
()
left_motor
=
Motor
(
Port
.
A
)
right_motor
=
Motor
(
Port
.
D
)
self
.
giroSensor
=
GyroSensor
(
Port
.
S1
)
self
.
colorSensor
=
ColorSensor
(
Port
.
S4
)
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
for
i
in
range
(
abs
(
ajuste
)):
if
(
ajuste
>
0
):
robot
.
turn
(
1
)
self
.
robot
.
turn
(
1
)
else
:
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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment