forked from bggardner/canopen-rpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
canopen-master.py
executable file
·378 lines (362 loc) · 19.3 KB
/
canopen-master.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
378
#!/usr/bin/python3
import CAN
import CANopen
from functools import reduce
from operator import xor
import RPi.GPIO as GPIO
import signal
from time import sleep
from sys import exit
DEFAULT_CAN_INTERFACE = "vcan0"
REDUNDANT_CAN_INTERFACE = "vcan1"
PIN_ENABLE_N = 16
PIN_ADDRESS_N = [12, 13, 14, 15, 17, 18, 19]
PIN_ADDRESS_PARITY_N = 20
PIN_RUNLED0 = 41
PIN_ERRLED0 = 40
PIN_RUNLED1 = 39
PIN_ERRLED1 = 38
def sigterm_handler(signum, frame):
GPIO.cleanup()
exit()
signal.signal(signal.SIGTERM, sigterm_handler)
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN_ENABLE_N, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(PIN_ADDRESS_N, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(PIN_ADDRESS_PARITY_N, GPIO.IN, pull_up_down=GPIO.PUD_UP)
runled0 = CANopen.RunIndicator(PIN_RUNLED0)
errled0 = CANopen.ErrorIndicator(PIN_ERRLED0)
runled1 = CANopen.Indicator(PIN_RUNLED1, CANopen.Indicator.OFF)
errled1 = CANopen.Indicator(PIN_ERRLED1, CANopen.Indicator.ON)
default_bus = CAN.Bus(DEFAULT_CAN_INTERFACE)
redundant_bus = CAN.Bus(REDUNDANT_CAN_INTERFACE)
active_bus = default_bus
class ResetNode(Exception):
pass
class ResetCommunication(Exception):
pass
while True:
try:
if GPIO.input(PIN_ENABLE_N) == GPIO.HIGH:
print("Enable_n is high")
sleep(1)
raise ResetNode
while True:
try:
address_n = [
GPIO.input(PIN_ADDRESS_N[6]),
GPIO.input(PIN_ADDRESS_N[5]),
GPIO.input(PIN_ADDRESS_N[4]),
GPIO.input(PIN_ADDRESS_N[3]),
GPIO.input(PIN_ADDRESS_N[2]),
GPIO.input(PIN_ADDRESS_N[1]),
GPIO.input(PIN_ADDRESS_N[0])]
address_parity_n = reduce(xor, address_n)
if address_parity_n != GPIO.input(PIN_ADDRESS_PARITY_N):
print("Address parity mismatch")
sleep(1)
raise ResetCommunication
node_id = 0
for bit in address_n:
node_id = (node_id << 1) | (not bit)
if node_id == CANopen.BROADCAST_NODE_ID:
print("Invalid Node ID")
sleep(1)
raise ResetCommunication
print("Booting with Node ID: {:d}".format(node_id))
canopen_od = CANopen.ObjectDictionary({
CANopen.ODI_DEVICE_TYPE: CANopen.Object(
parameter_name="Device type",
object_type=CANopen.ObjectType.VAR,
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED32,
default_value=0x00000000
),
CANopen.ODI_ERROR: CANopen.Object(
parameter_name="Error register",
object_type=CANopen.ObjectType.VAR,
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED8,
default_value=0x00
),
CANopen.ODI_SYNC: CANopen.Object(
parameter_name="COB-ID SYNC",
object_type=CANopen.ObjectType.VAR,
access_type=CANopen.AccessType.CONST,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED32,
default_value=0x00000000 + (CANopen.FUNCTION_CODE_SYNC << CANopen.FUNCTION_CODE_BITNUM), # 0x40000000 + ... if SYNC producer
),
CANopen.ODI_SYNC_TIME: CANopen.Object(
parameter_name="Communication cycle period",
object_type=CANopen.ObjectType.VAR,
access_type=CANopen.AccessType.RW,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED32,
default_value=1000000 # 1 second, 32-bit, in us
),
CANopen.ODI_EMCY_ID: CANopen.Object(
parameter_name="COB-ID emergency message",
object_type=CANopen.ObjectType.VAR,
access_type=CANopen.AccessType.CONST,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED32,
default_value=(CANopen.FUNCTION_CODE_EMCY << CANopen.FUNCTION_CODE_BITNUM) + node_id
),
CANopen.ODI_HEARTBEAT_CONSUMER_TIME: CANopen.Object(
parameter_name="Consumer Heartbeat Time",
object_type=CANopen.ObjectType.ARRAY,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED32,
sub_number=1,
subs={
CANopen.ODSI_VALUE: CANopen.SubObject(
parameter_name="Number of Entries",
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED8,
low_limit=1,
high_limit=127,
default_value=1,
),
CANopen.ODSI_HEARTBEAT_CONSUMER_TIME: CANopen.SubObject(
parameter_name="Consumer Heartbeat Time",
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED32,
low_limit=0x00000000,
high_limit=0xFFFFFFFF,
default_value=(1 << 16) + 2000 # Node-ID 1, 16-bit, in ms
),
}
),
CANopen.ODI_HEARTBEAT_PRODUCER_TIME: CANopen.Object(
parameter_name="Producer heartbeat time",
object_type=CANopen.ObjectType.VAR,
access_type=CANopen.AccessType.RW,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED16,
default_value=1000 # 16-bit, in ms
),
CANopen.ODI_IDENTITY: CANopen.Object(
parameter_name="Identity Object",
object_type=CANopen.ObjectType.RECORD,
data_type=CANopen.ODI_DATA_TYPE_IDENTITY,
sub_number=4,
subs={
CANopen.ODSI_VALUE: CANopen.SubObject(
parameter_name="number of entries",
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED8,
low_limit=1,
high_limit=4,
default_value=4
),
CANopen.ODSI_IDENTITY_VENDOR: CANopen.SubObject(
parameter_name="Vendor ID",
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED32,
low_limit=0x00000000,
high_limit=0xFFFFFFFF,
default_value=0x00000000
),
CANopen.ODSI_IDENTITY_PRODUCT: CANopen.SubObject(
parameter_name="Product code",
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED32,
low_limit=0x00000000,
high_limit=0xFFFFFFFF,
default_value=0x00000001
),
CANopen.ODSI_IDENTITY_REVISION: CANopen.SubObject(
parameter_name="Revision number",
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED32,
low_limit=0x00000000,
high_limit=0xFFFFFFFF,
default_value=0x00000000
),
CANopen.ODSI_IDENTITY_SERIAL: CANopen.SubObject(
parameter_name="Serial number",
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED32,
low_limit=0x00000000,
high_limit=0xFFFFFFFF,
default_value=0x00000001
)
}
),
CANopen.ODI_SDO_SERVER: CANopen.Object(
parameter_name="Server SDO parameter",
object_type=CANopen.ObjectType.RECORD,
data_type=CANopen.ODI_DATA_TYPE_SDO_PARAMETER,
sub_number=2,
subs={
CANopen.ODSI_VALUE: CANopen.SubObject(
parameter_name="number of entries",
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED8,
low_limit=2,
high_limit=2,
default_value=2
),
CANopen.ODSI_SDO_SERVER_DEFAULT_CSID: CANopen.SubObject(
parameter_name="COB-ID Client->Server (rx)",
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED32,
low_limit=0x00000000,
high_limit=0xFFFFFFFF,
default_value=(CANopen.FUNCTION_CODE_SDO_RX << CANopen.FUNCTION_CODE_BITNUM) + node_id
),
CANopen.ODSI_SDO_SERVER_DEFAULT_SCID: CANopen.SubObject(
parameter_name="COB-ID Server->Client (tx)",
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED32,
low_limit=0x00000000,
high_limit=0xFFFFFFFF,
default_value=(CANopen.FUNCTION_CODE_SDO_TX << CANopen.FUNCTION_CODE_BITNUM) + node_id
),
}
), #TODO: add Client SDO parameter Object(s)
CANopen.ODI_TPDO1_COMMUNICATION_PARAMETER: CANopen.Object(
parameter_name="transmit PDO parameter",
object_type=CANopen.ObjectType.RECORD,
data_type=CANopen.ODI_DATA_TYPE_PDO_COMMUNICATION_PARAMETER,
sub_number=2,
subs={
CANopen.ODSI_VALUE: CANopen.SubObject(
parameter_name="largest sub-index supported",
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED8,
low_limt=2,
high_limit=6,
default_value=2
),
CANopen.ODSI_TPDO_COMM_PARAM_ID: CANopen.SubObject(
parameter_name="COB-ID used by PDO",
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED32,
low_limit=0x00000000,
high_limit=0xFFFFFFFF,
default_value=node_id
),
CANopen.ODSI_TPDO_COMM_PARAM_TYPE: CANopen.SubObject(
parameter_name="transmission type",
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED8,
low_limit=0x00,
high_limit=0xFF,
default_value=1 # synchronous
)
}
),
CANopen.ODI_TPDO1_MAPPING_PARAMETER: CANopen.Object(
parameter_name="transmit PDO mapping",
object_type=CANopen.ObjectType.RECORD,
data_type=CANopen.ODI_DATA_TYPE_PDO_MAPPING_PARAMETER,
sub_number=2,
subs={
CANopen.ODSI_VALUE: CANopen.SubObject(
parameter_name="number of mapped application objects in PDO",
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED8,
low_limit=0x00,
high_limit=0x40,
default_value=2
),
0x01: CANopen.SubObject(
parameter_name="PDO mapping for the 1st application object to be mapped",
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED32,
low_limit=0x00000000,
high_limit=0xFFFFFFFF,
default_value=(CANopen.ODI_SYNC << 16) + (CANopen.ODSI_VALUE << 8) + 32
),
0x02: CANopen.SubObject(
parameter_name="PDO mapping for the 2nd application object to be mapped",
access_type=CANopen.AccessType.RO,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED32,
low_limit=0x00000000,
high_limit=0xFFFFFFFF,
default_value=(CANopen.ODI_SYNC_TIME << 16) + (CANopen.ODSI_VALUE << 8) + 32
),
}
),
CANopen.ODI_NMT_STARTUP: CANopen.Object(
parameter_name="NMT Startup",
object_type=CANopen.ObjectType.VAR,
access_type=CANopen.AccessType.CONST,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED32,
default_value=0x00000023 # Flying master, NMT master start, NMT master
),
CANopen.ODI_NMT_FLYING_MASTER_TIMING_PARAMETERS: CANopen.Object(
parameter_name="NMT flying master timing parameters",
object_type=CANopen.ObjectType.ARRAY,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED16,
sub_number=6,
subs={
CANopen.ODSI_VALUE: CANopen.SubObject(
parameter_name="Highest sub-index supported",
access_type=CANopen.AccessType.CONST,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED8,
low_limit=0x06,
high_limit=0x06,
default_value=0x06
),
CANopen.ODSI_NMT_FLYING_MASTER_TIMING_PARAMS_TIMEOUT: CANopen.SubObject(
parameter_name="NMT master timeout",
access_type=CANopen.AccessType.CONST,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED16,
low_limit=0x0000,
high_limit=0xFFFF,
default_value=100
),
CANopen.ODSI_NMT_FLYING_MASTER_TIMING_PARAMS_DELAY: CANopen.SubObject(
parameter_name="NMT master negotiation time delay",
access_type=CANopen.AccessType.CONST,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED16,
low_limit=0x0000,
high_limit=0xFFFF,
default_value=500
),
CANopen.ODSI_NMT_FLYING_MASTER_TIMING_PARAMS_PRIORITY: CANopen.SubObject(
parameter_name="NMT master priority",
access_type=CANopen.AccessType.CONST,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED16,
low_limit=0x0000,
high_limit=0xFFFF,
default_value=(node_id - 1) & 0x3
),
CANopen.ODSI_NMT_FLYING_MASTER_TIMING_PARAMS_PRIORITY_TIME_SLOT: CANopen.SubObject(
parameter_name="Priority time slot",
access_type=CANopen.AccessType.CONST,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED16,
low_limit=0x0000,
high_limit=0xFFFF,
default_value=1500
),
CANopen.ODSI_NMT_FLYING_MASTER_TIMING_PARAMS_DEVICE_TIME_SLOT: CANopen.SubObject(
parameter_name="CANopen device time slot",
access_type=CANopen.AccessType.CONST,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED16,
low_limit=0x0000,
high_limit=0xFFFF,
default_value=10
),
CANopen.ODSI_NMT_FLYING_MASTER_TIMING_PARAMS_DETECT_TIME: CANopen.SubObject(
parameter_name="Multiple NMT master detect cycle time",
access_type=CANopen.AccessType.CONST,
data_type=CANopen.ODI_DATA_TYPE_UNSIGNED16,
low_limit=0x0000,
high_limit=0xFFFF,
default_value=4000 + 10 * node_id
),
}
),
})
with CANopen.Node(active_bus, node_id, canopen_od, run_indicator=runled0, err_indicator=errled0) as node:
while True:
pass # Replace with application code and interact with Object Dictionary (node.od)
except ResetCommunication:
try:
node.reset_communication()
except NameError:
pass
except ResetNode:
try:
node.reset()
except NameError:
pass