### Build Unitree ROS2 Example
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Sources the main setup script, changes the current directory to the example folder, and builds the ROS2 example package using colcon. This step compiles the example source code into an executable.
```bash
source ~/unitree_ros2/setup.sh
cd ~/unitree_ros2/example
colcon build
```
--------------------------------
### Run Unitree ROS2 Motion State Example
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Executes the compiled 'read_motion_state' example program located in the install directory. This program demonstrates how to subscribe to and display the robot's motion state data.
```bash
./install/unitree_ros2_example/bin/read_motion_state
```
--------------------------------
### Example Motion State Output
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Sample output from the 'read_motion_state' example program, showing the format of the robot's motion state information including position, velocity, and foot contact data.
```bash
[INFO] [1697525196.266174885] [motion_state_suber]: Position -- x: 0.567083; y: 0.213920; z: 0.052338; body height: 0.320000
[INFO] [1697525196.266230044] [motion_state_suber]: Velocity -- vx: -0.008966; vy: -0.001431; vz: -0.019455; yaw: -0.002131
[INFO] [1697525196.266282725] [motion_state_suber]: Foot position and velcity relative to body -- num: 0; x: 0.204149; y: -0.145194; z: -0.067804, vx: 0.002683; vy: 0.003745; vz: -0.010052
[INFO] [1697525196.266339057] [motion_state_suber]: Foot position and velcity relative to body -- num: 1; x: 0.204200; y: 0.145049; z: -0.068205, vx: -0.001954; vy: -0.003442; vz: -0.004828
[INFO] [1697525196.266392028] [motion_state_suber]: Foot position and velcity relative to body -- num: 2; x: -0.183385; y: -0.159294; z: -0.039468, vx: -0.000739; vy: -0.002028; vz: -0.004532
[INFO] [1697525196.266442766] [motion_state_suber]: Foot position and velcity relative to body -- num: 3; x: -0.182412; y: 0.159754; z: -0.039045, vx: -0.002803; vy: -0.001381; vz: -0.004794
[INFO] [1697525196.316189064] [motion_state_suber]: Gait state -- gait type: 1; raise height: 0.090000
```
--------------------------------
### Install Targets
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/example/src/CMakeLists.txt
Specifies which executable targets should be installed and their destination directory.
```CMake
install(TARGETS
low_level_ctrl
low_level_ctrl_hg
g1_low_level_example
read_low_state
read_low_state_hg
read_motion_state
read_wireless_controller
sport_mode_ctrl
DESTINATION)
```
--------------------------------
### Project Setup and Minimum Version
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/example/src/CMakeLists.txt
Sets the minimum required CMake version and defines the project name for the ROS2 package.
```CMake
cmake_minimum_required(VERSION 3.5)
project(unitree_ros2_example)
```
--------------------------------
### Bash Run Sportmode State Example
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Provides the command to execute the compiled example program `read_motion_state`. This program demonstrates how to subscribe to and read the robot's sport mode state from the relevant ROS2 topic.
```bash
./install/unitree_ros2_example/bin/read_motion_state
```
--------------------------------
### Source Unitree ROS2 Local Setup Script
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Sources an alternative setup script that configures the environment to use the local loopback interface ('lo'). This is intended for simulation or testing without a physical robot connection.
```bash
source ~/unitree_ros2/setup_local.sh
```
--------------------------------
### Verify ROS2 Topics
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Sources the main setup script and then lists the available ROS2 topics. This sequence is a common test to confirm that the ROS2 environment is active and communicating correctly.
```bash
source ~/unitree_ros2/setup.sh
ros2 topic list
```
--------------------------------
### Launching ROS 2 Rviz2 Visualization (Bash)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Runs the `rviz2` executable from the `rviz2` package to start the ROS 2 visualization tool. Rviz2 is used to display sensor data, robot models, and other information.
```bash
ros2 run rviz2 rviz2
```
--------------------------------
### Unitree ROS2 Setup Script Content
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
The content of the main setup script. It sources ROS Foxy and CycloneDDS setups, sets the RMW implementation, and configures the CycloneDDS network interface. The 'name' attribute in the NetworkInterface tag must match your robot's network interface (e.g., 'enp3s0').
```bash
#!/bin/bash
echo "Setup unitree ros2 environment"
source /opt/ros/foxy/setup.bash
source $HOME/unitree_ros2/cyclonedds_ws/install/setup.bash
export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp
export CYCLONEDDS_URI='
'
```
--------------------------------
### Source Unitree ROS2 Default Setup Script
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Sources a setup script that does not explicitly specify a network interface for CycloneDDS. Use this if the interface is handled automatically or not required for your specific use case.
```bash
source ~/unitree_ros2/setup_default.sh
```
--------------------------------
### Install ROS2 CycloneDDS Dependencies
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Installs the required ROS2 packages for using CycloneDDS as the middleware implementation and for generating DDS IDL from ROS2 messages. These packages are necessary for the Unitree ROS2 communication.
```bash
sudo apt install ros-foxy-rmw-cyclonedds-cpp
sudo apt install ros-foxy-rosidl-generator-dds-idl
```
--------------------------------
### Source Unitree ROS2 Setup Script
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Sources the main setup script to load the configured environment variables into the current terminal session. This command must be run in every new terminal before executing Unitree ROS2 commands.
```bash
source ~/unitree_ros2/setup.sh
```
--------------------------------
### Compile Unitree ROS2 Packages
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Sources the ROS2 environment setup script and then uses colcon to build all packages within the current workspace. This step compiles the unitree_go and unitree_api packages after CycloneDDS has been built.
```bash
source /opt/ros/foxy/setup.bash
colcon build
```
--------------------------------
### Edit Unitree ROS2 Setup Script
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Opens the main setup script for the Unitree ROS2 environment using gedit for editing. This script contains environment variable configurations that may need modification based on your network setup.
```bash
sudo gedit ~/unitree_ros2/setup.sh
```
--------------------------------
### Comment Out ROS2 Source in .bashrc
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Instructs the user to comment out the line sourcing the ROS2 setup script in the .bashrc file. This is a prerequisite before compiling CycloneDDS to avoid potential conflicts.
```bash
sudo apt install gedit
sudo gedit ~/.bashrc
```
```bash
# source /opt/ros/foxy/setup.bash
```
--------------------------------
### Add Executable Targets
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/example/src/CMakeLists.txt
Defines various executable targets for different Unitree robot examples, specifying their source files.
```CMake
add_executable(low_level_ctrl src/low_level_ctrl.cpp src/common/motor_crc.cpp)
add_executable(low_level_ctrl_hg src/h1-2/lowlevel/low_level_ctrl_hg.cpp src/common/motor_crc_hg.cpp)
add_executable(g1_low_level_example src/g1/lowlevel/g1_low_level_example.cpp src/common/motor_crc_hg.cpp)
add_executable(read_low_state src/read_low_state.cpp)
add_executable(read_low_state_hg src/read_low_state_hg.cpp)
add_executable(read_motion_state src/read_motion_state.cpp)
add_executable(read_wireless_controller src/read_wireless_controller.cpp)
#add_executable(record_bag src/record_bag.cpp)
add_executable(sport_mode_ctrl src/sport_mode_ctrl.cpp src/common/ros2_sport_client.cpp)
```
--------------------------------
### Configure Testing and Linting
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/example/src/CMakeLists.txt
Includes configuration for ament linting and testing if `BUILD_TESTING` is enabled, allowing for code quality checks.
```CMake
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
```
--------------------------------
### Launch RViz2 Visualization Tool Bash
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README _zh.md
Executes the `ros2 run` command to start the RViz2 visualization tool, which is used to display robot state, sensor data, and other information from ROS2 topics.
```bash
ros2 run rviz2 rviz2
```
--------------------------------
### Include and Link Directories
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/example/src/CMakeLists.txt
Specifies directories to be included during compilation and linked during the build process.
```CMake
include_directories(include include/common include/nlohmann)
link_directories(src)
```
--------------------------------
### Clone Unitree ROS2 Repository
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Clones the unitree_ros2 repository from GitHub into the user's home directory. This repository contains the necessary workspaces and packages for Unitree robot ROS2 support.
```bash
git clone https://github.com/unitreerobotics/unitree_ros2
```
--------------------------------
### Find Required Packages
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/example/src/CMakeLists.txt
Locates the necessary ROS2 packages defined in the DEPENDENCY_LIST using `find_package`.
```CMake
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(unitree_go REQUIRED)
find_package(unitree_hg REQUIRED)
find_package(unitree_api REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(rosbag2_cpp REQUIRED)
```
--------------------------------
### Add Target Dependencies
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/example/src/CMakeLists.txt
Links the defined executable targets against the required ROS2 package dependencies using `ament_target_dependencies`.
```CMake
ament_target_dependencies(low_level_ctrl ${DEPENDENCY_LIST})
ament_target_dependencies(low_level_ctrl_hg ${DEPENDENCY_LIST})
ament_target_dependencies(g1_low_level_example ${DEPENDENCY_LIST})
ament_target_dependencies(read_low_state ${DEPENDENCY_LIST})
ament_target_dependencies(read_low_state_hg ${DEPENDENCY_LIST})
ament_target_dependencies(read_motion_state ${DEPENDENCY_LIST})
ament_target_dependencies(read_wireless_controller ${DEPENDENCY_LIST})
#ament_target_dependencies(record_bag ${DEPENDENCY_LIST})
ament_target_dependencies(sport_mode_ctrl ${DEPENDENCY_LIST})
```
--------------------------------
### Compile CycloneDDS for Unitree ROS2
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Navigates to the source directory, clones the specific foxy branch of rmw_cyclonedds and the 0.10.x release branch of cyclonedds, then uses colcon to build only the cyclonedds package.
```bash
cd ~/unitree_ros2/cyclonedds_ws/src
git clone https://github.com/ros2/rmw_cyclonedds -b foxy
git clone https://github.com/eclipse-cyclonedds/cyclonedds -b releases/0.10.x
cd ..
colcon build --packages-select cyclonedds
```
--------------------------------
### Listing ROS 2 Topics (Bash)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Executes the `ros2 topic list` command to display all currently available ROS 2 topics in the system. This is useful for discovering topics to subscribe to or publish data on.
```bash
ros2 topic list
```
--------------------------------
### Add Compile Options
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/example/src/CMakeLists.txt
Adds common compile options (-Wall, -Wextra, -Wpedantic) for GCC and Clang compilers to enable strict warnings.
```CMake
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
```
--------------------------------
### Calling ament_package()
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_api/CMakeLists.txt
Marks the end of the CMake configuration for an ament-based ROS2 package, performing final setup steps.
```CMake
ament_package()
```
--------------------------------
### Set C/C++ Standards
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/example/src/CMakeLists.txt
Configures the C and C++ language standards for the project, defaulting to C99 and C++14 if not already set.
```CMake
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
```
--------------------------------
### Finalize Ament Package
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/example/src/CMakeLists.txt
Marks the end of the CMake configuration for an ament-based ROS2 package.
```CMake
ament_package()
```
--------------------------------
### Identifying ROS 2 Lidar Topic (Bash)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Shows the name of the ROS 2 topic (`utlidar/cloud`) that publishes lidar point cloud data from the Unitree robot. This topic is identified by listing available topics.
```bash
utlidar/cloud
```
--------------------------------
### Define Dependency List
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/example/src/CMakeLists.txt
Defines a CMake list variable containing the names of required ROS2 package dependencies.
```CMake
set (
DEPENDENCY_LIST
unitree_go
unitree_hg
unitree_api
rclcpp
std_msgs
rosbag2_cpp
)
```
--------------------------------
### Finalizing Ament Package Configuration (CMake)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_hg/CMakeLists.txt
Calls the ament_package() macro, which performs final configuration steps for the ROS2 package, such as installing files and setting up package metadata for the ROS2 build system.
```CMake
ament_package()
```
--------------------------------
### Echoing ROS 2 Topic Data (Bash)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Uses the `ros2 topic echo` command with the `--no-arr` flag to display messages published on the `/utlidar/cloud` topic. This helps inspect the data format and content, specifically showing the `frame_id`.
```bash
ros2 topic echo --no-arr /utlidar/cloud
```
--------------------------------
### Marking Package as Ament Compliant (CMake)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_go/CMakeLists.txt
Declares the package as an ament-compliant ROS 2 package using the `ament_package()` macro, which is essential for the ROS 2 build system to correctly recognize, build, and install the package.
```CMake
ament_package()
```
--------------------------------
### Defining Unitree LowCmd Message Structure (C++)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Describes the C++ structure for the `LowCmd` message used to send low-level commands to Unitree robots via the `/lowcmd` topic. It includes fields for header, flags, sequence numbers, bandwidth, motor commands, BMS commands, wireless remote data, LEDs, fans, GPIO, reserve, and CRC.
```C++
uint8[2] head
uint8 level_flag
uint8 frame_reserve
uint32[2] sn
uint32[2] version
uint16 bandwidth
MotorCmd[20] motor_cmd //motor command
BmsCmd bms_cmd
uint8[40] wireless_remote
uint8[12] led
uint8[2] fan
uint8 gpio
uint32 reserve
uint32 crc
```
--------------------------------
### C++ Publish Sportmode Control Request
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Demonstrates how to create a ROS2 publisher for the "/api/sport/request" topic, instantiate a `SportClient` object to generate a specific `unitree_api::msg::Request` message (e.g., for setting Euler angles), and publish the generated request message to control the robot's sport mode behavior. This requires the `rclcpp` and `unitree_api::msg::Request` message type.
```C++
//Create a ros2 pubilsher
rclcpp::Publisher::SharedPtr req_puber = this->create_publisher("/api/sport/request", 10);
SportClient sport_req; //Sportclient
unitree_api::msg::Request req; //Sportmode request msg
sport_req.Euler(req,roll,pitch,yaw); //Get Sportmode request msg from Sportclient
req_puber->publish(req); // Publish request msg
```
--------------------------------
### C++ Low-level State Message Definition
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Defines the structure of the `lowstate` message used to acquire the robot's low-level state via ROS2 topics like "lf/lowstate" or "lowstate". It includes fields for header, flags, serial number, version, bandwidth, IMU state, an array of MotorState messages, BMS state, foot forces (estimated and actual), tick count, wireless remote data, bit flags, ADC readings, temperatures, power voltage/current, fan frequency, reserve fields, and CRC.
```C++
uint8[2] head
uint8 level_flag
uint8 frame_reserve
uint32[2] sn
uint32[2] version
uint16 bandwidth
IMUState imu_state //IMU
MotorState[20] motor_state //Motor state
BmsState bms_state
int16[4] foot_force
int16[4] foot_force_est
uint32 tick
uint8[40] wireless_remote
uint8 bit_flag
float32 adc_reel
int8 temperature_ntc1
int8 temperature_ntc2
float32 power_v
float32 power_a
uint16[4] fan_frequency
uint32 reserve
uint32 crc
```
--------------------------------
### C++ Wireless Controller State Message Definition
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Defines the structure of the `wirelesscontroller` message used to acquire the state of the robot's wireless controller via the "/wirelesscontroller" topic. It includes fields for the x and y axes of the left and right joysticks, and a field for key values.
```C++
float32 lx // left joystick x, range [-1.0~1.0]
float32 ly // left joystick y, range [-1.0~1.0]
float32 rx // right joystick x, range [-1.0~1.0]
float32 ry // right joystick y, range [-1.0~1.0]
uint16 keys // key values
```
--------------------------------
### Defining Unitree MotorCmd Message Structure (C++)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Describes the C++ structure for the `MotorCmd` message, which is part of the `LowCmd` message. It defines parameters for controlling individual motors, including mode, target position, velocity, torque, kp, kd, and reserve fields.
```C++
uint8 mode; //Mode(Foc mode -> 0x01 ,stop mode -> 0x00)
float q; //Target position (rad)
float dq; //Target velocity (rad/s)
float tau; //Target torque (N.M)
float kp;
float kd;
unsigned long reserve[3];
```
--------------------------------
### C++ Motor State Message Definition
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Defines the structure of the `MotorState` message, which is an array element within the `lowstate` message. It includes fields for the motor's mode, joint angle (q), velocity (dq), acceleration (ddq), estimated torque (tau_est), raw data for angle/velocity/acceleration, temperature, lost packet count, and reserve fields.
```C++
uint8 mode // Mode, 0x01 for control
float32 q // Joint angle
float32 dq // Joint velocity
float32 ddq // Joint acceleration
float32 tau_est // Estimated torque
float32 q_raw //raw data of q
float32 dq_raw //raw data of dq
float32 ddq_raw //raw data of dq
int8 temperature
uint32 lost
uint32[2] reserve
```
--------------------------------
### C++ Sportmode State Message Definition
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README.md
Defines the structure of the `sportmodestate` message used to acquire the robot's sport mode state via ROS2 topics like "lf/sportmodestate" or "sportmodestate". It includes fields for time, error, IMU state, current sport mode, action progress, gait type, foot raise height, position, body height, velocity, yaw speed, obstacle range, foot force, and foot positions/velocities in the body frame.
```C++
TimeSpec stamp // Time stamp
uint32 error_code //Error code
IMUState imu_state //IMU state
uint8 mode //Sport mode
/*
Sport mode
0. idle, default stand
1. balanceStand
2. pose
3. locomotion
4. reserve
5. lieDown
6. jointLock
7. damping
8. recoveryStand
9. reserve
10. sit
11. frontFlip
12. frontJump
13. frontPounc
*/
float32 progress //Is the dance action being executed?:0. dance false; 1. dance true
uint8 gait_type //Gait type
/*
Gait type
0.idle
1.trot
2.run
3.climb stair
4.forwardDownStair
9.adjust
*/
float32 foot_raise_height
float32[3] position
float32 body_height
float32[3] velocity
float32 yaw_speed
float32[4] range_obstacle
int16[4] foot_force
float32[12] foot_position_body //foot positions in body frame
float32[12] foot_speed_body //foot velcities in body frame
```
--------------------------------
### Configuring Testing and Linting (CMake)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_go/CMakeLists.txt
Sets up automatic testing and linting using `ament_lint_auto` if the `BUILD_TESTING` CMake option is enabled, allowing for automated code quality checks and test execution within the build process.
```CMake
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
```
--------------------------------
### List ROS2 Topics Bash
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README _zh.md
Executes the `ros2 topic list` command to display all currently available ROS2 topics in the system, useful for discovering data streams like sensor readings or command interfaces.
```bash
ros2 topic list
```
--------------------------------
### Configuring Conditional Testing and Linting
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_api/CMakeLists.txt
Sets up testing and linting using `ament_lint_auto` if the `BUILD_TESTING` option is enabled. It includes commented-out lines to optionally skip copyright and cpplint checks.
```CMake
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
```
--------------------------------
### Setting CMake Project and Standards (CMake)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_go/CMakeLists.txt
Configures the minimum required CMake version, defines the project name, and sets default C (C99) and C++ (C++14) standards if they are not already specified.
```CMake
cmake_minimum_required(VERSION 3.5)
project(unitree_go)
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
```
--------------------------------
### Finding Required ROS 2 Dependencies (CMake)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_go/CMakeLists.txt
Locates necessary ROS 2 packages and libraries required for building the package, including ament_cmake for the build system, geometry_msgs for standard message types, and rosidl generators for interface processing.
```CMake
find_package(ament_cmake REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package( REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(rosidl_generator_dds_idl REQUIRED)
```
--------------------------------
### Finding Required ROS2 Dependencies
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_api/CMakeLists.txt
Locates necessary ROS2 packages required for building the project and generating interfaces, including core build tools, message types, and interface generators.
```CMake
# find dependencies
find_package(ament_cmake REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package( REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(rosidl_generator_dds_idl REQUIRED)
```
--------------------------------
### Generating DDS Interfaces and Adding Dependencies (CMake)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_go/CMakeLists.txt
Generates DDS IDL files from the previously defined ROS 2 interfaces using `rosidl_generate_dds_interfaces` and adds build dependencies to ensure the interface generation targets are built before the main project.
```CMake
rosidl_generate_dds_interfaces(
${rosidl_generate_interfaces_TARGET}__dds_connext_idl
IDL_TUPLES ${rosidl_generate_interfaces_IDL_TUPLES}
OUTPUT_SUBFOLDERS "dds_connext"
)
add_dependencies(
${PROJECT_NAME}
${PROJECT_NAME}__dds_connext_idl
)
```
--------------------------------
### Configuring Ament Testing and Linting (CMake)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_hg/CMakeLists.txt
Includes configuration for ament_lint_auto to automatically find and run linters and tests if BUILD_TESTING is enabled. It also shows commented-out options to skip specific linters like copyright or cpplint.
```CMake
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
```
--------------------------------
### Setting CMake Standards and Compile Options (CMake)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_hg/CMakeLists.txt
Configures the minimum required CMake version, project name, default C/C++ standards (C99/C++14), and adds common compile options (-Wall, -Wextra, -Wpedantic) for GCC/Clang compilers.
```CMake
cmake_minimum_required(VERSION 3.5)
project(unitree_hg)
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
```
--------------------------------
### Adding Compiler Warning Flags (CMake)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_go/CMakeLists.txt
Adds common compiler warning flags (-Wall, -Wextra, -Wpedantic) for GCC and Clang compilers to encourage better code quality and catch potential issues.
```CMake
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
```
--------------------------------
### Generating DDS Interfaces
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_api/CMakeLists.txt
Generates DDS (Data Distribution Service) specific interface files (like IDL) from the ROS2 interfaces defined previously, specifically targeting Connext DDS.
```CMake
rosidl_generate_dds_interfaces(
${rosidl_generate_interfaces_TARGET}__dds_connext_idl
IDL_TUPLES ${rosidl_generate_interfaces_IDL_TUPLES}
OUTPUT_SUBFOLDERS "dds_connext"
)
```
--------------------------------
### Adding Target Dependencies
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_api/CMakeLists.txt
Ensures that the main project target depends on the generated DDS interface target, guaranteeing that the DDS files are generated before the main project is built.
```CMake
add_dependencies(
${PROJECT_NAME}
${PROJECT_NAME}__dds_connext_idl
)
```
--------------------------------
### Setting C/C++ Standard Versions
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_api/CMakeLists.txt
Sets the default C standard to C99 and the default C++ standard to C++14 if they are not already defined.
```CMake
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
```
--------------------------------
### Generating ROS 2 Message Interfaces (CMake)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_go/CMakeLists.txt
Uses the `rosidl_generate_interfaces` command to process a list of custom .msg files, generating source code for these message types in various languages supported by ROS 2. It specifies a dependency on `geometry_msgs`.
```CMake
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/AudioData.msg"
"msg/BmsCmd.msg"
"msg/BmsState.msg"
"msg/Error.msg"
"msg/Go2FrontVideoData.msg"
"msg/HeightMap.msg"
"msg/IMUState.msg"
"msg/InterfaceConfig.msg"
"msg/LidarState.msg"
"msg/LowCmd.msg"
"msg/LowState.msg"
"msg/MotorCmd.msg"
"msg/MotorCmds.msg"
"msg/MotorState.msg"
"msg/MotorStates.msg"
"msg/PathPoint.msg"
"msg/Req.msg"
"msg/Res.msg"
"msg/SportModeCmd.msg"
"msg/SportModeState.msg"
"msg/TimeSpec.msg"
"msg/UwbState.msg"
"msg/UwbSwitch.msg"
"msg/WirelessController.msg"
DEPENDENCIES geometry_msgs
)
```
--------------------------------
### Adding Compiler Options for GCC/Clang
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_api/CMakeLists.txt
Adds common warning flags (-Wall, -Wextra, -Wpedantic) when using GCC or Clang compilers to encourage robust code.
```CMake
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
```
--------------------------------
### Setting Minimum CMake Version and Project Name
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_api/CMakeLists.txt
Specifies the minimum required version of CMake to build the project and declares the project name.
```CMake
cmake_minimum_required(VERSION 3.5)
project(unitree_api)
```
--------------------------------
### Finding Required ROS2 Dependencies (CMake)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_hg/CMakeLists.txt
Locates necessary ROS2 packages and libraries using find_package. This includes core build tools (ament_cmake), message types (geometry_msgs), and interface generation tools (rosidl_default_generators, rosidl_generator_dds_idl).
```CMake
find_package(ament_cmake REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package( REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(rosidl_generator_dds_idl REQUIRED)
```
--------------------------------
### Echo ROS2 Topic Data Bash
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README _zh.md
Uses the `ros2 topic echo` command to display messages published on a specific ROS2 topic (`/utlidar/cloud` in this case), with the `--no-arr` flag to prevent printing array contents, useful for inspecting message structure and data like `frame_id`.
```bash
ros2 topic echo --no-arr /utlidar/cloud
```
--------------------------------
### Generating ROS2 Message Interfaces
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_api/CMakeLists.txt
Uses the `rosidl_generate_interfaces` command to process custom message definitions (.msg files) and generate source code for them in various languages, depending on the generators found. It specifies a dependency on `geometry_msgs`.
```CMake
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/Request.msg"
"msg/RequestHeader.msg"
"msg/RequestIdentity.msg"
"msg/RequestLease.msg"
"msg/RequestPolicy.msg"
"msg/Response.msg"
"msg/ResponseHeader.msg"
"msg/ResponseStatus.msg"
DEPENDENCIES geometry_msgs
)
```
--------------------------------
### Adding Build Dependencies for Generated Interfaces (CMake)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_hg/CMakeLists.txt
Establishes build dependencies between the main project target and the generated DDS interface target. This ensures that the DDS interfaces are built before the main project target that might depend on them.
```CMake
add_dependencies(
${PROJECT_NAME}
${PROJECT_NAME}__dds_connext_idl
)
```
--------------------------------
### Generating DDS Interfaces for ROS2 Messages (CMake)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_hg/CMakeLists.txt
Generates DDS (Data Distribution Service) specific interface files, typically IDL, from the ROS2 messages defined previously. This step is necessary for the DDS middleware used by ROS2.
```CMake
rosidl_generate_dds_interfaces(
${rosidl_generate_interfaces_TARGET}__dds_connext_idl
IDL_TUPLES ${rosidl_generate_interfaces_IDL_TUPLES}
OUTPUT_SUBFOLDERS "dds_connext"
)
```
--------------------------------
### Define LowCmd Message Structure C++
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README _zh.md
Defines the structure for the `LowCmd` ROS2 message used to send low-level control commands to the robot. It includes fields for motor commands, BMS status, wireless remote data, LEDs, fans, and GPIO.
```C++
uint8[2] head
uint8 level_flag
uint8 frame_reserve
uint32[2] sn
uint32[2] version
uint16 bandwidth
MotorCmd[20] motor_cmd //电机指令
BmsCmd bms_cmd
uint8[40] wireless_remote
uint8[12] led
uint8[2] fan
uint8 gpio
uint32 reserve
uint32 crc
```
--------------------------------
### Generating Custom ROS2 Messages (CMake)
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/cyclonedds_ws/src/unitree/unitree_hg/CMakeLists.txt
Uses rosidl_generate_interfaces to process custom message definitions (.msg files) listed in the command. It specifies geometry_msgs as a dependency required for these messages.
```CMake
rosidl_generate_interfaces(${PROJECT_NAME}
"msg/BmsCmd.msg"
"msg/BmsState.msg"
"msg/HandCmd.msg"
"msg/HandState.msg"
"msg/IMUState.msg"
"msg/LowCmd.msg"
"msg/LowState.msg"
"msg/MainBoardState.msg"
"msg/MotorCmd.msg"
"msg/MotorState.msg"
"msg/PressSensorState.msg"
DEPENDENCIES geometry_msgs
)
```
--------------------------------
### Define MotorCmd Message Structure C++
Source: https://github.com/unitreerobotics/unitree_ros2/blob/master/README _zh.md
Defines the structure for the `MotorCmd` message, which is part of the `LowCmd` message. It specifies parameters for controlling individual motors, including mode, target position, velocity, torque, stiffness (kp), and damping (kd).
```C++
uint8 mode; //电机控制模式(Foc模式(工作模式)-> 0x01 ,stop模式(待机模式)-> 0x00
float q; //关节目标位置
float dq; //关节目标速度
float tau; //关节目标力矩
float kp; //关节刚度系数
float kd; //关节阻尼系数
unsigned long reserve[3]; //保留位
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.