Ubuntu安装ROS并控制小海龟

1、安装ROS

可以直接导入已经装好的镜像,这个是Ubuntu18+ROS的镜像
https://pan.baidu.com/s/15CG3mXpxpqAYeBafS-vc8w 提取码:2022
我用的是Ubuntu22.04,所以安装ROS会有点麻烦。
当然有前辈已经搞了一键安装,只需要在打开终端后输入

wget http://fishros.com/install -O fishros && . fishros

就可以一键安装,可以根据需求选择自己需要的版本。我试过了,装好以后还是有问题,这个可能因为一些设置也不同导致的。
首先,从Ubuntu中装ROS需要更换镜像源,在软件和更新中将来自中国的服务器改为aliyun.com那个。
在这里插入图片描述

然后依次执行下列命令

sudo apt update && sudo apt install locales
sudo locale-gen en_US en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
export LANG=en_US.UTF-8

然后添加ROS 2 apt 仓库

sudo apt update && sudo apt install curl gnupg lsb-release
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg

这里后面那个网站可能会出错,无法连接什么的。需要先去查它的IP地址,我查过了,这里我就直接给
188.199.108.133 raw.githubusercontent.com
108可以换为109或者110或者111,都不影响。
然后用命令 sudo gedit /etc/hosts去添加这个IP
在这里插入图片描述
然后将库添加到源列表

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(source /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null

然后更新并安装ROS2

sudo apt update && sudo apt upgrade   
sudo apt install ros-humble-desktop

安装依赖

sudo apt update && sudo apt install -y \
  python3-flake8-docstrings \
  python3-pip \
  python3-pytest-cov \
  ros-dev-tools
  python3-flake8-blind-except \
  python3-flake8-builtins \
  python3-flake8-class-newline \
  python3-flake8-comprehensions \
  python3-flake8-deprecated \
  python3-flake8-import-order \
  python3-flake8-quotes \
  python3-pytest-repeat \
  python3-pytest-rerunfailures

刷新环境变量并初始化

source /opt/ros/humble/setup.bash
echo " source /opt/ros/humble/setup.bash" >> ~/.bashrc
sudo rosdep init
rosdep update

然后用两个终端分别运行下列命令

ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key

就可以看见小海龟并简单控制
在这里插入图片描述
如果上面是用的Ubuntu18的那个镜像则需要三个终端,分别运行,我到这里用的还是22.04.后面因为能找到的测试基本都不怎么适用22.04,所以最后还是换成了18.

roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key

因为ROS2后面的版本就没有自带roscore了,所以需要的话,要自己去下。
关于查看两个弄得之间的消息传递
新建终端并输入rqt_graph看到如下
在这里插入图片描述
查看数据类型,一般都是同一数据类型

rostopic type /turtle1/cmd_vel

查看geometry_msgs/Twist的组成:

rosmsg show geometry_msgs/Twist
结果:
geometry_msgs/Vector3 linear
  float64 x
  float64 y
  float64 z
geometry_msgs/Vector3 angular
  float64 x
  float64 y
  float64 z

如上就是控制小海龟方向的线速度和角速度参数,如果要控制控制小海龟运动,可以类似的通过指令给相应话题发送数据;来控制小海龟运动

rostopic pub /turtle1/cmd_vel geometry_msgs/Twist "linear:
  x: 1.0
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.0" 

上面这个命令只会让小海龟移动一格,因为没有加入循环,所以执行一次后就终止了。所以如果加入循环并调整参数就可以让小海龟画圆。

rostopic pub -r 10 /turtle1/cmd_vel geometry_msgs/Twist
"linear:
  x: 1.0
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 1.0"
#这里的 linear 表示线速度, angular 表示角速度

如果要用C语言来使小海龟画圆,先建立一个包

catkin_create_pkg learning_topic roscpp rospy std_msgs geometry_msgs turtlesim

然后切到该路径中

cd learning_topic/src/
touch velocity_publisher.cpp

放入代码

//代码
/**
 * 该例程将发布turtle1/cmd_vel话题,消息类型geometry_msgs::Twist
 */
 
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
 
int main(int argc, char **argv)
{
    // ROS节点初始化
    ros::init(argc, argv, "velocity_publisher");
 
    // 创建节点句柄
    ros::NodeHandle n;
 
    // 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10
    ros::Publisher turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);
 
    // 设置循环的频率
    ros::Rate loop_rate(10);
 
    int count = 0;
    while (ros::ok())
    {
        // 初始化geometry_msgs::Twist类型的消息
        geometry_msgs::Twist vel_msg;
        vel_msg.linear.x = 0.5;
        vel_msg.angular.z = 0.2;
 
        // 发布消息
        turtle_vel_pub.publish(vel_msg);
        ROS_INFO("Publsh turtle velocity command[%0.2f m/s, %0.2f rad/s]", 
                vel_msg.linear.x, vel_msg.angular.z);
 
        // 按照循环频率延时
        loop_rate.sleep();
    }
 
    return 0;
}

然后需要改写Cmakelist.txt,修改两个路径
包路径指定

catkin_package(
   INCLUDE_DIRS include
   LIBRARIES learning_topic
   CATKIN_DEPENDS geometry_msgs roscpp rospy std_msgs turtlesim
#  DEPENDS system_lib
)

目标文件指定

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

然后测试

roscore
 
rosrun turtlesim turtlesim_node
 
rosrun learning_topic  velocity_publisher

以上就是C语言编程,python语言的代码在下面,感兴趣的可以按差不多的方法来试一下

python语言

```cpp
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
 
import rospy
from geometry_msgs.msg import Twist
 
 
def velocity_publisher():
    # ROS 节点初始化
    rospy.init_node('velocity_publisher', anonymous=True)
 
    # 创建一个 publisher,发布名为 /turtle1/cmd_vel 的 topic.消息类型为 geometry_msgs::Twist, 队列长度 10
    turtle_vel_pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
 
    # 设置循环频率
    rate = rospy.Rate(10)
 
    while not rospy.is_shutdown():
        # 初始化 geomety_msgs::Twist 类型消息
        vel_msg = Twist()
        vel_msg.linear.x = 1.5
        vel_msg.angular.z = 0.2
 
        # 发布消息
        turtle_vel_pub.publish(vel_msg)
        rospy.loginfo("Publish turtle velocity command[%0.2f m/s, %0.2f rad/s]",
                      vel_msg.linear.x, vel_msg.angular.z)
 
        # 按照循环频率延时
        rate.sleep()
 
 
if __name__ == "__main__":
    try:
        velocity_publisher()
    except rospy.ROSInterruptException:
        pass

这是命令行做的效果图在这里插入图片描述

总结,安装ROS控制小海龟来学习了两个node之间话题发布的关系,通过查看数据类型来通过命令行控制小海龟移动,也学习了在ros中执行一个操作必须先建立一个包,来通过指定路径来运行所写的程序。
参考链接:https://blog.csdn.net/gongdiwudu/article/details/124266929
https://blog.csdn.net/lightningwl/article/details/120310558

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qq_1482581259

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

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

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

打赏作者

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

抵扣说明:

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

余额充值