Communication Examples

Set and Get Registers Example

import argparse

from ingeniamotion import MotionController


def main(args):
    mc = MotionController()
    mc.communication.connect_servo_eoe(args.ip, args.dictionary_path)
    mc.communication.set_register("DRV_PROT_USER_OVER_VOLT", 60)
    volt = mc.communication.get_register("DRV_PROT_VBUS_VALUE")
    print("Current voltage is", volt)
    mc.communication.disconnect()


def setup_command():
    parser = argparse.ArgumentParser(description='Disturbance example')
    parser.add_argument('--dictionary_path', help='Path to drive dictionary', required=True)
    parser.add_argument('--ip', help='Drive IP address', required=True)
    return parser.parse_args()


if __name__ == '__main__':
    args = setup_command()
    main(args)

Connect CANopen drive

import argparse

from ingeniamotion import MotionController
from ingeniamotion.enums import CAN_BAUDRATE, CAN_DEVICE


def main(args):
    # Create MotionController instance
    mc = MotionController()

    # Get list of all node id available
    can_device = CAN_DEVICE(args.can_transceiver)
    can_baudrate = CAN_BAUDRATE(args.can_baudrate)
    can_channel = args.can_channel
    node_id_list = mc.communication.scan_servos_canopen(
        can_device, can_baudrate, can_channel)

    if len(node_id_list) > 0:
        # Connect to servo with CANOpen
        mc.communication.connect_servo_canopen(
            can_device,
            args.dictionary_path,
            args.node_id,
            can_baudrate,
            can_channel
        )
        print("Servo connected!")
        # Disconnect servo, this lines is mandatory
        mc.communication.disconnect()
    else:
        print("No node id available")


def setup_command():
    parser = argparse.ArgumentParser(description='Canopen example')
    parser.add_argument('--dictionary_path', help='Path to drive dictionary', required=True)
    parser.add_argument('--node_id', default=32, type=int,
                        help='Node ID')
    parser.add_argument('--can_transceiver', default='ixxat',
                        choices=['pcan', 'kvaser', 'ixxat'],
                        help='CAN transceiver')
    parser.add_argument('--can_baudrate', default=1000000, type=int,
                        choices=[50000, 100000, 125000, 250000,
                                 500000, 1000000],
                        help='CAN baudrate')
    parser.add_argument('--can_channel', default=0, type=int,
                        help='CAN transceiver channel')
    return parser.parse_args()


if __name__ == '__main__':
    args = setup_command()
    main(args)

Connect EtherCAT - CoE drive

from ingeniamotion import MotionController


def connect_ethercat_coe(interface_index: int, slave_id: int, dictionary_path: str) -> None:
    mc = MotionController()

    interface_list = mc.communication.get_interface_name_list()
    print("List of interfaces:")
    for index, interface in enumerate(interface_list):
        print(f"{index}: {interface}")

    interface_selected = mc.communication.get_ifname_by_index(interface_index)
    print("Interface selected:")
    print(f"- Index interface: {interface_index}")
    print(f"- Interface identifier: {interface_selected}")
    print(f"- Interface name: {interface_list[interface_index]}")

    slave_id_list = mc.communication.scan_servos_ethercat(interface_selected)

    if not slave_id_list:
        print(f"No slave detected on interface: {interface_list[interface_index]}")
        return
    else:
        print(f"Found slaves: {slave_id_list}")

    mc.communication.connect_servo_ethercat(interface_selected, slave_id, dictionary_path)
    print("Drive is connected.")

    mc.communication.disconnect()
    print("The drive has been disconnected.")


if __name__ == "__main__":
    # Replace the ecat_coe_conf parameters
    interface_index = 3
    slave_id = 1
    dictionary_path = "parent_directory\\dictionary_file.xdf"
    connect_ethercat_coe(interface_index, slave_id, dictionary_path)

Load Firmware ECAT

import argparse

import ingeniamotion


def main(args):
    # Create MotionController instance
    mc = ingeniamotion.MotionController()

    # Print infame list to get ifname index
    print(mc.communication.get_interface_name_list())

    # Load firmware
    mc.communication.load_firmware_ecat_interface_index(
        args.interface_index,  # ifname index
        args.firmware_file,  # FW file
        args.slave_id)  # Slave index


def setup_command():
    parser = argparse.ArgumentParser(description='Disturbance example')
    parser.add_argument('--interface_index', help='Network adapter inteface index', required=True)
    parser.add_argument('--slave_id', help='Drive slave ID', required=True)
    parser.add_argument('--firmware_file', help='Firmware file to be loaded', required=True)
    return parser.parse_args()


if __name__ == '__main__':
    args = setup_command()
    main(args)

Load Firmware FTP

import argparse

import ingeniamotion


def main(args):
    # Create MotionController instance
    mc = ingeniamotion.MotionController()

    # Connect drive
    mc.communication.connect_servo_ethernet(args.ip, args.dictionary_path)

    # Load firmware
    mc.communication.boot_mode_and_load_firmware_ethernet(args.firmware_file)


def setup_command():
    parser = argparse.ArgumentParser(description='Disturbance example')
    parser.add_argument('--dictionary_path', help='Path to drive dictionary', required=True)
    parser.add_argument('--ip', help='Drive IP address', required=True)
    parser.add_argument('--firmware_file', help='Firmware file to be loaded', required=True)
    return parser.parse_args()


if __name__ == '__main__':
    args = setup_command()
    main(args)

