Skip to content

Commit 6641130

Browse files
committed
action server set up
1 parent d3658a0 commit 6641130

File tree

3 files changed

+83
-16
lines changed

3 files changed

+83
-16
lines changed

teleoperation/lander_align/lander_align.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ namespace mrover {
7171
double const mAngleP = 1;
7272
double const mAngleFloor = 0.05;
7373
double const mLinearP = 0.3;
74-
74+
7575
rclcpp::Publisher<geometry_msgs::msg::Vector3>::SharedPtr mDebugVectorPub;
7676

7777
//Action Server Variables
Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
1-
#include "lander_align.new.hpp"
1+
#include "lander_align.new.hpp"
2+
3+
//LanderAlignActionServer member functions:
4+
namespace mrover{
5+
6+
using LanderAlign = action::LanderAlign;
7+
using GoalHandleLanderAlign = rclcpp_action::ServerGoalHandle<LanderAlign>;
8+
9+
LanderAlignNode::LanderAlignNode(const rclcpp::NodeOptions& options)
10+
: Node("lander_align", options) {
11+
//The constructor also instantiates a new action server
12+
action_server_ = rclcpp_action::create_server<LanderAlign>(
13+
this,
14+
"lander_align_action",
15+
std::bind(&LanderAlignNode::handle_goal, this, std::placeholders::_1, std::placeholders::_2),
16+
std::bind(&LanderAlignNode::handle_cancel, this, std::placeholders::_1),
17+
std::bind(&LanderAlignNode::handle_accepted, this, std::placeholders::_1));
18+
}
19+
20+
//the callback for handling new goals (this implementation just accepts all goals)
21+
rclcpp_action::GoalResponse LanderAlignNode::handle_goal(const rclcpp_action::GoalUUID & uuid, std::shared_ptr<const LanderAlign::Goal> goal) {
22+
RCLCPP_INFO(get_logger(), "Received goal request with order %d", goal->num);
23+
(void)uuid;
24+
return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
25+
}
26+
27+
//the callback for dealing with cancellation (this implementation just tells the client that it accepted the cancellation)
28+
rclcpp_action::CancelResponse LanderAlignNode::handle_cancel(const std::shared_ptr<GoalHandleLanderAlign> goal_handle) {
29+
RCLCPP_INFO(get_logger(), "Received request to cancel goal");
30+
(void)goal_handle;
31+
return rclcpp_action::CancelResponse::ACCEPT;
32+
}
33+
34+
//the callback for accepting a new goal and processing it
35+
void LanderAlignNode::handle_accepted(const std::shared_ptr<GoalHandleLanderAlign> goal_handle) {
36+
using namespace std::placeholders;
37+
// this needs to return quickly to avoid blocking the executor, so spin up a new thread
38+
std::thread{std::bind(&LanderAlignNode::execute, this, _1), goal_handle}.detach();
39+
}
40+
41+
//all further processing and updates are done in the execute method in the new thread
42+
void LanderAlignNode::execute(const std::shared_ptr<GoalHandleLanderAlign> goal_handle) {
43+
RCLCPP_INFO(this->get_logger(), "Executing goal");
44+
auto result = std::make_shared<LanderAlign::Result>();
45+
// Check if goal is done
46+
if (rclcpp::ok()) {
47+
goal_handle->succeed(result);
48+
RCLCPP_INFO(this->get_logger(), "Goal succeeded");
49+
}
50+
}
51+
}
Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
11
#include "pch.hpp"
22
#include <geometry_msgs/msg/detail/pose__struct.hpp>
33
#include <rclcpp/node.hpp>
4-
#include <mrover/srv/align_lander.hpp>
5-
4+
#include <std_msgs/msg/detail/float32__struct.hpp>
5+
#include <std_msgs/msg/float32.hpp>
6+
#include <rclcpp/logging.hpp>
7+
#include <mrover/action/lander_align.hpp>
68
namespace mrover {
79
class LanderAlignNode final : public rclcpp::Node{
10+
public:
11+
using LanderAlign = action::LanderAlign;
12+
using GoalHandleLanderAlign = rclcpp_action::ServerGoalHandle<LanderAlign>;
13+
14+
explicit LanderAlignNode(rclcpp::NodeOptions const& options = rclcpp::NodeOptions());
15+
16+
~LanderAlignNode() override = default;
17+
18+
void filterNormals();
19+
820
private:
9-
rclcpp::Publisher<geometry_msgs::msg::Pose>::SharedPtr mCostMapPub;
21+
rclcpp::Publisher<std_msgs::msg::Float32>::SharedPtr mCostMapPub;
1022
rclcpp::Subscription<sensor_msgs::msg::PointCloud2>::SharedPtr mPcSub;
11-
rclcpp::Service<mrover::srv::AlignLander>::SharedPtr mAlignServer;
23+
rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr mPCDebugPub;
24+
1225
geometry_msgs::msg::Pose mFinalAngle;
1326

27+
rclcpp_action::Server<LanderAlign>::SharedPtr action_server_;
28+
29+
//the callback for handling new goals (this implementation just accepts all goals)
30+
rclcpp_action::GoalResponse handle_goal(const rclcpp_action::GoalUUID & uuid, std::shared_ptr<const LanderAlign::Goal> goal);
31+
32+
//the callback for dealing with cancellation (this implementation just tells the client that it accepted the cancellation)
33+
rclcpp_action::CancelResponse handle_cancel(const std::shared_ptr<GoalHandleLanderAlign> goal_handle);
34+
35+
//the callback for accepting a new goal and processing it
36+
void handle_accepted(const std::shared_ptr<GoalHandleLanderAlign> goal_handle);
37+
38+
//all further processing and updates are done in the execute method in the new thread
39+
void execute(const std::shared_ptr<GoalHandleLanderAlign> goal_handle);
40+
1441
/*
1542
Using PCA to calculate plane:
1643
1. Initial filtering of points by removing any normal with z-component above some threshold (plane normals will be
@@ -21,15 +48,5 @@ namespace mrover {
2148
4. Do PCA and find normal
2249
5. Keep doing to find better planes?
2350
*/
24-
25-
explicit LanderAlignNode(rclcpp::NodeOptions const& options = rclcpp::NodeOptions());
26-
27-
~LanderAlignNode() override = default;
28-
29-
public:
30-
31-
32-
33-
auto alignLanderCallBack(mrover::srv::AlignLander::Request::ConstSharedPtr& req, mrover::srv::AlignLander::Response::SharedPtr& res) -> void;
3451
};
3552
}

0 commit comments

Comments
 (0)