calculate_offsets.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from subprocess import call
4 
5 import rospy
6 from trajectory_msgs.msg import JointTrajectory, JointTrajectoryPoint
7 from force_torque_sensor.srv import CalculateSensorOffset
8 from force_torque_sensor.srv import SetSensorOffset
9 from geometry_msgs.msg import Wrench
10 
11 import dynamic_reconfigure.client
12 
14 
15  rospy.init_node('calculate_offsets')
16 
17  package_to_store = rospy.get_param('~package_to_store')
18  store_to_file = rospy.get_param('~store_to_file')
19  scenario = rospy.get_param('~scenario')
20  robot = rospy.get_param('~robot')
21  set_calculated_offset = rospy.get_param('~set_calculated_offset')
22  offset_params_ns = rospy.get_param('~offset_params_ns', "/temp") # /arm/driver
23  recalibrate_srv_ns = rospy.get_param('~recalibrate_srv_ns', "") # /arm/driver
24  if robot == "kuka":
25  joint_names = rospy.get_param('/controller_joint_names')
26  controller_topic = '/position_trajectory_controller/command'
27  calcOffset_service = '/CalculateOffsets'
28  setOffset_service = '/SetSensorOffset'
29  elif robot == "ur":
30  joint_names = rospy.get_param("/hardware_interface/joints")
31  controller_topic = "/pos_based_pos_traj_controller/command"
32  calcOffset_service = '/CalculateOffsets'
33  setOffset_service = '/SetSensorOffset'
34  else:
35  joint_names = rospy.get_param('/arm/joint_names')
36  controller_topic = '/arm/joint_trajectory_controller/command'
37  calcOffset_service = '/arm/CalculateOffsets'
38  setOffset_service = '/arm/SetSensorOffset'
39 
40  trajectory_pub = rospy.Publisher(controller_topic, JointTrajectory, latch=True, queue_size=1)
41  calculate_offsets_srv = rospy.ServiceProxy(calcOffset_service, CalculateSensorOffset)
42  set_offsets_srv = rospy.ServiceProxy(setOffset_service, SetSensorOffset)
43 
44  ##print call('rospack find force_torque_sensor', shell=True)
45  ##call('rosparam dump -v `rospack find force_torque_sensor`/config/sensor_offset.yaml /fts/Offset')
46 
47  # Posees
48  poses = [[0.0, 0.0, 1.5707963, 0.0, -1.5707963, 0.0],
49  [0.0, 0.0, 1.5707963, 0.0, 1.5707963, 0.0]]
50 
51  poses_kuka = [[0.0, -1.5707963, 1.5707963, 0.0, -1.5707963, 0.0],
52  [0.0, -1.5707963, 1.5707963, 0.0, 1.5707963, 0.0]]
53 
54  poses_ur = [[1.5707963, -1.5707963, 1.5707963, -1.5707963, 1.5707963, 0.0],
55  [1.5707963, -1.5707963, 1.5707963, -1.5707963, -1.5707963, 0.0]]
56 
57  measurement = Wrench()
58 
59  for i in range(0,len(poses)):
60  trajectory = JointTrajectory()
61  point = JointTrajectoryPoint()
62  trajectory.header.stamp = rospy.Time.now()
63  trajectory.joint_names = joint_names
64 
65  point.time_from_start = rospy.Duration(2.5)
66  if robot == "kuka":
67  point.positions = poses_kuka[i]
68  elif robot == "ur":
69  point.positions = poses_ur[i]
70  else:
71  point.positions = poses[i]
72 
73  trajectory.points.append(point)
74  trajectory_pub.publish(trajectory)
75  rospy.loginfo("Going to position: " + str(point.positions))
76 
77  rospy.sleep(10.0)
78 
79  rospy.loginfo("Calculating offsets.")
80  ret = calculate_offsets_srv(False)
81 
82  measurement.force.x += ret.offset.force.x
83  measurement.force.y += ret.offset.force.y
84  measurement.force.z += ret.offset.force.z
85  measurement.torque.x += ret.offset.torque.x
86  measurement.torque.y += ret.offset.torque.y
87  measurement.torque.z += ret.offset.torque.z
88 
89 
90  measurement.force.x /= len(poses)
91  measurement.force.y /= len(poses)
92  measurement.force.z /= len(poses)
93  measurement.torque.x /= len(poses)
94  measurement.torque.y /= len(poses)
95  measurement.torque.z /= len(poses)
96 
97 
98  rospy.set_param(offset_params_ns + '/Offset/force/x', measurement.force.x)
99  rospy.set_param(offset_params_ns + '/Offset/force/y', measurement.force.y)
100  rospy.set_param(offset_params_ns + '/Offset/force/z', measurement.force.z)
101  rospy.set_param(offset_params_ns + '/Offset/torque/x', measurement.torque.x)
102  rospy.set_param(offset_params_ns + '/Offset/torque/y', measurement.torque.y)
103  rospy.set_param(offset_params_ns + '/Offset/torque/z', measurement.torque.z)
104 
105  if set_calculated_offset:
106  ret = set_offsets_srv(measurement)
107  rospy.loginfo(ret.message)
108  #client = dynamic_reconfigure.client.Client(recalibrate_srv_ns)
109  #client.update_configuration({"force":{"x":measurement.force.x, "y":measurement.force.y, "z":measurement.force.z}, "torque":{"x":measurement.torque.x, "y":measurement.torque.y, "z":measurement.torque.z}})
110 
111  if store_to_file:
112  if scenario == '':
113  call('rosparam dump -v `rospack find ' + package_to_store + ' `/config/sensor_offset.yaml ' + offset_params_ns + '/Offset', shell=True)
114  else:
115  call('rosparam dump -v `rospack find ' + package_to_store + ' `/config/robot_with_' + scenario + '_offset.yaml ' + offset_params_ns + '/Offset', shell=True)
116 
117 
118 
119 if __name__ == "__main__":
120 
121  try:
123  except rospy.ROSInterruptException:
124  pass


force_torque_sensor
Author(s):
autogenerated on Fri Sep 18 2020 03:06:30