首页 > 解决方案 > 如何在 Python 中将 Unix 时间戳添加到传入的 Universal-Robots 数据中

问题描述

我在尝试使用 python over tcp socket 收集机器人数据时遇到问题。

问题:我可以使用 TCP/IP 套接字从 Universal Robots (UR) 实时数据交换 (RTDE) 获取机器人工具位置数据。但我无法通过机器人操作系统 (ROS) 获取这些数据。

我可以通过 ROS 但不能从 RTDE 获得“Robotiq”力/扭矩数据。因此我必须单独收集数据并将数据连接在一起。

ROS 使用 Unix 时间戳,但 UR 机器人控制器在启动时将其时间戳初始化为零。

因此,如何合并这些数据?为什么有人会将他们的时间戳初始化为零?

我想知道的是;我可以通过在提供的 python 代码中的某处添加类似“time.time()”的内容来将我自己的系统时间戳添加到这些数据中吗?

www.universal-robots.com/how-tos-and-faqs/how-to/ur-how-tos/real-time-data-exchange-rtde-guide-22229/

在上述链接的页面底部,有一些示例 python 代码的包下载,它演示了 RTDE 如何解析传入的 UR 数据并将其转换为 .csv 文件。

该代码对传入数据使用字典,其键位于包中包含的 xml 文件中。

我所做的一切都会引发错误,因为每段代码似乎都需要一定数量的参数,而我对 python 的了解不够,无法理解正在发生的事情。我试图在包中更改的脚本是'record.py,见下文。我导入了时间,然后尝试将 time.time() 添加到各个地方,看看会发生什么。我有很多错误。

我花了一整天的时间来解决这个问题。其中一半是试图弄清楚为什么 UR 有一个似乎与任何东西都不匹配的奇怪时间戳。

如果您不想自己打开包,下面是脚本'record.py',我认为我应该可以添加时间戳,但是在哪里???

#!/usr/bin/env/ python
import argparse
import logging
import sys
sys.path.append('..')
import rtde.rtde as rtde
import rtde.rtde_config as rtde_config
import rtde.csv_writer as csv_writer

#parameters
parser = argparse.ArgumentParser()
parser.add_argument('--host', default='localhost', help='name of host to connect to (localhost)')
parser.add_argument('--port', type=int, default=30004, help='port number (30004)')
parser.add_argument('--samples', type=int, default=0, help='number of samples to record')
parser.add_argument('--frequency', type=int, default=125, help='the sampling frequency in Herz')
parser.add_argument('--config', default='record_configuration.xml', help='data configuration file to use (record_configuration.xml)')
parser.add_argument('--output', default='robot_data.csv', help='data output file to write to (robot_data.csv)')
parser.add_argument("--verbose", help="increase output verbosity", action="store_true")
args = parser.parse_args()

if args.verbose:
    logging.basicConfig(level=logging.INFO)

conf = rtde_config.ConfigFile(args.config)
output_names, output_types = conf.get_recipe('out')

con = rtde.RTDE(args.host, args.port)
con.connect()

# get controller version
con.get_controller_version()

# setup recipes
if not con.send_output_setup(output_names, output_types, frequency = args.frequency):
    logging.error('Unable to configure output')
    sys.exit()

#start data synchronization
if not con.send_start():
    logging.error('Unable to start synchronization')
    sys.exit()

with open(args.output, 'w') as csvfile:
    writer = csv_writer.CSVWriter(csvfile, output_names, output_types)
    writer.writeheader()

    i = 1
    keep_running = True
    while keep_running:

        if i%args.frequency == 0:
            if args.samples > 0:
                sys.stdout.write("\r")
                sys.stdout.write("{:.2%} done.".format(float(i)/float(args.samples))) 
                sys.stdout.flush()
            else:
                sys.stdout.write("\r")
                sys.stdout.write("{:3d} samples.".format(i)) 
                sys.stdout.flush()
        if args.samples > 0 and i >= args.samples:
            keep_running = False
        try:
            state = con.receive()
            if state is not None:
                writer.writerow(state)
            else:
                sys.exit()
        except KeyboardInterrupt:
            keep_running = False
        i += 1

sys.stdout.write("\rComplete!            \n")

con.send_pause()
con.disconnect()

更改代码后,我期望获得一个 .csv 文件,其中包含一个名为 Timestamp 的新第一列,其中填充了每个数据条目的 Unix 时间戳。

我得到的都是垃圾,不管我做了什么。

非常感谢您的帮助。

标签: pythonxmldatabasetcpexport-to-csv

解决方案


推荐阅读