@z_xu_dong 我知道原因了,因为我在登记节点宏那行末尾加了分号
RCLCPP_COMPONENTS_REGISTER_NODE(ActionSeverNode); ❌
RCLCPP_COMPONENTS_REGISTER_NODE(ActionSeverNode) ✔
@z_xu_dong 我知道原因了,因为我在登记节点宏那行末尾加了分号
RCLCPP_COMPONENTS_REGISTER_NODE(ActionSeverNode); ❌
RCLCPP_COMPONENTS_REGISTER_NODE(ActionSeverNode) ✔
@z_xu_dong 最后一行创建组件的问题 但不知道问题具体出在哪
error: ‘NodeOptions’ in namespace ‘rclcpp’ does not name a type
42 | create_node_instance(const rclcpp::NodeOptions & options) = 0;
| ^~~~~~~~~~~
In file included from /opt/ros/humble/include/rclcpp_components/rclcpp_components/register_node_macro.hpp:19,
from /home/dong/projects/learning_ros2/src/learning_action/src/ActionServer.c++:5:
/opt/ros/humble/include/rclcpp_components/rclcpp_components/node_factory_template.hpp:43:38:
** error: ‘NodeOptions’ in namespace ‘rclcpp’ does not name a type**
43 | create_node_instance(const rclcpp::NodeOptions & options) override
Ubuntu22.04+ros humble
我在编写一个动作服务器节点时,编译总是不通过。我编写的代码如下:
#include <memory>
#include <string>
#include <functional>
#include "rclcpp_components/register_node_macro.hpp"
#include "rclcpp/rclcpp.hpp"
#include "rclcpp_action/rclcpp_action.hpp"
#include "learning_interfaces/action/move_action.hpp"
using namespace std::placeholders;
class ActionSeverNode : public rclcpp::Node
{
using MoveAction = learning_interfaces::action::MoveAction;
using GoalHandle = rclcpp_action::ServerGoalHandle<MoveAction>;
public:
explicit ActionSeverNode(const rclcpp::NodeOptions &options = rclcpp::NodeOptions())
: Node("ServerNode", options) //explicit?
{
sever_ = rclcpp_action::create_server<MoveAction>(this,
"move_action",
std::bind(&ActionSeverNode::goalCallback, this, _1, _2),
std::bind(&ActionSeverNode::cancelCallback, this, _1),
std::bind(&ActionSeverNode::AcceptCallback, this, _1));
}
private:
rclcpp_action::GoalResponse goalCallback(const rclcpp_action::GoalUUID uuid,
MoveAction::Goal::ConstSharedPtr goal_)
{
RCLCPP_INFO(this->get_logger(), "Received goal.dist = %ld", goal_->dist);
(void)uuid;
return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
}
rclcpp_action::CancelResponse cancelCallback(const std::shared_ptr<GoalHandle> handle_)
{
RCLCPP_INFO(this->get_logger(), "Received request to cancle goal.");
(void)handle_;
return rclcpp_action::CancelResponse::ACCEPT;
}
void AcceptCallback(const std::shared_ptr<GoalHandle> handle_)
{
std::thread(std::bind(&ActionSeverNode::excute_action, this, _1), handle_).detach(); // learn thread.
}
void excute_action(const std::shared_ptr<GoalHandle> handle_)
{
rclcpp::Rate loop_rate(1);
auto goal = handle_->get_goal();
auto feedback = std::make_shared<MoveAction::Feedback>();
auto state = feedback->state;
auto result = std::make_shared<MoveAction::Result>();
if (goal->enable)
{
if (handle_->is_canceling())
{
result->finish = false;
handle_->canceled(result);
RCLCPP_INFO(this->get_logger(), "cancel.");
return;
}
for (int i = 0; i < goal->dist; ++i)
{
state = i;
handle_->publish_feedback(feedback);
}
loop_rate.sleep();
}
if (rclcpp::ok())
{
result->finish = true;
handle_->succeed(result);
RCLCPP_INFO(this->get_logger(), "successed.");
}
}
private:
rclcpp_action::Server<MoveAction>::SharedPtr sever_;
};
RCLCPP_COMPONENTS_REGISTER_NODE(ActionSeverNode);