39 from naoqi_bridge_msgs.msg import(
40 JointTrajectoryResult,
41 JointTrajectoryAction,
43 JointAnglesWithSpeedResult,
44 JointAnglesWithSpeedAction,
45 BodyPoseWithSpeedAction,
46 BodyPoseWithSpeedGoal,
47 BodyPoseWithSpeedResult)
49 from naoqi_driver.naoqi_node
import NaoqiNode
51 from std_msgs.msg
import String
52 from std_srvs.srv
import Empty, EmptyResponse, Trigger, TriggerResponse
53 from sensor_msgs.msg
import JointState
57 NaoqiNode.__init__(self,
'pose_controller')
65 for collectionName
in [
'Head',
'LArm',
'LLeg',
'RLeg',
'RArm',
'Body',
'BodyJoints',
'BodyActuators']:
67 self.
collectionSize[collectionName] = len(self.motionProxy.getJointNames(collectionName));
70 rospy.logwarn(
'Collection %s not found on your robot.' % collectionName)
74 self.
poll_rate = int(rospy.get_param(
"~poll_rate", 0.2)*1000)
78 initStiffness = rospy.get_param(
'~init_stiffness', 0.0)
81 if initStiffness > 0.0
and initStiffness <= 1.0:
82 self.motionProxy.stiffnessInterpolation(
'Body', initStiffness, 0.5)
111 self.jointTrajectoryServer.start()
112 self.jointStiffnessServer.start()
113 self.jointAnglesServer.start()
120 self.bodyPoseWithSpeedServer.start()
122 rospy.logwarn(
"Proxy to ALRobotPosture not available, requests to body_pose_naoqi will be ignored.")
125 rospy.Subscriber(
"joint_angles", JointAnglesWithSpeed, self.
handleJointAngles, queue_size=10)
128 rospy.loginfo(
"nao_controller initialized")
131 '''(re-) connect to NaoQI''' 132 rospy.loginfo(
"Connecting to NaoQi at %s:%d", self.pip, self.pport)
146 rospy.logdebug(
"Received a joint angle target")
149 if (msg.relative==0):
150 self.motionProxy.setAngles(list(msg.joint_names), list(msg.joint_angles), msg.speed)
152 self.motionProxy.changeAngles(list(msg.joint_names), list(msg.joint_angles), msg.speed)
153 except RuntimeError,e:
154 rospy.logerr(
"Exception caught:\n%s", e)
157 rospy.logdebug(
"Received a joint angle stiffness")
159 self.motionProxy.setStiffnesses(list(msg.name), list(msg.effort))
160 except RuntimeError,e:
161 rospy.logerr(
"Exception caught:\n%s", e)
165 self.motionProxy.stiffnessInterpolation(
"Body", 1.0, 0.5)
166 rospy.loginfo(
"Body stiffness enabled")
167 return EmptyResponse()
168 except RuntimeError,e:
169 rospy.logerr(
"Exception caught:\n%s", e)
174 self.motionProxy.stiffnessInterpolation(
"Body", 0.0, 0.5)
175 rospy.loginfo(
"Body stiffness removed")
176 return EmptyResponse()
177 except RuntimeError,e:
178 rospy.logerr(
"Exception caught:\n%s", e)
183 self.motionProxy.wakeUp()
184 rospy.loginfo(
"Wake Up")
185 return EmptyResponse()
186 except RuntimeError,e:
187 rospy.logerr(
"Exception caught:\n%s", e)
192 self.motionProxy.rest()
193 rospy.loginfo(
"Rest")
194 return EmptyResponse()
195 except RuntimeError,e:
196 rospy.logerr(
"Exception caught:\n%s", e)
201 self.lifeProxy.setState(
"solitary")
202 rospy.loginfo(
"set life state to solitary")
203 return EmptyResponse()
204 except RuntimeError, e:
205 rospy.logerr(
"Exception while setting life state:\n%s", e)
210 self.lifeProxy.setState(
"disabled")
211 rospy.loginfo(
"set life state to disabled")
212 return EmptyResponse()
213 except RuntimeError, e:
214 rospy.logerr(
"Exception while disabling life state:\n%s", e)
219 res = TriggerResponse()
221 res.message = self.lifeProxy.getState()
222 rospy.loginfo(
"current life state is " + str(res.message))
224 except RuntimeError, e:
225 rospy.logerr(
"Exception while getting life state:\n%s", e)
229 """Helper, convert action goal msg to Aldebraran-style arrays for NaoQI""" 230 names = list(goal.trajectory.joint_names)
234 if len(goal.trajectory.points) == 1
and len(goal.trajectory.points[0].positions) == 1:
235 angles = goal.trajectory.points[0].positions[0]
237 angles = list(list(p.positions[i]
for p
in goal.trajectory.points)
for i
in range(0,len(goal.trajectory.points[0].positions)))
240 if not isinstance(angles, float)
and len(angles) > self.
collectionSize[
"Body"]:
241 rospy.loginfo(
"Stripping angles from %d down to %d", len(angles), self.
collectionSize[
"Body"])
248 rospy.loginfo(
"Stripping names from %d down to %d", len(names), self.
collectionSize[
"Body"])
254 times = list(p.time_from_start.to_sec()
for p
in goal.trajectory.points)
255 if len(times) == 1
and len(names) == 1:
258 times = [times]*len(names)
260 return (names, angles, times)
264 rospy.loginfo(
"JointTrajectory action executing");
268 rospy.logdebug(
"Received trajectory for joints: %s times: %s", str(names), str(times))
269 rospy.logdebug(
"Trajectory angles: %s", str(angles))
274 while running
and not self.jointTrajectoryServer.is_preempt_requested()
and not rospy.is_shutdown():
278 task_id = self.motionProxy.post.angleInterpolation(names, angles, times, (goal.relative==0))
281 running = self.motionProxy.wait(task_id, self.
poll_rate)
284 if running
and task_id:
285 self.motionProxy.stop( task_id )
287 jointTrajectoryResult = JointTrajectoryResult()
288 jointTrajectoryResult.goal_position.header.stamp = rospy.Time.now()
289 jointTrajectoryResult.goal_position.position = self.motionProxy.getAngles(names,
True)
290 jointTrajectoryResult.goal_position.name = names
293 self.jointTrajectoryServer.set_aborted(jointTrajectoryResult)
294 rospy.logerr(
"JointTrajectory action error in result: sizes mismatch")
297 self.jointTrajectoryServer.set_preempted(jointTrajectoryResult)
298 rospy.logdebug(
"JointTrajectory preempted")
301 self.jointTrajectoryServer.set_succeeded(jointTrajectoryResult)
302 rospy.loginfo(
"JointTrajectory action done")
306 rospy.loginfo(
"JointStiffness action executing");
310 rospy.logdebug(
"Received stiffness trajectory for joints: %s times: %s", str(names), str(times))
311 rospy.logdebug(
"stiffness values: %s", str(angles))
316 while running
and not self.jointStiffnessServer.is_preempt_requested()
and not rospy.is_shutdown():
320 task_id = self.motionProxy.post.stiffnessInterpolation(names, angles, times)
323 running = self.motionProxy.wait(task_id, self.
poll_rate)
326 if running
and task_id:
327 self.motionProxy.stop( task_id )
329 jointStiffnessResult = JointTrajectoryResult()
330 jointStiffnessResult.goal_position.header.stamp = rospy.Time.now()
331 jointStiffnessResult.goal_position.position = self.motionProxy.getStiffnesses(names)
332 jointStiffnessResult.goal_position.name = names
335 self.jointStiffnessServer.set_aborted(jointStiffnessResult)
336 rospy.logerr(
"JointStiffness action error in result: sizes mismatch")
339 self.jointStiffnessServer.set_preempted(jointStiffnessResult)
340 rospy.logdebug(
"JointStiffness preempted")
343 self.jointStiffnessServer.set_succeeded(jointStiffnessResult)
344 rospy.loginfo(
"JointStiffness action done")
349 names = list(goal.joint_angles.joint_names)
350 angles = list(goal.joint_angles.joint_angles)
351 rospy.logdebug(
"Received JointAnglesWithSpeed for joints: %s angles: %s", str(names), str(angles))
353 if goal.joint_angles.relative == 1:
355 currentAngles = self.motionProxy.getAngles(names,
True)
356 angles = list(map(
lambda x,y: x+y, angles, currentAngles))
361 while running
and not self.jointAnglesServer.is_preempt_requested()
and not rospy.is_shutdown():
365 task_id = self.motionProxy.post.angleInterpolationWithSpeed(names, angles, goal.joint_angles.speed)
368 running = self.motionProxy.wait(task_id, self.
poll_rate)
371 if running
and task_id:
372 self.motionProxy.stop( task_id )
374 jointAnglesWithSpeedResult = JointAnglesWithSpeedResult()
375 jointAnglesWithSpeedResult.goal_position.header.stamp = rospy.Time.now()
376 jointAnglesWithSpeedResult.goal_position.position = self.motionProxy.getAngles(names,
True)
377 jointAnglesWithSpeedResult.goal_position.name = names
379 if not self.
checkJointsLen(jointAnglesWithSpeedResult.goal_position):
380 self.jointAnglesServer.set_aborted(jointAnglesWithSpeedResult)
381 rospy.logerr(
"JointAnglesWithSpeed action error in result: sizes mismatch")
384 self.jointAnglesServer.set_preempted(jointAnglesWithSpeedResult)
385 rospy.logdebug(
"JointAnglesWithSpeed preempted")
388 self.jointAnglesServer.set_succeeded(jointAnglesWithSpeedResult)
389 rospy.loginfo(
"JointAnglesWithSpeed action done")
392 if len(goal_position.name) == 1
and self.collectionSize.has_key(goal_position.name[0]):
393 return len(goal_position.position) == self.
collectionSize[goal_position.name[0]]
395 return len(goal_position.position) == len(goal_position.name)
400 if (goal.speed < 0.0)
or (goal.speed > 1.0):
401 bodyPoseWithSpeedResult = BodyPoseWithSpeedResult()
402 self.bodyPoseWithSpeedServer.set_aborted(bodyPoseWithSpeedResult)
403 rospy.logerr(
"Body pose setter: Not a valid speed value.")
406 valid_postures = self.robotPostureProxy.getPostureList()
408 if goal.posture_name
not in valid_postures:
409 bodyPoseWithSpeedResult = BodyPoseWithSpeedResult()
410 self.bodyPoseWithSpeedServer.set_aborted(bodyPoseWithSpeedResult)
411 rospy.logerr(
"Body pose setter: Not a valid posture.")
416 self.motionProxy.stiffnessInterpolation(
"Body", 1.0, 0.5)
417 rospy.loginfo(
"Body stiffness enabled")
418 except RuntimeError,e:
419 rospy.logerr(
"Exception caught:\n%s", e)
423 self.robotPostureProxy.goToPosture(goal.posture_name, goal.speed)
425 self.bodyPoseWithSpeedServer.set_succeeded()
428 while self.is_looping():
430 if self.getLifeStatePub.get_num_connections() > 0:
431 get_life_state_msg = String()
432 get_life_state_msg.data = self.lifeProxy.getState()
433 self.getLifeStatePub.publish(get_life_state_msg)
435 except RuntimeError, e:
436 print "Error accessing ALMotion, ALRobotPosture, ALAutonomousLife, exiting...\n" 438 rospy.signal_shutdown(
"No NaoQI available anymore")
442 if __name__ ==
'__main__':
446 rospy.loginfo(
"nao pose_controller running...")
449 rospy.loginfo(
"nao pose_controller stopped.")
def handleStiffnessSrv(self, req)
def executeJointTrajectoryAction(self, goal)
def handleLifeSrv(self, req)
def handleWakeUpSrv(self, req)
def handleStiffnessOffSrv(self, req)
def executeJointAnglesWithSpeedAction(self, goal)
def handleLifeOffSrv(self, req)
def handleRestSrv(self, req)
def handleGetLifeSrv(self, req)
def jointTrajectoryGoalMsgToAL(self, goal)
def executeJointStiffnessAction(self, goal)
def handleJointStiffness(self, msg)
def handleJointAngles(self, msg)
def checkJointsLen(self, goal_position)
def executeBodyPoseWithSpeed(self, goal)