ros+modelarts realizes multi-vehicle collaboration

Series of articles catalog

The first chapter Huawei's cloud platform modelarts
Chapter Huawei's cloud platform SDK calls modelarts
Chapter ros + modelarts multi-vehicle coordination

Preface

Take advantage of the winter vacation to organize things related to the previous learning ros operating system. This blog is not suitable for beginners who don't understand ros at all. It is recommended that beginners watch the videos related to bilibili ros and then read it in conjunction with the blog. If you just want to use modelarts to identify, you don’t need to collaborate and you don’t need to read an article.


1. Introduction and installation of ros

ROS (Robot Operating System) provides a series of libraries and tools to help software developers create robot application software. It provides many functions such as hardware abstraction, device drivers, library functions, visualization, messaging, and software package management. ROS complies with the BSD open source license agreement.

The ubuntu version of jetson nano and tx2 I use is 18.04, and the ros version should use melodic.
The ros installed below is the full version, including gazebo, if the memory is small, it is recommended to install the simplified version, and then install which software packages are needed at that time.

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
sudo apt update
sudo apt install ros-melodic-desktop-full
sudo rosdep init
rosdep update
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc
sudo apt install python-rosinstall python-rosinstall-generator python-wstool build-essential

After installation, you can use the following procedures to test

roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key

sudo rosdep init
rosdep update
will report an error during installation, you can ignore it.

2. Creation of workspace and function package

Feature packages with the same name cannot appear in the same space
scr 代码空间

build 编译空间

devel 开发空间

install 安装空间

Create a workspace

$mkdir -p ~/catkin_ws/src
$cd ~/catkin_ws/src
$catkin_init_workspace

Compile workspace

$cd ~/catkin_ws
$catkin_make

Set environment variables

$source devel/setup.bash

Check environment variables

$echo $ROS_PACKAGE_PATH

Create feature pack

$cd ~/catkin_ws/src
$catkin_create_pkg+创建文件的名字(pkg_name)+std_msgs roscpp rospy 。。。(依赖包)
将代码放进~/catkin_ws/src/上面创建的文件名称(pkg_name)/src

Compile function package

$cd ~/catkin_ws
$catkin_make
$source ~/catkin_ws/devel/setup.bash

Three, some basic operations of ros

roscore

rosnode list 查看节点

rosnode info 查看节点信息

rostopic list 查看话题

rostopic pub 话题 tab键 发布指令

rosmsg show 话题 话题结构体

rosservice list 服务列表

rqt_graph 以图的形式显示关系

############################################## #
rostopic list View all topics

rostopic info topic View topic type and information

rosmsg info data type view data structure body
############################################################################################################################################################################################################################################################################################ #########
rosservice list lists all active services

rosservice type service to view the type of a service

rosservice type service | rossrv show View what type of parameters are entered for a certain service type, and what type of parameters are returned
##################################################################################################################################################################################################################################################################### #######################

Four, TF tree learning routines

Little turtles follow

sudo apt-get install ros-melodic-turtle-tf
roslaunch turtle_tf turtle_tf_demo.launch
rosrun turtlesim turtle_teleop_key 

rosrun turtlesim turtle_teleop_key   查看tf树,会在当前目录下生成一个pdf文件
rosrun tf tf_echo turtle1 turtle2    查看坐标等数
rosrun rviz rviz -d 'rospack find turtle_tf'/rviz/turtle_rviz.rviz   通过rviz查看坐标  需要将fixed改为world 然后add TF

Five, ROS multi-machine communication (important)


 一、ROS分布式多机通讯简介
ROS是一种分布式软件框架,节点之间通过松耦合的方式组合,在很多应用场景下,节点可以运行在不同的计算平台上,通过Topic,Service通信。
但是各个节点只能共同拥有一个Master,在多机系统中Master只能运行在一台机器上,其他机器通过ssh方式和Master取得联系。所以多机ROS系统需要进行一些配置。

