latest pushes

This commit is contained in:
Ronaldson Bellande 2024-05-03 01:09:16 -04:00
parent 5abfc97ef2
commit a3a665b71c
5 changed files with 113 additions and 7 deletions

View File

@ -49,3 +49,10 @@ elseif($ENV{ROS_VERSION} EQUAL "2")
install(DIRECTORY config/ DESTINATION share/${PROJECT_NAME}/config) install(DIRECTORY config/ DESTINATION share/${PROJECT_NAME}/config)
install(DIRECTORY launch/ DESTINATION share/${PROJECT_NAME}/launch) install(DIRECTORY launch/ DESTINATION share/${PROJECT_NAME}/launch)
endif() 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}
)

View File

@ -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

View File

@ -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)

View File

@ -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)

35
ros_web_api_bellande_step/src/bellande_step_api_2d.py Normal file → Executable file
View File

@ -4,19 +4,39 @@ import requests
import sys import sys
def main(): 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 # 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) config = json.load(config_file)
url = config['url'] url = config['url']
endpoint_path = config['endpoint_path'] endpoint_path = config['endpoint_path']
# Get the parameters from the launch file # Get the parameters from the launch file
x1 = float(os.getenv('x1')) x1_str = os.getenv('x1')
y1 = float(os.getenv('y1')) y1_str = os.getenv('y1')
x2 = float(os.getenv('x2')) x2_str = os.getenv('x2')
y2 = float(os.getenv('y2')) y2_str = os.getenv('y2')
limit = int(os.getenv('limit')) 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 # JSON payload
payload = { payload = {
"node0": {"x": x1, "y": y1}, "node0": {"x": x1, "y": y1},
@ -29,6 +49,7 @@ def main():
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
# Make POST request # Make POST request
try: try:
response = requests.post( response = requests.post(