ROS-Tutorials:rviz之Markers: Sending Basic Shapes (C++,附vscode调试说明)

2024-03-15 22:32

本文主要是介绍ROS-Tutorials:rviz之Markers: Sending Basic Shapes (C++,附vscode调试说明),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

官方的教程在这里:rviz/Tutorials/Markers: Basic Shapes - ROS Wikihttps://wiki.ros.org/rviz/Tutorials/Markers%3A%20Basic%20Shapes

我自己在ubuntu20.04上运行时碰到了一些问题,所以记录一下,

mkdir -p prj001/src
cd prj002/src
catkin_create_pkg using_markers roscpp visualization_msgs

这时候会生成下面这样的目录结构:

~/prj001/src/using_markers/src
~/prj001/src/using_markers/include
~/prj001/src/using_markers/CMakeLists.txt

然后建立一个叫basic_shapes.cpp的文件,放在这里,

~/prj001/src/using_markers/src/basic_shapes.cpp

文件内容如下,

#include <ros/ros.h>
#include <visualization_msgs/Marker.h>int main( int argc, char** argv )
{ros::init(argc, argv, "basic_shapes");ros::NodeHandle n;ros::Rate r(1);ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("visualization_marker", 1);// Set our initial shape type to be a cubeuint32_t shape = visualization_msgs::Marker::CUBE;while (ros::ok()){visualization_msgs::Marker marker;// Set the frame ID and timestamp.  See the TF tutorials for information on these.marker.header.frame_id = "my_frame"; //<------------注意这里有改动marker.header.stamp = ros::Time::now();// Set the namespace and id for this marker.  This serves to create a unique ID// Any marker sent with the same namespace and id will overwrite the old onemarker.ns = "basic_shapes";marker.id = 0;// Set the marker type.  Initially this is CUBE, and cycles between that and SPHERE, ARROW, and CYLINDERmarker.type = shape;// Set the marker action.  Options are ADD, DELETE, and new in ROS Indigo: 3 (DELETEALL)marker.action = visualization_msgs::Marker::ADD;// Set the pose of the marker.  This is a full 6DOF pose relative to the frame/time specified in the headermarker.pose.position.x = 0;marker.pose.position.y = 0;marker.pose.position.z = 0;marker.pose.orientation.x = 0.0;marker.pose.orientation.y = 0.0;marker.pose.orientation.z = 0.0;marker.pose.orientation.w = 1.0;// Set the scale of the marker -- 1x1x1 here means 1m on a sidemarker.scale.x = 1.0;marker.scale.y = 1.0;marker.scale.z = 1.0;// Set the color -- be sure to set alpha to something non-zero!marker.color.r = 0.0f;marker.color.g = 1.0f;marker.color.b = 0.0f;marker.color.a = 1.0;marker.lifetime = ros::Duration();// Publish the markerwhile (marker_pub.getNumSubscribers() < 1){if (!ros::ok()){return 0;}ROS_WARN_ONCE("Please create a subscriber to the marker");sleep(1);}marker_pub.publish(marker);// Cycle between different shapesswitch (shape){case visualization_msgs::Marker::CUBE:shape = visualization_msgs::Marker::SPHERE;break;case visualization_msgs::Marker::SPHERE:shape = visualization_msgs::Marker::ARROW;break;case visualization_msgs::Marker::ARROW:shape = visualization_msgs::Marker::CYLINDER;break;case visualization_msgs::Marker::CYLINDER:shape = visualization_msgs::Marker::CUBE;break;}r.sleep();}
}

要特别注意"/my_frame"被我改成了"my_frame",主要原因是新的tf不再支持原来的命名方式。如果不改的话rviz会报错:

Invalid argument "/my_frame" passed to canTransform argument source_frame in tf2 frame_ids cannot start with a '/' like

同时basic_shapes也会提示:

please create a subscriber to the marker 

然后,CMakeLists.txt之中要添加下面这两句

add_executable(basic_shapes src/basic_shapes.cpp)
target_link_libraries(basic_shapes ${catkin_LIBRARIES})

这样,准备好之后就可以按官方的操作编译了,

$ cd ~/prj001/
$ catkin_make

rosmake rviz

完成后分别打开三个terminal,在每个terminal中要记得source devel/setup.bash

