ROS机器人入门教程(2)

13. 参数的使用与编程方法

13.1 命令行实现参数配置

 

 创建功能包:
cd ~/catkin_ws/src
catkin_create_pkg learning_parameter roscpp rospy std_srvs

 

rosparam使用:
 

⚫ 列出当前多有参数
$ rosparam list
⚫ 显示某个参数值
$ rosparam get param_key
⚫ 设置某个参数值
$ rosparam set param_key param_value
⚫ 保存参数到文件
$ rosparam dump file_name
⚫ 从文件读取参数
$ rosparam load file_name
⚫ 删除参数
$ rosparam delete param_key

 参数命令行使用:

roscore

rosrun turtlesim turtlesim_node 

rosparam

rosparam get /turtlesim/background_b
rosparam get /turtlesim/background_g
rosparam get /turtlesim/background_r
rosparam get /rosversion 

rosparam get /rosdistro 

rosparam set /turtlesim/background_b 100

rosparam get /turtlesim/background_b
rosservice call /clear "{}"

rosparam set /turtlesim/background_b 255
rosparam set /turtlesim/background_g 255
rosparam set /turtlesim/background_r 255
rosservice call /clear "{}" 

 

 

 

 

 

 

 保存、修改、读取参数:

rosparam dump param.yaml        默认保存在当前路径下

rosparam load param.yaml         从文件读取参数
cy@cy-vm:~$ rosparam get /turtlesim/background_b
0
cy@cy-vm:~$ rosparam get /turtlesim/background_g
0
cy@cy-vm:~$ rosparam get /turtlesim/background_g
0
cy@cy-vm:~$ rosparam get /turtlesim/background_r
0
rosservice call /clear "{}"        刷新小海龟界面

删除参数:

rosparam delete /turtlesim/background_g

rosparam list

rosservice call /clear "{}" 

 

 

 

 

 13.2 用程序实现参数配置

在 /home/cy/catkin_ws/src/learning_parameter/src路径下创建parameter_config.cpp

/*
  该例程设置/读取海龟例程中的参数
 */

// parameter_config.cpp

#include <string>
#include <ros/ros.h>
#include <std_srvs/Empty.h>

int main(int argc, char **argv)
{
	int red, green, blue;

    // ROS节点初始化
    ros::init(argc, argv, "parameter_config");

    // 创建节点句柄
    ros::NodeHandle node;

    // 读取背景颜色参数
	ros::param::get("/turtlesim/background_r", red);
	ros::param::get("/turtlesim/background_g", green);
	ros::param::get("/turtlesim/background_b", blue);

	ROS_INFO("Get Backgroud Color[%d, %d, %d]", red, green, blue);

	// 设置背景颜色参数
	ros::param::set("/turtlesim/background_r", 0);
	ros::param::set("/turtlesim/background_g", 0);
	ros::param::set("/turtlesim/background_b", 0);

	ROS_INFO("Set Backgroud Color[0, 0, 0]");

    // 读取背景颜色参数
	ros::param::get("/turtlesim/background_r", red);
	ros::param::get("/turtlesim/background_g", green);
	ros::param::get("/turtlesim/background_b", blue);

	ROS_INFO("Re-get Backgroud Color[%d, %d, %d]", red, green, blue);

	// 调用服务,刷新背景颜色
	ros::service::waitForService("/clear");
	ros::ServiceClient clear_background = node.serviceClient<std_srvs::Empty>("/clear");
	std_srvs::Empty srv;
	clear_background.call(srv);
	
	sleep(1);

    return 0;
}

 

 配置代码编译规则:
如何配置CMakeLists.txt中的编译规则
• 设置需要编译的代码和生成的可执行文件;
• 设置链接库;

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

 

 编译并运行发布者:

$ cd ~/catkin_ws
$ catkin_make
$ source devel/setup.bash
$ roscore
$ rosrun turtlesim turtlesim_node
$ rosrun learning_parameter parameter_config
 

 

 

 

 在/home/cy/catkin_ws/src/learning_parameter/scripts路径下创建parameter_config.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 该例程设置/读取海龟例程中的参数

# parameter_config.py

import sys
import rospy
from std_srvs.srv import Empty

def parameter_config():
	# ROS节点初始化
    rospy.init_node('parameter_config', anonymous=True)

	# 读取背景颜色参数
    red   = rospy.get_param('/turtlesim/background_r')
    green = rospy.get_param('/turtlesim/background_g')
    blue  = rospy.get_param('/turtlesim/background_b')

    rospy.loginfo("Get Backgroud Color[%d, %d, %d]", red, green, blue)

	# 设置背景颜色参数
    rospy.set_param("/turtlesim/background_r", 255);
    rospy.set_param("/turtlesim/background_g", 255);
    rospy.set_param("/turtlesim/background_b", 255);

    rospy.loginfo("Set Backgroud Color[255, 255, 255]");

	# 读取背景颜色参数
    red   = rospy.get_param('/turtlesim/background_r')
    green = rospy.get_param('/turtlesim/background_g')
    blue  = rospy.get_param('/turtlesim/background_b')

    rospy.loginfo("Get Backgroud Color[%d, %d, %d]", red, green, blue)

	# 发现/spawn服务后,创建一个服务客户端,连接名为/spawn的service
    rospy.wait_for_service('/clear')
    try:
        clear_background = rospy.ServiceProxy('/clear', Empty)

		# 请求服务调用,输入请求数据
        response = clear_background()
        return response
    except rospy.ServiceException, e:
        print "Service call failed: %s"%e

