From a3a665b71c34e136102b8267ee37802da4addf90 Mon Sep 17 00:00:00 2001 From: RonaldsonBellande Date: Fri, 3 May 2024 01:09:16 -0400 Subject: [PATCH] latest pushes --- ros_web_api_bellande_step/CMakeLists.txt | 7 +++ ros_web_api_bellande_step/README.md | 10 ++++ .../launch/bellande_step_api_2d.launch.py | 57 +++++++++++++++++++ ros_web_api_bellande_step/setup.py | 11 ++++ .../src/bellande_step_api_2d.py | 35 +++++++++--- 5 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 ros_web_api_bellande_step/launch/bellande_step_api_2d.launch.py create mode 100755 ros_web_api_bellande_step/setup.py mode change 100644 => 100755 ros_web_api_bellande_step/src/bellande_step_api_2d.py diff --git a/ros_web_api_bellande_step/CMakeLists.txt b/ros_web_api_bellande_step/CMakeLists.txt index 90de820..ae4d43f 100644 --- a/ros_web_api_bellande_step/CMakeLists.txt +++ b/ros_web_api_bellande_step/CMakeLists.txt @@ -49,3 +49,10 @@ elseif($ENV{ROS_VERSION} EQUAL "2") install(DIRECTORY config/ DESTINATION share/${PROJECT_NAME}/config) install(DIRECTORY launch/ DESTINATION share/${PROJECT_NAME}/launch) endif() + +# Install Python scripts for both ROS 1 and ROS 2 +catkin_install_python( + PROGRAMS + src/bellande_step_api_2d.py + DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} +) diff --git a/ros_web_api_bellande_step/README.md b/ros_web_api_bellande_step/README.md index e69de29..c6e3257 100644 --- a/ros_web_api_bellande_step/README.md +++ b/ros_web_api_bellande_step/README.md @@ -0,0 +1,10 @@ +# Bellande Web ROS/ROS2 API + +# [Bellande Step](https://github.com/RonaldsonBellande/bellande_robots_step) + +# ROS1 Launch +- python3 bellande_step_api_2d.launch.py x1:=0 y1:=0 x2:=5 y2:=5 limit:=3 +- roslaunch ros_web_api_bellande_step bellande_step_api_2d.launch x1:=0 y1:=0 x2:=5 y2:=5 limit:=3 + +# ROS2 Launch +- ros2 launch ros_web_api_bellande_step bellande_step_api_2d.launch.py x1:=0 y1:=0 x2:=5 y2:=5 limit:=3 diff --git a/ros_web_api_bellande_step/launch/bellande_step_api_2d.launch.py b/ros_web_api_bellande_step/launch/bellande_step_api_2d.launch.py new file mode 100644 index 0000000..d6e8a89 --- /dev/null +++ b/ros_web_api_bellande_step/launch/bellande_step_api_2d.launch.py @@ -0,0 +1,57 @@ +import os +import sys +import subprocess + + +def ros1_launch_description(): + # Get command-line arguments + args = sys.argv[1:] + + # Construct the ROS 1 launch command + roslaunch_command = ["roslaunch", "ros_web_api_bellande_step", "bellande_step_api_2d.launch"] + args + + # Execute the launch command + subprocess.call(roslaunch_command) + + +def ros2_launch_description(): + # Declare launch arguments + x1_arg = DeclareLaunchArgument('x1') + y1_arg = DeclareLaunchArgument('y1') + x2_arg = DeclareLaunchArgument('x2') + y2_arg = DeclareLaunchArgument('y2') + limit_arg = DeclareLaunchArgument('limit') + + # Create a list to hold all nodes to be launched + nodes_to_launch = [] + + # ROS2 specific configurations + ros_launch_arguments = [ + x1_arg, y1_arg, x2_arg, y2_arg, limit_arg, + ] + nodes_to_launch.append(Node( + package='ros_web_api_bellande_step', + executable='bellande_step_api_2d.py', + name='bellande_step_api_2d_node', + output='screen', + parameters=[ + {'x1': LaunchConfiguration('x1')}, + {'y1': LaunchConfiguration('y1')}, + {'x2': LaunchConfiguration('x2')}, + {'y2': LaunchConfiguration('y2')}, + {'limit': LaunchConfiguration('limit')}, + ], + )) + + # Return the LaunchDescription containing all nodes and arguments + return LaunchDescription(ros_launch_arguments + nodes_to_launch) + +if __name__ == "__main__": + ros_version = os.getenv("ROS_VERSION") + if ros_version == "1": + ros1_launch_description() + elif ros_version == "2": + ros2_launch_description() + else: + print("Unsupported ROS version. Please set the ROS_VERSION environment variable to '1' for ROS 1 or '2' for ROS 2.") + sys.exit(1) diff --git a/ros_web_api_bellande_step/setup.py b/ros_web_api_bellande_step/setup.py new file mode 100755 index 0000000..5ec6985 --- /dev/null +++ b/ros_web_api_bellande_step/setup.py @@ -0,0 +1,11 @@ +from distutils.core import setup +from catkin_pkg.python_setup import generate_distutils_setup + +# fetch values from package.xml +setup_args = generate_distutils_setup( + scripts=['src/bellande_step_api_2d.py'], + packages=['ros_web_api_bellande_step'], + package_dir={'': 'src'}, +) + +setup(**setup_args) diff --git a/ros_web_api_bellande_step/src/bellande_step_api_2d.py b/ros_web_api_bellande_step/src/bellande_step_api_2d.py old mode 100644 new mode 100755 index a133866..91ee859 --- a/ros_web_api_bellande_step/src/bellande_step_api_2d.py +++ b/ros_web_api_bellande_step/src/bellande_step_api_2d.py @@ -4,19 +4,39 @@ import requests import sys def main(): + # Get the absolute path to the config file + config_file_path = os.path.join(os.path.dirname(__file__), '../config/config2d.json') + + # Check if the config file exists + if not os.path.exists(config_file_path): + print("Config file not found:", config_file_path) + return + # Read configuration from config.json - with open(os.path.join(sys.path[0], 'config.json'), 'r') as config_file: + with open(config_file_path, 'r') as config_file: config = json.load(config_file) url = config['url'] endpoint_path = config['endpoint_path'] - + # Get the parameters from the launch file - x1 = float(os.getenv('x1')) - y1 = float(os.getenv('y1')) - x2 = float(os.getenv('x2')) - y2 = float(os.getenv('y2')) - limit = int(os.getenv('limit')) + x1_str = os.getenv('x1') + y1_str = os.getenv('y1') + x2_str = os.getenv('x2') + y2_str = os.getenv('y2') + limit_str = os.getenv('limit') + # Check if any of the environment variables are not set + if any(v is None for v in [x1_str, y1_str, x2_str, y2_str, limit_str]): + print("One or more required environment variables are not set.") + return + + # Convert the parameters to float or int + x1 = float(x1_str) + y1 = float(y1_str) + x2 = float(x2_str) + y2 = float(y2_str) + limit = int(limit_str) + # JSON payload payload = { "node0": {"x": x1, "y": y1}, @@ -29,6 +49,7 @@ def main(): 'Content-Type': 'application/json' } + # Make POST request try: response = requests.post(