Skip to content

Commit 1b71ab4

Browse files
committed
Add more sophisticated drive control methods to WheelSpeedNode
1 parent ccfa5b1 commit 1b71ab4

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

src/drive_control/include/wheel_speed.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
#include <vector>
99
#include "rclcpp/qos.hpp" // Include QoS header
1010

11+
struct WheelSpeeds {
12+
std::vector<double> left_wheel_speeds;
13+
std::vector<double> right_wheel_speeds;
14+
};
15+
1116
class WheelSpeedNode : public rclcpp::Node {
1217
public:
1318
WheelSpeedNode();
@@ -20,6 +25,10 @@ class WheelSpeedNode : public rclcpp::Node {
2025
rclcpp::Publisher<std_msgs::msg::Float64MultiArray>::SharedPtr right_wheel_pub_;
2126
rclcpp::Subscription<geometry_msgs::msg::Twist>::SharedPtr cmd_vel_sub_;
2227

28+
// Functions for converting cmd_vel_ messages to wheel speeds, for different drive control behavior
29+
WheelSpeeds schmittTrigger(double linear, double angular); // Schmitt trigger/hysteresis
30+
WheelSpeeds ackermann(double linear, double angular); // Electronic Ackermann Steering
31+
2332
static constexpr double WHEEL_RADIUS_METERS = 0.3; // Wheel radius
2433
static constexpr double TRACK_WIDTH_METERS = 0.6; // Distance between left and right wheels
2534
};

src/drive_control/src/wheel_speed.cpp

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,56 @@ void WheelSpeedNode::cmdVelCallback(const geometry_msgs::msg::Twist::SharedPtr m
2828
linear *= wheel_speed_factor;
2929
angular *= wheel_speed_factor;
3030

31-
// Create vectors of motor commands that correspond to the calculated speeds for left and right wheels
32-
std::vector<double> left_wheel_commands = {
33-
-linear + angular, // Left Front Wheel
34-
-linear + angular, // Left Mid Wheel
35-
-linear + angular // Left Rear Wheel
36-
};
31+
WheelSpeeds wheel_speeds = schmittTrigger(linear, angular);
32+
// std::vector<double> wheel_commands = ackermann(linear, angular);
33+
34+
// // Create vectors of motor commands that correspond to the calculated speeds for left and right wheels
35+
// std::vector<double> left_wheel_commands = {
36+
// -linear + angular, // Left Front Wheel
37+
// -linear + angular, // Left Mid Wheel
38+
// -linear + angular // Left Rear Wheel
39+
// };
3740

38-
std::vector<double> right_wheel_commands = {
39-
linear + angular, // Right Front Wheel
40-
linear + angular, // Right Mid Wheel
41-
linear + angular // Right Rear Wheel
42-
};
41+
// std::vector<double> right_wheel_commands = {
42+
// linear + angular, // Right Front Wheel
43+
// linear + angular, // Right Mid Wheel
44+
// linear + angular // Right Rear Wheel
45+
// };
4346

4447
// Publish the separate left and right wheel speed messages
4548
std_msgs::msg::Float64MultiArray left_msg;
46-
left_msg.data = left_wheel_commands; // Set left wheel commands data
49+
left_msg.data = wheel_speeds.left_wheel_speeds; // Set left wheel commands data
4750
left_wheel_pub_->publish(left_msg); // Publish to "/left_wheel_speeds"
4851

4952
std_msgs::msg::Float64MultiArray right_msg;
50-
right_msg.data = right_wheel_commands; // Set right wheel commands data
53+
right_msg.data = wheel_speeds.right_wheel_speeds; // Set right wheel commands data
5154
right_wheel_pub_->publish(right_msg); // Publish to "/right_wheel_speeds"
5255
}
5356

57+
WheelSpeeds WheelSpeedNode::schmittTrigger(double linear, double angular) {
58+
// Tank drive as placeholder
59+
double l = -linear + angular;
60+
double r = linear + angular;
61+
62+
WheelSpeeds wheelSpeeds;
63+
wheelSpeeds.left_wheel_speeds = { l, l, l };
64+
wheelSpeeds.right_wheel_speeds = { r, r, r };
65+
66+
return wheelSpeeds;
67+
}
68+
69+
WheelSpeeds WheelSpeedNode::ackermann(double linear, double angular) {
70+
// Tank drive as placeholder
71+
double l = -linear + angular;
72+
double r = linear + angular;
73+
74+
WheelSpeeds wheelSpeeds;
75+
wheelSpeeds.left_wheel_speeds = { l, l, l };
76+
wheelSpeeds.right_wheel_speeds = { r, r, r };
77+
78+
return wheelSpeeds;
79+
}
80+
5481
// Main function to initialize the ROS2 node and start processing
5582
int main(int argc, char *argv[]) {
5683
rclcpp::init(argc, argv); // Initialize the ROS2 system

0 commit comments

Comments
 (0)