Skip to content

Commit

Permalink
add safety limiter package (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyuYamamoto authored May 6, 2024
1 parent ab372cd commit 4ea576b
Show file tree
Hide file tree
Showing 8 changed files with 522 additions and 8 deletions.
13 changes: 12 additions & 1 deletion navyu_navigation/launch/navyu_bringup.launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@


def generate_launch_description():
use_rviz = LaunchConfiguration("use_rviz", default="true")
use_sim_time = LaunchConfiguration("use_sim_time", default="true")

map_path = PathJoinSubstitution([FindPackageShare("navyu_navigation"), "map", "map.yaml"])
Expand All @@ -43,6 +42,10 @@ def generate_launch_description():
[FindPackageShare("navyu_path_tracker"), "config", "navyu_path_tracker_params.yaml"]
)

navyu_safety_limiter_config = PathJoinSubstitution(
[FindPackageShare("navyu_safety_limiter"), "config", "navyu_safety_limiter_params.yaml"]
)

lifecycle_node_list = ["map_server"]

navyu_launch_path = PathJoinSubstitution([FindPackageShare("navyu_navigation"), "launch"])
Expand Down Expand Up @@ -73,8 +76,16 @@ def generate_launch_description():
package="navyu_path_tracker",
executable="navyu_path_tracker_node",
name="navyu_path_tracker_node",
remappings=[("/cmd_vel", "/cmd_vel_in")],
parameters=[navyu_path_tracker_config, {"use_sim_time": use_sim_time}],
),
Node(
package="navyu_safety_limiter",
executable="navyu_safety_limiter_node",
name="safety_limiter_node",
remappings=[("/cmd_vel_out", "/cmd_vel")],
parameters=[navyu_safety_limiter_config, {"use_sim_time": use_sim_time}],
),
Node(
package="nav2_lifecycle_manager",
executable="lifecycle_manager",
Expand Down
54 changes: 47 additions & 7 deletions navyu_navigation/rviz/navyu.rviz
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Panels:
Property Tree Widget:
Expanded:
- /Global Options1
- /Grid1
Splitter Ratio: 0.5
Tree Height: 1079
- Class: rviz_common/Selection
Expand Down Expand Up @@ -318,7 +319,7 @@ Visualization Manager:
Enabled: true
Name: Nearest Path Point
Namespaces:
"": true
{}
Topic:
Depth: 5
Durability Policy: Volatile
Expand All @@ -331,7 +332,7 @@ Visualization Manager:
Enabled: true
Name: Target Path Point
Namespaces:
"": true
{}
Topic:
Depth: 5
Durability Policy: Volatile
Expand All @@ -340,6 +341,45 @@ Visualization Manager:
Reliability Policy: Reliable
Value: /target_point_marker
Value: true
- Alpha: 1
Class: rviz_default_plugins/Polygon
Color: 255; 255; 0
Enabled: true
Name: Slowdown Polygon
Topic:
Depth: 5
Durability Policy: Volatile
Filter size: 10
History Policy: Keep Last
Reliability Policy: Reliable
Value: /slowdown_polygon
Value: true
- Alpha: 1
Class: rviz_default_plugins/Polygon
Color: 255; 0; 0
Enabled: true
Name: Stop Polygon
Topic:
Depth: 5
Durability Policy: Volatile
Filter size: 10
History Policy: Keep Last
Reliability Policy: Reliable
Value: /stop_polygon
Value: true
- Class: rviz_default_plugins/Marker
Enabled: true
Name: Safety Status
Namespaces:
text: true
Topic:
Depth: 5
Durability Policy: Volatile
Filter size: 10
History Policy: Keep Last
Reliability Policy: Reliable
Value: /current_state_marker
Value: true
Enabled: true
Global Options:
Background Color: 48; 48; 48
Expand Down Expand Up @@ -385,7 +425,7 @@ Visualization Manager:
Value: true
Views:
Current:
Angle: 2.2700035572052
Angle: -0.9049999713897705
Class: rviz_default_plugins/TopDownOrtho
Enable Stereo Rendering:
Stereo Eye Separation: 0.05999999865889549
Expand All @@ -395,11 +435,11 @@ Visualization Manager:
Invert Z Axis: false
Name: Current View
Near Clip Distance: 0.009999999776482582
Scale: -66.70034790039062
Target Frame: <Fixed Frame>
Scale: 158.93878173828125
Target Frame: base_link
Value: TopDownOrtho (rviz_default_plugins)
X: 7.501063346862793
Y: -1.1170053482055664
X: 0.2840563654899597
Y: -0.15071021020412445
Saved: ~
Window Geometry:
Displays:
Expand Down
39 changes: 39 additions & 0 deletions navyu_safety_limiter/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
cmake_minimum_required(VERSION 3.5)
project(navyu_safety_limiter)

if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic )
endif()

find_package(backward_ros REQUIRED)
find_package(ament_cmake_auto REQUIRED)
ament_auto_find_build_dependencies()

ament_auto_add_library(${PROJECT_NAME} SHARED
src/${PROJECT_NAME}.cpp
)

rclcpp_components_register_node(${PROJECT_NAME}
PLUGIN "NavyuSafetyLimiter"
EXECUTABLE ${PROJECT_NAME}_node
)

add_backward(${PROJECT_NAME})

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()

ament_auto_package(
INSTALL_TO_SHARE
launch
config
)

install(FILES DESTINATION share/${PROJECT_NAME})
11 changes: 11 additions & 0 deletions navyu_safety_limiter/config/navyu_safety_limiter_params.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
safety_limiter_node:
ros__parameters:
base_frame: base_link
slowdown_ratio: 0.45
collision_points_threshold_in_polygon: 6
scan_timeout: 1.0
robot_radius: 0.5
length: 0.5
scale: 1.5
slowdown_polygon: [-0.4, 0.5, -0.4, -0.5, 0.7, -0.5, 0.7, 0.5]
stop_polygon: [-0.3, 0.4, -0.3, -0.4, 0.5, -0.4, 0.5, 0.4]
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2024 RyuYamamoto.
//
// 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 NAVYU_SAFETY_LIMITER__NAVYU_SAFETY_LIMITER_HPP_
#define NAVYU_SAFETY_LIMITER__NAVYU_SAFETY_LIMITER_HPP_

#include <laser_geometry/laser_geometry.hpp>
#include <rclcpp/rclcpp.hpp>
#include <tf2_eigen/tf2_eigen.hpp>

#include <geometry_msgs/msg/polygon.hpp>
#include <geometry_msgs/msg/polygon_stamped.hpp>
#include <geometry_msgs/msg/twist.hpp>
#include <sensor_msgs/msg/laser_scan.hpp>
#include <visualization_msgs/msg/marker.hpp>
#include <visualization_msgs/msg/marker_array.hpp>

#include <pcl/common/transforms.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl_conversions/pcl_conversions.h>
#include <tf2_ros/buffer.h>
#include <tf2_ros/transform_listener.h>

enum Type {
STOP = 0,
SLOWDOWN,
};

struct Polygon
{
std::string name_;
bool update_dynamic_{false};
double length_;
double scale_;
double robot_radius_;
geometry_msgs::msg::Polygon polygon_;
Type type_;

geometry_msgs::msg::Polygon update_polygon(geometry_msgs::msg::Twist twist)
{
if (!update_dynamic_) return polygon_;

geometry_msgs::msg::Polygon polygon;

geometry_msgs::msg::Point32 p1, p2, p3, p4;
p1.x = 0.0;
p1.y = robot_radius_;
p2.x = 0.0;
p2.y = -1 * robot_radius_;
p3.x = length_ + twist.linear.x * scale_;
p3.y = -1 * robot_radius_;
p4.x = length_ + twist.linear.x * scale_;
p4.y = robot_radius_;

polygon.points.emplace_back(p1);
polygon.points.emplace_back(p2);
polygon.points.emplace_back(p3);
polygon.points.emplace_back(p4);

return polygon;
}
};

