Writing movement via MODBUS TCP/IP


#1

I want to write movements in all axis using a python script that connects to the dobot mg400 via modbus tcp.
So far i´ve been able to write to just 1 bit registers like start, stop to run a blocky program saved on the robot. But what i need is seting coordinates for the robot to move to in the python script.
Anyone that has done something like this that could help me?

greetings.


#2

Did you already try to write movements with python via tcp (not modbus tcp)?


#3

hi, i think i made a mistake while writing my post. I am using tcp. This is the code that lets me change the start and stop bits and that i know it works.

from pymodbus.client import ModbusTcpClient
import time

# Dirección IP del dispositivo Modbus TCP para escritura
ip_address_write = '192.168.1.6'

# Puerto Modbus TCP (generalmente 502)
port = 502

# Crea un cliente Modbus TCP para escritura
client_write = ModbusTcpClient(ip_address_write, port)


try:
client_write.connect() # Conecta con el dispositivo de escritura
print("Robot Conectado")

bit_value = True # Valor del bit que deseas escribir (True para 1 o False para 0)
address_write = 0x00 # Dirección de escritura (dirección del Coil)
client_write.write_coil(address_write, bit_value, unit=0x01) # Escribe el valor del bit en el Coil
print(f"Valor escrito en el Coil {address_write} ROBOT: {bit_value}")    
time.sleep(3)

bit_valueF = False # Valor del bit que deseas escribir (True para 1 o False para 0)
client_write.write_coil(address_write, bit_valueF, unit=0x01)
print(f"Valor escrito en el Coil {address_write} ROBOT: {bit_valueF}")     
time.sleep(20)


except Exception as e:
print(f"Error de comunicación: {e}")
finally:
# Cierra la conexión
client_write.close()
print("Robot Desconectado")

sorry if the comments are in spanish. As i said, this script worked for running any program saved in the robot.

This is the script that tries to write movement on the robot and doesn´t work:

from pymodbus.client import ModbusTcpClient
import struct
import time

# Dirección IP del dispositivo Modbus TCP para escritura
ip_address_write = '192.168.1.6'

# Puerto Modbus TCP (generalmente 502)
port = 502

# Crea un cliente Modbus TCP para escritura
client_write = ModbusTcpClient(ip_address_write, port)

try:
    client_write.connect()  # Conecta con el dispositivo de escritura
    print("Robot Conectado")

    float_value = 200  # Valor flotante de 32 bits que deseas escribir
    address_write = 41326  # Dirección de escritura (dirección del Holding Register)

    # Convierte el valor flotante en una representación binaria en 32 bits
    value_bytes = struct.pack('>f', float_value)

    # Divide el valor binario en dos registros de 16 bits (Holding Registers)
    value_registers = struct.unpack('>HH', value_bytes)

    # Escribe los registros en el dispositivo de escritura
    client_write.write_register(address_write, value_registers[0], unit=0x01)  # Escribir la parte alta
    time.sleep(1)  # Espera para escribir la parte baja
    client_write.write_register(address_write + 1, value_registers[1], unit=0x01)  # Escribir la parte baja

    print(f"Valor escrito en el Holding Register {address_write} ROBOT: {float_value}")

except Exception as e:
    print(f"Error de comunicación: {e}")
finally:
    # Cierra la conexión
    client_write.close()
    print("Robot Desconectado")

i am getting the registers from the DobotStudio Pro User Guide (MG400&M1 Pro) V2.7.1 · Jul 10, 2023 page 75-79. I got it from https://www.dobot-robots.com/service/download-center

i would appreciate any help you could give me.

**


#4

I have set-up a Modbus connection from my PC with the MG400 LAN2 port with Modbus Poll (a Modbus master simulator).
Settings: IP: 192.168.2.6, Port 502, Slave1.
I can:
Function 01 Read/Write Coils(0X), Binary
(When Remote Mobus is enabled in DobotStudio Pro V2.7.1 with running script)
Address
00 Start
01 ? Pause (not tested yet)
02 ? Continue (not tested yet)
03 ? Stop (not tested yet)
04 ? Emergency stop (not tested yet)
05 Clear alarm
06 ? Reset

Function 02 Read Discrete Inputs(1X), Binary
(When Remote Mobus is enabled in DobotStudio Pro V2.7.1 with running script)
Address
00
01
02
03 Running status
04 Alarm status
05 Collision status
06 Manual/Automatic mode

Read and set Address
50 DO1
|
69 DO20

Function 04 Read Input Register (3X), Data Float
Address joint
202 J1
204 J2
206 J3
208 J4
242 X
244 Y
246 Z
248 R

I can also write the (float)value of 200 to HoldingRegister 1326 and, as with your example,
the MG400 did not move.
We probably have to set additional registers in the 1300-1313 range to make it move but I did not find a detailed description of these registers. (e.g. What U16 value do I have to write in HoldingRegister 1313 to Start jogging. Is it 1, or ??)


#5

Could you please give me more information about the Joint and Axis registers and their addresses? How did you get them, did you just configure them somewhere or did you have any docu decribing the addresses? If so, where can I get that docu, please?
Thanks for your help :).