ROS2_IGH方案配置(三)——VScode编译环境通讯配置

启动EtherCAT环境

接(二)配置完成环境,准备进行电机驱动测试
参考B站视频晴糖豆的电机转动视频:BV1kP4y1Y7QK
ctrl+alt+T打开终端
sudo /etc/init.d/ethercat start
每次重启后需操作一次,
随后查看ethercat从站连接:
ethercat slaves
查看
在这里插入图片描述打印
ethercat cstruct -p 0 -a 0

/* Master 0, Slave 0, “2HCS558-EC”

  • Vendor ID: 0x66668888
  • Product code: 0x20201312
  • Revision number: 0x20200720
    /
    ec_pdo_entry_info_t slave_0_pdo_entries[] = {
    {0x6040, 0x00, 16}, /
    controlword /
    {0x6060, 0x00, 8}, /
    op_mode /
    {0x607a, 0x00, 32}, /
    target_position /
    {0x60b8, 0x00, 16}, /
    Touch_probe_function /
    {0x60fe, 0x01, 32}, /
    physical_outputs /
    {0x60fe, 0x02, 32}, /
    bit_mask /
    {0x603f, 0x00, 16}, /
    errorcode /
    {0x6041, 0x00, 16}, /
    statusword /
    {0x6061, 0x00, 8}, /
    op_mode_display /
    {0x6064, 0x00, 32}, /
    actual_position /
    {0x60b9, 0x00, 16}, /
    Touch_probe_status /
    {0x60ba, 0x00, 32}, /
    Touch_probe_pos1_pos_value /
    {0x60bb, 0x00, 32}, /
    Touch_probe_pos1_neg_value /
    {0x60bc, 0x00, 32}, /
    Touch_probe_pos2_pos_value /
    {0x60bd, 0x00, 32}, /
    Touch_probe_pos2_neg_value /
    {0x60fd, 0x00, 32}, /
    digtal_inputs /
    };
    ec_pdo_info_t slave_0_pdos[] = {
    {0x1600, 6, slave_0_pdo_entries + 0}, /
    RxPdoMapping /
    {0x1a00, 10, slave_0_pdo_entries + 6}, /
    TxPdoMapping */
    };
    ec_sync_info_t slave_0_syncs[] = {
    {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
    {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
    {2, EC_DIR_OUTPUT, 1, slave_0_pdos + 0, EC_WD_ENABLE},
    {3, EC_DIR_INPUT, 1, slave_0_pdos + 1, EC_WD_DISABLE},
    {0xff}
    };

注意,有几个从站就放几个从站的命令到文件中,需要放在main之前,以使变量全局可用

新建工作目录Etherdemos
新建CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
project(test)
set(CMAKE_BUILD_TYPE “Debug”)
include_directories(/opt/etherlab/include)
link_directories(/opt/etherlab/lib)
add_executable(excutable main.c)
target_link_libraries(excutable ethercat pthread)

这里晴糖豆将编译执行文件命名为excutable,此处可以自定义,我选择跟随大佬一起走。

注:pthread全称为POSIX Threads库,它提供了一套用于多线程编程的接口。它是一种线程模型的实现,允许程序在多个并发执行的线程中运行。
POSIX是Portable Operating System Interface的缩写,这个接口定义了Unix-like操作系统的标准。pthread库是基于POSIX标准的多线程库,提供了一系列函数和数据类型,使程序员可以直接调用这些函数来创建、操作和同步线程。

/opt/etherlab/include 这里是晴糖豆大佬的ecrt目录,我们之前的安装过程中没有将etherlab文件复制到opt目录下,为此需要手动复制

sudo mkdir -p /opt/etherlab/include/
sudo mkdir -p /opt/etherlab/lib/ 
sudo cp -r ~/EtherLab/ethercat/include/* /opt/etherlab/include/
sudo cp -r ~/EtherLab/ethercat/lib/* /opt/etherlab/lib/

这个时候,再看c_cpp_properties.json

{
“configurations”: [
{
“name”: “Linux”,
“includePath”: [
“${workspaceFolder}/",
"/opt/etherlab/include/

],
“defines”: [],
“compilerPath”: “/usr/bin/gcc”,
“cStandard”: “c17”,
“cppStandard”: “gnu++17”,
“intelliSenseMode”: “linux-gcc-x64”
}
],
“version”: 4
}
保存,这时候,VSCODE的intelligent就可以正常使用了。

ctrl+shift+B编译的时候发现ecrt.h文件找不到,这里主要是g++找不到头文件了。为此修改task.json

        {
            "type": "cppbuild",
            "label": "C/C++: gcc-12 生成活动文件",
            "command": "/usr/bin/gcc-12",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-I",  #添加这一个指令,用来增加gcc的包含目录
                "/opt/etherlab/include/**"
            ],

在这里插入图片描述还是有问题

把后面的**去掉后好了。

在这里插入图片描述在c文件的最前面添加一行#define _GNU_SOURCE

            "-I",
            "/opt/etherlab/include/",
            "-L",
            "/opt/etherlab/lib/"

这两个在task.json里添加后没有什么用途。

/usr/local/etherlab/bin/ethercat

检查发现原来我的Etherlab安装在这里了。
研究了一下,加进去就好了。

编译代码:

=thread-group-added,id=“i1”
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1
Copyright © 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type “show copying” and “show warranty” for details.
This GDB was configured as “x86_64-linux-gnu”.
Type “show configuration” for configuration details.
For bug reporting instructions, please see:
https://www.gnu.org/software/gdb/bugs/.
Find the GDB manual and other documentation resources online at:
http://www.gnu.org/software/gdb/documentation/.
For help, type “help”.
Type “apropos word” to search for commands related to “word”.
Warning: Debuggee TargetArchitecture not detected, assuming x86_64.
=cmd-param-changed,param=“pagination”,value=“off”
Stopped due to shared library event (no libraries added or removed)
Loaded ‘/lib64/ld-linux-x86-64.so.2’. Symbols loaded.
[Inferior 1 (process 10138) exited with code 0177]
The program ‘/home/gkj/ETHERDEMOS/main’ has exited with code 177 (0x000000b1).

https://stackoverflow.com/questions/48089861/issue-while-debugging-linux-based-c-code-visual-studio-code
这里他也遇到了同样的问题,看了,说是WSL里面需要用windows下的dll,扯淡。
https://github.com/Tencent/plato/issues/105

error while loading shared libraries: libethercat.so.1: cannot open shared object file: No such file or directory

研究半天,找到一个老哥的方案:既然编译器找不到自定义库文件的位置,那就把这个位置添加到系统的默认库目录里面。
https://www.8a.hk/index.php/news/content/5093.html

  1. 笔者方案:
echo $LD_LIBRARY_PATH 

先看看里面有什么:

/opt/ros/humble/opt/rviz_ogre_vendor/lib:/opt/ros/humble/lib/x86_64-linux-gnu:/opt/ros/humble/lib

是之前装的ROS库,既然如此,直接添加。

export LD_LIBRARY_PATH=/usr/local/etherlab/lib:$LD_LIBRARY_PATH

把这个路径加上去。

这个需要每次打开终端都输入,既然如此,加到终端里面:
bash: ~/.bashrc 或者~/.bash_profile
zsh: ~/.zshrc
fish: ~/.config/fish/config.fish
vim进去编辑添加即可。
2. 参考方案:

sudo gedit /etc/ld.so.conf
添加如下,并保存:
include /etc/ld.so.conf.d/*.conf
/opt/etherlab/lib
其中/opt/etherlab/lib就是我自己的库所在的位置,
然后运行如下命令,让我们的添加生效,使所有的库文件都被缓存到文件/etc/ld.so.cache中。
sudo ldconfig
然后就可以正常编译了。

=thread-group-added,id=“i1”
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1
Copyright © 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type “show copying” and “show warranty” for details.
This GDB was configured as “x86_64-linux-gnu”.
Type “show configuration” for configuration details.
For bug reporting instructions, please see:
https://www.gnu.org/software/gdb/bugs/.
Find the GDB manual and other documentation resources online at:
http://www.gnu.org/software/gdb/documentation/.
For help, type “help”.
Type “apropos word” to search for commands related to “word”.
Warning: Debuggee TargetArchitecture not detected, assuming x86_64.
=cmd-param-changed,param=“pagination”,value=“off”
Stopped due to shared library event (no libraries added or removed)
Loaded ‘/lib64/ld-linux-x86-64.so.2’. Symbols loaded.
[Thread debugging using libthread_db enabled]
Using host libthread_db library “/lib/x86_64-linux-gnu/libthread_db.so.1”.
Breakpoint 1, main () at /home/gkj/ETHERDEMOS/main.c:365
365 {
Loaded ‘/usr/local/etherlab/lib/libethercat.so.1’. Symbols loaded.
Loaded ‘/lib/x86_64-linux-gnu/libc.so.6’. Symbols loaded.

这里把库链接上了,但是线程还是报错,debug指令目前没有研究透,暂时采用make方法进行代码撰写。

电机上电成功,但是通讯时6041状态字为0x1637,电机不动,暂时没找到原因。

  • 25
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在VSCode配置ROS环境的步骤: 1. 安装VSCode:首先,你需要下载并安装VSCode编辑器。你可以从VSCode官方网站上下载适用于你的操作系统的安装程序。 2. 安装ROS:接下来,你需要安装ROSROS是一个机器人操作系统,提供了一系列工具和库,用于构建机器人应用程序。你可以从ROS官方网站上找到适用于你的操作系统的安装指南。 3. 安装ROS插件:在VSCode中,你需要安装ROS插件以便与ROS进行集成。打开VSCode,点击左侧的扩展图标(四个方块组成的正方形),在搜索栏中输入"ROS",然后选择"ROS"插件并点击安装。 4. 配置ROS工作区:在VSCode中,你需要创建一个ROS工作区来管理你的ROS项目。在VSCode的菜单栏中,选择"文件" -> "打开文件夹",然后选择一个文件夹作为你的ROS工作区。 5. 配置ROS环境变量:在VSCode中,你需要配置ROS环境变量,以便VSCode能够正确地识别ROS命令和库。在VSCode的菜单栏中,选择"文件" -> "首选项" -> "设置",然后在右侧的设置面板中搜索"环境变量"。点击"编辑 in settings.json",然后在打开的settings.json文件中添加以下代码: ```json "terminal.integrated.env.linux": { "ROS_DISTRO": "your_ros_distro", "ROS_PACKAGE_PATH": "/opt/ros/your_ros_distro/share", "ROS_MASTER_URI": "http://localhost:11311" } ``` 请将"your_ros_distro"替换为你正在使用的ROS发行版的名称(例如"melodic")。 6. 构建和运行ROS项目:在VSCode中,你可以使用ROS插件提供的命令来构建和运行ROS项目。你可以在VSCode的终端中使用ROS命令(例如"catkin_make")来构建你的ROS项目,并使用ROS插件提供的调试功能来调试你的ROS应用程序。 希望这些步骤能帮助你在VSCode中成功配置ROS环境。如果你有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值