### Bash Gazebo Simulation Setup and Control Source: https://context7.com/fan-ziqi/rl_sar/llms.txt Provides bash commands to set up and run the Gazebo simulation for the rl_sar project. It includes steps for installing Gazebo models, launching the simulation with a specified robot, and running the RL control node. ```bash # Install Gazebo models (first time only) git clone https://github.com/osrf/gazebo_models.git ~/.gazebo/models # Terminal 1: Launch Gazebo with robot # ROS1 source devel/setup.bash roslaunch rl_sar gazebo.launch rname:=go2 # ROS2 source install/setup.bash ros2 launch rl_sar gazebo.launch.py rname:=go2 # Terminal 2: Run RL control # ROS1 source devel/setup.bash rosrun rl_sar rl_sim # ROS2 source install/setup.bash ros2 run rl_sar rl_sim # Control with keyboard: # - Num0: Stand up to default pose # - Num1: Start RL locomotion # - W/S: Forward/backward # - A/D: Left/right # - Q/E: Rotate yaw # - R: Reset robot position # - Space: Clear velocity commands # Expected output: # [INFO] Loaded policy: policy/go2/himloco/himloco.pt # [INFO] Model type: torch # [INFO] FSM: Transitioning to state 'stand_up' # [INFO] Standing up... 100% # [INFO] FSM: Transitioning to state 'rl_control' # [INFO] RL control active (200 Hz) ``` -------------------------------- ### Bash Onboard Jetson Deployment and Service Setup Source: https://context7.com/fan-ziqi/rl_sar/llms.txt Guides for deploying the rl_sar project on a Jetson onboard computer, including SSH access, cloning the repository, building with CMake, running the real robot executable, and configuring a systemd service for auto-start. ```bash # SSH into robot's onboard computer ssh unitree@192.168.123.18 # Password: 123 # Clone and build (CMake mode for hardware) git clone --recursive --depth 1 https://github.com/fan-ziqi/rl_sar.git cd rl_sar ./build.sh -m # Run on onboard Jetson ./cmake_build/bin/rl_real_go2 eth0 # Setup auto-start service sudo nano /etc/systemd/system/rl_sar.service # Add service configuration: # [Unit] # Description=RL SAR Service # After=network.target # # [Service] # Type=simple # User=unitree # WorkingDirectory=/home/unitree/rl_sar # ExecStart=/home/unitree/rl_sar/cmake_build/bin/rl_real_go2 eth0 # Restart=on-failure # RestartSec=5 # # [Install] # WantedBy=multi-user.target # Enable and start service sudo systemctl daemon-reload sudo systemctl enable rl_sar.service sudo systemctl start rl_sar.service # Check service status and logs sudo systemctl status rl_sar.service sudo journalctl -u rl_sar.service -f ``` -------------------------------- ### Install ROS Bridge and Web Video Server Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md Installs necessary ROS packages for mobile web control. For ROS2 versions not supporting these packages directly, it provides instructions to build web_video_server from source. ```bash sudo apt install ros-${ROS_DISTRO}-rosbridge-suite sudo apt install ros-${ROS_DISTRO}-web-video-server # If you are using a ROS2 version other than Humble, Jazz or Rolling, you need to compile `web_video_server` from source cd /src git clone https://github.com/RobotWebTools/web_video_server.git cd colcon build --packages-select web_video_server ``` -------------------------------- ### Install Web Control Dependencies Source: https://github.com/fan-ziqi/rl_sar/blob/main/README.md Installs necessary ROS packages for web-based robot control, including rosbridge-suite and web-video-server. For specific ROS2 versions, web_video_server may need to be built from source. ```bash sudo apt install ros-${ROS_DISTRO}-rosbridge-suite sudo apt install ros-${ROS_DISTRO}-web-video-server # If building web_video_server from source for ROS2 (non-Humble, Jazz, Rolling) cd /src git clone https://github.com/RobotWebTools/web_video_server.git cd colcon build --packages-select web_video_server ``` -------------------------------- ### Install Gazebo Models Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md Clones the gazebo_models repository to the ~/.gazebo/models directory. This is necessary if the Gazebo simulation fails to open on the first launch. ```bash git clone https://github.com/osrf/gazebo_models.git ~/.gazebo/models ``` -------------------------------- ### Run Rosbridge and Web Video Server on Robot Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md Starts the rosbridge and web_video_server services on the robot. These are required for controlling the robot via the mobile web interface. ```bash # ROS1 roslaunch rosbridge_server rosbridge_websocket.launch rosrun web_video_server web_video_server # ROS2 ros2 launch rosbridge_server rosbridge_websocket_launch.xml ros2 run web_video_server web_video_server ``` -------------------------------- ### Install Python Script: actuator_net.py (ROS) Source: https://github.com/fan-ziqi/rl_sar/blob/main/src/rl_sar/CMakeLists.txt Installs the 'actuator_net.py' Python script. The installation destination differs based on the ROS distribution. For 'noetic', it uses 'catkin_install_python', while for 'foxy' and 'humble', it uses the standard 'install' command with a different destination. ```cmake if(NOT USE_CMAKE) if($ENV{ROS_DISTRO} MATCHES "noetic") catkin_install_python(PROGRAMS scripts/actuator_net.py DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} ) elseif($ENV{ROS_DISTRO} MATCHES "foxy|humble") install(PROGRAMS scripts/actuator_net.py DESTINATION lib/${PROJECT_NAME} ) endif() endif() ``` -------------------------------- ### Install Directories (ROS) Source: https://github.com/fan-ziqi/rl_sar/blob/main/src/rl_sar/CMakeLists.txt Installs directories 'launch', 'worlds', and the 'policy' directory from the project root. The installation destination is relative to the package name and depends on the ROS distribution. This configuration is specifically for 'foxy' and 'humble' ROS distributions. ```cmake if(NOT USE_CMAKE) if($ENV{ROS_DISTRO} MATCHES "foxy|humble") install( DIRECTORY launch worlds ${PROJECT_ROOT_DIR}/policy DESTINATION share/${PROJECT_NAME}/${dir} ) endif() endif() ``` -------------------------------- ### Configure Systemd Service for RL-SAR Auto-Start Source: https://github.com/fan-ziqi/rl_sar/blob/main/README.md This snippet details the creation and management of a systemd service file to enable the RL-SAR application to start automatically on boot. It covers creating the service file, reloading systemd, enabling, disabling, starting, stopping, and viewing logs for the service. Ensure the paths and user match your system configuration. ```bash sudo touch /etc/systemd/system/rl_sar.service [Unit] Description=RL SAR Service After=network.target [Service] Type=simple User=unitree WorkingDirectory=/home/unitree/rl_sar ExecStart=/home/unitree/rl_sar/cmake_build/bin/rl_real_go2 eth0 wheel Restart=on-failure RestartSec=5 StandardOutput=journal StandardError=journal [Install] WantedBy=multi-user.target sudo systemctl daemon-reload sudo systemctl enable rl_sar.service sudo systemctl disable rl_sar.service sudo systemctl start rl_sar.service sudo systemctl stop rl_sar.service sudo systemctl restart rl_sar.service sudo journalctl -u rl_sar.service -f ``` -------------------------------- ### Start rl_sar Service Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md This command manually starts the 'rl_sar.service'. Note: The provided text also shows 'sudo systemctl enable rl_sar.service' for starting, which is typically used for enabling auto-start. ```bash sudo systemctl start rl_sar.service ``` -------------------------------- ### Bash MuJoCo Simulation Build and Run Source: https://context7.com/fan-ziqi/rl_sar/llms.txt Instructions for building the rl_sar project with MuJoCo support and running the MuJoCo simulator. It specifies the build command and example commands for launching the simulator with different robot and scene configurations. ```bash # Build with MuJoCo support ./build.sh -mj # Run MuJoCo simulator (replace ROBOT and SCENE) ./cmake_build/bin/rl_sim_mujoco go2 scene_legged ./cmake_build/bin/rl_sim_mujoco g1 scene_29dof ./cmake_build/bin/rl_sim_mujoco b2 scene_wheeled # Interactive MuJoCo viewer controls: # - Mouse: Rotate camera # - Scroll: Zoom # - Keyboard: Same as Gazebo controls ``` -------------------------------- ### Enable rl_sar Service for Auto-start Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md This command configures the 'rl_sar.service' to start automatically upon system boot. ```bash sudo systemctl enable rl_sar.service ``` -------------------------------- ### Run rl_sim Control Program Source: https://github.com/fan-ziqi/rl_sar/blob/main/README.md Launches the rl_sim control program for simulation. Requires sourcing the appropriate ROS setup file (ROS1 or ROS2). This should be run in a separate terminal from the Gazebo launch. ```bash # ROS1 source devel/setup.bash rosrun rl_sar rl_sim # ROS2 source install/setup.bash ros2 run rl_sar rl_sim ``` -------------------------------- ### Build and Run Robot Control on Onboard Jetson (Go2/G1) Source: https://github.com/fan-ziqi/rl_sar/blob/main/README.md This process involves SSHing into the robot's onboard Jetson, compiling the control program using a build script, and then running the executable. It's designed for deploying control directly on the robot after initial setup and network configuration. ```bash # SSH into the robot ssh unitree@192.168.123.18 # Compile the code ./build.sh -m # Run Go2 control ./cmake_build/bin/rl_real_go2 [wheel] # Run G1 control ./cmake_build/bin/rl_real_g1 ``` -------------------------------- ### Bash Unitree G1 Humanoid Deployment (ROS2/CMake) Source: https://context7.com/fan-ziqi/rl_sar/llms.txt Instructions for deploying the rl_sar project on a Unitree G1 humanoid robot. It covers the initial setup, launching the control node using ROS2 or CMake, and expected output messages. ```bash # Connect via Ethernet (same setup as Go2) # Turn on robot, lift off ground, press L2+R2 to enter debug mode # ROS2 source install/setup.bash ros2 run rl_sar rl_real_g1 enxf8e43b808e06 # CMake ./cmake_build/bin/rl_real_g1 enxf8e43b808e06 # Expected output: # [INFO] Connecting to G1 (29 DOF) on interface: enxf8e43b808e06 # [INFO] Loaded policy: policy/g1/robomimic/locomotion/model.pt # [INFO] Motion file: policy/g1/whole_body_tracking/dance_102/motion.txt ``` -------------------------------- ### ROS2 Deployment for rl_real_g1 Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md This snippet shows how to run the rl_real_g1 node using ROS2. Ensure you have sourced the ROS2 setup file and replace '' with your actual network interface name. ```bash source install/setup.bash ros2 run rl_sar rl_real_g1 ``` -------------------------------- ### Run Gazebo Simulation with ROS1/ROS2 Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md Launches the Gazebo simulation environment and the control program for the robot. Requires sourcing the appropriate ROS setup file. The rl_sim command automatically resets the robot if it's out of view or has fallen. ```bash # ROS1 source devel/setup.bash roslaunch rl_sar gazebo.launch rname:= # ROS2 source install/setup.bash ros2 launch rl_sar gazebo.launch.py rname:= ``` ```bash # ROS1 source devel/setup.bash rosrun rl_sar rl_sim # ROS2 source install/setup.bash ros2 run rl_sar rl_sim ``` -------------------------------- ### Run Real Robot Control Program for Unitree G1 (ROS1, ROS2, CMake) Source: https://github.com/fan-ziqi/rl_sar/blob/main/README.md Starts the control program for Unitree G1(29dofs) robots. This command requires specifying your network interface. Ensure the robot is in debugging mode (L2+R2) and the correct ROS environment is sourced. ```bash # ROS1 source devel/setup.bash rosrun rl_sar rl_real_g1 # ROS2 source install/setup.bash ros2 run rl_sar rl_real_g1 # CMake ./cmake_build/bin/rl_real_g1 ``` -------------------------------- ### ROS Foxy/Humble Build Configuration (CMake) Source: https://github.com/fan-ziqi/rl_sar/blob/main/src/robot_joint_controller/CMakeLists.txt Configures the build for ROS Foxy or Humble using ament_cmake. It defines ROS distribution macros, finds required ament packages, sets include directories, adds libraries, specifies dependencies, and installs the library and plugin descriptions. ```cmake elseif($ENV{ROS_DISTRO} MATCHES "foxy|humble") if($ENV{ROS_DISTRO} MATCHES "foxy") add_definitions(-DROS_DISTRO_FOXY) elseif($ENV{ROS_DISTRO} MATCHES "humble") add_definitions(-DROS_DISTRO_HUMBLE) else() add_definitions(-DROS_DISTRO_UNKNOWN) endif() find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(urdf REQUIRED) find_package(control_toolbox REQUIRED) find_package(realtime_tools REQUIRED) find_package(hardware_interface REQUIRED) find_package(controller_interface REQUIRED) find_package(pluginlib REQUIRED) find_package(robot_msgs REQUIRED) find_package(geometry_msgs REQUIRED) find_package(rcl_interfaces REQUIRED) include_directories(ros2/include) add_library(${PROJECT_NAME} SHARED ros2/src/robot_joint_controller.cpp ros2/src/robot_joint_controller_group.cpp ) target_include_directories(${PROJECT_NAME} PRIVATE ros2/include) ament_target_dependencies(${PROJECT_NAME} rclcpp urdf control_toolbox realtime_tools hardware_interface controller_interface pluginlib robot_msgs geometry_msgs rcl_interfaces ) pluginlib_export_plugin_description_file(controller_interface ros2/robot_joint_controller_plugins.xml) install(DIRECTORY ros2/include/ DESTINATION include/${PROJECT_NAME} ) ament_export_libraries(${PROJECT_NAME}) ament_export_dependencies( rclcpp urdf control_toolbox realtime_tools hardware_interface controller_interface pluginlib robot_msgs geometry_msgs rcl_interfaces ) install( TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME} ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin INCLUDES DESTINATION include ) ament_package() endif() ``` -------------------------------- ### Launch Gazebo Simulation Source: https://github.com/fan-ziqi/rl_sar/blob/main/README.md Launches the Gazebo simulation environment for rl_sar. Requires sourcing the appropriate ROS setup file (ROS1 or ROS2) and specifies the robot name. The control program is launched in a separate terminal. ```bash # ROS1 source devel/setup.bash roslaunch rl_sar gazebo.launch rname:= # ROS2 source install/setup.bash ros2 launch rl_sar gazebo.launch.py rname:= ``` -------------------------------- ### Configure Observation Buffer Library (CMake) Source: https://github.com/fan-ziqi/rl_sar/blob/main/src/rl_sar/CMakeLists.txt This snippet sets up the 'observation_buffer' library, which is used for managing observation data. It includes the source file observation_buffer.cpp. Installation is handled conditionally based on the USE_CMAKE flag and ROS distribution. ```cmake add_library(observation_buffer library/core/observation_buffer/observation_buffer.cpp) ``` -------------------------------- ### ROS1 Control Program for Lite3 Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md This command runs the rl_real_lite3 node using ROS1. Ensure you have sourced the ROS1 setup file. ```bash source devel/setup.bash rosrun rl_sar rl_real_lite3 ``` -------------------------------- ### Configure Real A1 Robot Executable (CMake) Source: https://github.com/fan-ziqi/rl_sar/blob/main/src/rl_sar/CMakeLists.txt This snippet defines the 'rl_real_a1' executable for controlling the real A1 robot on Linux systems. It links against UNITREE_A1_LIBS, 'rl_sdk', 'observation_buffer', and YAML CPP. It includes ROS-specific dependencies and installation logic for foxy/humble. ```cmake add_executable(rl_real_a1 src/rl_real_a1.cpp) target_link_libraries(rl_real_a1 ${UNITREE_A1_LIBS} rl_sdk observation_buffer ${YAML_CPP_LIBRARIES} ) if(NOT USE_CMAKE) if($ENV{ROS_DISTRO} MATCHES "noetic") target_link_libraries(rl_real_a1 ${catkin_LIBRARIES}) elseif($ENV{ROS_DISTRO} MATCHES "foxy|humble") ament_target_dependencies(rl_real_a1 rclcpp geometry_msgs ) install(TARGETS rl_real_a1 DESTINATION lib/${PROJECT_NAME}) endif() endif() ``` -------------------------------- ### Configure Real Lite3 Robot Executable (CMake) Source: https://github.com/fan-ziqi/rl_sar/blob/main/src/rl_sar/CMakeLists.txt This snippet defines the 'rl_real_lite3' executable for controlling the real Lite3 robot on Linux systems. It includes gamepad source files and links against LITE3_REAL_LIBS, 'rl_sdk', 'observation_buffer', and YAML CPP. ROS-specific dependencies and installation are handled for foxy/humble. ```cmake add_executable(rl_real_lite3 src/rl_real_lite3.cpp ${GAMEPAD_SRC} ) target_link_libraries(rl_real_lite3 ${LITE3_REAL_LIBS} rl_sdk observation_buffer ${YAML_CPP_LIBRARIES} ) target_include_directories(rl_real_lite3 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/library/thirdparty/robot_sdk/deeprobotics/gamepad/include ) if(NOT USE_CMAKE) if($ENV{ROS_DISTRO} MATCHES "noetic") target_link_libraries(rl_real_lite3 ${catkin_LIBRARIES}) elseif($ENV{ROS_DISTRO} MATCHES "foxy|humble") ament_target_dependencies(rl_real_lite3 rclcpp geometry_msgs ) install(TARGETS rl_real_lite3 DESTINATION lib/${PROJECT_NAME}) endif() endif() ``` -------------------------------- ### ROS2 Control Program for Lite3 Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md This command runs the rl_real_lite3 node using ROS2. Ensure you have sourced the ROS2 setup file. ```bash source install/setup.bash ros2 run rl_sar rl_real_lite3 ``` -------------------------------- ### Start RL-SAR Control Program for Lite3 Robot Source: https://github.com/fan-ziqi/rl_sar/blob/main/README.md These commands demonstrate how to launch the RL-SAR control program for the Deeprobotics Lite3 robot using ROS1, ROS2, or CMake build system. Ensure your ROS environment is sourced correctly or you are in the build directory. Adapt IP and port settings in `rl_sar/src/rl_sar/src/rl_real_lite3.cpp` and `jy_exe/conf/network.toml` for wireless connection. ```bash # ROS1 source devel/setup.bash rosrun rl_sar rl_real_lite3 ``` ```bash # ROS2 source install/setup.bash ros2 run rl_sar rl_real_lite3 ``` ```bash # CMake ./cmake_build/bin/rl_real_lite3 ``` -------------------------------- ### Configure MuJoCo Simulation Environment (CMake) Source: https://github.com/fan-ziqi/rl_sar/blob/main/src/rl_sar/CMakeLists.txt This CMake script configures the build environment for MuJoCo simulation. It checks for MuJoCo installation, sets up platform-specific include and library paths, imports the MuJoCo library, and defines the `rl_sim_mujoco` executable. It also links against necessary libraries and frameworks like GLFW, Threads, and platform-specific graphics libraries on macOS. ```cmake if(USE_MUJOCO) add_compile_definitions(USE_MUJOCO) set(MUJOCO_DIR "${PROJECT_ROOT_DIR}/library/mujoco") # Check MuJoCo installation (different paths for different platforms) if(SYSTEM_TYPE STREQUAL "Darwin") # macOS uses framework structure if(NOT EXISTS "${MUJOCO_DIR}/Headers/mujoco.h") message(FATAL_ERROR "MuJoCo not found. Please run: bash scripts/download_mujoco.sh") endif() # Create compatible include structure in build directory for macOS # This allows to work on macOS where headers are in Headers/ directory set(MUJOCO_COMPAT_INCLUDE_DIR "${CMAKE_BINARY_DIR}/mujoco_include") file(MAKE_DIRECTORY "${MUJOCO_COMPAT_INCLUDE_DIR}/mujoco") file(GLOB MUJOCO_HEADERS "${MUJOCO_DIR}/Headers/*.h") file(COPY ${MUJOCO_HEADERS} DESTINATION "${MUJOCO_COMPAT_INCLUDE_DIR}/mujoco") set(MUJOCO_INCLUDE_DIR "${MUJOCO_COMPAT_INCLUDE_DIR}") set(MUJOCO_LIB_DIR "${MUJOCO_DIR}/Versions/Current") else() # Linux/Windows use standard structure if(NOT EXISTS "${MUJOCO_DIR}/include/mujoco/mujoco.h") message(FATAL_ERROR "MuJoCo not found. Please run: bash scripts/download_mujoco.sh") endif() set(MUJOCO_INCLUDE_DIR "${MUJOCO_DIR}/include") set(MUJOCO_LIB_DIR "${MUJOCO_DIR}/lib") endif() # Add MuJoCo library path to global RPATH set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH};${MUJOCO_LIB_DIR}") message(STATUS "Added MuJoCo to RPATH: ${MUJOCO_LIB_DIR}") # Find GLFW find_package(glfw3 REQUIRED) # Platform-specific library names if(SYSTEM_TYPE STREQUAL "Linux") set(MUJOCO_LIB_NAME "libmujoco.so.3.2.7") elseif(SYSTEM_TYPE STREQUAL "Darwin") set(MUJOCO_LIB_NAME "libmujoco.3.2.7.dylib") elseif(SYSTEM_TYPE STREQUAL "Windows") set(MUJOCO_LIB_NAME "mujoco.dll") else() message(FATAL_ERROR "Unsupported platform: ${SYSTEM_TYPE}") endif() # Import MuJoCo library with RPATH add_library(mujoco_prebuilt SHARED IMPORTED) set_target_properties(mujoco_prebuilt PROPERTIES IMPORTED_LOCATION "${MUJOCO_LIB_DIR}/${MUJOCO_LIB_NAME}" INTERFACE_INCLUDE_DIRECTORIES "${MUJOCO_INCLUDE_DIR}" IMPORTED_NO_SONAME TRUE ) # Include directories include_directories( ${MUJOCO_INCLUDE_DIR} library/thirdparty/joystick ) # Source files file(GLOB MUJOCO_SIMULATE_SRC library/thirdparty/mujoco_simulate/*.cc) set(JOYSTICK_SRC library/thirdparty/joystick/joystick.cc) # On macOS, also include Objective-C++ files for CoreVideo support if(SYSTEM_TYPE STREQUAL "Darwin") file(GLOB MUJOCO_SIMULATE_MM library/thirdparty/mujoco_simulate/*.mm) list(APPEND MUJOCO_SIMULATE_SRC ${MUJOCO_SIMULATE_MM}) endif() # Build executable add_executable(rl_sim_mujoco src/rl_sim_mujoco.cpp ${MUJOCO_SIMULATE_SRC} ${JOYSTICK_SRC}) # Suppress deprecation warnings for macOS 15.0 API changes in MuJoCo Simulate if(SYSTEM_TYPE STREQUAL "Darwin") set_source_files_properties( ${MUJOCO_SIMULATE_MM} PROPERTIES COMPILE_FLAGS "-Wno-deprecated-declarations" ) endif() # Add include directories for mujoco_simulate and joystick headers target_include_directories(rl_sim_mujoco PRIVATE library/thirdparty/mujoco_simulate library/thirdparty/joystick ) target_link_libraries(rl_sim_mujoco rl_sdk observation_buffer ${YAML_CPP_LIBRARIES} Threads::Threads mujoco_prebuilt glfw ) # macOS specific frameworks and libraries if(SYSTEM_TYPE STREQUAL "Darwin") # OpenMP support if(OPENMP_LIBRARIES) target_link_libraries(rl_sim_mujoco ${OPENMP_LIBRARIES}) endif() # Required frameworks for GlfwCoreVideo target_link_libraries(rl_sim_mujoco "-framework CoreVideo" "-framework OpenGL" "-framework AppKit" ) endif() install(TARGETS rl_sim_mujoco DESTINATION bin) message(STATUS "MuJoCo configured: ${MUJOCO_DIR}") endif() ``` -------------------------------- ### Setup Finite State Machine (FSM) for Robot Control (C++) Source: https://context7.com/fan-ziqi/rl_sar/llms.txt Defines and initializes the Finite State Machine (FSM) for robot control, including custom states like 'StandUpState' and transitions between states based on user input or internal conditions. This FSM manages different robot behaviors such as standing up and entering the main RL control loop. ```cpp #include "fsm.hpp" // Custom FSM state for robot control class StandUpState : public RLFSMState { public: StandUpState(RL& rl) : RLFSMState(rl, "stand_up") {} void Enter() override { std::cout << "Entering stand up state" << std::endl; percent = 0.0f; start_pos = rl.robot_state.motor_state.q; target_pos = rl.params.Get>("default_dof_pos"); } void Run() override { // Interpolate from current position to standing pose over 2 seconds if (Interpolate(percent, start_pos, target_pos, 2.0, "Standing up", true)) { // Interpolation complete, transition to next state rl.fsm.SetCurrentState("rl_control"); } } void Exit() override { std::cout << "Stand up complete" << std::endl; } private: float percent; std::vector start_pos, target_pos; }; // Initialize FSM for Go2 robot void InitFSM_Go2(FSM& fsm, RL& rl) { fsm.AddState(std::make_shared(rl)); fsm.AddState(std::make_shared(rl)); fsm.AddState(std::make_shared(rl)); // Define state transitions fsm.AddTransition("stand_up", "rl_control", [&]() { return rl.control.current_keyboard == Input::Keyboard::Num1; }); fsm.AddTransition("rl_control", "dampen", [&]() { return rl.control.current_keyboard == Input::Keyboard::B; }); } ``` -------------------------------- ### Configure Unitree Legged SDK for Linux in CMake Source: https://github.com/fan-ziqi/rl_sar/blob/main/src/rl_sar/CMakeLists.txt This CMake configuration block sets up the Unitree legged SDK for Linux systems. It defines an imported shared library, sets its include and location properties, and specifies linking libraries. It also conditionally installs the SDK's shared object file based on the ROS distro. ```cmake # unitree_legged_sdk if(SYSTEM_TYPE STREQUAL "Linux") add_library(unitree_legged_sdk SHARED IMPORTED) set_target_properties(unitree_legged_sdk PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/library/thirdparty/robot_sdk/unitree/unitree_legged_sdk/include" IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/library/thirdparty/robot_sdk/unitree/unitree_legged_sdk/lib/${UNITREE_LIB}.so" ) set(UNITREE_A1_LIBS Threads::Threads unitree_legged_sdk lcm) if($ENV{ROS_DISTRO} MATCHES "foxy|humble") file(GLOB GLOB_UNITREE_LEGGED_SDK "${CMAKE_CURRENT_SOURCE_DIR}/library/thirdparty/robot_sdk/unitree/unitree_legged_sdk/lib/${UNITREE_LIB}.so") install(FILES ${GLOB_UNITREE_LEGGED_SDK} DESTINATION lib/ ) endif() endif() ``` -------------------------------- ### Download Gazebo Models Source: https://github.com/fan-ziqi/rl_sar/blob/main/README.md Downloads necessary model packages for Gazebo if it fails to open on the first launch. The models are cloned into the default Gazebo models directory. ```bash git clone https://github.com/osrf/gazebo_models.git ~/.gazebo/models ``` -------------------------------- ### Run Real Robot Control for Unitree Go2/Go2W/G1 Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md Launches the control program for Unitree Go2, Go2W, or G1 robots. Requires network interface configuration and optionally a 'wheel' argument for Go2W. G1 requires entering debug mode before launching. ```bash # Go2: # ROS1 source devel/setup.bash rosrun rl_sar rl_real_go2 [wheel] # ROS2 source install/setup.bash ros2 run rl_sar rl_real_go2 [wheel] # CMake ./cmake_build/bin/rl_real_go2 [wheel] ``` ```bash # G1(29dofs): # ROS1 source devel/setup.bash rosrun rl_sar rl_real_g1 ``` -------------------------------- ### Restart rl_sar Service Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md This command restarts the 'rl_sar.service', stopping it if it's running and then starting it again. ```bash sudo systemctl restart rl_sar.service ``` -------------------------------- ### Disable rl_sar Service for Auto-start Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md This command prevents the 'rl_sar.service' from starting automatically upon system boot. ```bash sudo systemctl disable rl_sar.service ``` -------------------------------- ### Run Real Robot Control Program for Unitree Go2 (ROS1, ROS2, CMake) Source: https://github.com/fan-ziqi/rl_sar/blob/main/README.md Launches the control program for Unitree Go2 robots. This command requires specifying your network interface and optionally 'wheel' for Go2W models. Ensure the network interface is correctly identified and the appropriate ROS environment is sourced. ```bash # ROS1 source devel/setup.bash rosrun rl_sar rl_real_go2 [wheel] # ROS2 source install/setup.bash ros2 run rl_sar rl_real_go2 [wheel] # CMake ./cmake_build/bin/rl_real_go2 [wheel] ``` -------------------------------- ### Configure RPATH for Inference Libraries Source: https://github.com/fan-ziqi/rl_sar/blob/main/src/rl_sar/CMakeLists.txt Sets up the RPATH (Run-time Search Path) for inference libraries (ONNX Runtime and LibTorch) when either is enabled. It configures CMake to skip build RPATH, use install RPATH, and sets the installation RPATH to include the library directories of LibTorch and ONNX Runtime. For Linux systems, it also adds a flag to the linker to disable new DT_FLAGS. ```cmake # RPATH Configuration for inference libraries if(USE_TORCH OR USE_ONNX) set(CMAKE_SKIP_BUILD_RPATH FALSE) set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE) set(PREBUILT_LIB_PATHS "") if(USE_TORCH) list(APPEND PREBUILT_LIB_PATHS "${LIBTORCH_DIR}/lib") endif() if(USE_ONNX) list(APPEND PREBUILT_LIB_PATHS "${ONNX_RUNTIME_DIR}/lib") endif() # OpenMP path will be added later after it's found on macOS set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH};${PREBUILT_LIB_PATHS}") if(CMAKE_SYSTEM_NAME STREQUAL "Linux") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--disable-new-dtags") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--disable-new-dtags") endif() message(STATUS "Configured RPATH:") foreach(path ${PREBUILT_LIB_PATHS}) message(STATUS " - ${path}") endforeach() endif() ``` -------------------------------- ### Run Web Control Services Source: https://github.com/fan-ziqi/rl_sar/blob/main/README.md Launches rosbridge and web_video_server services on the robot. These are required for the experimental mobile web control interface. Commands are provided for both ROS1 and ROS2. ```bash # ROS1 roslaunch rosbridge_server rosbridge_websocket.launch rosrun web_video_server web_video_server # ROS2 ros2 launch rosbridge_server rosbridge_websocket_launch.xml ros2 run web_video_server web_video_server ``` -------------------------------- ### CMake Build and Execution for rl_real_g1 Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md This command executes the rl_real_g1 binary built with CMake. Replace '' with the appropriate network interface. ```bash ./cmake_build/bin/rl_real_g1 ``` -------------------------------- ### Build and Run rl_real_go2 on Jetson Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md After connecting to the Jetson, enabling USB network sharing, and compiling with './build.sh -m', this command runs the rl_real_go2 executable. The optional '[wheel]' argument can be added if needed. ```bash ./cmake_build/bin/rl_real_go2 [wheel] ``` -------------------------------- ### Compile rl_sar Project Source: https://github.com/fan-ziqi/rl_sar/blob/main/README.md Builds the entire rl_sar project or specific packages. Options include cleaning the build, building with CMake for hardware deployment, and building with Mujoco simulator support. Use -h for detailed usage. ```bash # Compile the entire project ./build.sh # Compile specific packages ./build.sh package1 package2 # Clean the build ./build.sh -c # or ./build.sh --clean # Compile using CMake (hardware deployment) ./build.sh -m # or ./build.sh --cmake # Compile with Mujoco simulator support ./build.sh -mj # or ./build.sh --mujoco # Show help message ./build.sh -h ``` -------------------------------- ### Configure RL Simulation Executable (CMake) Source: https://github.com/fan-ziqi/rl_sar/blob/main/src/rl_sar/CMakeLists.txt This snippet defines the 'rl_sim' executable for reinforcement learning simulations. It links against 'rl_sdk', 'observation_buffer', YAML CPP, and Threads. It includes ROS-specific dependencies for foxy/humble distributions via ament_target_dependencies. ```cmake add_executable(rl_sim src/rl_sim.cpp) target_link_libraries(rl_sim rl_sdk observation_buffer ${YAML_CPP_LIBRARIES} Threads::Threads ) if($ENV{ROS_DISTRO} MATCHES "noetic") target_link_libraries(rl_sim ${catkin_LIBRARIES}) elseif($ENV{ROS_DISTRO} MATCHES "foxy|humble") ament_target_dependencies(rl_sim joint_state_broadcaster robot_state_publisher rclcpp gazebo_ros std_msgs robot_msgs robot_joint_controller rclpy gazebo_msgs std_srvs geometry_msgs ) install(TARGETS rl_sim DESTINATION lib/${PROJECT_NAME}) endif() ``` -------------------------------- ### Integrate Unitree SDK2 in CMake Source: https://github.com/fan-ziqi/rl_sar/blob/main/src/rl_sar/CMakeLists.txt This snippet integrates the Unitree SDK2 into the build system for Linux systems. It disables example builds for SDK2 and adds the SDK2's subdirectory to the CMake build. This allows the project to utilize functionalities provided by Unitree SDK2. ```cmake # unitree_sdk2 if(SYSTEM_TYPE STREQUAL "Linux") set(BUILD_EXAMPLES OFF CACHE BOOL "Disable examples build for unitree_sdk2" FORCE) add_subdirectory(library/thirdparty/robot_sdk/unitree/unitree_sdk2) endif() ``` -------------------------------- ### Robot Description File Structure Source: https://github.com/fan-ziqi/rl_sar/blob/main/README.md This YAML structure outlines the essential files required to add a new robot to the RL-SAR project. It includes paths for robot descriptions, policies (LibTorch and ONNX), and Finite State Machine (FSM) configurations. Refer to existing robot configurations (e.g., go2w) for examples. ```yaml # your robot description rl_sar/src/rl_sar_zoo/_description/CMakeLists.txt rl_sar/src/rl_sar_zoo/_description/package.ros1.xml rl_sar/src/rl_sar_zoo/_description/package.ros2.xml rl_sar/src/rl_sar_zoo/_description/xacro/robot.xacro rl_sar/src/rl_sar_zoo/_description/xacro/gazebo.xacro rl_sar/src/rl_sar_zoo/_description/config/robot_control.yaml rl_sar/src/rl_sar_zoo/_description/config/robot_control_ros2.yaml # your policy policy//base.yaml # This file must follow the physical robot's joint order policy///config.yaml policy///.pt # for libtorch, note that exporting JIT is required policy///.onnx # for onnxruntime # fsm for robot src/rl_sar/fsm_robot/fsm_.hpp src/rl_sar/fsm_robot/fsm_all.hpp # your real robot code rl_sar/src/rl_sar/src/rl_real_.cpp # You can customize the forward() function as needed to adapt to your policy ``` -------------------------------- ### Configure Lite3 Motion SDK for Linux in CMake Source: https://github.com/fan-ziqi/rl_sar/blob/main/src/rl_sar/CMakeLists.txt This CMake configuration sets up the Lite3 Motion SDK for Linux. It defines an imported shared library, specifies its include directories and imported location, and sets the linking libraries. Similar to Unitree SDK, it conditionally installs the SDK's shared object file based on the ROS distro. ```cmake # Lite3_MotionSDK if(SYSTEM_TYPE STREQUAL "Linux") add_library(lite3_motionsdk SHARED IMPORTED) set_target_properties(lite3_motionsdk PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/library/thirdparty/robot_sdk/deeprobotics/Lite3_MotionSDK/include;${CMAKE_CURRENT_SOURCE_DIR}/library/thirdparty/robot_sdk/deeprobotics/Lite3_MotionSDK/include/common" IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/library/thirdparty/robot_sdk/deeprobotics/Lite3_MotionSDK/lib/libdeeprobotics_legged_sdk_${ARCH_DIR}.so" ) set(LITE3_REAL_LIBS Threads::Threads lite3_motionsdk) if($ENV{ROS_DISTRO} MATCHES "foxy|humble") file(GLOB GLOB_LITE3_SDK_SO "${CMAKE_CURRENT_SOURCE_DIR}/library/thirdparty/robot_sdk/deeprobotics/Lite3_MotionSDK/lib/libdeeprobotics_legged_sdk_${ARCH_DIR}.so") install(FILES ${GLOB_LITE3_SDK_SO} DESTINATION lib/ ) endif() endif() ``` -------------------------------- ### Configure Real Go2 Robot Executable (CMake) Source: https://github.com/fan-ziqi/rl_sar/blob/main/src/rl_sar/CMakeLists.txt This snippet defines the 'rl_real_go2' executable for controlling the real Go2 robot on Linux systems. It links against 'unitree_sdk2', 'rl_sdk', 'observation_buffer', and YAML CPP. It also configures RPATH for the unitree_sdk2 library and includes ROS-specific dependencies for foxy/humble. ```cmake add_executable(rl_real_go2 src/rl_real_go2.cpp) target_link_libraries(rl_real_go2 unitree_sdk2 rl_sdk observation_buffer ${YAML_CPP_LIBRARIES} ) # Add unitree_sdk2 library path to RPATH set_target_properties(rl_real_go2 PROPERTIES INSTALL_RPATH "${CMAKE_INSTALL_RPATH};${CMAKE_CURRENT_SOURCE_DIR}/library/thirdparty/robot_sdk/unitree/unitree_sdk2/thirdparty/lib/${ARCH_DIR}" ) if(NOT USE_CMAKE) if($ENV{ROS_DISTRO} MATCHES "noetic") target_link_libraries(rl_real_go2 ${catkin_LIBRARIES}) elseif($ENV{ROS_DISTRO} MATCHES "foxy|humble") ament_target_dependencies(rl_real_go2 rclcpp geometry_msgs ) install(TARGETS rl_real_go2 DESTINATION lib/${PROJECT_NAME}) endif() endif() ``` -------------------------------- ### Systemd Service Configuration for rl_sar Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md This is the content for the 'rl_sar.service' file, configuring it to run the rl_real_go2 executable as a simple service. It sets the user, working directory, execution command, and restart policies. ```ini [Unit] Description=RL SAR Service After=network.target [Service] Type=simple User=unitree WorkingDirectory=/home/unitree/rl_sar ExecStart=/home/unitree/rl_sar/cmake_build/bin/rl_real_go2 eth0 wheel Restart=on-failure RestartSec=5 StandardOutput=journal StandardError=journal [Install] WantedBy=multi-user.target ``` -------------------------------- ### Run Mujoco Simulation Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md Executes the Mujoco simulation using the compiled rl_sim_mujoco binary. Requires specifying the robot type and scene as arguments. ```bash ./cmake_build/bin/rl_sim_mujoco # Example: ./cmake_build/bin/rl_sim_mujoco g1 scene_29dof ``` -------------------------------- ### Convert ONNX (.onnx) to PyTorch (.pt) Source: https://context7.com/fan-ziqi/rl_sar/llms.txt Converts an ONNX model file (.onnx) back to a PyTorch model file (.pt) using the 'onnx2torch' library. This process requires the 'onnx2torch' package to be installed. The conversion attempts to script the model for structure preservation, but users are advised to keep the original .pt file as structural differences may occur. ```bash # Convert .onnx to .pt (requires onnx2torch) pip install onnx2torch rosrun rl_sar convert_policy.py policy/go2/himloco/himloco.onnx # Output: # ============================================================ # Mode: ONNX (.onnx) â€Â212Â204) PyTorch (.pt) # ============================================================ # Loading ONNX model: policy/go2/himloco/himloco.onnx # Reading input/output dimensions from ONNX model... # âœâœ Detected input size: 45, output size: 12 # Converting to PyTorch format... # Testing converted model... # Attempting to script model (preserves structure)... # âœâœ Successfully scripted and saved to: policy/go2/himloco/himloco.pt # # âœ! Note: Model structure from ONNX conversion may differ from original # Recommendation: Keep original .pt file if available # # Verifying conversion... # Max difference between ONNX and PyTorch outputs: 4.1e-07 # âœâœ Conversion verified successfully! # ``` -------------------------------- ### Run Real Robot Control for Unitree A1 Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md Executes the control program for the Unitree A1 robot. Supports ROS1, ROS2, and CMake builds. Requires correct network configuration for wired connections. ```bash # ROS1 source devel/setup.bash rosrun rl_sar rl_real_a1 # ROS2 source install/setup.bash ros2 run rl_sar rl_real_a1 # CMake ./cmake_build/bin/rl_real_a1 ``` -------------------------------- ### Run Real Robot Control Program (ROS1, ROS2, CMake) Source: https://github.com/fan-ziqi/rl_sar/blob/main/README.md Executes the real robot control program for Unitree A1. This command assumes you are in the project's root directory and have sourced the necessary ROS environment. It's crucial to ensure the correct ROS version (ROS1 or ROS2) is sourced before running the command. ```bash # ROS1 source devel/setup.bash rosrun rl_sar rl_real_a1 # ROS2 source install/setup.bash ros2 run rl_sar rl_real_a1 # CMake ./cmake_build/bin/rl_real_a1 ``` -------------------------------- ### CMake Control Program for Lite3 Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md This command executes the rl_real_lite3 binary built with CMake. ```bash ./cmake_build/bin/rl_real_lite3 ``` -------------------------------- ### Create systemd Service File Source: https://github.com/fan-ziqi/rl_sar/blob/main/README_CN.md This command creates a new systemd service file named 'rl_sar.service' in the '/etc/systemd/system/' directory using sudo privileges. ```bash sudo touch /etc/systemd/system/rl_sar.service ``` -------------------------------- ### ONNX Runtime Configuration Variables Source: https://github.com/fan-ziqi/rl_sar/blob/main/src/rl_sar/CMakeLists.txt Sets up configuration variables for the ONNX Runtime inference library, including its directory and a flag to enable its use. It initializes the ONNX Runtime library variable to an empty string. ```cmake # Inference Runtime set(INFERENCE_RUNTIME_DIR "${PROJECT_ROOT_DIR}/library/inference_runtime") # ONNX Runtime Setup set(USE_ONNX "OFF") set(ONNX_RUNTIME_LIB "") set(ONNX_RUNTIME_DIR "${INFERENCE_RUNTIME_DIR}/onnxruntime") ``` -------------------------------- ### Run Mujoco Simulation Source: https://github.com/fan-ziqi/rl_sar/blob/main/README.md Executes the rl_sim_mujoco executable for running simulations with the Mujoco simulator. Requires building with the --mujoco flag. Takes robot name and scene as arguments. ```bash ./cmake_build/bin/rl_sim_mujoco # Example: ./cmake_build/bin/rl_sim_mujoco g1 scene_29dof ``` -------------------------------- ### Project Root and Compile Definitions Source: https://github.com/fan-ziqi/rl_sar/blob/main/src/rl_sar/CMakeLists.txt Determines the absolute path to the project's root directory and defines preprocessor macros for the current source directory, policy directory, and Boost placeholders. ```cmake # Get project root directory get_filename_component(PROJECT_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../.." ABSOLUTE) add_definitions(-DCMAKE_CURRENT_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}") add_definitions(-DPOLICY_DIR="${PROJECT_ROOT_DIR}/policy") add_definitions(-DBOOST_BIND_GLOBAL_PLACEHOLDERS) ```