第 2 節

Getting Started

0瀏覽次數0訪問次數--跳出率--平均停留

Introduction

Terminal Environment Setup

If you already added this statement to ~/.bashrc when installing ROS2 above, you don't need to follow this step.

source /opt/ros/humble/setup.bash  #将ROS2环境变量配置到当前位置
echo " source /opt/ros/humble/setup.bash" >> ~/.bashrc    #每次启动终端都会运行该句

ros2 run demo_nodes_cpp talker

Use Ctrl+C to cancel program execution.

ros2 run demo_nodes_py listener

ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key

Command line operations

mkdir -p create a new folder

rm -R recursively deletes (removes a folder along with all folders and files it contains).

touch creates a new file

rm deletes files

cd .. goes back to the parent directory (cd dot dot).


It will pop up a prompt message telling us the parameters to follow.

The ros2 node list command will list the nodes currently running in ROS 2.

ros2 node info + /node_name can be used to view the details of the target node.

It will pop up a prompt message telling us the parameters to follow.

The status of the robot's movement can be displayed through topics.

ros2 topic pub --rate 1 /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"

ros2 service call /spawn turtlesim/srv/Spawn "{x: 2, y: 2, theta: 0.2, name: ''}"

ros2 bag record /turtle1/cmd_vel
ros2 bag play rosbag2_2022_04_11-17_35_40/rosbag2_2022_04_11-17_35_40_0.db3

ros2 bag record + topic

Press Ctrl+C to stop, and the recorded data will be saved in the current terminal's directory.

How can I reproduce it?

ros2 bag play + folder name

ROS2 HelloWorld(C++)

  1. Create a function package

The instruction is to create a ROS2 package.

ros2 pkg create + package name + --build-type (build type) + ament_cmake / ament_python + --dependencies (dependencies) + rclcpp (ROS2 C++ client) + --node-name (node name) + node name

ros2 pkg create pkg01_helloworld_cpp --build-type ament_cmake --dependencies rclcpp --node-name helloworld

The source file was automatically generated, and the filename matches the node name we specified.

This is auto-generated content, but it has nothing to do with ROS2.

If there are more dependent libraries than just this one, press Enter again.

xxx

Add the next one.

10 lines is to find the package

Line 12 adds executable permissions.

The first argument of add_executable is the name of the executable (by default, it matches the node name and the source file name). The second argument is the name of the source file.

Line 17 adds a dependency for our executable — it depends on the RCLCPP library.

Line 22 is to set up an installation directory for our executable program, created under the lib directory of the current package, which is workspace_name/install/package_name/lib.

After editing the configuration file, compile and use cd.. to return to the ws directory.

The icon is green, indicating no warnings or errors.

If it is yellow, there is a warning.

If it is red, there is a fatal error.

Path to the executable binary file

source install/setup.bash #刷新环境变量

ros2 run pkg01_helloworld_cpp helloworld

ros2 run package_name executable_name (default matches the node name)

Edit the ROS2 C++ source file:

#include "rclcpp/rclcpp.hpp"

int main(int argc, char ** argv)
{
  rclcpp::init(argc,argv);

  auto node = rclcpp::Node::make_shared("helloworld_node");

  RCLCPP_INFO(node->get_logger(),"hello world!");

  rclcpp::shutdown();

  return 0;
}

ROS2 HelloWorld(Python)

ros2 pkg create pkg02_helloworld_py --build-type ament_python --dependencies rclpy --node-name helloworld

ros2 pkg create + package name + --build-type (build type) + ament_cmake / ament_python + --dependencies (dependencies) + rclpy (ROS2 Python client) + --node-name (node name) + node name

Same as the node name and the executable binary file.

By default, there is already code in here, but it has nothing to do with ROS2 — this is standard Python code.

The binary executable file maps to the main function in the source file.

How do I compile it?

First, go back to the parent directory to reach the ws directory.

There's a yellow warning, but it doesn't affect our usage.

source ./install/setup.bash

Refresh environment variables

ros2 run pkg02_helloworld_py helloworld


import rclpy

def main():
    rclpy.init()
    node = rclpy.create_node("helloworld_py_node")
    node.get_logger().info("hello world by python!")
    rclpy.shutdown()

if name == '__main__':
    main()

Runtime Optimization (Bash Terminal Environment)

To use an absolute path

Try to avoid doing this. ROS2 has a bug where function packages from different workspaces may get mixed up, so don't enable global runtime optimization for now.

VScode environment setup

Looking at C/C++, Python, CMake, XML, and YAML files, code syntax highlighting will be displayed.

When writing ROS2 message code, it can provide features like code completion.

Write the plugins needed for the robot model, and also enable code completion.

ROS2 frequently generates PDF files, and this plugin can be used to view them.

It is recommended to install ROS2 plugins only after they have matured.

