-
Notifications
You must be signed in to change notification settings - Fork 332
/
tricycle_controller.cpp
567 lines (494 loc) · 19.6 KB
/
tricycle_controller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
// Copyright 2022 Pixel Robotics.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* Author: Tony Najjar
*/
#include <memory>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#include "hardware_interface/types/hardware_interface_type_values.hpp"
#include "lifecycle_msgs/msg/state.hpp"
#include "rclcpp/logging.hpp"
#include "tf2/LinearMath/Quaternion.h"
#include "tricycle_controller/tricycle_controller.hpp"
namespace
{
constexpr auto DEFAULT_COMMAND_TOPIC = "~/cmd_vel";
constexpr auto DEFAULT_ACKERMANN_OUT_TOPIC = "~/cmd_ackermann";
constexpr auto DEFAULT_ODOMETRY_TOPIC = "~/odom";
constexpr auto DEFAULT_TRANSFORM_TOPIC = "/tf";
constexpr auto DEFAULT_RESET_ODOM_SERVICE = "~/reset_odometry";
} // namespace
namespace tricycle_controller
{
using namespace std::chrono_literals;
using controller_interface::interface_configuration_type;
using controller_interface::InterfaceConfiguration;
using hardware_interface::HW_IF_POSITION;
using hardware_interface::HW_IF_VELOCITY;
using lifecycle_msgs::msg::State;
TricycleController::TricycleController() : controller_interface::ControllerInterface() {}
CallbackReturn TricycleController::on_init()
{
try
{
// Create the parameter listener and get the parameters
param_listener_ = std::make_shared<ParamListener>(get_node());
params_ = param_listener_->get_params();
}
catch (const std::exception & e)
{
fprintf(stderr, "Exception thrown during init stage with message: %s \n", e.what());
return CallbackReturn::ERROR;
}
return CallbackReturn::SUCCESS;
}
InterfaceConfiguration TricycleController::command_interface_configuration() const
{
InterfaceConfiguration command_interfaces_config;
command_interfaces_config.type = interface_configuration_type::INDIVIDUAL;
command_interfaces_config.names.push_back(params_.traction_joint_name + "/" + HW_IF_VELOCITY);
command_interfaces_config.names.push_back(params_.steering_joint_name + "/" + HW_IF_POSITION);
return command_interfaces_config;
}
InterfaceConfiguration TricycleController::state_interface_configuration() const
{
InterfaceConfiguration state_interfaces_config;
state_interfaces_config.type = interface_configuration_type::INDIVIDUAL;
state_interfaces_config.names.push_back(params_.traction_joint_name + "/" + HW_IF_VELOCITY);
state_interfaces_config.names.push_back(params_.steering_joint_name + "/" + HW_IF_POSITION);
return state_interfaces_config;
}
controller_interface::return_type TricycleController::update(
const rclcpp::Time & time, const rclcpp::Duration & period)
{
if (get_lifecycle_state().id() == State::PRIMARY_STATE_INACTIVE)
{
if (!is_halted)
{
halt();
is_halted = true;
}
return controller_interface::return_type::OK;
}
std::shared_ptr<TwistStamped> last_command_msg;
received_velocity_msg_ptr_.get(last_command_msg);
if (last_command_msg == nullptr)
{
RCLCPP_WARN(get_node()->get_logger(), "Velocity message received was a nullptr.");
return controller_interface::return_type::ERROR;
}
const auto age_of_last_command = time - last_command_msg->header.stamp;
// Brake if cmd_vel has timeout, override the stored command
if (age_of_last_command > cmd_vel_timeout_)
{
last_command_msg->twist.linear.x = 0.0;
last_command_msg->twist.angular.z = 0.0;
}
// command may be limited further by Limiters,
// without affecting the stored twist command
TwistStamped command = *last_command_msg;
double & linear_command = command.twist.linear.x;
double & angular_command = command.twist.angular.z;
double Ws_read = traction_joint_[0].velocity_state.get().get_value(); // in radians/s
double alpha_read = steering_joint_[0].position_state.get().get_value(); // in radians
if (params_.open_loop)
{
odometry_.updateOpenLoop(linear_command, angular_command, period);
}
else
{
if (std::isnan(Ws_read) || std::isnan(alpha_read))
{
RCLCPP_ERROR(get_node()->get_logger(), "Could not read feedback value");
return controller_interface::return_type::ERROR;
}
odometry_.update(Ws_read, alpha_read, period);
}
tf2::Quaternion orientation;
orientation.setRPY(0.0, 0.0, odometry_.getHeading());
if (realtime_odometry_publisher_->trylock())
{
auto & odometry_message = realtime_odometry_publisher_->msg_;
odometry_message.header.stamp = time;
if (!params_.odom_only_twist)
{
odometry_message.pose.pose.position.x = odometry_.getX();
odometry_message.pose.pose.position.y = odometry_.getY();
odometry_message.pose.pose.orientation.x = orientation.x();
odometry_message.pose.pose.orientation.y = orientation.y();
odometry_message.pose.pose.orientation.z = orientation.z();
odometry_message.pose.pose.orientation.w = orientation.w();
}
odometry_message.twist.twist.linear.x = odometry_.getLinear();
odometry_message.twist.twist.angular.z = odometry_.getAngular();
realtime_odometry_publisher_->unlockAndPublish();
}
if (params_.enable_odom_tf && realtime_odometry_transform_publisher_->trylock())
{
auto & transform = realtime_odometry_transform_publisher_->msg_.transforms.front();
transform.header.stamp = time;
transform.transform.translation.x = odometry_.getX();
transform.transform.translation.y = odometry_.getY();
transform.transform.rotation.x = orientation.x();
transform.transform.rotation.y = orientation.y();
transform.transform.rotation.z = orientation.z();
transform.transform.rotation.w = orientation.w();
realtime_odometry_transform_publisher_->unlockAndPublish();
}
// Compute wheel velocity and angle
auto [alpha_write, Ws_write] = twist_to_ackermann(linear_command, angular_command);
// Reduce wheel speed until the target angle has been reached
double alpha_delta = abs(alpha_write - alpha_read);
double scale;
if (alpha_delta < M_PI / 6)
{
scale = 1;
}
else if (alpha_delta > M_PI_2)
{
scale = 0.01;
}
else
{
// TODO(anyone): find the best function, e.g convex power functions
scale = cos(alpha_delta);
}
Ws_write *= scale;
auto & last_command = previous_commands_.back();
auto & second_to_last_command = previous_commands_.front();
limiter_traction_.limit(
Ws_write, last_command.speed, second_to_last_command.speed, period.seconds());
limiter_steering_.limit(
alpha_write, last_command.steering_angle, second_to_last_command.steering_angle,
period.seconds());
previous_commands_.pop();
AckermannDrive ackermann_command;
// speed in AckermannDrive is defined as desired forward speed (m/s) but it is used here as wheel
// speed (rad/s)
ackermann_command.speed = static_cast<float>(Ws_write);
ackermann_command.steering_angle = static_cast<float>(alpha_write);
previous_commands_.emplace(ackermann_command);
// Publish ackermann command
if (params_.publish_ackermann_command && realtime_ackermann_command_publisher_->trylock())
{
auto & realtime_ackermann_command = realtime_ackermann_command_publisher_->msg_;
// speed in AckermannDrive is defined desired forward speed (m/s) but we use it here as wheel
// speed (rad/s)
realtime_ackermann_command.speed = static_cast<float>(Ws_write);
realtime_ackermann_command.steering_angle = static_cast<float>(alpha_write);
realtime_ackermann_command_publisher_->unlockAndPublish();
}
traction_joint_[0].velocity_command.get().set_value(Ws_write);
steering_joint_[0].position_command.get().set_value(alpha_write);
return controller_interface::return_type::OK;
}
CallbackReturn TricycleController::on_configure(const rclcpp_lifecycle::State & /*previous_state*/)
{
auto logger = get_node()->get_logger();
// update parameters if they have changed
if (param_listener_->is_old(params_))
{
params_ = param_listener_->get_params();
RCLCPP_INFO(logger, "Parameters were updated");
}
odometry_.setWheelParams(params_.wheelbase, params_.wheel_radius);
odometry_.setVelocityRollingWindowSize(params_.velocity_rolling_window_size);
cmd_vel_timeout_ = std::chrono::milliseconds{params_.cmd_vel_timeout};
params_.publish_ackermann_command =
get_node()->get_parameter("publish_ackermann_command").as_bool();
try
{
limiter_traction_ = TractionLimiter(
params_.traction.min_velocity, params_.traction.max_velocity,
params_.traction.min_acceleration, params_.traction.max_acceleration,
params_.traction.min_deceleration, params_.traction.max_deceleration,
params_.traction.min_jerk, params_.traction.max_jerk);
}
catch (const std::invalid_argument & e)
{
RCLCPP_ERROR(get_node()->get_logger(), "Error configuring traction limiter: %s", e.what());
return CallbackReturn::ERROR;
}
try
{
limiter_steering_ = SteeringLimiter(
params_.steering.min_position, params_.steering.max_position, params_.steering.min_velocity,
params_.steering.max_velocity, params_.steering.min_acceleration,
params_.steering.max_acceleration);
}
catch (const std::invalid_argument & e)
{
RCLCPP_ERROR(get_node()->get_logger(), "Error configuring steering limiter: %s", e.what());
return CallbackReturn::ERROR;
}
if (!reset())
{
return CallbackReturn::ERROR;
}
const TwistStamped empty_twist;
received_velocity_msg_ptr_.set(std::make_shared<TwistStamped>(empty_twist));
// Fill last two commands with default constructed commands
const AckermannDrive empty_ackermann_drive;
previous_commands_.emplace(empty_ackermann_drive);
previous_commands_.emplace(empty_ackermann_drive);
// initialize ackermann command publisher
if (params_.publish_ackermann_command)
{
ackermann_command_publisher_ = get_node()->create_publisher<AckermannDrive>(
DEFAULT_ACKERMANN_OUT_TOPIC, rclcpp::SystemDefaultsQoS());
realtime_ackermann_command_publisher_ =
std::make_shared<realtime_tools::RealtimePublisher<AckermannDrive>>(
ackermann_command_publisher_);
}
// initialize command subscriber
velocity_command_subscriber_ = get_node()->create_subscription<TwistStamped>(
DEFAULT_COMMAND_TOPIC, rclcpp::SystemDefaultsQoS(),
[this](const std::shared_ptr<TwistStamped> msg) -> void
{
if (!subscriber_is_active_)
{
RCLCPP_WARN(get_node()->get_logger(), "Can't accept new commands. subscriber is inactive");
return;
}
if ((msg->header.stamp.sec == 0) && (msg->header.stamp.nanosec == 0))
{
RCLCPP_WARN_ONCE(
get_node()->get_logger(),
"Received TwistStamped with zero timestamp, setting it to current "
"time, this message will only be shown once");
msg->header.stamp = get_node()->get_clock()->now();
}
received_velocity_msg_ptr_.set(std::move(msg));
});
// initialize odometry publisher and message
odometry_publisher_ = get_node()->create_publisher<nav_msgs::msg::Odometry>(
DEFAULT_ODOMETRY_TOPIC, rclcpp::SystemDefaultsQoS());
realtime_odometry_publisher_ =
std::make_shared<realtime_tools::RealtimePublisher<nav_msgs::msg::Odometry>>(
odometry_publisher_);
auto & odometry_message = realtime_odometry_publisher_->msg_;
odometry_message.header.frame_id = params_.odom_frame_id;
odometry_message.child_frame_id = params_.base_frame_id;
// initialize odom values zeros
odometry_message.twist =
geometry_msgs::msg::TwistWithCovariance(rosidl_runtime_cpp::MessageInitialization::ALL);
constexpr size_t NUM_DIMENSIONS = 6;
for (size_t index = 0; index < 6; ++index)
{
// 0, 7, 14, 21, 28, 35
const size_t diagonal_index = NUM_DIMENSIONS * index + index;
odometry_message.pose.covariance[diagonal_index] = params_.pose_covariance_diagonal[index];
odometry_message.twist.covariance[diagonal_index] = params_.twist_covariance_diagonal[index];
}
// initialize transform publisher and message
if (params_.enable_odom_tf)
{
odometry_transform_publisher_ = get_node()->create_publisher<tf2_msgs::msg::TFMessage>(
DEFAULT_TRANSFORM_TOPIC, rclcpp::SystemDefaultsQoS());
realtime_odometry_transform_publisher_ =
std::make_shared<realtime_tools::RealtimePublisher<tf2_msgs::msg::TFMessage>>(
odometry_transform_publisher_);
// keeping track of odom and base_link transforms only
auto & odometry_transform_message = realtime_odometry_transform_publisher_->msg_;
odometry_transform_message.transforms.resize(1);
odometry_transform_message.transforms.front().header.frame_id = params_.odom_frame_id;
odometry_transform_message.transforms.front().child_frame_id = params_.base_frame_id;
}
// Create odom reset service
reset_odom_service_ = get_node()->create_service<std_srvs::srv::Empty>(
DEFAULT_RESET_ODOM_SERVICE, std::bind(
&TricycleController::reset_odometry, this, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3));
return CallbackReturn::SUCCESS;
}
CallbackReturn TricycleController::on_activate(const rclcpp_lifecycle::State &)
{
RCLCPP_INFO(get_node()->get_logger(), "On activate: Initialize Joints");
// Initialize the joints
const auto wheel_front_result = get_traction(params_.traction_joint_name, traction_joint_);
const auto steering_result = get_steering(params_.steering_joint_name, steering_joint_);
if (wheel_front_result == CallbackReturn::ERROR || steering_result == CallbackReturn::ERROR)
{
return CallbackReturn::ERROR;
}
if (traction_joint_.empty() || steering_joint_.empty())
{
RCLCPP_ERROR(
get_node()->get_logger(), "Either steering or traction interfaces are non existent");
return CallbackReturn::ERROR;
}
is_halted = false;
subscriber_is_active_ = true;
RCLCPP_DEBUG(get_node()->get_logger(), "Subscriber and publisher are now active.");
return CallbackReturn::SUCCESS;
}
CallbackReturn TricycleController::on_deactivate(const rclcpp_lifecycle::State &)
{
subscriber_is_active_ = false;
return CallbackReturn::SUCCESS;
}
CallbackReturn TricycleController::on_cleanup(const rclcpp_lifecycle::State &)
{
if (!reset())
{
return CallbackReturn::ERROR;
}
received_velocity_msg_ptr_.set(std::make_shared<TwistStamped>());
return CallbackReturn::SUCCESS;
}
CallbackReturn TricycleController::on_error(const rclcpp_lifecycle::State &)
{
if (!reset())
{
return CallbackReturn::ERROR;
}
return CallbackReturn::SUCCESS;
}
void TricycleController::reset_odometry(
const std::shared_ptr<rmw_request_id_t> /*request_header*/,
const std::shared_ptr<std_srvs::srv::Empty::Request> /*req*/,
std::shared_ptr<std_srvs::srv::Empty::Response> /*res*/)
{
odometry_.resetOdometry();
RCLCPP_INFO(get_node()->get_logger(), "Odometry successfully reset");
}
bool TricycleController::reset()
{
odometry_.resetOdometry();
// release the old queue
std::queue<AckermannDrive> empty_ackermann_drive;
std::swap(previous_commands_, empty_ackermann_drive);
traction_joint_.clear();
steering_joint_.clear();
subscriber_is_active_ = false;
velocity_command_subscriber_.reset();
received_velocity_msg_ptr_.set(nullptr);
is_halted = false;
return true;
}
CallbackReturn TricycleController::on_shutdown(const rclcpp_lifecycle::State &)
{
return CallbackReturn::SUCCESS;
}
void TricycleController::halt()
{
traction_joint_[0].velocity_command.get().set_value(0.0);
steering_joint_[0].position_command.get().set_value(0.0);
}
CallbackReturn TricycleController::get_traction(
const std::string & traction_joint_name, std::vector<TractionHandle> & joint)
{
RCLCPP_INFO(get_node()->get_logger(), "Get Wheel Joint Instance");
// Lookup the velocity state interface
const auto state_handle = std::find_if(
state_interfaces_.cbegin(), state_interfaces_.cend(),
[&traction_joint_name](const auto & interface)
{
return interface.get_prefix_name() == traction_joint_name &&
interface.get_interface_name() == HW_IF_VELOCITY;
});
if (state_handle == state_interfaces_.cend())
{
RCLCPP_ERROR(
get_node()->get_logger(), "Unable to obtain joint state handle for %s",
traction_joint_name.c_str());
return CallbackReturn::ERROR;
}
// Lookup the velocity command interface
const auto command_handle = std::find_if(
command_interfaces_.begin(), command_interfaces_.end(),
[&traction_joint_name](const auto & interface)
{
return interface.get_prefix_name() == traction_joint_name &&
interface.get_interface_name() == HW_IF_VELOCITY;
});
if (command_handle == command_interfaces_.end())
{
RCLCPP_ERROR(
get_node()->get_logger(), "Unable to obtain joint state handle for %s",
traction_joint_name.c_str());
return CallbackReturn::ERROR;
}
// Create the traction joint instance
joint.emplace_back(TractionHandle{std::ref(*state_handle), std::ref(*command_handle)});
return CallbackReturn::SUCCESS;
}
CallbackReturn TricycleController::get_steering(
const std::string & steering_joint_name, std::vector<SteeringHandle> & joint)
{
RCLCPP_INFO(get_node()->get_logger(), "Get Steering Joint Instance");
// Lookup the velocity state interface
const auto state_handle = std::find_if(
state_interfaces_.cbegin(), state_interfaces_.cend(),
[&steering_joint_name](const auto & interface)
{
return interface.get_prefix_name() == steering_joint_name &&
interface.get_interface_name() == HW_IF_POSITION;
});
if (state_handle == state_interfaces_.cend())
{
RCLCPP_ERROR(
get_node()->get_logger(), "Unable to obtain joint state handle for %s",
steering_joint_name.c_str());
return CallbackReturn::ERROR;
}
// Lookup the velocity command interface
const auto command_handle = std::find_if(
command_interfaces_.begin(), command_interfaces_.end(),
[&steering_joint_name](const auto & interface)
{
return interface.get_prefix_name() == steering_joint_name &&
interface.get_interface_name() == HW_IF_POSITION;
});
if (command_handle == command_interfaces_.end())
{
RCLCPP_ERROR(
get_node()->get_logger(), "Unable to obtain joint state handle for %s",
steering_joint_name.c_str());
return CallbackReturn::ERROR;
}
// Create the steering joint instance
joint.emplace_back(SteeringHandle{std::ref(*state_handle), std::ref(*command_handle)});
return CallbackReturn::SUCCESS;
}
double TricycleController::convert_trans_rot_vel_to_steering_angle(
double Vx, double theta_dot, double wheelbase)
{
if (theta_dot == 0 || Vx == 0)
{
return 0;
}
return std::atan(theta_dot * wheelbase / Vx);
}
std::tuple<double, double> TricycleController::twist_to_ackermann(double Vx, double theta_dot)
{
// using naming convention in http://users.isr.ist.utl.pt/~mir/cadeiras/robmovel/Kinematics.pdf
double alpha, Ws;
if (Vx == 0 && theta_dot != 0)
{ // is spin action
alpha = theta_dot > 0 ? M_PI_2 : -M_PI_2;
Ws = abs(theta_dot) * params_.wheelbase / params_.wheel_radius;
return std::make_tuple(alpha, Ws);
}
alpha = convert_trans_rot_vel_to_steering_angle(Vx, theta_dot, params_.wheelbase);
Ws = Vx / (params_.wheel_radius * std::cos(alpha));
return std::make_tuple(alpha, Ws);
}
} // namespace tricycle_controller
#include <pluginlib/class_list_macros.hpp>
PLUGINLIB_EXPORT_CLASS(
tricycle_controller::TricycleController, controller_interface::ControllerInterface)