image_listener.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Open Source Robotics Foundation
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 */
17 
18 /* Authors: Taehun Lim (Darby) */
19 
20 #include <gazebo/transport/transport.hh>
21 #include <gazebo/msgs/msgs.hh>
22 #include <gazebo/gazebo_client.hh>
23 
24 #include <iostream>
25 
27 // Function is called everytime a message is received.
28 void imageCallbackMsg(ConstImageStampedPtr &msg)
29 {
30  std::cout << "image width : " << msg->image().width() << std::endl;
31  std::cout << "image height : " << msg->image().height() << std::endl;
32 
33  char *data;
34  data = new char[msg->image().data().length() + 1];
35 
36  memcpy(data, msg->image().data().c_str(), msg->image().data().length());
37 
38  // Add your code
39 
40  delete data;
41 }
42 
44 int main(int _argc, char **_argv)
45 {
46  // Load gazebo
47  gazebo::client::setup(_argc, _argv);
48 
49  // Create our node for communication
50  gazebo::transport::NodePtr node(new gazebo::transport::Node());
51  node->Init();
52 
53  if (_argc < 2)
54  {
55  std::cerr << "Please put a tb3 model(`waffle` or `waffle_pi`)" << std::endl;
56  exit(0);
57  }
58 
59  char* tb3_model = _argv[1];
60  std::string topic_name = "";
61 
62  if (std::string(tb3_model) == "waffle")
63  topic_name = "/gazebo/default/user/turtlebot3_waffle/image/intel_realsense_r200/image";
64  else if (std::string(tb3_model) == "waffle_pi")
65  topic_name = "/gazebo/default/user/turtlebot3_waffle_pi/image/raspberry_pi_cam/image";
66  else
67  {
68  std::cerr << "Please put a tb3 model(`waffle` or `waffle_pi`)" << std::endl;
69  exit(0);
70  }
71 
72  std::cout << "topic name is " << topic_name << std::endl;
73 
74  // Listen to Gazebo world_stats topic
75  gazebo::transport::SubscriberPtr sub = node->Subscribe(topic_name, imageCallbackMsg);
76 
77  // Busy wait loop...replace with your own code as needed.
78  while (true)
79  gazebo::common::Time::MSleep(10);
80 
81  // Make sure to shut everything down.
82  gazebo::client::shutdown();
83 }
int main(int _argc, char **_argv)
void imageCallbackMsg(ConstImageStampedPtr &msg)


turtlebot3_gazebo_plugin
Author(s): Pyo , Darby Lim
autogenerated on Tue Mar 13 2018 02:56:08