You can try installing this official plugin.

AI code completion

MarkDown highlighting


Although an error is reported, the program can still run normally. (The main issue is that VSCode cannot find the header file.)

            "includePath": [
                "${default}",
                "${workspaceFolder}/**",
                "/opt/ros/humble/include/**"
            ],

/** represents including all subsets under this folder.


Press Ctrl + ` (the key below ESC) to open the VS Code terminal.

The --node-name parameter is also optional. If not configured, there will be no source file and no mapping from the executable file to the source file.

No changes needed; it has already been generated by default.

Return to the WS directory to compile (but at this point, the compilation will build all the packages under the entire WS directory).

Refresh the environment variables and run.



How do I add multiple source files to a feature package?

Create a new file, for example hellovscode2.cpp.

However, at this point the file is isolated—it has no configuration, and accordingly, it will not be executed after compilation.

To compile and execute this file, we must configure the relevant configuration files.

You don't need the selected ones; you can delete them.

Build Optimization (colcon build)

I usually do a full build of the files in the WS directory.

colcon build --packages-select xxx xxx xxx #可以指向多个包

Advanced VScode Environment Setup

https://colcon.readthedocs.io/en/released/index.html

Since the C/C++ plugin performs terribly in large projects, we choose to use the clangd plugin from LLVM for code suggestions.

However, clangd relies on CMake to generate a compilation information file, so we need a few steps to generate that file.

Since ROS2 does not have a single standardized CMakeLists like ROS1, configuring it is not as convenient as ROS1.

  1. Configure colcon build parameters
    1. Method 1: (Not recommended) Use this command every time you compile:
    colcon build --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
    

Equivalent to writing it in the CMake file (generally not recommended to modify CMakeLists).

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
```
5.  Method 2: Global Parameters (Recommended)

```bash
mkdir ~/.colcon

vim ~/.colcon/defaults.yaml

Press the insert(插入) button.

1.1 Microcontroller Environment Setup

1.1.1 Microcontroller Development Environment Setup

1.1.1.1 STM32 Development Environment Setup

1.1.1.1.1 Installing Keil MDK

Keil MDK is a microcontroller development tool launched by ARM, which is currently the most widely used development environment for STM32 microcontrollers. It integrates with the STM32CubeMX code generation tool to quickly build STM32 microcontroller projects.

build:
  cmake-args:
    - -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

Press ESC, then press :wq, and finally press Enter(回车) to save successfully.

When compiling, using colcon build normally will automatically enable the CMAKE_EXPORT_COMPILE_COMMANDS=ON parameter.

  1. Then configure the clangd plugin.
    1. First, download the clangd plugin.
    2. Download the clangd file

Hold down Ctrl+Shift+P to open the search box.

Enter clangd, find the "Download Language Server" option, and click to install clangd (please ensure a stable internet connection).

![](https://cdn.tungchiahui.cn/tungwebsite/assets/images/2023/12/30/image208.webp)

6.  Next, configure clangd:

Disable C/C++ code suggestion features.

If the pop-up shown above does not appear, you can manually close it. Again, press Ctrl+Shift+P, type "settings", and then find the option shown in the image below.

Find the option shown in the image below and change it to disabled.

"C_Cpp.intelliSenseEngine": "disabled"

  1. Restart clangd

Then press Ctrl+Shift+P, search for "clangd", and find the option shown in the image below (before restarting the clangd language server, you need to run colcon build first).

The code hints are working normally now.

Install other tools

terminal

We choose to install Terminator (optional, depends on personal preference—there's an even better one called Warp, see below).
sudo apt install terminator

Shortcut keysFunctionMemory techniques
Ctrl + Shift + EVertical split screen, left and right splitE = East, open a window to the east, meaning left-right split.
Ctrl + Shift + OHorizontal split screen, top and bottom splitO = Over / Under, stacked vertically
Ctrl + Shift + SHide/Show scrollbarS = Scrollbar
F11Full screen / Exit full screenF = Fullscreen — F11 has always been the fullscreen key for many applications.
Ctrl + TabSwitch between different panesSimilar to switching tabs in a browser, it cycles through them.
Ctrl + LClear screenL in "clear L" can also be understood as "pull to a new page."
Ctrl + Shift + WClose the current pane.W = Window, closes the current small window.
Ctrl + Shift + QExit the entire Terminator.Q = Quit, exits the program.

Because its font is very dark, you need to adjust the settings.

cp ~/.config/terminator/config ~/.config/terminator/config.bak
vim ~/.config/terminator/config

Press ggdG, note the case. Then press the insert button. Then set the file content to:

[global_config]
  title_transmit_bg_color = "#31363b"
  title_transmit_fg_color = "#fcfcfc"
  title_receive_bg_color = "#232629"
  title_receive_fg_color = "#fcfcfc"
  title_inactive_bg_color = "#232629"
  title_inactive_fg_color = "#bdc3c7"

[keybindings]

[profiles]
  [[default]]
    use_theme_colors = False
    use_system_font = False
    font = Noto Sans Mono 11

    background_type = solid
    background_color = "#232629"
    foreground_color = "#fcfcfc"
    cursor_color = "#fcfcfc"

    palette = "#232629:#ed1515:#11d116:#f67400:#1d99f3:#9b59b6:#1abc9c:#fcfcfc:#7f8c8d:#ff5555:#50fa7b:#fdbc4b:#3daee9:#ff79c6:#8be9fd:#ffffff"

    scrollback_infinite = True

[layouts]
  [[default]]
    [[[window0]]]
      type = Window
      parent = ""
      size = 920, 660
    [[[child1]]]
      type = Terminal
      parent = window0

[plugins]

Finally, press ESC, type :wq to save and exit.

Then completely close Terminator, and reopen it. This configuration will do several things:

  1. Background: a deep gray close to Konsole Breeze, not pure black.
  2. Font color: nearly pure white, much brighter than before.
  3. Blue: Switch to a brighter KDE blue
  4. Green/yellow/cyan: also overall brightening.
  5. Top red title bar: change to gray, no longer so glaring.

Compared to Terminator, Warp Terminal's default appearance is more modern, the font is clearer, and the interface is brighter.

Most importantly, it also supports multi-pane split screens like Terminator, so it can serve as a modern replacement for Terminator.

However, note that Warp's keyboard shortcuts are not exactly the same as Terminator's.

  1. Install
    1. Ubuntu / Debian installation
      wget -O warp-terminal.deb "https://app.warp.dev/download?package=deb"
      sudo apt install ./warp-terminal.deb
      

After installation is complete, you can open Warp from the application menu, or type in the terminal:

warp-terminal
```

2. Fedora / RHEL Installation

If you are using distributions like Fedora, RHEL, or CentOS, you can install the rpm package:

```bash
wget -O warp-terminal.rpm "https://app.warp.dev/download?package=rpm"
sudo dnf install ./warp-terminal.rpm
```

The startup method is the same:

```bash
warp-terminal
```



2. Warp Common Keyboard Shortcuts

|Shortcut keys|Function|Memory techniques|
|---|---|---|
|`Ctrl + Shift + D`|Split screen to the right|**D = Divide**, splits out a new pane, default opens to the right.|
|`Ctrl + Shift + E`|Split Downwards|**E = Extend Down**, extend a new pane downward|
|`Ctrl + Alt + 方向键`|Switch pane by direction|Press the direction key corresponding to where you want to go.|
|`Ctrl + Shift + [`|Switch to the previous pane|`[` can be understood as cutting forward.|
|`Ctrl + Shift + ]`|Switch to the next pane.|`]` can be understood as cutting backwards.|
|`Ctrl + Shift + Enter`|Maximize/restore current pane|Enter to enter the focus mode of the current pane|
|`Ctrl + Shift + W`|Close the current pane.|**W = Window**, close the current small window.|
|`Ctrl + Shift + T`|New tab|**T = Tab**, opens a new tab.|
|`Ctrl + PageUp`|Switch to the previous tab.|Similar to switching browser tabs|
|`Ctrl + PageDown`|Switch to the next tab.|Similar to switching browser tabs|
|`Ctrl + L`|Clear screen|**L in L = clear**, equivalent to navigating to a new page.|
|`Ctrl + =`|Increase font size|Next to the equals sign, there is usually also a plus sign, indicating enlargement.|
|`Ctrl + -`|Reduce font size|Minus sign, indicates zoom out.|
|`Ctrl + 0`|Restore default font size|Reset to default|



3. Features of Warp
  1. By default, it looks better than Terminator, so no need to bother with color schemes.
  2. Supports persistent split screen, suitable for large screen development.
  3. The font rendering is quite clear, and it looks brighter than Terminator.
  4. Supports command blocks, long command outputs appear clearer.
  5. Supports AI features, but can also be used as a normal terminal without AI.

When opening Warp for the first time, it may require an internet connection for initialization. After that, it can be used as a regular terminal even offline, though cloud-based features like AI and collaboration will not be available.

If you just want a traditional, stable, lightweight terminal, you can continue to use Terminator.

If you want a better-looking interface, a more modern interactive experience, and also need split-screen, then Warp is a great choice.

git

Our Git introductory tutorial: Vinci Robotics Team Git Introductory Tutorial

ROS2 Architecture Framework

Client Library refers to the ROS2 client libraries, such as rclcpp and rclpy.

The Abstract DDS Layer is a DDS abstraction layer that makes DDS pluggable, allowing the DDS module to be easily swapped out.

Intra-process APIs are in-process communication APIs, a type of API that can improve communication efficiency.

音乐页