import canopen network = canopen.Network() network.connect(channel='/dev/ttyACM0', bustype='slcan', bitrate=250000) node = canopen.RemoteNode(9, 'lmu-dictionary.eds') network.add_node(node) network.send_message(0x701, [0x00]) network.send_message(0x709, [0x00]) network.send_message(0x711, [0x00]) network.send_message(0x00, [0x01, 0x01]) network.send_message(0x00, [0x01, 0x09]) network.send_message(0x00, [0x01, 0x11]) network.send_message(0x201, [0x00, 0x03, 0x01, 0xA0, 0x00]) network.send_message(0x209, [0x00, 0x03, 0x01, 0xA0, 0x00]) network.send_message(0x211, [0x00, 0x03, 0x01, 0xA0, 0x00]) pack_voltage = node.sdo[0x4100][1].raw print(pack_voltage / 100, 'V') pack_current = node.sdo[0x4100][2].raw print(pack_current / 100, ' A') signed_pack_temperature = node.sdo[0x4100][4].raw pack_temperature = (100 / 255) * (signed_pack_temperature + 128) - 25 print(round(pack_temperature, 2), '°C') network.sync.start(1) network.sync.stop()
import canopen network = canopen.Network() network.connect(channel='can0', bustype='socketcan', bitrate=250000) node = canopen.RemoteNode(1, '/path/to/file/lmu-dictionary.eds') network.add_node(node) pack_voltage = node.sdo[0x4100][1].raw print(pack_voltage / 100) pack_current = node.sdo[0x4100][2].raw print(pack_current / 100) signed_pack_temperature = node.sdo[0x4100][4].raw pack_temperature = (100 / 255) * (signed_pack_temperature + 128) - 25 print(pack_temperature) network.disconnect()
def CheckNodeSDO(self, nodeid): """Hit some node (7-bit Node ID) with a quick Vendor Identity SDO.""" node = canopen.RemoteNode(6) vendor_id = node.sdo[OD_IDENTITY][OD_IDENTITY_VENDORID].raw
#!/usr/bin/env python3 """System info script""" from argparse import ArgumentParser import canopen EDS_FILE = "../src/boards/generic/object_dictionary/generic.eds" SYSTEM_INFO_INDEX = 0x3001 parser = ArgumentParser(description="System info") parser.add_argument("bus", help="CAN bus to use") parser.add_argument("node", help="device node name in hex") args = parser.parse_args() network = canopen.Network() node = canopen.RemoteNode(int(args.node, 16), EDS_FILE) network.add_node(node) network.connect(bustype="socketcan", channel=args.bus) print("") os_name = node.sdo[SYSTEM_INFO_INDEX][1].phys os_distro = node.sdo[SYSTEM_INFO_INDEX][2].phys kernel_ver = node.sdo[SYSTEM_INFO_INDEX][3].phys print("OS Name: " + os_name) print("OS Distro: " + os_distro) print("Kernel Version: " + kernel_ver) print("") hostname = node.sdo[SYSTEM_INFO_INDEX][4].phys
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(1, 'master.eds') slave_node = canopen.RemoteNode(5,'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): # This will attempt to read an SDO from nodes 1 - 127
#!/usr/bin/env python # -*- coding: utf-8 -*- from common import * import canopen node_id = 42 nw = create_network() lss_waiting_state(nw) lss_configuration_state(nw) lss_set_node_id(nw, node_id) lss_waiting_state(nw) node = canopen.RemoteNode(node_id, 'Bootloader.eds') node.associate_network(nw) bootloader_start_app(node)
def canopen_test_pdo(user_os): """ canopen testing : read and write the data using PDO tested device : beckhoff remote I/O LC5100 """ print("test PDO") # Start with creating a network representing one CAN bus net = canopen.Network() # Connect to the CAN bus # Arguments are passed to python-can's can.interface.Bus() constructor # (see https://python-can.readthedocs.io/en/latest/bus.html). # connection is depended on your OS if user_os == "linux": net.connect(bustype='socketcan', channel='can0') print("connect via \'socketcan\' to can0") else: net.connect(bustype='usb2can', channel='69E696BD', bitrate=125000) print("connect via \'usb2can\' to channel 69E696BD, bitrate=125000") # Add some nodes with corresponding Object Dictionaries node_lc5100 = canopen.RemoteNode(node_id=1, object_dictionary='LC5100.eds') net.add_node(node_lc5100) # Set to pre-operational mode node_lc5100.nmt.send_command(0x80) time.sleep(5) # Config PDO of node device print("Config PDO device") node_lc5100.rpdo.read() node_lc5100.tpdo.read() print("tpdo map (before):\t{}".format(node_lc5100.tpdo[1].map)) print("tpdo enabled (before):\t{}".format(node_lc5100.tpdo[1].enabled)) #node_lc5100.rpdo[1].add_variable(0x6200, 1, 8) print("tpdo map (after):\t{}".format(node_lc5100.tpdo[1].map)) print("tpdo enabled (after):\t{}".format(node_lc5100.tpdo[1].enabled)) node_lc5100.tpdo[1].event_timer = 3000 node_lc5100.tpdo[1].enabled = True node_lc5100.tpdo.save() node_lc5100.rpdo.save() # Set to operational mode (Run mode) node_lc5100.nmt.send_command(0x01) time.sleep(3) # Test sending PDO to set output of Remote I/O LC5100 print("Test sending PDO to set output of Remote I/O LC5100") try: write_data = 0 while True: write_data += 1 if write_data > 0xFF: write_data = 0 print("Write output value = {}".format(write_data)) node_lc5100.rpdo[1][0x6200].raw = write_data #node_lc5100.rpdo[1].raw = write_data node_lc5100.rpdo[1].transmit() time.sleep(0.5) except KeyboardInterrupt: print("Exit from sending PDO to LC5100") # Test reading PDO to read input of Remote I/O LC5100 print("Test reading PDO to read input of Remote I/O LC5100") try: while True: timestamp = node_lc5100.tpdo[1].wait_for_reception() read_value = node_lc5100.tpdo[1][0x6000].raw print("Read input value = {}, t={}".format(read_value, timestamp)) except KeyboardInterrupt: print("Exit from reading PDO to LC5100")
def canopen_test_heartbeat(user_os): """ canopen testing : heartbeat tested device : beckhoff remote I/O LC5100 """ print("test Heartbeat...") # Start with creating a network representing one CAN bus net = canopen.Network() # Connect to the CAN bus # Arguments are passed to python-can's can.interface.Bus() constructor # (see https://python-can.readthedocs.io/en/latest/bus.html). # connection is depended on your OS if user_os == "linux": net.connect(bustype='socketcan', channel='can0') print("connect via \'socketcan\' to can0") else: net.connect(bustype='usb2can', channel='69E696BD', bitrate=125000) print("connect via \'usb2can\' to channel 69E696BD, bitrate=125000") # Check network net.check() # Add some nodes with corresponding Object Dictionaries node_lc5100 = canopen.RemoteNode(node_id=1, object_dictionary='LC5100.eds') net.add_node(node_lc5100) # Set from pre-op to operational mode (Run mode) node_lc5100.nmt.send_command(0x80) time.sleep(1) node_lc5100.nmt.send_command(0x01) time.sleep(1) # Read a previous heartbeat time before set heartbeat_time_consumer = node_lc5100.sdo[0x1016][1] heartbeat_time_producer = node_lc5100.sdo[0x1017] print("heartbeat time (consumer) before set: {}".format( heartbeat_time_consumer.raw)) print("heartbeat time (producer) after set : {}".format( heartbeat_time_producer.raw)) # Set a new heartbeat time set_time = input("enter a new heartbeat time (ms) : ") heartbeat_time_producer.raw = int(set_time) # Wait for heartbeat : Polling method global t1 global t2 print("Wait for heartbeat (Polling method)") try: while True: t1 = time.time() node_lc5100.nmt.wait_for_heartbeat() t2 = time.time() print("hello Heartbeat! wait duration = {}".format(t2 - t1)) except KeyboardInterrupt: print("Exit from heartbeat test") # Wait for heartbeat : Interrupt method print("Wait for heartbeat (Interrupt method)") node_lc5100.nmt.add_hearbeat_callback(heartbeat_callback) t1_callback = time.time() try: while True: pass except KeyboardInterrupt: print("Exit from heartbeat test")
def __init__(self, name, role, channel, node_idx, **kwargs): """ channel (str): channel name of can bus node_idx (int): node index of focus tracker """ model.HwComponent.__init__(self, name, role, **kwargs) # Connect to the CANbus and the CANopen network. self.network = canopen.Network() bustype = 'socketcan' if channel != 'fake' else 'virtual' try: self.network.connect(bustype=bustype, channel=channel) except IOError as exp: if exp.errno == 19: raise HwError("Focus Tracker is not connected.") else: raise self.network.check() object_dict = pkg_resources.resource_filename("odemis.driver", "FocusTracker.eds") if channel == 'fake': self.node = FakeRemoteNode(node_idx, object_dict) else: self.node = canopen.RemoteNode(node_idx, object_dict) self.network.add_node(self.node) self._swVersion = "python-canopen v%s , python-can v%s" % ( canopen.__version__, can.__version__) rev_num = self.node.sdo["Identity Object"]["Revision number"].raw minor = rev_num & 0xffff major = (rev_num >> 16) & 0xffff self._hwVersion = "{}.{}".format(major, minor) # Create SDO communication objects to communicate self._position_sdo = self.node.sdo["AI Input PV"][1] self._target_pos_sdo = self.node.sdo["CO Set Point W"][1] # Read PID gains from the device (and set the current metadata) self._proportional_gain_sdo = self.node.sdo[ 'CO Proportional Band Xp1'][1] self._integral_gain_sdo = self.node.sdo['CO Integral Action Time Tn1'][ 1] self._derivative_gain_sdo = self.node.sdo[ 'CO Derivative Action Time Tv1'][1] self._tracking_sdo = self.node.sdo['Controller On/ Off'][1] # set VAs only if there is hardware, channel = '' if no hardware self._metadata[model.MD_GAIN_P] = self._proportional_gain_sdo.raw self._metadata[model.MD_GAIN_I] = self._integral_gain_sdo.raw self._metadata[model.MD_GAIN_I] = self._derivative_gain_sdo.raw self.targetPosition = model.FloatContinuous( self._get_target_pos(), TARGET_POSITION_RANGE, unit="m", getter=self._get_target_pos, setter=self._set_target_pos) self.position = model.FloatVA(self._get_position(), unit="m", readonly=True, getter=self._get_position) self.tracking = model.BooleanVA(self._get_tracking(), getter=self._get_tracking, setter=self._set_tracking)