Carma-platform v4.2.0
CARMA Platform is built on robot operating system (ROS) and utilizes open source software (OSS) that enables Cooperative Driving Automation (CDA) features to allow Automated Driving Systems to interact and cooperate with infrastructure and other vehicles through communication.
carma_wm_ctrl::GeofenceScheduler Class Reference

A GeofenceScheduler is responsable for notifying the user when a geofence is active or inactive according to its schedule. More...

#include <GeofenceScheduler.hpp>

Collaboration diagram for carma_wm_ctrl::GeofenceScheduler:
Collaboration graph

Public Member Functions

 GeofenceScheduler (std::shared_ptr< TimerFactory > timerFactory)
 Constructor which takes in a TimerFactory. Timers from this factory will be used to generate the triggers for goefence activity. More...
 
void addGeofence (std::shared_ptr< Geofence > gf_ptr)
 Add a geofence to the scheduler. This will cause it to trigger an event when it becomes active or goes inactive according to its schedule. More...
 
void onGeofenceActive (std::function< void(std::shared_ptr< Geofence >)> active_callback)
 Method which allows the user to set a callback which will be triggered when a geofence becomes active. More...
 
void onGeofenceInactive (std::function< void(std::shared_ptr< Geofence >)> inactive_callback)
 Method which allows the user to set a callback which will be triggered when a geofence becomes in-active. More...
 
void clearTimers ()
 Clears the expired timers from the memory of this scheduler. More...
 
rcl_clock_type_t getClockType ()
 Get the clock type of the clock being created by the timer factory. More...
 
rclcpp::Time now ()
 Get current time used by scheduler. More...
 

Private Types

using Timer = carma_ros2_utils::timers::Timer
 
using TimerFactory = carma_ros2_utils::timers::TimerFactory
 
using ROSTimerFactory = carma_ros2_utils::timers::ROSTimerFactory
 
using TimerPtr = std::unique_ptr< Timer >
 

Private Member Functions

uint32_t nextId ()
 Generates the next id to be used for a timer. More...
 
void startGeofenceCallback (std::shared_ptr< Geofence > gf_ptr, const unsigned int schedule_id, const int32_t timer_id)
 The callback which is triggered when a geofence becomes active This will call the user set active_callback set from the onGeofenceActive function. More...
 
void endGeofenceCallback (std::shared_ptr< Geofence > gf_ptr, const unsigned int schedule_id, const int32_t timer_id)
 The callback which is triggered when a geofence becomes in-active This will call the user set inactive_callback set from the onGeofenceInactive function. More...
 

Private Attributes

std::mutex mutex_
 
std::shared_ptr< TimerFactorytimerFactory_
 
std::unordered_map< uint32_t, std::pair< TimerPtr, bool > > timers_
 
std::unique_ptr< Timerdeletion_timer_
 
std::function< void(std::shared_ptr< Geofence >)> active_callback_
 
std::function< void(std::shared_ptr< Geofence >)> inactive_callback_
 
uint32_t next_id_ = 0
 
rcl_clock_type_t clock_type_ = RCL_SYSTEM_TIME
 

Detailed Description

A GeofenceScheduler is responsable for notifying the user when a geofence is active or inactive according to its schedule.

Definition at line 34 of file GeofenceScheduler.hpp.

Member Typedef Documentation

◆ ROSTimerFactory

using carma_wm_ctrl::GeofenceScheduler::ROSTimerFactory = carma_ros2_utils::timers::ROSTimerFactory
private

Definition at line 38 of file GeofenceScheduler.hpp.

◆ Timer

using carma_wm_ctrl::GeofenceScheduler::Timer = carma_ros2_utils::timers::Timer
private

Definition at line 36 of file GeofenceScheduler.hpp.

◆ TimerFactory

using carma_wm_ctrl::GeofenceScheduler::TimerFactory = carma_ros2_utils::timers::TimerFactory
private

Definition at line 37 of file GeofenceScheduler.hpp.

◆ TimerPtr

using carma_wm_ctrl::GeofenceScheduler::TimerPtr = std::unique_ptr<Timer>
private

Definition at line 39 of file GeofenceScheduler.hpp.

Constructor & Destructor Documentation

◆ GeofenceScheduler()

carma_wm_ctrl::GeofenceScheduler::GeofenceScheduler ( std::shared_ptr< TimerFactory timerFactory)

Constructor which takes in a TimerFactory. Timers from this factory will be used to generate the triggers for goefence activity.

