### Build and Run ROS 2 Examples Source: https://x2-aimdk.agibot.com/zh-cn/latest/example/Python Instructions to build and run ROS 2 examples from the aimdk directory. This involves sourcing the ROS Humble environment, building the workspace with colcon, sourcing the install setup, and then running a specific Python example. ```bash source /opt/ros/humble/setup.bash colcon build source install/setup.bash ros2 run py_examples '对应功能名称如: get_mc_action' ``` -------------------------------- ### ROS2 Command-Line Execution Examples Source: https://x2-aimdk.agibot.com/zh-cn/latest/example/Cpp These are command-line examples demonstrating how to run the 'examples echo_camera_head_rear' ROS2 node with different parameters. They show how to subscribe to various image topics (RGB, compressed) and how to enable video recording to a specified file path. These commands are executed in a ROS2 environment. ```bash # 订阅RGB图像数据: ros2 run examples echo_camera_head_rear --ros-args -p topic_type:=rgb_image ``` ```bash # 订阅压缩图像数据: ros2 run examples echo_camera_head_rear --ros-args -p topic_type:=rgb_image_compressed ``` ```bash # 订阅相机内参: ros2 run examples echo_camera_head_rear --ros-args -p topic_type:=camera_info ``` ```bash # 录制视频: # dump_video_path的值可改为其他路径, 注意提前创建该文件所在目录才能保存 ros2 run examples echo_camera_head_rear --ros-args -p topic_type:=rgb_image -p dump_video_path:=$PWD/rear_camera.avi ``` -------------------------------- ### Register Example in setup.py for ROS 2 Source: https://x2-aimdk.agibot.com/zh-cn/latest/quick_start/code_sample Illustrates how to add an entry point in the `setup.py` file to make the new example runnable via the `ros2 run` command. This step is crucial for integrating the script into the ROS 2 build system. ```python # Example entry point addition in setup.py # ... other setup configurations ... entry_points={ 'console_scripts': [ 'action_sequence_demo = py_examples.action_sequence_demo:main', ], }, # ... ``` -------------------------------- ### Build and Run Agibot LED Example (Shell) Source: https://x2-aimdk.agibot.com/zh-cn/latest/example/Cpp Instructions for building the Agibot LED example using colcon and running it with ros2. Assumes a ROS 2 environment is set up. ```shell # 构建 colcon build --packages-select examples # 运行 ros2 run examples play_lights ``` -------------------------------- ### Python Example: Play Video Source: https://x2-aimdk.agibot.com/zh-cn/latest/Interface/interactor/screen This is a placeholder for a Python code example demonstrating how to play video files using the screen control interface. It would typically involve creating a ROS 2 client to call the PlayVideo service. ```python # Python 示例:播放视频 # (具体实现请参考项目提供的示例代码) ``` -------------------------------- ### C++ Example: Play Video Source: https://x2-aimdk.agibot.com/zh-cn/latest/Interface/interactor/screen This is a placeholder for a C++ code example demonstrating how to play video files using the screen control interface. It would typically involve creating a ROS 2 client to call the PlayVideo service. ```cpp // C++ 示例:播放视频 // (具体实现请参考项目提供的示例代码) ``` -------------------------------- ### ROS 2 Command Line Execution Examples Source: https://x2-aimdk.agibot.com/zh-cn/latest/example/Cpp These examples demonstrate how to run the robot mode setting executable using ROS 2 commands. They show how to pass the desired mode as a command-line argument or run the executable without arguments to enable interactive input. ```bash # Use command-line arguments to set the mode (recommended) ros2 run examples set_mc_action JD # Zero-torque mode >> Standby (position control standing) ros2 run examples set_mc_action SD # Execute after robot is standing and feet are on the ground, Standby >> Stable standing # Stable standing >> Walking/Running mode automatic switching, no manual switching required # Or run without arguments, the program will prompt the user to enter a two-letter abbreviation code ros2 run examples set_mc_action ``` -------------------------------- ### Agibot LED Strip Control Example Output Source: https://x2-aimdk.agibot.com/zh-cn/latest/example/Cpp An example of the expected output when running the Agibot LED strip control example, showing user prompts and successful command confirmation. ```text === LED灯带控制示例 === 请选择灯带模式: 0 - 常亮模式 1 - 呼吸模式 (4s周期,亮度正弦变化) 2 - 闪烁模式 (1s周期,0.5s亮,0.5s灭) 3 - 流水模式 (2s周期,从左到右依次点亮) 请输入模式 (0-3): 1 请设置RGB颜色值 (0-255): 红色分量 (R): 255 绿色分量 (G): 0 蓝色分量 (B): 0 发送LED控制命令... 模式: 1, 颜色: RGB(255, 0, 0) ✅ LED strip command sent successfully ``` -------------------------------- ### ROS 2 Command Line Usage Source: https://x2-aimdk.agibot.com/zh-cn/latest/example/Python Provides example commands to run the 'py_examples' ROS 2 package, specifically the 'echo_lidar_data' node. These commands demonstrate how to subscribe to either LiDAR PointCloud2 data or IMU data by setting the 'topic_type' parameter. ```bash # 订阅激光雷达点云数据 ros2 run py_examples echo_lidar_data --ros-args -p topic_type:=pointcloud # 订阅激光雷达IMU数据 ros2 run py_examples echo_lidar_data --ros-args -p topic_type:=imu ``` -------------------------------- ### 运行LED灯带控制命令 (ROS 2) Source: https://x2-aimdk.agibot.com/zh-cn/latest/example/Python 使用 'ros2 run' 命令执行 'play_lights' 节点,并传递命令行参数来设置LED灯带的模式和颜色。第一个参数是模式(0-3),后续三个参数是RGB颜色分量(0-255)。 ```bash ros2 run py_examples play_lights 1 255 0 0 # 模式1,红色 ``` -------------------------------- ### Python Example: Control Emoji Source: https://x2-aimdk.agibot.com/zh-cn/latest/Interface/interactor/screen This is a placeholder for a Python code example demonstrating how to control emoji playback using the screen control interface. It would typically involve creating a ROS 2 client to call the PlayEmoji service. ```python # Python 示例:表情控制 # (具体实现请参考项目提供的示例代码) ``` -------------------------------- ### C++ Example: Control Emoji Source: https://x2-aimdk.agibot.com/zh-cn/latest/Interface/interactor/screen This is a placeholder for a C++ code example demonstrating how to control emoji playback using the screen control interface. It would typically involve creating a ROS 2 client to call the PlayEmoji service. ```cpp // C++ 示例:表情控制 // (具体实现请参考项目提供的示例代码) ``` -------------------------------- ### Get Robot Mode Example (Python) Source: https://x2-aimdk.agibot.com/zh-cn/latest/Interface/control_mod/modeswitch Python code example for retrieving the robot's current motion mode. This snippet demonstrates how to call the query motion mode service. ```Python # Python 示例 :获取机器人模式 # Refer to the official documentation for the complete code. ``` -------------------------------- ### Get Robot Mode Example (C++) Source: https://x2-aimdk.agibot.com/zh-cn/latest/Interface/control_mod/modeswitch C++ code example for retrieving the robot's current motion mode. This snippet demonstrates how to call the query motion mode service. ```C++ // C++ 示例 : 获取机器人模式 // Refer to the official documentation for the complete code. ``` -------------------------------- ### Get Robot Mode (C++) Source: https://x2-aimdk.agibot.com/zh-cn/latest/example/Cpp This example demonstrates how to call the `GetMcAction` service to retrieve the robot's current operating mode, including its name and status. ```APIDOC ## POST /aimdk_5Fmsgs/srv/GetMcAction ### Description This service retrieves the robot's current operating mode, providing its name and status. ### Method POST ### Endpoint `/aimdk_5Fmsgs/srv/GetMcAction` ### Parameters #### Request Body - **request** (aimdk_msgs/msg/CommonRequest) - Required - Header information for the request. - **header** (aimdk_msgs/msg/ResponseHeader) - Required - Standard response header. - **stamp** (time) - Required - Timestamp of the request. ### Request Example ```json { "request": { "header": { "stamp": "current_time" } } } ``` ### Response #### Success Response (200) - **info** (aimdk_msgs/msg/ResponseHeader) - Required - Information about the robot's mode. - **action_desc** (string) - Description of the robot's current mode. - **status** (int) - The status code of the current mode. #### Response Example ```json { "info": { "action_desc": "PASSIVE_DEFAULT", "status": { "value": 100 } } } ``` ### Build & Run Instructions 1. Navigate to the root directory of the extracted SDK. 2. Execute the following commands: ```bash source /opt/ros/humble/setup.bash colcon build source install/setup.bash ros2 run examples get_mc_action ``` ### Output Example ``` ... [INFO] [timestamp] [get_mc_action_client]: Current robot mode: [INFO] [timestamp] [get_mc_action_client]: Mode name: PASSIVE_DEFAULT [INFO] [timestamp] [get_mc_action_client]: Mode status: 100 ``` ### Notes - The complete code implementation includes robust error handling, signal processing, and timeout mechanisms. - ROS service mechanisms may require optimization for cross-board communication. Consider implementing exception handling and rapid retry mechanisms during secondary development. ``` -------------------------------- ### Run ROS2 Node to Record Video Source: https://x2-aimdk.agibot.com/zh-cn/latest/example/Python This command starts the ROS2 `echo_camera_head_rear` node to capture RGB images and save them to a video file. The `dump_video_path` parameter specifies the output file (e.g., `rear_camera.avi`), and the directory must exist beforehand. This functionality is useful for offline analysis of camera feeds. ```bash # dump_video_path的值可改为其他路径, 注意提前创建该文件所在目录才能保存 ros2 run py_examples echo_camera_head_rear --ros-args -p topic_type:=rgb_image -p dump_video_path:=$PWD/rear_camera.avi ``` -------------------------------- ### Get Robot Mode using GetMcAction Service (Python) Source: https://x2-aimdk.agibot.com/zh-cn/latest/example/Python This Python script demonstrates how to call the `GetMcAction` ROS service to retrieve the robot's current operating mode. It includes service client creation, waiting for service availability, sending asynchronous requests, and handling responses or failures. The code is designed for robustness with error handling and retry mechanisms. ```python #!/usr/bin/env python3 import rclpy import rclpy.logging from rclpy.node import Node from aimdk_msgs.srv import GetMcAction from aimdk_msgs.msg import CommonRequest class GetMcActionClient(Node): def __init__(self): super().__init__('get_mc_action_client') self.client = self.create_client( GetMcAction, '/aimdk_5Fmsgs/srv/GetMcAction') self.get_logger().info('✅ GetMcAction client node created.') # Wait for the service to become available while not self.client.wait_for_service(timeout_sec=2.0): self.get_logger().info('⏳ Service unavailable, waiting...') self.get_logger().info('🟢 Service available, ready to send request.') def send_request(self): request = GetMcAction.Request() request.request = CommonRequest() self.get_logger().info('📨 Sending request to get robot mode') for i in range(8): request.request.header.stamp = self.get_clock().now().to_msg() future = self.client.call_async(request) rclpy.spin_until_future_complete(self, future, timeout_sec=0.25) if future.done(): break # retry as remote peer is NOT handled well by ROS self.get_logger().info(f'trying ... [{i}]') response = future.result() if response is None: self.get_logger().error('❌ Service call failed or timed out.') return self.get_logger().info('✅ Robot mode get successfully.') self.get_logger().info(f'Mode name: {response.info.action_desc}') self.get_logger().info(f'Mode status: {response.info.status.value}') def main(args=None): rclpy.init(args=args) node = None try: node = GetMcActionClient() node.send_request() except KeyboardInterrupt: pass except Exception as e: rclpy.logging.get_logger('main').error( f'Program exited with exception: {e}') if node: node.destroy_node() if rclpy.ok(): rclpy.shutdown() if __name__ == '__main__': main() ``` -------------------------------- ### Build Project with colcon Source: https://x2-aimdk.agibot.com/zh-cn/latest/quick_start/code_sample Commands to build the 'py_examples' package using colcon. This involves navigating to the SDK directory, sourcing the ROS 2 Humble environment, and then executing the build command. Ensure the path to aimdk and ROS 2 setup is correct. ```bash # 进入SDK目录 # 注意将以下路径替换为解压后实际路径 cd /path/to/aimdk # 设置环境变量 source /opt/ros/humble/setup.bash # 构建项目 colcon build --packages-select py_examples ``` -------------------------------- ### Install curse Module for Keyboard Control Source: https://x2-aimdk.agibot.com/zh-cn/latest/example/Cpp This command installs the necessary 'curse' module, which is a prerequisite for enabling keyboard input control in the robot's terminal. ```bash sudo apt install libncurses-dev ``` -------------------------------- ### Add entry_points for Action Sequence Demo Source: https://x2-aimdk.agibot.com/zh-cn/latest/quick_start/code_sample This snippet shows how to add the 'action_sequence_demo' entry point to the setup.py file's entry_points configuration. This is crucial for making the Python script executable as a ROS 2 node. ```python "action_sequence_demo = py_examples.action_sequence_demo:main", ``` -------------------------------- ### Set Robot Mode Example (Python) Source: https://x2-aimdk.agibot.com/zh-cn/latest/Interface/control_mod/modeswitch Python code example for setting the robot's motion mode. This snippet demonstrates how to call the set motion mode service. ```Python # Python 示例 :设置机器人模式 # Refer to the official documentation for the complete code. ``` -------------------------------- ### Complete entry_points configuration Source: https://x2-aimdk.agibot.com/zh-cn/latest/quick_start/code_sample This shows the complete 'entry_points' section in setup.py, including the 'console_scripts' list where the 'action_sequence_demo' entry point should be added. This ensures the script is recognized as a command-line script by ROS 2. ```python entry_points={ "console_scripts": [ # ... 现有条目 ... "action_sequence_demo = py_examples.action_sequence_demo:main", ], }, ``` -------------------------------- ### Camera Topic Echo Node Initialization (Python) Source: https://x2-aimdk.agibot.com/zh-cn/latest/example/Python This Python code snippet demonstrates the initialization and spinning of the `CameraTopicEcho` node within a ROS 2 environment. It includes error handling for `KeyboardInterrupt` and general exceptions, ensuring proper node destruction and ROS shutdown. ```python def main(args=None): rclpy.init(args=args) try: node = CameraTopicEcho() rclpy.spin(node) except KeyboardInterrupt: pass except Exception as e: print(f"Error: {e}") finally: if 'node' in locals(): node.destroy_node() rclpy.shutdown() if __name__ == '__main__': main() ``` -------------------------------- ### Set Robot Mode Example (C++) Source: https://x2-aimdk.agibot.com/zh-cn/latest/Interface/control_mod/modeswitch C++ code example for setting the robot's motion mode. This snippet demonstrates how to call the set motion mode service. ```C++ // C++ 示例 : 设置机器人模式 // Refer to the official documentation for the complete code. ``` -------------------------------- ### Build SDK with Colcon Source: https://x2-aimdk.agibot.com/zh-cn/latest/quick_start/prerequisites This snippet demonstrates how to build the SDK using colcon after extracting it to the target environment. It includes setting up the ROS 2 environment variables and then building the SDK. This is typically done on the development compute unit. ```bash # Set environment variables (skip this step for direct run mode on development compute unit) source /opt/ros/humble/setup.bash # Build SDK # Assuming the copied and extracted directory is named aimdk in the X2 AimDK root directory cd ./aimdk/ colcon build ``` -------------------------------- ### GetMcAction Service Client Source: https://x2-aimdk.agibot.com/zh-cn/latest/example/Python This section details how to use the GetMcAction service to retrieve the robot's current operating mode, including its name and status. It provides a Python client example for interacting with this ROS service. ```APIDOC ## POST /aimdk_5Fmsgs/srv/GetMcAction ### Description This endpoint allows you to retrieve the robot's current operating mode, including its name and status, by calling the `GetMcAction` service. ### Method POST ### Endpoint /aimdk_5Fmsgs/srv/GetMcAction ### Parameters #### Query Parameters None #### Request Body - **request** (aimdk_msgs.msg.CommonRequest) - Required - The request header, typically including a timestamp. ### Request Example ```json { "request": { "header": { "stamp": "timestamp" } } } ``` ### Response #### Success Response (200) - **info** (aimdk_msgs.msg.McActionInfo) - Description of the robot's action mode. - **action_desc** (string) - The name of the robot's current mode. - **status** (aimdk_msgs.msg.Status) - The current status of the robot's mode. - **value** (integer) - The status code. #### Response Example ```json { "info": { "action_desc": "Running Mode", "status": { "value": 1 } } } ``` ### Notes - The provided Python code includes error handling, signal processing, and timeout mechanisms for robustness. - When developing, it is recommended to implement additional exception handling and rapid retry mechanisms, as ROS service mechanisms may have optimization issues during cross-board communication. ```