文件目录
# 主目录
/home/yule/下载/camrea
请技师安装
参考庄博的ubuntu教程,重复造轮子
参考链接
ubuntu18.04安装ROS Melodic
基于Ubuntu18.04的ROS Melodic环境详细配置(含各种大坑及填坑)
安装流程
sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ $DISTRIB_CODENAME main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt-get update
sudo apt-get install ros-melodic-desktop-full
sudo apt-get install ros-melodic-rqt*
sudo apt-get install python-rosdep
sudo rosdep init
rosdep update
sudo apt-get install python-rosinstall
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc 加入.bashrc文件中
source ~/.bashrc 修改了bashrc之后用该指令可立即生效
gedit ~/.bashrc 使用gedit打开bashrc文件,可查看设置效果
source /opt/ros/melodic/setup.bash 只在当前终端生效(不写入bashrc)
sudo chmod 777 /opt/ros/melodic/share/rviz/default.rviz
打开终端输入:
roscore
再打开一个新的终端(Termial),输入以下命令,弹出一个小乌龟窗口:
rosrun turtlesim turtlesim_node
再打开一个终端,用键盘控制小乌龟运动:
rosrun turtlesim turtle_teleop_key
初始化rosdep的问题
参考链接
sudo gedit /usr/lib/python2.7/dist-packages/rosdep2/sources_list.py
在顶部直接插入两行代码取消SSL验证,代码如下
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
sudo nautilus
第1个文件
sudo gedit /usr/lib/python2.7/dist-packages/rosdistro/__init__.py
将DEFAULT_INDEX_URL 进行如下替换
DEFAULT_INDEX_URL = 'https://ghproxy.com/https://raw.githubusercontent.com/ros/rosdistro/master/index-v4.yaml'
第2个文件
sudo gedit /usr/lib/python2.7/dist-packages/rosdistro/__init__.py
将DEFAULT_INDEX_URL 进行如下替换
DEFAULT_INDEX_URL = 'https://ghproxy.com/https://raw.githubusercontent.com/ros/rosdistro/master/index-v4.yaml'
在download_gbpdistro_as_rosdep_data()这个函数开头加入
gbpdistro_url = FUERTE_GBPDISTRO_URL
第3个文件
sudo gedit /usr/lib/python2.7/dist-packages/rosdep2/sources_list.py
第72行改为
DEFAULT_SOURCES_LIST_URL = 'https://ghproxy.com/https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/sources.list.d/20-default.list'
在download_rosdep_data()这个函数的try里面加入
url="https://ghproxy.com/"+url
第4个文件
sudo gedit /usr/lib/python2.7/dist-packages/rosdep2/rep3.py
第39行改为
REP3_TARGETS_URL = 'https://ghproxy.com/https://raw.githubusercontent.com/ros/rosdistro/master/releases/targets.yaml'
第5个文件
sudo gedit /usr/lib/python2.7/dist-packages/rosdistro/manifest_provider/github.py
第68行改为
url = 'https://ghproxy.com/https://raw.githubusercontent.com/%s/%s/package.xml' % (path, release_tag)
第119行改为
url = 'https://ghproxy.com/https://raw.githubusercontent.com/%s/%s/%s' % \
均可通过在地址前添加https://ghproxy.com/前缀来应用代理服务
再次尝试执行rosdep update,问题解决
返回ROS安装
参考链接
只安装单一版本的opencv
下载对应版本的opencv源码
安装依赖包
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
unzip opencv-3.4.13
cd opencv-3.4.13
mkdir build
cd build
cmake ..
make
pkg-config --modversion opencv
in-source builds are not allowed问题
rm CMakeCache.txt
mkdir build
cd build
cmake ..
make
unzip Linux-amd64-Ubuntu18.04-Ros-2FE.zip
cd Linux-amd64-Ubuntu18.04-Ros-2FE
sudo dpkg -i inudev_4.16.0006.30-1_amd64.deb
sudo dpkg -i ros-melodic-inudev-ros-nodelet_1.0.14-0bionic_amd64.deb
sudo dpkg -i ros-melodic-inudev-ros-nodelet-dbgsym_1.0.14-0bionic_amd64.ddeb
执行以下语句,该语句只能够在该终端下生效
LD_LIBRARY_PATH=/opt/Inuitive/InuDev/bin:$LD_LIBRARY_PATH
如果不想每次都执行上述语句,可以把它写到.bashrc中
sudo gedit ~/.bashrc
☑️ 4. 修改输出模式
设置full模式
进入/opt/ros/melodic/share/inudev_ros_nodelet/cfg,修改文件InuRosParams.yaml
设置成输出原图(原来输出的图像是去畸变后的)
打开以下路径的文件
/opt/Inuitive/InuDev/bin/NU4000c0/boot0/nu4k_boot10065.xml
找到这行
<REGISTER NAME="bypass" ADDR_OFFSET="0xC" RESET_VAL="0x0" WIDTH="32">
将dsr0,dsr1,dsr2,dsr3的VALUE都设置成1
然后运行
systemctl restart inuservice.service
roslaunch /opt/ros/melodic/share/inudev_ros_nodelet/launch/inudev_ros_nodelet.launch
相机内外参可以到这个目录下获取
/opt/inudev-4.16.0006.30/Inuitive/InuDev/config/InuSensors/JHT21480339/temperature_low/Full/NU4K
银牛相机可添加的topic
/camera/aligned_depth_to_color/image_raw
# 彩色图像
/camera/color/image_raw
# 左、右鱼眼
/left_fish
/right_fish
/sensor_msgs/image/Video/left/image
/sensor_msgs/image/Video/right/image
这节介绍如何使用银牛相机录制视频
# 开启一个终端
systemctl restart inuservice.service
roslaunch /opt/ros/melodic/share/inudev_ros_nodelet/launch/inudev_ros_nodelet.launch
# 开启另一个终端
rviz
❓ 可以先查看相机有没有被usb识别到
# 查看usb设备
lsusb
在rviz中中add对应的相机topic,即可以看到对应的相机画面
录制视频
创建record.launch
文件
<launch>
<!-- 录制包 -->
<node pkg= "rosbag" type="record" name="bag_record"
args="/left_fish /right_fish /imu/sensor_msgs/Imu -O /home/yule/下载/bzl.bag"/>
<!-- args="/camera/depth/image_rect_raw /camera/infra1/image_rect_raw /camera/infra2/image_rect_raw /camera/imu /camera/infra1/camera_info /camera/infra2/camera_info /tf_static -O /home/zxl/bzl.bag"/> -->
</launch>
args
包含要录制的topic以及bag
文件保存的路径
运行record.launch
roslaunch record.launch
参考链接
查看bag
信息
rosbag info *.bag
安装好opencv后,创建readimg.py
文件,然后运行即可
#coding:utf-8
import roslib
import rosbag
import rospy
import cv2
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
from cv_bridge import CvBridgeError
path='/***/***/***/' #存放图片的位置
class ImageCreator():
def __init__(self):
self.bridge = CvBridge()
with rosbag.Bag('***.bag', 'r') as bag: #要读取的bag文件;
for topic,msg,t in bag.read_messages():
if topic == "/***/***/***/": #图像的topic;
try:
cv_image = self.bridge.imgmsg_to_cv2(msg,"bgr8")
except CvBridgeError as e:
print e
timestr = "%.6f" % msg.header.stamp.to_sec()
#%.6f表示小数点后带有6位,可根据精确度需要修改;
image_name = timestr + ".jpg" #图像命名:时间戳.jpg
cv2.imwrite(path+image_name, cv_image) #保存;
if __name__ == '__main__':
#rospy.init_node(PKG)
try:
image_creator = ImageCreator()
except rospy.ROSInterruptException:
pass
基础
参考资料
rospy.spin()函数处理单一话题的时候会进入死循环,导致后面的话题消息处理不了
rospy.spin()
message_filters
库,实现单节点订阅多个话题参考资料
回调函数的传参问题
一般在rospy.Subsciber
中使用回调函数,默认的参数是话题的数据类型,如果我们想要在回调函数中添加额外的参数,就需要使用字典的形式传入
#python代码,python中使用字典
def callback(data, args):
dict_1 = args[0]
dict_2 = args[1]
...
sub = rospy.Subscriber("text", String, callback, (dict_1, dict_2))
参考资料
rospy的键盘输入问题
最开始在主程序中我想加入cv2.WaitKey()
来监控键盘输入,发现失败了。(还没有分析原因)通过查阅资料发现,是需要自己写一个程序来发布键盘话题,然后在multi_task
主程序中订阅键盘话题即可。
参考资料
rospy监控键盘输入
rospy获取当前时间
1️⃣ 创建pub_key.py
文件,发布键盘话题
rospy监控键盘输入
# pub_key.py
#!/usr/bin/env python
import rospy
import sys, select, tty, termios
from std_msgs.msg import String
def pub_key():
rospy.init_node('keyboard')
pub = rospy.Publisher('keys',String,queue_size = 1)
rate = rospy.Rate(100)
old_attr = termios.tcgetattr(sys.stdin)
tty.setcbreak(sys.stdin.fileno())
print('Please input keys, press Ctrl + C to quit')
while not rospy.is_shutdown():
if select.select([sys.stdin], [], [], 0)[0] == [sys.stdin]:
pub.publish(sys.stdin.read(1))
rate.sleep()
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_attr)
if __name__ == '__main__':
pub_key()
2️⃣ 创建multi_test.py
,同时订阅多个话题并同步处理
ROS节点同时订阅多个话题并进行消息时间同步
# multi_test.py
import rospy
import numpy as np
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import cv2
import threading
from std_msgs.msg import String
import message_filters
def multi_callback(raw_data,key=None,left_data=None,right_data=None):
# define picture to_down' coefficient of ratio
scaling_factor = 0.5
global count,bridge
count = count + 1
if count == 1:
count = 0
cv_img = bridge.imgmsg_to_cv2(raw_data, "bgr8")
if left_data:
left_img = bridge.imgmsg_to_cv2(left_data, "bgr8")
cv2.imshow('left_data',left_img)
if right_data:
right_img = bridge.imgmsg_to_cv2(right_data,'bgr8')
cv2.imshow('right_data',right_img)
cv2.imshow("raw_data" , cv_img)
cv2.waitKey(3)
if key.data == 's':
now = rospy.get_rostime()
name = '/home/yule/camera/img/raw_img/{}.png'.format(now)
cv2.imwrite(name,cv_img)
print('save raw img ok')
if left_data:
now = rospy.get_rostime()
name = '/home/yule/camera/img/left_fish/{}.png'.format(now)
cv2.imwrite(name,left_img)
print('save left fish ok')
if right_data:
now = rospy.get_rostime()
name = '/home/yule/camera/img/right_fish/{}.png'.format(now)
cv2.imwrite(name,right_img)
print('save right fish ok')
# print(name)
else:
pass
def mutil_task():
global count,bridge
count = 0
bridge = CvBridge()
rospy.init_node('multi_task',anonymous=True)
# send img topic
sub_raw = message_filters.Subscriber('/camera/color/image_raw',Image)
sub_left = message_filters.Subscriber('/left_fish',Image)
sub_right = message_filters.Subscriber('/right_fish',Image)
sub_key = message_filters.Subscriber('/keys',String)
sync = message_filters.ApproximateTimeSynchronizer([sub_raw,sub_key,sub_left,sub_right],10,0.1,allow_headerless=True)
sync.registerCallback(multi_callback)
rospy.spin()
if __name__ =='__main__':
mutil_task()
3️⃣ 运行系统
# 开启一个终端
systemctl restart inuservice.service
roslaunch /opt/ros/melodic/share/inudev_ros_nodelet/launch/inudev_ros_nodelet.launch
# 开启另一个终端
rviz
pub_key.py
# 开启一个新终端
python pub_key.py
multi_test.py
# 开启一个新终端
python multi_test.py
pub_key
终端中,按键盘s键进行保存