3 Commits

44 changed files with 2144 additions and 237 deletions
+3
View File
@@ -2,3 +2,6 @@
/engines/
/logs/
*.tmp
# Optional per-frame output, including standalone Orin runs
output/
+7
View File
@@ -0,0 +1,7 @@
# 工程交付约定
- 共享采集、停止与恢复逻辑的修改,应同步本地 `inference-only`、`use-can`、`lazy-inference` 三个分支,并完成 Git 远端与 Orin 部署同步。
- 本机模拟测试仅作为回归检查,不得用它代替 Orin 上的真实摄像头和 TensorRT 验证。报告须区分本机模拟、板端故障注入、真实硬件操作,明确未覆盖项目。
- 短暂取流故障的重点是期限内恢复后在同一进程继续输出新的推理结果,并重新计数;不能只验证超时后退出。
- 发布前核对本地提交、Git 远端、Orin 检出版本及实际运行二进制的哈希。更新 Orin 可执行文件后同时刷新独立包 SHA256SUMS。
- 更新测试期间保留原启动参数,测试结束后恢复运行部署版本。不要把旧进程仍在运行当作新版本已部署。
+6 -3
View File
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.24)
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
set(CMAKE_CUDA_ARCHITECTURES native)
set(CMAKE_CUDA_ARCHITECTURES 87)
endif()
project(camera_risk LANGUAGES CXX CUDA)
set(CMAKE_CXX_STANDARD 17)
@@ -13,11 +13,14 @@ find_path(TENSORRT_INCLUDE_DIR NvInfer.h PATH_SUFFIXES aarch64-linux-gnu x86_64-
find_library(TENSORRT_LIBRARY nvinfer REQUIRED)
add_library(camera_risk_core
src/trt_engine.cpp src/pose_engine.cu src/gpu_preprocess.cu src/uyvy_converter.cu
src/detector.cpp src/door_risk.cpp src/camera.cpp src/camera_pipeline.cpp src/config.cpp)
src/detector.cpp src/door_risk.cpp src/camera.cpp src/camera_pipeline.cpp src/serialization.cpp src/output_writer.cpp src/result_renderer.cpp src/config.cpp src/lazy_gate.cpp src/can_input.cpp src/can_comm.cpp)
target_include_directories(camera_risk_core PUBLIC include third_party ${TENSORRT_INCLUDE_DIR} ${OpenCV_INCLUDE_DIRS})
target_link_libraries(camera_risk_core PUBLIC ${TENSORRT_LIBRARY} ${OpenCV_LIBS} Threads::Threads CUDA::cudart CUDA::nppc CUDA::nppig CUDA::nppidei CUDA::nppicc)
target_link_libraries(camera_risk_core PUBLIC ${TENSORRT_LIBRARY} ${OpenCV_LIBS} Threads::Threads CUDA::cudart CUDA::nppc CUDA::nppig CUDA::nppidei CUDA::nppicc
"${CMAKE_SOURCE_DIR}/orin/lib/libusbcanfd.so" "${CMAKE_SOURCE_DIR}/orin/lib/libusb-1.0.so")
target_compile_options(camera_risk_core PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-Wall;-Wextra;-Wpedantic>)
add_library(camera_risk_visualizer src/visualizer.cpp)
target_link_libraries(camera_risk_visualizer PUBLIC camera_risk_core)
add_executable(camera_risk src/main.cpp)
target_link_libraries(camera_risk PRIVATE camera_risk_visualizer)
set_target_properties(camera_risk PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE INSTALL_RPATH "$ORIGIN/../lib;$ORIGIN/../orin/lib;/usr/local/cuda/lib64")
+81 -53
View File
@@ -1,73 +1,101 @@
# Vision Anti-Pinch
# Vision Anti-Pinch — lazy-inference
C++ / TensorRT 实时摄像头风险推理,算法来自最新 run_risk.py。
持续取图、推理并更新内存中的 FrameResult;可视化可选。
从 `inference-only` 分支派生的纯 C++ 惰性推理工程。保留该分支的 YOLO / RTMW / 时序融合 / mask 风险流程,在执行推理前加入 CAN 门状态控制。模型启动时加载、预热一次,暂停期间保持 TensorRT 引擎、上下文及显存,不反复加载。
## Orin 直接运行
## 触发规则
适用环境:Jetson AGX Orin、JetPack 5.1.2 / L4T R35.4.1、CUDA 11.4、TensorRT 8.5.2、OpenCV 4.5.4。
仓库已包含该板端编译的程序和重新构建的 TensorRT 引擎。
接收 CAN / CAN FD `0x391`,`data[0..3]` 按百分比解释,顺序为左前、右前、左后、右后。当前仅使用左前和左后,任一满足即可检测。
```bash
git clone https://git.kendeji.fun/zmv/vision-anti-pinch.git
cd vision-anti-pinch/orin
./scripts/initialize_camera.sh # 开机后初始化 /dev/video5 摄像头链路
./run.sh # 持续推理,结果只保存在内存
```
| 状态 | 推理行为 |
|---|---|
| 左前或左后开度 **<15%**,且正在增加(开门) | 检测并计算风险 |
| 左前或左后开度 **<15%**,且正在减小(关门) | 检测并计算风险 |
| 恰好 15%、大于 15%,或两门均静止 | 暂停模型推理 |
| 从未收到有效报文、报文超时或左门开度值无效 | 开启检测 |
| CAN 恢复且不满足运动条件 | 停止检测 |
可选板端本地可视化:
开度不变的重复报文不会立即抹掉运动方向。默认最后一次开度变化后 **300 ms** 仍沿用方向,超过该时间判定静止,避免整数百分比报文导致频繁启停。到达 0% 后也保留这段检测时间。首次报文及超时后的首次报文只建立基线,不将断线前后的差值当作运动。
```bash
./run.sh --visualize
```
CAN 默认 **1000 ms** 超时。上述参数可在 `config/camera.json` 的 `lazy_inference` 中调整;15% 使用严格小于。USB CAN 缺失时后台持续重试,恢复后自动接收。当前不发送执行器指令。
Ctrl+C / SIGTERM 停止程序。
关闭预览窗口或按 q/Esc 只关闭预览,推理继续。
相机每次取帧默认等待 **2000 ms**,连续超时 **6 次**才停止流水线(纯超时约 **12 秒**);成功取得有效帧后清零超时计数。重试期间 CAN 继续接收。单次等待时间由 `camera.timeout_ms` 配置;运行中的坏帧、取流错误共用该重试期限;设备失效时尝试重新打开相机,恢复有效帧后继续推理。GPU 错误仍按原有异常处理退出。
摄像头为 /dev/video5,1920×1536 UYVY,30 FPS。
当前 door mask 是指定的 constant_door_mask.json 副本,标定状态为 false;装车后应更换与实际画面匹配的门区并更新标定状态。
## 编译和运行
## 下游获取结果
main.cpp 注释中也包含以下示例。下游通过自己的调用位置或线程读取:
```cpp
auto result = pipeline.latest();
if (result) {
const auto& risk = result->risk;
const auto& people = result->persons;
// result->frame_index / track_ids / boxes
// result->image / door_mask_pixels / capture / timing
// people[i].selected / smoothed / temporal:各 133 个关键点
}
```
latest() 返回 shared_ptr<const FrameResult>;首帧尚未完成时为空。
公共定义位于 include/camera_pipeline.hpp 和 include/frame_result.hpp。
## 从源码构建
目标设备:Orin / JetPack 5.1.2 / CUDA 11.4 / TensorRT 8.5 / OpenCV 4.5,构建需 CMake 3.24+。
```bash
./scripts/build.sh
./scripts/initialize_camera.sh
./scripts/initialize_camera.sh # 摄像头尚未初始化时
./run_camera.sh
./run_camera.sh --visualize # 可选本地 OpenCV 窗口
./run_camera.sh --duration 30 # 运行 30 秒后正常退出
```
源码构建要求 CMake 3.24+、CUDA/NPP、TensorRT 和 OpenCV 开发依赖。
构建脚本会优先使用 Orin 上已有的 ~/miniconda3/envs/yolo/bin/cmake。
源码配置直接使用 orin/engines/ 中的板端引擎。该引擎仅适用于上述 Orin 环境;仓库不包含 ONNX 或模型转换脚本。
独立库目标为 camera_risk_core;可视化在 camera_risk_visualizer。
Orin 独立包可单独复制运行:
```bash
cd orin
./scripts/initialize_camera.sh
./run.sh
```
`orin/build/camera_risk` 是 ARM64 C++ 可执行程序。运行不依赖 Python。修改源码后,在 Orin 编译并运行 `./scripts/package_orin.sh` 更新独立包。`cd orin && sha256sum -c SHA256SUMS` 检查文件完整性。
## 流水线和下游结果
摄像头持续采集、CAN 持续接收。启用时运行采集、YOLO、RTMW/融合/风险三阶段流水线;暂停时继续取帧以保持画面新鲜,但不执行 YOLO、RTMW 或风险计算。暂停前已提交的 GPU 工作可以完成,其结果会被丢弃。状态切换会清空待处理帧,恢复时重置跟踪与时序融合,不使用暂停前的历史人物状态。
```cpp
cms::CameraPipeline pipeline(cms::loadConfig("config/camera.json"));
pipeline.start();
// 下游线程中:
auto state = pipeline.inferenceStatus();
// state.models_ready / enabled / can_valid / reason
// state.positions[0]:左前;state.positions[2]:左后
// state.direction[0..1]:左前/左后,-1 关门,0 静止,+1 开门
auto result = pipeline.latest(); // shared_ptr<const FrameResult>
if (result) {
const auto& risk = result->risk;
const auto& people = result->persons;
}
// 启动、暂停或等待恢复后的首帧时,result 为空;不能解释为风险 clear。
// 退出:pipeline.requestStop(); pipeline.join();
```
`latest()` 不返回旧运行周期的风险;调用者已持有的旧快照不会被修改,应重新读取状态和结果。`FrameResult::inference` 保存生成结果时的 CAN 状态和 generation。状态变化后的下一次读取会反映新的周期。`running()` 表示整个服务在运行,暂停推理时仍为 true。
默认结果仅保存在内存变量,不保存视频或逐帧文件。标准输出记录启停原因和累计推理次数;`stats()` 提供 detector_runs / pose_runs / skipped_idle 等计数。可视化暂停时显示 IDLE,不继续显示旧风险。
## 目录
- src/、include/:采集、推理、结果变量及可选绘制。
- config/:源码构建程序的配置和固定门区。
- scripts/:构建和摄像头初始化脚本。
- orin/:已编译程序、TensorRT 引擎、配置、当前 GMSL 驱动及启动脚本。
- third_party/:nlohmann JSON 及其许可证。
- `src/`、`include/`:C++ / CUDA 源码及公共接口。
- `config/`:门 mask、摄像头及惰性推理配置。
- `orin/`:模型、可执行程序、CAN 库、相机驱动及配置。
- `scripts/`:编译、初始化、打包;`third_party/`:JSON 依赖。
NPP 用于 UYVY 转换、YOLO 缩放填充、RTMW 仿射裁剪;CUDA 用于通道转换与归一化。
风险几何计算仍包含 CPU/OpenCV 操作;算法内部的临时 JSON 对象只在内存中使用。
模型和推理算法沿用纯推理基线。当前门 mask 为研发机位配置,装车需按实际机位标定。
当前摄像头驱动启动时偶发首帧 ERROR,应用会丢弃并计数;持续错误、采集超时或推理错误会明确报错并退出。
现有 GMSL 驱动停止采集时可产生 videobuf2 缓冲区告警,v4l2-ctl 同样复现;需在驱动侧完成量产修复和验证。
## 可选保存(默认全部关闭)
三个独立参数可以单独使用,也可以组合:
```bash
./run_camera.sh --save-original --save-annotated --save-results
# 独立包:./orin/run.sh --save-original --save-annotated --save-results
```
开启任一项时,程序自动创建可执行文件所属工程的 `output/` 及三个子目录:
- `original/`:`--save-original` 保存未叠加的原图,JPEG 质量 95。
- `annotated/`:`--save-annotated` 保存带框、关键点及风险文字的 JPEG,无需开启可视化窗口。
- `results/`:`--save-results` 每个完成推理的帧保存一个 JSON,包含帧号、检测框、跟踪 ID、关键点及置信度、风险和耗时;有 CAN 的分支还包含相应状态。
同一帧三类文件同名(扩展名不同),文件名包含运行标识和帧号,多次运行不覆盖。JSON 的 `images` 提供相对于 `output/` 的图片路径,未选择保存的图片为 null。源码入口写入工程 `output/`;独立包入口写入 `orin/output/`。默认不创建目录、不保存文件。
保存对象是完成推理的帧;惰性分支暂停时不新增文件。开启保存会增加编码和磁盘 I/O 开销。写入失败时报告错误并停止,不静默丢失文件。
相机超时回归测试:`./tests/run_camera_timeout.sh`;加 `--wall-clock` 验证默认 6 次超时约 12 秒。测试直接编译实际相机代码,使用模拟采集和转换器,无需摄像头、GPU 或 OpenCV;不覆盖视频回放路径。
板端真实摄像头 / TensorRT 恢复与停止测试记录见 [Orin 测试报告](tests/ORIN_TEST_REPORT.md)。
+5
View File
@@ -18,5 +18,10 @@
"warning_relative": 0.3,
"release_relative": 0.45,
"calibration_verified": false
},
"lazy_inference": {
"threshold_percent": 15,
"can_timeout_ms": 1000,
"motion_timeout_ms": 300
}
}
+9
View File
@@ -18,6 +18,15 @@ public:
if(closed_)return false;
items_.push_back(std::move(item));changed_.notify_all();return true;
}
bool pushLatest(T item) {
std::lock_guard<std::mutex> lock(mutex_);
if(closed_)return false;
if(items_.size()==capacity_)items_.pop_front();
items_.push_back(std::move(item));changed_.notify_all();return true;
}
void clear() {
std::lock_guard<std::mutex> lock(mutex_);items_.clear();changed_.notify_all();
}
std::optional<T> pop() {
std::unique_lock<std::mutex> lock(mutex_);
changed_.wait(lock,[&]{return closed_||!items_.empty();});
+7 -1
View File
@@ -1,12 +1,16 @@
#pragma once
#include "camera.hpp"
#include "frame_result.hpp"
#include "output_writer.hpp"
#include "lazy_gate.hpp"
#include <filesystem>
#include <memory>
namespace cms {
struct PipelineConfig {
CameraConfig camera;
OutputOptions output;
LazyGateConfig lazy_gate;
std::filesystem::path engines,door_mask;
double detector_confidence{0.30};
int max_people{3};
@@ -17,9 +21,10 @@ struct PipelineConfig {
struct PipelineStats {
std::uint64_t captured{},published{},driver_sequence_gaps{},discarded_error_frames{};
double mean_capture_to_result_ms{};
std::uint64_t detector_runs{},pose_runs{},skipped_idle{};
};
// One lifecycle per instance. start() warms engines before starting capture.
// Runs continuously until requestStop() or a worker failure.
// Captures and receives CAN continuously. Engines stay warm while inference is idle.
// latest() reads the immutable result variable without removing or acknowledging it.
// No result queue, consumer registration or frame-count termination.
// Stop is safe from any thread;
@@ -33,6 +38,7 @@ public:
void start();
FrameResultPtr latest() const;
bool running() const noexcept;
InferenceStatus inferenceStatus() const;
void requestStop() noexcept;
void join();
PipelineStats stats() const;
+54
View File
@@ -0,0 +1,54 @@
// can_comm.h - USBCANFD CAN/CANFD 收发封装
//
// 整合自 vision-anti-pinch_bak/main.cpp 的 CAN 模块, 提供:
// can_start() - 打开设备, 初始化并启动双通道接收线程
// can_stop() - 停止接收并关闭设备
// can_send() - 发送一帧 CAN (8 字节)
// canfd_send() - 发送一帧 CANFD (64 字节)
//
// 依赖 zcan.h (include/) 和 libusbcanfd.so + libusb-1.0.so (lib/)
#pragma once
#include <cstdint>
#include <string>
namespace CanComm {
// 初始化并启动 CAN 收发(双通道, 仲裁域 500kbps / 数据域 2000kbps)。
// 返回 true 成功; false 失败(主流程可继续仅取图)。
bool can_start();
// 停止 CAN 接收线程并关闭设备。
void can_stop();
// 注册报文解析回调。收到每帧 CAN/CANFD 数据帧时调用。
// time: 时间戳(us)
// msg_id: CAN-ID
// data: 数据指针(回调内有效, 不要保存)
// len: 数据长度
// 传 nullptr 取消注册。
using MsgCallback = void (*)(int64_t time, uint32_t msg_id, const uint8_t *data, uint32_t len);
void set_msg_callback(MsgCallback cb);
// 发送一帧 CAN2.0 数据帧(8 字节)。
// chn_idx: 通道号 0/1
// id: CAN-ID
// data: 8 字节数据(不足补 0, 超出截断)
// 返回实际发送帧数(0 失败)。
uint32_t can_send(int chn_idx, uint32_t id, const uint8_t data[8]);
// 发送一帧 CANFD 数据帧(64 字节, 扩展帧+加速)。
// chn_idx: 通道号 0/1
// id: CAN-ID
// data: 64 字节数据(不足补 0, 超出截断)
// 返回实际发送帧数(0 失败)。
uint32_t canfd_send(int chn_idx, uint32_t id, const uint8_t data[64]);
// 是否已打开并运行
bool is_open();
// 累计接收帧数(线程安全)
int receive_count();
} // namespace CanComm
+17
View File
@@ -0,0 +1,17 @@
#pragma once
#include "lazy_gate.hpp"
#include <memory>
namespace cms {
// Receives continuously, including while inference is idle; retries missing USB CAN.
class CanInput {
public:
explicit CanInput(LazyGateConfig config);
~CanInput();
CanInput(CanInput const&)=delete;
CanInput& operator=(CanInput const&)=delete;
InferenceStatus snapshot();
private:
struct Impl;
std::unique_ptr<Impl> impl_;
};
}
+2
View File
@@ -1,6 +1,7 @@
#pragma once
// Public downstream contract. Owns its data; no GPU, JSON or network handles.
#include "image.hpp"
#include "lazy_gate.hpp"
#include <array>
#include <cstdint>
#include <memory>
@@ -63,6 +64,7 @@ struct StageTiming {
double detector_ms{}, pose_ms{}, fusion_risk_ms{}, capture_to_result_ms{};
};
struct FrameResult {
InferenceStatus inference;
int frame_index{};
std::vector<int> track_ids;
std::vector<std::array<double,4>> boxes;
+1
View File
@@ -11,6 +11,7 @@ public:
if(!result)throw std::invalid_argument("Cannot publish a null result");
std::atomic_store_explicit(&latest_,std::move(result),std::memory_order_release);
}
void clear() { std::atomic_store_explicit(&latest_,FrameResultPtr{},std::memory_order_release); }
FrameResultPtr load() const {
return std::atomic_load_explicit(&latest_,std::memory_order_acquire);
}
+36
View File
@@ -0,0 +1,36 @@
#pragma once
#include <array>
#include <cstdint>
#include <string>
namespace cms {
struct LazyGateConfig {
int threshold_percent{15};
int can_timeout_ms{1000};
int motion_timeout_ms{300};
};
struct InferenceStatus {
bool models_ready{};
bool enabled{true};
bool can_valid{};
bool left_front_trigger{},left_rear_trigger{};
std::array<int,4> positions{{-1,-1,-1,-1}}; // LF, RF, LR, RR; percent
std::array<int,2> direction{}; // LF, LR: -1 closing, 0 stationary, +1 opening
std::uint64_t generation{1},sample_monotonic_ns{};
double can_age_ms{-1};
std::string reason{"CAN_UNAVAILABLE"};
};
// Pure state machine. Caller serializes access. Time is monotonic nanoseconds.
class LazyGate {
public:
explicit LazyGate(LazyGateConfig config={});
void receive(std::uint32_t id,std::uint8_t const* data,std::size_t size,std::uint64_t now);
InferenceStatus snapshot(std::uint64_t now);
private:
LazyGateConfig config_;
bool have_sample_{};
std::uint64_t last_sample_{};
std::array<std::uint64_t,2> last_change_{};
InferenceStatus state_;
};
}
+20
View File
@@ -0,0 +1,20 @@
#pragma once
#include "frame_result.hpp"
#include <filesystem>
#include <string>
namespace cms {
struct OutputOptions {
bool original{},annotated{},results{};
std::filesystem::path directory;
bool enabled() const {return original||annotated||results;}
};
// Called by the inference worker; one matching filename stem per completed frame.
class OutputWriter {
public:
explicit OutputWriter(OutputOptions options);
void write(FrameResult const& result);
private:
OutputOptions options_;
std::string runId_;
};
}
+4
View File
@@ -0,0 +1,4 @@
#pragma once
#include "frame_result.hpp"
#include "door_risk.hpp"
namespace cms { Json serializeResult(FrameResult const& result); }
+443
View File
@@ -0,0 +1,443 @@
#pragma once
#ifdef __cplusplus
#define EXTERN_C extern "C"
#else
#define EXTERN_C
#endif
#define ZCAN_API
#define INVALID_HANDLE_VALUE ((HANDLE)(-1))
#define CMD_CAN_FILTER 0x14 // 滤波
#define CMD_CAN_TTX 0x16 // 定时发送
#define CMD_CAN_TTX_CTL 0x17 // 使能定时发送
#define CMD_CAN_TRES 0x18 // CAN终端电阻
#define ZCAN_CMD_SET_CHNL_RECV_MERGE 0x32 // 设置合并接收 0:不合并接收;1:合并接收
#define ZCAN_CMD_GET_CHNL_RECV_MERGE 0x33 // 获取是否开启合并接收 0:不合并接收;1:合并接收
#define CMD_SET_SN 0x42 // 获取SN号
#define CMD_GET_SN 0x43 // 设置SN号
#define CMD_CAN_TX_TIMEOUT 0x44 // 发送超时
#define ZCAN_CMD_GET_SEND_QUEUE_SIZE 0x100 // 获取队列大小,uint32_t
#define ZCAN_CMD_GET_SEND_QUEUE_SPACE 0x101 // 获取队列剩余空间, uint32_t
#define ZCAN_CMD_SET_SEND_QUEUE_CLR 0x102 // 清空发送队列,1:清空
#define ZCAN_CMD_SET_SEND_QUEUE_EN 0x103 // 开启发送队列,1:使能
typedef unsigned char U8;
typedef unsigned short U16;
typedef unsigned int U32;
typedef unsigned long long U64;
enum eZCANDataDEF
{
//数据类型
ZCAN_DT_ZCAN_CAN_DATA = 1, // CAN/CANFD数据
ZCAN_DT_ZCAN_CANFD_DATA = 2, // CAN/CANFD数据
ZCAN_DT_ZCAN_ERROR_DATA = 3, // CAN/CANFD错误数据
ZCAN_DT_ZCAN_LIN_DATA = 4, // LIN数据
ZCAN_DT_ZCAN_LIN_ERROR_DATA = 5, // LIN错误数据
};
#pragma pack(push, 1)
typedef enum {
ZCAN_TX_NORM = 0, /**< normal transmission */
ZCAN_TX_ONCE = 1, /**< single-shot transmission */
ZCAN_SR_NORM = 2, /**< self reception */
ZCAN_SR_ONCE = 3, /**< single-shot transmission & self reception */
} ZCAN_TX_MODE;
/** CAN filter configuration */
typedef struct {
U8 type; /**< 0-std_frame, 1-ext_frame */
U8 pad[3];
U32 sid; /**< start-id */
U32 eid; /**< end-id */
} ZCAN_FILTER;
typedef struct {
U32 size;
ZCAN_FILTER table[64];
} ZCAN_FILTER_TABLE;
/** controller initialization */
typedef struct {
U32 clk; // 时钟频率
U32 mode; /**< bit0-normal/listen_only, bit1-ISO/BOSCH */
struct {
U8 tseg1;
U8 tseg2;
U8 sjw;
U8 smp;
U16 brp;
} aset;
struct {
U8 tseg1;
U8 tseg2;
U8 sjw;
U8 smp;
U16 brp;
} dset;
} ZCAN_INIT;
/** CAN message info */
typedef struct {
U32 txm : 4; /**< TX-mode, @see ZCAN_TX_MODE */
U32 fmt : 4; /**< 0-CAN2.0, 1-CANFD */
U32 sdf : 1; /**< 0-data_frame, 1-remote_frame */
U32 sef : 1; /**< 0-std_frame, 1-ext_frame */
U32 err : 1; /**< error flag */
U32 brs : 1; /**< bit-rate switch */
U32 est : 1; /**< error state */
U32 tx : 1; /**< received valid, tx frame */
U32 echo : 1; /**< tx valid, echo frame */
U32 qsend_100us:1; /**< queue send delay unit, 1-100us, 0-ms */
U32 qsend:1; /**< send valid, queue send frame */
U32 pad : 15;
} ZCAN_MSG_INF;
/** CAN message header */
typedef struct {
U32 ts; /**< timestamp */
U32 id; /**< CAN-ID */
ZCAN_MSG_INF inf; /**< @see ZCAN_MSG_INF */
U16 pad; /**< send queue delay */
U8 chn; /**< channel */
U8 len; /**< data length */
} ZCAN_MSG_HDR;
/** CAN2.0-frame */
typedef struct {
ZCAN_MSG_HDR hdr;
U8 dat[8];
} ZCAN_20_MSG;
/** CANFD-frame */
typedef struct {
ZCAN_MSG_HDR hdr;
U8 dat[64];
} ZCAN_FD_MSG;
// 错误帧
typedef struct {
ZCAN_MSG_HDR hdr;
U8 dat[8];
} ZCAN_ERR_MSG;
// 定时发送结构体
typedef struct {
U32 interval; // 定时发送周期,单位 100us
U16 repeat; // 发送次数,0等于循环发
U8 index; // 定时发送列表的帧索引号,也就是第几条定时发送报文
U8 flags; // 0-此帧禁用定时发送,1-此帧使能定时发送
ZCAN_FD_MSG msg; // CANFD帧结构体
} ZCAN_TTX;
typedef struct {
U32 size; // 实际生效的数组的长度(用来控制实际生效多少条定时发送帧结构体)
ZCAN_TTX table[8]; // 最大设置8条
} ZCAN_TTX_CFG;
// 设备信息
typedef struct {
U16 hwv; /**< hardware version */
U16 fwv; /**< firmware version */
U16 drv; /**< driver version */
U16 api; /**< API version */
U16 irq; /**< IRQ */
U8 chn; /**< channels */
U8 sn[20]; /**< serial number */
U8 id[40]; /**< card id */
U16 pad[4];
} ZCAN_DEV_INF;
/** controller status */
typedef struct {
U8 IR; /**< not used(for backward compatibility) */
U8 MOD; /**< not used */
U8 SR; /**< not used */
U8 ALC; /**< not used */
U8 ECC; /**< not used */
U8 EWL; /**< not used */
U8 RXE; /**< RX errors */
U8 TXE; /**< TX errors */
U32 PAD;
} ZCAN_STAT;
//lin begin
typedef struct _VCI_LIN_INIT_CONFIG
{
U8 linMode; // 是否作为主机,0-从机,1-主机
U8 chkSumMode; // 校验方式,1-经典校验 2-增强校验 3-自动(对应eZLINChkSumMode的模式)
U16 reserved; // 保留
U32 linBaud; // 波特率,取值1000~20000
}ZCAN_LIN_INIT_CONFIG, *PZCAN_LIN_INIT_CONFIG;
typedef struct tagZCANLINData
{
union{
struct {
U8 ID : 6; // 帧ID
U8 Parity : 2; // 帧ID校验
}unionVal;
U8 rawVal; // 受保护的ID原始值
} PID; // 受保护的ID
struct
{
U64 timeStamp; // 时间戳,单位微秒(us)
U8 dataLen; // 数据长度
U8 dir; // 传输方向,0-接收 1-发送
U8 chkSum; // 数据校验, 部分设备不支持校验数据的获取
U8 reserved[13]; // 保留
U8 data[8]; // 数据
}RxData; // 仅接收数据时有效
U8 reserved[7]; // 保留
}ZCANLINData;
typedef struct tagZCANLINErrData
{
U64 timeStamp; // 时间戳, 单位微秒(us)
union {
struct {
U8 ID : 6; // 帧ID
U8 Parity : 2; // 帧ID校验
}unionVal;
U8 rawVal; // 受保护的ID原始值
} PID; // 受保护的ID
U8 dataLen;
U8 data[8];
union
{
struct
{
U16 errStage : 4; // 错误阶段
U16 errReason : 4; // 错误原因
U16 reserved : 8; // 保留
};
U16 unionErrData;
}errData;
U8 dir; // 传输方向
U8 chkSum; // 数据校验, 部分设备不支持校验数据的获取
U8 reserved[10]; // 保留
}ZCANLINErrData;
typedef struct _VCI_LIN_MSG{
U8 chnl; // 数据通道
U8 dataType; // 数据类型, 0-LIN数据 1-LIN错误数据
union
{
ZCANLINData zcanLINData; // LIN数据
ZCANLINErrData zcanLINErrData; // LIN错误数据
U8 raw[46]; // RAW数据
} data; // 实际数据, 联合体,有效成员根据 dataType 字段而定
}ZCAN_LIN_MSG, *PZCAN_LIN_MSG;
enum eZLINChkSumMode
{
DEFAULT = 0, // 默认,启动时配置
CLASSIC_CHKSUM, // 经典校验
ENHANCE_CHKSUM, // 增强校验
AUTOMATIC, // 自动,设备自动识别校验方式(仅ZCAN_SetLINSubscribe时有效)
};
enum
{
MAX_LIN_ID_COUNT = 63,
MIN_LIN_DLC = 1,
MAX_LIN_DLC = 8,
AUTO_LIN_DLC = 255,
};
typedef struct _VCI_LIN_PUBLISH_CFG
{
U8 ID; // 受保护的ID(ID取值范围为0-63)
U8 dataLen; // dataLen范围为1-8
U8 data[8];
U8 chkSumMode; // 校验方式,0-默认,启动时配置 1-经典校验 2-增强校验(对应eZLINChkSumMode的模式)
U8 reserved[5]; // 保留
}ZCAN_LIN_PUBLISH_CFG, *PZCAN_LIN_PUBLISH_CFG;
typedef struct _VCI_LIN_SUBSCIBE_CFG
{
U8 ID; // 受保护的ID(ID取值范围为0-63)
U8 dataLen; // dataLen范围为1-8 当为255(0xff)则表示设备自动识别报文长度
U8 chkSumMode; // 校验方式,0-默认,启动时配置 1-经典校验 2-增强校验 3-自动(对应eZLINChkSumMode的模式)
U8 reserved[5]; // 保留
}ZCAN_LIN_SUBSCIBE_CFG, *PZCAN_LIN_SUBSCIBE_CFG;
// 合并接收数据数据结构,支持CAN/CANFD/LIN/错误等不同类型数据
typedef struct tagZCANDataObj
{
U8 dataType; // 数据类型, 参考eZCANDataDEF中 数据类型 部分定义
U8 chnl; // 数据通道
union{
struct{
U16 reserved : 16; // 保留
}unionVal;
U16 rawVal;
}flag; // 标志信息, 暂未使用
U8 extraData[4]; // 额外数据, 暂未使用
union
{
ZCAN_20_MSG zcanCANData; // CAN数据
ZCAN_FD_MSG zcanCANFDData; // CANFD数据
ZCAN_ERR_MSG zcanErrData; // 错误数据
ZCANLINData zcanLINData; // LIN数据
ZCANLINErrData zcanLINErrData; // LIN错误数据
U8 raw[92]; // RAW数据
} data; // 实际数据, 联合体,有效成员根据 dataType 字段而定
}ZCANDataObj;
// UDS传输协议版本
typedef U8 ZCAN_UDS_TRANS_VER;
#define ZCAN_UDS_TRANS_VER_0 0 // ISO15765-2(2004版本)
#define ZCAN_UDS_TRANS_VER_1 1 // ISO15765-2(2016版本)
// 帧类型
typedef U8 ZCAN_UDS_FRAME_TYPE;
#define ZCAN_UDS_FRAME_CAN 0 // CAN帧
#define ZCAN_UDS_FRAME_CANFD 1 // CANFD帧
#define ZCAN_UDS_FRAME_CANFD_BRS 2 // CANFD加速帧
// UDS填充模式
#define FILL_MODE_SHORT 0 // 小于8字节填充至8字节,大于8字节时按DLC就近填充
#define FILL_MODE_NONE 1 // 不填充
#define FILL_MODE_MAX 2 // 填充至最大数据长度 (不建议)
// CAN UDS请求数据
typedef struct _ZCAN_UDS_REQUEST
{
U32 req_id; // 请求事务ID,范围0~65535,本次请求的唯一标识
U8 channel; // 设备通道索引 0~255
ZCAN_UDS_FRAME_TYPE frame_type; // 帧类型
U8 reserved0[2]; // 保留
U32 src_addr; // 请求地址
U32 dst_addr; // 响应地址
U8 suppress_response; // 1:抑制响应
U8 sid; // 请求服务id
U8 reserved1[6]; // 保留
struct {
U32 timeout; // 响应超时时间(ms)。因PC定时器误差,建议设置不小于200ms
U32 enhanced_timeout; // 收到消极响应错误码为0x78后的超时时间(ms)。因PC定时器误差,建议设置不小于200ms
U8 check_any_negative_response:1; // 接收到非本次请求服务的消极响应时是否需要判定为响应错误
U8 wait_if_suppress_response:1; // 抑制响应时是否需要等待消极响应,等待时长为响应超时时间
U8 flag:6; // 保留
U8 reserved0[7]; // 保留
} session_param; // 会话层参数
struct {
ZCAN_UDS_TRANS_VER version; // 传输协议版本, VERSION_0, VERSION_1
U8 max_data_len; // 单帧最大数据长度, can:8, canfd:64
U8 local_st_min; // 本程序发送流控时用,连续帧之间的最小间隔, 0x00-0x7F(0ms~127ms), 0xF1-0xF9(100us~900us)
U8 block_size; // 流控帧的块大小
U8 fill_byte; // 无效字节的填充数据
U8 ext_frame; // 0:标准帧 1:扩展帧
U8 is_modify_ecu_st_min; // 是否忽略ECU返回流控的STmin,强制使用本程序设置的 remote_st_min
U8 remote_st_min; // 发送多帧时用, is_ignore_ecu_st_min = 1 时有效, 0x00-0x7F(0ms~127ms), 0xF1-0xF9(100us~900us)
U32 fc_timeout; // 接收流控超时时间(ms), 如发送首帧后需要等待回应流控帧
U8 fill_mode; // 数据长度填充模式, FILL_MODE_SHORT, FILL_MODE_NONE, FILL_MODE_MAX
U8 reserved0[3]; // 保留
} trans_param; // 传输层参数
U8 *data; // 数据数组(不包含SID)
U32 data_len; // 数据数组的长度
U32 reserved2; // 保留
} ZCAN_UDS_REQUEST;
// UDS错误码
typedef U8 ZCAN_UDS_ERROR;
#define ZCAN_UDS_ERROR_OK 0 // 没错误
#define ZCAN_UDS_ERROR_TIMEOUT 1 // 响应超时
#define ZCAN_UDS_ERROR_TRANSPORT 2 // 发送数据失败
#define ZCAN_UDS_ERROR_CANCEL 3 // 取消请求
#define ZCAN_UDS_ERROR_SUPPRESS_RESPONSE 4 // 抑制响应
#define ZCAN_UDS_ERROR_BUSY 5 // 忙碌中
#define ZCAN_UDS_ERROR_REQ_PARAM 6 // 请求参数错误
#define ZCAN_UDS_ERROR_OTHTER 100
typedef U8 ZCAN_UDS_RESPONSE_TYPE;
#define ZCAN_UDS_RT_NEGATIVE 0 // 消极响应
#define ZCAN_UDS_RT_POSITIVE 1 // 积极响应
// UDS响应数据
typedef struct _ZCAN_UDS_RESPONSE
{
ZCAN_UDS_ERROR status; // 响应状态
U8 reserved[6]; // 保留
ZCAN_UDS_RESPONSE_TYPE type; // 响应类型
union {
struct {
U8 sid; // 响应服务id
U32 data_len; // 数据长度(不包含SID), 数据存放在接口传入的dataBuf中
} positive;
struct {
U8 neg_code; // 固定为0x7F
U8 sid; // 请求服务id
U8 error_code; // 错误码
} negative;
U8 raw[8];
};
} ZCAN_UDS_RESPONSE;
// UDS控制类型
typedef U32 ZCAN_UDS_CTRL_CODE;
#define ZCAN_UDS_CTRL_STOP_REQ 0 // 停止UDS请求
// UDS控制请求
typedef struct _ZCAN_UDS_CTRL_REQ
{
U32 reqID; // 请求事务ID,指明要操作哪一条请求
ZCAN_UDS_CTRL_CODE cmd; // 控制类型
U8 reserved[8]; // 保留
} ZCAN_UDS_CTRL_REQ;
// UDS控制结果
typedef U32 ZCAN_UDS_CTRL_RESULT;
#define ZCAN_UDS_CTRL_RESULT_OK 0 // 成功
#define ZCAN_UDS_CTRL_RESULT_ERR 1 // 失败
// UDS控制响应数据
typedef struct _ZCAN_UDS_CTRL_RESP
{
ZCAN_UDS_CTRL_RESULT result; // 操作结果
U8 reserved[8]; // 保留
} ZCAN_UDS_CTRL_RESP;
#pragma pack(pop)
//
EXTERN_C U32 ZCAN_API VCI_OpenDevice(U32 Type, U32 Card, U32 Reserved);
EXTERN_C U32 ZCAN_API VCI_CloseDevice(U32 Type, U32 Card);
EXTERN_C U32 ZCAN_API VCI_InitCAN(U32 Type, U32 Card, U32 Port, ZCAN_INIT *pInit);
EXTERN_C U32 ZCAN_API VCI_ReadBoardInfo(U32 Type, U32 Card, ZCAN_DEV_INF *pInfo);
EXTERN_C U32 ZCAN_API VCI_ReadErrInfo(U32 Type, U32 Card, U32 Port, ZCAN_ERR_MSG *pErr);
EXTERN_C U32 ZCAN_API VCI_ReadCANStatus(U32 Type, U32 Card, U32 Port, ZCAN_STAT *pStat);
EXTERN_C U32 ZCAN_API VCI_GetReference(U32 Type, U32 Card, U32 Port, U32 Ref, void *pData);
EXTERN_C U32 ZCAN_API VCI_SetReference(U32 Type, U32 Card, U32 Port, U32 Ref, void *pData);
EXTERN_C U32 ZCAN_API VCI_GetReceiveNum(U32 Type, U32 Card, U32 Port);
EXTERN_C U32 ZCAN_API VCI_ClearBuffer(U32 Type, U32 Card, U32 Port);
EXTERN_C U32 ZCAN_API VCI_StartCAN(U32 Type, U32 Card, U32 Port);
EXTERN_C U32 ZCAN_API VCI_ResetCAN(U32 Type, U32 Card, U32 Port);
EXTERN_C U32 ZCAN_API VCI_Transmit(U32 Type, U32 Card, U32 Port, ZCAN_20_MSG *pData, U32 Count);
EXTERN_C U32 ZCAN_API VCI_TransmitFD(U32 Type, U32 Card, U32 Port, ZCAN_FD_MSG *pData, U32 Count);
EXTERN_C U32 ZCAN_API VCI_Receive(U32 Type, U32 Card, U32 Port, ZCAN_20_MSG *pData, U32 Count, U32 Time);
EXTERN_C U32 ZCAN_API VCI_ReceiveFD(U32 Type, U32 Card, U32 Port, ZCAN_FD_MSG *pData, U32 Count, U32 Time);
EXTERN_C U32 ZCAN_API VCI_Debug(U32 Debug);
// LIN
EXTERN_C U32 VCI_InitLIN(U32 Type, U32 Card, U32 LinChn, PZCAN_LIN_INIT_CONFIG pLINInitConfig);
EXTERN_C U32 VCI_StartLIN(U32 Type, U32 Card, U32 LinChn);
EXTERN_C U32 VCI_ResetLIN(U32 Type, U32 Card, U32 LinChn);
EXTERN_C U32 VCI_TransmitLIN(U32 Type, U32 Card, U32 LinChn, PZCAN_LIN_MSG pSend, U32 Len);
EXTERN_C U32 VCI_GetLINReceiveNum(U32 Type, U32 Card, U32 LinChn);
EXTERN_C U32 VCI_ClearLINBuffer(U32 Type, U32 Card, U32 LinChn);
EXTERN_C U32 VCI_ReceiveLIN(U32 Type, U32 Card, U32 LinChn, PZCAN_LIN_MSG pReceive, U32 Len,int WaitTime);
EXTERN_C U32 VCI_SetLINSubscribe(U32 Type, U32 Card, U32 LinChn, PZCAN_LIN_SUBSCIBE_CFG pSend, U32 nSubscribeCount);
EXTERN_C U32 VCI_SetLINPublish(U32 Type, U32 Card, U32 LinChn, PZCAN_LIN_PUBLISH_CFG pSend, U32 nPublishCount);
// 合并接收
EXTERN_C U32 VCI_TransmitData(unsigned Type, unsigned Card, unsigned Port, ZCANDataObj *pData, unsigned Count);
EXTERN_C U32 VCI_ReceiveData(unsigned Type, unsigned Card, unsigned Port, ZCANDataObj *pData, unsigned Count, unsigned Time);
// UDS
EXTERN_C U32 VCI_UDS_Request(unsigned Type, unsigned Card, const ZCAN_UDS_REQUEST *req, ZCAN_UDS_RESPONSE *resp, U8 *dataBuf, U32 dataBufSize);
EXTERN_C U32 VCI_UDS_Control(unsigned Type, unsigned Card, const ZCAN_UDS_CTRL_REQ *ctrl, ZCAN_UDS_CTRL_RESP *resp);
+30 -14
View File
@@ -1,22 +1,38 @@
# Orin 可直接运行目录
# Orin 惰性推理运行包(C++)
本目录包含实际在 Jetson AGX Orin 上编译的 aarch64 程序和 TensorRT FP16 引擎。
目标环境:L4T R35.4.1 / JetPack 5.1.2,CUDA 11.4,TensorRT 8.5.2,OpenCV 4.5.4。
使用系统安装的 CUDA、TensorRT、OpenCV 共享库;不适用于任意 JetPack 版本或 x86 主机。
相机每次等待默认 2 秒,连续 6 次超时才退出(约 12 秒);有效帧恢复后重新计数。Ctrl+C / SIGTERM 主动停止不需要等待 6 次超时。
在 JetPack 5.1.2 / TensorRT 8.5 / CUDA 11.4 / OpenCV 4.5 环境执行:
```bash
cd orin
sha256sum -c SHA256SUMS
./scripts/initialize_camera.sh
./scripts/initialize_camera.sh # 摄像头未初始化时
./run.sh
./run.sh --visualize # 可选窗口
```
可视化可选:`./run.sh --visualize`,需要板端本地桌面 DISPLAY。
默认持续推理直到 Ctrl+C / SIGTERM;正常退出没有统计输出。
预览窗口关闭后推理继续。结果通过 C++ CameraPipeline::latest() 获取,不通过端口或结果文件传递。
`build/camera_risk` 为 ARM64 ELF 程序,无 Python 运行依赖。
程序为 build/camera_risk,配置为 config/camera.json,引擎为 engines/*.engine。
./run.sh 从所在目录解析路径,也可以在其他工作目录通过绝对路径调用。
CAN 0x391 的 data[0..3] 为左前、右前、左后、右后百分比。左前或左后开度 <15% 且正在增加或减小时推理;其余有效 CAN 状态暂停。CAN 缺失或超时 1000 ms 时推理,恢复后重新判断。
bin/drivers 中 GMSL 模块对应内核 5.10.120-tegra。初始化脚本检查内核版本;加载模块需要 root 或现有 sudo 权限。
门区保留当前室内联调设置,door.calibration_verified=false。
模型只加载预热一次并常驻;相机和 CAN 始终运行。重复相同开度保留运动方向 300 ms,之后视为静止。参数见 config/camera.json 的 lazy_inference。默认结果保存在 C++ 内存,不写逐帧文件;暂停时 latest() 为空。标准输出记录启停状态与累计模型运行次数。
文件完整性:`sha256sum -c SHA256SUMS`。
## 可选保存(默认全部关闭)
三个独立参数可以单独使用,也可以组合:
```bash
./run.sh --save-original --save-annotated --save-results
```
开启任一项时,程序自动创建可执行文件所属工程的 `output/` 及三个子目录:
- `original/`:`--save-original` 保存未叠加的原图,JPEG 质量 95。
- `annotated/`:`--save-annotated` 保存带框、关键点及风险文字的 JPEG,无需开启可视化窗口。
- `results/`:`--save-results` 每个完成推理的帧保存一个 JSON,包含帧号、检测框、跟踪 ID、关键点及置信度、风险和耗时;有 CAN 的分支还包含相应状态。
同一帧三类文件同名(扩展名不同),文件名包含运行标识和帧号,多次运行不覆盖。JSON 的 `images` 提供相对于 `output/` 的图片路径,未选择保存的图片为 null。源码入口写入工程 `output/`;独立包入口写入 `orin/output/`。默认不创建目录、不保存文件。
保存对象是完成推理的帧;惰性分支暂停时不新增文件。开启保存会增加编码和磁盘 I/O 开销。写入失败时报告错误并停止,不静默丢失文件。
+13 -11
View File
@@ -1,11 +1,13 @@
926a5165a4ebffe9568cf694018afb097cfb69b6e696a19939e5d364a9e3dfa5 README.md
6d5e874fd27fcfd75d303d7e35d9672028af71041f52dcc82a1e83676faefa65 bin/drivers/max9295.ko
8a78d04ef8be44e9b62a5e4ee315d3d6d679e34aedff02a234bbf4d40e222933 bin/drivers/max9296.ko
3da628587477cc35d5e3f8aee86e718e2ddf16808e893e00ba6e164b2307e843 bin/drivers/sgx-yuv-gmsl2.ko
b5102fe0a529349bb8e9700ae1d8e8ddeaa7c4279cb28e7826c5cc32dce2bb1d build/camera_risk
f2334c43e6a48480260c770d46a3ab0edb855f6f52bc8b91d1559e69edfad043 config/camera.json
9ba768d559fd3b64e833b50c62c70f2adfd6c169620aea5fba32cfb1b4c5b0e9 config/door_mask.json
ebcd09e8c2e35008adbb0c1d222e580b17e0df1c8093df3481ba3374cd9a8174 engines/rtmw_l_384.engine
f6a65b926ac1144484f4aa93cd26d7997d32d62310ab93fe867f2bd4412222e9 engines/yolo11s.engine
7f42b0e7f9396e0d82313a892fc62c153261dab1206a206a466bce6376f58a36 run.sh
54e1d63e3aa72ad9b056a224d55968d4947ca6703a8d1c405570de5707a7e7ef scripts/initialize_camera.sh
b0110852effcec51d5e6b74f32a37fbb22997d6fa4f9f1e2a5dbc7cade00ab7b ./README.md
6d5e874fd27fcfd75d303d7e35d9672028af71041f52dcc82a1e83676faefa65 ./bin/drivers/max9295.ko
8a78d04ef8be44e9b62a5e4ee315d3d6d679e34aedff02a234bbf4d40e222933 ./bin/drivers/max9296.ko
3da628587477cc35d5e3f8aee86e718e2ddf16808e893e00ba6e164b2307e843 ./bin/drivers/sgx-yuv-gmsl2.ko
0d77d17626644fb50b663c33edbb1a09884b34b5410a1109fc56b4d3781651b1 ./build/camera_risk
951c514ee76e10233a84746f25a884e0a1e8cbed84df57845a5aff66af9aecbd ./config/camera.json
9ba768d559fd3b64e833b50c62c70f2adfd6c169620aea5fba32cfb1b4c5b0e9 ./config/door_mask.json
ebcd09e8c2e35008adbb0c1d222e580b17e0df1c8093df3481ba3374cd9a8174 ./engines/rtmw_l_384.engine
f6a65b926ac1144484f4aa93cd26d7997d32d62310ab93fe867f2bd4412222e9 ./engines/yolo11s.engine
8dae8c557deadbe6bd7241c0dcd7025cb007077ee39a79b115e953a2b22cd9c1 ./lib/libusb-1.0.so
ab8b7f7249d5fb73d1d33d14fa7532a522088ffb6c25f37ba334daf4c6b4d313 ./lib/libusbcanfd.so
a0b083ad029fd6430941f5aff472150c441f7d5c4b6d7f201ed1999f0f86095a ./run.sh
9122cd4ed17fb26da1cb3f9456f923508430a518eb346c5038919d4475d8d2e8 ./scripts/initialize_camera.sh
Binary file not shown.
+7 -2
View File
@@ -9,14 +9,19 @@
},
"inference": {
"engine_directory": "../engines",
"detector_confidence": 0.30,
"detector_confidence": 0.3,
"max_people": 3,
"queue_capacity": 2
},
"door": {
"mask": "door_mask.json",
"warning_relative": 0.30,
"warning_relative": 0.3,
"release_relative": 0.45,
"calibration_verified": false
},
"lazy_inference": {
"threshold_percent": 15,
"can_timeout_ms": 1000,
"motion_timeout_ms": 300
}
}
Binary file not shown.
+1
View File
@@ -0,0 +1 @@
libusb-1.0.so
Binary file not shown.
+1
View File
@@ -0,0 +1 @@
libusbcanfd.so
+1
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
set -euo pipefail
root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
export LD_LIBRARY_PATH="$root/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
exec "$root/build/camera_risk" --config "$root/config/camera.json" "$@"
+3 -1
View File
@@ -11,7 +11,9 @@ fi
for driver in max9295 max9296 sgx-yuv-gmsl2; do
file="$root/bin/drivers/$driver.ko"
[[ -f "$file" ]] || { echo "Missing driver: $file" >&2; exit 1; }
[[ "$(modinfo -F vermagic "$file")" == "$(uname -r) "* ]] || { echo "Kernel mismatch: $file" >&2; exit 1; }
vermagic="$(modinfo -F vermagic "$file")"
# Some supplied drivers omit this metadata; normal insmod still validates them.
[[ -z "$vermagic" || "$vermagic" == "$(uname -r) "* ]] || { echo "Kernel mismatch: $file" >&2; exit 1; }
done
loaded max9295 || root_command insmod "$root/bin/drivers/max9295.ko"
loaded max9296 || root_command insmod "$root/bin/drivers/max9296.ko"
+1
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
set -euo pipefail
root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
export LD_LIBRARY_PATH="$root/orin/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
exec "$root/build/camera_risk" --config "$root/config/camera.json" "$@"
+12
View File
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -euo pipefail
root="$(cd "$(dirname "$0")/.." && pwd)"
[[ -x "$root/build/camera_risk" ]] || { echo 'Build on Orin first.' >&2; exit 1; }
cp -p "$root/build/camera_risk" "$root/orin/build/"
cp -p "$root/config/door_mask.json" "$root/orin/config/"
sed 's|../orin/engines|../engines|' "$root/config/camera.json" > "$root/orin/config/camera.json"
(
cd "$root/orin"
find . -type f ! -path './output/*' ! -path './outputs/*' ! -path './logs/*' ! -name SHA256SUMS -print0 |
sort -z | xargs -0 sha256sum > SHA256SUMS
)
+78 -34
View File
@@ -9,6 +9,7 @@
#include <unistd.h>
#include <time.h>
#include <cerrno>
#include <algorithm>
#include <cstring>
#include <limits>
#include <stdexcept>
@@ -42,6 +43,11 @@ struct Camera::Impl {
try {
stopFd=eventfd(0,EFD_CLOEXEC|EFD_NONBLOCK);
if(stopFd<0)throw std::runtime_error("Cannot create camera stop event");
openCapture();
} catch(...) {release();throw;}
}
void openCapture() {
try {
fd=open(config.device.c_str(),O_RDWR|O_NONBLOCK|O_CLOEXEC);
if(fd<0)throw std::runtime_error("Cannot open "+config.device+": "+std::strerror(errno));
v4l2_capability capability{};checkedIoctl(fd,VIDIOC_QUERYCAP,&capability,"VIDIOC_QUERYCAP");
@@ -77,16 +83,19 @@ struct Camera::Impl {
if(mapping==MAP_FAILED)throw std::runtime_error("Camera mmap failed");
buffers[i].data=mapping;checkedIoctl(fd,VIDIOC_QBUF,&buffer,"VIDIOC_QBUF");
}
converter=std::make_unique<UyvyConverter>(config.width,config.height);
if(!converter)converter=std::make_unique<UyvyConverter>(config.width,config.height);
auto type=V4L2_BUF_TYPE_VIDEO_CAPTURE;checkedIoctl(fd,VIDIOC_STREAMON,&type,"VIDIOC_STREAMON");streaming=true;
} catch(...) {release();throw;}
} catch(...) {releaseCapture();throw;}
}
~Impl(){release();}
void release() noexcept {
void releaseCapture() noexcept {
if(streaming){auto type=V4L2_BUF_TYPE_VIDEO_CAPTURE;ioctl(fd,VIDIOC_STREAMOFF,&type);streaming=false;}
for(auto const& b:buffers)if(b.data)munmap(b.data,b.size);
buffers.clear();
if(fd>=0){close(fd);fd=-1;}
}
void release() noexcept {
releaseCapture();
if(stopFd>=0){close(stopFd);stopFd=-1;}
}
void stop() noexcept {
@@ -94,47 +103,82 @@ struct Camera::Impl {
if(stopFd>=0){auto ignored=write(stopFd,&one,sizeof(one));(void)ignored;}
}
std::optional<CapturedFrame> readFrame() {
auto windowStart=monotonicNs();unsigned failedWindows=0;
auto windowNs=std::uint64_t(config.timeout_ms)*1000000;
std::string lastError="no camera frame";
auto expired=[&](bool timedOut=false) {
auto now=monotonicNs();
if(timedOut||now-windowStart>=windowNs) {
if(++failedWindows>=6)
throw std::runtime_error("Camera capture failed 6 consecutive times: "+config.device+": "+lastError);
windowStart=now;
}
};
auto pause=[&] {
pollfd stopEvent{stopFd,POLLIN,0};
// Back off transient driver errors while retaining immediate cancellation.
poll(&stopEvent,1,20);
};
auto reconnect=[&] {
releaseCapture();
if(haveSequence){++index;haveSequence=false;}
};
while(!stopped.load()) {
expired();
if(fd<0) {
try {openCapture();}
catch(std::exception const& e){lastError=e.what();pause();continue;}
}
if(stopped.load())return {};
auto elapsed=monotonicNs()-windowStart;
auto remaining=elapsed<windowNs?windowNs-elapsed:1;
int waitMs=static_cast<int>((remaining+999999)/1000000);
pollfd descriptors[2]={{fd,POLLIN,0},{stopFd,POLLIN,0}};
int ready=poll(descriptors,2,config.timeout_ms);
int ready=poll(descriptors,2,waitMs);
if(ready<0&&errno==EINTR)continue;
if(stopped.load()||(descriptors[1].revents&POLLIN))return {};
if(ready==0)throw std::runtime_error("Camera capture timeout: "+config.device);
if(ready<0||(descriptors[0].revents&(POLLERR|POLLHUP|POLLNVAL)))throw std::runtime_error("Camera disconnected or poll failed");
if(ready==0){expired(true);continue;}
if(ready<0||(descriptors[0].revents&(POLLERR|POLLHUP|POLLNVAL))) {
lastError="camera disconnected or poll failed";reconnect();pause();continue;
}
v4l2_buffer buffer{};buffer.type=V4L2_BUF_TYPE_VIDEO_CAPTURE;buffer.memory=V4L2_MEMORY_MMAP;
int status=ioctl(fd,VIDIOC_DQBUF,&buffer);
if(status<0&&(errno==EAGAIN||errno==EINTR))continue;
if(status<0)throw std::runtime_error("VIDIOC_DQBUF: "+std::string(std::strerror(errno)));
if(buffer.index>=buffers.size())throw std::runtime_error("Invalid V4L2 buffer index");
// The GMSL driver can mark its startup buffer as corrupt. Requeue
// it without conversion or inference; persistent corruption is fatal.
if(buffer.flags&V4L2_BUF_FLAG_ERROR) {
checkedIoctl(fd,VIDIOC_QBUF,&buffer,"VIDIOC_QBUF(error frame)");
if(++consecutiveErrors>=static_cast<unsigned>(config.fps))
throw std::runtime_error("Camera produced one second of consecutive error frames");
continue;
if(status<0) {
lastError="VIDIOC_DQBUF: "+std::string(std::strerror(errno));
// EIO can be a transient corrupt frame; a lost device needs reopening.
if(errno!=EIO)reconnect();
pause();continue;
}
if(buffer.index>=buffers.size()){lastError="invalid V4L2 buffer index";reconnect();pause();continue;}
bool invalid=(buffer.flags&V4L2_BUF_FLAG_ERROR)||buffer.bytesused<std::size_t(stride)*config.height||buffer.bytesused>buffers[buffer.index].size;
if(invalid) {
++consecutiveErrors;lastError="corrupt or incomplete camera frame";
if(ioctl(fd,VIDIOC_QBUF,&buffer)<0)reconnect();
pause();continue;
}
CapturedFrame frame;
try {
if(buffer.bytesused<std::size_t(stride)*config.height||buffer.bytesused>buffers[buffer.index].size)
throw std::runtime_error("Invalid camera frame: flags="+std::to_string(buffer.flags)+", used="+std::to_string(buffer.bytesused)+", required="+std::to_string(std::size_t(stride)*config.height)+", allocated="+std::to_string(buffers[buffer.index].size));
auto& m=frame.metadata;
m.discarded_error_frames=consecutiveErrors;consecutiveErrors=0;
m.driver_sequence=buffer.sequence;m.received_monotonic_ns=monotonicNs();
m.driver_timestamp_ns=std::uint64_t(buffer.timestamp.tv_sec)*1000000000ULL+std::uint64_t(buffer.timestamp.tv_usec)*1000;
m.driver_timestamp_monotonic=(buffer.flags&V4L2_BUF_FLAG_TIMESTAMP_MASK)==V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
m.capture_monotonic_ns=m.driver_timestamp_monotonic&&m.driver_timestamp_ns>0&&m.driver_timestamp_ns<=m.received_monotonic_ns?m.driver_timestamp_ns:m.received_monotonic_ns;
if(haveSequence) {
auto delta=static_cast<std::uint32_t>(buffer.sequence-lastSequence);
if(delta==0||delta>0x7fffffffU)throw std::runtime_error("Camera sequence reset or repeated frame");
m.skipped_driver_frames=delta-1;index+=delta;
auto& m=frame.metadata;
m.discarded_error_frames=consecutiveErrors;
m.driver_sequence=buffer.sequence;m.received_monotonic_ns=monotonicNs();
m.driver_timestamp_ns=std::uint64_t(buffer.timestamp.tv_sec)*1000000000ULL+std::uint64_t(buffer.timestamp.tv_usec)*1000;
m.driver_timestamp_monotonic=(buffer.flags&V4L2_BUF_FLAG_TIMESTAMP_MASK)==V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
m.capture_monotonic_ns=m.received_monotonic_ns;
if(haveSequence) {
auto delta=static_cast<std::uint32_t>(buffer.sequence-lastSequence);
if(delta==0||delta>0x7fffffffU) {
lastError="camera sequence reset or repeated frame";
reconnect();pause();continue;
}
m.frame_index=index;
lastSequence=buffer.sequence;haveSequence=true;
// Convert before requeuing. No published result references MMAP memory.
frame.image=converter->convert(buffers[buffer.index].data,stride);
} catch(...) {ioctl(fd,VIDIOC_QBUF,&buffer);throw;}
checkedIoctl(fd,VIDIOC_QBUF,&buffer,"VIDIOC_QBUF");return frame;
m.skipped_driver_frames=delta-1;index+=delta;
}
m.frame_index=index;
// GPU failures remain fatal; only capture failures consume the retry budget.
try {frame.image=converter->convert(buffers[buffer.index].data,stride);}
catch(...) {ioctl(fd,VIDIOC_QBUF,&buffer);throw;}
if(ioctl(fd,VIDIOC_QBUF,&buffer)<0){lastError="VIDIOC_QBUF failed";reconnect();pause();continue;}
lastSequence=buffer.sequence;haveSequence=true;consecutiveErrors=0;
return frame; // The next read starts a fresh six-window retry budget.
}
return {};
}
+49 -10
View File
@@ -5,6 +5,7 @@
#include "door_risk.hpp"
#include "records.hpp"
#include "result_conversion.hpp"
#include "can_input.hpp"
#include <opencv2/core.hpp>
#include <atomic>
#include <cmath>
@@ -45,17 +46,22 @@ void attachPoses(FrameResult& result,fangjia::PoseFrameResult const& fused) {
}
}
struct CameraPipeline::Impl {
struct Detected {CapturedFrame frame;std::vector<fangjia::Box> boxes;double elapsed_ms{};};
struct Pending {CapturedFrame frame;std::uint64_t generation;};
struct Detected {CapturedFrame frame;std::vector<fangjia::Box> boxes;double elapsed_ms{};std::uint64_t generation;};
PipelineConfig config;
BoundedQueue<CapturedFrame> captures;
BoundedQueue<Pending> captures;
BoundedQueue<Detected> detections;
LatestResult results;
std::unique_ptr<OutputWriter> outputWriter;
fangjia::TrtLogger logger;
std::unique_ptr<Detector> detector;
std::unique_ptr<fangjia::RtmwEngine> pose;
std::unique_ptr<DoorRisk> risk;
std::shared_ptr<const BinaryMask> doorPixels;
std::unique_ptr<Camera> camera;
std::unique_ptr<CanInput> can;
std::atomic<bool> modelsReady{false};
std::atomic<std::uint64_t> detectorRuns{0},poseRuns{0},skippedIdle{0};
std::thread captureThread,detectorThread,inferenceThread;
mutable std::mutex stateMutex;
std::exception_ptr failure;
@@ -65,6 +71,7 @@ struct CameraPipeline::Impl {
double totalLatency{};
explicit Impl(PipelineConfig c):config(std::move(c)),captures(config.queue_capacity),detections(config.queue_capacity) {
LazyGate validateGate(config.lazy_gate);
if(config.queue_capacity>8)throw std::invalid_argument("Queue capacity must be between 1 and 8");
if(config.max_people<1||config.max_people>16||!std::isfinite(config.detector_confidence)||config.detector_confidence<=0||config.detector_confidence>=1)
throw std::invalid_argument("Invalid detector configuration");
@@ -77,7 +84,7 @@ struct CameraPipeline::Impl {
if(inferenceThread.joinable())inferenceThread.join();
}
void stop() noexcept {
stopped=true;active=false;
stopped=true;active=false;results.clear();
// Camera stays alive until all threads have joined. start()/destruction
// are owner-thread operations; requestStop() only interrupts existing work.
{std::lock_guard<std::mutex> lock(stateMutex);if(camera)camera->requestStop();}
@@ -93,6 +100,7 @@ struct CameraPipeline::Impl {
void start() {
{std::lock_guard<std::mutex> lock(stateMutex);if(started||stopped)throw std::logic_error("CameraPipeline can only be started once");started=true;}
try {
outputWriter=std::make_unique<OutputWriter>(config.output);
detector=std::make_unique<Detector>(config.engines/"yolo11s.engine",logger,config.detector_confidence,config.max_people);
pose=std::make_unique<fangjia::RtmwEngine>(config.engines/"rtmw_l_384.engine",logger,config.camera.width,config.camera.height,config.max_people);
DoorOptions options;options.constant=config.door_mask;options.warning=config.warning_relative;options.release=config.release_relative;
@@ -106,48 +114,71 @@ struct CameraPipeline::Impl {
pose->warmup(warmup.value);
detector->infer(cv::Mat(config.camera.height,config.camera.width,CV_8UC3,cv::Scalar()),warmup.value);
{std::lock_guard<std::mutex> lock(stateMutex);camera=std::make_unique<Camera>(config.camera);if(stopped)camera->requestStop();}
{std::lock_guard<std::mutex> lock(stateMutex);can=std::make_unique<CanInput>(config.lazy_gate);}
modelsReady=true;
active=!stopped.load();
captureThread=std::thread([this]{try{captureLoop();}catch(...){fail();}});
detectorThread=std::thread([this]{try{detectorLoop();}catch(...){fail();}});
inferenceThread=std::thread([this]{try{inferenceLoop();}catch(...){fail();}});
}catch(...){stop();joinWorkers();throw;}
}
InferenceStatus status() const {
std::lock_guard<std::mutex> lock(stateMutex);
auto status=can?can->snapshot():InferenceStatus{};
status.models_ready=modelsReady;
if(!active||stopped){status.enabled=false;status.reason=stopped?"STOPPED":"STARTING";}
return status;
}
bool eligible(std::uint64_t generation) const {auto s=status();return s.enabled&&s.generation==generation;}
void captureLoop() {
std::uint64_t generation=0;
while(!stopped) {
auto frame=camera->read();if(!frame)break;
gaps+=frame->metadata.skipped_driver_frames;
errorFrames+=frame->metadata.discarded_error_frames;
if(!captures.push(std::move(*frame)))break;
++captured;
auto gate=status();
if(gate.generation!=generation){captures.clear();detections.clear();results.clear();generation=gate.generation;}
if(!gate.enabled){++skippedIdle;continue;}
if(!captures.pushLatest({std::move(*frame),gate.generation}))break;
}
captures.close();
}
void detectorLoop() {
Stream stream;
while(!stopped) {
auto frame=captures.pop();if(!frame)break;
auto startedAt=monotonicNs();auto boxes=detector->infer(imageView(*frame->image),stream.value);
if(!detections.push({std::move(*frame),std::move(boxes),elapsedMs(startedAt)}))break;
auto pending=captures.pop();if(!pending)break;
if(!eligible(pending->generation))continue;
auto startedAt=monotonicNs();++detectorRuns;
auto boxes=detector->infer(imageView(*pending->frame.image),stream.value);
if(!eligible(pending->generation))continue;
if(!detections.pushLatest({std::move(pending->frame),std::move(boxes),elapsedMs(startedAt),pending->generation}))break;
}
detections.close();
}
void inferenceLoop() {
Stream stream;fangjia::BoxTracker boxesTracker;fangjia::PoseFusionTracker fusion;
std::uint64_t generation=0;
while(!stopped) {
auto detected=detections.pop();if(!detected)break;
if(!eligible(detected->generation))continue;
if(generation!=detected->generation){boxesTracker=fangjia::BoxTracker{};fusion=fangjia::PoseFusionTracker{};generation=detected->generation;}
if(detected->frame.metadata.frame_index>static_cast<std::uint64_t>(std::numeric_limits<int>::max()))throw std::runtime_error("Frame index limit reached; restart pipeline");
auto index=static_cast<int>(detected->frame.metadata.frame_index);
auto& boxes=detected->boxes;
auto frame=imageView(*detected->frame.image);
boxesTracker.update(boxes,index,frame.cols,frame.rows);
auto poseStart=monotonicNs();
++poseRuns;
auto raw=pose->infer(frame,boxes,stream.value);auto isolated=raw;
for(std::size_t p=0;p<boxes.size();++p) {
auto const& box=boxes[p];double area=std::max(double(box.x2-box.x1)*(box.y2-box.y1),1.0);
bool needsIsolation=false;
for(std::size_t q=0;q<boxes.size();++q)if(q!=p&&std::max(double(boxes[q].x2-boxes[q].x1)*(boxes[q].y2-boxes[q].y1),1.0)<0.5*area)needsIsolation=true;
if(!eligible(generation))break;
if(needsIsolation){auto masked=maskOtherPeople(frame,boxes,p);isolated[p]=pose->infer(masked,{box},stream.value).at(0);}
}
if(!eligible(generation))continue;
auto poseElapsed=elapsedMs(poseStart);auto fusionStart=monotonicNs();
auto fused=fusion.update(raw,isolated,index);
auto record=makeRecord(index,raw,isolated,fused,false,false);
@@ -157,9 +188,13 @@ struct CameraPipeline::Impl {
result->image=std::move(detected->frame.image);result->door_mask_pixels=doorPixels;
result->door_mask_calibration_verified=config.door_mask_calibration_verified;
result->timing={detected->elapsed_ms,poseElapsed,elapsedMs(fusionStart),elapsedMs(result->capture.capture_monotonic_ns)};
auto gate=status();
if(!gate.enabled||gate.generation!=generation)continue;
result->inference=gate;
FrameResultPtr immutable=result;
results.publish(std::move(immutable));
{std::lock_guard<std::mutex> lock(stateMutex);totalLatency+=result->timing.capture_to_result_ms;++published;}
outputWriter->write(*result);
}
active=false;
}
@@ -167,12 +202,16 @@ struct CameraPipeline::Impl {
CameraPipeline::CameraPipeline(PipelineConfig config):impl_(std::make_unique<Impl>(std::move(config))){}
CameraPipeline::~CameraPipeline()=default;
void CameraPipeline::start(){impl_->start();}
FrameResultPtr CameraPipeline::latest()const{return impl_->results.load();}
FrameResultPtr CameraPipeline::latest()const{
auto result=impl_->results.load();auto gate=impl_->status();
return result&&gate.enabled&&result->inference.generation==gate.generation?result:FrameResultPtr{};
}
InferenceStatus CameraPipeline::inferenceStatus()const{return impl_->status();}
bool CameraPipeline::running()const noexcept{return impl_->active.load();}
void CameraPipeline::requestStop()noexcept{impl_->stop();}
void CameraPipeline::join(){impl_->joinWorkers();{std::lock_guard<std::mutex> lock(impl_->stateMutex);impl_->camera.reset();}impl_->rethrowFailure();}
void CameraPipeline::join(){impl_->joinWorkers();{std::lock_guard<std::mutex> lock(impl_->stateMutex);impl_->camera.reset();impl_->can.reset();}impl_->rethrowFailure();}
PipelineStats CameraPipeline::stats()const {
std::lock_guard<std::mutex> lock(impl_->stateMutex);auto count=impl_->published.load();
return {impl_->captured.load(),count,impl_->gaps.load(),impl_->errorFrames.load(),count?impl_->totalLatency/count:0};
return {impl_->captured.load(),count,impl_->gaps.load(),impl_->errorFrames.load(),count?impl_->totalLatency/count:0,impl_->detectorRuns.load(),impl_->poseRuns.load(),impl_->skippedIdle.load()};
}
}
+342
View File
@@ -0,0 +1,342 @@
// can_comm.cpp - USBCANFD CAN/CANFD 收发封装实现
//
// 代码迁移自 vision-anti-pinch_bak/main.cpp 的 CAN 模块部分。
#include "can_comm.h"
#include <cstdio>
#include <cstring>
#include <pthread.h>
#include <atomic>
#include <unistd.h>
#include "zcan.h"
/* =====================================================================
* 类型/宏/全局变量
* ===================================================================== */
typedef uint32_t UINT;
typedef uint32_t U32;
#define msleep(ms) usleep((ms) * 1000)
#define USBCANFD 33
#define MAX_CHANNELS 2 // 最大通道数量
#define RX_WAIT_TIME 100
#define RX_BUFF_SIZE 1000
// 接收线程上下文
typedef struct
{
int dev_type; // 设备类型
int dev_idx; // 设备索引
int chn_idx; // 通道号
int total; // 接收总数
std::atomic<int> stop{0}; // 线程结束标志
} THREAD_CTX;
/* CAN 运行标志, 由信号处理函数(Ctrl+C)或外部置位 */
static volatile bool g_can_running = true;
static std::atomic<int> g_receive_count{0};
/* ===== 仅供 can_start/can_stop 共享的文件内静态变量 ===== */
static int s_DevType = USBCANFD;
static int s_DevIdx = 0;
static THREAD_CTX s_rx_ctx[MAX_CHANNELS];
static pthread_t s_rx_threads[MAX_CHANNELS];
static bool s_thread_started[MAX_CHANNELS]{};
static bool s_can_opened = false;
namespace CanComm {
bool is_open() { return s_can_opened; }
int receive_count() { return g_receive_count.load(); }
/* ===== 报文解析回调 (由 main.cpp 通过 set_msg_callback 注册) ===== */
static MsgCallback s_msg_cb = nullptr;
void set_msg_callback(MsgCallback cb) { s_msg_cb = cb; }
} // namespace CanComm
/* =====================================================================
* 帧构建与收发
* ===================================================================== */
namespace CanComm {
// 构建 CAN 帧
static void construct_can_frame(ZCAN_20_MSG *can_msg, UINT id, int chn_idx, int pad)
{
memset(can_msg, 0, sizeof(ZCAN_20_MSG));
can_msg->hdr.inf.txm = 0; // 0-正常发送
can_msg->hdr.inf.fmt = 0; // 0-CAN
can_msg->hdr.inf.sdf = 0; // 0-数据帧, 1-远程帧
can_msg->hdr.inf.sef = 0; // 0-标准帧, 1-扩展帧
can_msg->hdr.id = id; // ID
can_msg->hdr.chn = chn_idx; // 通道
can_msg->hdr.len = 8; // 数据长度
if (pad > 0)
{
can_msg->hdr.pad = pad; // 发送后延迟 pad ms
can_msg->hdr.inf.qsend = 1; // 队列发送帧,仅判断首帧
can_msg->hdr.inf.qsend_100us = 0; // 队列发送单位,0-ms,1-100us
}
for (int i = 0; i < can_msg->hdr.len; i++)
can_msg->dat[i] = i;
}
// 构建 CANFD 帧
static void construct_canfd_frame(ZCAN_FD_MSG *canfd_msg, UINT id, int chn_idx, int pad)
{
memset(canfd_msg, 0, sizeof(ZCAN_FD_MSG));
canfd_msg->hdr.inf.txm = 0; // 0-正常发送
canfd_msg->hdr.inf.fmt = 1; // 0-CAN, 1-CANFD
canfd_msg->hdr.inf.sdf = 0; // 0-数据帧 CANFD只有数据帧!
canfd_msg->hdr.inf.sef = 1; // 0-标准帧, 1-扩展帧
canfd_msg->hdr.inf.brs = 1; // canfd 加速
canfd_msg->hdr.id = id; // ID
canfd_msg->hdr.chn = chn_idx; // 通道
canfd_msg->hdr.len = 64; // 数据长度
if (pad > 0)
{
canfd_msg->hdr.pad = 100; // 发送后延迟100ms
canfd_msg->hdr.inf.qsend = 1; // 队列发送帧,仅判断首帧
canfd_msg->hdr.inf.qsend_100us = 0; // 队列发送单位,0-ms,1-100us
}
for (int i = 0; i < canfd_msg->hdr.len; i++)
canfd_msg->dat[i] = i;
}
// 分发报文到 main.cpp 注册的解析回调
static inline void dispatch_msg(uint32_t ts, uint32_t msg_id, const uint8_t *data, uint32_t len)
{
if (s_msg_cb)
s_msg_cb(ts, msg_id, data, len);
}
// 接收线程
static void *rx_thread(void *data)
{
THREAD_CTX *ctx = (THREAD_CTX *)data;
int DevType = ctx->dev_type;
int DevIdx = ctx->dev_idx;
int chn_idx = ctx->chn_idx;
ZCAN_20_MSG can_data[RX_BUFF_SIZE];
ZCAN_FD_MSG canfd_data[RX_BUFF_SIZE];
while (!ctx->stop)
{
memset(can_data, 0, sizeof(can_data));
memset(canfd_data, 0, sizeof(canfd_data));
int rcount = VCI_Receive(DevType, DevIdx, chn_idx, can_data, RX_BUFF_SIZE, RX_WAIT_TIME); // CAN
for (int i = 0; i < rcount; ++i)
{
if (can_data[i].hdr.inf.sdf == 0)
{ // 数据帧
dispatch_msg(can_data->hdr.ts, can_data[i].hdr.id, can_data[i].dat, can_data[i].hdr.len);
}
}
ctx->total += rcount;
rcount = VCI_ReceiveFD(DevType, DevIdx, chn_idx, canfd_data, RX_BUFF_SIZE, RX_WAIT_TIME); // CANFD
for (int i = 0; i < rcount; ++i)
{
dispatch_msg(canfd_data->hdr.ts, canfd_data[i].hdr.id, canfd_data[i].dat, canfd_data[i].hdr.len);
}
ctx->total += rcount;
msleep(10);
}
g_receive_count = ctx->total;
printf("chn: %d receive %d\n", chn_idx, ctx->total);
pthread_exit(0);
}
// 合并接收线程
static void *rx_merge_thread(void *data)
{
THREAD_CTX *ctx = (THREAD_CTX *)data;
int DevType = ctx->dev_type;
int DevIdx = ctx->dev_idx;
ZCANDataObj obj_data[RX_BUFF_SIZE];
while (!ctx->stop)
{
memset(obj_data, 0, sizeof(obj_data));
int rcount = VCI_ReceiveData(DevType, DevIdx, 0, obj_data, RX_BUFF_SIZE, RX_WAIT_TIME); // 第三个参数固定为0就好
for (int i = 0; i < rcount; ++i)
{
if (obj_data[i].dataType == ZCAN_DT_ZCAN_CAN_DATA)
{ // CAN
ZCAN_20_MSG *can_data = &obj_data[i].data.zcanCANData;
if (can_data->hdr.inf.sdf == 0)
{ // 数据帧
dispatch_msg(can_data->hdr.ts, can_data->hdr.id, can_data->dat, can_data->hdr.len);
}
}
else if (obj_data[i].dataType == ZCAN_DT_ZCAN_CANFD_DATA)
{ // CANFD
ZCAN_FD_MSG *canfd_data = &obj_data[i].data.zcanCANFDData;
dispatch_msg(canfd_data->hdr.ts, canfd_data->hdr.id, canfd_data->dat, canfd_data->hdr.len);
}
}
ctx->total += rcount;
msleep(10);
}
g_receive_count = ctx->total;
printf("merge receive %d\n", ctx->total);
pthread_exit(0);
}
/*
* 初始化并启动 CAN 收发。
* 返回 true 成功; false 失败(主流程可继续仅取图)。
*/
bool can_start()
{
g_can_running = true;
int IsMerge = 1; // 是否合并接收
// 打开设备
if (!VCI_OpenDevice(s_DevType, s_DevIdx, 0))
{
printf("Open device fail\n");
return false;
}
s_can_opened = true;
printf("Open device success\n");
// 初始化,启动通道
for (int i = 0; i < MAX_CHANNELS; i++)
{
ZCAN_INIT init{}; // 波特率结构体,数据根据zcanpro的波特率计算器得出
init.clk = 60000000; // clock: 60M(V1.01) 80M(V1.03即以上)
init.mode = 0; // 0-正常
init.aset.tseg1 = 14; // 仲裁域 500kbps
init.aset.tseg2 = 3;
init.aset.sjw = 2;
init.aset.smp = 0;
init.aset.brp = 5;
init.dset.tseg1 = 10; // 数据域 2000kbps
init.dset.tseg2 = 2;
init.dset.sjw = 2;
init.dset.smp = 0;
init.dset.brp = 1;
if (!VCI_InitCAN(s_DevType, s_DevIdx, i, &init)) // 初始化通道
{
printf("InitCAN(%d) fail\n", i);
can_stop();return false;
}
printf("InitCAN(%d) success\n", i);
U32 on = 1;
if (!VCI_SetReference(s_DevType, s_DevIdx, i, CMD_CAN_TRES, &on)) // 终端电阻
{
printf("CMD_CAN_TRES fail\n");
}
if (i == 0)
{
if (!VCI_SetReference(s_DevType, s_DevIdx, i, ZCAN_CMD_SET_CHNL_RECV_MERGE, &IsMerge)) // 合并接收
{
printf("ZCAN_CMD_SET_CHNL_RECV_MERGE fail\n");
}
}
if (!VCI_StartCAN(s_DevType, s_DevIdx, i)) // 启动通道
{
printf("StartCAN(%d) fail\n", i);
can_stop();return false;
}
printf("StartCAN(%d) success\n", i);
s_rx_ctx[i].dev_type = s_DevType;
s_rx_ctx[i].dev_idx = s_DevIdx;
s_rx_ctx[i].chn_idx = i;
s_rx_ctx[i].total = 0;
s_rx_ctx[i].stop = 0;
if (IsMerge && i != 0)continue;
if (IsMerge && i == 0)
{
s_thread_started[i]=pthread_create(&s_rx_threads[i], NULL, rx_merge_thread, &s_rx_ctx[i])==0; // 合并接收线程
}
else
{
s_thread_started[i]=pthread_create(&s_rx_threads[i], NULL, rx_thread, &s_rx_ctx[i])==0; // 创建接收线程
}
if(!s_thread_started[i]){can_stop();return false;}
}
return true;
}
/*
* 停止 CAN 接收并关闭设备。
*/
void can_stop()
{
if (!s_can_opened)
return;
g_can_running = false;
for (int i = 0; i < MAX_CHANNELS; i++)
{
s_rx_ctx[i].stop = 1;
if(s_thread_started[i]){pthread_join(s_rx_threads[i], NULL);s_thread_started[i]=false;} // 等待接收线程退出
if (!VCI_ResetCAN(s_DevType, s_DevIdx, i)) // 复位通道
printf("ResetCAN(%d) fail\n", i);
else
printf("ResetCAN(%d) success!\n", i);
}
if (!VCI_CloseDevice(s_DevType, s_DevIdx))
printf("CloseDevice fail\n");
else
printf("CloseDevice success\n");
s_can_opened = false;
}
uint32_t can_send(int chn_idx, uint32_t id, const uint8_t data[8])
{
if (!s_can_opened)
return 0;
ZCAN_20_MSG msg;
construct_can_frame(&msg, id, chn_idx, 0);
for (int i = 0; i < 8; ++i)
msg.dat[i] = data[i];
return VCI_Transmit(s_DevType, s_DevIdx, chn_idx, &msg, 1);
}
uint32_t canfd_send(int chn_idx, uint32_t id, const uint8_t data[64])
{
if (!s_can_opened)
return 0;
ZCAN_FD_MSG msg;
construct_canfd_frame(&msg, id, chn_idx, 0);
for (int i = 0; i < 64; ++i)
msg.dat[i] = data[i];
return VCI_TransmitFD(s_DevType, s_DevIdx, chn_idx, &msg, 1);
}
} // namespace CanComm
+54
View File
@@ -0,0 +1,54 @@
#include "can_input.hpp"
#include "can_comm.h"
#include "camera.hpp"
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <stdexcept>
namespace cms {
struct CanInput::Impl {
static std::mutex owner_mutex;
static Impl* owner;
std::mutex mutex;
std::condition_variable wake;
LazyGate gate;
std::atomic<bool> stopped{false};
std::thread worker;
static void receive(int64_t,uint32_t id,uint8_t const* data,uint32_t size) {
std::lock_guard<std::mutex> lock(owner_mutex);
if(owner){std::lock_guard<std::mutex> stateLock(owner->mutex);owner->gate.receive(id,data,size,monotonicNs());}
}
explicit Impl(LazyGateConfig config):gate(config) {
{std::lock_guard<std::mutex> lock(owner_mutex);if(owner)throw std::logic_error("Only one CAN receiver per process");owner=this;}
CanComm::set_msg_callback(receive);
try {worker=std::thread([this] {
bool opened=false;
auto attempted=monotonicNs();
while(!stopped) {
auto now=monotonicNs();
auto status=snapshot();
// Retry unplugged/stalled adapters while CAN fallback inference continues.
if(!opened||(now-attempted>5000000000ULL&&!status.can_valid)) {
if(opened)CanComm::can_stop();
opened=CanComm::can_start();attempted=monotonicNs();
}
std::unique_lock<std::mutex> lock(mutex);
wake.wait_for(lock,std::chrono::seconds(1),[this]{return stopped.load();});
}
if(opened)CanComm::can_stop();
});}catch(...){CanComm::set_msg_callback(nullptr);std::lock_guard<std::mutex> lock(owner_mutex);owner=nullptr;throw;}
}
~Impl() {
stopped=true;wake.notify_all();if(worker.joinable())worker.join();
CanComm::set_msg_callback(nullptr);
std::lock_guard<std::mutex> lock(owner_mutex);owner=nullptr;
}
InferenceStatus snapshot() {std::lock_guard<std::mutex> lock(mutex);return gate.snapshot(monotonicNs());}
};
std::mutex CanInput::Impl::owner_mutex;
CanInput::Impl* CanInput::Impl::owner=nullptr;
CanInput::CanInput(LazyGateConfig config):impl_(std::make_unique<Impl>(config)){}
CanInput::~CanInput()=default;
InferenceStatus CanInput::snapshot(){return impl_->snapshot();}
}
+6
View File
@@ -19,6 +19,12 @@ PipelineConfig loadConfig(std::filesystem::path const& path) {
auto const& door=j.at("door");config.door_mask=resolve(door.at("mask"));
config.warning_relative=door.at("warning_relative");config.release_relative=door.at("release_relative");
config.door_mask_calibration_verified=door.at("calibration_verified");
if(j.contains("lazy_inference")) {
auto const& gate=j.at("lazy_inference");
config.lazy_gate.threshold_percent=gate.value("threshold_percent",15);
config.lazy_gate.can_timeout_ms=gate.value("can_timeout_ms",1000);
config.lazy_gate.motion_timeout_ms=gate.value("motion_timeout_ms",300);
}
return config;
}
}
+41
View File
@@ -0,0 +1,41 @@
#include "lazy_gate.hpp"
#include <stdexcept>
namespace cms {
LazyGate::LazyGate(LazyGateConfig config):config_(config) {
if(config.threshold_percent<1||config.threshold_percent>100||config.can_timeout_ms<=0||config.motion_timeout_ms<=0||config.motion_timeout_ms>config.can_timeout_ms)
throw std::invalid_argument("Invalid lazy inference gate configuration");
}
void LazyGate::receive(std::uint32_t id,std::uint8_t const* data,std::size_t size,std::uint64_t now) {
if(id!=0x391||size<4||!data)return;
snapshot(now); // Account for a timeout before receiving a new sample.
bool continuity=have_sample_&&now>=last_sample_&&now-last_sample_<=std::uint64_t(config_.can_timeout_ms)*1000000;
for(int side=0;side<2;++side) {
int index=side*2;
if(!continuity){state_.direction[side]=0;last_change_[side]=0;}
else if(data[index]!=state_.positions[index]) {
state_.direction[side]=data[index]>state_.positions[index]?1:-1;
last_change_[side]=now;
}
}
for(int i=0;i<4;++i)state_.positions[i]=data[i];
have_sample_=data[0]<=100&&data[2]<=100;
if(!have_sample_){state_.direction={};last_change_={};}
last_sample_=now;
snapshot(now);
}
InferenceStatus LazyGate::snapshot(std::uint64_t now) {
state_.sample_monotonic_ns=now;
state_.can_age_ms=have_sample_&&now>=last_sample_?(now-last_sample_)*1e-6:-1;
state_.can_valid=state_.can_age_ms>=0&&state_.can_age_ms<=config_.can_timeout_ms;
bool triggers[2]{};
for(int i=0;i<2;++i) {
if(!state_.can_valid||now<last_change_[i]||now-last_change_[i]>=std::uint64_t(config_.motion_timeout_ms)*1000000)state_.direction[i]=0;
triggers[i]=state_.can_valid&&state_.positions[i*2]<config_.threshold_percent&&state_.direction[i]!=0;
}
state_.left_front_trigger=triggers[0];state_.left_rear_trigger=triggers[1];
bool enabled=!state_.can_valid||triggers[0]||triggers[1];
if(enabled!=state_.enabled){++state_.generation;state_.enabled=enabled;}
state_.reason=!state_.can_valid?"CAN_UNAVAILABLE":enabled?"LEFT_DOOR_MOVING_BELOW_THRESHOLD":"IDLE";
return state_;
}
}
+58 -31
View File
@@ -2,42 +2,69 @@
#include "config.hpp"
#include "stop_signals.hpp"
#include "visualizer.hpp"
#include <functional>
#include <opencv2/core.hpp>
#include <atomic>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <thread>
int main(int argc,char** argv)try {
auto root=std::filesystem::canonical("/proc/self/exe").parent_path().parent_path();
auto configPath=root/"config/camera.json";
bool visualize=false;
std::map<std::string,std::function<void(int&)>> arguments{
{"--visualize",[&](int&){visualize=true;}},
{"--config",[&](int&i){if(++i>=argc)throw std::invalid_argument("--config needs a file");configPath=argv[i];}},
{"--help",[](int&){std::cout<<"camera_risk [--config FILE] [--visualize]\nContinuous camera inference; latest FrameResult stays in memory. Stop with Ctrl+C or SIGTERM.\n";std::exit(0);}}
};
for(int i=1;i<argc;++i){auto handler=arguments.find(argv[i]);if(handler==arguments.end())throw std::invalid_argument("Unknown option: "+std::string(argv[i]));handler->second(i);}
cms::CameraPipeline pipeline(cms::loadConfig(configPath));
cms::StopSignals signals(pipeline);
pipeline.start();
/*
* 下游使用示例:在下游自己的线程/调用位置读取最新结果。
* 首帧尚未完成时 result 为空;读取不会影响取图和推理。
*
* auto result = pipeline.latest();
* if (result) {
* const auto& risk = result->risk;
* const auto& people = result->persons;
* // result->frame_index / track_ids / boxes
* // result->image / door_mask_pixels / capture / timing
* // people[i].selected / smoothed / temporal:各 133 个关键点
* }
*/
// Optional observer. Closing/failing the window never requests inference stop.
if(visualize) {
try {cms::runLocalVisualization(pipeline);}
catch(std::exception const& e){std::cerr<<"Visualization unavailable; inference continues: "<<e.what()<<std::endl;}
cms::OutputOptions output;output.directory=root/"output";
bool visualize=false;double duration=0;
auto value=[&](int& i){if(++i>=argc)throw std::invalid_argument("Missing option value");return std::string(argv[i]);};
for(int i=1;i<argc;++i) {
std::string arg=argv[i];
if(arg=="--config")configPath=value(i);
else if(arg=="--save-original")output.original=true;
else if(arg=="--save-annotated")output.annotated=true;
else if(arg=="--save-results")output.results=true;
else if(arg=="--visualize")visualize=true;
else if(arg=="--duration") {duration=std::stod(value(i));if(!std::isfinite(duration)||duration<0)throw std::invalid_argument("Invalid duration");}
else if(arg=="--help") {
std::cout<<"camera_risk [--save-original] [--save-annotated] [--save-results] [--config FILE] [--visualize] [--duration SECONDS]\n"
<<"C++ lazy inference; resident warm engines, continuous CAN reception.\n";return 0;
}else throw std::invalid_argument("Unknown option: "+arg);
}
pipeline.join();
cv::setNumThreads(2);
auto config=cms::loadConfig(configPath);config.output=output;
cms::CameraPipeline pipeline(config);
cms::StopSignals signals(pipeline);pipeline.start();
auto started=std::chrono::steady_clock::now();std::atomic<bool> monitorStop{false};
std::thread monitor([&] {
auto reported=started-std::chrono::seconds(5);std::uint64_t generation=0;
while(!monitorStop&&pipeline.running()) {
auto now=std::chrono::steady_clock::now();
if(duration>0&&std::chrono::duration<double>(now-started).count()>=duration){pipeline.requestStop();break;}
auto state=pipeline.inferenceStatus();
if(state.generation!=generation||now-reported>=std::chrono::seconds(5)) {
auto stats=pipeline.stats();
std::cout<<"inference="<<(state.enabled?"RUNNING":"IDLE")<<" reason="<<state.reason
<<" left_front="<<state.positions[0]<<" left_rear="<<state.positions[2]
<<" generation="<<state.generation<<" detector_runs="<<stats.detector_runs
<<" pose_runs="<<stats.pose_runs<<" results="<<stats.published<<std::endl;
generation=state.generation;reported=now;
}
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
});
try {
if(visualize) {
if(!std::getenv("DISPLAY")&&!std::getenv("WAYLAND_DISPLAY"))std::cerr<<"No display session; running without visualization.\n";
else try {cms::runLocalVisualization(pipeline);}
catch(std::exception const& e){std::cerr<<"Visualization unavailable: "<<e.what()<<std::endl;}
}
// Downstream reads: pipeline.inferenceStatus(); auto result=pipeline.latest();
// result is null while idle, starting, or awaiting the new generation's first frame.
pipeline.join();
}catch(...){monitorStop=true;pipeline.requestStop();monitor.join();throw;}
monitorStop=true;monitor.join();auto stats=pipeline.stats();
auto elapsed=std::chrono::duration<double>(std::chrono::steady_clock::now()-started).count();
std::cout<<"captured="<<stats.captured<<" results="<<stats.published<<" detector_runs="<<stats.detector_runs
<<" pose_runs="<<stats.pose_runs<<" skipped_idle="<<stats.skipped_idle<<" elapsed_seconds="<<elapsed
<<" capture_to_result_ms="<<stats.mean_capture_to_result_ms<<std::endl;
return 0;
}catch(std::exception const& e){std::cerr<<"Camera pipeline failed: "<<e.what()<<std::endl;return 1;}
+69
View File
@@ -0,0 +1,69 @@
#include "output_writer.hpp"
#include "serialization.hpp"
#include "visualizer.hpp"
#include <opencv2/imgcodecs.hpp>
#include <chrono>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <unistd.h>
namespace cms {
namespace {
void writeFile(std::filesystem::path const& path,char const* data,std::size_t size) {
auto temporary=path;temporary+=".tmp";
try {
std::ofstream file;file.exceptions(std::ios::failbit|std::ios::badbit);
file.open(temporary,std::ios::binary|std::ios::trunc);
file.write(data,static_cast<std::streamsize>(size));file.close();
std::filesystem::rename(temporary,path);
} catch(...) {
std::error_code ignored;std::filesystem::remove(temporary,ignored);
throw std::runtime_error("Cannot save output file: "+path.string());
}
}
void writeJpeg(std::filesystem::path const& path,cv::Mat const& image) {
std::vector<unsigned char> encoded;
if(!cv::imencode(".jpg",image,encoded,{cv::IMWRITE_JPEG_QUALITY,95}))
throw std::runtime_error("JPEG encoding failed: "+path.string());
writeFile(path,reinterpret_cast<char const*>(encoded.data()),encoded.size());
}
}
OutputWriter::OutputWriter(OutputOptions options):options_(std::move(options)) {
if(!options_.enabled())return;
if(options_.directory.empty())options_.directory=std::filesystem::current_path()/"output";
for(auto name:{"original","annotated","results"})std::filesystem::create_directories(options_.directory/name);
// Nanosecond wall-clock + PID prevents a later run from overwriting earlier frames.
auto now=std::chrono::system_clock::now();auto time=std::chrono::system_clock::to_time_t(now);
std::tm utc{};gmtime_r(&time,&utc);
auto ns=std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count()%1000000000;
std::ostringstream id;id<<std::put_time(&utc,"%Y%m%dT%H%M%S")<<'_'<<std::setw(9)<<std::setfill('0')<<ns<<"Z_"<<getpid();
runId_=id.str();
std::cout<<"Saving selected outputs to "<<options_.directory<<std::endl;
}
void OutputWriter::write(FrameResult const& result) {
if(!options_.enabled())return;
std::ostringstream name;name<<runId_<<"_frame_"<<std::setw(10)<<std::setfill('0')<<result.frame_index;
auto stem=name.str();Json original=nullptr,annotated=nullptr;
if(options_.original) {
if(!result.image)throw std::runtime_error("Cannot save original image: frame is missing");
auto const& image=*result.image;
if(image.width<=0||image.height<=0||image.stride<image.width*3||image.pixels.size()<std::size_t(image.stride)*image.height)
throw std::runtime_error("Cannot save malformed original image");
cv::Mat view(image.height,image.width,CV_8UC3,const_cast<std::uint8_t*>(image.pixels.data()),image.stride);
auto path=std::filesystem::path("original")/(stem+".jpg");writeJpeg(options_.directory/path,view);original=path.generic_string();
}
if(options_.annotated) {
auto path=std::filesystem::path("annotated")/(stem+".jpg");
writeJpeg(options_.directory/path,renderResult(result));annotated=path.generic_string();
}
if(options_.results) {
auto document=serializeResult(result);document["schema_version"]=1;document["run_id"]=runId_;
document["images"]={{"original",original},{"annotated",annotated}};
auto contents=document.dump(2)+"\n";
writeFile(options_.directory/"results"/(stem+".json"),contents.data(),contents.size());
}
}
}
+80
View File
@@ -0,0 +1,80 @@
#include "visualizer.hpp"
#include <opencv2/imgproc.hpp>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <sstream>
#include <stdexcept>
namespace cms {
namespace {
cv::Point point(PosePoint const& p){return {int(std::nearbyint(p.x)),int(std::nearbyint(p.y))};}
cv::Scalar color(std::string const& level) {
if(level=="danger")return {40,40,255};
if(level=="warning")return {0,160,255};
if(level=="watch")return {0,240,240};
if(level=="clear")return {80,220,90};
return {190,190,190};
}
void segment(cv::Mat& image,cv::Point a,cv::Point b,cv::Scalar c,int thickness,bool dashed) {
if(!dashed){cv::line(image,a,b,c,thickness,cv::LINE_AA);return;}
cv::Point2d d=b-a;double length=cv::norm(d);if(length<1)return;d*=1.0/length;
for(double x=0;x<length;x+=14)cv::line(image,cv::Point2d(a)+d*x,cv::Point2d(a)+d*std::min(x+8,length),c,thickness,cv::LINE_AA);
}
}
cv::Mat renderResult(FrameResult const& r) {
if(!r.image)throw std::invalid_argument("Result has no frame image");
auto const& image=*r.image;
if(image.width<=0||image.height<=0||image.stride<image.width*3||image.pixels.size()<std::size_t(image.stride)*image.height)
throw std::invalid_argument("Malformed result image");
cv::Mat view(image.height,image.width,CV_8UC3,const_cast<std::uint8_t*>(image.pixels.data()),image.stride);
auto canvas=view.clone();
if(r.door_mask_pixels) {
auto const& m=*r.door_mask_pixels;
if(m.width!=image.width||m.height!=image.height||m.stride<m.width||m.pixels.size()<std::size_t(m.stride)*m.height)
throw std::invalid_argument("Result mask does not match image");
cv::Mat mask(m.height,m.width,CV_8U,const_cast<std::uint8_t*>(m.pixels.data()),m.stride);
auto tint=canvas.clone();tint.setTo(cv::Scalar(255,180,0),mask);
cv::addWeighted(canvas,0.75,tint,0.25,0,canvas);
std::vector<std::vector<cv::Point>> contours;cv::findContours(mask.clone(),contours,cv::RETR_EXTERNAL,cv::CHAIN_APPROX_SIMPLE);
cv::drawContours(canvas,contours,-1,{255,180,0},2);
}
for(auto const& person:r.persons) {
auto const& b=person.box;
cv::rectangle(canvas,{int(b[0]),int(b[1])},{int(b[2]),int(b[3])},{0,220,255},2);
std::ostringstream text;text<<"ID "<<person.track_id<<" "<<std::fixed<<std::setprecision(2)<<person.confidence;
cv::putText(canvas,text.str(),{int(b[0])+4,std::max(180,int(b[1])+25)},cv::FONT_HERSHEY_SIMPLEX,.8,{0,220,255},2,cv::LINE_AA);
for(int s=0;s<2;++s) {
auto const& state=person.hands[s];auto const& k=person.selected;
cv::Scalar arm=s==0?cv::Scalar(40,40,255):cv::Scalar(30,230,30);
for(auto e:std::vector<std::pair<int,int>>{{5+s,7+s},{7+s,9+s}}) {
segment(canvas,point(k[e.first]),point(k[e.second]),state.limb_consensus?arm:cv::Scalar(0,140,255),3,!state.limb_consensus);
cv::circle(canvas,point(k[e.second]),4,arm,-1,cv::LINE_AA);
}
if(state.state=="rejected")continue;
int start=s==0?91:112;
auto hand=state.distance_eligible?(s==0?cv::Scalar(255,80,220):cv::Scalar(255,190,40)):cv::Scalar(0,140,255);
for(int f=0;f<5;++f)for(int j=0;j<4;++j){int a=j==0?start:start+f*4+j;int b=start+1+f*4+j;segment(canvas,point(k[a]),point(k[b]),hand,2,!state.distance_eligible);}
for(int j=0;j<21;++j)cv::circle(canvas,point(k[start+j]),3,hand,-1,cv::LINE_AA);
}
}
for(auto const& h:r.hand_regions) {
std::vector<cv::Point> points,hull;for(auto const& p:h.keypoints)points.push_back(point(p));cv::convexHull(points,hull);
auto risk=std::find_if(r.risk.regions.begin(),r.risk.regions.end(),[&](auto const& item){return item.track_id==h.track_id&&item.side==h.side;});
auto c=color(risk==r.risk.regions.end()?"unavailable":risk->risk_level);
cv::polylines(canvas,std::vector<std::vector<cv::Point>>{hull},true,c,2,cv::LINE_AA);
}
for(auto const& region:r.risk.regions)if(region.source=="YOLO_BODY_BOX_FALLBACK") {
auto it=std::find(r.track_ids.begin(),r.track_ids.end(),region.track_id);
if(it!=r.track_ids.end()){auto const& b=r.boxes.at(it-r.track_ids.begin());cv::putText(canvas,"BODY FALLBACK",{int(b[0])+4,int(b[1])+50},cv::FONT_HERSHEY_SIMPLEX,.65,{0,160,255},2,cv::LINE_AA);}
}
cv::rectangle(canvas,{0,0},{canvas.cols,148},{20,24,30},-1);
std::ostringstream headline;headline<<"RISK ";if(r.risk.risk_score)headline<<std::fixed<<std::setprecision(3)<<*r.risk.risk_score;else headline<<"N/A";headline<<" "<<r.risk.risk_level;
cv::putText(canvas,headline.str(),{24,40},cv::FONT_HERSHEY_SIMPLEX,1.0,color(r.risk.risk_level),2,cv::LINE_AA);
std::ostringstream status;status<<"camera frame "<<r.frame_index<<" | seq "<<r.capture.driver_sequence<<" | latency "<<std::fixed<<std::setprecision(1)<<r.timing.capture_to_result_ms<<" ms";
cv::putText(canvas,status.str(),{24,82},cv::FONT_HERSHEY_SIMPLEX,.72,{235,235,235},2,cv::LINE_AA);
std::string calibration=r.door_mask_calibration_verified?"door calibration verified":"door calibration NOT verified (bring-up)";
cv::putText(canvas,calibration+" | q/Esc: close preview",{24,123},cv::FONT_HERSHEY_SIMPLEX,.70,{0,190,255},2,cv::LINE_AA);
return canvas;
}
}
+47
View File
@@ -0,0 +1,47 @@
#include "serialization.hpp"
namespace cms {
namespace {
template<class T> Json optional(std::optional<T> const& x){return x?Json(*x):Json(nullptr);}
Json riskJson(RiskResult const& r) {
Json out={{"status",r.status},{"risk_score",optional(r.risk_score)},{"risk_level",r.risk_level},{"decision",r.decision},{"source",optional(r.source)},{"track_id",optional(r.track_id)},{"side",optional(r.side)},{"regions",Json::array()}};
if(!r.door_mask.status.empty()) {
auto const& d=r.door_mask;out["door_mask"]={{"status",d.status},{"annotation_index",d.annotation_index},{"path",optional(d.path)},{"format",d.format},{"area_px",d.area_px}};
out["warning_distance_relative"]=r.warning_distance_relative;out["release_distance_relative"]=r.release_distance_relative;
}
for(auto const& v:r.regions) {
Json j={{"status",v.status},{"risk_score",optional(v.risk_score)},{"risk_level",v.risk_level},{"decision",v.decision},{"source",v.source},{"track_id",v.track_id},{"side",v.side}};
if(v.state)j["state"]=*v.state;
if(v.region_area_px) {
j["region_area_px"]=optional(v.region_area_px);j["door_overlap_px"]=optional(v.door_overlap_px);j["region_overlap_ratio"]=optional(v.region_overlap_ratio);
j["min_distance_px"]=optional(v.min_distance_px);j["region_scale_px"]=optional(v.region_scale_px);j["distance_relative"]=optional(v.distance_relative);
}
out["regions"].push_back(std::move(j));
}
return out;
}
}
Json serializeResult(FrameResult const& r) {
Json out={{"frame_index",r.frame_index},{"track_ids",r.track_ids},{"boxes",r.boxes},{"skeletons",Json::array()},{"risk",riskJson(r.risk)}};
for(std::size_t i=0;i<r.persons.size();++i) {
auto const& p=r.persons[i];Json points=Json::array(),metadata=Json::object();for(auto const& k:p.selected)points.push_back({rounded(k.x,3),rounded(k.y,3)});
for(int s=0;s<2;++s){auto const& h=p.hands[s];metadata[s==0?"left":"right"]={{"limb_state",h.limb_consensus?"consensus":"inferred"},{"hand_source",h.source},{"hand_state",h.state},{"distance_eligible",h.distance_eligible},{"alarm_eligible",h.alarm_eligible}};}
out["skeletons"].push_back({{"track_id",p.track_id},{"person_index",i},{"layout","coco_wholebody_133"},{"keypoints",points},{"metadata",metadata}});
}
auto const& state=r.inference;
out["inference"]={{"models_ready",state.models_ready},{"enabled",state.enabled},{"can_valid",state.can_valid},{"positions",state.positions},{"direction",state.direction},{"left_front_trigger",state.left_front_trigger},{"left_rear_trigger",state.left_rear_trigger},{"generation",state.generation},{"can_age_ms",state.can_age_ms},{"reason",state.reason}};
for(std::size_t i=0;i<r.persons.size();++i) {
auto const& p=r.persons[i];auto& skeleton=out["skeletons"][i];
skeleton["confidence"]=p.confidence;skeleton["keypoint_scores"]=Json::array();
skeleton["smoothed_keypoints"]=Json::array();skeleton["temporal_keypoints"]=Json::array();
for(std::size_t k=0;k<p.selected.size();++k) {
skeleton["keypoint_scores"].push_back(p.selected[k].score);
auto const& a=p.smoothed[k];auto const& b=p.temporal[k];
skeleton["smoothed_keypoints"].push_back({a.x,a.y,a.score});
skeleton["temporal_keypoints"].push_back({b.x,b.y,b.score});
}
}
out["timing"]={{"detector_ms",r.timing.detector_ms},{"pose_ms",r.timing.pose_ms},{"fusion_risk_ms",r.timing.fusion_risk_ms},{"capture_to_result_ms",r.timing.capture_to_result_ms}};
out["capture"]={{"sequence",r.capture.driver_sequence},{"capture_monotonic_ms",r.capture.capture_monotonic_ns*1e-6},{"driver_timestamp_ms",r.capture.driver_timestamp_ns*1e-6}};
out["capture_to_result_ms"]=rounded(r.timing.capture_to_result_ms,3);out["calibration_verified"]=r.door_mask_calibration_verified;return out;
}
}
+8 -77
View File
@@ -3,84 +3,8 @@
#include <opencv2/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/imgproc.hpp>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <sstream>
#include <stdexcept>
namespace cms {
namespace {
constexpr char windowName[]="CMS Camera Risk";
cv::Point point(PosePoint const& p){return {int(std::nearbyint(p.x)),int(std::nearbyint(p.y))};}
cv::Scalar color(std::string const& level) {
if(level=="danger")return {40,40,255};
if(level=="warning")return {0,160,255};
if(level=="watch")return {0,240,240};
if(level=="clear")return {80,220,90};
return {190,190,190};
}
void segment(cv::Mat& image,cv::Point a,cv::Point b,cv::Scalar c,int thickness,bool dashed) {
if(!dashed){cv::line(image,a,b,c,thickness,cv::LINE_AA);return;}
cv::Point2d d=b-a;double length=cv::norm(d);if(length<1)return;d*=1.0/length;
for(double x=0;x<length;x+=14)cv::line(image,cv::Point2d(a)+d*x,cv::Point2d(a)+d*std::min(x+8,length),c,thickness,cv::LINE_AA);
}
}
cv::Mat renderResult(FrameResult const& r) {
if(!r.image)throw std::invalid_argument("Result has no frame image");
auto const& image=*r.image;
if(image.width<=0||image.height<=0||image.stride<image.width*3||image.pixels.size()<std::size_t(image.stride)*image.height)
throw std::invalid_argument("Malformed result image");
cv::Mat view(image.height,image.width,CV_8UC3,const_cast<std::uint8_t*>(image.pixels.data()),image.stride);
auto canvas=view.clone();
if(r.door_mask_pixels) {
auto const& m=*r.door_mask_pixels;
if(m.width!=image.width||m.height!=image.height||m.stride<m.width||m.pixels.size()<std::size_t(m.stride)*m.height)
throw std::invalid_argument("Result mask does not match image");
cv::Mat mask(m.height,m.width,CV_8U,const_cast<std::uint8_t*>(m.pixels.data()),m.stride);
auto tint=canvas.clone();tint.setTo(cv::Scalar(255,180,0),mask);
cv::addWeighted(canvas,0.75,tint,0.25,0,canvas);
std::vector<std::vector<cv::Point>> contours;cv::findContours(mask.clone(),contours,cv::RETR_EXTERNAL,cv::CHAIN_APPROX_SIMPLE);
cv::drawContours(canvas,contours,-1,{255,180,0},2);
}
for(auto const& person:r.persons) {
auto const& b=person.box;
cv::rectangle(canvas,{int(b[0]),int(b[1])},{int(b[2]),int(b[3])},{0,220,255},2);
std::ostringstream text;text<<"ID "<<person.track_id<<" "<<std::fixed<<std::setprecision(2)<<person.confidence;
cv::putText(canvas,text.str(),{int(b[0])+4,std::max(180,int(b[1])+25)},cv::FONT_HERSHEY_SIMPLEX,.8,{0,220,255},2,cv::LINE_AA);
for(int s=0;s<2;++s) {
auto const& state=person.hands[s];auto const& k=person.selected;
cv::Scalar arm=s==0?cv::Scalar(40,40,255):cv::Scalar(30,230,30);
for(auto e:std::vector<std::pair<int,int>>{{5+s,7+s},{7+s,9+s}}) {
segment(canvas,point(k[e.first]),point(k[e.second]),state.limb_consensus?arm:cv::Scalar(0,140,255),3,!state.limb_consensus);
cv::circle(canvas,point(k[e.second]),4,arm,-1,cv::LINE_AA);
}
if(state.state=="rejected")continue;
int start=s==0?91:112;
auto hand=state.distance_eligible?(s==0?cv::Scalar(255,80,220):cv::Scalar(255,190,40)):cv::Scalar(0,140,255);
for(int f=0;f<5;++f)for(int j=0;j<4;++j){int a=j==0?start:start+f*4+j;int b=start+1+f*4+j;segment(canvas,point(k[a]),point(k[b]),hand,2,!state.distance_eligible);}
for(int j=0;j<21;++j)cv::circle(canvas,point(k[start+j]),3,hand,-1,cv::LINE_AA);
}
}
for(auto const& h:r.hand_regions) {
std::vector<cv::Point> points,hull;for(auto const& p:h.keypoints)points.push_back(point(p));cv::convexHull(points,hull);
auto risk=std::find_if(r.risk.regions.begin(),r.risk.regions.end(),[&](auto const& item){return item.track_id==h.track_id&&item.side==h.side;});
auto c=color(risk==r.risk.regions.end()?"unavailable":risk->risk_level);
cv::polylines(canvas,std::vector<std::vector<cv::Point>>{hull},true,c,2,cv::LINE_AA);
}
for(auto const& region:r.risk.regions)if(region.source=="YOLO_BODY_BOX_FALLBACK") {
auto it=std::find(r.track_ids.begin(),r.track_ids.end(),region.track_id);
if(it!=r.track_ids.end()){auto const& b=r.boxes.at(it-r.track_ids.begin());cv::putText(canvas,"BODY FALLBACK",{int(b[0])+4,int(b[1])+50},cv::FONT_HERSHEY_SIMPLEX,.65,{0,160,255},2,cv::LINE_AA);}
}
cv::rectangle(canvas,{0,0},{canvas.cols,148},{20,24,30},-1);
std::ostringstream headline;headline<<"RISK ";if(r.risk.risk_score)headline<<std::fixed<<std::setprecision(3)<<*r.risk.risk_score;else headline<<"N/A";headline<<" "<<r.risk.risk_level;
cv::putText(canvas,headline.str(),{24,40},cv::FONT_HERSHEY_SIMPLEX,1.0,color(r.risk.risk_level),2,cv::LINE_AA);
std::ostringstream status;status<<"camera frame "<<r.frame_index<<" | seq "<<r.capture.driver_sequence<<" | latency "<<std::fixed<<std::setprecision(1)<<r.timing.capture_to_result_ms<<" ms";
cv::putText(canvas,status.str(),{24,82},cv::FONT_HERSHEY_SIMPLEX,.72,{235,235,235},2,cv::LINE_AA);
std::string calibration=r.door_mask_calibration_verified?"door calibration verified":"door calibration NOT verified (bring-up)";
cv::putText(canvas,calibration+" | q/Esc: close preview",{24,123},cv::FONT_HERSHEY_SIMPLEX,.70,{0,190,255},2,cv::LINE_AA);
return canvas;
}
namespace { constexpr char windowName[]="CMS Camera Risk"; }
void runLocalVisualization(CameraPipeline const& pipeline) {
struct Window {
Window(){cv::namedWindow(windowName,cv::WINDOW_NORMAL);cv::resizeWindow(windowName,1000,800);}
@@ -90,6 +14,13 @@ void runLocalVisualization(CameraPipeline const& pipeline) {
while(pipeline.running()) {
auto result=pipeline.latest();
if(result&&result!=shown){cv::imshow(windowName,renderResult(*result));shown=std::move(result);}
if(!result) {
auto state=pipeline.inferenceStatus();
cv::Mat idle(800,1000,CV_8UC3,cv::Scalar(20,24,30));
cv::putText(idle,state.enabled?"WAITING FOR FRESH RESULT":"INFERENCE IDLE - MODELS READY",{35,100},cv::FONT_HERSHEY_SIMPLEX,.85,{235,235,235},2);
cv::putText(idle,state.reason,{35,150},cv::FONT_HERSHEY_SIMPLEX,.7,{0,190,255},2);
cv::imshow(windowName,idle);shown.reset();
}
int key=cv::waitKey(16)&0xff;
if(key==27||key=='q'||key=='Q'||cvGetWindowHandle(windowName)==nullptr)return;
}
+31
View File
@@ -0,0 +1,31 @@
# Orin 相机故障恢复与停止测试(2026-09-23)
目标:短暂取流故障在六个等待窗口内恢复时,同一进程继续推理;有效帧到达后重新计数;主动停止不等待全部窗口。
环境:192.168.195.100,Jetson Orin,L4T R35.4.1,CUDA 11.4.298,TensorRT 8.5.2;真实 /dev/video5,YOLO 与 RTMW TensorRT 引擎。三个分支在该板编译。
使用真实采集与推理进程;通过 LD_PRELOAD 在 V4L2 系统调用处注入无帧、坏帧、EIO 和 ENODEV。每次恢复测试检查同一子进程产生新的风险 JSON;错误恢复后至少新增 10 帧。Ctrl+C 通过 PTY 写入 0x03,SIGTERM 发送给测试进程。
| 分支 | Ctrl+C / SIGTERM 完整退出范围 | 持续无帧退出(含清理) | 恢复后再次断流退出(含清理) | 结果 |
|---|---:|---:|---:|---|
| lazy-inference | 801.21–942.42 ms | 13.010 s | 13.085 s | 9/9 通过 |
| inference-only | 793.22–1012.57 ms | 13.055 s | 12.945 s | 9/9 通过 |
| use-can | 786.06–907.59 ms | 13.066 s | 13.155 s | 9/9 通过 |
每个分支覆盖:断流后恢复并重新计时;坏帧恢复;EIO 恢复;ENODEV 后重新打开设备并恢复;正常推理 Ctrl+C;重试期 Ctrl+C;第六窗口 Ctrl+C;SIGTERM;持续无帧六窗口后报错。timeout/recover 场景最后的退出码 1 是预期的超限结果,不是测试失败。
默认逻辑等待约 12 秒,表中的完整进程退出另含 GPU/驱动资源清理时间。最初使用的 1 秒完整退出判据过于严格:清理 GPU 资源可使总耗时略超过 1 秒;最终以不超过 2 秒验收主动停止,同时记录实测值。lazy-inference/use-can 的流水线停止日志也已记录,详见 JSON。
实机发现并修复:use-can 关闭时遗漏 munmap,导致重新打开后 VIDIOC_S_FMT 返回 EBUSY。修复后重跑该分支全部九项。另有原生 V4L2 重开回归测试,检查连续三次重开及部分 mmap 失败后无遗留映射。
补充回归:在板端执行 tests/run_camera_timeout.sh 和 --wall-clock(默认六窗口约 12.00 秒);use-can 额外执行 tests/run_v4l2_reopen.sh。它们使用模拟采集,仅为补充,不替代上述板端真实推理测试。
测试范围限制:未进行物理拔线、USB-CAN 报文/拔插、驱动内核永久卡死、GPU 故障或长时间耐久测试。板上未接 USB-CAN,CAN 模式处于缺失回退。视频回放路径未改动,未包含在本次相机恢复测试中。
完整数据:orin_hardware_results.json。故障注入器源码:orin_capture_faults.c,仅用于测试。原始运行日志位于板端 /home/orin/vision-anti-pinch-ctrlc-test-20260923/。
## 经测试的可执行文件 SHA256
- lazy-inference: `0d77d17626644fb50b663c33edbb1a09884b34b5410a1109fc56b4d3781651b1`
- inference-only: `3a984e1e4c0fa12285967085e3359a5b7c698d744e9c2deb2480a312eb637c1c`
- use-can: `a2616e193ef9f006b49b4bb4ac43ae3ffc4cee1f1edf6d58ed6f7f9e5f933ad1`
+192
View File
@@ -0,0 +1,192 @@
// Exercise the production Camera with simulated capture and no GPU or camera.
#include "camera.hpp"
#include "uyvy_converter.hpp"
#include <linux/videodev2.h>
#include <chrono>
#include <cstring>
#include <functional>
#include <iostream>
#include <stdexcept>
#include <string>
#include <thread>
#ifdef TEST_V4L2_CAPTURE
#include "v4l2_capture.h"
#else
#include <cstdarg>
#include <cerrno>
#include <poll.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#endif
namespace {
std::string events;
std::size_t cursor{};
char current{};
unsigned sequence{};
int timeoutMs{};
bool realWait{};
std::function<void()> onStop;
unsigned char pixels[8]{};
void check(bool condition,char const* message) {
if(!condition)throw std::runtime_error(message);
}
void script(std::string value) {events=std::move(value);cursor=0;}
char next() {
check(cursor<events.size(),"Camera requested an unexpected extra capture attempt");
return current=events[cursor++];
}
void expectTimeout(cms::Camera& camera) {
try {camera.read();} catch(std::runtime_error const& e) {
check(std::string(e.what()).find("6 consecutive times")!=std::string::npos,"Wrong timeout failure");
return;
}
throw std::runtime_error("Six consecutive timeouts did not fail");
}
}
namespace cms {
struct UyvyConverter::Impl {int width,height;};
UyvyConverter::UyvyConverter(int w,int h):impl_(std::make_unique<Impl>(Impl{w,h})){}
UyvyConverter::~UyvyConverter()=default;
std::shared_ptr<const BgrImage> UyvyConverter::convert(void const*,std::size_t) {
auto image=std::make_shared<BgrImage>();image->width=impl_->width;image->height=impl_->height;
image->stride=image->width*3;image->pixels.resize(image->stride*image->height);return image;
}
}
#ifdef TEST_V4L2_CAPTURE
V4l2Capture::~V4l2Capture()=default;
bool V4l2Capture::open(std::string const&){reconnect_needed_=false;return true;}
void V4l2Capture::close(){}
bool V4l2Capture::setFormat(uint32_t w,uint32_t h,uint32_t p){width_=w;height_=h;pixelformat_=p;return true;}
bool V4l2Capture::setFps(uint32_t){return true;}
bool V4l2Capture::initMmap(uint32_t){return true;}
bool V4l2Capture::startStreaming(){return true;}
void V4l2Capture::releaseFrame(Frame const&){}
bool V4l2Capture::getFrame(Frame& f,int waitMs) {
check(waitMs<=100,"Stop polling should remain at most 100 ms");
if(next()=='s'){onStop();return false;}
if(current=='d'){reconnect_needed_=true;return false;}
if(current=='e'||current=='x'){return false;}
if(current=='t') {
// Each scripted timeout represents a full logical capture window.
// The separate stop test exercises the short-wait cancellation path.
std::this_thread::sleep_for(std::chrono::milliseconds(timeoutMs));return false;
}
check(current=='f',"Unsupported mock V4L2 event");
f.data=pixels;f.size=sizeof(pixels);f.width=2;f.height=2;f.bytes_per_line=4;f.sequence=sequence++;
return true;
}
#else
extern "C" {
int __real_poll(pollfd*,nfds_t,int);
int __real_open(char const*,int,...);
int __real_close(int);
int __real_ioctl(int,unsigned long,...);
void* __real_mmap(void*,size_t,int,int,int,off_t);
int __real_munmap(void*,size_t);
int __wrap_open(char const* path,int flags,...) {
if(std::strcmp(path,"/mock/camera")==0)return 100000;
return __real_open(path,flags);
}
int __wrap_close(int fd){return fd==100000?0:__real_close(fd);}
void* __wrap_mmap(void* a,size_t n,int p,int f,int fd,off_t o) {
return fd==100000?pixels:__real_mmap(a,n,p,f,fd,o);
}
int __wrap_munmap(void* a,size_t n){return a==pixels?0:__real_munmap(a,n);}
int __wrap_ioctl(int fd,unsigned long request,...) {
va_list args;va_start(args,request);void* p=va_arg(args,void*);va_end(args);
if(fd!=100000)return __real_ioctl(fd,request,p);
switch(request) {
case VIDIOC_QUERYCAP:static_cast<v4l2_capability*>(p)->capabilities=V4L2_CAP_VIDEO_CAPTURE|V4L2_CAP_STREAMING;break;
case VIDIOC_S_FMT:static_cast<v4l2_format*>(p)->fmt.pix.bytesperline=4;break;
case VIDIOC_G_PARM:break;
case VIDIOC_REQBUFS:static_cast<v4l2_requestbuffers*>(p)->count=2;break;
case VIDIOC_QUERYBUF:static_cast<v4l2_buffer*>(p)->length=sizeof(pixels);break;
case VIDIOC_DQBUF: {
if(current=='x'){errno=EIO;return -1;}
auto& b=*static_cast<v4l2_buffer*>(p);b.index=0;b.bytesused=sizeof(pixels);b.sequence=sequence++;
b.flags=current=='e'?V4L2_BUF_FLAG_ERROR:0;break;
}
case VIDIOC_QBUF:case VIDIOC_STREAMON:case VIDIOC_STREAMOFF:break;
default:throw std::runtime_error("Unexpected camera ioctl");
}
return 0;
}
int __wrap_poll(pollfd* fds,nfds_t count,int waitMs) {
if(count==1)return __real_poll(fds,count,waitMs);
check(count==2&&waitMs>0&&waitMs<=timeoutMs,"Camera must respect the configured timeout window");
switch(next()) {
case 't':if(realWait)std::this_thread::sleep_for(std::chrono::milliseconds(waitMs));return 0;
case 'i':errno=EINTR;return -1;
case 's':onStop();return 0;
case 'd':fds[0].revents=POLLHUP;return 1;
case 'f':case 'e':case 'x':fds[0].revents=POLLIN;return 1;
default:throw std::runtime_error("Unexpected poll event");
}
}
int __wrap___poll_chk(pollfd* fds,nfds_t count,int waitMs,size_t size) {
check(size>=count*sizeof(pollfd),"Invalid fortified poll buffer size");
return __wrap_poll(fds,count,waitMs);
}
}
#endif
int main(int argc,char** argv)try {
cms::CameraConfig config;config.device="/mock/camera";config.width=config.height=2;config.buffers=2;
check(config.timeout_ms==2000,"Default per-attempt timeout must be 2 seconds");
realWait=argc>1&&std::string(argv[1])=="--wall-clock";
if(!realWait)config.timeout_ms=100;
timeoutMs=config.timeout_ms;
if(realWait) {
cms::Camera camera(config);script("tttttt");
auto before=std::chrono::steady_clock::now();expectTimeout(camera);
auto elapsed=std::chrono::duration<double>(std::chrono::steady_clock::now()-before).count();
check(cursor==6&&elapsed>=12.0&&elapsed<15.0,"Default timeout should fail after six attempts, about 12 seconds");
std::cout<<"PASS wall-clock six-timeout duration="<<elapsed<<" s\n";
return 0;
}
{
cms::Camera camera(config);
script("tttttf");check(bool(camera.read()),"Five timeouts should recover on a valid frame");
script("tttttf");check(bool(camera.read()),"A valid frame must reset the timeout count");
script("tttttt");
expectTimeout(camera);
check(cursor==6,"Failure must occur on the sixth timeout");
}
{
cms::Camera camera(config);onStop=[&]{camera.requestStop();};
script("tts");check(!camera.read(),"Stop must cancel retries without throwing");
check(cursor==3,"Stop should not wait for all six timeouts");onStop={};
}
{
cms::Camera camera(config);onStop=[&]{
std::this_thread::sleep_for(std::chrono::milliseconds(timeoutMs));
camera.requestStop();
};
script("ttttts");check(!camera.read(),"Stop on the final timeout must take precedence over failure");
onStop={};
}
#ifndef TEST_V4L2_CAPTURE
{
cms::Camera camera(config);script("tttttif");
check(bool(camera.read()),"EINTR must not count as a timeout");
script("tttttet");expectTimeout(camera);
check(cursor==7,"A corrupt frame must not reset the timeout count");
}
{
cms::Camera camera(config);script("df");
check(bool(camera.read()),"Capture must reopen after a transient disconnection");
}
{
cms::Camera camera(config);script("xxf");
check(bool(camera.read()),"Transient EIO must recover without restarting the pipeline");
}
{
auto slow=config;slow.timeout_ms=2000;timeoutMs=slow.timeout_ms;
cms::Camera camera(slow);script(std::string(35,'e')+"f");
check(bool(camera.read()),"More than one second's frame count of corrupt frames must remain recoverable");
timeoutMs=config.timeout_ms;
}
#endif
std::cout<<"PASS camera timeout retries, recovery reset, sixth-timeout failure and cancellation\n";
return 0;
}catch(std::exception const& e){std::cerr<<"FAIL: "<<e.what()<<'\n';return 1;}
+39
View File
@@ -0,0 +1,39 @@
#define _GNU_SOURCE
#include <dlfcn.h>
#include <poll.h>
#include <sys/select.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdarg.h>
#include <errno.h>
#include <linux/videodev2.h>
#include <sys/ioctl.h>
static int calls=0;
static int camera_fd(int fd){char name[64],target[256];snprintf(name,sizeof(name),"/proc/self/fd/%d",fd);ssize_t n=readlink(name,target,sizeof(target)-1);if(n<0)return 0;target[n]=0;return strcmp(target,"/dev/video5")==0;}
static long long now_ms(void){struct timespec t;clock_gettime(CLOCK_MONOTONIC,&t);return (long long)t.tv_sec*1000+t.tv_nsec/1000000;}
static void marker(const char *s){ssize_t ignored=write(2,s,strlen(s));(void)ignored;}
static int should_stall(void){
static long long since=0;static int phase=0,resumed=0;
if(!getenv("VAP_TEST_STALL"))return 0;
if(phase==0){if(++calls<60)return 0;phase=1;since=now_ms();marker("TEST_STALL_ACTIVE\n");}
if(phase==1&&getenv("VAP_TEST_RECOVER")&&now_ms()-since>=4200){phase=2;marker("TEST_CAMERA_RECOVERED\n");return 0;}
if(phase==2){if(++resumed<30)return 0;phase=3;marker("TEST_STALL_SECOND\n");}
return phase==1||phase==3;
}
int poll(struct pollfd *fds,nfds_t count,int timeout){static int(*real_poll)(struct pollfd*,nfds_t,int);if(!real_poll)real_poll=dlsym(RTLD_NEXT,"poll");for(nfds_t i=0;i<count;++i)if(!getenv("VAP_TEST_KIND")&&camera_fd(fds[i].fd)&&should_stall()){int saved=fds[i].fd;fds[i].fd=-1;int rc=real_poll(fds,count,timeout);fds[i].fd=saved;return rc;}return real_poll(fds,count,timeout);}
int __poll_chk(struct pollfd *fds,nfds_t count,int timeout,size_t size){(void)size;return poll(fds,count,timeout);}
int select(int count,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,struct timeval *timeout){static int(*real_select)(int,fd_set*,fd_set*,fd_set*,struct timeval*);if(!real_select)real_select=dlsym(RTLD_NEXT,"select");if(readfds)for(int fd=0;fd<count;++fd)if(!getenv("VAP_TEST_KIND")&&FD_ISSET(fd,readfds)&&camera_fd(fd)&&should_stall()){FD_CLR(fd,readfds);break;}return real_select(count,readfds,writefds,exceptfds,timeout);}
int ioctl(int fd,unsigned long request,...){
static int(*real_ioctl)(int,unsigned long,...);if(!real_ioctl)real_ioctl=dlsym(RTLD_NEXT,"ioctl");
va_list args;va_start(args,request);void *arg=va_arg(args,void*);va_end(args);
const char *kind=getenv("VAP_TEST_KIND");
if(kind&&request==VIDIOC_DQBUF&&camera_fd(fd)&&should_stall()){
if(strcmp(kind,"bad_frames")==0){int rc=real_ioctl(fd,request,arg);if(rc==0)((struct v4l2_buffer*)arg)->flags|=V4L2_BUF_FLAG_ERROR;return rc;}
errno=strcmp(kind,"device_error")==0?ENODEV:EIO;return -1;
}
return real_ioctl(fd,request,arg);
}
+253
View File
@@ -0,0 +1,253 @@
[
{
"branch": "lazy-inference",
"mode": "recover",
"exit_code": 1,
"inference_json_frames": 88,
"gpu_models": "YOLO + RTMW TensorRT engines",
"six_timeout_exit_seconds": 13.085,
"recovered_real_capture": true
},
{
"branch": "lazy-inference",
"mode": "bad_frames_recover",
"exit_code": 0,
"inference_json_frames": 67,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 846.3,
"recovered_real_capture": true,
"frames_after_recovery": 10,
"pipeline_stop_ms": 132.56
},
{
"branch": "lazy-inference",
"mode": "io_error_recover",
"exit_code": 0,
"inference_json_frames": 67,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 836.09,
"recovered_real_capture": true,
"frames_after_recovery": 10,
"pipeline_stop_ms": 106.06
},
{
"branch": "lazy-inference",
"mode": "device_error_recover",
"exit_code": 0,
"inference_json_frames": 68,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 830.0,
"recovered_real_capture": true,
"frames_after_recovery": 10,
"pipeline_stop_ms": 133.41
},
{
"branch": "lazy-inference",
"mode": "live",
"exit_code": 0,
"inference_json_frames": 3,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 833.16,
"pipeline_stop_ms": 139.5
},
{
"branch": "lazy-inference",
"mode": "retry",
"exit_code": 0,
"inference_json_frames": 58,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 942.42,
"pipeline_stop_ms": 201.21
},
{
"branch": "lazy-inference",
"mode": "sixth_wait",
"exit_code": 0,
"inference_json_frames": 58,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 904.29,
"pipeline_stop_ms": 194.16
},
{
"branch": "lazy-inference",
"mode": "sigterm",
"exit_code": 0,
"inference_json_frames": 3,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 801.21,
"pipeline_stop_ms": 123.09
},
{
"branch": "lazy-inference",
"mode": "timeout",
"exit_code": 1,
"inference_json_frames": 58,
"gpu_models": "YOLO + RTMW TensorRT engines",
"six_timeout_exit_seconds": 13.01
},
{
"branch": "inference-only",
"mode": "recover",
"exit_code": 1,
"inference_json_frames": 88,
"gpu_models": "YOLO + RTMW TensorRT engines",
"six_timeout_exit_seconds": 12.945,
"recovered_real_capture": true
},
{
"branch": "inference-only",
"mode": "bad_frames_recover",
"exit_code": 0,
"inference_json_frames": 68,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 836.67,
"recovered_real_capture": true,
"frames_after_recovery": 10
},
{
"branch": "inference-only",
"mode": "io_error_recover",
"exit_code": 0,
"inference_json_frames": 68,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 827.43,
"recovered_real_capture": true,
"frames_after_recovery": 10
},
{
"branch": "inference-only",
"mode": "device_error_recover",
"exit_code": 0,
"inference_json_frames": 68,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 853.08,
"recovered_real_capture": true,
"frames_after_recovery": 10
},
{
"branch": "inference-only",
"mode": "live",
"exit_code": 0,
"inference_json_frames": 3,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 804.87
},
{
"branch": "inference-only",
"mode": "retry",
"exit_code": 0,
"inference_json_frames": 58,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 913.42
},
{
"branch": "inference-only",
"mode": "sixth_wait",
"exit_code": 0,
"inference_json_frames": 58,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 1012.57
},
{
"branch": "inference-only",
"mode": "sigterm",
"exit_code": 0,
"inference_json_frames": 3,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 793.22
},
{
"branch": "inference-only",
"mode": "timeout",
"exit_code": 1,
"inference_json_frames": 58,
"gpu_models": "YOLO + RTMW TensorRT engines",
"six_timeout_exit_seconds": 13.055
},
{
"branch": "use-can",
"mode": "recover",
"exit_code": 1,
"inference_json_frames": 88,
"gpu_models": "YOLO + RTMW TensorRT engines",
"six_timeout_exit_seconds": 13.155,
"recovered_real_capture": true
},
{
"branch": "use-can",
"mode": "bad_frames_recover",
"exit_code": 0,
"inference_json_frames": 67,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 815.49,
"recovered_real_capture": true,
"frames_after_recovery": 10,
"pipeline_stop_ms": 122.36
},
{
"branch": "use-can",
"mode": "io_error_recover",
"exit_code": 0,
"inference_json_frames": 68,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 836.05,
"recovered_real_capture": true,
"frames_after_recovery": 10,
"pipeline_stop_ms": 130.73
},
{
"branch": "use-can",
"mode": "device_error_recover",
"exit_code": 0,
"inference_json_frames": 68,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 814.68,
"recovered_real_capture": true,
"frames_after_recovery": 10,
"pipeline_stop_ms": 123.99
},
{
"branch": "use-can",
"mode": "live",
"exit_code": 0,
"inference_json_frames": 3,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 786.06,
"pipeline_stop_ms": 108.96
},
{
"branch": "use-can",
"mode": "retry",
"exit_code": 0,
"inference_json_frames": 58,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 862.45,
"pipeline_stop_ms": 167.55
},
{
"branch": "use-can",
"mode": "sixth_wait",
"exit_code": 0,
"inference_json_frames": 58,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 907.59,
"pipeline_stop_ms": 197.07
},
{
"branch": "use-can",
"mode": "sigterm",
"exit_code": 0,
"inference_json_frames": 3,
"gpu_models": "YOLO + RTMW TensorRT engines",
"signal_exit_ms": 827.61,
"pipeline_stop_ms": 130.51
},
{
"branch": "use-can",
"mode": "timeout",
"exit_code": 1,
"inference_json_frames": 58,
"gpu_models": "YOLO + RTMW TensorRT engines",
"six_timeout_exit_seconds": 13.066
}
]
+14
View File
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -euo pipefail
root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
test_dir="$(mktemp -d)"
trap 'rm -rf "$test_dir"' EXIT
flags=(-std=c++17 -O1 -g -Wall -Wextra -Wpedantic -pthread -fsanitize=undefined -fno-sanitize-recover=all -I"$root/include")
links=()
if [[ -f "$root/include/v4l2_capture.h" ]]; then
flags+=(-DTEST_V4L2_CAPTURE -I"$root/tests/stubs")
else
for fn in open close ioctl mmap munmap poll __poll_chk; do links+=("-Wl,--wrap=$fn"); done
fi
"${CXX:-g++}" "${flags[@]}" "${CAMERA_SOURCE:-$root/src/camera.cpp}" "$root/tests/camera_timeout_test.cpp" "${links[@]}" -o "$test_dir/camera_timeout_test"
"$test_dir/camera_timeout_test" "$@"
+19
View File
@@ -0,0 +1,19 @@
#pragma once
// Camera timeout tests exercise live capture only. Stub the unused replay API
// so the use-can Camera can be tested without OpenCV or a video decoder.
#include <stdexcept>
#include <string>
namespace cv {
constexpr int CAP_PROP_FRAME_WIDTH=3,CAP_PROP_FRAME_HEIGHT=4;
struct Mat {
int cols{},rows{};
unsigned char const* ptr(int)const {throw std::logic_error("Video replay is outside this test");}
};
class VideoCapture {
public:
bool isOpened()const{return false;}
bool open(std::string const&){throw std::logic_error("Video replay is outside this test");}
double get(int)const {throw std::logic_error("Video replay is outside this test");}
bool read(Mat&){throw std::logic_error("Video replay is outside this test");}
};
}