using PolygonPublisher = rclcpp::Publisher<geometry_msgs::msg::PolygonStamped>::SharedPtr;

class NavyuSafetyLimiter : public rclcpp::Node
{
public:
explicit NavyuSafetyLimiter(const rclcpp::NodeOptions & node_options);
~NavyuSafetyLimiter();

void callback_laser_scan(const sensor_msgs::msg::LaserScan::SharedPtr msg);
void callback_cmd_vel(const geometry_msgs::msg::Twist msg);

geometry_msgs::msg::Polygon create_polygon(std::vector<double> vertex);

bool check_collision_points_in_polygon(
const geometry_msgs::msg::Polygon polygon,
const pcl::PointCloud<pcl::PointXYZ>::Ptr collison_points);

bool get_transform(
const std::string target_frame, const std::string source_frame,
geometry_msgs::msg::TransformStamped & frame);

private:
rclcpp::Subscription<sensor_msgs::msg::LaserScan>::SharedPtr scan_subscriber_;
rclcpp::Subscription<geometry_msgs::msg::Twist>::SharedPtr cmd_vel_in_subscriber_;
rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr cmd_vel_out_publisher_;
rclcpp::Publisher<visualization_msgs::msg::Marker>::SharedPtr current_state_marker_publisher_;
std::map<std::string, PolygonPublisher> polygon_pubilsher_;

tf2_ros::Buffer tf_buffer_{get_clock()};
tf2_ros::TransformListener listener_{tf_buffer_};

laser_geometry::LaserProjection projection_;

geometry_msgs::msg::Twist cmd_vel_in_;
geometry_msgs::msg::Twist current_control_twist_;

std::vector<Polygon> polygons_;

std::string base_frame_;
double slowdown_ratio_;
int collision_points_threshold_in_polygon_;

std::string current_state_{"NONE"};
};

#endif
43 changes: 43 additions & 0 deletions navyu_safety_limiter/launch/navyu_safety_limiter.launch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2024 RyuYamamoto.
#
# 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

from launch_ros.actions import Node

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
from launch.substitutions import PathJoinSubstitution
from launch_ros.substitutions import FindPackageShare


def generate_launch_description():
use_sim_time = LaunchConfiguration("use_sim_time", default="true")

safety_limiter_config_path = PathJoinSubstitution(
[FindPackageShare("navyu_safety_limiter"), "config", "navyu_safety_limiter_params.yaml"]
)

return LaunchDescription(
[
DeclareLaunchArgument("use_sim_time", default_value="true"),
Node(
package="navyu_safety_limiter",
executable="navyu_safety_limiter_node",
name="safety_limiter_node",
output="screen",
remappings=[("/cmd_vel_out", "cmd_vel")],
parameters=[safety_limiter_config_path, {"use_sim_time": use_sim_time}],
),
]
)
31 changes: 31 additions & 0 deletions navyu_safety_limiter/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0"?>
<package format="3">
<name>navyu_safety_limiter</name>
<version>0.1.0</version>
<description>The navyu_safety_limiter package</description>
<maintainer email="[email protected]">RyuYamamoto</maintainer>
<license>Apache License 2.0</license>

<buildtool_depend>ament_cmake_auto</buildtool_depend>

<depend>geometry_msgs</depend>
<depend>nav_msgs</depend>
<depend>rclcpp</depend>
<depend>rclcpp_components</depend>
<depend>sensor_msgs</depend>
<depend>laser_geometry</depend>
<depend>tf2</depend>
<depend>tf2_eigen</depend>
<depend>tf2_geometry_msgs</depend>
<depend>tf2_ros</depend>
<depend>pcl_ros</depend>
<depend>visualization_msgs</depend>

<exec_depend>launch_ros</exec_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Loading

0 comments on commit 4ea576b

Please sign in to comment.