def addmasternode(network): # node = network.add_node(7, '/home/pi/canopen-rpi/examples/NXP_CiA401.eds') local_node = canopen.LocalNode( 1, '/home/pi/canopen-rpi/examples/NXP_CiA401.eds') network.add_node(local_node) # node.nmt.__init__(7) # local_node.nmt.__init__(7,1) # node.nmt.state = 'OPERATIONAL'
def discover_connected_devices(self): self.can_network.scanner.reset() self.can_network.scanner.search() time.sleep(0.05) script_dir = os.path.dirname(__file__) # <-- absolute dir the script is in rel_path_master = r"master_dictionary.eds" #self dictionary rel_path_remote = r"object_dictionary.eds" abs_file_path_master_eds = os.path.join(script_dir, rel_path_master) abs_file_path_remote_eds = os.path.join(script_dir, rel_path_remote) local_node_id = int(self.settings.get("CANOpen", "NodeID")) localnode = canopen.LocalNode(local_node_id, abs_file_path_master_eds) self.can_network.add_node(localnode) for node_id in self.can_network.scanner.nodes: self.can_network.add_node(node_id, abs_file_path_remote_eds) return self.can_network.scanner.nodes
import canopen import os import can import time os.system('sudo ip link set can0 type can bitrate 1000000') os.system('sudo ifconfig can0 up') os.system('sudo ifconfig can0 txqueuelen 1000') network = canopen.Network() network.connect(channel='can0', bustype='socketcan') network.scanner.search() time.sleep(0.05) for node_id in network.scanner.nodes: print(node_id) node = network.add_node(12, 'test.eds') local_node = canopen.LocalNode(1, 'test.eds') network.add_node(local_node) for node_id in network: print(network[node_id]) #node.sdo.download(0x60FF, 00, b'\x84\x00') #device_type_data = node.sdo.upload(0x1007, 0) #print(device_type_data) #network.send_message(0x60C, [0x23, 0xFF, 0x60, 0x00, 0x42, 0x05, 0x00, 0x00]) #set velocity network.send_message( 0x60C, [0x40, 0x41, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00]) #Get Status #network.send_message(0x0, [0x1, 0]) #network.sync.start(0.01) #node.tpdo.read() #node.rpdo.read()
import time import struct ''' An example of communicating with an Arduino via a canbus network using the CanOpen protocol. This example uses a Joy-It SBC-CAN01 module connected via SPI as a CAN interface using the GPIO pins of a Raspberry Pi. See https://joy-it.net/en/products/SBC-CAN01 for connection and setup instructions. The Arduino must be running Canfestivino with .EDS and .CPP files made in ObjDictEdit.py #TODO add URL to arduino sketch/instructions ''' network = canopen.Network() master_node = canopen.LocalNode(0x01, 'master.eds') slave_node = canopen.RemoteNode(0x05,'slave_node_example.eds') #.eds and object dictionary are interchangable network.add_node(master_node) network.add_node(slave_node) ''' You may need to enable the CAN interface (this must be done after rebooting the OS) Run the following commands in the terminal $ sudo ip lin set can0 up type can bitrate 500000 $ sudo ifconfig can0 txqueuelen 10000 You can view the raw CAN messages by running $ candump can0 ''' network.connect(bustype='socketcan', channel='can0', bitrate=500000) def network_scan(network):