testRT_comm.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import roslib; roslib.load_manifest('ur_driver')
3 import time, sys, threading, math
4 import copy
5 import datetime
6 import socket, select
7 import struct
8 import traceback, code
9 import SocketServer
10 
11 import rospy
12 
13 from sensor_msgs.msg import JointState
14 from ur_driver.deserializeRT import RobotStateRT
15 from ur_msgs.msg import *
16 
17 
18 joint_names = ['shoulder_pan_joint', 'shoulder_lift_joint', 'elbow_joint',
19  'wrist_1_joint', 'wrist_2_joint', 'wrist_3_joint']
20 joint_offsets = {}
21 
22 
23 def __on_packet(buf):
24  stateRT = RobotStateRT.unpack(buf)
25 
26  msg = RobotStateRTMsg()
27  msg.time = stateRT.time
28  msg.q_target = stateRT.q_target
29  msg.qd_target = stateRT.qd_target
30  msg.qdd_target = stateRT.qdd_target
31  msg.i_target = stateRT.i_target
32  msg.m_target = stateRT.m_target
33  msg.q_actual = stateRT.q_actual
34  msg.qd_actual = stateRT.qd_actual
35  msg.i_actual = stateRT.i_actual
36  msg.tool_acc_values = stateRT.tool_acc_values
37  msg.tcp_force = stateRT.tcp_force
38  msg.tool_vector = stateRT.tool_vector
39  msg.tcp_speed = stateRT.tcp_speed
40  msg.digital_input_bits = stateRT.digital_input_bits
41  msg.motor_temperatures = stateRT.motor_temperatures
42  msg.controller_timer = stateRT.controller_timer
43  msg.test_value = stateRT.test_value
44  msg.robot_mode = stateRT.robot_mode
45  msg.joint_modes = stateRT.joint_modes
46  pub_robot_stateRT.publish(msg)
47 
48 
49  msg = JointState()
50  msg.header.stamp = rospy.get_rostime()
51  msg.header.frame_id = "From real-time state data"
52  msg.name = joint_names
53  msg.position = [0.0] * 6
54  for i, q in enumerate(stateRT.q_actual):
55  msg.position[i] = q + joint_offsets.get(joint_names[i], 0.0)
56  msg.velocity = stateRT.qd_actual
57  msg.effort = [0]*6
58  pub_joint_statesRT.publish(msg)
59 
60 
61 
62 
63 def main():
64  rospy.init_node('testRT_comm', disable_signals=True)
65 
66  global pub_joint_statesRT
67  pub_joint_statesRT = rospy.Publisher('joint_statesRT', JointState, queue_size=1)
68  global pub_robot_stateRT
69  pub_robot_stateRT = rospy.Publisher('robot_stateRT', RobotStateRTMsg, queue_size=1)
70 
71 
72 
73  robot_hostname = '192.168.0.42'
74  rt_port = 30003
75 
76  rt_socket = socket.create_connection((robot_hostname, rt_port))
77  buf = ""
78 
79  while not rospy.is_shutdown():
80  more = rt_socket.recv(4096)
81  if more:
82  buf = buf + more
83 
84  # Attempts to extract a packet
85  packet_length = struct.unpack_from("!i", buf)[0]
86  print("PacketLength: ", packet_length, "; BufferSize: ", len(buf))
87  if len(buf) >= packet_length:
88  packet, buf = buf[:packet_length], buf[packet_length:]
89  __on_packet(packet)
90  else:
91  print("There is no more...")
92 
93  rt_socket.close()
94 
95 
96 
97 
98 if __name__ == '__main__': main()


ur_driver
Author(s): Stuart Glaser, Shaun Edwards, Felix Messmer
autogenerated on Sun Nov 24 2019 03:36:29