Load Firmware CAN

from ingenialink import CAN_BAUDRATE, CAN_DEVICE
from ingenialink.exceptions import ILFirmwareLoadError

from ingeniamotion.motion_controller import MotionController


def status_log(current_status: str) -> None:
    print(f"Load firmware status: {current_status}")


def progress_log(current_progress: int) -> None:
    print(f"Load firmware progress: {current_progress}%")


def load_firmware_canopen(
    device: CAN_DEVICE,
    channel: int,
    baudrate: CAN_BAUDRATE,
    node_id: int,
    dictionary_path: str,
    fw_path: str,
) -> None:
    mc = MotionController()
    # Find available nodes
    node_id_list = mc.communication.scan_servos_canopen(device, baudrate, channel)
    if node_id not in node_id_list:
        print(f"Node {node_id} is not detected.")
        return
    else:
        print(f"Found nodes: {node_id_list}")

    print("Starts to establish a communication.")
    mc.communication.connect_servo_canopen(
        device,
        dictionary_path,
        node_id,
        baudrate,
        channel,
    )
    print("Drive is connected.")

    print("Starts to load the firmware.")
    try:
        mc.communication.load_firmware_canopen(
            fw_path,
            status_callback=status_log,
            progress_callback=progress_log,
        )
        print("Firmware is uploaded successfully.")
    except ILFirmwareLoadError as e:
        print(f"Firmware loading failed: {e}")
    mc.communication.disconnect()
    print("Drive is disconnected.")


if __name__ == "__main__":
    # Remember to replace all parameters here
    device = CAN_DEVICE.KVASER
    channel = 0
    baudrate = CAN_BAUDRATE.Baudrate_1M
    node_id = 32
    dictionary_path = "parent_directory/full_dictionary_path.xdf"
    fw_path = "parent_directory/full_firmware_file_path.lfu"
    load_firmware_canopen(device, channel, baudrate, node_id, dictionary_path, fw_path)

Change Node ID

from typing import Optional

from ingenialink import CAN_BAUDRATE, CAN_DEVICE

from ingeniamotion.motion_controller import MotionController


def change_node_id(
    device: CAN_DEVICE,
    channel: int,
    node_id: int,
    baudrate: CAN_BAUDRATE,
    dictionary_path: str,
    new_node_id: int,
) -> None:
    mc = MotionController()
    print("Connect to the drive.")
    mc.communication.connect_servo_canopen(
        device,
        dictionary_path,
        node_id,
        baudrate=baudrate,
        channel=channel,
    )
    print(f"Drive is connected with {node_id} as a node ID.")

    print("Starts to change the node ID.")
    old_node_id = mc.info.get_node_id()
    if old_node_id == new_node_id:
        print(f"This drive already has this node ID: {old_node_id}.")
        mc.communication.disconnect()
        return

    mc.configuration.change_node_id(new_node_id)
    print("Node ID has been changed")

    mc.communication.disconnect()
    print("Drive is disconnected.")

    print("Re-connect to the drive.")
    mc.communication.connect_servo_canopen(
        device,
        dictionary_path,
        new_node_id,
        baudrate=baudrate,
        channel=channel,
    )
    print(f"Now the drive is connected with {new_node_id} as a node ID.")

    mc.communication.disconnect()
    print("Drive is disconnected.")


if __name__ == "__main__":
    # Remember to replace all parameters here
    # If you want to connect to a node manually, set the node_id parameter as an integer.
    # Instead, set the node_id parameter as a NoneType value to connect the first detected CAN node.

    device = CAN_DEVICE.KVASER
    channel = 0
    node_id = 20
    new_node_id = 32
    baudrate = CAN_BAUDRATE.Baudrate_1M
    dictionary_path = "parent_directory/dictionary_file.xdf"

    change_node_id(device, channel, node_id, baudrate, dictionary_path, new_node_id)

Change Baudrate

from typing import Optional

from ingenialink import CAN_BAUDRATE, CAN_DEVICE

from ingeniamotion.motion_controller import MotionController


def change_baudrate(
    device: CAN_DEVICE,
    channel: int,
    node_id: int,
    baudrate: CAN_BAUDRATE,
    dictionary_path: str,
    new_baudrate: CAN_BAUDRATE,
) -> None:
    mc = MotionController()
    mc.communication.connect_servo_canopen(device, dictionary_path, node_id, baudrate, channel)
    print(f"Drive is connected with {baudrate} baudrate.")
    print("Starts to change the baudrate.")
    old_baudrate = mc.info.get_baudrate()
    if old_baudrate == new_baudrate:
        print(f"This drive already has this baudrate: {old_baudrate}.")
        mc.communication.disconnect()
        return
    mc.configuration.change_baudrate(new_baudrate)
    print(f"Baudrate has been changed from {old_baudrate} to {new_baudrate}.")

    mc.communication.disconnect()
    print("Drive is disconnected.")
    print(
        f"Perform a power cycle and reconnect to the drive using the new baud rate: {new_baudrate}"
    )


if __name__ == "__main__":
    # Remember to replace all parameters here
    device = CAN_DEVICE.KVASER
    channel = 0
    node_id = 20
    baudrate = CAN_BAUDRATE.Baudrate_1M
    dictionary_path = "parent_directory/dictionary_file.xdf"
    new_baudrate = CAN_BAUDRATE.Baudrate_250K

    change_baudrate(device, channel, node_id, baudrate, dictionary_path, new_baudrate)