force_publisher.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 import rospy
5 from std_srvs.srv import Empty, EmptyResponse
6 from geometry_msgs.msg import WrenchStamped, Wrench
7 from fsrobo_r_msgs.srv import GetWrench
8 
9 class ForcePublisher(object):
10  def __init__(self):
11  self._zero_offset = Wrench()
12  self._reset_time = rospy.Time.now()
14  self._resetting = True
15  self.wrench = Wrench()
16 
17  def cb_raw_force(self, msg):
18  if self._reset_time > rospy.Time.now():
19  self._zero_offset_buffer.append(msg)
20 
21  else:
22  if self._resetting:
23  rospy.logwarn("reset!")
25  self._resetting = False
26  ws = WrenchStamped()
27  ws.header = msg.header
28  ws.wrench.force.x = msg.wrench.force.x - self._zero_offset.force.x
29  ws.wrench.force.y = msg.wrench.force.y - self._zero_offset.force.y
30  ws.wrench.force.z = msg.wrench.force.z - self._zero_offset.force.z
31  ws.wrench.torque.x = msg.wrench.torque.x - self._zero_offset.torque.x
32  ws.wrench.torque.y = msg.wrench.torque.y - self._zero_offset.torque.y
33  ws.wrench.torque.z = msg.wrench.torque.z - self._zero_offset.torque.z
34  self.wrench = ws.wrench
35  self._pub.publish(ws)
36 
37  def _avg_wrench(self, wrench_list):
38  w = Wrench()
39  for x in wrench_list:
40  w.force.x += x.wrench.force.x
41  w.force.y += x.wrench.force.y
42  w.force.z += x.wrench.force.z
43  w.torque.x += x.wrench.torque.x
44  w.torque.y += x.wrench.torque.y
45  w.torque.z += x.wrench.torque.z
46 
47  n = len(wrench_list)
48  if n:
49  w.force.x /= n
50  w.force.y /= n
51  w.force.z /= n
52  w.torque.x /= n
53  w.torque.y /= n
54  w.torque.z /= n
55 
56  return w
57 
58  def cb_reset(self, req):
59  self._zero_offset_buffer = []
60  self._reset_time = rospy.Time.now() + rospy.Duration(1)
61  self._resetting = True
62  rospy.logwarn("cb_reset!")
63 
64  return EmptyResponse()
65 
66  def cb_get_current_force(self, req):
67  return self.wrench
68 
69  def run(self):
70  self._pub = rospy.Publisher('force', WrenchStamped, queue_size=10)
71  rospy.Subscriber('raw_force', WrenchStamped, self.cb_raw_force)
72  rospy.Service('~reset', Empty, self.cb_reset)
73  rospy.Service('get_current_force', GetWrench, self.cb_get_current_force)
74  rospy.spin()
75 
76 if __name__ == '__main__':
77  rospy.init_node('force_publisher')
79  fp.run()
def _avg_wrench(self, wrench_list)


fsrobo_r_driver
Author(s): F-ROSROBO
autogenerated on Sun Feb 9 2020 03:58:29