Gazebo communicates on TCP/IP sockets Messages are sent on named channels called topics via publishers. On the other side of a topic are subscribers, which receive callbacks when messages arrive.
gz topic -l
list the topic on running system
Subscriber
Demo code subscribe to /gazebo/default/world_stats
- topic list
gz topic -l
....
/gazebo/default/world_control
/gazebo/default/world_stats
/gazebo/motor_failure_num
/gazebo/server/control
/gazebo/world/modify
- topic info
gz topic -i /gazebo/default/world_stats
Type: gazebo.msgs.WorldStatistics
Publishers:
192.168.2.200:39165
Subscribers:
- topic message view
# gz topic -e (echo)
gz topic -v /gazebo/default/world_stats
Write code
find_package(gazebo REQUIRED)
include_directories(${GAZEBO_INCLUDE_DIRS})
link_directories(${GAZEBO_LIBRARY_DIRS})
list(APPEND CMAKE_CXX_FLAGS "${GAZEBO_CXX_FLAGS}")
add_executable(sub_demo sub_demo.cpp)
target_link_libraries(sub_demo ${GAZEBO_LIBRARIES} pthread)
- sub_demo source
#include <gazebo-7/gazebo/transport/TransportIface.hh>
#include <gazebo-7/gazebo/msgs/msgs.hh>
#include <gazebo-7/gazebo/gazebo_client.hh>
#include <iostream>
#include <csignal>
const std::string TOPIC = "/gazebo/default/world_stats";
void my_shutdown(int signal){
std::cout<<"shutdown gazebo client" << std::endl;
gazebo::client::shutdown();
exit(0);
}
void cb(ConstWorldStatisticsPtr &_msg){
std::cout << _msg->DebugString() << std::endl;
}
int main(int argc, char **argv){
signal (SIGINT, my_shutdown);
gazebo::client::setup(argc, argv);
//create node for communication
gazebo::transport::NodePtr node(new gazebo::transport::Node());
node->Init();
//subscribe to topic
gazebo::transport::SubscriberPtr sub = node->Subscribe(TOPIC, cb);
while(true){
gazebo::common::Time::MSleep(100);
}
return 0;
}
No comments:
Post a Comment