24namespace std_ph = std::placeholders;
32 declare_parameter(
"lightbar_cda_table");
33 declare_parameter(
"lightbar_ind_table");
34 declare_parameter(
"lightbar_priorities");
35 lbm_ = std::make_shared<LightBarManagerWorker>();
43 RCLCPP_INFO_STREAM(rclcpp::get_logger(
"lightbar_manager"),
"Initalizing lightbar manager node...");
59 rclcpp::Parameter lightbar_cda_table_param = get_parameter(
"lightbar_cda_table");
60 if (lightbar_cda_table_param.get_type() != rclcpp::ParameterType::PARAMETER_NOT_SET)
63 rclcpp::Parameter lightbar_ind_table_param = get_parameter(
"lightbar_ind_table");
64 if (lightbar_ind_table_param.get_type() != rclcpp::ParameterType::PARAMETER_NOT_SET)
69 throw std::invalid_argument(
"Size of lightbar_cda_table is not same as that of lightbar_ind_table");
75 lbm_->setIndicatorControllers();
79 lbm_->light_status.push_back(
OFF);
81 rclcpp::Parameter lightbar_priorities_param = get_parameter(
"lightbar_priorities");
82 if (lightbar_priorities_param.get_type() != rclcpp::ParameterType::PARAMETER_NOT_SET)
91 RCLCPP_INFO_STREAM(rclcpp::get_logger(
"lightbar_manager"),
"Loaded params: " <<
config_);
96 if (denied_list.size() != 0)
100 RCLCPP_WARN_STREAM(rclcpp::get_logger(
"lightbar_manager"),
"In fuction " << __FUNCTION__ <<
", LightBarManager was not able to take control of all green indicators."
101 <<
".\n Please check priority list rosparameter, and ensure " <<
node_name_ <<
" has the highest priority."
102 <<
".\n If this is intended, pass false to normal_operation argument. Exiting...");
107 RCLCPP_WARN_STREAM(rclcpp::get_logger(
"lightbar_manager"),
"In fuction " << __FUNCTION__ <<
", LightBarManager was not able to take control of all green indicators."
108 <<
".\n Resuming...");
111 return CallbackReturn::SUCCESS;
119 return CallbackReturn::SUCCESS;
125 std::vector<LightBarIndicator> all_indicators;
127 RCLCPP_INFO_STREAM(rclcpp::get_logger(
"lightbar_manager"),
"LightBarManager was commanded to turn off all indicators!");
129 for (std::pair <LightBarIndicator, std::string> element :
lbm_->getIndicatorControllers())
130 all_indicators.push_back(element.first);
133 lbm_->setIndicatorControllers();
137 int response_code = 0;
138 for (
auto indicator : all_indicators)
141 if (response_code != 0)
142 RCLCPP_WARN_STREAM(rclcpp::get_logger(
"lightbar_manager"),
"In Function " << __FUNCTION__ <<
": LightBarManager was not able to turn off indicator ID:"
143 << indicator <<
". Response code: " << response_code);
147 all_indicators.erase(std::remove(all_indicators.begin(), all_indicators.end(),
GREEN_FLASH), all_indicators.end());
148 all_indicators.erase(std::remove(all_indicators.begin(), all_indicators.end(),
GREEN_SOLID), all_indicators.end());
153 const std::shared_ptr<carma_msgs::srv::RequestIndicatorControl::Request> req,
154 std::shared_ptr<carma_msgs::srv::RequestIndicatorControl::Response> resp)
156 std::vector<LightBarIndicator> ind_list, controlled_ind_list;
157 std::vector<LightBarCDAType> controlled_cda_type_list;
160 if (req->cda_list.size() != 0)
162 for (
auto cda_type : req->cda_list)
167 ind_list.push_back(
lbm_->getIndicatorFromCDAType(
static_cast<LightBarCDAType>(cda_type.type)));
172 for (
auto indicator : req->ind_list)
182 controlled_ind_list =
lbm_->requestControl(ind_list, req->requester_name);
184 for (
auto indicator : controlled_ind_list)
189 controlled_cda_type_list.push_back(
lbm_->getCDATypeFromIndicator(indicator));
199 resp->cda_list =
lbm_->getMsg(controlled_cda_type_list);
200 resp->ind_list =
lbm_->getMsg(controlled_ind_list);
205 const std::shared_ptr<carma_msgs::srv::ReleaseIndicatorControl::Request> req,
206 std::shared_ptr<carma_msgs::srv::ReleaseIndicatorControl::Response> resp)
208 std::vector<LightBarIndicator> ind_list;
211 if (req->cda_list.size() != 0)
213 for (
auto cda_type : req->cda_list)
218 ind_list.push_back(
lbm_->getIndicatorFromCDAType(
static_cast<LightBarCDAType>(cda_type.type)));
223 for (
auto indicator : req->ind_list)
231 lbm_->releaseControl(ind_list, req->requester_name);
245 lbm_->handleStateChange(*msg_ptr);
249 if (curr_lightbar_state != prev_lightbar_state)
251 switch(curr_lightbar_state)
282 std::vector<lightbar_manager::LightBarIndicator> changed_turn_signal =
lbm_->handleTurnSignal(msg);
285 if (changed_turn_signal.empty())
292 if (msg.turn_signal == automotive_platform_msgs::msg::TurnSignalCommand::NONE)
302 if (
lbm_->requestControl(changed_turn_signal,
node_name_).empty())
304 int response_code = 0;
306 if (response_code != 0)
307 RCLCPP_ERROR_STREAM(rclcpp::get_logger(
"lightbar_manager"),
"In Function " << __FUNCTION__ <<
": LightBarManager was not able to set light of indicator ID:"
308 << changed_turn_signal[0] <<
". Response code: " << response_code);
312 std::string turn_string = msg.turn_signal == automotive_platform_msgs::msg::TurnSignalCommand::LEFT ?
"left" :
"right";
313 RCLCPP_WARN_STREAM(rclcpp::get_logger(
"lightbar_manager"),
"Lightbar was not able to take control of lightbar to indicate " << turn_string <<
"turn!");
337 int response_code = 0;
338 if (
static_cast<uint8_t
>(ind) >=
INDICATOR_COUNT ||
static_cast<uint8_t
>(ind_status) > 1)
342 return response_code;
346 std::string current_controller =
lbm_->getIndicatorControllers()[ind];
347 if (requester_name ==
"" || current_controller != requester_name)
349 RCLCPP_WARN_STREAM(rclcpp::get_logger(
"lightbar_manager"),requester_name <<
" failed to set the LightBarIndicator ID" << ind
350 <<
" as this was already controlled by " << current_controller);
352 return response_code;
355 std::vector<IndicatorStatus> light_status_proposed =
lbm_->setIndicator(ind, ind_status, requester_name);
356 carma_driver_msgs::msg::LightBarStatus msg =
lbm_->getLightBarStatusMsg(light_status_proposed);
357 auto srv = std::make_shared<carma_driver_msgs::srv::SetLights::Request>();
358 srv->set_state = msg;
362 auto future_status = resp.wait_for(std::chrono::milliseconds(100));
364 if (future_status == std::future_status::ready)
367 lbm_->light_status = light_status_proposed;
372 RCLCPP_WARN_STREAM(rclcpp::get_logger(
"lightbar_manager"),
"In function: " << __FUNCTION__ <<
": Failed to set lights. ROSservice call to the driver failed. ");
375 return response_code;
380 const std::shared_ptr<carma_driver_msgs::srv::SetLightBarIndicator::Request> req,
381 std::shared_ptr<carma_driver_msgs::srv::SetLightBarIndicator::Response> resp)
384 int response_code = 0;
385 if (req->cda_type.type != NULL)
386 indicator =
lbm_->getIndicatorFromCDAType(
static_cast<LightBarCDAType>(req->cda_type.type));
391 resp->status_code = response_code;
392 if (response_code != 0)
void turnOffAll()
Miscellaneous function that forces the state to disengaged and turn off all indicators....
carma_ros2_utils::PubPtr< carma_msgs::msg::LightBarIndicatorControllers > indicator_control_publisher_
void turnSignalCallback(automotive_platform_msgs::msg::TurnSignalCommand::UniquePtr msg_ptr)
Callback function for turning signal.
carma_ros2_utils::SubPtr< automotive_platform_msgs::msg::TurnSignalCommand > turn_signal_subscriber_
carma_ros2_utils::ClientPtr< carma_driver_msgs::srv::SetLights > lightbar_driver_client_
carma_ros2_utils::CallbackReturn handle_on_configure(const rclcpp_lifecycle::State &)
bool requestControlCallBack(const std::shared_ptr< rmw_request_id_t >, const std::shared_ptr< carma_msgs::srv::RequestIndicatorControl::Request > req, std::shared_ptr< carma_msgs::srv::RequestIndicatorControl::Response > resp)
std::shared_ptr< LightBarManagerWorker > getWorker()
Get ptr to lightbar_manager_worker (for ease of unit testing)
carma_ros2_utils::ServicePtr< carma_msgs::srv::ReleaseIndicatorControl > release_control_server_
void processTurnSignal(const automotive_platform_msgs::msg::TurnSignalCommand &msg)
Turn signal callback function helper.
bool releaseControlCallBack(const std::shared_ptr< rmw_request_id_t >, const std::shared_ptr< carma_msgs::srv::ReleaseIndicatorControl::Request > req, std::shared_ptr< carma_msgs::srv::ReleaseIndicatorControl::Response > resp)
carma_ros2_utils::CallbackReturn handle_on_activate(const rclcpp_lifecycle::State &)
carma_ros2_utils::SubPtr< carma_planning_msgs::msg::GuidanceState > guidance_state_subscriber_
int setIndicator(LightBarIndicator ind, IndicatorStatus ind_status, const std::string &requester_name)
Try to turn the given indicator ON or OFF (comm with hardware) upon the given component's request.
std::map< lightbar_manager::LightBarIndicator, std::string > prev_owners_before_turn_
std::shared_ptr< LightBarManagerWorker > lbm_
void stateChangeCallBack(carma_planning_msgs::msg::GuidanceState::UniquePtr msg_ptr)
LightBarManager(const rclcpp::NodeOptions &)
carma_ros2_utils::ServicePtr< carma_driver_msgs::srv::SetLightBarIndicator > set_indicator_server_
carma_ros2_utils::ServicePtr< carma_msgs::srv::RequestIndicatorControl > request_control_server_
rclcpp::TimerBase::SharedPtr pub_timer_
bool setIndicatorCallBack(const std::shared_ptr< rmw_request_id_t >, const std::shared_ptr< carma_driver_msgs::srv::SetLightBarIndicator::Request > req, std::shared_ptr< carma_driver_msgs::srv::SetLightBarIndicator::Response > resp)
std::vector< std::string > lightbar_priorities
std::vector< std::string > lightbar_cda_table
std::vector< std::string > lightbar_ind_table