Modbus Examples

Motion

Position

from edcon.edrive.com_modbus import ComModbus
from edcon.edrive.motion_handler import MotionHandler
from edcon.utils.logging import Logging

# Enable loglevel info
Logging()

com = ComModbus('192.168.0.1')
with MotionHandler(com) as mot:
    mot.acknowledge_faults()
    mot.enable_powerstage()
    mot.referencing_task()

    mot.position_task(100000, 600000)
    mot.position_task(-50000, 50000)
    mot.position_task(300000, 600000, absolute=True)

Download Example

Position (nonblock)

import time
from edcon.edrive.com_modbus import ComModbus
from edcon.edrive.motion_handler import MotionHandler
from edcon.utils.logging import Logging

# Enable loglevel info
Logging()

com = ComModbus('192.168.0.1')
with MotionHandler(com) as mot:
    mot.acknowledge_faults()
    mot.enable_powerstage()
    mot.referencing_task()

    # Enable continuous update so that new position tasks can be started while still in motion
    mot.configure_continuous_update(True)

    mot.position_task(-100000, 300000, absolute=True, nonblocking=True)
    time.sleep(1.0)
    mot.position_task(100000, 300000, absolute=True, nonblocking=True)
    time.sleep(1.0)
    mot.position_task(-100000, 300000, absolute=True, nonblocking=True)
    time.sleep(1.0)
    mot.position_task(1000000, 300000, absolute=True, nonblocking=True)

    mot.wait_for_target_position()

Download Example

Multi Position (nonblock)

import time
from edcon.utils.logging import Logging
from edcon.edrive.com_modbus import ComModbus
from edcon.edrive.motion_handler import MotionHandler
from edcon.utils.logging import Logging

# Enable loglevel info
Logging()

# Create a list of all Modbus targets
coms = [ComModbus(ip_address='192.168.0.1'),
        ComModbus(ip_address='192.168.0.119')]
mots = [MotionHandler(com) for com in coms]

for mot in mots:
    mot.acknowledge_faults()
    mot.enable_powerstage()
    if not mot.referenced():
        mot.referencing_task()

for mot in mots:
    mot.position_task(10000000, 300000, nonblocking=True)
time.sleep(0.1)

while True:
    target_positions_reached = [mot.target_position_reached() for mot in mots]
    Logging.logger.info(f"Target positions reached: {target_positions_reached}")
    if all(target_positions_reached):
        break
    time.sleep(0.1)

for mot in mots:
    mot.shutdown()

Download Example

Travel to fix stop (nonblock)

from edcon.edrive.com_modbus import ComModbus
from edcon.edrive.motion_handler import MotionHandler
from edcon.utils.logging import Logging
import time

# Enable loglevel info
Logging()

com = ComModbus("192.168.0.1")
base_velocity = com.read_pnu(12345)  # Read base velocity (P1.11280701)
# The following example assumes a rotative axis with factor group pos 10^-6 and vel 10^-3
com.write_pnu(12168, value=0.2)  # Set clamping torque to 0.2 Nm (P1.526801)
# Optionally configure the clamping torque windows
# com.write_pnu(11614, value=0.1)  # Monitoring window torque (P1.4668)
# Optionally also configure the monitoring windows for fix stop detection
# com.write_pnu(11635, value=0.1)  # Fixed stop detection damping time in seconds (P1.4693)
# com.write_pnu(11636, value=0.01)  # Limit value following error in revolutions (P1.4694)
with MotionHandler(com) as mot:
    mot.base_velocity = base_velocity
    mot.configure_traversing_to_fixed_stop(True)  # Enable traversing to fixed stop
    mot.acknowledge_faults()
    mot.enable_powerstage()
    if not mot.referenced():
        mot.referencing_task()

    # Move to position 100.0 with speed 6 rpm
    mot.position_task(100000000, 12000, nonblocking=True)
    while True:
        pos_str = f"{mot.current_position()*10e-6:.3f}"
        vel_str = f"{mot.current_velocity():.3f}"
        ct_str = f"{mot.clamping_torque_reached()}"
        fs_str = f"{mot.fix_stop_reached()}"
        print(
            f"Pos: {pos_str:>10} | Vel: {vel_str:>10} | Clamping torque: {ct_str:>5} | Fix stop: {fs_str:>5}"
        )
        if mot.fix_stop_reached():
            print("Fixed stop reached.")
            break
        if mot.target_position_reached():
            print("Target position reached without fixed stop.")
            break
        if mot.fault_present():
            print(f"{mot.fault_string()}")
            break
        time.sleep(0.1)
    mot.stop_motion_task()

