-
Notifications
You must be signed in to change notification settings - Fork 9
/
servoGripper.py
48 lines (42 loc) · 1.29 KB
/
servoGripper.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
#!/usr/bin/python3
import time
import RPi.GPIO as GPIO
from threading import Thread
"""
A class for controlling a servo via pigpio deamon
This class should get updated in it's own thread.
"""
# - - - - - - - - - - - - - - - -
# - - - SERVO GRIPPER CLASS - - -
# - - - - - - - - - - - - - - - -
class ServoGripper:
def __init__(self, pigpiod_pi, servoPin):
self.pi = pigpiod_pi
self.servoPin = servoPin
self.targetPos = 0
self.currentPos = 0
self.mode = 'idle'
self.sleepTime = 0.001
# available modes are:
# - idle
# - moving
self.startUpdateThread()
def startUpdateThread(self):
self.t = Thread(target=self.update, args=())
self.t.start()
def setTargetPos(self, targetPos):
self.targetPos = targetPos
def update(self):
while True:
self.updateCurrentPos()
self.pi.set_servo_pulsewidth(self.servoPin, 1000 + self.currentPos)
time.sleep(self.sleepTime)
def updateCurrentPos(self):
if self.targetPos > self.currentPos:
self.mode = "moving"
self.currentPos += 1
elif self.targetPos < self.currentPos:
self.mode = "moving"
self.currentPos -= 1
else:
self.mode = "idle"