Autonomous Racing  1
f1tenth Project Group of Technical University Dortmund, Germany
drive_parameters_source.cpp
Go to the documentation of this file.
2 #include <cmath>
3 
4 DriveParametersSource::DriveParametersSource(ros::NodeHandle* node_handle, const char* topic,
5  DriveParameterCallbackFunction update_callback, DriveMode drive_mode,
6  ros::Duration timeout)
7 {
8  ROS_ASSERT_MSG(drive_mode != DriveMode::LOCKED, "Don't define a drive parameter source for the LOCKED mode.");
9 
10  this->m_drive_parameters_subscriber =
11  node_handle->subscribe<drive_msgs::drive_param>(topic, 1, &DriveParametersSource::driveParametersCallback,
12  this);
13  this->m_drive_mode = drive_mode;
14  this->m_idle = true;
15  this->m_updateCallback = update_callback;
16  this->m_timeout = timeout;
17  this->m_last_update = ros::Time();
18 }
19 
20 void DriveParametersSource::driveParametersCallback(const drive_msgs::drive_param::ConstPtr& message)
21 {
22  this->m_last_update = ros::Time::now();
23  this->m_idle = std::abs(message->velocity) < IDLE_RANGE && std::abs(message->angle) < IDLE_RANGE;
24  this->m_updateCallback(this, message);
25 }
26 
28 {
29  if (this->m_last_update.is_zero())
30  {
31  return true;
32  }
33  return this->m_last_update + this->m_timeout < ros::Time::now();
34 }
35 
37 {
38  return this->m_idle;
39 }
40 
42 {
43  return this->m_drive_mode;
44 }
constexpr float IDLE_RANGE
bool isOutdated()
Returns true if no update was received for a certain time, determined by the timeout variable...
DriveMode
Definition: drive_mode.h:3
DriveParametersSource(ros::NodeHandle *node_handle, const char *topic, DriveParameterCallbackFunction update_callback, DriveMode drive_mode, ros::Duration timeout)
Construct a new DriveParametersSource object, subscribes to the topic and stores the parameters into ...
bool isIdle()
Returns true if the last update contained values close to 0 for both steering and throttle...
std::function< void(DriveParametersSource *, const drive_msgs::drive_param::ConstPtr &)> DriveParameterCallbackFunction