diff --git a/ros_web_api_bellande_ai_system/CMakeLists.txt b/ros_web_api_bellande_ai_system/CMakeLists.txt index e79e1ca..3447650 100644 --- a/ros_web_api_bellande_ai_system/CMakeLists.txt +++ b/ros_web_api_bellande_ai_system/CMakeLists.txt @@ -44,6 +44,7 @@ if($ENV{ROS_VERSION} EQUAL 1) catkin_install_python( PROGRAMS src/bellande_ai_system_base.py + src/bellande_ai_system_command.py DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} ) endif() @@ -51,10 +52,12 @@ endif() # Install Python scripts, configuration files, and launch files if($ENV{ROS_VERSION} EQUAL "1") install(PROGRAMS src/bellande_ai_system_base.py DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}) + install(PROGRAMS src/bellande_ai_system_command.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_ai_system_base.py DESTINATION lib/${PROJECT_NAME}) + install(PROGRAMS src/bellande_ai_system_command.py DESTINATION lib/${PROJECT_NAME}) install(DIRECTORY config/ DESTINATION share/${PROJECT_NAME}/config) install(DIRECTORY launch/ DESTINATION share/${PROJECT_NAME}/launch) ament_package() diff --git a/ros_web_api_bellande_ai_system/config/configs.json b/ros_web_api_bellande_ai_system/config/configs.json index a9211f7..752cf84 100644 --- a/ros_web_api_bellande_ai_system/config/configs.json +++ b/ros_web_api_bellande_ai_system/config/configs.json @@ -17,7 +17,8 @@ ], "url": "https://bellanderoboticssensorsresearchinnovationcenter-kot42qxp.b4a.run", "endpoint_path": { - "base": "/api/Bellande_AI_System/base" + "base": "/api/Bellande_AI_System/base", + "command": "api/Bellande_AI_System/command" }, "Bellande_Framework_Access_Key": "bellande_web_api_opensource" } diff --git a/ros_web_api_bellande_ai_system/launch/bellande_ai_system_command.launch.py b/ros_web_api_bellande_ai_system/launch/bellande_ai_system_command.launch.py new file mode 100644 index 0000000..9296bb6 --- /dev/null +++ b/ros_web_api_bellande_ai_system/launch/bellande_ai_system_command.launch.py @@ -0,0 +1,67 @@ +# 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(): + # Get command-line arguments + args = sys.argv[1:] + # Construct the ROS 1 launch command + roslaunch_command = ["roslaunch", "ros_web_api_bellande_ai_system", "bellande_ai_system_command.launch"] + args + # Execute the launch command + subprocess.call(roslaunch_command) + +def ros2_launch_description(): + # Declare launch arguments + config_file_arg = DeclareLaunchArgument( + 'config_file', + default_value='$(find ros_web_api_bellande_ai_system)/config/configs.json', + description='Path to the AI system configuration file' + ) + + # Create a list to hold all nodes to be launched + nodes_to_launch = [] + + # ROS2 specific configurations + ros_launch_arguments = [config_file_arg] + + nodes_to_launch.append(Node( + package='ros_web_api_bellande_ai_system', + executable='bellande_ai_system_command.py', + name='bellande_ai_system_command_node', + output='screen', + parameters=[ + {'config_file': LaunchConfiguration('config_file')}, + ], + )) + + # 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_ai_system/launch/ros1/bellande_ai_system_command.launch b/ros_web_api_bellande_ai_system/launch/ros1/bellande_ai_system_command.launch new file mode 100644 index 0000000..54cd1e2 --- /dev/null +++ b/ros_web_api_bellande_ai_system/launch/ros1/bellande_ai_system_command.launch @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + diff --git a/ros_web_api_bellande_ai_system/setup.py b/ros_web_api_bellande_ai_system/setup.py index 3a81b2a..f910ebb 100755 --- a/ros_web_api_bellande_ai_system/setup.py +++ b/ros_web_api_bellande_ai_system/setup.py @@ -1,9 +1,24 @@ +# 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_ai_system_base.py'], + scripts=["src/bellande_ai_system_base.py", "src/bellande_ai_system_command.py"], packages=['ros_web_api_bellande_ai_system'], package_dir={'': 'src'}, ) diff --git a/ros_web_api_bellande_ai_system/src/bellande_ai_system_base.py b/ros_web_api_bellande_ai_system/src/bellande_ai_system_base.py index b604f42..4f0fd40 100755 --- a/ros_web_api_bellande_ai_system/src/bellande_ai_system_base.py +++ b/ros_web_api_bellande_ai_system/src/bellande_ai_system_base.py @@ -62,13 +62,13 @@ def main(): # Initialize ROS node if ros_version == "1": rospy.init_node('ai_system_node', anonymous=True) - pub = rospy.Publisher('ai_response', String, queue_size=10) - sub = rospy.Subscriber('ai_input', String, input_callback) + pub = rospy.Publisher('ai_system_node_ai_response', String, queue_size=10) + sub = rospy.Subscriber('ai_system_node_ai_input', String, input_callback) elif ros_version == "2": rclpy.init() node = rclpy.create_node('ai_system_node') - pub = node.create_publisher(String, 'ai_response', 10) - sub = node.create_subscription(String, 'ai_input', input_callback, 10) + pub = node.create_publisher(String, 'ai_system_node_ai_response', 10) + sub = node.create_subscription(String, 'ai_system_node_ai_input', input_callback, 10) api_url = f"{url}{endpoint_path}" diff --git a/ros_web_api_bellande_ai_system/src/bellande_ai_system_command.py b/ros_web_api_bellande_ai_system/src/bellande_ai_system_command.py new file mode 100755 index 0000000..cf4a1bb --- /dev/null +++ b/ros_web_api_bellande_ai_system/src/bellande_ai_system_command.py @@ -0,0 +1,96 @@ +# 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 get_ai_response(input_command): + payload = { + "input_command": input_command + } + headers = { + 'accept': 'application/json', + 'Content-Type': 'application/json', + "Authorization": f"Bearer {api_access_key}" + } + try: + response = requests.post( + api_url, + json=payload, + headers=headers + ) + response.raise_for_status() + data = response.json() + return String(f"AI Response: {data['response']}") + except requests.exceptions.RequestException as e: + print(f"Error: {e}") + return None + +def input_callback(msg): + ai_response = get_ai_response(msg.data) + if ai_response: + pub.publish(ai_response) + +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']["command"] + api_access_key = config["Bellande_Framework_Access_Key"] + + # Initialize ROS node + if ros_version == "1": + rospy.init_node('ai_system_node', anonymous=True) + pub = rospy.Publisher('ai_system_node_ai_command_response', String, queue_size=10) + sub = rospy.Subscriber('ai_system_node_ai_coomand_input', String, input_callback) + elif ros_version == "2": + rclpy.init() + node = rclpy.create_node('ai_system_node') + pub = node.create_publisher(String, 'ai_system_node_ai_command_response', 10) + sub = node.create_subscription(String, 'ai_system_node_ai_command_input', input_callback, 10) + + api_url = f"{url}{endpoint_path}" + + try: + print("AI system 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 AI system 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()