Created
November 15, 2018 13:51
-
-
Save garyservin/2b507e8b5f7ca6ebb50ce2a4e52e66cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 2.8.3) | |
project(test_issue) | |
find_package(catkin REQUIRED COMPONENTS | |
roscpp | |
std_msgs | |
) | |
catkin_package() | |
include_directories( | |
${catkin_INCLUDE_DIRS} | |
) | |
add_executable(${PROJECT_NAME}_node src/test.cpp) | |
target_link_libraries(${PROJECT_NAME}_node | |
${catkin_LIBRARIES} | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ros/ros.h" | |
#include "std_msgs/String.h" | |
#include <sstream> | |
class A | |
{ | |
public: | |
A(ros::NodeHandle& nh) | |
{ | |
sub = nh.subscribe("cmd", 10, &A::cmd_callback, this); | |
cmd_received = "none"; | |
} | |
void cmd_callback(const std_msgs::String::ConstPtr& msg) | |
{ | |
cmd_received = msg->data; | |
std::cout<< "cmd received in callback: " << cmd_received << std::endl; | |
} | |
void update() | |
{ | |
ROS_INFO_STREAM_THROTTLE(2.0, "cmd received: " << cmd_received); | |
} | |
private: | |
std::string cmd_received; | |
ros::Subscriber sub; | |
}; | |
int main(int argc, char **argv) | |
{ | |
ros::init(argc, argv, "test"); | |
ros::NodeHandle n; | |
A a(n); | |
ros::Rate loop_rate(500); | |
while (ros::ok()) | |
{ | |
a.update(); | |
ros::spinOnce(); | |
loop_rate.sleep(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment