-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Low-level controller #64
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env python | ||
|
||
from dynamic_reconfigure.parameter_generator_catkin import * | ||
|
||
# Parameter generator handle | ||
gen = ParameterGenerator() | ||
|
||
# Add double parameters for angular velocity control | ||
# Name | Type | Level | Description | Default | Min | Max | ||
gen.add("kp_angular", double_t, 0, "Proportional gain for angular velocity", 3.0, 0, 10) | ||
gen.add("ki_angular", double_t, 0, "Integral gain for angular velocity", 1.0, 0, 5) | ||
gen.add("kd_angular", double_t, 0, "Derivative gain for angular velocity", 0.0, 0, 1.0) | ||
|
||
# Add double parameters for linear velocity control | ||
gen.add("kp_linear", double_t, 0, "Proportional gain for linear velocity", 1.5, 0, 10) | ||
gen.add("ki_linear", double_t, 0, "Integral gain for linear velocity", 1.0, 0, 5) | ||
gen.add("kd_linear", double_t, 0, "Derivative gain for linear velocity", 0.0, 0, 1.0) | ||
|
||
# Add boolean parameters for EKF sensor inputs | ||
# Name | Type | Level | Description | Default | ||
gen.add("use_imu", bool_t, 0, "Use IMU data", True) | ||
gen.add("use_differential_drive_estimation", bool_t, 0, "Use Differential Drive Estimation data", True) | ||
gen.add("use_vio", bool_t, 0, "Use VIO data", False) | ||
|
||
# Exit parameter generator | ||
# Package Name | Node Name (used to generate documentation only) | cfg File Name | ||
exit(gen.generate("smb_control", "PIDControllerNode", "PID")) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#ifndef PIDCONTROLLERNODE_HPP | ||
#define PIDCONTROLLERNODE_HPP | ||
|
||
#include <ros/ros.h> | ||
#include <std_msgs/Int32.h> | ||
|
||
#include <geometry_msgs/Twist.h> | ||
#include <nav_msgs/Odometry.h> | ||
|
||
#include <dynamic_reconfigure/server.h> | ||
#include <smb_control/PIDConfig.h> | ||
|
||
class PIDController { | ||
public: | ||
PIDController(double kp = 0.0, double ki = 0.0, double kd = 0.0, double max_integral = 2.0) | ||
: kp_(kp), ki_(ki), kd_(kd), integral_(0.0), prev_error_(0.0), max_integral_(max_integral) {} | ||
|
||
void setGains(double kp, double ki, double kd) { | ||
kp_ = kp; | ||
ki_ = ki; | ||
kd_ = kd; | ||
} | ||
|
||
void setMaxIntegral(double max_integral) { | ||
max_integral_ = max_integral; | ||
} | ||
|
||
double update(double error, double dt) { | ||
if (dt <= 0.0001) return 0.0; // Prevent division by zero | ||
|
||
integral_ += error * dt; | ||
|
||
// Clamp the integral value to the maximum limit | ||
if (integral_ > max_integral_) integral_ = max_integral_; | ||
if (integral_ < -max_integral_) integral_ = -max_integral_; | ||
|
||
double derivative = (error - prev_error_) / dt; | ||
prev_error_ = error; | ||
|
||
return kp_ * error + ki_ * integral_ + kd_ * derivative; | ||
} | ||
|
||
void reset() { | ||
integral_ = 0.0; | ||
prev_error_ = 0.0; | ||
} | ||
|
||
void resetIfZero(double cmd) { | ||
if (std::abs(cmd) < 0.05) { // Check if the command is approximately zero | ||
reset(); | ||
} | ||
} | ||
|
||
private: | ||
double kp_, ki_, kd_; | ||
double integral_, prev_error_; | ||
double max_integral_; | ||
}; | ||
|
||
class PIDControllerNode | ||
{ | ||
public: | ||
// Constructor | ||
PIDControllerNode(); | ||
|
||
// Destructor | ||
~PIDControllerNode(); | ||
|
||
// Main loop function | ||
void run(); | ||
|
||
void configCallback(smb_control::PIDConfig &config, uint32_t level); | ||
|
||
void ekfConfigCallback(smb_control::PIDConfig &config, uint32_t level); | ||
|
||
private: | ||
ros::NodeHandle nh_; | ||
|
||
// Publisher | ||
ros::Publisher cmd_vel_pub_; | ||
|
||
// Subscriber | ||
ros::Subscriber twist_sub_; | ||
ros::Subscriber odom_sub_; | ||
|
||
bool pure_feed_forward_{true}; | ||
|
||
// Subscriber callback | ||
void twistCallback(const geometry_msgs::Twist::ConstPtr& msg); | ||
void odomCallback(const nav_msgs::Odometry::ConstPtr& msg); | ||
|
||
// PID | ||
PIDController angular_velocity_pid_; | ||
PIDController linear_velocity_pid_; | ||
bool use_diff_drive_estimation_{false}; | ||
bool use_vio_{false}; | ||
bool use_imu_{false}; | ||
|
||
std::shared_ptr<dynamic_reconfigure::Server<smb_control::PIDConfig>> config_server_; | ||
|
||
geometry_msgs::Twist cmd_vel_requested_; | ||
geometry_msgs::Twist cmd_vel_; | ||
|
||
ros::Time last_twist_time_; | ||
ros::Time last_odom_time_; | ||
double dt_{0.01}; | ||
double odom_timeout_{0.0}; | ||
double twist_timeout_{0.0}; | ||
|
||
void checkTimeout(); | ||
void executeFeedforwardControl(); | ||
}; | ||
|
||
#endif // PIDCONTROLLERNODE_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
|
||
<launch> | ||
<!-- Launch the EKF node --> | ||
<node pkg="robot_localization" type="ekf_localization_node" name="ekf_se" output="screen"/> | ||
|
||
</launch> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where is the EKF estimation published?
I would like to keep the topic names somewhat tidy and organized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The EKF topic is: /filtered/odometry
Should I add a comment for that?