Hello everyone,
I am currently working on a project where I need to read the position and force data of a UR robot (Universal Robots) via the RTDE protocol (Real-Time Data Exchange) in Python. However, I have encountered some challenges and would appreciate any help.
Here are the steps I have taken so far:
- The connection to the robot via RTDE has been successfully established, and the configuration is done using an .xml configuration file.
- The RTDE data stream has been started, and some data can be received.
- I have already retrieved and printed the
actual_TCP_pose
(position) andactual_TCP_force
(force) as variables. However, I am unsure if the configuration is optimal and how I might further process or sample these data.
import sys
import time
import logging
import rtde.rtde as rtde
import rtde.rtde_config as rtde_config
# Set logging level (optional)
logging.getLogger().setLevel(logging.INFO)
# Robot IP and port
ROBOT_HOST = "192......"
ROBOT_PORT = 30004
config_filename = "control_loop_configuration.xml"
# Loop condition
keep_running = True
# Load configuration file and establish RTDE connection
conf = rtde_config.ConfigFile(config_filename)
state_names, state_types = conf.get_recipe("state")
setp_names, setp_types = conf.get_recipe("setp")
watchdog_names, watchdog_types = conf.get_recipe("watchdog")
con = rtde.RTDE(ROBOT_HOST, ROBOT_PORT)
con.connect()
# Get controller version (optional, for information)
con.get_controller_version()
# Set up the data to be received and sent
con.send_output_setup(state_names, state_types)
watchdog = con.send_input_setup(watchdog_names, watchdog_types)
# Watchdog setting
watchdog.input_int_register_0 = 0
# Start synchronization
if not con.send_start():
sys.exit("Error starting data transfer")
# Variables to store received data
Position_Data = None
Force_Data = None
# ################################## Start of Loop ##################################
try:
while keep_running:
# Receive data package
state = con.receive()
if state is None:
continue
# Extract position and force data into variables (no parentheses)
Position_Data = state.actual_TCP_pose # TCP position (xyz + rotation)
Force_Data = state.actual_TCP_force # TCP force (Fx, Fy, Fz)
# Print data for monitoring
print("Current Position (TCP):", Position_Data)
print("Current Force (TCP):", Force_Data)
# Split data if needed
Position_Z = Position_Data[0:3]
Force_Z = Force_Data[0:3]
# Sampling rate
time.sleep(10)
# Kick the watchdog
con.send(watchdog)
except KeyboardInterrupt:
print("Ending data acquisition.")
finally:
con.send_pause()
con.disconnect()
Does anyone have experience with this or know specific RTDE commands I can use to receive this data more efficiently and reliably? Any sample code, tips on additional RTDE configuration settings, or further configuration suggestions would be greatly appreciated!
Or is the code already fully functional as it is?
Additionally, I would like to output the data to a table (Excel) after each loop.
Thanks in advance for your help!