Skip to content

Commit 733c844

Browse files
authored
add max_dec param to separate max acc/dec acceleration (#34)
Signed-off-by: Daisuke Sato <[email protected]>
1 parent 1d5baae commit 733c844

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

cabot_base/config/cabot2-common.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ cabot:
6262
cmd_vel_topic: /cabot/cmd_vel
6363
pause_control_topic: /cabot/pause_control
6464
max_acc: 1.2
65+
max_dec: -1.2
6566
target_rate: 20
6667
bias: 1.0 # NEED_TO_BE_CONFIGURED
6768
gain_omega: 1.0 # NEED_TO_BE_CONFIGURED

motor_controller/motor_adapter/include/motor_adapter/odriver_adapter.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class ODriverNode : public rclcpp::Node
7070

7171
int targetRate_;
7272
double maxAcc_;
73+
double maxDec_;
7374

7475
double bias_;
7576
double wheel_diameter_;

motor_controller/motor_adapter/src/odriver_adapter.cpp

+22-8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ ODriverNode::ODriverNode(rclcpp::NodeOptions options)
5555

5656
targetRate_(40),
5757
maxAcc_(0.5),
58+
maxDec_(-0.5),
5859

5960
bias_(0),
6061
wheel_diameter_(0),
@@ -95,6 +96,7 @@ ODriverNode::ODriverNode(rclcpp::NodeOptions options)
9596
imuSub = create_subscription<sensor_msgs::msg::Imu>("/imu", rclcpp::SensorDataQoS(), std::bind(&ODriverNode::imuCallback, this, _1));
9697

9798
maxAcc_ = declare_parameter("max_acc", maxAcc_);
99+
maxDec_ = declare_parameter("max_dec", maxDec_);
98100
targetRate_ = declare_parameter("target_rate", targetRate_);
99101
motorOutput_ = declare_parameter("motor_topic", motorOutput_);
100102

@@ -125,23 +127,35 @@ void ODriverNode::cmdVelLoop(int publishRate)
125127

126128
motorPub = create_publisher<odriver_msgs::msg::MotorTarget>(motorOutput_, 10);
127129

128-
double minimumStep = maxAcc_ / publishRate;
130+
double maxAccStep = maxAcc_ / publishRate;
131+
double maxDecStep = maxDec_ / publishRate;
129132

130133
while (rclcpp::ok()) {
131134
odriver_msgs::msg::MotorTarget target;
132135

133136
double targetL = targetSpdLinear_;
134137
double targetT = targetSpdTurn_;
135138

136-
// change linear speed by maximum acc rate
137-
double lDiff = targetL - currentSpdLinear_;
138-
if (fabs(lDiff) < minimumStep) {
139-
currentSpdLinear_ = targetL;
139+
if (targetL == 0.0) {
140+
// change linear speed by maximum dec rate, if the control is zero (maybe not specified)
141+
double lDiff = targetL - currentSpdLinear_;
142+
if (fabs(lDiff) < fabs(maxDecStep)) {
143+
currentSpdLinear_ = targetL;
144+
} else {
145+
currentSpdLinear_ -= maxDecStep * lDiff / fabs(lDiff);
146+
}
140147
} else {
141-
currentSpdLinear_ += minimumStep * lDiff / fabs(lDiff);
148+
// change linear speed by maximum acc rate
149+
double lDiff = targetL - currentSpdLinear_;
150+
if (fabs(lDiff) < maxAccStep) {
151+
currentSpdLinear_ = targetL;
152+
} else {
153+
currentSpdLinear_ += maxAccStep * lDiff / fabs(lDiff);
154+
}
142155
}
143156

144157
// adjust angular speed
158+
target.header.stamp = get_clock()->now();
145159
target.spd_left = currentSpdLinear_ - targetT;
146160
target.spd_right = currentSpdLinear_ + targetT;
147161

@@ -188,10 +202,10 @@ void ODriverNode::cmdVelLoop(int publishRate)
188202
} else {
189203
// reduce current speed to zero at maximum acc rate when feedback is disabled
190204
double lDiff = 0.0 - currentSpdLinear_;
191-
if (fabs(lDiff) < minimumStep) {
205+
if (fabs(lDiff) < fabs(maxDecStep)) {
192206
currentSpdLinear_ = 0.0;
193207
} else {
194-
currentSpdLinear_ += minimumStep * lDiff / fabs(lDiff);
208+
currentSpdLinear_ -= maxDecStep * lDiff / fabs(lDiff);
195209
}
196210

197211
// reset integrator when feedback is disabled

0 commit comments

Comments
 (0)