Open source | smart car target recognition system serial tutorial - RT thread connected ROS camera car control (6)

__ Shirley 2021-08-10 14:19:55 阅读数:778

本文一共[544]字,预计阅读时长:1分钟~
open source smart car target

 

introduction

This document describes how to use RT-Thread and ROS Connect to realize a remote control car with camera . watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk= But actually RT-Thread Part of the code has been introduced in this document : RT-Thread Connect ROS Car control , On this basis , We just need to modify it ROS The code is OK . Here, first draw the whole system block diagram , In this way, if you want to make such a car yourself, you can also have a try : watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

The physical picture looks like this :

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

2.ROS Smooth motion

 

2.1 ROS The work environment

 

The following code is installed ROS On your computer

 

ROS The installation of has been described before , I won't repeat it here , Let's create a new working section first :
1$ mkdir telebot_ws && cd telebot_ws
2$ catkin_init_workspace
  • 1.
  • 2.

 

Let's build another ROS software package :

1$ cd src
2$ catkin_create_pkg telebot rospy

  • 1.
  • 2.

 

So we can start ROS Development of , stay telebot_ws Under the table of contents :
1$ catkin_make
2$ source devel/setup.bash

  • 1.
  • 2.

 

2.2  Key trigger

Let's create a node to listen to the keys of the keyboard , And publish the received keys to /keys This topic (ROS The communication between nodes is realized by publishing and subscribing to topics ), We are telebot_ws/src/telebot/src Create a new file in the directory key_publisher.py

 

 1#!/usr/bin/python
2
3# Import package 
4import sys, select, tty, termios
5import rospy
6from std_msgs.msg import String
7
8if __name__ == '__main__':
9 # Initialize node 
10 key_pub = rospy.Publisher('keys', String, queue_size=1)
11 rospy.init_node("keyboard_driver")
12 rate = rospy.Rate(100)
13
14 # Set the terminal input mode 
15 old_attr = termios.tcgetattr(sys.stdin)
16 tty.setcbreak(sys.stdin.fileno())
17 print("Publishing keystrokes. Press Ctrl-C to exit ...")
18
19 # Loop listening for keyboard events 
20 while not rospy.is_shutdown():
21 if select.select([sys.stdin], [], [], 0)[0] == [sys.stdin]:
22 # Release the monitored keys 
23 key_pub.publish(sys.stdin.read(1))
24 rate.sleep()
25
26 # Restore terminal settings 
27 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_attr)

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

The code above is less than 20 That's ok , I also added some comments , I won't explain it in detail , We add executable permissions to this file :

1$ chmod u+x key_publisher.py

  • 1.
You can start the node :
1$ rosrun telebot key_publisher.py

  • 1.
So we can see that there is a /keys The topic will continue to output keyboard keys :
1$ rostopic echo /keys
2data: "w"
3---
4data: "a"
5---
6data: "s"
7---

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
2.3 Key analysis

Now a node is publishing our key messages , The next step is to convert the key message into the motion command of the car , That is, publish to /cmd_vel, We are telebot_ws/src/telebot/src Create a new file in the directory keys_to_twist_with_ramps.py

 1#!/usr/bin/python
