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.
GeofenceScheduler.hpp
Go to the documentation of this file.
1#pragma once
2/*
3 * Copyright (C) 2022 LEIDOS.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 * use this file except in compliance with the License. You may obtain a copy of
7 * the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17#include <functional>
18#include <mutex>
19#include <memory>
20#include <unordered_map>
22#include <carma_ros2_utils/timers/Timer.hpp>
23#include <carma_ros2_utils/timers/TimerFactory.hpp>
24#include <carma_ros2_utils/timers/ROSTimerFactory.hpp>
25#include <gtest/gtest_prod.h>
26
27
28namespace carma_wm_ctrl
29{
35{
36 using Timer = carma_ros2_utils::timers::Timer;
37 using TimerFactory = carma_ros2_utils::timers::TimerFactory;
38 using ROSTimerFactory = carma_ros2_utils::timers::ROSTimerFactory;
39 using TimerPtr = std::unique_ptr<Timer>;
40
41 std::mutex mutex_;
42 std::shared_ptr<TimerFactory> timerFactory_;
43 std::unordered_map<uint32_t, std::pair<TimerPtr, bool>> timers_; // Pairing of timers with their Id and valid status
44 std::unique_ptr<Timer> deletion_timer_;
45 std::function<void(std::shared_ptr<Geofence>)> active_callback_;
46 std::function<void(std::shared_ptr<Geofence>)> inactive_callback_;
47 uint32_t next_id_ = 0; // Timer id counter
48 rcl_clock_type_t clock_type_ = RCL_SYSTEM_TIME;
49
50public:
57 GeofenceScheduler(std::shared_ptr<TimerFactory> timerFactory);
58
65 void addGeofence(std::shared_ptr<Geofence> gf_ptr);
66
72 void onGeofenceActive(std::function<void(std::shared_ptr<Geofence>)> active_callback);
78 void onGeofenceInactive(std::function<void(std::shared_ptr<Geofence>)> inactive_callback);
79
83 void clearTimers();
84
88 rcl_clock_type_t getClockType();
89
93 rclcpp::Time now();
94
95private:
101 uint32_t nextId();
102
112 void startGeofenceCallback(std::shared_ptr<Geofence> gf_ptr, const unsigned int schedule_id, const int32_t timer_id);
122 void endGeofenceCallback(std::shared_ptr<Geofence> gf_ptr, const unsigned int schedule_id, const int32_t timer_id);
123
124};
125} // namespace carma_wm_ctrl
A GeofenceScheduler is responsable for notifying the user when a geofence is active or inactive accor...
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...
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...
std::unique_ptr< Timer > TimerPtr
rcl_clock_type_t getClockType()
Get the clock type of the clock being created by the timer factory.
GeofenceScheduler(std::shared_ptr< TimerFactory > timerFactory)
Constructor which takes in a TimerFactory. Timers from this factory will be used to generate the trig...
std::function< void(std::shared_ptr< Geofence >)> active_callback_
carma_ros2_utils::timers::ROSTimerFactory ROSTimerFactory
rclcpp::Time now()
Get current time used by scheduler.
carma_ros2_utils::timers::Timer Timer
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...
uint32_t nextId()
Generates the next id to be used for a timer.
std::unique_ptr< Timer > deletion_timer_
std::function< void(std::shared_ptr< Geofence >)> inactive_callback_
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 goe...
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-act...
std::unordered_map< uint32_t, std::pair< TimerPtr, bool > > timers_
carma_ros2_utils::timers::TimerFactory TimerFactory
std::shared_ptr< TimerFactory > timerFactory_
void clearTimers()
Clears the expired timers from the memory of this scheduler.