Skip to content

Commit 02fe886

Browse files
committed
refactor: reimplement control, costmap, and map memory nodes and cores with expanded functionality and remove odometry spoofing.
1 parent 89fa0b0 commit 02fe886

File tree

21 files changed

+209
-440
lines changed

21 files changed

+209
-440
lines changed

src/robot/control/include/control/control_core.hpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#ifndef CONTROL_CORE_HPP_
1616
#define CONTROL_CORE_HPP_
1717

18+
#include "geometry_msgs/msg/twist.hpp"
19+
#include "nav_msgs/msg/path.hpp"
1820
#include "rclcpp/rclcpp.hpp"
1921

2022
namespace robot
@@ -23,11 +25,30 @@ namespace robot
2325
class ControlCore
2426
{
2527
public:
26-
// Constructor, we pass in the node's RCLCPP logger to enable logging to terminal
2728
explicit ControlCore(const rclcpp::Logger & logger);
2829

30+
void initControlCore(double kp, double ki, double kd, double max_steering_angle, double linear_velocity);
31+
32+
void updatePath(nav_msgs::msg::Path path);
33+
34+
bool isPathEmpty();
35+
36+
unsigned int findClosestPoint(double robot_x, double robot_y);
37+
38+
geometry_msgs::msg::Twist calculateControlCommand(double robot_x, double robot_y, double robot_theta, double dt);
39+
2940
private:
41+
nav_msgs::msg::Path path_;
3042
rclcpp::Logger logger_;
43+
44+
double kp_;
45+
double ki_;
46+
double kd_;
47+
double max_steering_angle_;
48+
double linear_velocity_;
49+
50+
double prev_error_;
51+
double integral_error_;
3152
};
3253

3354
} // namespace robot

src/robot/control/include/control/control_node.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,65 @@
1515
#ifndef CONTROL_NODE_HPP_
1616
#define CONTROL_NODE_HPP_
1717

18+
#include <string>
19+
1820
#include "control/control_core.hpp"
21+
#include "geometry_msgs/msg/twist.hpp"
22+
#include "nav_msgs/msg/odometry.hpp"
23+
#include "nav_msgs/msg/path.hpp"
1924
#include "rclcpp/rclcpp.hpp"
2025

2126
class ControlNode : public rclcpp::Node
2227
{
2328
public:
2429
ControlNode();
2530

31+
// Read and load in ROS2 parameters
32+
void processParameters();
33+
34+
// Utility: Convert quaternion to yaw
35+
double quaternionToYaw(double x, double y, double z, double w);
36+
37+
// Callback for path
38+
void pathCallback(const nav_msgs::msg::Path::SharedPtr msg);
39+
40+
// Callback for odometry
41+
void odomCallback(const nav_msgs::msg::Odometry::SharedPtr msg);
42+
43+
// Main loop to continuously follow the path
44+
void followPath();
45+
46+
// Timer callback
47+
void timerCallback();
48+
2649
private:
2750
robot::ControlCore control_;
51+
52+
// Subscriber and Publisher
53+
rclcpp::Subscription<nav_msgs::msg::Path>::SharedPtr path_subscriber_;
54+
rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr odom_subscriber_;
55+
rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr cmd_vel_publisher_;
56+
57+
// Timer
58+
rclcpp::TimerBase::SharedPtr timer_;
59+
60+
// Path and robot state
61+
double robot_x_;
62+
double robot_y_;
63+
double robot_theta_;
64+
65+
// ROS2 params
66+
std::string path_topic_;
67+
std::string odom_topic_;
68+
std::string cmd_vel_topic_;
69+
70+
int control_period_ms_;
71+
double kp_;
72+
double ki_;
73+
double kd_;
74+
75+
double max_steering_angle_;
76+
double linear_velocity_;
2877
};
2978

3079
#endif

