-
Notifications
You must be signed in to change notification settings - Fork 49
/
control_motores.py
68 lines (51 loc) · 1.17 KB
/
control_motores.py
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
import RPi.GPIO as GPIO
import time
SPEED = 10
L_PWM_PIN = 16
L_DIR_PIN = 15
R_PWM_PIN = 12
R_DIR_PIN = 11
GPIO.setmode(GPIO.BOARD)
GPIO.setup(L_PWM_PIN, GPIO.OUT)
GPIO.setup(L_DIR_PIN, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(R_PWM_PIN, GPIO.OUT)
GPIO.setup(R_DIR_PIN, GPIO.OUT, initial=GPIO.LOW)
L_MOTOR = GPIO.PWM(L_PWM_PIN, 1000)
R_MOTOR = GPIO.PWM(R_PWM_PIN, 1000)
def MotorsSetup():
L_MOTOR.start(0)
R_MOTOR.start(0)
def L_Speed(speed):
if speed <= 0:
GPIO.output(L_DIR_PIN, GPIO.HIGH)
else:
GPIO.output(L_DIR_PIN, GPIO.LOW)
speed = abs(speed)
if speed > 100:
speed = 100
L_MOTOR.ChangeDutyCycle(speed)
def R_Speed(speed):
if speed <= 0:
GPIO.output(R_DIR_PIN, GPIO.LOW)
else:
GPIO.output(R_DIR_PIN, GPIO.HIGH)
speed = abs(speed)
if speed > 100:
speed = 100
R_MOTOR.ChangeDutyCycle(speed)
def Direction(difference):
difference /= 2
L_Speed(SPEED+difference)
R_Speed(SPEED-difference)
def BaseSpeed(speed):
global SPEED
SPEED = speed
L_Speed(SPEED)
R_Speed(SPEED)
def GetSpeed():
global SPEED
return SPEED
def MotorsStop():
L_MOTOR.stop()
R_MOTOR.stop()
GPIO.cleanup()