0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

機(jī)器人多節(jié)點(diǎn)動(dòng)作通信編程方法

麥辣雞腿堡 ? 來(lái)源:古月居 ? 作者:古月居 ? 2023-11-27 17:10 ? 次閱讀

編程方法

相比之前話題和服務(wù)的程序,動(dòng)作通信的例程相對(duì)較長(zhǎng),我們一起來(lái)運(yùn)行并分析一下。

運(yùn)行示例程序

圖片

$ source /opt/tros/local_setup.bash
$ source install/local_setup.bash
$ ros2 run learning_action_cpp server 
$ ros2 run learning_action_cpp client

代碼解析

動(dòng)作的服務(wù)器fibonacci_action_server.cpp:

#include < inttypes.h >
#include < memory >
#include "learning_action_cpp/action/fibonacci.hpp"
#include "rclcpp/rclcpp.hpp"
// TODO(jacobperron): Remove this once it is included as part of 'rclcpp.hpp'
#include "rclcpp_action/rclcpp_action.hpp"


class MinimalActionServer : public rclcpp::Node
{
public:
  using Fibonacci = learning_action_cpp::action::Fibonacci;
  using GoalHandleFibonacci = rclcpp_action::ServerGoalHandle< Fibonacci >;


  explicit MinimalActionServer(const rclcpp::NodeOptions & options = rclcpp::NodeOptions())
  : Node("minimal_action_server", options)
{
    using namespace std::placeholders;


    this- >action_server_ = rclcpp_action::create_server< Fibonacci >(
      this- >get_node_base_interface(),
      this- >get_node_clock_interface(),
      this- >get_node_logging_interface(),
      this- >get_node_waitables_interface(),
      "fibonacci",
      std::bind(&MinimalActionServer::handle_goal, this, _1, _2),
      std::bind(&MinimalActionServer::handle_cancel, this, _1),
      std::bind(&MinimalActionServer::handle_accepted, this, _1));
  }


private:
  rclcpp_action::Server< Fibonacci >::SharedPtr action_server_;


  rclcpp_action::GoalResponse handle_goal(
    const rclcpp_action::GoalUUID & uuid,
    std::shared_ptr< const Fibonacci::Goal > goal)
{
    RCLCPP_INFO(this- >get_logger(), "Received goal request with order %d", goal- >order);
    (void)uuid;
    // Let's reject sequences that are over 9000
    if (goal- >order > 9000) {
      return rclcpp_action::GoalResponse::REJECT;
    }
    return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
  }


  rclcpp_action::CancelResponse handle_cancel(
    const std::shared_ptr< GoalHandleFibonacci > goal_handle)
{
    RCLCPP_INFO(this- >get_logger(), "Received request to cancel goal");
    (void)goal_handle;
    return rclcpp_action::CancelResponse::ACCEPT;
  }


  void execute(const std::shared_ptr< GoalHandleFibonacci > goal_handle)
{
    RCLCPP_INFO(this- >get_logger(), "Executing goal");
    rclcpp::Rate loop_rate(1);
    const auto goal = goal_handle- >get_goal();
    auto feedback = std::make_shared< Fibonacci::Feedback >();
    auto & sequence = feedback- >sequence;
    sequence.push_back(0);
    sequence.push_back(1);
    auto result = std::make_shared< Fibonacci::Result >();


    for (int i = 1; (i < goal- >order) && rclcpp::ok(); ++i) {
      // Check if there is a cancel request
      if (goal_handle- >is_canceling()) {
        result- >sequence = sequence;
        goal_handle- >canceled(result);
        RCLCPP_INFO(this- >get_logger(), "Goal Canceled");
        return;
      }
      // Update sequence
      sequence.push_back(sequence[i] + sequence[i - 1]);
      // Publish feedback
      goal_handle- >publish_feedback(feedback);
      RCLCPP_INFO(this- >get_logger(), "Publish Feedback");


      loop_rate.sleep();
    }


    // Check if goal is done
    if (rclcpp::ok()) {
      result- >sequence = sequence;
      goal_handle- >succeed(result);
      RCLCPP_INFO(this- >get_logger(), "Goal Succeeded");
    }
  }


  void handle_accepted(const std::shared_ptr< GoalHandleFibonacci > goal_handle)
{
    using namespace std::placeholders;
    // this needs to return quickly to avoid blocking the executor, so spin up a new thread
    std::thread{std::bind(&MinimalActionServer::execute, this, _1), goal_handle}.detach();
  }
};  // class MinimalActionServer


int main(int argc, char ** argv)
{
  rclcpp::init(argc, argv);


  auto action_server = std::make_shared< MinimalActionServer >();


  rclcpp::spin(action_server);


  rclcpp::shutdown();
  return 0;
}

動(dòng)作的客戶端fibonacci_action_client.cpp:

#include < inttypes.h >
#include < memory >
#include < string >
#include < iostream >
#include "learning_action_cpp/action/fibonacci.hpp"
#include "rclcpp/rclcpp.hpp"
// TODO(jacobperron): Remove this once it is included as part of 'rclcpp.hpp'
#include "rclcpp_action/rclcpp_action.hpp"


class MinimalActionClient : public rclcpp::Node
{
public:
  using Fibonacci = learning_action_cpp::action::Fibonacci;
  using GoalHandleFibonacci = rclcpp_action::ClientGoalHandle< Fibonacci >;


  explicit MinimalActionClient(const rclcpp::NodeOptions & node_options = rclcpp::NodeOptions())
  : Node("minimal_action_client", node_options), goal_done_(false)
{
    this- >client_ptr_ = rclcpp_action::create_client< Fibonacci >(
      this- >get_node_base_interface(),
      this- >get_node_graph_interface(),
      this- >get_node_logging_interface(),
      this- >get_node_waitables_interface(),
      "fibonacci");


    this- >timer_ = this- >create_wall_timer(
      std::chrono::milliseconds(500),
      std::bind(&MinimalActionClient::send_goal, this));
  }


  bool is_goal_done() const
{
    return this- >goal_done_;
  }


  void send_goal()
{
    using namespace std::placeholders;


    this- >timer_- >cancel();


    this- >goal_done_ = false;


    if (!this- >client_ptr_) {
      RCLCPP_ERROR(this- >get_logger(), "Action client not initialized");
    }


    if (!this- >client_ptr_- >wait_for_action_server(std::chrono::seconds(10))) {
      RCLCPP_ERROR(this- >get_logger(), "Action server not available after waiting");
      this- >goal_done_ = true;
      return;
    }


    auto goal_msg = Fibonacci::Goal();
    goal_msg.order = 10;


    RCLCPP_INFO(this- >get_logger(), "Sending goal");


    auto send_goal_options = rclcpp_action::Client< Fibonacci >::SendGoalOptions();
    send_goal_options.goal_response_callback =
      std::bind(&MinimalActionClient::goal_response_callback, this, _1);
    send_goal_options.feedback_callback =
      std::bind(&MinimalActionClient::feedback_callback, this, _1, _2);
    send_goal_options.result_callback =
      std::bind(&MinimalActionClient::result_callback, this, _1);
    auto goal_handle_future = this- >client_ptr_- >async_send_goal(goal_msg, send_goal_options);
  }


private:
  rclcpp_action::Client< Fibonacci >::SharedPtr client_ptr_;
  rclcpp::TimerBase::SharedPtr timer_;
  bool goal_done_;


  void goal_response_callback(std::shared_future< GoalHandleFibonacci::SharedPtr > future)
{
    auto goal_handle = future.get();
    if (!goal_handle) {
      RCLCPP_ERROR(this- >get_logger(), "Goal was rejected by server");
    } else {
      RCLCPP_INFO(this- >get_logger(), "Goal accepted by server, waiting for result");
    }
  }


  void feedback_callback(
    GoalHandleFibonacci::SharedPtr,
    const std::shared_ptr< const Fibonacci::Feedback > feedback)
{
    RCLCPP_INFO(
      this- >get_logger(),
      "Next number in sequence received: %" PRId32,
      feedback- >sequence.back());
  }


