Skip to content

Commit 78961c7

Browse files
committed
修复已知BUG
1 parent 2a4f6a3 commit 78961c7

31 files changed

+343
-162
lines changed
0 Bytes
Binary file not shown.
76 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
435 Bytes
Binary file not shown.
406 Bytes
Binary file not shown.

LinkerHand/core/can/linker_hand_l10_can.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import threading
66
import numpy as np
77
from enum import Enum
8+
from LinkerHand.utils.open_can import OpenCan
89

910
class FrameProperty(Enum):
1011
INVALID_FRAME_PROPERTY = 0x00
@@ -25,7 +26,12 @@ class FrameProperty(Enum):
2526
MOTOR_TEMPERATURE_2 = 0x34
2627

2728
class LinkerHandL10Can:
28-
def __init__(self,can_id, can_channel='can0', baudrate=1000000, ):
29+
def __init__(self,can_id, can_channel='can0', baudrate=1000000, yaml=""):
30+
self.can_id = can_id
31+
self.can_channel = can_channel
32+
self.baudrate = baudrate
33+
self.open_can = OpenCan(load_yaml=yaml)
34+
2935
self.x01 = [-1] * 5
3036
self.x02 = [-1] * 5
3137
self.x03 = [-1] * 5
@@ -70,12 +76,15 @@ def __init__(self,can_id, can_channel='can0', baudrate=1000000, ):
7076
self.receive_thread.start()
7177

7278
def init_can_bus(self, channel, baudrate):
73-
if sys.platform == "linux":
74-
return can.interface.Bus(channel=channel, interface="socketcan", bitrate=baudrate)
75-
elif sys.platform == "win32":
76-
return can.interface.Bus(channel=channel, interface='pcan', bitrate=baudrate)
77-
else:
78-
raise EnvironmentError("Unsupported platform for CAN interface")
79+
try:
80+
if sys.platform == "linux":
81+
return can.interface.Bus(channel=channel, interface="socketcan", bitrate=baudrate)
82+
elif sys.platform == "win32":
83+
return can.interface.Bus(channel=channel, interface='pcan', bitrate=baudrate)
84+
else:
85+
raise EnvironmentError("Unsupported platform for CAN interface")
86+
except:
87+
print("Please insert CAN device")
7988

8089
def send_frame(self, frame_property, data_list,sleep=0.003):
8190
"""Send a single CAN frame with specified properties and data."""
@@ -86,6 +95,16 @@ def send_frame(self, frame_property, data_list,sleep=0.003):
8695
self.bus.send(msg)
8796
except can.CanError as e:
8897
print(f"Failed to send message: {e}")
98+
self.open_can.open_can(self.can_channel)
99+
time.sleep(1)
100+
self.is_can = self.open_can.is_can_up_sysfs(interface=self.can_channel)
101+
time.sleep(1)
102+
if self.is_can:
103+
self.bus = can.interface.Bus(channel=self.can_channel, interface="socketcan", bitrate=self.baudrate)
104+
else:
105+
print("Reconnecting CAN devices ....")
106+
# time.sleep(1)
107+
#
89108
time.sleep(sleep)
90109

91110
def set_joint_positions(self, joint_angles):

LinkerHand/core/can/linker_hand_l20_can.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import threading
55
from enum import Enum
66
import numpy as np
7+
from LinkerHand.utils.open_can import OpenCan
78

89
class FrameProperty(Enum):
910
INVALID_FRAME_PROPERTY = 0x00 # Invalid CAN frame property | No return
@@ -30,8 +31,12 @@ class FrameProperty(Enum):
3031

3132

3233
class LinkerHandL20Can:
33-
def __init__(self, can_channel='can0', baudrate=1000000, can_id=0x28):
34+
def __init__(self, can_channel='can0', baudrate=1000000, can_id=0x28,yaml=""):
3435
self.can_id = can_id
36+
self.can_channel = can_channel
37+
self.baudrate = baudrate
38+
self.open_can = OpenCan(load_yaml=yaml)
39+
3540
self.running = True
3641
self.x05, self.x06, self.x07 = [],[],[]
3742
# New pressure sensors
@@ -58,18 +63,21 @@ def __init__(self, can_channel='can0', baudrate=1000000, can_id=0x28):
5863
}
5964

