【ROSチュートリアル】ROS共通APIの解説


1. ノードの初期化

ROSCPP_DECL void init(int &argc, char **argv, const std::string& name, uint32_t options = 0);
ROSCPP_DECL void init(const M_string& remappings, const std::string& name, uint32_t options = 0);
ROSCPP_DECL void init(const VP_string& remapping_args, const std::string& name, uint32_t options = 0);
  • 最初の初期化メソッドは次のとおりです
パラメータ 意味
argc main関数の最初のパラメータ
引数 main関数の2番目のパラメータ
名前 ノードの名前は一意である必要があります

2. トピックコミュニケーション

2.1 パブリッシャーオブジェクトの作成

    template <class M>
    Publisher NodeHandle::advertise(const std::string& topic, uint32_t queue_size, bool latch = false);
    
  template <class M>
  Publisher NodeHandle::advertise(const std::string& topic, uint32_t queue_size,
                            const SubscriberStatusCallback& connect_cb,
                            const SubscriberStatusCallback& disconnect_cb = SubscriberStatusCallback(),
                            const VoidConstPtr& tracked_object = VoidConstPtr(),
                            bool latch = false);
  • ここでは最初の施工方法について説明します。
パラメータ 意味
トピック トピック名は一意である必要があります
キューサイズ サブスクライバへの送信を待機しているメッセージの最大数
ラッチ true の場合、トピックによって公開された最後のメッセージが保存され、後でサブスクライバーが接続したときにメッセージがサブスクライバーに送信されます。

2.2 ニュースリリース

template <typename M>
      void Publisher::publish(const M& message) const;
パラメータ 意味
メッセージ 整理されたメッセージ

2.3 加入者オブジェクトの作成

  template<class M>
  Subscriber NodeHandle::subscribe(const std::string& topic, uint32_t queue_size, void(*fp)(const boost::shared_ptr<M const>&), const TransportHints& transport_hints = TransportHints());
  
  template<class M>
  Subscriber NodeHandle::subscribe(const std::string& topic, uint32_t queue_size, const boost::function<void (const boost::shared_ptr<M const>&)>& callback,
                             const VoidConstPtr& tracked_object = VoidConstPtr(), const TransportHints& transport_hints = TransportHints());
パラメータ 意味
トピック トピック名は一意である必要があります
キューサイズ main関数の2番目のパラメータ
FP コールバック関数の関数ポインタ
戻る 呼び出しが成功すると加入者オブジェクトが返され、呼び出しが失敗すると空のオブジェクトが返されます。

3.サービスコミュニケーション

3.1 サービスオブジェクトの作成

  template<class T, class MReq, class MRes>
  ServiceServer NodeHandle::advertiseService(const std::string& service, bool(T::*srv_func)(MReq &, MRes &), T *obj);
  
  template<class T, class MReq, class MRes>
  ServiceServer NodeHandle::advertiseService(const std::string& service, bool(T::*srv_func)(ServiceEvent<MReq, MRes>&), T *obj);

  template<class T, class MReq, class MRes>
  ServiceServer NodeHandle::advertiseService(const std::string& service, bool(T::*srv_func)(MReq &, MRes &), const boost::shared_ptr<T>& obj);

  template<class T, class MReq, class MRes>
  ServiceServer NodeHandle::advertiseService(const std::string& service, bool(T::*srv_func)(ServiceEvent<MReq, MRes>&), const boost::shared_ptr<T>& obj);

  template<class MReq, class MRes>
  ServiceServer NodeHandle::advertiseService(const std::string& service, bool(*srv_func)(MReq&, MRes&));

  template<class MReq, class MRes>
  ServiceServer NodeHandle::advertiseService(const std::string& service, bool(*srv_func)(ServiceEvent<MReq, MRes>&));
  
  template<class MReq, class MRes>
  ServiceServer NodeHandle::advertiseService(const std::string& service, const boost::function<bool(MReq&, MRes&)>& callback, 
                                 const VoidConstPtr& tracked_object = VoidConstPtr());

  template<class S>
  ServiceServer NodeHandle::advertiseService(const std::string& service, const boost::function<bool(S&)>& callback, 
                                 const VoidConstPtr& tracked_object = VoidConstPtr());
パラメータ 意味
サービス サービス名は一意である必要があります
srv_func リクエストを受信した場合、コールバック関数を使用してリクエストを処理する必要があります
戻る リクエストが成功するとサービス オブジェクトが返され、それ以外の場合は空のオブジェクトが返されます。

3.2 顧客オブジェクトの作成

  template<class MReq, class MRes>
  ServiceClient NodeHandle::serviceClient(const std::string& service_name, bool persistent = false, 
                              const M_string& header_values = M_string());
  template<class Service>
  ServiceClient NodeHandle::serviceClient(const std::string& service_name, bool persistent = false, 
                              const M_string& header_values = M_string());
                              
パラメータ 意味
サービス名 サービス名は一意である必要があります

3.3 クライアントがリクエストを送信する

  template<class Service>
  bool ServiceClient::call(Service& service);
パラメータ 意味
サービス .srv ファイルで定義されたサービス タイプ

3.4 サービスを待機しているクライアント オブジェクト

ROSCPP_DECL bool service::waitForService(const std::string& service_name, int32_t timeout);
ROSCPP_DECL bool service::waitForService(const std::string& service_name, ros::Duration timeout = ros::Duration(-1));
パラメータ 意味
サービス名 待機中のサービスの名前は一意である必要があります
タイムアウト 最大待機時間。デフォルトは -1 です。ノードがシャットダウンされるまで永遠に待機できます。
戻る 成功した場合は true を返し、それ以外の場合は false を返します

