-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjetos.py
More file actions
84 lines (65 loc) · 1.89 KB
/
objetos.py
File metadata and controls
84 lines (65 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# modo_lab.py
import time, machine, pyb
from hcsr04 import HCSR04
# === PINES MOTORES ===
ena_pin = machine.Pin("PA6", machine.Pin.OUT)
enb_pin = machine.Pin("PA5", machine.Pin.OUT)
pwm_a = machine.Pin("PB6")
pwm_b = machine.Pin("PA7")
tim_a = pyb.Timer(4, freq=2000)
ch_a = tim_a.channel(1, pyb.Timer.PWM, pin=pwm_a)
tim_b = pyb.Timer(3, freq=2000)
ch_b = tim_b.channel(2, pyb.Timer.PWM, pin=pwm_b)
# === SENSOR ULTRASONICO FRONTAL ===
sensor = HCSR04(
trigger_pin=machine.Pin("PA9"),
echo_pin=machine.Pin("PA8"),
echo_timeout_us=30000
)
# ============================
# FUNCIONES DE MOVIMIENTO
# ============================
def adelante(speed=70): # velocidad mayor
ena_pin.low()
enb_pin.low()
ch_a.pulse_width_percent(speed)
ch_b.pulse_width_percent(speed)
def detener():
ch_a.pulse_width_percent(0)
ch_b.pulse_width_percent(0)
def girar_derecha(speed=70): # giro más rápido
ena_pin.high()
enb_pin.low()
ch_a.pulse_width_percent(speed)
ch_b.pulse_width_percent(speed)
# ============================
# DISTANCIA FRONTAL SEGURA
# ============================
def distancia_frontal():
try:
return sensor.distance_cm()
except:
return 999
# ============================
# LOOP PRINCIPAL
# ============================
def loop():
# Límites
OBSTACULO = 10
LIBRE = 15
dist = distancia_frontal()
print("Distancia:", dist)
if dist > OBSTACULO:
adelante(70)
return
# Si está muy cerca → detener
detener()
time.sleep(0.1)
# Evita el obstaculo girando a la derecha
while distancia_frontal() < LIBRE:
print("Evitando obstaculo")
girar_derecha(60)
time.sleep(0.03)
detener()
time.sleep(0.05)
adelante(70)