Install ROS on ubuntu 20.04 and experience the little turtle turning in circles


foreword

Environment:
ubuntu 20.04
VMware16.0
vscode


One, ros installation

1. Add ROS software source:

sudo sh -c ‘echo “deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main” > /etc/apt/sources.list.d/ros-latest.list’

2. Add key:

sudo apt-key adv --keyserver ‘hkp://keyserver.ubuntu.com:80’ --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654

3. Install ROS:

sudo apt update

Note: This command is to update the software source. It will search for the path of all ros-related installation packages from the above software source. When updating to the system, there may be some errors. You can replace it with a hotspot and try again .

But I can't find the problem I encountered. Later, I found that it is not necessary to install ros. It seems that I installed python2.7 and then changed it to 3.8. I did a lot of operations and couldn't solve it. Then I Just ignore it.
insert image description here

sudo apt install ros-noetic-desktop-full

Note: Different ubuntu versions correspond to different versions of ros. I am ubuntu20.0.4, so I use noetic. The download time here may be a bit long, wait a minute.

4. Initialize rosdep:

sudo rosdep init

Initialize rosdep to help us complete the setup and installation of dependencies for some feature packages.
Error 1:
insert image description here

In this case, it may be blocked, try again several times or change the network.

Mistake 2:
insert image description here
Enter the following command to modify the host file:

       sudo gedit /etc/hosts

insert image description here

Add the ip address of raw.githubusercontent.com in the first line, and then initialize:

        199.232.28.133 raw.githubusercontent.com

Mistake 3:
insert image description here
Enter the following command:

sudo rm /etc/ros/rosdep/sources.list.d/20-default.list

If it is normalized, the following situation should appear, but mine is not normal, and it returns to the second error:
insert image description here

So I used the configuration modification tool of Liubu Workshop to deal with it:
1. Install the Python package management tool pip

     sudo apt-get install python3-pip

2. Use pip to install configuration modification tools

      sudo pip3 install 6-rosdep

3. Run the configuration modification tool

      sudo 6-rosdep

Then you can run sudo rosdep init normally. If you still can't solve it, it is recommended to use the one-click installation of Yuxiang, which is simple and direct.
Yuxiang one-click installation

  • After init is finished, enter the following command to update:
 rosdep update

5. Set environment variables:

Note that the following line of command is also related to the version of ubuntu:

         echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc

Run the following command to make the environment variable take effect:

        source ~/.bashrc

6. Install rosinstall

       sudo apt install python3-rosinstall python3-rosinstall-generator python3-wstool

mistake:insert image description here

Unable to solve it successfully, but running roscore successfully.

insert image description here
At this point, the ros installation is over.

2. Experience the case of the little turtle

1. The keyboard controls the little turtle:

1. Create a new terminal to run ros

        roscore

2. Create a new terminal to start the emulator of the little turtle

            rosrun turtlesim turtlesim_node

3. Create a new terminal and open the keyboard controller

          rosrun turtlesim turtle_teleop_key

The little turtle is controlled by the up and down keys of the keyboard. A small detail is that the mouse needs to click the keyboard on the terminal where the keyboard is turned on to take effect.
insert image description here

2. The topic viewer can view the message transmission between two nodes

When the above three terminals are running, create a new terminal and enter the following command to view the topic:

rqt_graph

insert image description here

The publisher of /teleop_turtle publishes content to the subscriber /turtlesim through the topic /turtle1/cmd_vel .

3. Write a program in C++ to control the little turtle to walk in a circle

Output the content of the topic:

rostopic info /turtle1/cmd_vel

insert image description here
To see what a custom type has:

rosmsg show geometry_msgs/Twist

insert image description here

  • practice:

Because the case of the little turtle has been encapsulated in ros, we can write a C++ program to control the little turtle through topic communication.

  • Create a workspace and open it with code:
mkdir -p test_ws/src
cd test_ws
catkin_make 
code .     
  • Set environment variables:
source devel/setup.bash
  • Create a function package:
    cd to the src directory, create a new haigui package
catkin_create_pkg haigui std_msgs roscpp
  • Right-click under the function package to create a cpp file:
    insert image description here
  • code:
/*
    编写 ROS 节点,控制小乌龟画圆
    准备工作:
        1.获取topic(已知: /turtle1/cmd_vel)
        2.获取消息类型(已知: geometry_msgs/Twist)
        3.运行前,注意先启动 turtlesim_node 节点
    实现流程:
        1.包含头文件
        2.初始化 ROS 节点
        3.创建发布者对象
        4.循环发布运动控制消息
*/
 
#include "ros/ros.h"
#include "geometry_msgs/Twist.h"
 
int main(int argc, char *argv[])
{
    
    
    setlocale(LC_ALL,"");
    // 2.初始化 ROS 节点
    ros::init(argc,argv,"control");
    ros::NodeHandle nh;
    // 3.创建发布者对象
    ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",1000);
    // 4.循环发布运动控制消息
    //4-1.组织消息
    geometry_msgs::Twist msg;
    msg.linear.x = 1.0;
    msg.linear.y = 0.0;
    msg.linear.z = 0.0;
 
    msg.angular.x = 0.0;
    msg.angular.y = 0.0;
    msg.angular.z = 2.0;
 
    //4-2.设置发送频率
    ros::Rate r(10);
    //4-3.循环发送
    while (ros::ok())
    {
    
    
        pub.publish(msg);
 
        ros::spinOnce();
    }
 
 
    return 0;
}

insert image description here

  • Error 1:
    insert image description here

When these two sentences are first introduced, an error will be reported. It seems that the file cannot be found. We need to click the details in the red area to perform the include operation.

insert image description here

Different versions have different locations, generally under opt, after entering the section, click outside the box to make it take effect.

  • Error 2:
    insert image description here

There are many cases of this kind of problem. I did not save it because I modified it, so it is best to set it to automatically save after downloading vscode.

  • Modify the content of the CMakeLists.txt file under the haigui folder:
    insert image description here

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

  • test:

Return to the directory under test_ws to compile and make the code take effect.

Open the following three terminals in turn for testing:

roscore

Enter the above command to start ros

rosrun turtlesim turtlesim_node

running baby turtle

cd  ~/test_ws/                 //进入工作空间
source devel/setup.bash			//设置环境
rosrun haigui circular
  • Effect:

ros little turtle


3. Summary

In fact, ros2 has already been installed during the holiday period, but at that time, it was a one-click installation method packaged by other up masters, which was simple and trouble-free. Now downloading and installing again step by step feels like a more thorough understanding of some of the internal operations of ros. Especially when encountering various problems, the ability to solve problems has been further improved. However, after I got started with ros2, I felt that ros made me uncomfortable. I may not have adapted to it. I always feel that ros2 is easier to use and the commands are easy to remember. The above is the first experience of ros.

4. Reference

ROS communication practice (small turtle communication implementation) (9) C++, Python
details how to install the ROS system in ubuntu20.04, and complete the installation super fast (the latest version of the tutorial)

Guess you like

Origin blog.csdn.net/qq_52215423/article/details/129331888