gear_example_node.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright 2016 Open Source Robotics Foundation, Inc.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from __future__ import print_function
17 
18 import time
19 
20 import rospy
21 
22 from osrf_gear.msg import Goal
23 from sensor_msgs.msg import JointState
24 from std_srvs.srv import Trigger
25 from trajectory_msgs.msg import JointTrajectory
26 from trajectory_msgs.msg import JointTrajectoryPoint
27 
28 
30  rospy.loginfo("Waiting for competition to be ready...")
31  rospy.wait_for_service('/ariac/start_competition')
32  rospy.loginfo("Competition is now ready.")
33  rospy.loginfo("Requesting competition start...")
34 
35  try:
36  start = rospy.ServiceProxy('/ariac/start_competition', Trigger)
37  response = start()
38  except rospy.ServiceException as exc:
39  rospy.logerr("Failed to start the competition: %s" % exc)
40  return
41  if not response.success:
42  rospy.logerr("Failed to start the competition: %s" % response)
43  else:
44  rospy.loginfo("Competition started!")
45 
46 
48  def __init__(self):
50  rospy.Publisher("/ariac/arm/command", JointTrajectory, queue_size=10)
51  self.received_goals = []
52  self.current_joint_state = None
53  self.last_joint_state_print = time.time()
54  self.has_been_zeroed = False
55 
56  def goal_callback(self, msg):
57  rospy.loginfo("Received goal:\n" + str(msg))
58 
59  def joint_state_callback(self, msg):
60  if time.time() - self.last_joint_state_print >= 10:
61  rospy.loginfo("Current Joint States (throttled to 0.1 Hz):\n" + str(msg))
62  self.last_joint_state_print = time.time()
63  self.current_joint_state = msg
64  if not self.has_been_zeroed:
65  self.has_been_zeroed = True
66  rospy.loginfo("Sending arm to zero joint positions...")
68 
70  msg = JointTrajectory()
71  msg.joint_names = self.current_joint_state.name
72  point = JointTrajectoryPoint()
73  point.positions = [0] * len(msg.joint_names)
74  point.time_from_start = rospy.Duration(1.0)
75  msg.points = [point]
76  rospy.loginfo("Sending command:\n" + str(msg))
77  self.joint_trajectory_publisher.publish(msg)
78 
79 
80 def main():
81  rospy.init_node("gear_example_node")
82 
83  comp_class = MyCompetitionClass()
84  goal_sub = rospy.Subscriber("/ariac/orders", Goal, comp_class.goal_callback)
85  joint_state_sub = rospy.Subscriber("/joint_states", JointState, comp_class.joint_state_callback)
86 
87  rospy.loginfo("Setup complete.")
89  rospy.spin()
90 
91 if __name__ == '__main__':
92  main()


gear_example
Author(s):
autogenerated on Wed Sep 7 2016 03:48:17