Parameters
timerFactoryA pointer to a TimerFactory which can be used to generate timers for geofence triggers.

Definition at line 23 of file GeofenceScheduler.cpp.

24 : timerFactory_(timerFactory)
25{
26 // Create repeating loop to clear geofence timers which are no longer needed
28 timerFactory_->buildTimer(nextId(), rclcpp::Duration(1), std::bind(&GeofenceScheduler::clearTimers, this));
29 clock_type_ = timerFactory_->now().get_clock_type();
30}
uint32_t nextId()
Generates the next id to be used for a timer.
std::unique_ptr< Timer > deletion_timer_
std::shared_ptr< TimerFactory > timerFactory_
void clearTimers()
Clears the expired timers from the memory of this scheduler.

References clearTimers(), clock_type_, deletion_timer_, nextId(), and timerFactory_.

Here is the call graph for this function:

Member Function Documentation

◆ addGeofence()

void carma_wm_ctrl::GeofenceScheduler::addGeofence ( std::shared_ptr< Geofence gf_ptr)

Add a geofence to the scheduler. This will cause it to trigger an event when it becomes active or goes inactive according to its schedule.

Parameters
geofenceThe geofence to be added

Definition at line 70 of file GeofenceScheduler.cpp.

71{
72 std::lock_guard<std::mutex> guard(mutex_);
73
74 RCLCPP_INFO_STREAM(rclcpp::get_logger("carma_wm_ctrl"), "Attempting to add Geofence with Id: " << gf_ptr->id_);
75
76 // Create timer for next start time
77 for (size_t schedule_idx = 0; schedule_idx < gf_ptr->schedules.size(); schedule_idx++)
78 {
79 // resolve clock type
80 auto interval_info = gf_ptr->schedules[schedule_idx].getNextInterval(timerFactory_->now());
81 rclcpp::Time startTime = interval_info.second;
82
83 if (!interval_info.first && startTime == rclcpp::Time(0, 0, clock_type_))
84 {
85 RCLCPP_WARN_STREAM(rclcpp::get_logger("carma_wm_ctrl"),
86 "Failed to add geofence as its schedule did not contain an active or upcoming control period. GF Id: "
87 << gf_ptr->id_);
88 return;
89 }
90 // If this geofence is currently active set the start time to now
91 if (interval_info.first)
92 {
93 startTime = timerFactory_->now();
94 }
95
96 int32_t timer_id = nextId();
97
98 rclcpp::Duration control_duration = rclcpp::Duration(std::max((startTime - timerFactory_->now()).seconds() * 1e9, 0.0)); //guard for negative value
99
100 // Build timer to trigger when this geofence becomes active
101 TimerPtr timer = timerFactory_->buildTimer(
102 timer_id, control_duration,
103 std::bind(&GeofenceScheduler::startGeofenceCallback, this, gf_ptr, schedule_idx, timer_id), true, true);
104
105 timers_[timer_id] = std::make_pair(std::move(timer), false); // Add start timer to map by Id
106 }
107
108}
std::unique_ptr< Timer > TimerPtr
void startGeofenceCallback(std::shared_ptr< Geofence > gf_ptr, const unsigned int schedule_id, const int32_t timer_id)
The callback which is triggered when a geofence becomes active This will call the user set active_cal...
std::unordered_map< uint32_t, std::pair< TimerPtr, bool > > timers_

References clock_type_, mutex_, nextId(), startGeofenceCallback(), timerFactory_, and timers_.

Referenced by carma_wm_ctrl::WMBroadcaster::scheduleGeofence().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ clearTimers()

void carma_wm_ctrl::GeofenceScheduler::clearTimers ( )

Clears the expired timers from the memory of this scheduler.

Definition at line 48 of file GeofenceScheduler.cpp.

49{
50 std::lock_guard<std::mutex> guard(mutex_);
51 auto it = timers_.begin();
52
53 // Erase all expired timers_ using iterators to ensure operation is safe
54 while (it != timers_.end())
55 {
56 // Check if timer is marked for deletion
57 if (it->second.second)
58 {
59 // erase() function returns the iterator of the next
60 // to last deleted element.
61 it = timers_.erase(it);
62 }
63 else
64 {
65 it++;
66 }
67 }
68}

References mutex_, and timers_.

Referenced by GeofenceScheduler().

Here is the caller graph for this function:

◆ endGeofenceCallback()