if __name__ == "__main__":
    parameter_config()

 

14. ROS中的坐标系管理系统

 

 

 

机器人中的坐标变换:

 $ sudo apt-get install ros-melodic-turtle-tf
$ roslaunch turtle_tf turtle_tf_demo.launch
$ rosrun turtlesim turtle_teleop_key
$ rosrun tf view_frames        在主文件夹下面会产生一个frames.pdf

 

 

 

rosrun tf tf_echo turtle1 turtle2         两个小海龟动态的坐标关系

 

 rosrun rviz rviz -d `rospack find turtle_tf`/rviz/turtle_rviz.rviz

然后按上下左右键让小海龟动起来

 

 

 

 

15. tf坐标系广播与监听的编程实现

创建功能包
cd ~/catkin_ws/src
catkin_create_pkg learning_tf roscpp rospy tf turtlesim

 

 

 

在/home/cy/catkin_ws/src/learning_tf/src路径下写 turtle_tf_broadcaster.cpp

/**
 * 该例程产生tf数据,并计算、发布turtle2的速度指令
 */

// turtle_tf_broadcaster.cpp

#include <ros/ros.h>
#include <tf/transform_broadcaster.h>
#include <turtlesim/Pose.h>

std::string turtle_name;

void poseCallback(const turtlesim::PoseConstPtr& msg)
{
	// 创建tf的广播器
	static tf::TransformBroadcaster br;

	// 初始化tf数据
	tf::Transform transform;
	transform.setOrigin( tf::Vector3(msg->x, msg->y, 0.0) );
	tf::Quaternion q;
	q.setRPY(0, 0, msg->theta);
	transform.setRotation(q);

	// 广播world与海龟坐标系之间的tf数据
	br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "world", turtle_name));
}

int main(int argc, char** argv)
{
    // 初始化ROS节点
	ros::init(argc, argv, "my_tf_broadcaster");

	// 输入参数作为海龟的名字
	if (argc != 2)
	{
		ROS_ERROR("need turtle name as argument"); 
		return -1;
	}

	turtle_name = argv[1];

	// 订阅海龟的位姿话题
	ros::NodeHandle node;
	ros::Subscriber sub = node.subscribe(turtle_name+"/pose", 10, &poseCallback);

    // 循环等待回调函数
	ros::spin();

	return 0;
};

 

 在/home/cy/catkin_ws/src/learning_tf/src路径下写 turtle_tf_listener.cpp

 

/**
 * 该例程监听tf数据,并计算、发布turtle2的速度指令
 */

// turtle_tf_listener.cpp

#include <ros/ros.h>
#include <tf/transform_listener.h>
#include <geometry_msgs/Twist.h>
#include <turtlesim/Spawn.h>

int main(int argc, char** argv)
{
	// 初始化ROS节点
	ros::init(argc, argv, "my_tf_listener");

    // 创建节点句柄
	ros::NodeHandle node;

	// 请求产生turtle2
	ros::service::waitForService("/spawn");
	ros::ServiceClient add_turtle = node.serviceClient<turtlesim::Spawn>("/spawn");
	turtlesim::Spawn srv;
	add_turtle.call(srv);

	// 创建发布turtle2速度控制指令的发布者
	ros::Publisher turtle_vel = node.advertise<geometry_msgs::Twist>("/turtle2/cmd_vel", 10);

	// 创建tf的监听器
	tf::TransformListener listener;

	ros::Rate rate(10.0);
	while (node.ok())
	{
		// 获取turtle1与turtle2坐标系之间的tf数据
		tf::StampedTransform transform;
		try
		{
			listener.waitForTransform("/turtle2", "/turtle1", ros::Time(0), ros::Duration(3.0));
			listener.lookupTransform("/turtle2", "/turtle1", ros::Time(0), transform);
		}
		catch (tf::TransformException &ex) 
		{
			ROS_ERROR("%s",ex.what());
			ros::Duration(1.0).sleep();
			continue;
		}

		// 根据turtle1与turtle2坐标系之间的位置关系,发布turtle2的速度控制指令
		geometry_msgs::Twist vel_msg;
		vel_msg.angular.z = 4.0 * atan2(transform.getOrigin().y(),
				                        transform.getOrigin().x());
		vel_msg.linear.x = 0.5 * sqrt(pow(transform.getOrigin().x(), 2) +
				                      pow(transform.getOrigin().y(), 2));
		turtle_vel.publish(vel_msg);

		rate.sleep();
	}
	return 0;
};

 

 如何配置CMakeLists.txt中的编译规则