src/robot/control/include/control_core.hpp

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/robot/control/include/control_node.hpp

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/robot/costmap/include/costmap/costmap_core.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
#ifndef COSTMAP_CORE_HPP_
1616
#define COSTMAP_CORE_HPP_
1717

18+
#include "nav_msgs/msg/occupancy_grid.hpp"
1819
#include "rclcpp/rclcpp.hpp"
20+
#include "sensor_msgs/msg/laser_scan.hpp"
21+
#include "sensor_msgs/msg/point_cloud2.hpp"
1922

2023
namespace robot
2124
{
@@ -26,8 +29,28 @@ class CostmapCore
2629
// Constructor, we pass in the node's RCLCPP logger to enable logging to terminal
2730
explicit CostmapCore(const rclcpp::Logger & logger);
2831

32+
// Initializes the Costmap with the parameters that we get from the params.yaml
33+
void initCostmap(double resolution, int width, int height, geometry_msgs::msg::Pose origin, double inflation_radius);
34+
35+
// Update the costmap based on the current laserscan reading
36+
void updateCostmap(const sensor_msgs::msg::LaserScan::SharedPtr laserscan) const;
37+
38+
// Update the costmap based on point cloud data from RGBD camera
39+
void updateCostmapFromPointCloud(const sensor_msgs::msg::PointCloud2::SharedPtr cloud) const;
40+
41+
// Inflate the obstacle in the laserscan on the costmap because we want of range of values
42+
// where we can and cannot go
43+
void inflateObstacle(int origin_x, int origin_y) const;
44+
45+
// Retrieves costmap data
46+
nav_msgs::msg::OccupancyGrid::SharedPtr getCostmapData() const;
47+
2948
private:
49+
nav_msgs::msg::OccupancyGrid::SharedPtr costmap_data_;
3050
rclcpp::Logger logger_;
51+
52+
double inflation_radius_;
53+
int inflation_cells_;
3154
};
3255

3356
} // namespace robot

src/robot/costmap/include/costmap/costmap_node.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,47 @@
1515
#ifndef COSTMAP_NODE_HPP_
1616
#define COSTMAP_NODE_HPP_
1717

18+
#include <string>
19+
1820
#include "costmap/costmap_core.hpp"
21+
#include "nav_msgs/msg/occupancy_grid.hpp"
1922
#include "rclcpp/rclcpp.hpp"
23+
#include "sensor_msgs/msg/laser_scan.hpp"
24+
#include "sensor_msgs/msg/point_cloud2.hpp"
2025

2126
class CostmapNode : public rclcpp::Node
2227
{
2328
public:
29+
// Costmap Node constructor
2430
CostmapNode();
2531

32+
// Retrieves all the parameters and their values in params.yaml
33+
void processParameters();
34+
35+
// Given a laserscan, it will send it into CostmapCore for processing and then
36+
// retrieve the costmap
37+
void laserScanCallback(const sensor_msgs::msg::LaserScan::SharedPtr msg) const;
38+
39+
// Given a point cloud, it will send it into CostmapCore for processing and then
40+
// retrieve the costmap
41+
void pointCloudCallback(const sensor_msgs::msg::PointCloud2::SharedPtr msg) const;
42+
2643
private:
2744
robot::CostmapCore costmap_;
45+
46+
rclcpp::Subscription<sensor_msgs::msg::LaserScan>::SharedPtr laser_scan_sub_;
47+
rclcpp::Subscription<sensor_msgs::msg::PointCloud2>::SharedPtr point_cloud_sub_;
48+
rclcpp::Publisher<nav_msgs::msg::OccupancyGrid>::SharedPtr costmap_pub_;
49+
50+
std::string laserscan_topic_;
51+
std::string pointcloud_topic_;
52+
std::string costmap_topic_;
53+
54+
double resolution_;
55+
int width_;
56+
int height_;
57+
geometry_msgs::msg::Pose origin_;
58+
double inflation_radius_;
2859
};
2960

3061
#endif

src/robot/costmap/include/costmap_core.hpp

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)