文章目录
- 前言
- 测试实践
-
- 文件目录
- 包管理BUILD文件以及cyberfile.xml文件
- 源程序
- BUILD
- 运行
- 结果
- 其他参考
- Camera
-
- [Output channels](#Output channels)
- 启动camera驱动
- [启动camera + video compression驱动](#启动camera + video compression驱动)
|---|
| |
前言
本文是对Cyber RT的学习记录,文章可能存在不严谨、不完善、有缺漏的部分,还请大家多多指出。这一章的内容还是比较简单的,直接上代码与结果。
课程地址 : https://apollo.baidu.com/community/course/outline/329?activeId=10200
更多还请参考 :[1] Apollo星火计划学习笔记------第三讲(Apollo Cyber RT 模块详解与实战)https://blog.csdn.net/sinat_52032317/article/details/126924375
[2] 【Apollo星火计划】------ Cyber基础概念|通信机制
https://blog.csdn.net/sinat_52032317/article/details/131878429?spm=1001.2014.3001.5501[3] 第一章:Cyber RT基础入门与实践https://apollo.baidu.com/community/article/1093
[4] 第二章:Cyber RT通信机制解析与实践https://apollo.baidu.com/community/article/1094
[5] 第三章:Component组件认知与实践https://apollo.baidu.com/community/article/1103
[6] 第四章:Cyber RT之调度简介与实践https://apollo.baidu.com/community/article/1106
[7] 第五章:使用Cyber RT进行相机仿真https://apollo.baidu.com/community/article/1105
相关代码整理
链接: https://pan.baidu.com/s/1ENgXE4yQ1v4nJRjcfZtd8w?pwd=ht4c 提取码: ht4c
|---|
| |
测试实践
文件目录
参考以下文件目录进行
bash
camera_demo
|-- driver
|-- camera_sim
| |-- BUILD
| |-- camera_driver.cc
|--BUILD
|--camera_demo.BUILD
|--cyberfile.xml
包管理BUILD文件以及cyberfile.xml文件
参考之前的文章https://blog.csdn.net/sinat_52032317/article/details/131878429?spm=1001.2014.3001.5501
源程序
camera_driver.cc
cpp
/*
需求: 发布摄像头仿真数据。
实现:
1.头文件;
2.初始化 cyber 框架;
3.创建节点;
4.创建发布者;
5.组织数据并发布;
6.等待关闭。
*/
#include "cyber/cyber.h"
#include "modules/common_msgs/sensor_msgs/sensor_image.pb.h"
using apollo::drivers::Image;
int main(int argc, char *argv[])
{
apollo::cyber::Init(argv[0]);
// 3.创建节点;
auto talker_node = apollo::cyber::CreateNode("camear_sim_node");
// 4.创建发布者;
auto talker = talker_node->CreateWriter<Image>("/image_sim");
// 5.组织数据并发布;
size_t width = 500;
size_t height = 350;
size_t cell = 50; // 单元格宽度
size_t step = width * 3; // 一行像素数
char black = 200;
char white = 10;
apollo::cyber::Rate rate(10.0);
while (apollo::cyber::OK()){
// 组织数据
auto msg = std::make_shared<Image>();
msg->set_frame_id("camera");
auto now = apollo::cyber::Time::Now();
msg->set_measurement_time(now.ToSecond());
msg->set_width(width);
msg->set_height(height);
msg->set_encoding("rgb8");
msg->set_step(msg->width() * 3); // 一图片行的元素个数
size_t length = msg->width() * msg->height() * 3;
char value[length];
for (size_t i = 0; i < height; i++){ // 遍历像素行
for (size_t j = 0; j < step; j++){ // 遍历列
// 当前字节索引 value[i * step + j]
int index = i * step + j;
// 行赋值
if (i / cell % 2 == 0){ // 偶数行
value[index] = black;
}
else {
value[index] = white;
}
// 列赋值
// 偶数列,无需更改
if (j / 3 / cell % 2 == 1) {// 奇数列,取反
value[index] = value[index] == white ? black : white;
}
}
}
msg->set_data(value);
//发布
talker->Write(msg);
rate.Sleep();
}
// 6.等待关闭。
apollo::cyber::WaitForShutdown();
return 0;
}
BUILD
cpp
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
load("//tools/install:install.bzl", "install", "install_src_files")
load("//tools:cpplint.bzl", "cpplint")
package(default_visibility = ["//visibility:public"])
cc_binary(
name = "camera_driver",
srcs = ["camera_driver.cc"],
deps = ["//cyber",
"//modules/common_msgs/sensor_msgs:sensor_image_cc_proto",
],
)
install(
name = "install",
runtime_dest = "camera_demo/bin",
targets = [
":camera_driver"
],
)
install_src_files(
name = "install_src",
src_dir = ["."],
dest = "camera_demo/src/cyberatest",
filter = "*",
)
记得修改包管理BUILD中的deps
运行
bash
./bazel-bin/test/test_camera/camera_driver
另开一个终端打开DreamView
bash
aem bootstrap start
选定合适的camera channel
结果
其他参考
apollo相机驱动在modules/drivers/camera
文件目录下,需要设置好相应的配置文件,才能进行正常驱动。下面贴出文档中的README部分
Camera
camera包是基于V4L USB相机设备实现封装,提供图像采集及发布的功能。本驱动中使用了一台长焦相机和一台短焦相机。
Output channels
- /apollo/sensor/camera/front_12mm/image
- /apollo/sensor/camera/front_6mm/image
- /apollo/sensor/camera/front_fisheye/image
- /apollo/sensor/camera/left_fisheye/image
- /apollo/sensor/camera/right_fisheye/image
- /apollo/sensor/camera/rear_6mm/image
启动camera驱动
请先修改并确认launch文件中的参数与实际车辆相对应
bash
# in docker
bash /apollo/scripts/camera.sh
# or
cd /apollo && cyber_launch start modules/drivers/camera/launch/camera.launch
启动camera + video compression驱动
请先修改并确认launch文件中的参数与实际车辆相对应
bash
# in docker
bash /apollo/scripts/camera_and_video.sh
# or
cd /apollo && cyber_launch start modules/drivers/camera/launch/camera_and_video.launch
### 常见问题
1. 如果出现报错"sh: 1: v4l2-ctl: not found",需要安装v4l2库。
```bash
sudo apt-get install v4l-utils
camera 驱动解释可以参考这篇博客Apollo camera驱动分析(二十九)