• 设置需要编译的代码和生成的可执行文件;
• 设置链接库;

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

 

 编译并运行:

$ cd ~/catkin_ws
$ catkin_make
$ source devel/setup.bash
$ roscore
$ rosrun turtlesim turtlesim_node
$ rosrun learning_tf turtle_tf_broadcaster __name:=turtle1_tf_broadcaster /turtle1
$ rosrun learning_tf turtle_tf_broadcaster __name:=turtle2_tf_broadcaster /turtle2
$ rosrun learning_tf turtle_tf_listener
$ rosrun turtlesim turtle_teleop_key
 

 

 

16. launch启动文件的使用方法

launch文件将命令行写成代码运行

 

 

 

 

 

 cy@cy-vm:~/catkin_ws/src$ catkin_create_pkg learning_launch

再在功能包下面创建一个launch文件夹

将simple.launch文件添加到aunch文件夹下

然后回到/home/cy/catkin_ws下进行编译:catkin_make

roslaunch learning_launch simple.launch        启动 simple.launch 文件

 simple.launch文件:

<launch>
    <node pkg="learning_topic" type="person_subscriber" name="talker" output="screen" />
    <node pkg="learning_topic" type="person_publisher" name="listener" output="screen" /> 
</launch>

 

/home/cy/catkin_ws/src/learning_launch/launch路径下创建 turtlesim_parameter_config.launch,

<launch>

	<param name="/turtle_number"   value="2"/>

    <node pkg="turtlesim" type="turtlesim_node" name="turtlesim_node">
		<param name="turtle_name1"   value="Tom"/>
		<param name="turtle_name2"   value="Jerry"/>

		<rosparam file="$(find learning_launch)/config/param.yaml" command="load"/>
	</node>

    <node pkg="turtlesim" type="turtle_teleop_key" name="turtle_teleop_key" output="screen"/>

</launch>

 

 /home/cy/catkin_ws/src/learning_launch路径下创建config文件夹,

/home/cy/catkin_ws/src/learning_launch/config路径下创建param.yaml文件

A: 123
B: "hello"

group:
  C: 456
  D: "hello"

roslaunch learning_launch turtlesim_parameter_config.launch 

rosparam list 

 

/home/cy/catkin_ws/src/learning_launch/launch路径下建立:start_tf_demo_c++.launch

 <launch>

    <!-- Turtlesim Node-->
    <node pkg="turtlesim" type="turtlesim_node" name="sim"/>
    <node pkg="turtlesim" type="turtle_teleop_key" name="teleop" output="screen"/>

    <node pkg="learning_tf" type="turtle_tf_broadcaster" args="/turtle1" name="turtle1_tf_broadcaster" />
    <node pkg="learning_tf" type="turtle_tf_broadcaster" args="/turtle2" name="turtle2_tf_broadcaster" />

    <node pkg="learning_tf" type="turtle_tf_listener" name="listener" />

  </launch>

 

17. 常用可视化工具的使用

17.1 rqt

 roscore 

 rosrun turtlesim turtlesim_node 

rqt_ +tab按键

rqt_console

rosrun turtlesim turtle_teleop_key        小海龟撞墙后日志信息发出错误警告

 

 rqt_plot

 

 rqt_image_view

 

 rqt

 

 17.2 Rviz

 

 

 

 roscore

rosrun rviz rviz 

 

 

 

 

 

 

 17.3 Gazebo

 

 roslaunch gazebo_ros willowgarage_world.launch 

 

18. 课程总结与进阶攻略

http://wiki.ros.org/ros_control

 http://wiki.ros.org/gmapping/、 http://wiki.ros.org/hector_slam

 

 https://moveit.ros.org/

 

 斯坦福大学公开课 —— 机器人学
https://www.bilibili.com/video/av4506104/

交通大学 —— 机器人学
https://www.bilibili.com/video/av18516816/?p=2

Andrew Davison的机器人学讲座课程
http://www.doc.ic.ac.uk/~ajd/Robotics/index.html


ETH - Robotic Systems Lab
http://www.rsl.ethz.ch/education-students/lectures.html


ROS理论与实践(以移动机器人为例)
http://www.shenlanxueyuan.com/course/16


ROS机械臂开发:从入门到实战
http://www.shenlanxueyuan.com/course/154
 

⚫ ROS : https://www.ros.org
⚫ ROS Wiki : http://wiki.ros.org/
⚫ ROSCon 2012 ~ 2019 : https://roscon.ros.org
⚫ ROS Robots : https://robots.ros.org/
⚫ Ubuntu Wiki : https://wiki.ubuntu.org.cn
⚫ 古月居 : http://www.gyh.ai
⚫ zhangrelay的专栏 : https://blog.csdn.net/ZhangRelay
⚫ 易科机器人实验室: http://blog.exbot.net/
⚫ 开源机器人学学习指南: https://github.com/qqfly/how-to-learn-robotics
 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力学习的代码小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值