-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
378 lines (298 loc) · 10.3 KB
/
main.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
from bottle import Bottle, request, response
import openlch
import subprocess
from bottle_cors_plugin import cors_plugin
app = Bottle()
cors = cors_plugin(origins='*')
app.install(cors)
DEFAULT_IP = "192.168.1.1"
@app.post('/start_calibration')
def start_calibration():
try:
servo_id = int(request.query.get('servo_id'))
speed = int(request.query.get('speed', 300))
current = float(request.query.get('current', 600.0))
ip = request.query.get('ip', DEFAULT_IP)
print(f"Received: servo_id={servo_id}, speed={speed}, current={current}, ip={ip}")
hal = openlch.HAL(ip)
success = hal.servo.start_calibration(
servo_id, calibration_speed=speed,
current_threshold=current
)
hal.close()
if success:
return {
"status": "success",
"message": f"Calibration started for servo {servo_id}",
"details": {
"speed": speed,
"current_threshold": current
}
}
else:
response.status = 400
return {
"status": "failure",
"message": f"Failed to start calibration for servo {servo_id}"
}
except Exception as e:
response.status = 500
return {"status": "error", "message": str(e)}
@app.get('/ping')
def ping():
"""
Ping endpoint to check server status.
"""
ip = request.query.get('ip', DEFAULT_IP)
try:
result = subprocess.run(["ping", "-c", "1", ip], capture_output=True, text=True, check=False)
if result.returncode == 0:
response.status = 200
return {
"status": "success",
"message": f"Successfully pinged {ip}",
"output": result.stdout
}
else:
response.status = 400
return {
"status": "failure",
"message": f"Failed to ping {ip}",
"output": result.stderr
}
except Exception as e:
response.status = 500
return {
"status": "error",
"message": f"An error occurred while pinging {ip}: {str(e)}"
}
@app.get('/get_positions')
def get_positions():
"""Get current positions and speeds of all servos."""
ip = request.query.get('ip', DEFAULT_IP)
try:
hal = openlch.HAL(ip)
positions = hal.servo.get_positions()
result = []
for servo_id, position, speed in positions:
result.append({
"servo_id": servo_id,
"position": round(position, 2),
"speed": round(speed, 2)
})
hal.close()
return {
"status": "success",
"message": "Current positions and speeds retrieved",
"data": result
}
except Exception as e:
response.status = 500
return {
"status": "error",
"message": f"An error occurred while retrieving positions: {str(e)}"
}
@app.get('/get_servo_info')
def get_servo_info():
"""Get information about a specific servo."""
try:
print(request.query.get('servo_id'))
servo_id = int(request.query.get('servo_id'))
ip = request.query.get('ip', DEFAULT_IP)
print(f"Received: id={servo_id}, ip={ip}")
hal = openlch.HAL(ip)
info = hal.servo.get_servo_info(servo_id)
hal.close()
return {
"status": "success",
"servo_id": servo_id,
"info": info
}
except Exception as e:
response.status = 500
return {
"status": "error",
"message": f"An error occurred while fetching the servo information: {str(e)}"
}
@app.get('/scan_servos')
def scan_servos():
"""Scan for connected servos."""
try:
ip = request.query.get('ip', DEFAULT_IP)
print(f"Scanning for servos at IP: {ip}")
hal = openlch.HAL(ip)
servo_ids = hal.servo.scan()
hal.close()
return {
"status": "success",
"servo_ids": servo_ids
}
except Exception as e:
response.status = 500
return {
"status": "error",
"message": f"An error occurred while scanning for servos: {str(e)}"
}
@app.post('/change_servo_id')
def change_servo_id():
"""Change the ID of a servo."""
try:
old_id = int(request.query.get('old_id'))
new_id = int(request.query.get('new_id'))
ip = request.query.get('ip', DEFAULT_IP)
print(f"Changing servo ID from {old_id} to {new_id} at IP: {ip}")
hal = openlch.HAL(ip)
success = hal.servo.change_id(old_id, new_id)
hal.close()
if success:
return {
"status": "success",
"message": f"Successfully changed servo ID from {old_id} to {new_id}"
}
else:
response.status = 400
return {
"status": "failure",
"message": "Failed to change servo ID"
}
except Exception as e:
response.status = 500
return {
"status": "error",
"message": f"An error occurred: {str(e)}"
}
@app.post('/cancel_calibration')
def cancel_calibration():
"""Cancel ongoing calibration for a specific servo."""
try:
servo_id = int(request.query.get('servo_id'))
ip = request.query.get('ip', DEFAULT_IP)
print(f"Cancelling calibration for servo {servo_id} at IP: {ip}")
hal = openlch.HAL(ip)
success = hal.servo.cancel_calibration(servo_id)
hal.close()
if success:
return {
"status": "success",
"message": f"Calibration cancelled for servo {servo_id}"
}
else:
response.status = 400
return {
"status": "failure",
"message": f"Failed to cancel calibration for servo {servo_id}"
}
except Exception as e:
response.status = 500
return {
"status": "error",
"message": f"An error occurred: {str(e)}"
}
@app.get('/get_calibration_status')
def get_calibration_status():
"""Get the current calibration status."""
try:
ip = request.query.get('ip', DEFAULT_IP)
hal = openlch.HAL(ip)
status = hal.servo.get_calibration_status()
hal.close()
if status['is_calibrating']:
return {
"status": "success",
"message": f"Calibration in progress for servo {status['calibrating_servo_id']}"
}
else:
return {
"status": "success",
"message": "No calibration in progress"
}
except Exception as e:
response.status = 500
return {
"status": "error",
"message": f"An error occurred: {str(e)}"
}
@app.post('/set_torque')
def set_torque():
"""Set torque for multiple servos."""
try:
settings = request.json.get('settings')
ip = request.json.get('ip', DEFAULT_IP)
if not settings:
response.status = 400
return {"status": "failure", "message": "No settings provided"}
hal = openlch.HAL(ip)
hal.servo.set_torque(settings)
hal.close()
return {
"status": "success",
"message": "Torque settings applied successfully",
"settings": [{"servo_id": servo_id, "torque": torque} for servo_id, torque in settings]
}
except Exception as e:
response.status = 500
return {"status": "error", "message": f"An error occurred: {str(e)}"}
@app.post('/set_torque_enable')
def set_torque_enable():
"""Enable or disable torque for multiple servos."""
try:
settings = request.json.get('settings')
ip = request.json.get('ip', DEFAULT_IP)
if not settings:
response.status = 400
return {"status": "failure", "message": "No settings provided"}
bool_settings = [(id, status.lower() == 'true') for id, status in settings]
hal = openlch.HAL(ip)
hal.servo.set_torque_enable(bool_settings)
hal.close()
return {
"status": "success",
"message": "Torque enable settings applied successfully",
"settings": [{"servo_id": servo_id, "status": "enabled" if status else "disabled"} for servo_id, status in bool_settings]
}
except Exception as e:
response.status = 500
return {"status": "error", "message": f"An error occurred: {str(e)}"}
@app.get('/get_imu_data')
def get_imu_data():
"""Get current IMU sensor data (gyroscope and accelerometer readings)."""
try:
ip = request.query.get('ip', DEFAULT_IP)
hal = openlch.HAL(ip)
imu_data = hal.imu.get_data()
hal.close()
return {
"status": "success",
"message": "IMU Sensor Data",
"gyro": imu_data['gyro'],
"accel": imu_data['accel']
}
except Exception as e:
response.status = 500
return {"status": "error", "message": f"An error occurred: {str(e)}"}
@app.post('/enable_movement')
def enable_movement():
"""Enable movement for all servos."""
try:
ip = request.json.get('ip', DEFAULT_IP)
hal = openlch.HAL(ip)
hal.servo.enable_movement()
hal.close()
return {"status": "success", "message": "Movement enabled for all servos"}
except Exception as e:
response.status = 500
return {"status": "error", "message": f"An error occurred: {str(e)}"}
@app.post('/disable_movement')
def disable_movement():
"""Disable movement for all servos."""
try:
ip = request.json.get('ip', DEFAULT_IP)
hal = openlch.HAL(ip)
hal.servo.disable_movement()
hal.close()
return {"status": "success", "message": "Movement disabled for all servos"}
except Exception as e:
response.status = 500
return {"status": "error", "message": f"An error occurred: {str(e)}"}
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)