|
28 | 28 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29 | 29 | # POSSIBILITY OF SUCH DAMAGE.
|
30 | 30 |
|
31 |
| -import numpy, openravepy, rospy |
| 31 | +import openravepy |
32 | 32 | from manipulator import Manipulator
|
33 |
| -from prpy.clone import Clone |
34 |
| -from .. import util |
35 |
| -from .. import exceptions |
36 |
| -from IPython import embed |
37 | 33 |
|
38 | 34 | class Mico(Manipulator):
|
39 |
| - def _load_controllers(self, controllers, timeout=10): |
40 |
| - from controller_manager_msgs.srv import SwitchController, ListControllers |
41 |
| - """Load a list of ros_control controllers by name.""" |
42 |
| - |
43 |
| - rospy.wait_for_service('controller_manager/switch_controller', timeout=10) |
44 |
| - rospy.wait_for_service('controller_manager/list_controllers', timeout=10) |
45 |
| - |
46 |
| - switch_controllers = rospy.ServiceProxy( |
47 |
| - 'controller_manager/switch_controller', SwitchController) |
48 |
| - list_controllers = rospy.ServiceProxy( |
49 |
| - 'controller_manager/list_controllers', ListControllers) |
50 |
| - |
51 |
| - running = set([c.name for c in list_controllers().controller if c.state == "running"]) |
52 |
| - controllers = set(controllers) |
53 |
| - switch_controllers(list(controllers - running), list(running - controllers), 2) |
54 |
| - |
55 |
| - def _unload_controllers(self, controllers): |
56 |
| - from controller_manager_msgs.srv import SwitchController, ListControllers |
57 |
| - """Unload a list of ros_control controllers by name""" |
58 |
| - |
59 |
| - rospy.wait_for_service('controller_manager/switch_controller') |
60 |
| - rospy.wait_for_service('controller_manager/list_controllers') |
61 |
| - |
62 |
| - switch_controllers = rospy.ServiceProxy( |
63 |
| - 'controller_manager/switch_controller', SwitchController) |
64 |
| - list_controllers = rospy.ServiceProxy( |
65 |
| - 'controller_manager/list_controllers', ListControllers) |
66 |
| - |
67 |
| - running = set([c.name for c in list_controllers().controller |
68 |
| - if c.state == "running"]) |
69 |
| - controllers = set(controllers) |
70 |
| - switch_controllers([], list(controllers & running), 2) |
71 |
| - |
72 |
| - |
73 |
| - def __init__(self, sim, controller_namespace, |
| 35 | + def __init__(self, sim, |
74 | 36 | iktype=openravepy.IkParameterization.Type.Transform6D):
|
75 | 37 | Manipulator.__init__(self)
|
76 | 38 |
|
77 | 39 | self.simulated = sim
|
78 |
| - # TODO: learn how to attach an ideal controller controller |
79 |
| - #self.controller = self.GetRobot().AttachController(name=self.GetName(), |
80 |
| - #args='OWDController {0:s} {1:s}'.format('prpy', owd_namespace), |
81 |
| - #dof_indices=self.GetArmIndices(), affine_dofs=0, simulated=sim) |
82 |
| - # self.controller = self.GetRobot().AttachController(name=self.GetName(), |
83 |
| - # args='MicoController {0:s} {1:s}'.format('prpy', controller_namespace), |
84 |
| - # dof_indices=self.GetArmIndices(), affine_dofs=0, simulated=sim) |
85 |
| - # Load the IK database. |
86 |
| - robot = self.GetRobot() |
87 |
| - if (sim == True): |
88 |
| - self.controller = self.GetRobot().AttachController(name=self.GetName(), |
89 |
| - args='idealcontroller'.format('prpy', controller_namespace), |
90 |
| - dof_indices=self.GetArmIndices(), affine_dofs=0, simulated=sim) |
91 |
| - else: |
92 |
| - #self.controller = self.GetRobot().AttachController(name=self.GetName(), |
93 |
| - #args='roscontroller openrave {0} 1'.format(rospy.get_namespace()), |
| 40 | + self.iktype = iktype |
94 | 41 |
|
95 |
| - self.or_physical_controller = ('roscontroller openrave {0} 1'.format(rospy.get_namespace())) |
96 |
| - self.ros_controllers = [ |
97 |
| - "traj_controller", |
98 |
| - "joint_state_controller", |
99 |
| - ] |
100 |
| - or_controller_string = self.or_physical_controller |
101 |
| - self._load_controllers(self.ros_controllers) |
102 |
| - env = robot.GetEnv() |
103 |
| - with env: |
104 |
| - self.controller = openravepy.RaveCreateController(env, |
105 |
| - or_controller_string) |
106 |
| - m = robot.GetActiveDOFIndices() |
107 |
| - c = self.controller |
108 |
| - robot.SetController(c, m, 0) |
109 |
| - |
110 |
| - #self.hand_controller = self.GetRobot().AttachController(name=self.GetRobot().GetName(), |
111 |
| - #args='MicoHandController {0:s} {1:s}'.format('prpy', '/mico_hand_controller'), |
112 |
| - #dof_indices=self.GetGripperIndices(), affine_dofs=0, simulated=sim) |
| 42 | + robot = self.GetRobot() |
| 43 | + env = robot.GetEnv() |
113 | 44 |
|
114 |
| - |
| 45 | + with env: |
| 46 | + dof_indices = self.GetIndices() |
| 47 | + accel_limits = robot.GetDOFAccelerationLimits() |
| 48 | + accel_limits[dof_indices[0:3]] = 1.2 |
| 49 | + accel_limits[dof_indices[3:6]] = 1.0 |
| 50 | + robot.SetDOFAccelerationLimits(accel_limits) |
115 | 51 |
|
116 |
| - ''' |
| 52 | + # Load or_nlopt_ik as the IK solver. Unfortunately, IKFast doesn't work |
| 53 | + # on the Mico. |
117 | 54 | if iktype is not None:
|
118 |
| - with robot: |
119 |
| - self.SetActive() |
120 |
| - self.ikmodel = openravepy.databases.inversekinematics.InverseKinematicsModel(robot, iktype=iktype) |
121 |
| - if not self.ikmodel.load(): |
122 |
| - self.ikmodel.autogenerate() |
123 |
| - ''' |
124 |
| - # Enable servo motions in simulation mode. |
125 |
| - from prpy.simulation import ServoSimulator |
126 |
| - self.servo_simulator = ServoSimulator(self, rate=20, watchdog_timeout=0.1) |
127 |
| - |
128 |
| - def CloneBindings(self, parent): |
129 |
| - self.__init__(True, None) |
130 |
| - |
131 |
| - def PlanToNamedConfiguration(self, name, execute=True, **kw_args): |
132 |
| - """ |
133 |
| - Plan this arm to saved configuration stored in robot.configurations by |
134 |
| - ignoring any other DOFs specified in the named configuration. |
135 |
| - @param name name of a saved configuration |
136 |
| - @param **kw_args optional arguments passed to PlanToConfiguration |
137 |
| - @returns traj trajectory |
138 |
| - """ |
139 |
| - robot = self.GetRobot() |
140 |
| - saved_dof_indices, saved_dof_values = robot.configurations.get_configuration(name) |
141 |
| - |
142 |
| - with Clone(robot.GetEnv()) as cloned_env: |
143 |
| - cloned_env.Cloned(self).SetActive() |
144 |
| - cloned_robot = cloned_env.Cloned(robot) |
| 55 | + self.iksolver = openravepy.RaveCreateIkSolver(env, 'NloptIK') |
| 56 | + self.SetIKSolver(self.iksolver) |
145 | 57 |
|
146 |
| - arm_dof_indices = cloned_robot.GetActiveDOFIndices() |
147 |
| - arm_dof_values = cloned_robot.GetActiveDOFValues() |
| 58 | + # Load simulation controllers. |
| 59 | + if sim: |
| 60 | + from prpy.simulation import ServoSimulator |
148 | 61 |
|
149 |
| - for arm_dof_index, arm_dof_value in zip(saved_dof_indices, saved_dof_values): |
150 |
| - if arm_dof_index in arm_dof_indices: |
151 |
| - i = list(arm_dof_indices).index(arm_dof_index) |
152 |
| - arm_dof_values[i] = arm_dof_value |
| 62 | + self.controller = robot.AttachController( |
| 63 | + self.GetName(), '', self.GetArmIndices(), 0, True) |
| 64 | + self.servo_simulator = ServoSimulator(self, rate=20, |
| 65 | + watchdog_timeout=0.1) |
153 | 66 |
|
154 |
| - traj = cloned_robot.PlanToConfiguration(arm_dof_values, execute=False, **kw_args) |
| 67 | + def CloneBindings(self, parent): |
| 68 | + super(Mico, self).CloneBindings(parent) |
155 | 69 |
|
156 |
| - # Copy the trajectory back to the original environment. |
157 |
| - from ..util import CopyTrajectory |
158 |
| - live_traj = CopyTrajectory(traj, env=robot.GetEnv()) |
| 70 | + self.simulated = True |
| 71 | + self.iktype = parent.iktype |
159 | 72 |
|
160 |
| - if execute: |
161 |
| - return robot.ExecuteTrajectory(live_traj, **kw_args) |
162 |
| - else: |
163 |
| - return live_traj |
| 73 | + self.servo_simulator = None |
164 | 74 |
|
165 |
| - def SetStiffness(manipulator, stiffness): |
| 75 | + # TODO: This is broken on nlopt_ik |
166 | 76 | """
|
167 |
| - Set the Mico's stiffness. This enables or disables gravity compensation. |
168 |
| - Values between 0 and 1 are experimental. |
169 |
| - @param stiffness value between 0.0 and 1.0 |
| 77 | + if parent.iktype is not None: |
| 78 | + self.iksolver = openravepy.RaveCreateIkSolver(env, 'NloptIK') |
| 79 | + self.SetIKSolver(self.iksolver) |
170 | 80 | """
|
171 |
| - if not (0 <= stiffness <= 1): |
172 |
| - raise Exception('Stiffness must in the range [0, 1]; got %f.' % stiffness) |
173 |
| - |
174 |
| - if not manipulator.simulated: |
175 |
| - manipulator.controller.SendCommand('SetStiffness {0:f}'.format(stiffness)) |
176 | 81 |
|
177 |
| - def Servo(manipulator, velocities): |
| 82 | + def Servo(self, velocities): |
178 | 83 | """
|
179 | 84 | Servo with an instantaneous vector of joint velocities.
|
180 | 85 | @param velocities instantaneous joint velocities in radians per second
|
181 | 86 | """
|
182 |
| - num_dof = len(manipulator.GetArmIndices()) |
183 |
| - if len(velocities) != num_dof: |
184 |
| - raise ValueError('Incorrect number of joint velocities. Expected {0:d}; got {0:d}.'.format( |
185 |
| - num_dof, len(velocities))) |
186 |
| - |
187 |
| - if not manipulator.simulated: |
188 |
| - manipulator.controller.SendCommand('Servo ' + ' '.join([ str(qdot) for qdot in velocities ])) |
189 |
| - else: |
190 |
| - manipulator.controller.Reset(0) |
191 |
| - manipulator.servo_simulator.SetVelocity(velocities) |
192 |
| - |
193 |
| - def ServoTo(manipulator, target, duration, timeStep = 0.05, collisionChecking= True): |
194 |
| - """ |
195 |
| - Servo's the Mico to the target taking the duration passed to it |
196 |
| - @param target desired joint angles |
197 |
| - @param duration duration in seconds |
198 |
| - @param timestep period of the control loop |
199 |
| - @param collisionchecking check collisions in the simulation environment |
200 |
| - """ |
201 |
| - steps = int(math.ceil(duration/timeStep)) |
202 |
| - original_dofs = manipulator.GetRobot().GetDOFValues(manipulator.GetArmIndices()) |
203 |
| - velocity = numpy.array(target-manipulator.GetRobot().GetDOFValues(manipulator.GetArmIndices())) |
204 |
| - velocities = v/steps#[v/steps for v in velocity] |
205 |
| - inCollision = False |
206 |
| - if collisionChecking==True: |
207 |
| - inCollision = manipulator.CollisionCheck(target) |
208 |
| - if inCollision == False: |
209 |
| - for i in range(1,steps): |
210 |
| - manipulator.Servo(velocities) |
211 |
| - time.sleep(timeStep) |
212 |
| - manipulator.Servo([0] * len(manipulator.GetArmIndices())) |
213 |
| - new_dofs = manipulator.GetRobot().GetDOFValues(manipulator.GetArmIndices()) |
214 |
| - return True |
215 |
| - return False |
| 87 | + num_dof = len(self.GetArmIndices()) |
216 | 88 |
|
217 |
| - def SetVelocityLimits(self, velocity_limits, min_accel_time, |
218 |
| - openrave=True, owd=True): |
219 |
| - """ |
220 |
| - Change the OpenRAVE and OWD joint velocity limits. Joint velocities |
221 |
| - that exceed these limits will trigger a velocity fault. |
222 |
| - @param velocity_limits vector of joint velocity limits in radians per second |
223 |
| - @param min_accel_time minimum acceleration time used to compute acceleration limits |
224 |
| - @param openrave flag to set the OpenRAVE velocity limits |
225 |
| - @param owd flag to set the OWD velocity limits |
226 |
| - """ |
227 |
| - # Update the OpenRAVE limits. |
228 |
| - if openrave: |
229 |
| - Manipulator.SetVelocityLimits(self, velocity_limits, min_accel_time) |
230 |
| - |
231 |
| - # Update the OWD limits. |
232 |
| - if owd and not self.simulated: |
233 |
| - args = [ 'SetSpeed' ] |
234 |
| - args += [ str(min_accel_time) ] |
235 |
| - args += [ str(velocity) for velocity in velocity_limits ] |
236 |
| - args_str = ' '.join(args) |
237 |
| - self.controller.SendCommand(args_str) |
| 89 | + if len(velocities) != num_dof: |
| 90 | + raise ValueError( |
| 91 | + 'Incorrect number of joint velocities.' |
| 92 | + ' Expected {:d}; got {:d}.'.format(num_dof, len(velocities))) |
238 | 93 |
|
239 |
| - def GetTrajectoryStatus(manipulator): |
240 |
| - """ |
241 |
| - Gets the status of the current (or previous) trajectory executed by the |
242 |
| - controller. |
243 |
| - @return status of the current (or previous) trajectory executed |
244 |
| - """ |
245 |
| - if not manipulator.simulated: |
246 |
| - return manipulator.controller.SendCommand('GetStatus') |
| 94 | + if self.simulated: |
| 95 | + self.GetRobot().GetController().Reset(0) |
| 96 | + self.servo_simulator.SetVelocity(velocities) |
247 | 97 | else:
|
248 |
| - if manipulator.controller.IsDone(): |
249 |
| - return 'done' |
250 |
| - else: |
251 |
| - return 'active' |
252 |
| - |
253 |
| - def ClearTrajectoryStatus(manipulator): |
254 |
| - """ |
255 |
| - Clears the current trajectory execution status. |
256 |
| - """ |
257 |
| - if not manipulator.simulated: |
258 |
| - manipulator.controller.SendCommand('ClearStatus') |
| 98 | + raise NotImplementedError('Servo is not implemented.') |
0 commit comments