-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathdemo.py
executable file
·150 lines (112 loc) · 4.45 KB
/
demo.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/python3
'''
Python demo for controlling ServoProject servos
'''
import time
import math
import numpy as np
from ServoProjectModules.Communication import pi
import ServoProjectModules.Communication as ServoComModule
from ServoProjectModules.Communication import CommunicationError
dt = 0.018
def createServoManager(port):
def createServosFunction():
simCom = ServoComModule.SimulateCommunication()
if port != '':
com = ServoComModule.SerialCommunication(port)
else:
print('Simulation mode active')
com = simCom
servoArray = []
newServo = ServoComModule.DCServoCommunicator(1, com)
newServo.setOffsetAndScaling(360.0 / 4096.0, -110.0, 0)
newServo.setControlSpeed(32)
newServo.setBacklashControlSpeed(6, 180.0, 0.00)
servoArray.append(newServo)
newServo = ServoComModule.DCServoCommunicator(2, com)
newServo.setOffsetAndScaling(180.0 / 1900.0, 0.0, 0.0)
servoArray.append(newServo)
return servoArray
servoManager = ServoComModule.ServoManager(cycleTime=dt, initFunction=createServosFunction)
return servoManager
def playTrajectory(servoManager, trajectory):
doneRunning = False
index = 0
def sendCommandHandlerFunction(cycleTime, servoManager):
nonlocal index
nonlocal doneRunning
if index == 0:
pos = trajectory[0]
vel = [0.0] * len(pos)
elif index < len(trajectory) - 1:
pos = trajectory[index]
vel = (np.array(trajectory[index + 1]) - np.array(trajectory[index - 1])) / 2.0 / cycleTime
else:
pos = trajectory[-1]
vel = [0.0] * len(pos)
for i, servo in enumerate(servoManager.servoArray):
servo.setReference(pos[i], vel[i], 0.0)
index += 1
if index == len(trajectory):
servoManager.removeHandlerFunctions()
doneRunning = True
def readResultHandlerFunction(cycleTime, servoManager):
pos = servoManager.getPosition()
print(f'{pos[0] = :0.4f}, {pos[1] = :0.4f}', end='\r')
def errorHandlerFunction(inputException):
try:
raise inputException
except CommunicationError as e:
if e.code == CommunicationError.ErrorCode.COULD_NOT_SEND:
raise e
print('exception triggered: restarting communication...')
servoManager.shutdown()
servoManager.start()
servoManager.setHandlerFunctions(sendCommandHandlerFunction, readResultHandlerFunction, errorHandlerFunction)
while not doneRunning:
if not servoManager.isAlive():
break
time.sleep(0.1)
def addLinearMove(trajectory, endPos, duration):
startPos = np.array(trajectory[-1])
endPos = np.array(endPos)
steps = int(round(duration / dt))
for i in range(1, steps + 1):
t = i / steps
trajectory.append(endPos * t + startPos * (1 - t))
return trajectory
def addSmoothMove(trajectory, endPos, duration):
startPos = np.array(trajectory[-1])
endPos = np.array(endPos)
steps = int(round(duration / dt))
for i in range(1, steps + 1):
t = i / steps
t = (1.0 - math.cos(pi * t)) / 2.0
trajectory.append(endPos * t + startPos * (1 - t))
return trajectory
def addWait(trajectory, duration):
startPos = np.array(trajectory[-1])
steps = int(round(duration / dt))
for _ in range(1, steps + 1):
trajectory.append(startPos)
return trajectory
def main():
port = '' # '/dev/ttyACM0'
with createServoManager(port) as servoManager:
trajectory = []
trajectory.append(servoManager.getPosition())
trajectory = addSmoothMove(trajectory, [0.0, 0.0], 1.0)
trajectory = addWait(trajectory, 10.0)
for _ in range(0, 3):
# trajectory = addSmoothMove(trajectory, [2.0, 2.0], 0.2)
trajectory = addSmoothMove(trajectory, [-10.0, -10.0], 1.0)
trajectory = addSmoothMove(trajectory, [0.0, 0.0], 3.0)
trajectory = addWait(trajectory, 3.0)
# trajectory = addSmoothMove(trajectory, [-2.0, -2.0], 0.2)
trajectory = addSmoothMove(trajectory, [10.0, 10.0], 1.0)
trajectory = addSmoothMove(trajectory, [0.0, 0.0], 3.0)
trajectory = addWait(trajectory, 3.0)
trajectory = addWait(trajectory, 7.0)
playTrajectory(servoManager, trajectory)
if __name__ == '__main__':
main()