二、两台电脑的ROS通讯配置
两台机器的hostname与IP假设如下:
主机名与IP地址为:A     IP_A        
从机名与IP地址为:B    IP_B
1:先使用下面命令查看两台计算机的局域网IP地址
ifconfig 
然后打开hosts文件:
sudo gedit /etc/hosts
接着在hosts中加入(间隔为tab键):
IP_A  A
IP_B  B
2:重启网络服务:
sudo /etc/init.d/networking restart
3:装上chrony包,用于实现同步:
sudo apt-get install chrony
4:安装ssh工具
sudo apt-get install openssh-server
安装完以后,确认服务器是否已经启动:
ps -e|grep ssh
如果看到sshd那说明ssh-server已经启动了。
5:相互ping一下对方机子,看网络通不通:
ping A     //主机
pnig B     //从机
对主机与从机相同地执行上述操作,第六条略有不同
6:把下面的内容增加到.bashrc末尾
export ROS_HOSTNAME=[本机的hostname] #!!!注意是本机的hostname 端口号11311是固定值,照抄即可
export ROS_MASTER_URI=http://[主机的hostname]:11311
执行以下命令,使之有效:
source ~/.bashrc

三、注意事项
如果在从机运行一个roslaunch,需要现在主机上运行roscore后,从机才能启动起来。
原本一个pc上运行roslaunch的时候,会默认启动rosmaster,但是现在主机是另一个pc了,所以需要这样先把master跑起来
尽量把最常用的pc设置为master。
电脑A端:

首先启动 ROS:
$ roscore
然后 Ctrl + T 打开新的控制台,运行:
$ rosrun turtlesim turtlesim_node
电脑B端:
$ rosrun turtlesim turtle_teleop_key

Six, ros+modelarts realizes multi-vehicle collaboration

project description:

Car a patrols, collects data through the camera, and sends a message to car b when a disease or pest is found, and car b goes to disinfect after receiving the message.

The ros knowledge involved mainly includes ROS multi-machine communication and writing Publisher and Subscriber (Python version).
For the method of writing Publisher and Subscriber, please refer to this blog:https://blog.csdn.net/zong596568821xp/article/details/78088394

Difficulty one

Environment configuration, the default python version of ros is 2.7, but the jetbot driver is python3.6, you need to change the default python version of ros to 3.6.

Difficulty two

As mentioned earlier, python data analysis.

Difficulty three

Configuration of multi-machine communication environment.

a car program:

Solution 1: While using the camera to recognize the picture while uploading the picture to the modelarts platform for recognition
problems: because python does not support multi-threading (I tried python multi-threading is not easy to use and not the same) and the modelarts platform recognition speed is slow, resulting in the camera The screen is black.
Solution: A python program collects images and saves the pictures to a fixed address at regular intervals. Another python program reads the picture and sends it to HUAWEI CLOUD for identification. Send a message to car b when it is identified as a pest.

Picture collection

import cv2
cap = cv2.VideoCapture(1)    #打开摄像头
i=100
while(1):
  # get a frame
  ret, frame = cap.read()
  # show a frame
  cv2.imshow("capture", frame)   #生成摄像头窗口
  if i%100==0:
    cv2.imwrite("/home/nvidia/Desktop/1.jpg", frame)  #保存路径
  if cv2.waitKey(1) & 0xFF == ord('q'):  #如果按下q 就截图保存并退出
    break
  i=i+1
cap.release()
cv2.destroyAllWindows()

Picture recognition

#!/usr/bin/env python
# BEGIN IMPORT
import rospy
# END IMPORT
# BEGIN STD_MSGS
from std_msgs.msg import Int32
#from modelarts.session import Session
#from modelarts.model import Predictor
import cv2
# END STD_MSGS
rospy.init_node('topic_publisher')
# BEGIN PUB
pub = rospy.Publisher('counter', Int32)
# END PUB
# BEGIN LOOP
rate = rospy.Rate(0.25)
count = 0
while not rospy.is_shutdown():
    rate.sleep()
    session = Session(access_key='******',secret_key='******', project_id='******', region_name='******')

    predictor_instance = Predictor(session, service_id="******")
    predict_result = predictor_instance.predict(data="/home/nvidia/Desktop/1.jpg", data_type="images")
    print(predict_result)
    a=1
    b=2
    c=3
    j=0
	for key,value in predict_result.items():
       	 for i in predict_result[key]:
                	if (i=="黑星病")&(j==0):
                        	print("检测到黑星病即将调用杀毒车")
                        	pub.publish(a)
                        	j=j+1
      	            if(i=="锈果病")&(j==0):
      	                    print("检测到锈果病即将调用杀毒车")
                        	pub.publish(a)
                        	j=j+1
      	            if(i=="轮纹病")&(j==0):
      	                    print("检测到轮纹病即将调用杀毒车")
                        	pub.publish(a)
                        	j=j+1

