Autonomous Racing  1
f1tenth Project Group of Technical University Dortmund, Germany
drive_parameters_multiplexer.cpp
Go to the documentation of this file.
2 
4  : m_drive_mode{ DriveMode::LOCKED }
5 {
6  this->m_drive_parameters_publisher = this->m_node_handle.advertise<drive_msgs::drive_param>(TOPIC_DRIVE_PARAM, 1);
7  this->m_last_updated_source = NULL;
8  auto callback =
9  std::bind(&DriveParametersMultiplexer::onUpdate, this, std::placeholders::_1, std::placeholders::_2);
10 
11  this->m_sources = {
12  std::move(std::make_unique<DriveParametersSource>(&this->m_node_handle, TOPIC_DRIVE_PARAMETERS_KEYBOARD,
13  callback, DriveMode::MANUAL, ros::Duration(0.1))),
14  std::move(std::make_unique<DriveParametersSource>(&this->m_node_handle, TOPIC_DRIVE_PARAMETERS_JOYSTICK,
15  callback, DriveMode::MANUAL, ros::Duration(0.1))),
16  std::move(std::make_unique<DriveParametersSource>(&this->m_node_handle, TOPIC_DRIVE_PARAMETERS_AUTONOMOUS,
17  callback, DriveMode::AUTONOMOUS, ros::Duration(0.1))),
18  };
19  this->m_drive_mode_subscriber =
20  this->m_node_handle.subscribe<std_msgs::Int32>(TOPIC_DRIVE_MODE, 1,
21  &DriveParametersMultiplexer::driveModeCallback, this);
22 }
23 
24 // clang-format off
25 bool DriveParametersMultiplexer::validateSource(DriveParametersSource* source)
26 {
27  ROS_ASSERT_MSG(source != nullptr, "Parameter 'source' must not be null.");
28 
29  if (source->getDriveMode() != this->m_drive_mode)
30  {
31  return false;
32  }
33 
34  return this->m_last_updated_source == nullptr
35  || this->m_last_updated_source == source
36  || this->m_last_updated_source->isOutdated()
37  || this->m_last_updated_source->getDriveMode() != this->m_drive_mode
38  || (!source->isIdle() && this->m_last_updated_source->isIdle());
39 }
40 // clang-format on
41 
42 void DriveParametersMultiplexer::onUpdate(DriveParametersSource* source,
43  const drive_msgs::drive_param::ConstPtr& message)
44 {
45  ROS_ASSERT_MSG(source != nullptr, "Parameter 'source' must not be null.");
46  if (!this->validateSource(source))
47  {
48  return;
49  }
50  this->m_drive_parameters_publisher.publish(message);
51  this->m_last_updated_source = source;
52 }
53 
54 void DriveParametersMultiplexer::driveModeCallback(const std_msgs::Int32::ConstPtr& drive_mode_message)
55 {
56  this->m_drive_mode = (DriveMode)drive_mode_message->data;
57 }
58 
59 int main(int argc, char** argv)
60 {
61  ros::init(argc, argv, "drive_parameters_multiplexer");
62  DriveParametersMultiplexer multiplexer;
63  ros::spin();
64  return EXIT_SUCCESS;
65 }
constexpr const char * TOPIC_DRIVE_MODE
int main(int argc, char **argv)
constexpr const char * TOPIC_DRIVE_PARAMETERS_KEYBOARD
bool isOutdated()
Returns true if no update was received for a certain time, determined by the timeout variable...
constexpr const char * TOPIC_DRIVE_PARAMETERS_AUTONOMOUS
DriveMode
Definition: drive_mode.h:3
constexpr const char * TOPIC_DRIVE_PARAMETERS_JOYSTICK
bool isIdle()
Returns true if the last update contained values close to 0 for both steering and throttle...
constexpr const char * TOPIC_DRIVE_PARAM
DriveParametersMultiplexer()
Construct a new DriveParametersMultiplexer object and initialize sources for all publishers of drive ...