From 2c405514bbf42d2cbeba95d126bdbb8e67ddb01a Mon Sep 17 00:00:00 2001 From: RonaldsonBellande Date: Mon, 29 Jul 2024 07:40:33 -0400 Subject: [PATCH] latest pushes --- .../CMakeLists.txt | 60 +++++++++++ .../README.md | 3 + .../config/configs.json | 22 +++++ .../launch/bellande_control_system.launch.py | 62 ++++++++++++ .../ros1/bellande_control_system.launch | 30 ++++++ .../package.xml | 54 ++++++++++ .../setup.py | 26 +++++ .../src/bellande_control_system.py | 99 +++++++++++++++++++ 8 files changed, 356 insertions(+) create mode 100644 ros_web_api_bellande_adaptive_continuious_control_system/CMakeLists.txt create mode 100644 ros_web_api_bellande_adaptive_continuious_control_system/README.md create mode 100644 ros_web_api_bellande_adaptive_continuious_control_system/config/configs.json create mode 100644 ros_web_api_bellande_adaptive_continuious_control_system/launch/bellande_control_system.launch.py create mode 100644 ros_web_api_bellande_adaptive_continuious_control_system/launch/ros1/bellande_control_system.launch create mode 100644 ros_web_api_bellande_adaptive_continuious_control_system/package.xml create mode 100755 ros_web_api_bellande_adaptive_continuious_control_system/setup.py create mode 100755 ros_web_api_bellande_adaptive_continuious_control_system/src/bellande_control_system.py diff --git a/ros_web_api_bellande_adaptive_continuious_control_system/CMakeLists.txt b/ros_web_api_bellande_adaptive_continuious_control_system/CMakeLists.txt new file mode 100644 index 0000000..725e12e --- /dev/null +++ b/ros_web_api_bellande_adaptive_continuious_control_system/CMakeLists.txt @@ -0,0 +1,60 @@ +# Copyright (C) 2024 Bellande Robotics Sensors Research Innovation Center, Ronaldson Bellande +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +cmake_minimum_required(VERSION 3.8) +project(ros_web_api_bellande_adaptive_continuious_control_system) + +# Find ROS +if($ENV{ROS_VERSION} EQUAL 1) + find_package(catkin REQUIRED COMPONENTS + roscpp + rospy + ) +else() + find_package(ament_cmake REQUIRED COMPONENTS + rclcpp + rclpy + ) +endif() + +if($ENV{ROS_VERSION} EQUAL 1) + catkin_package( + INCLUDE_DIRS + LIBRARIES ${PROJECT_NAME} + CATKIN_DEPENDS + roscpp + rospy + ) +endif() + +# Install Python scripts for both ROS 1 +if($ENV{ROS_VERSION} EQUAL 1) + catkin_install_python( + PROGRAMS + src/bellande_control_system.py + DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} + ) +endif() + +# Install Python scripts, configuration files, and launch files +if($ENV{ROS_VERSION} EQUAL "1") + install(PROGRAMS src/bellande_control_system.py DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}) + install(DIRECTORY config/ DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/config) + install(DIRECTORY launch/ DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch) +elseif($ENV{ROS_VERSION} EQUAL "2") + install(PROGRAMS src/bellande_control_system.py DESTINATION lib/${PROJECT_NAME}) + install(DIRECTORY config/ DESTINATION share/${PROJECT_NAME}/config) + install(DIRECTORY launch/ DESTINATION share/${PROJECT_NAME}/launch) +endif() diff --git a/ros_web_api_bellande_adaptive_continuious_control_system/README.md b/ros_web_api_bellande_adaptive_continuious_control_system/README.md new file mode 100644 index 0000000..b78d1e8 --- /dev/null +++ b/ros_web_api_bellande_adaptive_continuious_control_system/README.md @@ -0,0 +1,3 @@ +# Bellande Search Path Web ROS/ROS2 API Intergration + +[Bellande Search Path](https://github.com/RonaldsonBellande/bellande_robots_step) diff --git a/ros_web_api_bellande_adaptive_continuious_control_system/config/configs.json b/ros_web_api_bellande_adaptive_continuious_control_system/config/configs.json new file mode 100644 index 0000000..0ceecdd --- /dev/null +++ b/ros_web_api_bellande_adaptive_continuious_control_system/config/configs.json @@ -0,0 +1,22 @@ +{ + "license": [ + "Copyright (C) 2024 Bellande Robotics Sensors Research Innovation Center, Ronaldson Bellande", + "This program is free software: you can redistribute it and/or modify", + "it under the terms of the GNU General Public License as published by", + "the Free Software Foundation, either version 3 of the License, or", + "(at your option) any later version.", + "", + "This program is distributed in the hope that it will be useful,", + "but WITHOUT ANY WARRANTY; without even the implied warranty of", + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the", + "GNU General Public License for more details.", + "", + "You should have received a copy of the GNU General Public License", + "along with this program. If not, see .", + "GNU General Public License v3.0 or later" + ], + "url": "https://bellanderoboticssensorsresearchinnovationcenter-kot42qxp.b4a.run", + "endpoint_path": { + "bellande_control_system_base": "/api/Bellande_Adaptive_Continuious_Control_System/bellande_adaptive_continuious_control_system" + } +} diff --git a/ros_web_api_bellande_adaptive_continuious_control_system/launch/bellande_control_system.launch.py b/ros_web_api_bellande_adaptive_continuious_control_system/launch/bellande_control_system.launch.py new file mode 100644 index 0000000..b2614ea --- /dev/null +++ b/ros_web_api_bellande_adaptive_continuious_control_system/launch/bellande_control_system.launch.py @@ -0,0 +1,62 @@ +# Copyright (C) 2024 Bellande Robotics Sensors Research Innovation Center, Ronaldson Bellande +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import os +import sys +import subprocess +from launch import LaunchDescription +from launch_ros.actions import Node +from launch.actions import DeclareLaunchArgument +from launch.substitutions import LaunchConfiguration + +def ros1_launch_description(): + args = sys.argv[1:] + roslaunch_command = ["roslaunch", "bellande_control_system", "bellande_control_system.launch"] + args + subprocess.call(roslaunch_command) + +def ros2_launch_description(): + initial_state_arg = DeclareLaunchArgument('initial_state', default_value='idle') + name_arg = DeclareLaunchArgument('name', default_value='BellandeControlSystem') + action_parameters_arg = DeclareLaunchArgument('action_parameters', default_value='{}') + + nodes_to_launch = [] + + ros_launch_arguments = [ + initial_state_arg, name_arg, action_parameters_arg, + ] + + nodes_to_launch.append(Node( + package='bellande_control_system', + executable='bellande_control_system.py', + name='bellande_control_system_node', + output='screen', + parameters=[ + {'initial_state': LaunchConfiguration('initial_state')}, + {'name': LaunchConfiguration('name')}, + {'action_parameters': LaunchConfiguration('action_parameters')}, + ], + )) + + 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_adaptive_continuious_control_system/launch/ros1/bellande_control_system.launch b/ros_web_api_bellande_adaptive_continuious_control_system/launch/ros1/bellande_control_system.launch new file mode 100644 index 0000000..699eaf0 --- /dev/null +++ b/ros_web_api_bellande_adaptive_continuious_control_system/launch/ros1/bellande_control_system.launch @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + diff --git a/ros_web_api_bellande_adaptive_continuious_control_system/package.xml b/ros_web_api_bellande_adaptive_continuious_control_system/package.xml new file mode 100644 index 0000000..f874393 --- /dev/null +++ b/ros_web_api_bellande_adaptive_continuious_control_system/package.xml @@ -0,0 +1,54 @@ + + + + + ros_web_api_bellande_adaptive_continuious_control_system + 0.0.1 + Bellande ROS/ROS2 package with a JSON config file for making HTTP requests + Ronaldson Bellande + Ronaldson Bellande + Apache License 2.0 + + + catkin + + roscpp + rospy + + roscpp + rospy + + roscpp + rospy + + + ament_cmake + + rclcpp + rclpy + + rclcpp + rclpy + + rclcpp + rclpy + + + + + diff --git a/ros_web_api_bellande_adaptive_continuious_control_system/setup.py b/ros_web_api_bellande_adaptive_continuious_control_system/setup.py new file mode 100755 index 0000000..5b57e1d --- /dev/null +++ b/ros_web_api_bellande_adaptive_continuious_control_system/setup.py @@ -0,0 +1,26 @@ +# Copyright (C) 2024 Bellande Robotics Sensors Research Innovation Center, Ronaldson Bellande +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +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_control_system.py'], + packages=['ros_web_api_bellande_adaptive_continuious_control_system'], + package_dir={'': 'src'}, +) + +setup(**setup_args) diff --git a/ros_web_api_bellande_adaptive_continuious_control_system/src/bellande_control_system.py b/ros_web_api_bellande_adaptive_continuious_control_system/src/bellande_control_system.py new file mode 100755 index 0000000..f812d44 --- /dev/null +++ b/ros_web_api_bellande_adaptive_continuious_control_system/src/bellande_control_system.py @@ -0,0 +1,99 @@ +# Copyright (C) 2024 Bellande Robotics Sensors Research Innovation Center, Ronaldson Bellande +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import json +import os +import requests +from std_msgs.msg import String + +def bellande_control_system(state, action, parameters): + payload = { + "state": state, + "action": action, + "parameters": parameters + } + headers = { + "Authorization": f"Bearer {api_access_key}" + } + response = requests.post(api_url, json=payload, headers=headers) + if response.status_code == 200: + result = response.json() + return result['output'], result.get('next_state') + else: + print(f"Error: {response.status_code} - {response.text}") + return None, None + +def control_callback(msg): + current_state = rospy.get_param('current_state', 'idle') + action = msg.data + parameters = json.loads(rospy.get_param('action_parameters', '{}')) + + output, next_state = bellande_control_system(current_state, action, parameters) + if output is not None: + output_msg = String() + output_msg.data = json.dumps({"output": output, "next_state": next_state}) + pub.publish(output_msg) + if next_state: + rospy.set_param('current_state', next_state) + +def main(): + global api_url, api_access_key, pub + config_file_path = os.path.join(os.path.dirname(__file__), '../config/configs.json') + + if not os.path.exists(config_file_path): + print("Config file not found:", config_file_path) + return + + with open(config_file_path, 'r') as config_file: + config = json.load(config_file) + url = config['url'] + endpoint_path = config['endpoint_path']["bellande_control_system_base"] + api_access_key = config["Bellande_Framework_Access_Key"] + + # Initialize ROS node + if ros_version == "1": + rospy.init_node('bellande_control_system_node', anonymous=True) + pub = rospy.Publisher('bellande_control_system_node_control_output', String, queue_size=10) + sub = rospy.Subscriber('bellande_control_system_node_control_action', String, control_callback) + elif ros_version == "2": + rclpy.init() + node = rclpy.create_node('bellande_control_system_node') + pub = node.create_publisher(String, 'bellande_control_system_node_control_output', 10) + sub = node.create_subscription(String, 'bellande_control_system_node_control_action', control_callback, 10) + + api_url = f"{url}{endpoint_path}" + + try: + print("BellandeControlSystem node is running. Ctrl+C to exit.") + if ros_version == "1": + rospy.spin() + elif ros_version == "2": + rclpy.spin(node) + except KeyboardInterrupt: + print("Shutting down BellandeControlSystem node.") + except Exception as e: + print(f"An error occurred: {str(e)}") + finally: + if ros_version == "2": + node.destroy_node() + rclpy.shutdown() + +if __name__ == '__main__': + ros_version = os.getenv("ROS_VERSION") + if ros_version == "1": + import rospy + elif ros_version == "2": + import rclpy + main()