Skip to content

Commit f916f0c

Browse files
committed
feat(controller): motor controller working, but not fine tuned
1 parent 69a66aa commit f916f0c

File tree

4 files changed

+54
-36
lines changed

4 files changed

+54
-36
lines changed

arc_base/src/low_level_motor_controller.cpp

+25-20
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ class MotorController : public ros2::Node {
6565
Publisher<Float64Msg>::SharedPtr motor_pub_;
6666
Publisher<MotorControllerStatusMsg>::SharedPtr status_pub_;
6767

68+
void pub_status_() {
69+
// update message
70+
71+
msg_status_.control.target = target_;
72+
msg_status_.control.actual = actual_;
73+
msg_status_.control.error = error_;
74+
msg_status_.pid.p = control_pid_.p;
75+
msg_status_.pid.i = control_pid_.i;
76+
msg_status_.pid.d = control_pid_.d;
77+
msg_status_.output = output_;
78+
79+
status_pub_->publish(msg_status_);
80+
// RCLCPP_DEBUG(this->get_logger(), "published status");
81+
}
82+
6883
public:
6984
MotorController()
7085
: Node("motor_controller"), control_pid_{0.0, 0.0, 0.0},
@@ -186,7 +201,6 @@ class MotorController : public ros2::Node {
186201
diff_drive_sub_ = this->create_subscription<Float64Msg>(
187202
diff_drive_topic.c_str(), 10, [this](Float64Msg::UniquePtr msg) {
188203
this->target_ = this->get_target_value_(*msg);
189-
this->actual_ = this->get_sensor_value_(vesc_state_);
190204
});
191205

192206
const String state_topic =
@@ -197,6 +211,7 @@ class MotorController : public ros2::Node {
197211
state_topic.c_str(), 10,
198212
[this](VescStateStampedMsg::UniquePtr msg) {
199213
this->vesc_state_ = *msg;
214+
this->actual_ = this->get_sensor_value_(*msg);
200215
});
201216

202217
// publishers
@@ -228,20 +243,8 @@ class MotorController : public ros2::Node {
228243
motor_pub_->publish(msg_control_);
229244
});
230245

231-
timer_status_ = this->create_wall_timer(cooldown, [this]() {
232-
// update message
233-
234-
msg_status_.control.target = target_;
235-
msg_status_.control.actual = actual_;
236-
msg_status_.control.error = error_;
237-
msg_status_.pid.p = control_pid_.p;
238-
msg_status_.pid.i = control_pid_.i;
239-
msg_status_.pid.d = control_pid_.d;
240-
msg_status_.output = output_;
241-
242-
status_pub_->publish(msg_status_);
243-
// RCLCPP_DEBUG(this->get_logger(), "published status");
244-
});
246+
timer_status_ =
247+
this->create_wall_timer(cooldown, [this]() { pub_status_(); });
245248
}
246249

