Connection Examples
Slave connection through CANopen Example
import argparse
from ingenialink.canopen.network import CanBaudrate, CanDevice, CanopenNetwork
def connection_example(args: argparse.Namespace) -> None:
"""Canopen connection example.
It scans for nodes in a network, connects to the first found node,
reads a register and disconnects the found servo from the network.
Args:
args: Command line arguments.
"""
can_device = CanDevice(args.transceiver)
can_baudrate = CanBaudrate(args.baudrate)
net = CanopenNetwork(device=can_device, channel=args.channel, baudrate=can_baudrate)
nodes = net.scan_slaves()
print(nodes)
if len(nodes) > 0:
servo = net.connect_to_slave(target=args.node_id, dictionary=args.dictionary_path)
fw_version = servo.read("DRV_ID_SOFTWARE_VERSION")
print(fw_version)
net.disconnect_from_slave(servo)
else:
print("Could not find any nodes")
def setup_command() -> argparse.Namespace:
"""Sets up the command line argument parser.
Returns:
Parsed command line arguments.
"""
parser = argparse.ArgumentParser(description="Canopen example")
parser.add_argument("-d", "--dictionary_path", help="Path to drive dictionary", required=True)
parser.add_argument("-n", "--node_id", default=32, type=int, help="Node ID")
parser.add_argument(
"-t",
"--transceiver",
default="ixxat",
choices=["pcan", "kvaser", "ixxat"],
help="CAN transceiver",
)
parser.add_argument(
"-b",
"--baudrate",
default=1000000,
type=int,
choices=[50000, 100000, 125000, 250000, 500000, 1000000],
help="CAN baudrate",
)
parser.add_argument("-c", "--channel", default=0, type=int, help="CAN transceiver channel")
return parser.parse_args()
if __name__ == "__main__":
args = setup_command()
connection_example(args)
Slave connection through CANopen Example (Linux)
In Linux, the CANopen network should be configured in a terminal before running the ingenialink script (administrator privileges are needed).
sudo ip link set can0 up type can bitrate 1000000
sudo ip link set can0 txqueuelen 1000
Then to establish the connection use always the SOCKETCAN device and the bitrate configured in previous step:
import argparse
from ingenialink.canopen.network import CanBaudrate, CanDevice, CanopenNetwork
def connection_example(args: argparse.Namespace) -> None:
"""Canopen connection example.
It scans for nodes in a network, connects to the first found node,
reads a register and disconnects the found servo from the network.
Args:
args: Command line arguments.
"""
can_baudrate = CanBaudrate(args.baudrate)
net = CanopenNetwork(device=CanDevice.SOCKETCAN, channel=args.channel, baudrate=can_baudrate)
nodes = net.scan_slaves()
print(nodes)
if len(nodes) > 0:
servo = net.connect_to_slave(target=args.node_id, dictionary=args.dictionary_path)
fw_version = servo.read("DRV_ID_SOFTWARE_VERSION")
print(fw_version)
net.disconnect_from_slave(servo)
else:
print("Could not find any nodes")
def setup_command() -> argparse.Namespace:
"""Sets up the command line argument parser.
Returns:
Parsed command line arguments.
"""
parser = argparse.ArgumentParser(description="Canopen example")
parser.add_argument("-d", "--dictionary_path", help="Path to drive dictionary", required=True)
parser.add_argument("-n", "--node_id", default=32, type=int, help="Node ID")
parser.add_argument(
"-b",
"--baudrate",
default=1000000,
type=int,
choices=[50000, 100000, 125000, 250000, 500000, 1000000],
help="CAN baudrate",
)
parser.add_argument("-c", "--channel", default=0, type=int, help="CAN transceiver channel")
return parser.parse_args()
if __name__ == "__main__":
args = setup_command()
connection_example(args)
Slave connection through Ethernet Example
import argparse
from ingenialink.ethernet.network import EthernetNetwork
def connection_example(args: argparse.Namespace) -> None:
"""Demonstrates how to connect to a slave device over Ethernet.
Args:
args: Parsed command-line arguments.
"""
net = EthernetNetwork()
servo = net.connect_to_slave(args.ip_address, args.dictionary_path, args.port)
print(servo.read("DRV_ID_SOFTWARE_VERSION"))
net.disconnect_from_slave(servo)
def setup_command() -> argparse.Namespace:
"""Parse input arguments.
Returns:
Parsed arguments.
"""
parser = argparse.ArgumentParser(description="Ethernet connection example")
parser.add_argument("-d", "--dictionary_path", help="Path to drive dictionary", required=True)
parser.add_argument("-ip", "--ip_address", help="IP address", type=str, required=True)
parser.add_argument("-p", "--port", help="TCP port", type=int, default=1061)
return parser.parse_args()
if __name__ == "__main__":
args = setup_command()
connection_example(args)
Slave connection through EtherCAT Example
import argparse
from ingenialink.ethercat.network import EthercatNetwork
def main(args: argparse.Namespace) -> None:
"""Connect to an EtherCAT slave and read its firmware version.
Args:
args: Command line arguments.
"""
net = EthercatNetwork(args.interface)
servo = net.connect_to_slave(args.slave_id, args.dictionary_path)
firmware_version = servo.read("DRV_ID_SOFTWARE_VERSION")
print(firmware_version)
net.disconnect_from_slave(servo)
def setup_command() -> argparse.Namespace:
"""Setup command line argument parser.
Returns:
Parsed arguments.
"""
parser = argparse.ArgumentParser(description="EtherCAT connection example script.")
interface_help = """Network adapter interface name. To find it: \n
- On Windows, \\Device\\NPF_{id}. To get the id, run the command: wmic nic get name, guid \n
- On linux, run the command: ip link show
"""
parser.add_argument("-i", "--interface", type=str, help=interface_help, required=True)
parser.add_argument(
"-d", "--dictionary_path", type=str, help="Path to the drive's dictionary.", required=True
)
parser.add_argument("-s", "--slave_id", type=int, help="Slave ID.", default=1)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = setup_command()
main(args)
In Linux, administrator privileges are needed to establish the EtherCAT connection.
sudo python3 examples/ethercat/ecat_connection.py