2
3# Import package 
4import rospy
5import math
6from std_msgs.msg import String
7from geometry_msgs.msg import Twist
8
9# Keyboard and speed mapping w a s d
10key_mapping = { 'w': [ 0, 1], 'x': [ 0, -1],
11 'a': [ -1, 0], 'd': [1, 0],
12 's': [ 0, 0] }
13g_twist_pub = None
14g_target_twist = None
15g_last_twist = None
16g_last_send_time = None
17g_vel_scales = [0.1, 0.1] # default to very slow 
18g_vel_ramps = [1, 1] # units: meters per second^2
19
20# Prevent sudden changes in speed 
21def ramped_vel(v_prev, v_target, t_prev, t_now, ramp_rate):
22 # compute maximum velocity step
23 step = ramp_rate * (t_now - t_prev).to_sec()
24 sign = 1.0 if (v_target > v_prev) else -1.0
25 error = math.fabs(v_target - v_prev)
26 if error < step: # we can get there within this timestep. we're done.
27 return v_target
28 else:
29 return v_prev + sign * step # take a step towards the target
30
31def ramped_twist(prev, target, t_prev, t_now, ramps):
32 tw = Twist()
33 tw.angular.z = ramped_vel(prev.angular.z, target.angular.z, t_prev,
34 t_now, ramps[0])
35 tw.linear.x = ramped_vel(prev.linear.x, target.linear.x, t_prev,
36 t_now, ramps[1])
37 return tw
38
39# Issue control instructions to /cmd_vel
40def send_twist():
41 global g_last_twist_send_time, g_target_twist, g_last_twist,\
42 g_vel_scales, g_vel_ramps, g_twist_pub
43 t_now = rospy.Time.now()
44 g_last_twist = ramped_twist(g_last_twist, g_target_twist,
45 g_last_twist_send_time, t_now, g_vel_ramps)
46 g_last_twist_send_time = t_now
47 g_twist_pub.publish(g_last_twist)
48
49# subscribe /keys Callback function for 
50def keys_cb(msg):
51 global g_target_twist, g_last_twist, g_vel_scales
52 if len(msg.data) == 0 or not key_mapping.has_key(msg.data[0]):
53 return # unknown key.
54 vels = key_mapping[msg.data[0]]
55 g_target_twist.angular.z = vels[0] * g_vel_scales[0]
56 g_target_twist.linear.x = vels[1] * g_vel_scales[1]
57
58# Get the velocity acceleration ratio transmitted in 
59def fetch_param(name, default):
60 if rospy.has_param(name):
61 return rospy.get_param(name)
62 else:
63 print "parameter [%s] not defined. Defaulting to %.3f" % (name, default)
64 return default
65
66if __name__ == '__main__':
67 rospy.init_node('keys_to_twist')
68 g_last_twist_send_time = rospy.Time.now()
69 g_twist_pub = rospy.Publisher('cmd_vel', Twist, queue_size=1)
70 rospy.Subscriber('keys', String, keys_cb)
71 g_target_twist = Twist() # initializes to zero
72 g_last_twist = Twist()
73 g_vel_scales[0] = fetch_param('~angular_scale', 0.1)
74 g_vel_scales[1] = fetch_param('~linear_scale', 0.1)
75 g_vel_ramps[0] = fetch_param('~angular_accel', 1.0)
76 g_vel_ramps[1] = fetch_param('~linear_accel', 1.0)
77
78 rate = rospy.Rate(20)
79 while not rospy.is_shutdown():
80 send_twist()
81 rate.sleep()

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.

alike , We add executable permissions to this file :

1$ chmod u+x keys_to_twist_with_ramps.py

  • 1.

 

You can start the node :
1$ rosrun telebot keys_to_twist_with_ramps.py _linear_scale:=1.0 _angular_scale:=0.8 _linear_accel:=1.0 _angular_accel:=0.8

  • 1.

The parameters passed in above are the ratio of the speed and acceleration of the car we want , So we can see that there is a /cmd_vel The topic will output the desired car speed :

 1$ rostopic echo /cmd_vel
2linear:
3 x: 1.0
4 y: 0.0
5 z: 0.0
6angular:
7 x: 0.0
8 y: 0.0
9 z: 0.0
10---

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

 

Now the car can start slowly according to our instructions , stop , Turn , The next step is to add a remote camera to it .

3.ROS camera

Before connecting to the car's camera , One very important point , The previous operations were performed on the computer , Next, we're going to put our ARM Development board as ROS Master node , So you need to set environment variables , need Put the following IP Replace the address with... On the trolley ARM The actual of the board ip Address
1$ export ROS_MASTER_URI=http://your.armbian_ros.ip.address:11311

  • 1.
