This repository was archived by the owner on May 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathzlg_can.py
More file actions
439 lines (359 loc) · 16.7 KB
/
Copy pathzlg_can.py
File metadata and controls
439 lines (359 loc) · 16.7 KB
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import asyncio
import sys
import platform
from typing import Optional
from can_utils import (
logger, libstark, setup_shutdown_event,
print_finger_touch_data
)
# Import the corresponding ZLG CAN driver module according to the operating system
if platform.system() == "Windows":
from zlg_win import *
elif platform.system() == "Linux":
from zlg_linux import *
else:
raise NotImplementedError(f"Unsupported operating system: {platform.system()}")
# Note: The following functions are imported from the zlg_win or zlg_linux module through the above conditions:
# - zlgcan_open()
# - zlgcan_close()
# - zlgcan_send_message(can_id, data)
# - zlgcan_receive_message(quick_retries, dely_retries)
class Revo2CanController:
"""Revo2 CAN communication controller"""
def __init__(self, master_id: int = 1, slave_id: int = 2):
self.master_id = master_id
self.slave_id = slave_id
self.client = None # Will be initialized in initialize()
self.device_info = None # Initialize device info
async def initialize(self):
"""Initialize CAN connection"""
try:
# Initialize ZLG CAN device
zlgcan_open() # type: ignore
# Set callback functions
libstark.set_can_tx_callback(self._can_send)
libstark.set_can_rx_callback(self._can_read)
# Initialize device handler for CAN protocol
self.client = libstark.init_device_handler(libstark.StarkProtocolType.Can, self.master_id)
logger.info(
f"CAN connection initialized successfully - Master ID: {self.master_id}, Slave ID: {self.slave_id}"
)
return True
except Exception as e:
logger.error(f"CAN connection initialization failed: {e}")
return False
def _can_send(self, _slave_id: int, can_id: int, data: list) -> bool:
"""CAN message sending"""
try:
if not zlgcan_send_message(can_id, bytes(data)): # type: ignore
logger.error("Failed to send CAN message")
return False
return True
except Exception as e:
logger.error(f"CAN sending exception: {e}")
return False
def _can_read(self, _slave_id: int, expected_can_id: int, expected_frames: int) -> tuple:
"""
CAN message receiving with multi-frame protocol support
SDK tells us how many frames to expect via expected_frames parameter.
We need to collect all frames and return concatenated data.
Supports multi-frame protocols:
- MultiRead (0x0B): Detects is_last flag in byte[1] bit 7
- TouchSensorRead (0x0D): Detects total/seq in byte[0] (total:4bit|seq:4bit)
Args:
_slave_id: Slave ID (not used)
expected_can_id: Expected CAN ID (used for filtering)
expected_frames: Expected frame count (0 = single frame, >0 = multi-frame)
Returns:
tuple: (can_id, data)
"""
try:
result = zlgcan_receive_filtered(expected_can_id, expected_frames) # type: ignore
if result is None:
return 0, bytes([])
can_id, data, frame_count = result
return can_id, bytes(data)
except Exception as e:
logger.error(f"CAN message receiving failed: {e}")
return 0, bytes([])
async def get_device_info(self):
"""Get device information"""
try:
device_info = await self.client.get_device_info(self.slave_id)
logger.info(f"Device information: {device_info.description}")
self.device_info = device_info # Store device info
return device_info
except Exception as e:
logger.error(f"Failed to get device information: {e}")
return None
def uses_revo1_touch_api(self) -> bool:
"""Check if device uses Revo1 Touch API"""
if self.device_info is None:
return False
return self.device_info.uses_revo1_touch_api()
async def change_slave_id(self, new_slave_id: int):
"""Change device slave ID (use with caution, device will reboot)"""
try:
await self.client.set_slave_id(self.slave_id, new_slave_id)
logger.info(f"Slave ID has been changed to {new_slave_id}, device will reboot...")
return True
except Exception as e:
logger.error(f"Failed to change slave ID: {e}")
return False
async def configure_device(self):
"""Configure device parameters"""
try:
# Option: change slave ID (if needed, uncomment and set new ID)
# WARNING: device will reboot, program will exit
# if await self.change_slave_id(new_slave_id=2):
# sys.exit(0)
# Disable automatic calibration and perform manual calibration
# await self.client.set_auto_calibration(self.slave_id, False) # Disable automatic calibration after power-on
# await self.client.calibrate_position(self.slave_id)
# await self.client.set_auto_calibration(self.slave_id, True) # Enable automatic calibration after power-on
# auto_calibration_enabled = await self.client.get_auto_calibration_enabled(self.slave_id)
# logger.info(f"Automatic calibration after power-on: {auto_calibration_enabled}")
# Configure: Turbo mode (optional, uncomment to enable)
# await self.configure_turbo_mode()
return True
except Exception as e:
logger.error(f"Failed to configure device: {e}")
return False
async def configure_turbo_mode(self):
"""Configure Turbo mode"""
try:
# Enable Turbo mode
await self.client.set_turbo_mode_enabled(self.slave_id, True)
# Set Turbo parameters
turbo_interval = 200 # Grip interval time (milliseconds)
turbo_duration = 300 # Grip duration time (milliseconds)
turbo_conf = libstark.TurboConfig(turbo_interval, turbo_duration)
await self.client.set_turbo_config(self.slave_id, turbo_conf)
# Verify configuration
turbo_mode_enabled = await self.client.get_turbo_mode_enabled(self.slave_id)
turbo_config = await self.client.get_turbo_config(self.slave_id)
logger.info(f"Turbo mode: {turbo_mode_enabled}")
logger.info(
f"Turbo configuration - interval: {turbo_config.interval}ms, duration: {turbo_config.duration}ms"
)
except Exception as e:
logger.error(f"Failed to configure Turbo mode: {e}")
async def finger_position_examples(self):
"""Finger position control example"""
logger.info("=== Finger position control example ===")
# Example 1: Sequential finger grip
logger.info("Example 1: Sequential finger grip")
positions = [
[200, 0, 0, 0, 0, 0], # Thumb
[200, 300, 0, 0, 0, 0], # Thumb + Index
[200, 300, 500, 0, 0, 0], # + Middle
[200, 300, 500, 700, 0, 0], # + Ring
[200, 300, 500, 700, 800, 0], # + Pinky
[200, 300, 500, 700, 800, 900], # + Wrist
]
for i, pos in enumerate(positions):
logger.info(f"Step {i+1}: {pos}")
await self.client.set_finger_positions(self.slave_id, pos)
await asyncio.sleep(0.8)
await asyncio.sleep(1.0)
# Example 2: Preset gestures sequence
logger.info("Example 2: Preset gestures")
gestures = {
"Open hand": [0, 0, 0, 0, 0, 0],
"Point": [0, 300, 0, 0, 0, 0],
"Victory gesture": [0, 300, 800, 0, 0, 0],
"OK gesture": [500, 300, 800, 0, 0, 0],
"Grip": [500, 300, 1000, 1000, 1000, 1000],
}
for gesture_name, positions in gestures.items():
logger.info(f"Execute gesture: {gesture_name} - {positions}")
await self.client.set_finger_positions(self.slave_id, positions)
await asyncio.sleep(1.5)
# Example 3: Grab action simulation
logger.info("Example 3: Grab action simulation")
grab_sequence = [
([0, 0, 0, 0, 0, 0], "Initial position"),
([300, 200, 600, 800, 800, 800], "Grab preparation"),
([500, 400, 900, 1000, 1000, 1000], "Grab completion"),
]
for positions, description in grab_sequence:
logger.info(f"{description}: {positions}")
await self.client.set_finger_positions(self.slave_id, positions)
await asyncio.sleep(1.0)
async def finger_speed_examples(self):
"""Finger speed control example"""
logger.info("=== Finger speed control example ===")
# Stop all movement
await self.client.set_finger_speeds(self.slave_id, [0] * 6)
logger.info("All fingers stopped")
async def single_finger_control_example(self, finger_id: libstark.FingerId):
"""Single finger control example"""
logger.info(f"=== Single finger control example - {finger_id} ===")
# Position control
logger.info("Position control test")
await self.client.set_finger_position(self.slave_id, finger_id, 1000) # Maximum position
await asyncio.sleep(1.0)
await self.client.set_finger_position(self.slave_id, finger_id, 0) # Initial position
await asyncio.sleep(1.0)
# Speed control
logger.info("Speed control test")
await self.client.set_finger_speed(
self.slave_id, finger_id, 1000
) # Maximum forward speed
await asyncio.sleep(1.0)
await self.client.set_finger_speed(
self.slave_id, finger_id, -1000
) # Maximum reverse speed
await asyncio.sleep(1.0)
await self.client.set_finger_speed(self.slave_id, finger_id, 0) # Stop
async def get_motor_status(self) -> libstark.MotorStatusData:
"""Get motor status"""
try:
status = await self.client.get_motor_status(self.slave_id)
# Optional: enable detailed status logging
# logger.info(f"Motor status: {status.description}")
return status
except Exception as e:
logger.error(f"Failed to get motor status: {e}")
return None # type: ignore
async def monitor_motor_status(self, interval: float = 0.001):
"""Continuous monitoring of motor status"""
logger.info(f"Start monitoring motor status of device {self.slave_id:02x}")
while True:
try:
status = await self.get_motor_status()
logger.debug(f"Motor status: {status.description}")
await asyncio.sleep(interval)
except asyncio.CancelledError:
logger.info("Motor status monitoring cancelled")
break
except Exception as e:
logger.error(f"Motor status monitoring exception: {e}")
await asyncio.sleep(1)
async def test_touch_sensor_control(self):
"""Test touch sensor control (Revo1AdvancedTouch)"""
logger.info("=== Test touch sensor control ===")
try:
# 1. Enable touch sensors for all fingers
logger.info("1. Enable touch sensors for all fingers...")
await self.client.touch_sensor_setup(self.slave_id, 0xFF)
await asyncio.sleep(0.5)
# # 2. Calibrate touch sensors
# logger.info("2. Calibrate touch sensors for all fingers...")
# await self.client.touch_sensor_calibrate(self.slave_id, 0xFF)
# await asyncio.sleep(2.0)
# # 3. Reset touch sensors
# logger.info("3. Reset touch sensors for all fingers...")
# await self.client.touch_sensor_reset(self.slave_id, 0xFF)
# await asyncio.sleep(0.5)
logger.info("Touch sensor control test completed\n")
except Exception as e:
logger.error(f"Touch sensor control test failed: {e}")
async def test_touch_sensor_reading(self):
"""Test touch sensor data reading"""
logger.info("\n=== Test touch sensor data reading ===")
try:
touch_data = await self.client.get_touch_sensor_status(self.slave_id)
logger.info(f"Successfully read touch data for {len(touch_data)} fingers")
finger_names = ["Thumb", "Index", "Middle", "Ring", "Pinky"]
uses_revo1 = self.uses_revo1_touch_api()
for i, finger in enumerate(touch_data):
if i < len(finger_names):
if uses_revo1:
print_finger_touch_data_revo1(finger, i, finger_names[i])
else:
print_finger_touch_data_revo2(finger, finger_names[i])
logger.info("\nTouch sensor data reading test completed\n")
except Exception as e:
logger.error(f"Touch sensor data reading failed: {e}")
async def monitor_touch_sensor(self, iterations: int = 1000, interval: float = 0.5):
"""Periodic touch sensor data monitoring"""
logger.info(f"\n=== Periodic touch sensor monitoring ({iterations} iterations) ===")
finger_names = ["Thumb", "Index", "Middle", "Ring", "Pinky"]
uses_revo1 = self.uses_revo1_touch_api()
for i in range(iterations):
try:
touch_data = await self.client.get_touch_sensor_status(self.slave_id)
logger.info(f"\n[{i + 1}] Touch data snapshot:")
for idx, finger in enumerate(touch_data):
if idx < len(finger_names):
if uses_revo1:
print_finger_touch_data_revo1(finger, idx, finger_names[idx])
else:
print_finger_touch_data_revo2(finger, finger_names[idx])
await asyncio.sleep(interval)
except Exception as e:
logger.error(f"[{i + 1}] Reading failed: {e}")
logger.info("\nPeriodic monitoring completed\n")
async def demo_task(self):
"""Demo task"""
# Get device information first
await self.get_device_info()
# Configure device parameters, enable as needed
# await self.configure_device()
# Check if device uses Revo1 Touch API
if self.uses_revo1_touch_api():
logger.info("Detected Revo1 Touch device, running touch sensor demos...")
# await self.test_touch_sensor_control()
await self.test_touch_sensor_reading()
await self.monitor_touch_sensor(iterations=1000, interval=0.5)
else:
# Enable the following demos for other devices
await self.finger_position_examples()
await asyncio.sleep(1.0)
# await self.finger_speed_examples()
# await asyncio.sleep(1.0)
# await self.single_finger_control_example(libstark.FingerId.Pinky)
def cleanup(self):
"""Clean up resources"""
try:
zlgcan_close() # type: ignore
logger.info("CAN connection closed")
except Exception as e:
logger.error(f"Error cleaning up resources: {e}")
async def close(self):
"""Async close method for compatibility"""
self.cleanup()
async def main():
"""Main function"""
# Default left hand is 1, right hand is 2
controller = Revo2CanController(master_id=1, slave_id=2)
try:
# Initialize connection
if not await controller.initialize():
logger.error("Initialization failed, program exiting")
return
# Set shutdown event listener
shutdown_event = setup_shutdown_event(logger)
# Create tasks
tasks = []
# Start demo task
demo_task = asyncio.create_task(controller.demo_task())
tasks.append(demo_task)
# Optional: start motor status monitoring (disabled to avoid CAN bus conflicts)
# monitor_task = asyncio.create_task(controller.monitor_motor_status())
# tasks.append(monitor_task)
# Wait for shutdown signal
await shutdown_event.wait()
logger.info("Received shutdown signal, stopping all tasks...")
# Cancel all tasks
for task in tasks:
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
except Exception as e:
logger.error(f"Program execution exception: {e}")
finally:
controller.cleanup()
sys.exit(0)
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
logger.info("User interrupted")
sys.exit(0)
except Exception as e:
logger.error(f"Error: {e}", exc_info=True)
sys.exit(1)