<style>
.red {color: red;}
</style>
<style>
.sol {color: green; font-weight: bold;}
</style>
# 台達機械手臂IMP3整合ROS2
###### tags: `vsclab`
:arrow_backward: 回到 [**2023 VSCLab 温梓傑 交接資料**](https://hackmd.io/fH9acJ0lTbKa5WOxYPA9UA)
:::spoiler Outline
[toc]
:::
# Git Config
```
user.email=n26104256@gs.ncku.edu.tw
user.name=112-VSCLAB.Wun
```
# Installation
* [安裝 Win10 ROS2-humble](https://hackmd.io/1-3Kw98CSSirML59XAK2MA#Win10)
# Practices of [Official Docs](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries.html)
:::spoiler (2022/9/26) [Using colcon to build packages](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Colcon-Tutorial.html#create-a-workspace)
:::danger
**[colcon build Error]**: [this step](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Colcon-Tutorial.html#build-the-workspace)
```bash
# Input
colcon build --symlink-install --merge-install
# Output
ROS2 error: can‘t find examples_rclcpp_minimal_subscriber/Release/wait_set_subscriber
```
* [Github issue](https://github.com/ros2/examples/issues/344)
* [Solution](https://www.reddit.com/r/ROS/comments/wxkfes/colcon_build_failed_in_example_failed_examples/)
修改 src\examples\rclcpp\topics\minimal_subscriber\CMakeLists.txt
:::
:::spoiler (2022/9/27) [Creating a workspace](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Creating-A-Workspace/Creating-A-Workspace.html#creating-a-workspace)
:::danger
**[colcon build error]**
```
C:\dev\ros2_ws>colcon build --merge-install --packages-select my_package
[1.125s] colcon.colcon_core.package_identification WARNING Failed to parse ROS package manifest in 'src\my_package': Error(s) in package 'src\my_package\package.xml':
Invalid email "Delta Robot@todo.todo" for person "Delta Robot"
Starting >>> my_package
--- stderr: my_package
Error parsing 'C:/dev/ros2_ws/src/my_package/package.xml':
Traceback (most recent call last):
File "C:/dev/ros2_humble/ros2-windows/share/ament_cmake_core/cmake/core/package_xml_2_cmake.py", line 150, in <module>
main()
File "C:/dev/ros2_humble/ros2-windows/share/ament_cmake_core/cmake/core/package_xml_2_cmake.py", line 53, in main
raise e
File "C:/dev/ros2_humble/ros2-windows/share/ament_cmake_core/cmake/core/package_xml_2_cmake.py", line 49, in main
package = parse_package_string(
File "C:\Python38\lib\site-packages\catkin_pkg\package.py", line 788, in parse_package_string
pkg.validate(warnings=warnings)
File "C:\Python38\lib\site-packages\catkin_pkg\package.py", line 330, in validate
raise InvalidPackage('\n'.join(errors), self.filename)
catkin_pkg.package.InvalidPackage: Error(s) in package 'C:/dev/ros2_ws/src/my_package/package.xml':
Invalid email "Delta Robot@todo.todo" for person "Delta Robot"
CMake Error at C:/dev/ros2_humble/ros2-windows/share/ament_cmake_core/cmake/core/ament_package_xml.cmake:95 (message):
execute_process(C:/Python38/python.exe
C:/dev/ros2_humble/ros2-windows/share/ament_cmake_core/cmake/core/package_xml_2_cmake.py
C:/dev/ros2_ws/src/my_package/package.xml
C:/dev/ros2_ws/build/my_package/ament_cmake_core/package.cmake) returned
error code 1
Call Stack (most recent call first):
C:/dev/ros2_humble/ros2-windows/share/ament_cmake_core/cmake/core/ament_package_xml.cmake:49 (_ament_package_xml)
C:/dev/ros2_humble/ros2-windows/share/ament_lint_auto/cmake/ament_lint_auto_find_test_dependencies.cmake:31 (ament_package_xml)
CMakeLists.txt:32 (ament_lint_auto_find_test_dependencies)
---
Failed <<< my_package [0.72s, exited with code 1]
Summary: 0 packages finished [1.16s]
1 package failed: my_package
1 package had stderr output: my_package
```
* Solution:
`C:/dev/ros2_ws/src/my_package/package.xml`改成有效 email
:::
:::spoiler (2022/9/27) [Writing a simple publisher and subscriber (C++)](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.html#writing-a-simple-publisher-and-subscriber-c)
:::warning
**[Visual Studio Code IncludePath Issue with ROS headers]**

* [Solution](https://blog.csdn.net/Epoch_saber/article/details/121684018)
```
# .vscode/c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/dev/ros2_humble/ros2-windows/include/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
```
:::
:::spoiler (Skip) [Writing a simple publisher and subscriber (Python)](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Py-Publisher-And-Subscriber.html#writing-a-simple-publisher-and-subscriber-python)
:::
:::spoiler (2022/9/28) [Writing a simple service and client (C++)](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Service-And-Client.html#writing-a-simple-service-and-client-c)
:::
:::spoiler (Skip) [Writing a simple service and client (Python)](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Py-Service-And-Client.html#writing-a-simple-service-and-client-python)
:::
---
## Integrate IMP3 code with ROS2 packages
#### Create workspace and package
```bash
# Create a workspace
mkdir delta_ros2_ws\src
cd delta_ros2_ws\src
# Create a ros2 package named "delta" and dependencies
ros2 pkg create --build-type ament_cmake delta --dependencies rclcpp std_msgs example_interfaces
```
#### Modify `src/delta/package.xml`
1. Update the package descriptioin
2. Update your valid email
3. Update license
```xml=6
<description>Delta Robot with ROS2</description>
<maintainer email="vsclab.youbot@gmail.com">Delta Robot</maintainer>
<license>Apache License 2.0</license>
```
#### Copy IMP3 code
1. Header Files (.h/.hpp): `delta_ros2_ws/src/delta/include/delta`
2. Source Files (.cpp): `delta_ros2_ws/src/delta/src`
---
# Troubleshoots
## Colcon build **in Win10**
### Settings of colcon and debug
#### [o] No error message output while building
* Solution: [](https://github.com/colcon/colcon-cmake/issues/67#issuecomment-1104434793)
```
colcon build --event-handlers console_direct+
```
#### [o] MVC output garbled message
* Solution: Switch Win10 Language preferences to "English, United States"
#### [o] Annoying notifications after build success/failure
* Solution: [Set Environment Variable](https://answers.ros.org/question/356880/how-to-disable-desktop-notifications-for-colcon/?answer=356881#post-id-356881)
```
$ set COLCON_EXTENSION_BLOCKLIST=colcon_core.event_handler.desktop_notification
```
### CMake issues
#### [o] Include custom header files in ROS2 package
* Solution: [修改 src/[package_name]/CMakeList.txt](https://answers.ros.org/question/386462/including-custom-header-file-in-ros2-cmakelist/?answer=386715#post-id-386715)
```cmake=23
# Custom Lib
include_directories(include) # Find the include directories
add_library(savedata src/SaveData.cpp) # add library definition
target_link_libraries(main savedata) # Link to the executable ("main")
```
* :::spoiler References
1. [CMake Tutorial](https://youtu.be/PLhJwDqzPU0)
:::
#### [x] Include static/dynamic library
* [Solution](https://stackoverflow.com/a/14078232/11781780)
```cmake=36
set(IMC_LIB_PATH "C:/Program Files (x86)/ITRI/IMP3/IDDL/Library/VC/IMC3Driver.lib") # At Program Files(x86)
target_link_libraries(main ${IMC_LIB_PATH})
```
* :::spoiler References
1. [Simplified CMake Tutorial](https://youtu.be/mKZ-i-UfGgQ)
2. [Creating and Linking Static Libraries](https://youtu.be/t5TfYRRHG04)
2. [Creating and Linking Shared Libraries](https://youtu.be/mUbWcxSb4fw)
#### [o] Build custom library with ros dependencies
```cmake
add_library(motioncard src/delta/MotionCard.cpp) # Compile "MotionCard.cpp"
ament_target_dependencies(motioncard rclcpp std_msgs example_interfaces)
target_include_directories(motioncard
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
```
* :::spoiler References
1. [ament_cmake user documentation - Adding files and headers](http://docs.ros.org/en/humble/How-To-Guides/Ament-CMake-Documentation.html#adding-files-and-headers)
:::
#### [o] Compiler Warning (level 1) C4819
* [Solution](https://www.cnblogs.com/Steven-HU/p/14547749.html)
> 1. 打开 Control Panel
> 2. 选择 Region (and Language)
> 3. 选择 Administrative 页签
> 4. 在 Language for non-Unicode programs 区域点击 "Change System Locale" 按钮
> 5. 设置 Current system locale,比如 English (United States)。
> 6. 点击 "OK" 保存。
:::spoiler
* [探究Visual Studio中的乱码问题](https://blog.csdn.net/no_say_you_know/article/details/126695461?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EAD_ESQUERY%7Eyljh-1-126695461-blog-121911771.pc_relevant_3mothn_strategy_and_data_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EAD_ESQUERY%7Eyljh-1-126695461-blog-121911771.pc_relevant_3mothn_strategy_and_data_recovery&utm_relevant_index=2)
* [MSVC中C++ UTF8中文编码处理探究](https://www.cnblogs.com/Esfog/p/MSVC_UTF8_CHARSET_HANDLE.html)
:::
# Program Descriptions
```
```
# 開發過程
## Joint State Message
* [sensor_msgs/JointState Documentation](http://docs.ros.org/en/noetic/api/sensor_msgs/html/msg/JointState.html)
* [ROS URDF(五): sensor_msgs::JointState 消息类型的赋值](https://blog.csdn.net/bbtang5568/article/details/108613582)
* [ROS2 時間格式](https://answers.ros.org/question/287946/ros-2-time-handling/?answer=287949#post-id-287949)
# Some Tricks
* [Win10 Command Line alias](https://superuser.com/a/560583)
# 台達機械手臂緊急停機SOP
:::info
:information_source: **先備知識**
| 命令 | 行為 | 備註 |
| ------------ | -------------- | -------- |
| Servo ON | 馬達 **鬆** 煞車 | |
| Servo OFF | 馬達 **拉** 煞車 | |
* 控制箱總電源上電時,會先進行 Servo OFF (煞車) 動作
:::
```mermaid
graph TD;
緊急停機旋鈕壓下-->關閉控制箱總電源-->執行清命令程式-->開啟控制箱總電源-->旋開緊急開關-->按下藍色RESET鈕-->一人先扶著手臂,另一人再按下ServoON-->手臂復位-->ServoOFF
```
# 在兩台虛擬機之間用ROS2溝通
> :key: ros2 communication between two virtual machines
```bash
# Step 1: 確認兩台VM的IP位址
$ ip a # Ubuntu 20.04 up
$ ipconfig # Windows
# Step 2: 互相ping對方
$ ping 192.168.aaa.aaa -c 3 # Ubuntu ([-c] 代表ping的次數)
$ ping 192.168.bbb.bbb # Windows
# Step 3: 設定相同號碼的$ROS_DOMAIN_ID
$ export ROS_DOMAIN_ID=5 # 設定Ubuntu環境變數
$ echo $ROS_DOMAIN_ID # 查看Ubuntu環境變數
> 5
$ set ROS_DOMAIN_ID=5 # 設定Windows環境變數
$ echo %ROS_DOMAIN_ID% # 查看Windows環境變數
> 5
```
:::spoiler 參考教學
* [ROS2 Multiple Machines Tutorial (including Raspberry Pi)](https://roboticsbackend.com/ros2-multiple-machines-including-raspberry-pi/)
* [Concepts / About-Domain-ID - ROS2 Official Tutorials](https://docs.ros.org/en/humble/Concepts/About-Domain-ID.html)
* [使用命令列設定Windows環境變數](https://shaochien.gitbooks.io/command-line-and-environment-variable-tutorial/content/environment-variable.html)
:::