247250
void step() {
@@ -277,14 +280,16 @@ class MotorController : public ros2::Node {
277280

278281
// In (0, 450) we want to clamp to 900
279282
// In [450, 900) we want to clamp to 0
280-
if (std::abs(output_) < 450) {
281-
output_ =
282-
output_ == 0 ? 0 : (output_ > 0 ? speed_min_ : -speed_min_);
283-
} else if (std::abs(output_) < 900) {
283+
if (std::abs(output_) < 450) { // In (0, 450)
284+
output_ = std::abs(output_) < std::numeric_limits<double>::epsilon()
285+
? 0
286+
: std::copysign(speed_min_, output_);
287+
} else if (std::abs(output_) < 900) { // In [450, 900)
284288
output_ = 0;
285289
integral_ = 0;
286290
error_ = 0;
287-
// std::this_thread::sleep_for(500ms);
291+
// actual_ = 0;
292+
pub_status_();
288293
// ros2::sleep_for(500ms);
289294
}
290295

arc_base/src/twist_to_diff_drive_node.cpp

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
#include "arc_base/utils.hpp"
12
#include "geometry_msgs/msg/twist_stamped.hpp"
23
#include "rclcpp/executor.hpp"
34
#include "rclcpp/rclcpp.hpp"
45
#include "std_msgs/msg/float64.hpp"
5-
#include "arc_base/utils.hpp"
66
#include <cmath>
77
#include <cstdio>
88
#include <iostream>
@@ -73,12 +73,14 @@ class DifferentialDrive {
7373

7474
class TwistToDiffDriveNode : public rclcpp::Node {
7575
public:
76-
TwistToDiffDriveNode(double rate)
77-
: Node("twist_to_diff_drive_node"), rate_{rate},
76+
TwistToDiffDriveNode()
77+
: Node("twist_to_diff_drive_node"),
7878
diff_drive_model_(0.224, 0.15, GEARING, 1) {
7979
// Create a DifferentialDrive object with a wheel base of 224mm and a
8080
// wheel radius of 150mm
8181

82+
this->declare_parameter("controller_rate"); // Hz
83+
8284
// twist command subscriber
8385
twist_sub_ = this->create_subscription<TwistStampedMsg>(
8486
"cmd_vel", 10,
@@ -92,26 +94,32 @@ class TwistToDiffDriveNode : public rclcpp::Node {
9294
}
9395

9496
void spin() {
97+
double controller_rate =
98+
this->get_parameter("controller_rate").as_double();
99+
std::fprintf(stderr, "%s: %.5f\n", magenta("controller_rate").c_str(),
100+
controller_rate);
101+
ros2::Rate rate(controller_rate);
102+
95103
while (rclcpp::ok()) {
96104
// publish the left and right motor velocities
97105
if (publish_motor_speed(MotorSide::LEFT, left_motor_speed_)) {
98106
// RCLCPP_INFO(this->get_logger(), "Published left motor speed:
99107
// %0.5f", left_motor_speed_);
100108
}
101109
rclcpp::spin_some(this->get_node_base_interface());
102-
rate_.sleep();
110+
rate.sleep();
103111

104112
if (publish_motor_speed(MotorSide::RIGHT, right_motor_speed_)) {
105113
// RCLCPP_INFO(this->get_logger(), "Published right motor speed:
106114
// %0.5f", right_motor_speed_);
107115
}
108116
rclcpp::spin_some(this->get_node_base_interface());
109-
rate_.sleep();
117+
rate.sleep();
110118
}
111119
}
112120

113121
private:
114-
rclcpp::Rate rate_;
122+
// rclcpp::Rate rate_;
115123
Subscriber<TwistStampedMsg>::SharedPtr twist_sub_;
116124
Publisher<Float64Msg>::SharedPtr motor_speed_left_pub_;
117125
Publisher<Float64Msg>::SharedPtr motor_speed_right_pub_;
@@ -185,7 +193,7 @@ int main(int argc, char **argv) {
185193

186194
// rclcpp::executors::MultiThreadedExecutor executor;
187195

188-
auto node = std::make_shared<arc::TwistToDiffDriveNode>(10);
196+
auto node = std::make_shared<arc::TwistToDiffDriveNode>();
189197
node->spin();
190198
// executor.add_node(node);
191199

arc_bringup/config/params.yaml

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ low_level_motor_controller_left:
66
current_max: 2.0
77
speed_min: 900.0
88
speed_max: 23000.0
9-
acc_max: 0.1
9+
acc_max: 5000.0
1010
kp_speed: 1.0 # 0.004
1111
ki_speed: 0.0 # 0.004
1212
kd_speed: 0.0 # 0.0001
@@ -22,10 +22,14 @@ low_level_motor_controller_right:
2222
current_max: 2.0
2323
speed_min: 900.0
2424
speed_max: 23000.0
25-
acc_max: 0.1
25+
acc_max: 5000.0
2626
kp_speed: 1.0 # 0.004
2727
ki_speed: 0.0 # 0.004
2828
kd_speed: 0.0 # 0.0001
2929
kp_current: 0.004
3030
ki_current: 0.004
3131
kd_current: 0.0001
32+
33+
twist_to_diff_drive:
34+
ros__parameters:
35+
controller_rate: 1000.0

arc_bringup/launch/test-setup.launch.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,20 @@ def generate_launch_description():
9494
)
9595
)
9696

97-
# twist to diff drive
98-
twist_to_diff_drive = Node(
99-
package='arc_base',
100-
executable='twist_to_diff_drive_node',
101-
name='twist_to_diff_drive'
102-
)
103-
10497
config = os.path.join(
10598
get_package_share_directory('arc_bringup'),
10699
'config',
107100
'params.yaml'
108101
)
109102

103+
# twist to diff drive
104+
twist_to_diff_drive = Node(
105+
package='arc_base',
106+
executable='twist_to_diff_drive_node',
107+
name='twist_to_diff_drive',
108+
parameters=[config]
109+
)
110+
110111
print(green("config: " + config))
111112

112113
# low level motor controller for left motor

0 commit comments

Comments
 (0)