b car program:

#!/usr/bin/env python
# BEGIN ALL
#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
from jetbot import Robot
import time
# BEGIN CALLBACK
def callback(msg):
    print(msg.data)

     if(msg.data==1):
        print("检测到黑星病已调用杀毒车")
        robot.set_motors(0.6, 0.6)
        time.sleep(1.5)
        robot.set_motors(0,0)
    if(msg.data==2):
        print("检测到锈果病已调用杀毒车")
        robot.set_motors(0.6, 0.6)
        time.sleep(1.5)
        robot.set_motors(0,0)
    if(msg.data==3):
        print("检测到轮纹病已调用杀毒车")
        robot.set_motors(0.6, 0.6)
        time.sleep(1.5)
        robot.set_motors(0,0)
# END CALLBACK
rospy.init_node('topic_subscriber')
robot = Robot()
# BEGIN SUBSCRIBER
sub = rospy.Subscriber('counter', Int32, callback)
# END SUBSCRIBER
rospy.spin()
# END ALL

Seven, ros+modelarts realizes intelligent elevator

program

1. Trolley enters and exits the elevator

1)小车对应程序
#!/usr/bin/env python
# BEGIN ALL
#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
from jetbot import Robot
import time
# BEGIN CALLBACK
def callback(msg):
    print(msg.data)

     if(msg.data==3):
        print("进入电梯")
        robot.set_motors(-0.6,-0.6)
        time.sleep(1.5)
        robot.set_motors(0,0)
    if(msg.data==6):
        print("出电梯")
        robot.set_motors(0.6, 0.6)
        time.sleep(1.5)
        robot.set_motors(0,0)
# END CALLBACK
rospy.init_node('topic_subscriber')
robot = Robot()
# BEGIN SUBSCRIBER
sub = rospy.Subscriber('counter', Int32, callback)
# END SUBSCRIBER
rospy.spin()
# END ALL2)电梯对应程序
#!/usr/bin/env python
# BEGIN IMPORT
import rospy
# END IMPORT
# BEGIN STD_MSGS
from std_msgs.msg import Int32
#from modelarts.session import Session
#from modelarts.model import Predictor
import cv2
# END STD_MSGS
rospy.init_node('topic_publisher')
# BEGIN PUB
pub = rospy.Publisher('counter', Int32)
# END PUB
# BEGIN LOOP
rate = rospy.Rate(0.25)
count = 0
while not rospy.is_shutdown():
    rate.sleep()
    count += 1
    print("%d层"%count)
    pub.publish(count)

2. Mask recognition

1)口罩识别运行时定时截图
import cv2
cap = cv2.VideoCapture(1)    #打开摄像头
i=100
while(1):
  # get a frame
  ret, frame = cap.read()
  # show a frame
  cv2.imshow("capture", frame)   #生成摄像头窗口
  if i%100==0:
    cv2.imwrite("/home/nvidia/Desktop/1.jpg", frame)  #保存路径
  if cv2.waitKey(1) & 0xFF == ord('q'):  #如果按下q 就截图保存并退出
    break
  i=i+1
cap.release()
cv2.destroyAllWindows()


(2)口罩识别处理 运用AI开发平台ModelArts_华为云平台
from modelarts.session import Session
from modelarts.model import Predictor
import cv2
session = Session(access_key='**************',secret_key='**************', project_id='**************', region_name='**************')
predictor_instance = Predictor(session, service_id="**************")

i=1
time=120
while(1):
   predict_result = predictor_instance.predict(data="/home/nvidia/Desktop/1.jpg",data_type="images")
  print(predict_result)
cap.release()
cv2.destroyAllWindows()

to sum up

The program is not the most difficult, the most difficult is the configuration of the environment.

(2021.2.2)

Guess you like

Origin blog.csdn.net/qq_44181970/article/details/113531789