forked from adeept/Adeept_RaspClaws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servo_service.py
234 lines (211 loc) · 7.61 KB
/
servo_service.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import random
import threading
import time
from blinker import signal
from server.RPIservo import ServoCtrl
MOVEMENT_TIME_X = 0.1
MOVEMENT_TIME_Y = 0.1
MOVEMENT_ANGLE_X = 5.0
MOVEMENT_ANGLE_Y = 10.0
class ServoService:
sc_direction = [
1,1,1,1,
1,1,1,1,
1,1,1,1,
1,1,1,1
]
sc_angles = [
0,0,0,0,
0,0,0,0,
0,
0,0,0,
0,0,0,0
]
sc_current = [
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0
]
sc_time = [
0.0,0.0,0.0,0.0,
0.0,0.0,0.0,0.0,
0.0,0.0,0.0,0.0,
0.0,0.0,0.0,0.0
]
forward = [
{
'angles': [
0.0,+MOVEMENT_ANGLE_Y,
0.0,-MOVEMENT_ANGLE_Y,
0.0,+MOVEMENT_ANGLE_Y,
0.0,+MOVEMENT_ANGLE_Y,
0.0,-MOVEMENT_ANGLE_Y,
0.0,+MOVEMEN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.0,MOVEMENT_TIME_X,0.0,
0.0,0.0,0.0,0.0
]
},
]
update_frequency = 30.0
move = False
move_step = 0
sc_gear = None
p_sc = None
t_sc = None
init_pwm = []
thread = None
running = True
def __init__(self, command_controller):
self.command_controller = command_controller
self.create_servos()
self.initialize_pwm()
self.create_commands()
signal('exit').connect(self.exit)
signal('diag').send(self, name='servo_service', state='starting')
def create_servos(self):
self.sc_gear = ServoCtrl()
self.sc_gear.moveInit()
# self.p_sc = ServoCtrl()
# self.p_sc.start()
#
# self.t_sc = ServoCtrl()
# self.t_sc.start()
def initialize_pwm(self):
for i in range(16):
self.init_pwm.append(self.sc_gear.initPos[i])
def create_commands(self):
parser = self.command_controller.create_command(
'servo',
'Control the servo',
self.handle_servo_command
)
parser.add_argument('--id', type=int, default=-1, help='The change affects to the given servo')
parser.add_argument('--angle', type=int, help='The angle')
parser.add_argument('--time', type=float, default=1.0, help='The angle')
parser.add_argument('--update-frequency', type=float, help='Sets the update frequency')
parser.add_argument('--forward', type=int, help='Moves forward')
parser.add_argument('--move', type=self.str_to_bool, help='Move automatically')
def str_to_bool(self, value):
if isinstance(value, bool):
return value
if value.lower() in {'false', 'f', '0', 'no', 'n', 'off'}:
return False
elif value.lower() in {'true', 't', '1', 'yes', 'y', 'on'}:
return True
raise ValueError(f'{value} is not a valid boolean value')
def create_thread(self):
self.thread = threading.Thread(target=self.run)
def start(self):
self.thread.start()
def join(self):
self.thread.join()
def run(self):
signal('diag').send(self, name='servo_service', state='started')
while self.running:
# print("[servo_service] run")
# self.sc_gear.moveAngle(0, random.random() * 40 - 20)
time_delta = 1.0 / self.update_frequency
moved = False
for id in range(len(self.sc_angles)):
if self.sc_time[id] + 0.01 >= time_delta:
total_delta = self.sc_angles[id] - self.sc_current[id]
time_steps = self.sc_time[id] / time_delta
delta_step = total_delta / time_steps
print('%f --- %f %f %f --- %f %f %f --- %f %f %f' %(
time_delta,
self.sc_current[id], self.sc_angles[id], self.sc_time[id],
total_delta, time_steps, delta_step,
self.sc_current[id] + delta_step, self.sc_angles[id] - delta_step, self.sc_time[id] - time_delta
))
self.sc_current[id] += delta_step
# self.sc_angles[id] -= delta_step
self.sc_time[id] -= time_delta
if -45 <= self.sc_current[id] <= 45:
self.sc_gear.moveAngle(id, self.sc_current[id])
moved = True
if self.move and not moved:
self.move_step = (self.move_step + 1) % 4
self.sc_angles = self.forward[self.move_step]['angles'].copy()
self.sc_time = self.forward[self.move_step]['time'].copy()
time.sleep(time_delta)
print('[servo_service] stopping')
signal('diag').send(self, name='servo_service', state='stopping')
def exit(self, args=None):
print('[servo_service] exiting...')
self.running = False
def handle_servo_command(self, args=None):
print(args)
if args.update_frequency is not None:
self.update_frequency = args.update_frequency
if args.angle is not None:
if args.id >= 0:
self.sc_angles[args.id] = args.angle
self.sc_time[args.id] = args.time
else:
for id in range(len(self.sc_angles)):
self.sc_angles[id] = args.angle
self.sc_time[id] = args.time
if args.forward is not None:
self.sc_angles = self.forward[args.forward]['angles'].copy()
self.sc_time = self.forward[args.forward]['time'].copy()
if args.move is not None:
self.move = args.move
self.move_step = 0
self.sc_angles = self.forward[self.move_step]['angles'].copy()
self.sc_time = self.forward[self.move_step]['time'].copy()
if __name__ == '__main__':
servo_service = ServoService()