void carma_wm_ctrl::GeofenceScheduler::endGeofenceCallback ( std::shared_ptr< Geofence gf_ptr,
const unsigned int  schedule_id,
const int32_t  timer_id 
)
private

The callback which is triggered when a geofence becomes in-active This will call the user set inactive_callback set from the onGeofenceInactive function.

Parameters
eventThe record of the timer event causing this to trigger
gfThe geofence which is being un-activated
schedule_idindex number of the schedule being used corresponding to this geofence
timer_idThe id of the timer which caused this callback to occur

Definition at line 132 of file GeofenceScheduler.cpp.

133{
134 std::lock_guard<std::mutex> guard(mutex_);
135
136 RCLCPP_INFO_STREAM(rclcpp::get_logger("carma_wm_ctrl"), "Deactivating Geofence with Id: " << gf_ptr->id_);
137
138 inactive_callback_(gf_ptr);
139 timers_[timer_id].second = true; // Mark timer for deletion
140
141 // Determine if a new timer is needed for this geofence
142 auto interval_info = gf_ptr->schedules[schedule_id].getNextInterval(timerFactory_->now());
143 rclcpp::Time startTime = interval_info.second;
144
145 // If this geofence should currently be active set the start time to now
146 if (interval_info.first)
147 {
148 startTime = timerFactory_->now();
149 }
150
151 if (!interval_info.first && startTime == rclcpp::Time(0, 0, clock_type_))
152 {
153 // No more active periods for this geofence so return
154 return;
155 }
156
157 // Build timer to trigger when this geofence becomes active
158 int32_t start_timer_id = nextId();
159
160 rclcpp::Duration control_duration = rclcpp::Duration(std::max((startTime - timerFactory_->now()).seconds() * 1e9, 0.0)); //guard for negative value
161
162 TimerPtr timer = timerFactory_->buildTimer(
163 start_timer_id, control_duration,
164 std::bind(&GeofenceScheduler::startGeofenceCallback, this, gf_ptr, schedule_id, start_timer_id), true, true);
165
166 timers_[start_timer_id] = std::make_pair(std::move(timer), false); // Add start timer to map by Id
167}
std::function< void(std::shared_ptr< Geofence >)> inactive_callback_

References clock_type_, inactive_callback_, mutex_, nextId(), startGeofenceCallback(), timerFactory_, and timers_.

Referenced by startGeofenceCallback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getClockType()

rcl_clock_type_t carma_wm_ctrl::GeofenceScheduler::getClockType ( )

Get the clock type of the clock being created by the timer factory.

Definition at line 43 of file GeofenceScheduler.cpp.

44{
45 return clock_type_;
46}

References clock_type_.

Referenced by carma_wm_ctrl::WMBroadcaster::addScheduleFromMsg(), and carma_wm_ctrl::WMBroadcaster::controlRequestFromRoute().

Here is the caller graph for this function:

◆ nextId()

uint32_t carma_wm_ctrl::GeofenceScheduler::nextId ( )
private

Generates the next id to be used for a timer.

Returns
The next available timer id

Definition at line 37 of file GeofenceScheduler.cpp.

38{
39 next_id_++;
40 return next_id_;
41}

References next_id_.

Referenced by GeofenceScheduler(), addGeofence(), endGeofenceCallback(), and startGeofenceCallback().

Here is the caller graph for this function:

◆ now()

rclcpp::Time carma_wm_ctrl::GeofenceScheduler::now ( )

Get current time used by scheduler.

Definition at line 32 of file GeofenceScheduler.cpp.

33{
34 return timerFactory_->now();
35}

References timerFactory_.

Referenced by carma_wm_ctrl::WMBroadcaster::pubTCMACK().

Here is the caller graph for this function:

◆ onGeofenceActive()

void carma_wm_ctrl::GeofenceScheduler::onGeofenceActive ( std::function< void(std::shared_ptr< Geofence >)>  active_callback)

Method which allows the user to set a callback which will be triggered when a geofence becomes active.

Parameters
active_callbackThe callback which will be triggered

Definition at line 169 of file GeofenceScheduler.cpp.

170{
171 std::lock_guard<std::mutex> guard(mutex_);
172 active_callback_ = active_callback;
173}
std::function< void(std::shared_ptr< Geofence >)> active_callback_

References active_callback_, and mutex_.

Referenced by carma_wm_ctrl::WMBroadcaster::WMBroadcaster().

Here is the caller graph for this function:

◆ onGeofenceInactive()

