on_activate

LifecycleNode

  • register_on_activate
  • on_activate
// include\rclcpp_lifecycle\lifecycle_node.hpp

namespace rclcpp_lifecycle
{

class LifecycleNode : public node_interfaces::LifecycleNodeInterface,
  public std::enable_shared_from_this<LifecycleNode>
{
public:


  /// Register the activate callback
  /**
   * This callback will be called when the transition to this state is triggered
   * \param[in] fcn callback function to call
   * \return always true
   */
  RCLCPP_LIFECYCLE_PUBLIC
  bool
  register_on_activate(std::function<LifecycleNodeInterface::CallbackReturn(const State &)> fcn);


  RCLCPP_LIFECYCLE_PUBLIC
  CallbackReturn
  on_activate(const State & previous_state) override;


}
}

ros_controller

  • 使用 rclcpp_lifecycle::LifecycleNode 类创建Node
  • 使用 rclcpp_lifecycle::LifecycleNode 类的成员方法 register_on_activate,注册 on_activate()
// controller_interface\src\controller_interface_base.cpp

namespace controller_interface
{
return_type ControllerInterfaceBase::init(
  const std::string & controller_name, const std::string & namespace_,
  const rclcpp::NodeOptions & node_options)
{
  node_ = std::make_shared<rclcpp_lifecycle::LifecycleNode>(
    controller_name, namespace_, node_options, false);  // disable LifecycleNode service interfaces

  try
  {
    auto_declare<int>("update_rate", 0);
  }
  catch (const std::exception & e)
  {
    fprintf(stderr, "Exception thrown during init stage with message: %s \n", e.what());
    return return_type::ERROR;
  }

  switch (on_init())
  {
    case LifecycleNodeInterface::CallbackReturn::SUCCESS:
      break;
    case LifecycleNodeInterface::CallbackReturn::ERROR:
    case LifecycleNodeInterface::CallbackReturn::FAILURE:
      return return_type::ERROR;
  }

  node_->register_on_configure(
    std::bind(&ControllerInterfaceBase::on_configure, this, std::placeholders::_1));

  node_->register_on_cleanup(
    std::bind(&ControllerInterfaceBase::on_cleanup, this, std::placeholders::_1));

  node_->register_on_activate(
    std::bind(&ControllerInterfaceBase::on_activate, this, std::placeholders::_1));

  node_->register_on_deactivate(
    std::bind(&ControllerInterfaceBase::on_deactivate, this, std::placeholders::_1));

  node_->register_on_shutdown(
    std::bind(&ControllerInterfaceBase::on_shutdown, this, std::placeholders::_1));

  node_->register_on_error(
    std::bind(&ControllerInterfaceBase::on_error, this, std::placeholders::_1));

  return return_type::OK;
}
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值