force_observer.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 import sys
5 import rospy
6 import moveit_commander
7 import threading
8 from geometry_msgs.msg import Wrench, WrenchStamped
9 from fsrobo_r_msgs.srv import SetStopForce, SetStopForceResponse
10 from std_srvs.srv import Empty, EmptyResponse, Trigger, TriggerResponse
11 from std_msgs.msg import String
12 
13 class ForceObserver(object):
14  def __init__(self, group_name):
15  #self._group = moveit_commander.MoveGroupCommander(group_name, ns="/")
16  self._handlers = {}
17  self._current_wrench = Wrench()
18  self._lock = threading.Lock()
19  self._is_stopped = False
20 
21  def _cb_set_stop_force(self, req):
22 
23  if req.axis in (req.AXIS_X, req.AXIS_Y, req.AXIS_Z):
24  threshold = getattr(self._current_wrench.force, req.axis) + req.val
25  info = 'axis: {}, val: {}, threshold: {}'.format(req.axis, req.val, threshold)
26  rospy.loginfo('set_stop_force: {}'.format(info))
27  if req.val >= 0:
28  self.add_handler(lambda msg: getattr(msg.wrench.force, req.axis) > threshold, lambda msg: self.stop(), info)
29  else:
30  self.add_handler(lambda msg: getattr(msg.wrench.force, req.axis) < threshold, lambda msg: self.stop(), info)
31  elif req.axis in (req.AXIS_MX, req.AXIS_MY, req.AXIS_MZ):
32  threshold = getattr(self._current_wrench.torque, req.axis) + req.val
33  info = 'axis: {}, val: {}, threshold: {}'.format(req.axis, req.val, threshold)
34  rospy.loginfo('set_stop_force: {}'.format(info))
35  if req.val >= 0:
36  self.add_handler(lambda msg: getattr(msg.wrench.torque, req.axis) > threshold, lambda msg: self.stop(), info)
37  else:
38  self.add_handler(lambda msg: getattr(msg.wrench.torque, req.axis) < threshold, lambda msg: self.stop(), info)
39  else:
40  raise rospy.ServiceException('invalid axis: {}'.format(req.axis))
41 
42  res = SetStopForceResponse()
43  return res
44 
45  def _cb_clear_stop_force(self, req):
46  rospy.loginfo('clear_stop_force')
47  with self._lock:
48  self._handlers = {}
49  self._is_stopped = False
50  res = EmptyResponse()
51  return res
52 
53  def _cb_is_stopped(self, req):
54  res = TriggerResponse()
55  res.success = self._is_stopped
56  return res
57 
58  def stop(self):
59  #self._group.stop()
60  self._is_stopped = True
61  self._pub.publish('stop')
62 
63  def add_handler(self, condition, command, info='', key='default'):
64  with self._lock:
65  self._handlers['default'] = {'condition': condition, 'command': command, 'info': info}
66 
67  def _cb_wrench_stamped(self, msg):
68  self._current_wrench = msg.wrench
69  new_handlers = {}
70  with self._lock:
71  for k, x in self._handlers.iteritems():
72  if x['condition'](msg):
73  rospy.loginfo('command is called!: {}'.format(x['info']))
74  x['command'](msg)
75  else:
76  new_handlers[k] = x
77 
78  self._handlers = new_handlers
79 
80  def run(self):
81  rospy.Subscriber('force', WrenchStamped, self._cb_wrench_stamped)
82  rospy.Service('set_stop_force', SetStopForce, self._cb_set_stop_force)
83  rospy.Service('clear_stop_force', Empty, self._cb_clear_stop_force)
84  rospy.Service('is_stopped', Trigger, self._cb_is_stopped)
85  self._pub = rospy.Publisher('/trajectory_execution_event', String, queue_size=10)
86 
87 def main():
88  rospy.init_node('force_observer', anonymous=True)
89  fo = ForceObserver(sys.argv[1])
90  fo.run()
91  rospy.spin()
92 
93 if __name__ == '__main__':
94  main()
def add_handler(self, condition, command, info='', key='default')
def _cb_clear_stop_force(self, req)
def _cb_set_stop_force(self, req)
def _cb_wrench_stamped(self, msg)
def __init__(self, group_name)


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