6065
# Initialize CAN bus according to operating system
61-
if sys.platform == "linux":
62-
self.bus = can.interface.Bus(
63-
channel=can_channel, interface="socketcan", bitrate=baudrate,
64-
can_filters=[{"can_id": can_id, "can_mask": 0x7FF}]
65-
)
66-
elif sys.platform == "win32":
67-
self.bus = can.interface.Bus(
68-
channel=can_channel, interface='pcan', bitrate=baudrate,
69-
can_filters=[{"can_id": can_id, "can_mask": 0x7FF}]
70-
)
71-
else:
72-
raise EnvironmentError("Unsupported platform for CAN interface")
66+
try:
67+
if sys.platform == "linux":
68+
self.bus = can.interface.Bus(
69+
channel=can_channel, interface="socketcan", bitrate=baudrate,
70+
can_filters=[{"can_id": can_id, "can_mask": 0x7FF}]
71+
)
72+
elif sys.platform == "win32":
73+
self.bus = can.interface.Bus(
74+
channel=can_channel, interface='pcan', bitrate=baudrate,
75+
can_filters=[{"can_id": can_id, "can_mask": 0x7FF}]
76+
)
77+
else:
78+
raise EnvironmentError("Unsupported platform for CAN interface")
79+
except:
80+
print("Please insert CAN device")
7381

7482
# Initialize data storage
7583
self.x01, self.x02, self.x03, self.x04 = [[0.0] * 5 for _ in range(4)]
@@ -108,6 +116,14 @@ def receive_response(self):
108116
self.process_response(msg)
109117
except can.CanError as e:
110118
print(f"Error receiving message: {e}")
119+
self.open_can.open_can(self.can_channel)
120+
time.sleep(1)
121+
self.is_can = self.open_can.is_can_up_sysfs(interface=self.can_channel)
122+
time.sleep(1)
123+
if self.is_can:
124+
self.bus = can.interface.Bus(channel=self.can_channel, interface="socketcan", bitrate=self.baudrate)
125+
else:
126+
print("Reconnecting CAN devices ....")
111127

112128
def set_finger_base(self, angles):
113129
self.send_command(FrameProperty.JOINT_PITCH_R, angles)

LinkerHand/core/can/linker_hand_l21_can.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import threading
55
import numpy as np
66
from enum import Enum
7+
from LinkerHand.utils.open_can import OpenCan
78
current_dir = os.path.dirname(os.path.abspath(__file__))
89
target_dir = os.path.abspath(os.path.join(current_dir, ".."))
910
sys.path.append(target_dir)
@@ -106,8 +107,12 @@ class FrameProperty(Enum):
106107
FINGER_TEMPERATURE = 0x84 # Finger joint temperatures
107108

108109
class LinkerHandL21Can:
109-
def __init__(self, can_channel='can0', baudrate=1000000, can_id=0x28):
110+
def __init__(self, can_channel='can0', baudrate=1000000, can_id=0x28,yaml=""):
110111
self.can_id = can_id
112+
self.can_channel = can_channel
113+
self.baudrate = baudrate
114+
self.open_can = OpenCan(load_yaml=yaml)
115+
111116
self.running = True
112117
self.last_thumb_pos, self.last_index_pos,self.last_ring_pos,self.last_middle_pos, self.last_little_pos = None,None,None,None,None
113118
self.x01, self.x02, self.x03, self.x04,self.x05,self.x06,self.x07, self.x08,self.x09,self.x0A,self.x0B,self.x0C,self.x0D,self.x0E,self.speed = [],[],[],[],[],[],[],[],[],[],[],[],[],[],[]
@@ -145,18 +150,21 @@ def __init__(self, can_channel='can0', baudrate=1000000, can_id=0x28):
145150
176: 11,
146151
}
147152
# Initialize CAN bus according to operating system
148-
if sys.platform == "linux":
149-
self.bus = can.interface.Bus(
150-
channel=can_channel, interface="socketcan", bitrate=baudrate,
151-
can_filters=[{"can_id": can_id, "can_mask": 0x7FF}]
152-
)
153-
elif sys.platform == "win32":
154-
self.bus = can.interface.Bus(
155-
channel=can_channel, interface='pcan', bitrate=baudrate,
156-
can_filters=[{"can_id": can_id, "can_mask": 0x7FF}]
157-
)
158-
else:
159-
raise EnvironmentError("Unsupported platform for CAN interface")
153+
try:
154+
if sys.platform == "linux":
155+
self.bus = can.interface.Bus(
156+
channel=can_channel, interface="socketcan", bitrate=baudrate,
157+
can_filters=[{"can_id": can_id, "can_mask": 0x7FF}]
158+
)
159+
elif sys.platform == "win32":
160+
self.bus = can.interface.Bus(
161+
channel=can_channel, interface='pcan', bitrate=baudrate,
162+
can_filters=[{"can_id": can_id, "can_mask": 0x7FF}]
163+
)
164+
else:
165+
raise EnvironmentError("Unsupported platform for CAN interface")
166+
except:
167+
print("Please insert CAN device")
160168

161169
# Start receive thread
162170
self.receive_thread = threading.Thread(target=self.receive_response)
@@ -176,6 +184,14 @@ def send_command(self, frame_property, data_list,sleep_time=0.003):
176184
self.bus.send(msg)
177185
except can.CanError as e:
178186
print(f"Failed to send message: {e}")
187+
self.open_can.open_can(self.can_channel)
188+
time.sleep(1)
189+
self.is_can = self.open_can.is_can_up_sysfs(interface=self.can_channel)
190+
time.sleep(1)
191+
if self.is_can:
192+
self.bus = can.interface.Bus(channel=self.can_channel, interface="socketcan", bitrate=self.baudrate)
193+
else:
194+
print("Reconnecting CAN devices ....")
179195
time.sleep(sleep_time)
180196

181197
def receive_response(self):

LinkerHand/core/can/linker_hand_l25_can.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import threading
55
import numpy as np
66
from enum import Enum
7+
from LinkerHand.utils.open_can import OpenCan
78
current_dir = os.path.dirname(os.path.abspath(__file__))
89
target_dir = os.path.abspath(os.path.join(current_dir, ".."))
910
sys.path.append(target_dir)
@@ -109,8 +110,12 @@ class FrameProperty(Enum):
109110
WHOLE_FRAME = 0xF0 # Whole frame transmission | Returns one byte frame property + the entire structure for 485 and network transmission only
110111

111112
class LinkerHandL25Can:
112-
def __init__(self, can_channel='can0', baudrate=1000000, can_id=0x28):
113+
def __init__(self, can_channel='can0', baudrate=1000000, can_id=0x28,yaml=""):
113114
self.can_id = can_id
115+
self.can_channel = can_channel
116+
self.baudrate = baudrate
117+
self.open_can = OpenCan(load_yaml=yaml)
118+
114119
self.running = True
115120
self.last_thumb_pos, self.last_index_pos,self.last_ring_pos,self.last_middle_pos, self.last_little_pos = None,None,None,None,None
116121
self.x01, self.x02, self.x03, self.x04,self.x05,self.x06,self.x07, self.x08,self.x09,self.x0A,self.x0B,self.x0C,self.x0D,self.x0E,self.speed = [],[],[],[],[],[],[],[],[],[],[],[],[],[],[]
@@ -148,18 +153,21 @@ def __init__(self, can_channel='can0', baudrate=1000000, can_id=0x28):
148153
176: 11,
149154
}
150155
# 根据操作系统初始化 CAN 总线
151-
if sys.platform == "linux":
152-
self.bus = can.interface.Bus(
153-
channel=can_channel, interface="socketcan", bitrate=baudrate,
154-
can_filters=[{"can_id": can_id, "can_mask": 0x7FF}]
155-
)
156-
elif sys.platform == "win32":
157-
self.bus = can.interface.Bus(
158-
channel=can_channel, interface='pcan', bitrate=baudrate,
159-
can_filters=[{"can_id": can_id, "can_mask": 0x7FF}]
160-
)
161-
else:
162-
raise EnvironmentError("Unsupported platform for CAN interface")
156+
try:
157+
if sys.platform == "linux":
158+
self.bus = can.interface.Bus(
159+
channel=can_channel, interface="socketcan", bitrate=baudrate,
160+
can_filters=[{"can_id": can_id, "can_mask": 0x7FF}]
161+
)
162+
elif sys.platform == "win32":
163+
self.bus = can.interface.Bus(
164+
channel=can_channel, interface='pcan', bitrate=baudrate,
165+
can_filters=[{"can_id": can_id, "can_mask": 0x7FF}]
166+
)
167+
else:
168+
raise EnvironmentError("Unsupported platform for CAN interface")
169+
except:
170+
print("Please insert CAN device")
163171

164172
# 启动接收线程
165173
self.receive_thread = threading.Thread(target=self.receive_response)
@@ -180,6 +188,14 @@ def send_command(self, frame_property, data_list):
180188
#print(f"Message sent: ID={hex(self.can_id)}, Data={data}")
181189
except can.CanError as e:
182190
print(f"Failed to send message: {e}")
191+
self.open_can.open_can(self.can_channel)
192+
time.sleep(1)
193+
self.is_can = self.open_can.is_can_up_sysfs(interface=self.can_channel)
194+
time.sleep(1)
195+
if self.is_can:
196+
self.bus = can.interface.Bus(channel=self.can_channel, interface="socketcan", bitrate=self.baudrate)
197+
else:
198+
print("Reconnecting CAN devices ....")
183199
time.sleep(0.001)
184200

185201
def receive_response(self):

0 commit comments

Comments
 (0)