Download Example

Velocity

import time
from edcon.edrive.com_modbus import ComModbus
from edcon.edrive.motion_handler import MotionHandler
from edcon.utils.logging import Logging

# Enable loglevel info
Logging()

com = ComModbus('192.168.0.1')
with MotionHandler(com) as mot:
    mot.acknowledge_faults()
    mot.enable_powerstage()
    mot.referencing_task()

    mot.velocity_task(50000, 3.0)
    time.sleep(1.0)
    mot.velocity_task(-30000, 3.0)

Download Example

Velocity (nonblock)

import time
from edcon.edrive.com_modbus import ComModbus
from edcon.edrive.motion_handler import MotionHandler
from edcon.utils.logging import Logging

# Enable loglevel info
Logging()

com = ComModbus('192.168.0.1')
with MotionHandler(com) as mot:
    mot.acknowledge_faults()
    mot.enable_powerstage()
    mot.referencing_task()

    # Enable continuous update so that new position tasks can be started while still in motion
    mot.configure_continuous_update(True)

    mot.velocity_task(50000)
    time.sleep(1)
    mot.velocity_task(-10000)
    time.sleep(1)
    # Be aware that only changing the absolute velocity value update the ongoing task
    mot.velocity_task(10001)

    time.sleep(3)
    mot.stop_motion_task()

Download Example

Record

import time
from edcon.edrive.com_modbus import ComModbus
from edcon.edrive.motion_handler import MotionHandler
from edcon.utils.logging import Logging

# Enable loglevel info
Logging()

edrive = ComModbus("192.168.0.1")
with MotionHandler(edrive) as mot:
    mot.acknowledge_faults()
    mot.enable_powerstage()
    mot.referencing_task()

    # toggle five times between records 1 and 2
    for i in range(5):
        mot.record_task(1)
        time.sleep(1)
        mot.record_task(2)
        time.sleep(1)

    mot.stop_motion_task()
    mot.disable_powerstage()

Download Example

Jog

from edcon.edrive.com_modbus import ComModbus
from edcon.edrive.motion_handler import MotionHandler
from edcon.utils.logging import Logging

# Enable loglevel info
Logging()

com = ComModbus('192.168.0.1')
with MotionHandler(com) as mot:
    mot.acknowledge_faults()
    mot.enable_powerstage()

    mot.jog_task(True, False, duration=3.0)

Download Example

Jog (nonblock)

import time
from edcon.edrive.com_modbus import ComModbus
from edcon.edrive.motion_handler import MotionHandler
from edcon.utils.logging import Logging

# Enable loglevel info
Logging()

com = ComModbus('192.168.0.1')
with MotionHandler(com) as mot:
    mot.acknowledge_faults()
    mot.enable_powerstage()

    mot.jog_task(True, False, duration=0.0)
    time.sleep(4)
    mot.jog_task(False, True, duration=0.0)
    time.sleep(4)

    mot.stop_motion_task()

Download Example

PNU Access

from edcon.edrive.com_modbus import ComModbus
from edcon.utils.logging import Logging

# Enable loglevel info
Logging()

com = ComModbus('192.168.0.1')

# Read currently selected telegram
original_value = com.read_pnu(3490, 0)

# Configure a different telegram
new_value = 1 if not original_value == 1 else 111
com.write_pnu(3490, 0, new_value)

# Write back original value
com.write_pnu(3490, 0, original_value)

Download Example

Parameterset load

from edcon.utils.logging import Logging
from edcon.edrive.parameter_set import ParameterSet
from edcon.edrive.parameter_handler import ParameterHandler
from edcon.edrive.com_modbus import ComModbus

# Enable loglevel info
Logging()

com = ComModbus("192.168.0.1")

parameter_set = ParameterSet("my_parameters.pck")
parameter_handler = ParameterHandler(com)

i = None
for i, parameter in enumerate(parameter_set):
    status = parameter_handler.write(parameter)

    if not status:
        Logging.logger.error(f"Setting {parameter.uid()} to {parameter.value} failed")

print(f"{i+1} PNUs succesfully written!")

Download Example