ik_proxy.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 # FSRobo-R Package BSDL
5 # ---------
6 # Copyright (C) 2019 FUJISOFT. All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without modification,
9 # are permitted provided that the following conditions are met:
10 # 1. Redistributions of source code must retain the above copyright notice,
11 # this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright notice,
13 # this list of conditions and the following disclaimer in the documentation and/or
14 # other materials provided with the distribution.
15 # 3. Neither the name of the copyright holder nor the names of its contributors
16 # may be used to endorse or promote products derived from this software without
17 # specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # --------
30 
31 from moveit_msgs.srv import GetPositionIK, GetPositionIKResponse
32 import rospy
33 from fsrobo_r_driver.robot_interface import RobotInterface
34 from fsrobo_r_driver.robot_tool_interface import RobotToolInterface
35 
37  def __init__(self):
38  self._last_offset = []
39 
40  def load_config(self):
41  self._config = {}
42 
43  params = rospy.get_param('ik_proxy')
44 
45  robot_interface = {}
46  tool_interface = {'': None}
47  joint_names = {}
48 
49  for x in params:
50  group_name = x['group_name']
51  robot_name = x['robot_name']
52  tool_name = x['tool_name']
53 
54  if not robot_name in robot_interface:
55  robot_interface[robot_name] = RobotInterface(robot_name)
56  if robot_name:
57  ns = robot_name + '/'
58  else:
59  ns = ''
60  joint_names[robot_name] = rospy.get_param(ns + 'controller_joint_names')
61 
62  if not tool_name in tool_interface:
63  tool_interface[tool_name] = RobotToolInterface(tool_name)
64 
65  self._config[group_name] = {
66  'robot': robot_interface[robot_name],
67  'tool': tool_interface[tool_name],
68  'joint': joint_names[robot_name] }
69 
70  def handle_solve_ik(self, req):
71  print "-----------request!"
72  pos = req.ik_request.pose_stamped.pose.position
73  ori = req.ik_request.pose_stamped.pose.orientation
74  group_name = req.ik_request.group_name
75 
76  tool = self._config[group_name]['tool']
77 
78  res = GetPositionIKResponse()
79 
80  print pos
81  print ori
82  rb = self._config[group_name]['robot']
83  if tool:
84  print('tool_offset: {}'.format(tool.get_offset()))
85  offset = tool.get_offset()
86  if self._last_offset != offset:
87  rb.set_tool(tool)
88  self._last_offset = offset
89  else:
90  if self._last_offset != []:
91  rb.set_tool(tool)
92  self._last_offset = []
93 
94  joint = rb.get_position_ik([pos.x, pos.y, pos.z], [ori.x, ori.y, ori.z, ori.w])
95  res.solution.joint_state.header = req.ik_request.pose_stamped.header
96  if len(joint) != 0:
97  print "pose!"
98  print req.ik_request.pose_stamped.pose
99  print "req!"
100  print req.ik_request
101  print "joint!"
102  print joint
103  print "success!"
104  #res.solution.joint_state.name = self._config[group_name]['joint'] + ['left_ezgripper_knuckle_palm_L1_1', 'left_ezgripper_knuckle_L1_L2_1', 'left_ezgripper_knuckle_palm_L1_2', 'left_ezgripper_knuckle_L1_L2_2']
105  res.solution.joint_state.name = self._config[group_name]['joint']
106  #res.solution.joint_state.position = joint + [0, 0, 0, 0]
107  res.solution.joint_state.position = joint
108  res.error_code.val = res.error_code.SUCCESS
109  #res.error_code.val = 10
110  print '>--------------res'
111  print res
112  print '<--------------res'
113  print res.solution.joint_state.position
114  else:
115  print('NO IK!')
116  res.error_code.val = res.error_code.NO_IK_SOLUTION
117 
118  return res
119 
120  def run(self):
121  rospy.init_node('ik_proxy')
122  self.load_config()
123  s = rospy.Service('solve_ik', GetPositionIK, self.handle_solve_ik)
124  print "Ready to solve ik."
125  rospy.spin()
126 
127 if __name__ == "__main__":
129  s.run()
def handle_solve_ik(self, req)
Definition: ik_proxy.py:70
def load_config(self)
Definition: ik_proxy.py:40


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