  void result_callback(const GoalHandleFibonacci::WrappedResult & result)
{
    this- >goal_done_ = true;
    switch (result.code) {
      case rclcpp_action::ResultCode::SUCCEEDED:
        break;
      case rclcpp_action::ResultCode::ABORTED:
        RCLCPP_ERROR(this- >get_logger(), "Goal was aborted");
        return;
      case rclcpp_action::ResultCode::CANCELED:
        RCLCPP_ERROR(this- >get_logger(), "Goal was canceled");
        return;
      default:
        RCLCPP_ERROR(this- >get_logger(), "Unknown result code");
        return;
    }


    RCLCPP_INFO(this- >get_logger(), "Result received");
    for (auto number : result.result- >sequence) {
      RCLCPP_INFO(this- >get_logger(), "%" PRId32, number);
    }
  }
};  // class MinimalActionClient


int main(int argc, char ** argv)
{
  rclcpp::init(argc, argv);
  auto action_client = std::make_shared< MinimalActionClient >();


  while (!action_client- >is_goal_done()) {
    rclcpp::spin_some(action_client);
  }


  rclcpp::shutdown();
  return 0;
}
聲明:本文內(nèi)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 機(jī)器人
    +關(guān)注

    關(guān)注

    210

    文章

    28007

    瀏覽量

    205579
  • 通信
    +關(guān)注

    關(guān)注

    18

    文章

    5927

    瀏覽量

    135712
  • 程序
    +關(guān)注

    關(guān)注

    115

    文章

    3749

    瀏覽量

    80672
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    機(jī)器人設(shè)計(jì)中PID控制的編程方法是什么?

    什么是PID控制?機(jī)器人設(shè)計(jì)中PID控制的編程方法是什么?
    發(fā)表于 06-30 06:53

    機(jī)器人編程的區(qū)別

    在前一篇文章中講了機(jī)器人編程的區(qū)別,但總感覺(jué)講的比較空泛,這篇文章繼續(xù)講講那些區(qū)別。計(jì)算機(jī)編程機(jī)器人編程最大的區(qū)別就是一個(gè)是控制虛擬的,
    發(fā)表于 09-01 07:12

    機(jī)器人所用的線纜動(dòng)作

    根據(jù)機(jī)器人動(dòng)作選擇電纜首先請(qǐng)掌握電纜的動(dòng)作。配線時(shí)盡量加大彎曲半徑,減少扭轉(zhuǎn)和摩擦是減少斷線故障的要點(diǎn),然后根據(jù)該動(dòng)作下的期望壽命,選擇最合適的配線材料和配線
    發(fā)表于 06-09 10:44

    多節(jié)點(diǎn)大容量FPGA系統(tǒng)的遠(yuǎn)程升級(jí)方法

    多節(jié)點(diǎn)大容量FPGA系統(tǒng)的遠(yuǎn)程升級(jí)方法:針對(duì)目前廣泛使用的以大容量FPGA 實(shí)現(xiàn)主要功能的多節(jié)點(diǎn)系統(tǒng)的遠(yuǎn)程升級(jí)問(wèn)題,提出了一種基于ATmega64 單片機(jī)和RS485 總線以及接入以太網(wǎng)的主控
    發(fā)表于 11-20 17:42 ?17次下載

    多節(jié)點(diǎn)大容量FPGA系統(tǒng)的遠(yuǎn)程升級(jí)方法

    多節(jié)點(diǎn)大容量FPGA系統(tǒng)的遠(yuǎn)程升級(jí)方法 針對(duì)目前廣泛使用的以大容量FPGA實(shí)現(xiàn)主要功能的多節(jié)點(diǎn)系統(tǒng)的遠(yuǎn)程升級(jí)問(wèn)題,提出了一種基于ATmega64單片機(jī)和RS485總
    發(fā)表于 03-29 15:09 ?798次閱讀
    <b class='flag-5'>多節(jié)點(diǎn)</b>大容量FPGA系統(tǒng)的遠(yuǎn)程升級(jí)<b class='flag-5'>方法</b>

    機(jī)器人多傳感器測(cè)距系統(tǒng)研究與設(shè)計(jì)

    機(jī)器人多傳感器測(cè)距系統(tǒng)研究與設(shè)計(jì)......
    發(fā)表于 12-23 14:46 ?10次下載

    機(jī)器人多傳感器信息融合測(cè)距系統(tǒng)設(shè)計(jì)

    機(jī)器人多傳感器信息融合測(cè)距系統(tǒng)設(shè)計(jì)....
    發(fā)表于 12-23 15:00 ?16次下載

    智能機(jī)器人多傳感器融合感知方法

    智能機(jī)器人多傳感器融合感知方法,感興趣的小伙伴們可以瞧一瞧。
    發(fā)表于 09-20 16:10 ?19次下載

    解讀機(jī)器人編程機(jī)器人語(yǔ)言

    機(jī)器人編程為使機(jī)器人完成某種任務(wù)而設(shè)置的動(dòng)作順序描述。機(jī)器人運(yùn)動(dòng)和作業(yè)的指令都是由程序進(jìn)行控制,常見(jiàn)的編制
    的頭像 發(fā)表于 11-24 15:32 ?6792次閱讀

    機(jī)器人的最佳編程語(yǔ)言是什么?機(jī)器人十大流行編程語(yǔ)言匯總

    機(jī)器人的主要特點(diǎn)之一是其通用性,是機(jī)器人具有可編程能力是實(shí)現(xiàn)這一特點(diǎn)的重要手段。機(jī)器人編程必然涉及機(jī)器人
    的頭像 發(fā)表于 05-01 17:32 ?3.5w次閱讀

    常用的機(jī)器人編程方法有哪些

    機(jī)器人編程【robotprogramming】為使機(jī)器人完成某種任務(wù)而設(shè)置的動(dòng)作順序描述。機(jī)器人運(yùn)動(dòng)和作業(yè)的指令都是由程序進(jìn)行控制,常見(jiàn)的編
    發(fā)表于 08-15 17:41 ?1.6w次閱讀

    機(jī)器人編程是學(xué)的什么_機(jī)器人編程有什么前景

     機(jī)器人編程為使機(jī)器人完成某種任務(wù)而設(shè)置的動(dòng)作順序描述。機(jī)器人運(yùn)動(dòng)和作業(yè)的指令都是由程序進(jìn)行控制,常見(jiàn)的編制
    的頭像 發(fā)表于 07-23 11:42 ?3.8w次閱讀

    基于區(qū)塊鏈的智能機(jī)器人多傳感信息加密控制方法

    基于區(qū)塊鏈的智能機(jī)器人多傳感信息加密控制方法
    發(fā)表于 06-23 10:44 ?5次下載

    機(jī)器人多節(jié)點(diǎn)話題通信模型介紹

    節(jié)點(diǎn)實(shí)現(xiàn)了機(jī)器人各種各樣的功能,但這些功能并不是獨(dú)立的,之間會(huì)有千絲萬(wàn)縷的聯(lián)系,其中最重要的一種聯(lián)系方式就是話題,它是節(jié)點(diǎn)間傳遞數(shù)據(jù)的橋梁。 通信模型 以兩個(gè)
    的頭像 發(fā)表于 11-27 17:25 ?420次閱讀
    <b class='flag-5'>機(jī)器人多節(jié)點(diǎn)</b>話題<b class='flag-5'>通信</b>模型介紹

    機(jī)器人多節(jié)點(diǎn)話題通信編程方法

    編程方法 了解了話題的基本原理,接下來(lái)我們就要開(kāi)始編寫(xiě)代碼啦。 創(chuàng)建工作空間 請(qǐng)大家先按照這個(gè)流程創(chuàng)建工作空間、下載課程的例程代碼,并進(jìn)行編譯。 $ mkdir –p dev_ws/src $ cd
    的頭像 發(fā)表于 11-27 17:48 ?315次閱讀
    <b class='flag-5'>機(jī)器人多節(jié)點(diǎn)</b>話題<b class='flag-5'>通信</b><b class='flag-5'>編程</b><b class='flag-5'>方法</b>