Corrected wrong definition.

This commit is contained in:
Charles STELANDRE 2025-12-17 10:08:29 +01:00
parent f000862dc1
commit e3f9bd8ed1
1 changed files with 54 additions and 20 deletions

View File

@ -1,6 +1,6 @@
#include <chrono> #include <chrono>
#include <memory>
#include <cmath> #include <cmath>
#include <memory>
#include "rclcpp/rclcpp.hpp" #include "rclcpp/rclcpp.hpp"
#include "advrobotics_lab3_interfaces/msg/joints.hpp" #include "advrobotics_lab3_interfaces/msg/joints.hpp"
@ -14,8 +14,11 @@ class BuildTower : public rclcpp::Node
public: public:
BuildTower() : Node("build_tower") BuildTower() : Node("build_tower")
{ {
joint_cmd_pub_ = create_publisher<advrobotics_lab3_interfaces::msg::Joints>("joint_cmd", 10); joint_cmd_pub_ = create_publisher<advrobotics_lab3_interfaces::msg::Joints>(
gripper_cmd_pub_ = create_publisher<advrobotics_lab3_interfaces::msg::Gripper>("gripper_cmd", 10); "joint_cmd", 10);
gripper_cmd_pub_ = create_publisher<advrobotics_lab3_interfaces::msg::Gripper>(
"gripper_cmd", 10);
joint_pos_sub_ = create_subscription<advrobotics_lab3_interfaces::msg::Joints>( joint_pos_sub_ = create_subscription<advrobotics_lab3_interfaces::msg::Joints>(
"joint_position", 10, "joint_position", 10,
@ -24,32 +27,46 @@ public:
ik_client_ = create_client<advrobotics_lab3_interfaces::srv::Invkin>("invkin"); ik_client_ = create_client<advrobotics_lab3_interfaces::srv::Invkin>("invkin");
while (!ik_client_->wait_for_service(1s)) while (!ik_client_->wait_for_service(1s))
{
RCLCPP_INFO(get_logger(), "Waiting for IK service..."); RCLCPP_INFO(get_logger(), "Waiting for IK service...");
}
executeTower(); // Run once AFTER construction is complete
start_timer_ = create_wall_timer(
500ms, std::bind(&BuildTower::startOnce, this));
} }
private: private:
/* ---------------- CALLBACKS ---------------- */ /* ================= CALLBACKS ================= */
void jointPosCallback(const advrobotics_lab3_interfaces::msg::Joints::SharedPtr msg) void jointPosCallback(
const advrobotics_lab3_interfaces::msg::Joints::SharedPtr msg)
{ {
current_q1_ = msg->q1; current_q1_ = msg->q1;
current_q2_ = msg->q2; current_q2_ = msg->q2;
current_q3_ = msg->q3; current_q3_ = msg->q3;
} }
/* ---------------- MOTION PRIMITIVES ---------------- */ void startOnce()
{
start_timer_->cancel();
executeTower();
}
/* ================= MOTION PRIMITIVES ================= */
void moveToCartesian(double x, double y, double z) void moveToCartesian(double x, double y, double z)
{ {
auto request = std::make_shared<advrobotics_lab3_interfaces::srv::Invkin::Request>(); auto request =
std::make_shared<advrobotics_lab3_interfaces::srv::Invkin::Request>();
request->x = x; request->x = x;
request->y = y; request->y = y;
request->z = z; request->z = z;
auto future = ik_client_->async_send_request(request); auto future = ik_client_->async_send_request(request);
rclcpp::spin_until_future_complete(shared_from_this(), future);
rclcpp::spin_until_future_complete(
get_node_base_interface(), future);
auto response = future.get(); auto response = future.get();
@ -62,7 +79,8 @@ private:
waitUntilReached(cmd); waitUntilReached(cmd);
} }
void waitUntilReached(const advrobotics_lab3_interfaces::msg::Joints &target) void waitUntilReached(
const advrobotics_lab3_interfaces::msg::Joints &target)
{ {
while (rclcpp::ok()) while (rclcpp::ok())
{ {
@ -83,7 +101,7 @@ private:
rclcpp::sleep_for(1s); rclcpp::sleep_for(1s);
} }
/* ---------------- MAIN LOGIC ---------------- */ /* ================= MAIN TOWER LOGIC ================= */
void executeTower() void executeTower()
{ {
@ -92,8 +110,8 @@ private:
constexpr double pick_z = 0.02; constexpr double pick_z = 0.02;
constexpr double place_x = 0.15; constexpr double place_x = 0.15;
constexpr double place_y = 0.0; constexpr double place_y = 0.00;
constexpr double base_z = 0.02; constexpr double base_z = 0.02;
constexpr double block_height = 0.03; constexpr double block_height = 0.03;
constexpr double approach_z = 0.10; constexpr double approach_z = 0.10;
@ -106,28 +124,44 @@ private:
{ {
double place_z = base_z + i * block_height; double place_z = base_z + i * block_height;
// Pick
moveToCartesian(pick_x, pick_y, approach_z); moveToCartesian(pick_x, pick_y, approach_z);
moveToCartesian(pick_x, pick_y, pick_z); moveToCartesian(pick_x, pick_y, pick_z);
setGripper(gripper_closed); setGripper(gripper_closed);
// Lift & move
moveToCartesian(pick_x, pick_y, approach_z); moveToCartesian(pick_x, pick_y, approach_z);
moveToCartesian(place_x, place_y, approach_z); moveToCartesian(place_x, place_y, approach_z);
moveToCartesian(place_x, place_y, place_z);
// Place
moveToCartesian(place_x, place_y, place_z);
setGripper(gripper_open); setGripper(gripper_open);
// Retreat
moveToCartesian(place_x, place_y, approach_z); moveToCartesian(place_x, place_y, approach_z);
} }
RCLCPP_INFO(get_logger(), "Tower completed."); RCLCPP_INFO(get_logger(), "Tower successfully built.");
} }
private: private:
rclcpp::Publisher<advrobotics_lab3_interfaces::msg::Joints>::SharedPtr joint_cmd_pub_; rclcpp::Publisher<advrobotics_lab3_interfaces::msg::Joints>::SharedPtr
rclcpp::Publisher<advrobotics_lab3_interfaces::msg::Gripper>::SharedPtr gripper_cmd_pub_; joint_cmd_pub_;
rclcpp::Subscription<advrobotics_lab3_interfaces::msg::Joints>::SharedPtr joint_pos_sub_;
rclcpp::Client<advrobotics_lab3_interfaces::srv::Invkin>::SharedPtr ik_client_;
double current_q1_{0.0}, current_q2_{0.0}, current_q3_{0.0}; rclcpp::Publisher<advrobotics_lab3_interfaces::msg::Gripper>::SharedPtr
gripper_cmd_pub_;
rclcpp::Subscription<advrobotics_lab3_interfaces::msg::Joints>::SharedPtr
joint_pos_sub_;
rclcpp::Client<advrobotics_lab3_interfaces::srv::Invkin>::SharedPtr
ik_client_;
rclcpp::TimerBase::SharedPtr start_timer_;
double current_q1_{0.0};
double current_q2_{0.0};
double current_q3_{0.0};
}; };
int main(int argc, char **argv) int main(int argc, char **argv)