ROS 学习|查找、安装、卸载、删除功能包|发布和订阅整数/浮点数/布尔/字符串消息|为 .py 文件添加可执行权限
携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第9天。
前言
本文总结了自己在ROS开发过程中的一些小技巧和经验,东西比较碎,但都蛮实用的。以上,Enjoy~~~
查找、安装、卸载删除某个功能包
在运行别人的demo时,是不是会出现找不到功能包的情况。所以查找、安装和卸载某个功能包是蛮重要的。
查找
查询当前安装完成的所有包文件
rospack list
查询ros的所有功能包
apt-cache search ros-kinetic
或者说在不知道要安装功能包的确切名字的情况下找到目标包
apt-cache search ros-kinetic | grep rqt-
通过管道线 与grep命令, 输出与关键字rqt-相关的行
再或者下述安装命令与Tab键结合
sudo apt-get install ros-kinetic-rqt-
系统输出相应提示。
安装
安装某个ros包,kinetic要切换为自己的ros版本,比如melodic等。**处时自己索要安装的pkg的名字,这里注意大小写
sudo apt-get install ros-kinetic-** // 将中间的kinetic换成自己的ros版本
卸载
卸载某个功能包:与安装类似,ros-版本名称-功能包名称。
sudo apt-get purge ros-kinetic-** // 将中间的kinetic换成自己的ros版本
最后想要删除的更干净的话,可以删除包的依赖项:
sudo apt-get autoremove
发布和订阅整型/浮点型/布尔型/字符串消息
需要用ROS发布一个布尔类型的消息,上手敲代码的时候竟然不知道具体改用什么数据类型的消息,查找了资料才弄清楚,再这里记录一下
话题消息类型
ROS中的std_msgs包含几乎全部基础的数据类型,包括布尔型,整型,字符串等等。可用rosmsg show std_msgs
std_msgs/Bool
std_msgs/Byte
std_msgs/ByteMultiArray
std_msgs/Char
std_msgs/ColorRGBA
std_msgs/Duration
std_msgs/Empty
std_msgs/Float32
std_msgs/Float32MultiArray
std_msgs/Float64
std_msgs/Float64MultiArray
std_msgs/Header
std_msgs/Int16
std_msgs/Int16MultiArray
std_msgs/Int32
std_msgs/Int32MultiArray
std_msgs/Int64
std_msgs/Int64MultiArray
std_msgs/Int8
std_msgs/Int8MultiArray
std_msgs/MultiArrayDimension
std_msgs/MultiArrayLayout
std_msgs/String
std_msgs/Time
std_msgs/UInt16
std_msgs/UInt16MultiArray
std_msgs/UInt32
std_msgs/UInt32MultiArray
std_msgs/UInt64
std_msgs/UInt64MultiArray
std_msgs/UInt8
std_msgs/UInt8MultiArray
发布话题
这个很基础的操作,注意以下几点就行
- include 相关的头文件
- 消息的类型为Std_msgs::XXX. 消息成员是"data" 示例:
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
/**
* This tutorial demonstrates simple sending of messages over the ROS system.
*/
int main(int argc, char **argv)
{
/**
* The ros::init() function needs to see argc and argv so that it can perform
* any ROS arguments and name remapping that were provided at the command line.
* For programmatic remappings you can use a different version of init() which takes
* remappings directly, but for most command-line programs, passing argc and argv is
* the easiest way to do it. The third argument to init() is the name of the node.
*
* You must call one of the versions of ros::init() before using any other
* part of the ROS system.
*/
ros::init(argc, argv, "talker");
/**
* NodeHandle is the main access point to communications with the ROS system.
* The first NodeHandle constructed will fully initialize this node, and the last
* NodeHandle destructed will close down the node.
*/
ros::NodeHandle n;
/**
* The advertise() function is how you tell ROS that you want to
* publish on a given topic name. This invokes a call to the ROS
* master node, which keeps a registry of who is publishing and who
* is subscribing. After this advertise() call is made, the master
* node will notify anyone who is trying to subscribe to this topic name,
* and they will in turn negotiate a peer-to-peer connection with this
* node. advertise() returns a Publisher object which allows you to
* publish messages on that topic through a call to publish(). Once
* all copies of the returned Publisher object are destroyed, the topic
* will be automatically unadvertised.
*
* The second parameter to advertise() is the size of the message queue
* used for publishing messages. If messages are published more quickly
* than we can send them, the number here specifies how many messages to
* buffer up before throwing some away.
*/
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
ros::Rate loop_rate(10);
/**
* A count of how many messages we have sent. This is used to create
* a unique string for each message.
*/
int count = 0;
while (ros::ok())
{
/**
* This is a message object. You stuff it with data, and then publish it.
*/
std_msgs::String msg;
std::stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();
ROS_INFO("%s", msg.data.c_str());
/**
* The publish() function is how you send messages. The parameter
* is the message object. The type of this object must agree with the type
* given as a template parameter to the advertise<>() call, as was done
* in the constructor above.
*/
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}
这是ROS WIKI上的代码,如果看不懂的话,点击这个链接,上面有详细的解析。
订阅话题
同样是基础的操作,注意:
- include 相关头文件
- 回调函数的参数是指针,因此需要用“msg->data”的形式访问话题内容 代码:
#include "ros/ros.h"
#include "std_msgs/String.h"
/**
* This tutorial demonstrates simple receipt of messages over the ROS system.
*/
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
int main(int argc, char **argv)
{
/**
* The ros::init() function needs to see argc and argv so that it can perform
* any ROS arguments and name remapping that were provided at the command line.
* For programmatic remappings you can use a different version of init() which takes
* remappings directly, but for most command-line programs, passing argc and argv is
* the easiest way to do it. The third argument to init() is the name of the node.
*
* You must call one of the versions of ros::init() before using any other
* part of the ROS system.
*/
ros::init(argc, argv, "listener");
/**
* NodeHandle is the main access point to communications with the ROS system.
* The first NodeHandle constructed will fully initialize this node, and the last
* NodeHandle destructed will close down the node.
*/
ros::NodeHandle n;
/**
* The subscribe() call is how you tell ROS that you want to receive messages
* on a given topic. This invokes a call to the ROS
* master node, which keeps a registry of who is publishing and who
* is subscribing. Messages are passed to a callback function, here
* called chatterCallback. subscribe() returns a Subscriber object that you
* must hold on to until you want to unsubscribe. When all copies of the Subscriber
* object go out of scope, this callback will automatically be unsubscribed from
* this topic.
*
* The second parameter to the subscribe() function is the size of the message
* queue. If messages are arriving faster than they are being processed, this
* is the number of messages that will be buffered up before beginning to throw
* away the oldest ones.
*/
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
/**
* ros::spin() will enter a loop, pumping callbacks. With this version, all
* callbacks will be called from within this thread (the main one). ros::spin()
* will exit when Ctrl-C is pressed, or the node is shutdown by the master.
*/
ros::spin();
return 0;
}
参考文献
std_msgs ROSWIKI DEMO官方
批量化为.py文件添加可执行权限
在ROS中,想要用rosrun命令运行python文件,首先要对该文件提价可执行权限。如果要是用鼠标一个一个点左键设置属性,效率很低,并且不够优雅~~~可以用chmod命令解决
解决方案
chmod命令用于修改文件的访问权限。该命令有两种用法。一种是包含字母和操作符表达式的文字设定法;另一种是包含数字的数字设定法。 命令格式:chmod [权限] 文件名 文件名可以使用通配符,文件名为星号*表示匹配所有文件。
用法示例: 为/home/zhidao/py目录下的所有文件添加执行权限。 $ chmod +x /home/zhidao/py/* +表示增加权限,r, w, x分别表示可读取、可写入和可执行。
推荐阅读
-
为 CentOS 7 部署 SAMBA 文件共享服务
-
Linux 下的 Samba 详细说明和安装配置
-
TCP 和 UDP 端口的作用 135、137、138、139、445-端口 139
-
python 如何进行物理计算和模拟
-
全面了解格林尼治标准时间、世界协调时、时区和夏令时
-
CST、CET、UTC、GMT、DST、Unix 时间戳 几种常用时间的概述和关系 (跨时)
-
PHP 中日期和 gmdate 与默认时区设置的区别_php 技能
-
☕ 【Java 技术指南】"Java8 编程主题",让您真正会使用新版 Java 日期和时间 API 编程指南
-
几种 MySQL 引擎的差异和应用场景汇总
-
Java 编程问题:III.使用日期和时间