gear_example_node.cpp
Go to the documentation of this file.
1 // Copyright 2016 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // %Tag(FULLTEXT)%
16 // %Tag(INCLUDE_STATEMENTS)%
17 #include <algorithm>
18 #include <vector>
19 
20 #include <ros/ros.h>
21 
22 #include <osrf_gear/Goal.h>
23 #include <osrf_gear/LogicalCameraImage.h>
24 #include <osrf_gear/Proximity.h>
25 #include <sensor_msgs/JointState.h>
26 #include <sensor_msgs/LaserScan.h>
27 #include <std_msgs/Float32.h>
28 #include <std_msgs/String.h>
29 #include <std_srvs/Trigger.h>
30 #include <trajectory_msgs/JointTrajectory.h>
31 // %EndTag(INCLUDE_STATEMENTS)%
32 
33 // %Tag(START_COMP)%
36  // Create a Service client for the correct service, i.e. '/ariac/start_competition'.
37  ros::ServiceClient start_client =
38  node.serviceClient<std_srvs::Trigger>("/ariac/start_competition");
39  // If it's not already ready, wait for it to be ready.
40  // Calling the Service using the client before the server is ready would fail.
41  if (!start_client.exists()) {
42  ROS_INFO("Waiting for the competition to be ready...");
43  start_client.waitForExistence();
44  ROS_INFO("Competition is now ready.");
45  }
46  ROS_INFO("Requesting competition start...");
47  std_srvs::Trigger srv; // Combination of the "request" and the "response".
48  start_client.call(srv); // Call the start Service.
49  if (!srv.response.success) { // If not successful, print out why.
50  ROS_ERROR_STREAM("Failed to start the competition: " << srv.response.message);
51  } else {
52  ROS_INFO("Competition started!");
53  }
54 }
55 // %EndTag(START_COMP)%
56 
59 {
60 public:
62  : current_score_(0), has_been_zeroed_(false)
63  {
64  // %Tag(ADV_CMD)%
65  joint_trajectory_publisher_ = node.advertise<trajectory_msgs::JointTrajectory>(
66  "/ariac/arm/command", 10);
67  // %EndTag(ADV_CMD)%
68  }
69 
71  void current_score_callback(const std_msgs::Float32::ConstPtr & msg) {
72  if (msg->data != current_score_)
73  {
74  ROS_INFO_STREAM("Score: " << msg->data);
75  }
76  current_score_ = msg->data;
77  }
78 
80  void competition_state_callback(const std_msgs::String::ConstPtr & msg) {
81  if (msg->data == "done" && competition_state_ != "done")
82  {
83  ROS_INFO("Competition ended.");
84  }
85  competition_state_ = msg->data;
86  }
87 
89  void goal_callback(const osrf_gear::Goal::ConstPtr & goal_msg) {
90  ROS_INFO_STREAM("Received goal:\n" << *goal_msg);
91  received_goals_.push_back(*goal_msg);
92  }
93 
94  // %Tag(CB_CLASS)%
97  const sensor_msgs::JointState::ConstPtr & joint_state_msg)
98  {
100  "Joint States (throttled to 0.1 Hz):\n" << *joint_state_msg);
101  // ROS_INFO_STREAM("Joint States:\n" << *joint_state_msg);
102  current_joint_states_ = *joint_state_msg;
103  if (!has_been_zeroed_) {
104  has_been_zeroed_ = true;
105  ROS_INFO("Sending arm to zero joint positions...");
107  }
108  }
109  // %EndTag(CB_CLASS)%
110 
111  // %Tag(ARM_ZERO)%
114  // Create a message to send.
115  trajectory_msgs::JointTrajectory msg;
116  // Copy the joint names from the msg off the '/ariac/joint_states' topic.
117  msg.joint_names = current_joint_states_.name;
118  // Create one point in the trajectory.
119  msg.points.resize(1);
120  // Resize the vector to the same length as the joint names.
121  // Values are initialized to 0.
122  msg.points[0].positions.resize(current_joint_states_.name.size(), 0.0);
123  // How long to take getting to the point (floating point seconds).
124  msg.points[0].time_from_start = ros::Duration(0.001);
125  ROS_INFO_STREAM("Sending command:\n" << msg);
127  }
128  // %EndTag(ARM_ZERO)%
129 
132  const osrf_gear::LogicalCameraImage::ConstPtr & image_msg)
133  {
135  "Logical camera: '" << image_msg->models.size() << "' objects.");
136  }
137 
139  void break_beam_callback(const osrf_gear::Proximity::ConstPtr & msg) {
140  if (msg->object_detected) { // If there is an object in proximity.
141  ROS_INFO("Break beam triggered.");
142  }
143  }
144 
145 private:
146  std::string competition_state_;
149  std::vector<osrf_gear::Goal> received_goals_;
150  sensor_msgs::JointState current_joint_states_;
152 };
153 
154 void proximity_sensor_callback(const osrf_gear::Proximity::ConstPtr & msg) {
155  if (msg->object_detected) { // If there is an object in proximity.
156  ROS_INFO("Proximity sensor triggered.");
157  }
158 }
159 
160 void laser_profiler_callback(const sensor_msgs::LaserScan::ConstPtr & msg) {
161  size_t number_of_valid_ranges = std::count_if(
162  msg->ranges.begin(), msg->ranges.end(), std::isfinite<float>);
163  if (number_of_valid_ranges > 0) {
164  ROS_INFO_THROTTLE(1, "Laser profiler sees something.");
165  }
166 }
167 
168 // %Tag(MAIN)%
169 int main(int argc, char ** argv) {
170  // Last argument is the default name of the node.
171  ros::init(argc, argv, "gear_example_node");
172 
173  ros::NodeHandle node;
174 
175  // Instance of custom class from above.
176  MyCompetitionClass comp_class(node);
177 
178  // Subscribe to the '/ariac/current_score' topic.
179  ros::Subscriber current_score_subscriber = node.subscribe(
180  "/ariac/current_score", 10,
182 
183  // Subscribe to the '/ariac/competition_state' topic.
184  ros::Subscriber competition_state_subscriber = node.subscribe(
185  "/ariac/competition_state", 10,
187 
188  // %Tag(SUB_CLASS)%
189  // Subscribe to the '/ariac/orders' topic.
190  ros::Subscriber goals_subscriber = node.subscribe(
191  "/ariac/orders", 10,
192  &MyCompetitionClass::goal_callback, &comp_class);
193  // %EndTag(SUB_CLASS)%
194 
195  // Subscribe to the '/ariac/joint_states' topic.
196  ros::Subscriber joint_state_subscriber = node.subscribe(
197  "/ariac/joint_states", 10,
199 
200  // %Tag(SUB_FUNC)%
201  // Subscribe to the '/ariac/proximity_sensor_1_changed' topic.
202  ros::Subscriber proximity_sensor_subscriber = node.subscribe(
203  "/ariac/proximity_sensor_1_changed", 10, proximity_sensor_callback);
204  // %EndTag(SUB_FUNC)%
205 
206  // Subscribe to the '/ariac/break_beam_1_changed' topic.
207  ros::Subscriber break_beam_subscriber = node.subscribe(
208  "/ariac/break_beam_1_changed", 10,
210 
211  // Subscribe to the '/ariac/logical_camera_1' topic.
212  ros::Subscriber logical_camera_subscriber = node.subscribe(
213  "/ariac/logical_camera_1", 10,
215 
216  // Subscribe to the '/ariac/laser_profiler_1' topic.
217  ros::Subscriber laser_profiler_subscriber = node.subscribe(
218  "/ariac/laser_profiler_1", 10, laser_profiler_callback);
219 
220  ROS_INFO("Setup complete.");
221  start_competition(node);
222  ros::spin(); // This executes callbacks on new data until ctrl-c.
223 
224  return 0;
225 }
226 // %EndTag(MAIN)%
227 // %EndTag(FULLTEXT)%
msg
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
void start_competition(ros::NodeHandle &node)
Start the competition by waiting for and then calling the start ROS Service.
void proximity_sensor_callback(const osrf_gear::Proximity::ConstPtr &msg)
void publish(const boost::shared_ptr< M > &message) const
void current_score_callback(const std_msgs::Float32::ConstPtr &msg)
Called when a new message is received.
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void laser_profiler_callback(const sensor_msgs::LaserScan::ConstPtr &msg)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
void break_beam_callback(const osrf_gear::Proximity::ConstPtr &msg)
Called when a new Proximity message is received.
Example class that can hold state and provide methods that handle incoming data.
std::vector< osrf_gear::Goal > received_goals_
sensor_msgs::JointState current_joint_states_
void logical_camera_callback(const osrf_gear::LogicalCameraImage::ConstPtr &image_msg)
Called when a new LogicalCameraImage message is received.
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
void send_arm_to_zero_state()
Create a JointTrajectory with all positions set to zero, and command the arm.
#define ROS_INFO(...)
ros::Publisher joint_trajectory_publisher_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ROSCPP_DECL void spin()
MyCompetitionClass(ros::NodeHandle &node)
#define ROS_INFO_STREAM(args)
void goal_callback(const osrf_gear::Goal::ConstPtr &goal_msg)
Called when a new Goal message is received.
std::string competition_state_
#define ROS_INFO_THROTTLE(rate,...)
void joint_state_callback(const sensor_msgs::JointState::ConstPtr &joint_state_msg)
Called when a new JointState message is received.
#define ROS_ERROR_STREAM(args)
#define ROS_INFO_STREAM_THROTTLE(rate, args)
void competition_state_callback(const std_msgs::String::ConstPtr &msg)
Called when a new message is received.
int main(int argc, char **argv)


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