4. ロータリー機能

  • つまり、コールバック関数を使用する場合は、ラウンドアバウト関数を使用して処理する必要があります。

4.1スピン

/** 
 * \brief 进入循环处理回调 
 */
ROSCPP_DECL void spin();

4.2 スピンワンス

/**
 * \brief 处理一轮回调
 *
 * 一般应用场景:
 *     在循环体内,处理所有可用的回调函数
 * 
 */
ROSCPP_DECL void spinOnce();

5.時間

5.1 瞬間

5.1.1 現在の瞬間を取得する

ros::init(argc,argv,"hello_time");
ros::NodeHandle nh;//必须创建句柄,否则时间没有初始化,导致后续API调用失败
ros::Time right_now = ros::Time::now();//将当前时刻封装成对象
ROS_INFO("当前时刻:%.2f",right_now.toSec());//获取距离 1970年01月01日 00:00:00 的秒数
ROS_INFO("当前时刻:%d",right_now.sec);//获取距离 1970年01月01日 00:00:00 的秒数

5.1.2 時刻の設定

ros::init(argc,argv,"hello_time");
ros::NodeHandle nh;//必须创建句柄,否则时间没有初始化,导致后续API调用失败
ros::Time someTime(100,100000000);// 参数1:秒数  参数2:纳秒
ROS_INFO("时刻:%.2f",someTime.toSec()); //100.10
ros::Time someTime2(100.3);//直接传入 double 类型的秒数
ROS_INFO("时刻:%.2f",someTime2.toSec()); //100.30

5.2 時間間隔

5.2.1 時間間隔の設定

ros::init(argc,argv,"hello_time");
ros::NodeHandle nh;//必须创建句柄,否则时间没有初始化,导致后续API调用失败
ros::Duration du(10);//持续10秒钟,参数是double类型的,以秒为单位
ROS_INFO("持续时间:%.2f",du.toSec());//将持续时间换算成秒

5.2.2 休止状態

ros::init(argc,argv,"hello_time");
ros::NodeHandle nh;//必须创建句柄,否则时间没有初始化,导致后续API调用失败
ros::Duration du(10);//持续10秒钟,参数是double类型的,以秒为单位
du.sleep();//按照指定的持续时间休眠

5.3 運転周波数を設定する

Rate::Rate(double frequency);
パラメータ 意味
頻度 動作周波数

6.パラメータ設定

6.1 パラメータの変更または追加

  void NodeHandle::setParam(const std::string& key, const XmlRpc::XmlRpcValue& v) const;
  void NodeHandle::setParam(const std::string& key, const std::string& s) const;
  void NodeHandle::setParam(const std::string& key, const char* s) const;
  void NodeHandle::setParam(const std::string& key, double d) const;
  void NodeHandle::setParam(const std::string& key, int i) const;
  void NodeHandle::setParam(const std::string& key, bool b) const;
  void NodeHandle::setParam(const std::string& key, const std::vector<std::string>& vec) const;
  void NodeHandle::setParam(const std::string& key, const std::vector<double>& vec) const;
  void NodeHandle::setParam(const std::string& key, const std::vector<float>& vec) const;
  void NodeHandle::setParam(const std::string& key, const std::vector<int>& vec) const;
  void NodeHandle::setParam(const std::string& key, const std::vector<bool>& vec) const;
  void NodeHandle::setParam(const std::string& key, const std::map<std::string, std::string>& map) const;
  void NodeHandle::setParam(const std::string& key, const std::map<std::string, double>& map) const;
  void NodeHandle::setParam(const std::string& key, const std::map<std::string, float>& map) const;
  void NodeHandle::setParam(const std::string& key, const std::map<std::string, int>& map) const;
  void NodeHandle::setParam(const std::string& key, const std::map<std::string, bool>& map) const;
  • ROS には、上に示したように、設定できる 16 種類のパラメータが用意されています。
  • を使用するとros::param::setまったく同じ効果があります

6.2 パラメータの取得

/*
    参数服务器操作之查询_C++实现:
    在 roscpp 中提供了两套 API 实现参数操作
    
    ros::NodeHandle::param(键,默认值) 
            存在,返回对应结果,否则返回默认值

    ros::NodeHandle::getParam(键,存储结果的变量)
            存在,返回 true,且将值赋值给参数2
            若果键不存在,那么返回值为 false,且不为参数2赋值

    ros::NodeHandle::getParamCached(键,存储结果的变量)--提高变量获取效率
            存在,返回 true,且将值赋值给参数2
            若果键不存在,那么返回值为 false,且不为参数2赋值

    ros::NodeHandle::getParamNames(std::vector<std::string>)
            获取所有的键,并存储在参数 vector 中 

    ros::NodeHandle::hasParam(键)
            是否包含某个键,存在返回 true,否则返回 false

    ros::NodeHandle::searchParam(参数1,参数2)
            搜索键,参数1是被搜索的键,参数2存储搜索结果的变量

    ros::param ----- 与 NodeHandle 类似
*/

6.3 パラメータの削除

/* 
    参数服务器操作之删除_C++实现:

    ros::NodeHandle::deleteParam("键")
        根据键删除参数,删除成功,返回 true,否则(参数不存在),返回 false

    ros::param::del("键")
        根据键删除参数,删除成功,返回 true,否则(参数不存在),返回 false
*/

おすすめ

転載: blog.csdn.net/qq_50791664/article/details/130093578