roscore
rosrun using_markers basic_shapes
rosrun rviz rviz

最后就可以看到输出的图像了。

VSCode调试说明

参考: 

Ubuntu20.04+vscode快速调试ROS通用程序_tanmx219的博客-CSDN博客(PlaceHolder.....)这里假设你已经安装好了ROS noetic和git。(1) 安装vscode和extensionsubuntu上如何安装vscode可以参考官网,Running Visual Studio Code on Linux需要安装的vscode扩展如下,C/C++ (c++ intellisense and configuration help) -> MandatoryClangd (Alternative intellisense provhttps://blog.csdn.net/tanmx219/article/details/122799015

快捷键ctrl+shift+p,找到C/C++ :Edit configurations (JSON),添加c_cppproperties.json文件,这个文件应该是指定一些路径和语言标准,如下,

c_cpp_properties.json

{"configurations": [{"browse": {"databaseFilename": "${workspaceFolder}/.vscode/browse.vc.db","limitSymbolsToIncludedHeaders": false},"includePath": ["/opt/ros/noetic/include/**","/home/matthew/projects/prj001/src/using_markers/include/**","/usr/include/**"],"name": "ROS","intelliSenseMode": "gcc-x64","compilerPath": "/usr/bin/gcc","cStandard": "c11","cppStandard": "c++14","configurationProvider": "ms-vscode.cmake-tools"}],"version": 4
}

快捷键ctrl+shift+p,找到Tasks:Configure Task,添加tasks.json文件,这个文件指定一些catkin_make的编译参数。

注意这里的定义"-DCMAKE_BUILD_TYPE=Debug", 

tasks.json

{"tasks": [{"type": "shell","label": "prerun","command": "source ./devel/setup.sh && export ROS_MASTER_URI=http://localhost:11311/",            },{"type": "shell","label": "catkin_make","command": "catkin_make","args": ["--directory","~/projects/prj001/","-DCMAKE_BUILD_TYPE=Debug"],"options": {"cwd": "${fileDirname}"},"problemMatcher": ["$msCompile"],"group": "build","detail": "调试器生成的任务。"},{"label": "Build","dependsOn": ["catkin_make","prerun"]}],"version": "2.0.0"
}

launch.json

{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "basic_shapes","type": "cppdbg","request": "launch","program": "${workspaceFolder}/devel/lib/using_markers/basic_shapes","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": false,"MIMode": "gdb","setupCommands": [{"description": "为 gdb 启用整齐打印","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "catkin_make","miDebuggerPath": "/usr/bin/gdb"}]
}

附加说明

如果需要在launch之前运行tasks.json里的任务,就可以加上preLaunchTask这一条,比如这里tasks.json里有一个名字叫"catkin_make",可以通过

"preLaunchTask": "catkin_make"

这样的语句使程序在启动前都编译一次源码。

这样,启动vscode,选择basic_shapes,就可以调试源码了。

 

这篇关于ROS-Tutorials:rviz之Markers: Sending Basic Shapes (C++,附vscode调试说明)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/813460

相关文章

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

Redis中哨兵机制和集群的区别及说明

《Redis中哨兵机制和集群的区别及说明》Redis哨兵通过主从复制实现高可用,适用于中小规模数据;集群采用分布式分片,支持动态扩展,适合大规模数据,哨兵管理简单但扩展性弱,集群性能更强但架构复杂,根... 目录一、架构设计与节点角色1. 哨兵机制(Sentinel)2. 集群(Cluster)二、数据分片

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基

redis和redission分布式锁原理及区别说明

《redis和redission分布式锁原理及区别说明》文章对比了synchronized、乐观锁、Redis分布式锁及Redission锁的原理与区别,指出在集群环境下synchronized失效,... 目录Redis和redission分布式锁原理及区别1、有的同伴想到了synchronized关键字

MySQL 临时表创建与使用详细说明

《MySQL临时表创建与使用详细说明》MySQL临时表是存储在内存或磁盘的临时数据表,会话结束时自动销毁,适合存储中间计算结果或临时数据集,其名称以#开头(如#TempTable),本文给大家介绍M... 目录mysql 临时表详细说明1.定义2.核心特性3.创建与使用4.典型应用场景5.生命周期管理6.注