void carma_wm_ctrl::GeofenceScheduler::onGeofenceInactive ( std::function< void(std::shared_ptr< Geofence >)>  inactive_callback)

Method which allows the user to set a callback which will be triggered when a geofence becomes in-active.

Parameters
inactive_callbackThe callback which will be triggered

Definition at line 175 of file GeofenceScheduler.cpp.

176{
177 std::lock_guard<std::mutex> guard(mutex_);
178 inactive_callback_ = inactive_callback;
179}

References inactive_callback_, and mutex_.

Referenced by carma_wm_ctrl::WMBroadcaster::WMBroadcaster().

Here is the caller graph for this function:

◆ startGeofenceCallback()

void carma_wm_ctrl::GeofenceScheduler::startGeofenceCallback ( std::shared_ptr< Geofence gf_ptr,
const unsigned int  schedule_id,
const int32_t  timer_id 
)
private

The callback which is triggered when a geofence becomes active This will call the user set active_callback set from the onGeofenceActive function.

Parameters
eventThe record of the timer event causing this to trigger
gfThe geofence which is being activated
schedule_idindex number of the schedule being used corresponding to this geofence
timer_idThe id of the timer which caused this callback to occur

Definition at line 110 of file GeofenceScheduler.cpp.

111{
112 std::lock_guard<std::mutex> guard(mutex_);
113 rclcpp::Time endTime = timerFactory_->now() + gf_ptr->schedules[schedule_id].control_span_;
114
115 RCLCPP_INFO_STREAM(rclcpp::get_logger("carma_wm_ctrl"), "Activating Geofence with Id: " << gf_ptr->id_ << ", which will end at:" << endTime.seconds());
116
117 active_callback_(gf_ptr);
118
119 // Build timer to trigger when this geofence becomes inactive
120 int32_t ending_timer_id = nextId();
121
122 rclcpp::Duration control_duration = rclcpp::Duration(std::max((endTime - timerFactory_->now()).seconds() * 1e9, 0.0)); //guard for negative value
123
124 TimerPtr timer = timerFactory_->buildTimer(
125 ending_timer_id, control_duration,
126 std::bind(&GeofenceScheduler::endGeofenceCallback, this, gf_ptr, schedule_id, ending_timer_id), true, true);
127 timers_[ending_timer_id] = std::make_pair(std::move(timer), false); // Add end timer to map by Id
128
129 timers_[timer_id].second = true; // Mark start timer for deletion
130}
void endGeofenceCallback(std::shared_ptr< Geofence > gf_ptr, const unsigned int schedule_id, const int32_t timer_id)
The callback which is triggered when a geofence becomes in-active This will call the user set inactiv...

References active_callback_, endGeofenceCallback(), mutex_, nextId(), timerFactory_, and timers_.

Referenced by addGeofence(), and endGeofenceCallback().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ active_callback_

std::function<void(std::shared_ptr<Geofence>)> carma_wm_ctrl::GeofenceScheduler::active_callback_
private

Definition at line 45 of file GeofenceScheduler.hpp.

Referenced by onGeofenceActive(), and startGeofenceCallback().

◆ clock_type_

rcl_clock_type_t carma_wm_ctrl::GeofenceScheduler::clock_type_ = RCL_SYSTEM_TIME
private

◆ deletion_timer_

std::unique_ptr<Timer> carma_wm_ctrl::GeofenceScheduler::deletion_timer_
private

Definition at line 44 of file GeofenceScheduler.hpp.

Referenced by GeofenceScheduler().

◆ inactive_callback_

std::function<void(std::shared_ptr<Geofence>)> carma_wm_ctrl::GeofenceScheduler::inactive_callback_
private

Definition at line 46 of file GeofenceScheduler.hpp.

Referenced by endGeofenceCallback(), and onGeofenceInactive().

◆ mutex_

std::mutex carma_wm_ctrl::GeofenceScheduler::mutex_
private

◆ next_id_

uint32_t carma_wm_ctrl::GeofenceScheduler::next_id_ = 0
private

Definition at line 47 of file GeofenceScheduler.hpp.

Referenced by nextId().

◆ timerFactory_

std::shared_ptr<TimerFactory> carma_wm_ctrl::GeofenceScheduler::timerFactory_
private

◆ timers_

std::unordered_map<uint32_t, std::pair<TimerPtr, bool> > carma_wm_ctrl::GeofenceScheduler::timers_
private

The documentation for this class was generated from the following files: