Skip to content

Commit

Permalink
Car 5920 Traffic Incident ROS2 Migration (#230)
Browse files Browse the repository at this point in the history
<!-- Thanks for the contribution, this is awesome. -->

# PR Details
## Description

This PR migrates the `traffic_incident` package to ROS2.

## Related GitHub Issue

NA

## Related Jira Key

[CAR-5920](https://usdot-carma.atlassian.net/browse/CAR-5920)

## Motivation and Context

Migration to ROS2

## How Has This Been Tested?

1.  Set the config and launched using `carma start all`
2. Entered the `messenger_ros2` container using `docker exec -it
messenger_ros2 bash`
3. Verified that `/traffic_incident` shows up when running `ros2 node
list`
4. Verified that the state of `/traffic_incident` automatically switches
to `active` using `ros2 lifecycle get /traffic_incident`
5. Open the CARMA Messenger GUI and started a traffic event broadcast in
the event management plugin
a. Verified that the correct message starts publishing on
`/outgoing_mobility_operation`
    b. Verified that publishing stops after clicking "Stop Broadcast"

## Types of changes

- [ ] Defect fix (non-breaking change that fixes an issue)
- [x] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that cause existing functionality
to change)

## Checklist:

- [ ] I have added any new packages to the sonar-scanner.properties file
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have read the
[**CONTRIBUTING**](https://github.com/usdot-fhwa-stol/carma-platform/blob/develop/Contributing.md)
document.
- [ ] I have added tests to cover my changes.
- [x] All new and existing tests passed.


[CAR-5920]:
https://usdot-carma.atlassian.net/browse/CAR-5920?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
  • Loading branch information
john-chrosniak authored Jul 16, 2024
1 parent 274a6b0 commit bb52d1b
Show file tree
Hide file tree
Showing 14 changed files with 691 additions and 629 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<!--
Copyright (C) 2018-2021 LEIDOS.
Copyright (C) 2024 LEIDOS.
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
Expand Down Expand Up @@ -36,7 +36,4 @@
args="pub -l /system_alert cav_msgs/SystemAlert '{ type: 5, description: Simulated Drivers Ready }'"
/>

<!-- Traffic Incident Node -->
<include file="$(find traffic_incident)/launch/traffic_incident.launch"/>

</launch>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022-2023 LEIDOS.
# Copyright (C) 2024 LEIDOS.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -85,11 +85,20 @@ def generate_launch_description():
]
)

traffic_incident_group = GroupAction(
actions=[
IncludeLaunchDescription(
PythonLaunchDescriptionSource([get_package_share_directory('traffic_incident'), '/launch', '/traffic_incident.launch.py']),
),
]
)

return LaunchDescription([
declare_configuration_delay_arg,
declare_route_file_folder,
transform_group,
v2x_group,
plugins_group,
ui_group
ui_group,
traffic_incident_group
])
107 changes: 38 additions & 69 deletions carma-messenger-core/traffic_incident/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (C) 2020-2021 LEIDOS.
# Copyright (C) 2024 LEIDOS.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
Expand All @@ -13,91 +13,60 @@
# License for the specific language governing permissions and limitations under
# the License.

cmake_minimum_required(VERSION 2.8.3)
cmake_minimum_required(VERSION 3.5)
project(traffic_incident)

find_package(carma_cmake_common REQUIRED)
carma_check_ros_version(1)
carma_check_ros_version(2)
carma_package()

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
carma_utils
cav_msgs
cav_srvs
gps_common
roscpp
)


###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
INCLUDE_DIRS include
# LIBRARIES traffic_incident
CATKIN_DEPENDS carma_utils cav_msgs gps_common roscpp
# DEPENDS system_lib
)
find_package(ament_cmake_auto REQUIRED)
ament_auto_find_build_dependencies()

###########
## Build ##
###########
# Name build targets
set(node_exec traffic_node_exec)
set(node_lib traffic_node)

## Specify additional locations of header files
## Your package locations should be listed before other locations
# Includes
include_directories(
include
${catkin_INCLUDE_DIRS}
)

add_executable(traffic_incident_node src/main.cpp src/traffic_incident_node.cpp)
ament_export_include_directories(include)

add_library(traffic_incident_worker_lib src/traffic_incident_worker.cpp)
add_dependencies(traffic_incident_worker_lib ${catkin_EXPORTED_TARGETS})
# Build
ament_auto_add_library(${node_lib} SHARED
src/traffic_incident_node.cpp
src/traffic_incident_worker.cpp
)

## Specify libraries to link a library or executable target against
target_link_libraries(traffic_incident_node traffic_incident_worker_lib ${catkin_LIBRARIES})
ament_auto_add_executable(${node_exec}
src/main.cpp
)

#############
## Install ##
#############
# Register component
rclcpp_components_register_nodes(${node_lib} "traffic::TrafficIncidentNode")

## Mark executables for installation
install(TARGETS traffic_incident_node traffic_incident_worker_lib
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# All locally created targets will need to be manually linked
# ament auto will handle linking of external dependencies
target_link_libraries(${node_exec}
${node_lib}
)

## Mark cpp header files for installation
install(DIRECTORY include/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
)
# Testing
if(BUILD_TESTING)

## Mark other files for installation (e.g. launch and bag files, etc.)
install(DIRECTORY
launch
config
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies() # This populates the ${${PROJECT_NAME}_FOUND_TEST_DEPENDS} variable

#############
## Testing ##
#############
ament_add_gtest(test_traffic_incident test/test_traffic.cpp)

## Add gtest based cpp test target and link libraries
catkin_add_gmock(${PROJECT_NAME}-test
test/main.cpp
test/test_traffic.cpp
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/test # Add test directory as working directory for unit tests
)
target_link_libraries(${PROJECT_NAME}-test traffic_incident_worker_lib ${catkin_LIBRARIES})
ament_target_dependencies(test_traffic_incident ${${PROJECT_NAME}_FOUND_TEST_DEPENDS})

target_link_libraries(test_traffic_incident ${node_lib})

endif()

ament_auto_package(
INSTALL_TO_SHARE config launch
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (C) 2024 LEIDOS.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

#ifndef TRAFFIC_INCIDENT_H
#define TRAFFIC_INCIDENT_H

#include <functional>
#include <string>

#include "traffic_incident_worker.hpp"
#include <carma_ros2_utils/carma_ros2_utils.hpp>
#include <rclcpp/rclcpp.hpp>
#include <std_srvs/srv/trigger.hpp>

#include <carma_msgs/srv/set_traffic_event.hpp>
#include <carma_v2x_msgs/msg/mobility_operation.hpp>
#include <gps_msgs/msg/gps_fix.hpp>

namespace traffic
{
class TrafficIncidentNode : public carma_ros2_utils::CarmaLifecycleNode
{
public:
/*! \fn TrafficIncidentNode()
\brief TrafficIncidentNode constructor
*/
explicit TrafficIncidentNode(const rclcpp::NodeOptions &);

/*! \fn publishTrafficIncidentMobilityOperation()
\brief Publish mobility operation message
*/
void publishTrafficIncidentMobilityOperation(
const carma_v2x_msgs::msg::MobilityOperation & traffic_msg);

// Service callback
bool startTrafficBroadcastCallback(
const std::shared_ptr<rmw_request_id_t>,
const carma_msgs::srv::SetTrafficEvent::Request::SharedPtr req,
carma_msgs::srv::SetTrafficEvent::Response::SharedPtr resp);
bool stopTrafficBroadcastCallback(
const std::shared_ptr<rmw_request_id_t>,
const std_srvs::srv::Trigger::Request::SharedPtr req,
std_srvs::srv::Trigger::Response::SharedPtr resp);

private:
carma_ros2_utils::CallbackReturn handle_on_configure(const rclcpp_lifecycle::State &);

carma_ros2_utils::CallbackReturn handle_on_activate(const rclcpp_lifecycle::State &);

void spin_callback(void);

// subscriber
carma_ros2_utils::SubPtr<gps_msgs::msg::GPSFix> pinpoint_driver_sub_;

// publisher
carma_ros2_utils::PubPtr<carma_v2x_msgs::msg::MobilityOperation> traffic_mobility_operation_pub_;

// services
carma_ros2_utils::ServicePtr<carma_msgs::srv::SetTrafficEvent>
start_broadcast_request_service_server;
carma_ros2_utils::ServicePtr<std_srvs::srv::Trigger> stop_broadcast_request_service_server;

// timer
rclcpp::TimerBase::SharedPtr spin_timer_;

// TrafficIncidentWorker class object
TrafficIncidentWorker traffic_worker_;

// ROS Params
std::string sender_id_;
std::string event_reason_;
std::string event_type_;
double down_track_;
double up_track_;
double min_gap_;
};

} // namespace traffic

#endif /* TRAFFIC_INCIDENT_H */
Loading

0 comments on commit bb52d1b

Please sign in to comment.