8: Node(
"motion_capture"),
9 previous_position_(Eigen::Vector4f::Zero()),
10 previous_velocity_(Eigen::Vector3f::Zero()),
11 previous_time_(this->now()), previous_velocity_time_(this->now())
15 subscription_ = this->create_subscription<sensor_msgs::msg::PointCloud2>(
28 timer_ = this->create_wall_timer(
32 RCLCPP_INFO(this->get_logger(),
"MotionCapture node initialized.");
37 this->declare_parameter<std::string>(
38 "point_cloud_topic_filtered",
42 this->declare_parameter<std::string>(
43 "acceleration_publish_topic",
55 publish_rate_ = this->get_parameter(
"publish_rate").as_double();
56 queue_size_ = this->get_parameter(
"queue_size").as_int();
61 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(
new pcl::PointCloud<pcl::PointXYZRGB>);
62 pcl::fromROSMsg(*msg, *cloud);
64 Eigen::Vector4f centroid;
65 pcl::compute3DCentroid(*cloud, centroid);
80 Eigen::Vector4f sum = Eigen::Vector4f::Zero();
82 while (!temp_queue.empty()) {
83 sum += temp_queue.front();
99 Eigen::Vector3f sum = Eigen::Vector3f::Zero();
101 while (!temp_queue.empty()) {
102 sum += temp_queue.front();
129 geometry_msgs::msg::Point position_msg;
139 rclcpp::Time current_time = this->now();
140 double time_difference = (current_time -
previous_time_).seconds();
141 Eigen::Vector3f velocity = Eigen::Vector3f::Zero();
143 if (time_difference > 0) {
151 geometry_msgs::msg::Vector3 velocity_msg;
164 rclcpp::Time current_time = this->now();
166 Eigen::Vector3f acceleration = Eigen::Vector3f::Zero();
168 if (time_difference > 0) {
176 geometry_msgs::msg::Vector3 acceleration_msg;
177 acceleration_msg.x = acceleration[
kVectorX];
178 acceleration_msg.y = acceleration[
kVectorY];
179 acceleration_msg.z = acceleration[
kVectorZ];
190int main(
int argc,
char ** argv)
192 rclcpp::init(argc, argv);
193 rclcpp::spin(std::make_shared<pointcloud_motion_capture::MotionCapture>());
rclcpp::Publisher< geometry_msgs::msg::Vector3 >::SharedPtr acceleration_publisher_
ROS2 publisher to send the calculated object acceleration.
@ kCentroidY
Index for the Y-coordinate of the centroid.
@ kCentroidX
Index for the X-coordinate of the centroid.
@ kCentroidZ
Index for the Z-coordinate of the centroid.
void publishData()
Publishes the calculated position, velocity, and acceleration.
double kDefaultPublishRate
Default publish rate in Hz for publishing position, velocity, and acceleration.
int kDefaultQueueSize
Default queue size for ROS publishers and subscriptions.
Eigen::Vector4f getFilteredPosition()
Retrieves the filtered position of the object.
@ kVectorZ
Index for the Z-component of the velocity vector.
@ kVectorY
Index for the Y-component of the velocity vector.
@ kVectorX
Index for the X-component of the velocity vector.
std::string kDefaultPointCloudTopicFiltered
Default topic name for subscribing to the filtered point cloud data.
Eigen::Vector4f previous_position_
The previous position of the object in 3D space.
void updatePositionSamples(const Eigen::Vector4f ¢roid)
Updates the position samples queue with a new centroid value.
std::string acceleration_publish_topic_
Topic name for publishing the calculated acceleration of an object.
void loadParameters()
Loads parameters from the ROS2 parameter server.
rclcpp::Time previous_time_
The timestamp of the previous position sample.
std::queue< Eigen::Vector4f > position_queue_
Queue to store recent position samples for calculating position.
int kDefaultPositionSamples
Default number of position samples used for calculations.
Eigen::Vector3f publishVelocity(const Eigen::Vector4f ¤t_position)
Publishes the object's velocity to a ROS topic.
std::string kDefaultAccelerationPublishTopic
Default topic name for publishing the calculated object acceleration.
std::queue< Eigen::Vector3f > velocity_queue_
Queue to store recent velocity samples for calculating velocity.
int kDefaultVelocitySamples
Default number of velocity samples used for calculations.
Eigen::Vector3f getFilteredVelocity()
Retrieves the filtered velocity of the object.
int velocity_samples_
Number of velocity samples used for calculations.
std::string kDefaultPositionPublishTopic
Default topic name for publishing the calculated object position.
int position_samples_
Number of position samples used for calculations.
rclcpp::Publisher< geometry_msgs::msg::Vector3 >::SharedPtr velocity_publisher_
ROS2 publisher to send the calculated object velocity.
std::string point_cloud_topic_filtered_
Topic name for subscribing to filtered point cloud data.
double kMillisecondsPerSecond
Conversion factor from seconds to milliseconds.
std::string kDefaultVelocityPublishTopic
Default topic name for publishing the calculated object velocity.
MotionCapture()
Constructor for the MotionCapture class.
int queue_size_
Queue size for message publishers and subscriptions.
rclcpp::TimerBase::SharedPtr timer_
Timer used for scheduling the publication of data.
Eigen::Vector3f previous_velocity_
The previous velocity of the object in 3D space.
rclcpp::Subscription< sensor_msgs::msg::PointCloud2 >::SharedPtr subscription_
ROS2 subscription to receive PointCloud2 messages.
void pointCloudCallback(const sensor_msgs::msg::PointCloud2::SharedPtr msg)
Callback function for processing incoming PointCloud2 messages.
void publishPosition(const Eigen::Vector4f ¢roid)
Publishes the object's position to a ROS topic.
void updateVelocitySamples(const Eigen::Vector3f &velocity)
Updates the velocity samples queue with a new velocity value.
void publishAcceleration(const Eigen::Vector3f ¤t_velocity)
Publishes the object's acceleration to a ROS topic.
std::string velocity_publish_topic_
Topic name for publishing the calculated velocity of an object.
rclcpp::Publisher< geometry_msgs::msg::Point >::SharedPtr position_publisher_
ROS2 publisher to send the calculated object position.
rclcpp::Time previous_velocity_time_
The timestamp of the previous velocity sample.
std::string position_publish_topic_
Topic name for publishing the calculated position of an object.
double publish_rate_
Publish rate for sending position, velocity, and acceleration data.
int main(int argc, char **argv)