The following code is installed ROS In my car ARM Operating on the development board   Let's create a new working section first :
1$ mkdir telebot_ws && cd telebot_ws
2$ catkin_init_workspace

  • 1.
  • 2.
Let's build another ROS software package :
1$ cd src
2$ catkin_create_pkg telebot_image roscpp

  • 1.
  • 2.

So we can start ROS Development of , stay telebot_ws Under the table of contents :

1$ catkin_make
2$ source devel/setup.bash

  • 1.
  • 2.

 

3.1 Post camera messages

In fact, the code for publishing camera messages is 30 Row or so , We are telebot_ws/src/telebot_image/src New under the directory my_publisher.cpp

 1#include <ros/ros.h>
2#include <image_transport/image_transport.h>
3#include <opencv2/opencv.hpp>
4#include <cv_bridge/cv_bridge.h>
5
6int main(int argc, char** argv)
7{
8 ros::init(argc, argv, "video_transp");
9 ros::NodeHandle nh;
10 image_transport::ImageTransport it(nh);
11 image_transport::Publisher pub = it.advertise("camera/image", 1);
12
13 cv::VideoCapture cap(0);
14 cv::Mat frame;
15 sensor_msgs::ImagePtr frame_msg;
16
17 ros::Rate rate(10);
18
19 while(ros::ok())
20 {
21 cap >> frame;
22 if (!frame.empty())
23 {
24 frame_msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", frame).toImageMsg();
25 pub.publish(frame_msg);
26 cv::waitKey(1);
27 }
28 ros::spinOnce();
29 rate.sleep();
30 }
31 return 0;
32}

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.

 

Before compiling It needs to be installed first OpenCV Development environment of , Because we use OpenCV The library function of gets the camera data , And then use ROS The library function is published , This is a telebot_ws/src/telebot_image In the catalog CMakeLists.txt
 1cmake_minimum_required(VERSION 2.8.3)
2project(telebot_image)
3
4find_package(catkin REQUIRED COMPONENTS
5 cv_bridge
6 image_transport
7)
8
9catkin_package(
10# INCLUDE_DIRS include
11# LIBRARIES my_image_transport
12# CATKIN_DEPENDS cv_bridge image_transport
13# DEPENDS system_lib
14)
15
16include_directories(
17 ${catkin_INCLUDE_DIRS}
18)
19
20find_package(OpenCV)
21include_directories(include ${OpenCV_INCLUDE_DIRS})
22#build my_publisher and my_subscriber
23add_executable(my_publisher src/my_publisher.cpp)
24target_link_libraries(my_publisher ${catkin_LIBRARIES} ${OpenCV_LIBS})

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

We are telebot_ws Compile the project under the directory :

1$ catkin_make

  • 1.

So you can post camera messages , The image message is posted on camera/image:

1$rosrun telebot_image my_publisher
  • 1.

3.2 Subscribe to camera messages

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

It's actually very simple to subscribe to and see camera messages :
1$ rosrun image_view image_view image:=/camera/image

  • 1.
You can see the picture of the car camera on the computer . watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

4. Conclusion

If you already have one that works ROS Controlled trolley , In fact, you just need to write the first 3 Some images are released 30 Lines left and right , You can use it OpenCV Library to get camera information , And then use ROS The library has been released .

 

Of course , If it's just to see the car's camera on the computer , In fact, there are many other ways not even to write code , use ROS The advantage of publishing image data is that we can process the obtained image , for example object detection , This will be further described in the following documents .

5. reference

  • ROS Robot programming practice :https://github.com/osrf/rosbook

  • rosserial software package :https://github.com/wuhanstudio/rt-rosserial

 

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=

版权声明:本文为[__ Shirley]所创,转载请带上原文链接,感谢。 https://car.inotgo.com/2021/08/20210810140249319M.html