ros中使用c++类

目录

1.创建工作空间

2.创建功能包

3.在include中创建广播publisher_class.h文件

4.在src中创建publisher_class.cpp

5.创建publisher_class_main.cpp

6.修改cmake文件

7.编译

8.运行节点roscore,source devel/setup.bash


1.创建工作空间

mkdir -p ~/test_ws/src 
cd ~/test_ws/src
catkin_init_workspace
cd ~/test_ws
catkin_make

2.创建功能包

cd ~/tets_ws/src
catkin_create_pkg example_class roscpp std_msgs std_srvs

3.在include中创建广播publisher_class.h文件

// example_ros_class.h header file //
// wsn; Feb, 2015
// include this file in "example_ros_class.cpp"
// here's a good trick--should always do this with header files:
// create a unique mnemonic for this header file, so it will get included if needed,
// but will not get included multiple times
#ifndef EXAMPLE_ROS_CLASS_H_
#define EXAMPLE_ROS_CLASS_H_

//some generically useful stuff to include...
#include <math.h>
#include <stdlib.h>
#include <string>
#include <vector>

#include <ros/ros.h> //ALWAYS need to include this

//message types used in this example code;  include more message types, as needed
#include <std_msgs/Bool.h> 
#include <std_msgs/Float32.h>
#include <std_srvs/Trigger.h> // uses the "Trigger.srv" message defined in ROS

// define a class, including a constructor, member variables and member functions
class ExampleRosClass
{
public:
    ExampleRosClass(ros::NodeHandle* nodehandle); //"main" will need to instantiate a ROS nodehandle, then pass it to the constructor
    // may choose to define public methods or public variables, if desired
private:
    // put private member data here;  "private" data will only be available to member functions of this class;
    ros::NodeHandle nh_; // we will need this, to pass between "main" and constructor
    // some objects to support subscriber, service, and publisher
    ros::Subscriber minimal_subscriber_; //these will be set up within the class constructor, hiding these ugly details
    ros::ServiceServer minimal_service_;
    ros::Publisher  minimal_publisher_;
    
    double val_from_subscriber_; //example member variable: better than using globals; convenient way to pass data from a subscriber to other member functions
    double val_to_remember_; // member variables will retain their values even as callbacks come and go
    
    // member methods as well:
    void initializeSubscribers(); // we will define some helper methods to encapsulate the gory details of initializing subscribers, publishers and services
    void initializePublishers();
    void initializeServices();
    
    void subscriberCallback(const std_msgs::Float32& message_holder); //prototype for callback of example subscriber
    //prototype for callback for example service
    bool serviceCallback(std_srvs::TriggerRequest& request, std_srvs::TriggerResponse& response);
}; // note: a class definition requires a semicolon at the end of the definition

#endif  // this closes the header-include trick...ALWAYS need one of these to match #ifndef

4.在src中创建publisher_class.cpp

#include <example_class/publisher_class.h>
//定义构造函数
ExampleRosClass::ExampleRosClass(ros::NodeHandle* nodehandle):nh_(*nodehandle)
{
   ROS_INFO("in class constructor of ExampleRosClass");
   initializeSubscribers();
   initializePublishers();
   initializeServices();
   val_to_remember_=0.0;
}
//初始化订阅函数,收到消息后调用回调函数
void ExampleRosClass::initializeSubscribers()
{
  ROS_INFO("Initializing Subscribers");
  minimal_subscriber_=nh_.subscribe("example_class_input_topic",1,&ExampleRosClass::subscriberCallback,this);//回调函数
}

//初始化服务函数,服务回调函数
void ExampleRosClass::initializeServices()
{
  ROS_INFO("Initializing Services");
  minimal_service_=nh_.advertiseService("example_minimal_service",&ExampleRosClass::serviceCallback,this);
}

//初始化广播函数
void ExampleRosClass::initializePublishers()
{
  ROS_INFO("Initializing Publishers");
  minimal_publisher_=nh_.advertise<std_msgs::Float32>("example_class_output_topic",1,true);
}
//回调函数
void ExampleRosClass::subscriberCallback(const std_msgs::Float32& message_holder)
{
  val_from_subscriber_=message_holder.data;
  ROS_INFO("myCallback activated:received value %f",val_from_subscriber_);
  std_msgs::Float32 output_msg;
  val_to_remember_+=val_from_subscriber_;
  output_msg.data=val_to_remember_;
  minimal_publisher_.publish(output_msg);
}

//服务回调函数
bool ExampleRosClass::serviceCallback(std_srvs::TriggerRequest& request,std_srvs::TriggerResponse& response)
{
  ROS_INFO("service callback activated");
  response.success=true;
  response.message="here is a response string";
  return true;
}

5.创建publisher_class_main.cpp

#include <example_class/publisher_class.h>
int main(int argc,char** argv)
{
  ros::init(argc,argv,"example_lib_test_main");
  ros::NodeHandle nh;
  ROS_INFO("main:instantiating an object of type ExampleRosClass");
  ExampleRosClass exampleRosClass(&nh);
  ROS_INFO("main:going into spin;let the callbacks do all the work");
  ros::spin();
  return 0;
}

6.修改cmake文件

cmake_minimum_required(VERSION 3.0.2)
project(example_class)
find_package(catkin REQUIRED COMPONENTS
  std_srvs
  roscpp
  std_msgs
)
catkin_package()
include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)

## Declare a C++ library
 add_library(
   publisherclass##起一个库的名字
   include/example_class/publisher_class.h##.h文件路径
   src/publisher_class.cpp ##.cpp文件路径
 )

add_executable(publisher_class src/publisher_class_main.cpp) 
#手动添加 第一个是可执行文件的文件名,后面是源,可以多个,用空格隔开
target_link_libraries(publisher_class publisherclass ${catkin_LIBRARIES}) #此可执行文件所需的链接库,第一个可执行文件名,后面为链接库,可多个,用空格隔开

7.编译

catkin_make

8.运行节点roscore,source devel/setup.bash

rosrun example_class publisher_class

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值