|
1 | | -#!/usr/bin/env python3 |
2 | | -""" |
3 | | -Debug script for Damiao motor CAN communication on macOS. |
4 | | -This replaces candump for macOS SLCAN debugging. |
5 | | -""" |
6 | | - |
7 | | -import can |
8 | | -import time |
9 | | -import sys |
10 | | - |
11 | | -def test_can_communication(port="/dev/cu.usbmodem2101"): |
12 | | - """Test basic CAN communication with a Damiao motor.""" |
13 | | - |
14 | | - print("=" * 60) |
15 | | - print("Damiao Motor CAN Communication Debug Tool") |
16 | | - print("=" * 60) |
17 | | - print(f"\nPort: {port}") |
18 | | - print() |
19 | | - |
20 | | - try: |
21 | | - # Connect to SLCAN |
22 | | - print("Step 1: Connecting to SLCAN...") |
23 | | - bus = can.interface.Bus( |
24 | | - channel=port, |
25 | | - interface='slcan', |
26 | | - bitrate=1000000 |
27 | | - ) |
28 | | - print("✓ Connected to SLCAN") |
29 | | - |
30 | | - # Test 1: Send enable command and listen for ANY response |
31 | | - print("\n" + "=" * 60) |
32 | | - print("Test 1: Enable Motor (ID 0x01)") |
33 | | - print("=" * 60) |
34 | | - print("Sending enable command to 0x01...") |
35 | | - |
36 | | - enable_msg = can.Message( |
37 | | - arbitration_id=0x01, |
38 | | - data=[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC], |
39 | | - is_extended_id=False |
40 | | - ) |
41 | | - bus.send(enable_msg) |
42 | | - print("✓ Enable command sent") |
43 | | - |
44 | | - print("\nListening for responses (2 seconds)...") |
45 | | - print("Expected: Response from 0x11 (master ID)") |
46 | | - print() |
47 | | - |
48 | | - responses = [] |
49 | | - start_time = time.time() |
50 | | - timeout = 2.0 |
51 | | - |
52 | | - while time.time() - start_time < timeout: |
53 | | - msg = bus.recv(timeout=0.1) |
54 | | - if msg: |
55 | | - responses.append(msg) |
56 | | - print(f" → Response from 0x{msg.arbitration_id:02X}: {msg.data.hex()}") |
57 | | - |
58 | | - if not responses: |
59 | | - print("✗ NO RESPONSES RECEIVED") |
60 | | - print("\nPossible issues:") |
61 | | - print(" 1. Motor not powered (check 24V supply)") |
62 | | - print(" 2. CAN wiring incorrect (CANH, CANL, GND)") |
63 | | - print(" 3. Motor master ID not set to 0x11") |
64 | | - print(" 4. SLCAN adapter not working properly") |
65 | | - print(" 5. Wrong CAN port specified") |
66 | | - else: |
67 | | - print(f"\n✓ Received {len(responses)} response(s)") |
68 | | - |
69 | | - # Check if we got response from expected ID |
70 | | - recv_ids = [msg.arbitration_id for msg in responses] |
71 | | - if 0x11 in recv_ids: |
72 | | - print("✓ Motor 0x11 is responding!") |
73 | | - else: |
74 | | - print(f"⚠ Responses from unexpected IDs: {[hex(id) for id in recv_ids]}") |
75 | | - |
76 | | - # Test 2: Send refresh command |
77 | | - print("\n" + "=" * 60) |
78 | | - print("Test 2: Refresh Motor State (ID 0x01)") |
79 | | - print("=" * 60) |
80 | | - print("Sending refresh command...") |
81 | | - |
82 | | - refresh_msg = can.Message( |
83 | | - arbitration_id=0x7FF, # Parameter ID |
84 | | - data=[0x01, 0x00, 0xCC, 0x00, 0x00, 0x00, 0x00, 0x00], |
85 | | - is_extended_id=False |
86 | | - ) |
87 | | - bus.send(refresh_msg) |
88 | | - print("✓ Refresh command sent") |
89 | | - |
90 | | - print("\nListening for responses (2 seconds)...") |
91 | | - responses = [] |
92 | | - start_time = time.time() |
93 | | - |
94 | | - while time.time() - start_time < timeout: |
95 | | - msg = bus.recv(timeout=0.1) |
96 | | - if msg: |
97 | | - responses.append(msg) |
98 | | - print(f" → Response from 0x{msg.arbitration_id:02X}: {msg.data.hex()}") |
99 | | - |
100 | | - if not responses: |
101 | | - print("✗ NO RESPONSES RECEIVED") |
102 | | - else: |
103 | | - print(f"\n✓ Received {len(responses)} response(s)") |
104 | | - |
105 | | - # Test 3: Listen for any spontaneous traffic |
106 | | - print("\n" + "=" * 60) |
107 | | - print("Test 3: Listen for Any CAN Traffic") |
108 | | - print("=" * 60) |
109 | | - print("Listening for 5 seconds...") |
110 | | - print("(This will catch any background CAN traffic)") |
111 | | - print() |
112 | | - |
113 | | - start_time = time.time() |
114 | | - traffic_count = 0 |
115 | | - |
116 | | - while time.time() - start_time < 5.0: |
117 | | - msg = bus.recv(timeout=0.1) |
118 | | - if msg: |
119 | | - traffic_count += 1 |
120 | | - print(f" [{time.time() - start_time:.2f}s] ID=0x{msg.arbitration_id:03X}: {msg.data.hex()}") |
121 | | - |
122 | | - if traffic_count == 0: |
123 | | - print("✗ No CAN traffic detected at all") |
124 | | - print("\nThis suggests:") |
125 | | - print(" - SLCAN adapter may not be working") |
126 | | - print(" - No devices on the CAN bus are active") |
127 | | - print(" - Wrong port specified") |
128 | | - else: |
129 | | - print(f"\n✓ Detected {traffic_count} CAN messages") |
130 | | - |
131 | | - # Cleanup |
132 | | - print("\n" + "=" * 60) |
133 | | - print("Cleanup") |
134 | | - print("=" * 60) |
135 | | - print("Sending disable command...") |
136 | | - disable_msg = can.Message( |
137 | | - arbitration_id=0x01, |
138 | | - data=[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFD], |
139 | | - is_extended_id=False |
140 | | - ) |
141 | | - bus.send(disable_msg) |
142 | | - time.sleep(0.1) |
143 | | - |
144 | | - bus.shutdown() |
145 | | - print("✓ Disconnected from CAN bus") |
146 | | - |
147 | | - except Exception as e: |
148 | | - print(f"\n✗ Error: {e}") |
149 | | - import traceback |
150 | | - traceback.print_exc() |
151 | | - return False |
152 | | - |
153 | | - print("\n" + "=" * 60) |
154 | | - print("Debug Complete") |
155 | | - print("=" * 60) |
156 | | - return True |
157 | | - |
158 | | - |
159 | | -if __name__ == "__main__": |
160 | | - if len(sys.argv) > 1: |
161 | | - port = sys.argv[1] |
162 | | - else: |
163 | | - port = "/dev/cu.usbmodem2101" |
164 | | - |
165 | | - test_can_communication(port) |
166 | | - |
0 commit comments