robot_interface.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # FSRobo-R Package BSDL
4 # ---------
5 # Copyright (C) 2019 FUJISOFT. All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without modification,
8 # are permitted provided that the following conditions are met:
9 # 1. Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright notice,
12 # this list of conditions and the following disclaimer in the documentation and/or
13 # other materials provided with the distribution.
14 # 3. Neither the name of the copyright holder nor the names of its contributors
15 # may be used to endorse or promote products derived from this software without
16 # specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 # --------
29 
30 import sys
31 import rospy
32 from fsrobo_r_msgs.srv import *
33 from fsrobo_r_msgs.msg import *
34 from moveit_msgs.srv import GetPositionIK, GetPositionIKRequest
35 from fsrobo_r_driver.robot_program_interface import RobotProgramInterface
36 import json
37 
39  ROBOT_PROGRAM_DIR = '/opt/ros/scripts'
40 
41  def __init__(self, robot_name=None):
42  if robot_name:
43  self.ns = '/' + robot_name + '/'
44  else:
45  self.ns = '/'
46  self._get_position_ik = rospy.ServiceProxy(self.ns + 'solve_ik', GetPositionIK)
47  self._set_posture = rospy.ServiceProxy(self.ns + 'set_posture', SetPosture)
48  self._get_posture = rospy.ServiceProxy(self.ns + 'get_posture', GetPosture)
49  self._set_tool_offset = rospy.ServiceProxy(self.ns + 'set_tool_offset', SetToolOffset)
50 
51  self._tool = None
52  self._joint_speed = 25
53  self._line_speed = 25
54 
55  self._rpi = RobotProgramInterface(robot_name)
56 
57  def get_position_ik(self, position, orientation):
58  rospy.wait_for_service(self.ns + 'solve_ik')
59 
60  req = GetPositionIKRequest()
61  req.ik_request.pose_stamped.pose.position.x = position[0]
62  req.ik_request.pose_stamped.pose.position.y = position[1]
63  req.ik_request.pose_stamped.pose.position.z = position[2]
64  req.ik_request.pose_stamped.pose.orientation.x = orientation[0]
65  req.ik_request.pose_stamped.pose.orientation.y = orientation[1]
66  req.ik_request.pose_stamped.pose.orientation.z = orientation[2]
67  req.ik_request.pose_stamped.pose.orientation.w = orientation[3]
68  result = self._get_position_ik(req)
69 
70  if result.error_code.val == result.error_code.SUCCESS:
71  return list(result.solution.joint_state.position)
72  else:
73  return []
74 
75  def set_posture(self, posture):
76  rospy.wait_for_service(self.ns + 'set_posture')
77  self._set_posture(posture)
78 
79  def get_posture(self):
80  rospy.wait_for_service(self.ns + 'get_posture')
81  result = self._get_posture()
82  return result.posture
83 
84  def set_tool(self, tool):
85  print('set_tool!')
86  if tool:
87  self._tool = tool
88  print(self._tool.get_offset())
89  self._set_tool(1, *self._tool.get_offset())
90  else:
91  self._tool = None
92  self._set_tool(1)
93 
94  def _set_tool(self, tool_id, offx=0, offy=0, offz=0, offrz=0, offry=0, offrx=0):
95  rospy.wait_for_service(self.ns + 'set_tool_offset')
96 
97  req = SetToolOffsetRequest()
98  req.origin.x = offx
99  req.origin.y = offy
100  req.origin.z = offz
101  req.rotation.z = offrz
102  req.rotation.y = offry
103  req.rotation.x = offrx
104 
105  return self._set_tool_offset(req)
106 
107 
108  #def _set_tool(self, tool_id, offx=0, offy=0, offz=0, offrz=0, offry=0, offrx=0):
109  # param = json.dumps({
110  # 'settool': {
111  # 'id': tool_id,
112  # 'offx': offx,
113  # 'offy': offy,
114  # 'offz': offz,
115  # 'offrz': offrz,
116  # 'offry': offry,
117  # 'offrx': offrx
118  # },
119  # 'changetool': {
120  # 'tid': tool_id
121  # }
122  # })
123 
124  # return self._rpi.execute(self.ROBOT_PROGRAM_DIR + '/settool.py', param)
125 
126  def set_joint_speed(self, speed):
127  self._joint_speed = speed
128 
129  def set_line_speed(self, speed):
130  self._line_speed = speed
131 
132  def line_move(self, x, y, z, rz, ry, rx, posture=7):
133  tool_id = 1 if self._tool else 0
134  param = json.dumps({
135  'changetool': {
136  'tid': tool_id
137  },
138  'motionparam': {
139  'lin_speed': self._line_speed
140  },
141  'line': {
142  'x': x,
143  'y': y,
144  'z': z,
145  'rz': rz,
146  'ry': ry,
147  'rx': rx,
148  'posture': posture
149  }
150  })
151 
152  return self._rpi.execute(self.ROBOT_PROGRAM_DIR + '/line.py', param)
153 
154  def tool_move(self, x=0, y=0, z=0, rz=0, ry=0, rx=0):
155  tool_id = 1 if self._tool else 0
156  param = json.dumps({
157  'changetool': {
158  'tid': tool_id
159  },
160  'motionparam': {
161  'lin_speed': self._line_speed
162  },
163  'toolmove': {
164  'dx': x,
165  'dy': y,
166  'dz': z,
167  'drz': rz,
168  'dry': ry,
169  'drx': rx
170  }
171  })
172 
173  return self._rpi.execute(self.ROBOT_PROGRAM_DIR + '/toolmove.py', param)
174 
175 
176 if __name__ == '__main__':
178  print rb.get_position_ik([0.4, 0.066, 0.663], [0, 0, 0, 1])
179  rb.set_posture(5)
180  print rb.get_posture()
181  rb.set_posture(3)
182  print rb.get_posture()
183 
def _set_tool(self, tool_id, offx=0, offy=0, offz=0, offrz=0, offry=0, offrx=0)
def line_move(self, x, y, z, rz, ry, rx, posture=7)
def get_position_ik(self, position, orientation)
def tool_move(self, x=0, y=0, z=0, rz=0, ry=0, rx=0)


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