-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor_laser.py
57 lines (46 loc) · 1.59 KB
/
motor_laser.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
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BOARD)
def translate(value, leftMin, leftMax, rightMin, rightMax):
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the left range into a 0-1 range (float)
valueScaled = float(value - leftMin) / float(leftSpan)
# Convert the 0-1 range into a value in the right range.
return rightMin + (valueScaled * rightSpan)
class Servo:
def __init__(self, pin):
GPIO.setup(pin, GPIO.OUT)
self.current_pos = 7.5 # 90 degrees
self.pwm = GPIO.PWM(pin, 50)
self.pwm.start(self.current_pos)
def move_right(self, degree=10):
change = degree / 18
self.current_pos = max(2.5, self.current_pos - change)
self.pwm.ChangeDutyCycle(self.current_pos)
print("[SERVO] Moving left")
print("[SERVO] Current position:", self.current_pos)
def move_left(self, degree=10):
change = degree / 18
self.current_pos = min(12.5, self.current_pos + change)
self.pwm.ChangeDutyCycle(self.current_pos)
print("[SERVO] Moving right")
print("[SERVO] Current position:", self.current_pos)
def move(self, degree=0):
if degree >= 0:
self.move_right(translate(degree, 0, 400, 0, 20))
else:
self.move_left(translate(-degree, 0, 400, 0, 20))
sleep(0.1)
class Laser():
def __init__(self, pin):
self.pin = pin
GPIO.setup(self.pin, GPIO.OUT)
GPIO.output(self.pin, False)
def on(self):
GPIO.output(self.pin, True)
print("[LASER] ON")
def off(self):
GPIO.output(self.pin, False)
print("[LASER] OFF")