This commit is contained in:
xtkuang 2026-09-07 10:03:07 +08:00
parent 33097d0279
commit d7c4c0c381
1621 changed files with 863304 additions and 1148 deletions

View File

@ -29,6 +29,38 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
include(FindExternalLib)
set(ARCH "x86")
setup_external_libs(${ARCH})
# Intel oneVPL / VA-API runtime. The shared libraries in lib/ are installed
# by setup_external_libs(); the VA-API driver plugin directory is installed
# separately because it must retain its dri layout.
set(INTEL_MEDIA_STACK_ROOT
"${PROJECT_SOURCE_DIR}/dependency/${ARCH}/third_party/intel-media-stack/vpl-2.17"
)
set(INTEL_MEDIA_DRIVER_DIR "${INTEL_MEDIA_STACK_ROOT}/lib/dri")
set(INTEL_IHD_DRIVER "${INTEL_MEDIA_DRIVER_DIR}/iHD_drv_video.so")
if(NOT EXISTS "${INTEL_IHD_DRIVER}")
message(FATAL_ERROR "Intel iHD VA-API driver not found: ${INTEL_IHD_DRIVER}")
endif()
message(STATUS "Intel media stack: ${INTEL_MEDIA_STACK_ROOT}")
install(
DIRECTORY "${INTEL_MEDIA_DRIVER_DIR}/"
DESTINATION lib/dri
)
install(CODE [=[
find_program(CMVR_PATCHELF_EXECUTABLE patchelf REQUIRED)
set(_cmvr_ihd_driver
"${CMAKE_INSTALL_PREFIX}/lib/dri/iHD_drv_video.so"
)
execute_process(
COMMAND "${CMVR_PATCHELF_EXECUTABLE}"
--set-rpath "$ORIGIN/.."
"${_cmvr_ihd_driver}"
COMMAND_ERROR_IS_FATAL ANY
)
]=])
# 在调用 setup_external_libs 之后
message(STATUS "CMAKE_EXE_LINKER_FLAGS: ${CMAKE_EXE_LINKER_FLAGS}")
message(STATUS "CMAKE_SHARED_LINKER_FLAGS: ${CMAKE_SHARED_LINKER_FLAGS}")

View File

@ -55,7 +55,17 @@ function(setup_external_libs ARCH)
# ---- library dirs ----
if(EXISTS "${FULL_PATH}/lib")
list(APPEND LIBRARY_DIRS "${FULL_PATH}/lib")
file(GLOB _BUNDLED_LIBSTDCXX_FILES
"${FULL_PATH}/lib/libstdc++.so"
"${FULL_PATH}/lib/libstdc++.so.*"
)
if(_BUNDLED_LIBSTDCXX_FILES)
message(STATUS
"${LIB_NAME}: excluding vendor lib directory from global "
"link paths because it contains a private libstdc++")
else()
list(APPEND LIBRARY_DIRS "${FULL_PATH}/lib")
endif()
set(HAS_LIB TRUE)
# Collect shared libs for install: *.so and *.so.*
@ -63,6 +73,7 @@ function(setup_external_libs ARCH)
"${FULL_PATH}/lib/*.so"
"${FULL_PATH}/lib/*.so.*"
)
list(FILTER _SO_FILES EXCLUDE REGEX "/libstdc\\+\\+\\.so(\\..*)?$")
if(_SO_FILES)
list(APPEND INSTALL_SO_FILES ${_SO_FILES})
endif()
@ -85,6 +96,7 @@ function(setup_external_libs ARCH)
"${FULL_PATH}/*.so"
"${FULL_PATH}/*.so.*"
)
list(FILTER _SO_FILES2 EXCLUDE REGEX "/libstdc\\+\\+\\.so(\\..*)?$")
if(_SO_FILES2)
list(APPEND INSTALL_SO_FILES ${_SO_FILES2})
endif()
@ -138,6 +150,14 @@ function(setup_external_libs ARCH)
)
install(FILES ${INSTALL_SO_FILES} DESTINATION lib)
install(CODE [[
file(GLOB _bundled_stdlib_files
"${CMAKE_INSTALL_PREFIX}/lib/libstdc++.so"
"${CMAKE_INSTALL_PREFIX}/lib/libstdc++.so.*")
if(_bundled_stdlib_files)
file(REMOVE ${_bundled_stdlib_files})
endif()
]])
# After installing, patch RPATH of installed shared libs to $ORIGIN
install(CODE [[

View File

@ -7,6 +7,7 @@ add_subdirectory(algorithms)
add_subdirectory(simulate)
add_subdirectory(devices)
add_subdirectory(manager/device_manager)
add_subdirectory(manager/media_source_hub)
add_subdirectory(task)
add_subdirectory(manager/task_manager)
add_subdirectory(service)

View File

@ -6,16 +6,25 @@
#define CMVR_ES_RING_BUFFER_H
#pragma once
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <deque>
#include <memory>
#include <mutex>
#include <vector>
#include <atomic>
#include <optional>
#include <stdexcept>
#include <utility>
#include <vector>
template<typename T>
class RingBuffer {
public:
explicit RingBuffer(size_t capacity) : capacity_(capacity) {}
explicit RingBuffer(size_t capacity) : capacity_(capacity) {
if (capacity_ == 0) {
throw std::invalid_argument("RingBuffer capacity must be greater than zero");
}
}
void push(const T& item) {
std::lock_guard<std::mutex> lock(mutex_);
@ -50,47 +59,49 @@ template<typename T>
class SPMCRingBuffer {
public:
explicit SPMCRingBuffer(size_t capacity)
: buffer_(capacity), capacity_(capacity),
head_(0), tail_(0) {}
: buffer_(capacity), capacity_(capacity) {
if (capacity_ == 0) {
throw std::invalid_argument("SPMCRingBuffer capacity must be greater than zero");
}
}
// 写入操作(仅支持单个生产者)
void push(const T& item) {
size_t head = head_.load(std::memory_order_relaxed);
size_t tail = tail_.load(std::memory_order_acquire);
buffer_[head % capacity_] = item;
head = head + 1;
head_.store(head, std::memory_order_release);
if (head - tail >= capacity_) {
// 队列满,覆盖最旧的数据
tail_.store(tail + 1, std::memory_order_release);
{
std::lock_guard<std::mutex> lock(mutex_);
buffer_[head_ % capacity_] = item;
++head_;
if (head_ - tail_ > capacity_) {
// 队列满,覆盖最旧的数据
tail_ = head_ - capacity_;
}
}
condition_.notify_all();
}
// 单消费者使用(内部 tail_)
std::optional<T> pop() {
size_t tail = tail_.load(std::memory_order_relaxed);
size_t head = head_.load(std::memory_order_acquire);
if (tail >= head) return std::nullopt;
T value = buffer_[tail % capacity_];
tail_.store(tail + 1, std::memory_order_release);
std::lock_guard<std::mutex> lock(mutex_);
if (tail_ >= head_) return std::nullopt;
T value = buffer_[tail_ % capacity_];
++tail_;
return value;
}
std::optional<T> getLast() {
size_t tail = tail_.load(std::memory_order_relaxed);
size_t head = head_.load(std::memory_order_acquire);
if (tail >= head) return std::nullopt;
T value = buffer_[head_ % capacity_];
return value;
std::optional<T> getLast() const {
std::lock_guard<std::mutex> lock(mutex_);
if (tail_ >= head_) return std::nullopt;
return buffer_[(head_ - 1) % capacity_];
}
// 多消费者使用(每个读者独立维护 reader_tail)
// 多消费者使用(每个读者独立维护 reader_tail)。同一个 reader_tail 只能由
// 一个消费线程拥有,且不要把该游标与无参 pop() 的共享 tail_ 混合作为同一路读取。
std::optional<T> pop(size_t& reader_tail) const {
size_t head = head_.load(std::memory_order_acquire);
if (reader_tail >= head) return std::nullopt;
if (head > reader_tail + capacity_) {
std::lock_guard<std::mutex> lock(mutex_);
if (reader_tail >= head_) return std::nullopt;
if (reader_tail < tail_) {
// 数据已被覆盖,跳过无效读取区间
reader_tail = head - capacity_;
reader_tail = tail_;
return std::nullopt;
}
T value = buffer_[reader_tail % capacity_];
@ -98,16 +109,52 @@ public:
return value;
}
// 在同一次加锁中把独立读游标跳到当前最新元素并读取,避免先 getHead()
// 再 pop() 时被高速覆盖造成的检查/读取竞态。
std::optional<T> getLatest(size_t& reader_tail) const {
std::lock_guard<std::mutex> lock(mutex_);
if (tail_ >= head_) {
return std::nullopt;
}
reader_tail = head_ - 1;
T value = buffer_[reader_tail % capacity_];
++reader_tail;
return value;
}
template<class Rep, class Period>
std::optional<T> waitPop(
size_t& reader_tail,
const std::chrono::duration<Rep, Period>& timeout) const {
std::unique_lock<std::mutex> lock(mutex_);
condition_.wait_for(lock, timeout, [&] { return reader_tail < head_; });
if (reader_tail >= head_) {
return std::nullopt;
}
if (reader_tail < tail_) {
reader_tail = tail_;
}
if (reader_tail >= head_) {
return std::nullopt;
}
T value = buffer_[reader_tail % capacity_];
++reader_tail;
return value;
}
size_t size() const {
return head_.load(std::memory_order_acquire) - tail_.load(std::memory_order_acquire);
std::lock_guard<std::mutex> lock(mutex_);
return head_ - tail_;
}
size_t getHead() const {
return head_.load(std::memory_order_acquire);
std::lock_guard<std::mutex> lock(mutex_);
return head_;
}
size_t getTail() const {
return tail_.load(std::memory_order_acquire);
std::lock_guard<std::mutex> lock(mutex_);
return tail_;
}
bool empty() const {
@ -119,16 +166,278 @@ public:
}
void clear() {
head_.store(0, std::memory_order_release);
tail_.store(0, std::memory_order_release);
{
std::lock_guard<std::mutex> lock(mutex_);
// Keep sequence numbers monotonic so cursors created before clear()
// cannot alias newly published slots after the reset.
tail_ = head_;
}
condition_.notify_all();
}
private:
std::vector<T> buffer_;
mutable std::vector<T> buffer_;
const size_t capacity_;
mutable std::mutex mutex_;
mutable std::condition_variable condition_;
size_t head_{0}; // 单调写序号;clear() 仅推进 tail_,避免旧游标 ABA。
size_t tail_{0}; // 当前仍保留的最旧序号,同时也是 pop() 的共享读指针。
};
std::atomic<size_t> head_; // 共享写指针
std::atomic<size_t> tail_; // 共享读指针(仅用于 SPSC 模式)
// 线程安全的多消费者广播缓冲区。缓冲区只保存不可变共享对象,消费者通过各自
// 的 Cursor 独立前进;慢消费者被覆盖的数据会累计到 Cursor::dropped_count。
// Cursor 是单线程所有权对象,不可由多个线程同时读写;每个消费者应创建自己的 Cursor。
template<typename T>
class BroadcastFrameRing {
public:
using ValuePtr = std::shared_ptr<const T>;
enum class StartPosition {
NEXT_PUBLISHED,
OLDEST_AVAILABLE,
LATEST_AVAILABLE
};
struct Cursor {
uint64_t generation{0};
uint64_t next_sequence{0};
uint64_t dropped_count{0};
StartPosition start_position{StartPosition::NEXT_PUBLISHED};
private:
bool generation_changed{false};
uint64_t reported_dropped_count{0};
friend class BroadcastFrameRing<T>;
};
struct ReadResult {
ValuePtr value;
uint64_t generation{0};
uint64_t sequence{0};
uint64_t dropped_count{0};
uint64_t dropped_since_last_read{0};
bool generation_changed{false};
};
struct Stats {
size_t capacity{0};
size_t size{0};
uint64_t generation{0};
uint64_t next_sequence{0};
uint64_t dropped_count{0};
bool closed{false};
};
explicit BroadcastFrameRing(const size_t capacity)
: capacity_(capacity) {
if (capacity_ == 0) {
throw std::invalid_argument("BroadcastFrameRing capacity must be greater than zero");
}
}
BroadcastFrameRing(const BroadcastFrameRing&) = delete;
BroadcastFrameRing& operator=(const BroadcastFrameRing&) = delete;
Cursor makeCursor(const StartPosition start_position = StartPosition::NEXT_PUBLISHED) const {
std::lock_guard<std::mutex> lock(mutex_);
Cursor cursor;
cursor.generation = generation_;
cursor.start_position = start_position;
cursor.next_sequence = startSequenceLocked_(start_position);
return cursor;
}
std::optional<uint64_t> publish(ValuePtr value) {
if (!value) {
return std::nullopt;
}
std::optional<uint64_t> published_sequence;
{
std::lock_guard<std::mutex> lock(mutex_);
if (closed_) {
return std::nullopt;
}
const uint64_t sequence = next_sequence_++;
if (entries_.size() == capacity_) {
entries_.pop_front();
++dropped_count_;
}
entries_.push_back(Entry{generation_, sequence, std::move(value)});
published_sequence = sequence;
}
condition_.notify_all();
return published_sequence;
}
std::optional<ReadResult> tryRead(Cursor& cursor) const {
std::lock_guard<std::mutex> lock(mutex_);
return tryReadLocked_(cursor);
}
// Low-latency consumers can use this before reading to abandon an excessive
// backlog atomically. When the number of currently readable entries exceeds
// maximum_pending_frames, every pending entry is discarded and the cursor is
// advanced to the next sequence that will be published. Frames already
// overwritten by the ring and frames actively discarded here are both
// reflected in Cursor::dropped_count; the next successful read reports their
// sum through ReadResult::dropped_since_last_read.
uint64_t discardPendingIfExceeds(
Cursor& cursor,
const size_t maximum_pending_frames) const {
std::lock_guard<std::mutex> lock(mutex_);
synchronizeCursorGenerationLocked_(cursor);
if (!entries_.empty()) {
const uint64_t oldest_sequence = entries_.front().sequence;
if (cursor.next_sequence < oldest_sequence) {
cursor.dropped_count += oldest_sequence - cursor.next_sequence;
cursor.next_sequence = oldest_sequence;
}
}
const uint64_t pending =
cursor.next_sequence < next_sequence_
? next_sequence_ - cursor.next_sequence
: 0;
if (pending <= static_cast<uint64_t>(maximum_pending_frames)) {
return 0;
}
cursor.next_sequence = next_sequence_;
cursor.dropped_count += pending;
return pending;
}
template<class Rep, class Period>
std::optional<ReadResult> waitRead(
Cursor& cursor,
const std::chrono::duration<Rep, Period>& timeout) const {
const auto deadline = std::chrono::steady_clock::now() + timeout;
std::unique_lock<std::mutex> lock(mutex_);
while (true) {
if (auto result = tryReadLocked_(cursor)) {
return result;
}
if (closed_) {
return std::nullopt;
}
if (condition_.wait_until(lock, deadline) == std::cv_status::timeout) {
return tryReadLocked_(cursor);
}
}
}
// 开始一个新的发布代次。旧 Cursor 会在下一次成功读取时收到
// generation_changed=true,序号从 0 重新开始。
uint64_t reset() {
uint64_t generation = 0;
{
std::lock_guard<std::mutex> lock(mutex_);
entries_.clear();
++generation_;
next_sequence_ = 0;
closed_ = false;
generation = generation_;
}
condition_.notify_all();
return generation;
}
void close() {
{
std::lock_guard<std::mutex> lock(mutex_);
closed_ = true;
}
condition_.notify_all();
}
bool closed() const {
std::lock_guard<std::mutex> lock(mutex_);
return closed_;
}
Stats stats() const {
std::lock_guard<std::mutex> lock(mutex_);
return Stats{capacity_, entries_.size(), generation_, next_sequence_, dropped_count_, closed_};
}
private:
struct Entry {
uint64_t generation;
uint64_t sequence;
ValuePtr value;
};
uint64_t startSequenceLocked_(const StartPosition start_position) const {
if (entries_.empty()) {
return next_sequence_;
}
switch (start_position) {
case StartPosition::OLDEST_AVAILABLE:
return entries_.front().sequence;
case StartPosition::LATEST_AVAILABLE:
return entries_.back().sequence;
case StartPosition::NEXT_PUBLISHED:
default:
return next_sequence_;
}
}
void synchronizeCursorGenerationLocked_(Cursor& cursor) const {
if (cursor.generation == generation_) {
return;
}
cursor.generation = generation_;
cursor.next_sequence = startSequenceLocked_(cursor.start_position);
cursor.generation_changed = true;
}
std::optional<ReadResult> tryReadLocked_(Cursor& cursor) const {
synchronizeCursorGenerationLocked_(cursor);
if (entries_.empty()) {
return std::nullopt;
}
const uint64_t oldest_sequence = entries_.front().sequence;
if (cursor.next_sequence < oldest_sequence) {
cursor.dropped_count += oldest_sequence - cursor.next_sequence;
cursor.next_sequence = oldest_sequence;
}
if (cursor.next_sequence >= next_sequence_) {
return std::nullopt;
}
const size_t index = static_cast<size_t>(cursor.next_sequence - oldest_sequence);
if (index >= entries_.size()) {
return std::nullopt;
}
const Entry& entry = entries_[index];
++cursor.next_sequence;
const uint64_t dropped_since_last = cursor.dropped_count - cursor.reported_dropped_count;
cursor.reported_dropped_count = cursor.dropped_count;
ReadResult result;
result.value = entry.value;
result.generation = entry.generation;
result.sequence = entry.sequence;
result.dropped_count = cursor.dropped_count;
result.dropped_since_last_read = dropped_since_last;
result.generation_changed = cursor.generation_changed;
cursor.generation_changed = false;
return result;
}
const size_t capacity_;
mutable std::mutex mutex_;
mutable std::condition_variable condition_;
std::deque<Entry> entries_;
uint64_t generation_{1};
uint64_t next_sequence_{0};
uint64_t dropped_count_{0};
bool closed_{false};
};

View File

@ -62,14 +62,14 @@ int CameraCapture::initialize(const Config& config) {
}
int CameraCapture::init_device() {
AVInputFormat* input_fmt = nullptr;
// AVInputFormat* input_fmt = nullptr;
std::string device_path;
#ifdef _WIN32
input_fmt = av_find_input_format("dshow");
auto input_fmt = av_find_input_format("dshow");
device_path = "video=" + config_.device_name;
#else
input_fmt = av_find_input_format("v4l2");
auto input_fmt = av_find_input_format("v4l2");
device_path = config_.device_name;
#endif

View File

@ -0,0 +1,244 @@
#ifndef CMVR_ES_COMMON_MEDIA_MEDIA_FRAME_H
#define CMVR_ES_COMMON_MEDIA_MEDIA_FRAME_H
#pragma once
#include <cstddef>
#include <cstdint>
#include <memory>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
namespace cmvr::media {
enum class MediaKind : uint8_t {
UNKNOWN = 0,
VIDEO = 1,
AUDIO = 2
};
enum class Codec : uint8_t {
UNKNOWN = 0,
H264 = 1,
H265 = 2,
OPUS = 3,
PCM_S16LE = 4,
AAC = 5
};
enum class PayloadFormat : uint8_t {
UNKNOWN = 0,
ANNEX_B = 1,
AVCC = 2,
RAW = 3,
OPUS_PACKET = 4,
AAC_ADTS = 5
};
struct Rational {
int32_t numerator{0};
int32_t denominator{1};
constexpr bool valid() const noexcept {
return numerator > 0 && denominator > 0;
}
};
constexpr bool operator==(const Rational lhs, const Rational rhs) noexcept {
return lhs.numerator == rhs.numerator && lhs.denominator == rhs.denominator;
}
constexpr bool operator!=(const Rational lhs, const Rational rhs) noexcept {
return !(lhs == rhs);
}
// A TrackDescriptor is immutable after construction. Reconfiguration is represented by
// publishing a new descriptor with a larger generation and attaching it to later frames.
class TrackDescriptor final {
public:
struct Config {
std::string id;
std::string source_id;
MediaKind kind{MediaKind::UNKNOWN};
Codec codec{Codec::UNKNOWN};
PayloadFormat payload_format{PayloadFormat::UNKNOWN};
Rational time_base{};
uint32_t width{0};
uint32_t height{0};
uint32_t sample_rate{0};
uint32_t channels{0};
uint32_t nominal_rate{0};
float fx{0.0F};
float fy{0.0F};
float cx{0.0F};
float cy{0.0F};
std::vector<float> distortion;
uint64_t generation{1};
std::vector<uint8_t> codec_config;
};
explicit TrackDescriptor(Config config)
: id(std::move(config.id)),
source_id(std::move(config.source_id)),
kind(config.kind),
codec(config.codec),
payload_format(config.payload_format),
time_base(config.time_base),
width(config.width),
height(config.height),
sample_rate(config.sample_rate),
channels(config.channels),
nominal_rate(config.nominal_rate),
fx(config.fx),
fy(config.fy),
cx(config.cx),
cy(config.cy),
distortion(std::move(config.distortion)),
generation(config.generation),
codec_config(std::move(config.codec_config)) {
if (id.empty()) {
throw std::invalid_argument("TrackDescriptor id must not be empty");
}
if (source_id.empty()) {
throw std::invalid_argument("TrackDescriptor source_id must not be empty");
}
if (kind == MediaKind::UNKNOWN) {
throw std::invalid_argument("TrackDescriptor kind must not be UNKNOWN");
}
if (!time_base.valid()) {
throw std::invalid_argument("TrackDescriptor time_base is invalid");
}
if (generation == 0) {
throw std::invalid_argument("TrackDescriptor generation must be greater than zero");
}
}
const std::string id;
const std::string source_id;
const MediaKind kind;
const Codec codec;
const PayloadFormat payload_format;
const Rational time_base;
const uint32_t width;
const uint32_t height;
const uint32_t sample_rate;
const uint32_t channels;
const uint32_t nominal_rate;
const float fx;
const float fy;
const float cx;
const float cy;
const std::vector<float> distortion;
const uint64_t generation;
const std::vector<uint8_t> codec_config;
};
using TrackDescriptorPtr = std::shared_ptr<const TrackDescriptor>;
inline bool equivalentTrackDescriptor(
const TrackDescriptor& lhs,
const TrackDescriptor& rhs) noexcept {
return lhs.id == rhs.id &&
lhs.source_id == rhs.source_id &&
lhs.kind == rhs.kind &&
lhs.codec == rhs.codec &&
lhs.payload_format == rhs.payload_format &&
lhs.time_base == rhs.time_base &&
lhs.width == rhs.width &&
lhs.height == rhs.height &&
lhs.sample_rate == rhs.sample_rate &&
lhs.channels == rhs.channels &&
lhs.nominal_rate == rhs.nominal_rate &&
lhs.fx == rhs.fx &&
lhs.fy == rhs.fy &&
lhs.cx == rhs.cx &&
lhs.cy == rhs.cy &&
lhs.distortion == rhs.distortion &&
lhs.generation == rhs.generation &&
lhs.codec_config == rhs.codec_config;
}
// MediaFrame and its payload are immutable and therefore safe to share across all protocol
// adapters and consumers without copying. PTS/DTS use TrackDescriptor::time_base;
// capture_time_ns is monotonic for pacing, while capture_utc_ns is optional wall time.
class MediaFrame final {
public:
using Payload = std::vector<uint8_t>;
using PayloadPtr = std::shared_ptr<const Payload>;
struct Config {
TrackDescriptorPtr descriptor;
Payload payload;
uint64_t sequence{0};
// Opaque producer-native timing/counter values. Their units and epoch
// are source-defined; zero means that the source did not provide them.
uint64_t source_timestamp{0};
uint64_t source_frame_number{0};
int64_t pts{0};
int64_t dts{0};
int64_t duration{0};
uint64_t capture_time_ns{0};
int64_t capture_utc_ns{0};
bool key_frame{false};
bool discontinuity{false};
};
explicit MediaFrame(Config config)
: descriptor(std::move(config.descriptor)),
payload(std::make_shared<const Payload>(std::move(config.payload))),
sequence(config.sequence),
source_timestamp(config.source_timestamp),
source_frame_number(config.source_frame_number),
pts(config.pts),
dts(config.dts),
duration(config.duration),
capture_time_ns(config.capture_time_ns),
capture_utc_ns(config.capture_utc_ns),
key_frame(config.key_frame),
discontinuity(config.discontinuity) {
if (!descriptor) {
throw std::invalid_argument("MediaFrame descriptor must not be null");
}
}
const uint8_t* data() const noexcept {
return payload->empty() ? nullptr : payload->data();
}
size_t size() const noexcept {
return payload->size();
}
bool empty() const noexcept {
return payload->empty();
}
const TrackDescriptorPtr descriptor;
const PayloadPtr payload;
const uint64_t sequence;
const uint64_t source_timestamp;
const uint64_t source_frame_number;
const int64_t pts;
const int64_t dts;
const int64_t duration;
const uint64_t capture_time_ns;
const int64_t capture_utc_ns;
const bool key_frame;
const bool discontinuity;
};
using MediaFramePtr = std::shared_ptr<const MediaFrame>;
inline TrackDescriptorPtr makeTrackDescriptor(TrackDescriptor::Config config) {
return std::make_shared<const TrackDescriptor>(std::move(config));
}
inline MediaFramePtr makeMediaFrame(MediaFrame::Config config) {
return std::make_shared<const MediaFrame>(std::move(config));
}
} // namespace cmvr::media
#endif // CMVR_ES_COMMON_MEDIA_MEDIA_FRAME_H

View File

@ -101,27 +101,7 @@ camera {
}
}
cameras {
id: "left_eye_cam"
uvc {
usb: "/dev/uvc_left_camera"
camera_mode: CAMERA_MODE_VIDEO
capture {
width: 640
height: 480
fps: 30
stream_mode: STREAM_MODE_RGB
}
encoder {
width: 640
height: 480
fps: 30
codec: "H265"
enable_stream_timestamp: true
buffer_size: 30
}
}
}
cameras {
id: "cam5"
@ -146,4 +126,89 @@ camera {
sync: false
}
}
cameras {
id: "hikvision_cam"
hikvision {
ip: "192.168.192.64"
port: 8000
username: "admin"
password: "okwy1688"
channel: 1
stream_type: 0
link_mode: 0
width: 1920
height: 1080
fps: 25
codec: "H264"
camera_mode: CAMERA_MODE_VIDEO
stream_mode: STREAM_MODE_RGB
buffer_size: 30
}
}
cameras {
id: "hikvision_thermal_cam"
hikvision {
ip: "192.168.192.65"
port: 8000
username: "admin"
password: "okwy1688"
channel: 1
stream_type: 0
link_mode: 0
width: 384
height: 288
fps: 50
codec: "H264"
camera_mode: CAMERA_MODE_VIDEO
stream_mode: STREAM_MODE_RGB
buffer_size: 30
}
}
cameras {
id: "real_cam1"
realsense {
serialNumber: "332522076896"
camera_mode: CAMERA_MODE_VIDEO
capture {
width: 640
height: 480
fps: 30
stream_mode: STREAM_MODE_RGBD
}
encoder {
width: 640
height: 480
fps: 30
codec: "h265_qsv"
enable_stream_timestamp: true
buffer_size: 30
}
align_mode: ALIGN_MODE_COLOR
sync: false
}
}
cameras {
id: "usb_cam1"
uvc {
usb: "/dev/video0"
camera_mode: CAMERA_MODE_VIDEO
capture {
width: 640
height: 480
fps: 30
stream_mode: STREAM_MODE_RGB
}
encoder {
width: 640
height: 480
fps: 30
codec: "h265_qsv"
enable_stream_timestamp: true
buffer_size: 30
}
}
}
}

View File

@ -3,9 +3,9 @@ microphone {
id: "mic1"
ffmpeg {
channels: 2
sampleRate: 44100
sampleRate: 48000
volume: 100
input_device: "default"
input_device: "plughw:CARD=XFMDPV0018,DEV=0"
}
}
}

View File

@ -2,6 +2,7 @@ device_manager {
name: "cmvr_es"
version: "0.1"
description: "cmvr edge system version 0.1"
init_all_motors_when_no_active_joints: true
devices {
id: "mujoco_world"
@ -74,6 +75,13 @@ device_manager {
enable: false
}
devices {
id: "ethercat_motors"
type: DEVICE_TYPE_MOTOR_SYSTEM
config_file: "devices/motor/ethercat_motors.pb.txt"
enable: false
}
devices {
id: "right_arm"
type: DEVICE_TYPE_ROBOT_ARM
@ -81,6 +89,20 @@ device_manager {
enable: false
}
devices {
id: "eyou_arm"
type: DEVICE_TYPE_ROBOT_ARM
config_file: "devices/arm/arm.pb.txt"
enable: false
}
devices {
id: "eyou_left_arm"
type: DEVICE_TYPE_ROBOT_ARM
config_file: "devices/arm/arm_eyou_left.pb.txt"
enable: false
}
devices {
id: "aubo_arm"
type: DEVICE_TYPE_ROBOT_ARM
@ -92,7 +114,7 @@ device_manager {
id: "huayan_arm"
type: DEVICE_TYPE_ROBOT_ARM
config_file: "devices/arm/huayan_arm.pb.txt"
enable: true
enable: false
}
devices {
@ -101,11 +123,58 @@ device_manager {
config_file: "devices/biohead/bio_head.pb.txt"
enable: false
}
devices {
id: "agv_1"
id: "src1100"
type: DEVICE_TYPE_AGV
config_file: "devices/agv/agv.pb.txt"
config_file: "devices/agv/src1100.pb.txt"
enable: false
}
devices {
id: "hikvision_cam"
type: DEVICE_TYPE_CAMERA
config_file: "devices/camera/camera.pb.txt"
# Host-development default: keep physical cameras disabled.
enable: false
}
devices {
id: "hikvision_thermal_cam"
type: DEVICE_TYPE_CAMERA
config_file: "devices/camera/camera.pb.txt"
# Host-development default: keep physical cameras disabled.
enable: false
}
devices {
id: "mic1"
type: DEVICE_TYPE_MICROPHONE
config_file: "devices/microphone/microphone.pb.txt"
# Host-development default: keep physical audio devices disabled.
enable: false
}
devices {
id: "spk1"
type: DEVICE_TYPE_SPEAKER
config_file: "devices/speaker/speaker.pb.txt"
# Host-development default: keep physical audio devices disabled.
enable: false
}
devices {
id: "real_cam1"
type: DEVICE_TYPE_CAMERA
config_file: "devices/camera/camera.pb.txt"
# Host-development default: keep physical cameras disabled.
enable: false
}
devices {
id: "usb_cam1"
type: DEVICE_TYPE_CAMERA
config_file: "devices/camera/camera.pb.txt"
# Host-development default: keep physical cameras disabled.
enable: false
}
}

View File

@ -3,4 +3,6 @@ grpc_server {
host: "0.0.0.0"
port: "50052"
enable_reflection: true
camera_stream_max_pending_frames: 2
camera_stream_max_frame_age_ms: 250
}

View File

@ -34,7 +34,10 @@ namespace cmvr::device {
virtual bool start() { return true; }
virtual bool stop() { return true; }
virtual bool update() { return true; }
virtual bool executeJsonCommand(const std::string& request_json, std::string& response_json) {
response_json = R"({"success":false,"error_message":"JSON command unsupported"})";
return false;
}
protected:
std::string id_; // 设备名称
};

View File

@ -3,7 +3,7 @@ add_subdirectory(common)
add_subdirectory(uvc_camera)
add_subdirectory(realsense_camera)
add_subdirectory(mujoco_camera)
add_subdirectory(hikvision_camera)
add_library(camera INTERFACE)
target_include_directories(camera INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
@ -14,6 +14,7 @@ target_link_libraries(camera
cmvr_es::device::realsense_camera
cmvr_es::device::mujoco_camera
cmvr_es::device::camera_stream_encoder
cmvr_es::device::hikvision_camera
cmvr_es::proto
)

View File

@ -2,6 +2,11 @@
#define CMVR_ES_ABSTRACT_CAMERA_H
#pragma once
#include <cstdint>
#include <chrono>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
#include "../abstract_device.h"
#include <Eigen/Core>
@ -31,13 +36,50 @@ namespace cmvr::device {
std::vector<uint8_t> depthFrame;
//编码格式
std::string codec = ".h264";
Rs2Intrinsics intrinsics;
int width;
int height;
int fps;
bool bKey;
bool depthKey;
Rs2Intrinsics intrinsics{};
int width = 0;
int height = 0;
// Depth frames may use a different resolution from the encoded color frame.
int depth_width = 0;
int depth_height = 0;
int fps = 0;
bool bKey = false;
bool depthKey = false;
// Protocol-neutral real-time metadata. The producer fills these values
// when a complete encoded access unit is published.
uint64_t stream_epoch = 0;
uint64_t sequence = 0;
// Opaque producer-native timing/counter values. Their units and epoch
// are source-defined; zero means that the source did not provide them.
uint64_t source_timestamp = 0;
uint64_t source_frame_number = 0;
int64_t capture_monotonic_ns = 0;
int64_t capture_utc_ns = 0;
int64_t pts = 0;
int64_t dts = 0;
int32_t time_base_num = 1;
int32_t time_base_den = 1;
int64_t duration = 0;
bool discontinuity = false;
uint32_t codec_config_generation = 0;
std::vector<uint8_t> codec_config;
};
enum class PtzCommand {
TiltUp,
TiltDown,
PanLeft,
PanRight,
UpLeft,
UpRight,
DownLeft,
DownRight,
ZoomIn,
ZoomOut,
PanAuto
};
class AbstractCamera : public AbstractDevice {
public:
// 录制状态
@ -51,7 +93,10 @@ namespace cmvr::device {
~AbstractCamera() override = default;
DeviceKind kind() const noexcept override { return DeviceKind::Camera; }
inline void getState(CameraState &state) {state = state_;}
// Every implementation must take the same lock used by its state_
// writers; CameraState contains std::string and cannot be snapshotted
// safely while another thread mutates it.
virtual void getState(CameraState &state) = 0;
virtual void getRGBImage(cv::Mat &color, Rs2Intrinsics& intrinsics) {}
virtual void getDepthImage(cv::Mat &depth, Rs2Intrinsics& intrinsics) {}
virtual void getRGBDImages(cv::Mat &color, cv::Mat &depth, Rs2Intrinsics& intrinsics) {}
@ -60,12 +105,27 @@ namespace cmvr::device {
virtual void pauseRecording() {}
virtual void resumeRecording() {}
virtual void getEncodedFrame(StreamFrameData& frame_data, size_t& index) {}
virtual bool waitEncodedFrame(
StreamFrameData& frame_data,
size_t& index,
std::chrono::milliseconds timeout) {
(void)timeout;
getEncodedFrame(frame_data, index);
return !frame_data.rgbFrame.empty() || !frame_data.depthFrame.empty();
}
virtual bool getLatestEncodedFrame(StreamFrameData& frame_data, size_t& next_index) {
return false;
}
virtual bool startStreaming() {return true;}
virtual void stopStreaming() {}
virtual bool controlPtz(PtzCommand command, bool stop, int speed) {
(void)command;
(void)stop;
(void)speed;
return false;
}
virtual bool requestKeyFrame() { return false; }
virtual Eigen::Vector3f get3DPointFromPixel(int u, int v) {return {0,0,0};}
protected:
CameraState state_{};

View File

@ -9,6 +9,7 @@
#include "common/base/logging/logger.h"
#include "devices/camera/abstract_camera.h"
#include "devices/camera/mujoco_camera/include/mujoco_camera.h"
#include "devices/camera/hikvision_camera/include/hikvision_camera.h"
#include "devices/camera/realsense_camera/include/realsense_camera.h"
#include "devices/camera/uvc_camera/include/uvc_camera.h"
@ -41,6 +42,10 @@ public:
return nullptr;
}
case config::CameraDeviceConfig::kHikvision:
return std::make_shared<HikvisionCamera>(
backendWithId_(cfg.id(), cfg.hikvision()));
case config::CameraDeviceConfig::kMujoco:
return std::make_shared<MujocoCamera>(
backendWithId_(cfg.id(), cfg.mujoco()));

View File

@ -19,6 +19,7 @@ struct FfmpegEncoderInfo {
bool bRunning = false;
AVCodecContext* codec_context = nullptr;
AVFrame* frame = nullptr;
AVFrame* transfer_frame = nullptr;
AVPacket* packet = nullptr;
SwsContext* sws_context = nullptr;
@ -42,6 +43,12 @@ public:
std::vector<uint8_t>& encoded_frame,
bool& is_key,
const CameraStreamEncodeOptions& options = {});
static bool encode(std::shared_ptr<FfmpegEncoderInfo>& encoder,
const AVFrame* frame,
std::vector<uint8_t>& encoded_frame,
bool& is_key,
const CameraStreamEncodeOptions& options = {});
};
} // namespace cmvr::device

View File

@ -6,6 +6,8 @@
#include <sstream>
#include <libavutil/opt.h>
#include <libavutil/pixdesc.h>
#include <libavutil/hwcontext.h>
#include <opencv2/imgproc.hpp>
#include "common/base/logging/logger.h"
@ -48,6 +50,13 @@ void drawTimeStamp(cv::Mat& image)
const AVCodec* findEncoder(const std::string& codec_name)
{
if (codec_name == "h264_qsv" || codec_name == "H264_QSV") {
return avcodec_find_encoder_by_name("h264_qsv");
}
if (codec_name == "hevc_qsv" || codec_name == "h265_qsv" ||
codec_name == "HEVC_QSV" || codec_name == "H265_QSV") {
return avcodec_find_encoder_by_name("hevc_qsv");
}
if (codec_name == "h264" || codec_name == "H264") {
const AVCodec* codec = avcodec_find_encoder_by_name("libx264");
return codec ? codec : avcodec_find_encoder(AV_CODEC_ID_H264);
@ -73,6 +82,55 @@ AVPixelFormat sourcePixelFormat(const cv::Mat& frame)
return AV_PIX_FMT_NONE;
}
bool encodePreparedFrame(FfmpegEncoderInfo& encoder,
std::vector<uint8_t>& encoded_frame,
bool& is_key)
{
encoder.frame->pts = encoder.frame_pts++;
int ret = avcodec_send_frame(encoder.codec_context, encoder.frame);
if (ret < 0) {
char errbuf[AV_ERROR_MAX_STRING_SIZE] = {0};
av_strerror(ret, errbuf, sizeof(errbuf));
CMVR_LOG(ERROR) << "[CameraStreamEncoder] error sending frame to encoder: " << errbuf;
return false;
}
while (true) {
ret = avcodec_receive_packet(encoder.codec_context, encoder.packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
}
if (ret < 0) {
char errbuf[AV_ERROR_MAX_STRING_SIZE] = {0};
av_strerror(ret, errbuf, sizeof(errbuf));
CMVR_LOG(ERROR) << "[CameraStreamEncoder] error receiving packet from encoder: " << errbuf;
return false;
}
if (encoder.packet->flags & AV_PKT_FLAG_KEY) {
is_key = true;
}
encoded_frame.reserve(encoded_frame.size() + encoder.packet->size);
encoded_frame.insert(encoded_frame.end(),
encoder.packet->data,
encoder.packet->data + encoder.packet->size);
av_packet_unref(encoder.packet);
}
if (encoded_frame.size() < 4) {
return false;
}
const bool has_start_code =
(encoded_frame[0] == 0 && encoded_frame[1] == 0 && encoded_frame[2] == 1) ||
(encoded_frame[0] == 0 && encoded_frame[1] == 0 && encoded_frame[2] == 0 && encoded_frame[3] == 1);
if (!has_start_code) {
CMVR_LOG(ERROR) << "[CameraStreamEncoder] invalid frame: no NALU start code";
return false;
}
return true;
}
} // namespace
FfmpegEncoderInfo::~FfmpegEncoderInfo()
@ -81,6 +139,10 @@ FfmpegEncoderInfo::~FfmpegEncoderInfo()
av_frame_free(&frame);
frame = nullptr;
}
if (transfer_frame) {
av_frame_free(&transfer_frame);
transfer_frame = nullptr;
}
if (packet) {
av_packet_free(&packet);
packet = nullptr;
@ -129,7 +191,17 @@ bool CameraStreamEncoder::init(std::shared_ptr<FfmpegEncoderInfo>& encoder,
ctx->max_b_frames = 0;
ctx->gop_size = 10;
if (codec->id == AV_CODEC_ID_H264) {
const bool is_qsv = codec->name &&
(std::string(codec->name) == "h264_qsv" || std::string(codec->name) == "hevc_qsv");
if (is_qsv) {
// Feed system-memory NV12 frames to oneVPL/QSV. The QSV encoder
// performs the upload and hardware encoding internally.
ctx->pix_fmt = AV_PIX_FMT_NV12;
ctx->bit_rate = 4'000'000;
av_opt_set_int(ctx->priv_data, "async_depth", 1, 0);
av_opt_set_int(ctx->priv_data, "repeat_pps", 1, 0);
} else if (codec->id == AV_CODEC_ID_H264) {
av_opt_set(ctx->priv_data, "preset", "ultrafast", 0);
av_opt_set(ctx->priv_data, "tune", "zerolatency", 0);
av_opt_set(ctx->priv_data, "profile", "baseline", 0);
@ -144,11 +216,13 @@ bool CameraStreamEncoder::init(std::shared_ptr<FfmpegEncoderInfo>& encoder,
av_opt_set(ctx->priv_data, "no-open-gop", "1", 0);
}
const AVPixelFormat* pix_fmts = codec->pix_fmts;
if (!pix_fmts) {
ctx->pix_fmt = AV_PIX_FMT_YUV420P;
} else {
ctx->pix_fmt = pix_fmts[0];
if (!is_qsv) {
const AVPixelFormat* pix_fmts = codec->pix_fmts;
if (!pix_fmts) {
ctx->pix_fmt = AV_PIX_FMT_YUV420P;
} else {
ctx->pix_fmt = pix_fmts[0];
}
}
const int open_ret = avcodec_open2(ctx, codec, nullptr);
@ -179,9 +253,11 @@ bool CameraStreamEncoder::init(std::shared_ptr<FfmpegEncoderInfo>& encoder,
return false;
}
CMVR_LOG(INFO) << "[CameraStreamEncoder] initialized " << codec_name
const char* pixel_format_name = av_get_pix_fmt_name(ctx->pix_fmt);
CMVR_LOG(INFO) << "[CameraStreamEncoder] initialized " << codec->name
<< " encoder, size=" << width << "x" << height
<< ", fps=" << fps;
<< ", fps=" << fps
<< ", pixel_format=" << (pixel_format_name ? pixel_format_name : "unknown");
return true;
}
@ -216,24 +292,30 @@ bool CameraStreamEncoder::encode(std::shared_ptr<FfmpegEncoderInfo>& encoder,
return false;
}
if (encoder->sws_context) {
sws_freeContext(encoder->sws_context);
}
encoder->sws_context = sws_getContext(frame_to_encode.cols,
frame_to_encode.rows,
src_pix_fmt,
encoder->codec_context->width,
encoder->codec_context->height,
encoder->codec_context->pix_fmt,
SWS_BILINEAR,
nullptr,
nullptr,
nullptr);
encoder->sws_context = sws_getCachedContext(encoder->sws_context,
frame_to_encode.cols,
frame_to_encode.rows,
src_pix_fmt,
encoder->codec_context->width,
encoder->codec_context->height,
encoder->codec_context->pix_fmt,
SWS_BILINEAR,
nullptr,
nullptr,
nullptr);
if (!encoder->sws_context) {
CMVR_LOG(ERROR) << "[CameraStreamEncoder] failed to create SwsContext";
return false;
}
const int writable_ret = av_frame_make_writable(encoder->frame);
if (writable_ret < 0) {
char errbuf[AV_ERROR_MAX_STRING_SIZE] = {0};
av_strerror(writable_ret, errbuf, sizeof(errbuf));
CMVR_LOG(ERROR) << "[CameraStreamEncoder] frame is not writable: " << errbuf;
return false;
}
const uint8_t* src_data[AV_NUM_DATA_POINTERS] = {frame_to_encode.data};
int src_linesize[AV_NUM_DATA_POINTERS] = {static_cast<int>(frame_to_encode.step)};
@ -249,49 +331,125 @@ bool CameraStreamEncoder::encode(std::shared_ptr<FfmpegEncoderInfo>& encoder,
return false;
}
encoder->frame->pts = encoder->frame_pts++;
int ret = avcodec_send_frame(encoder->codec_context, encoder->frame);
if (ret < 0) {
char errbuf[AV_ERROR_MAX_STRING_SIZE] = {0};
av_strerror(ret, errbuf, sizeof(errbuf));
CMVR_LOG(ERROR) << "[CameraStreamEncoder] error sending frame to encoder: " << errbuf;
return encodePreparedFrame(*encoder, encoded_frame, is_key);
}
bool CameraStreamEncoder::encode(std::shared_ptr<FfmpegEncoderInfo>& encoder,
const AVFrame* frame,
std::vector<uint8_t>& encoded_frame,
bool& is_key,
const CameraStreamEncodeOptions& options)
{
encoded_frame.clear();
is_key = false;
if (!encoder || !encoder->codec_context || !encoder->frame || !encoder->packet || !frame) {
return false;
}
while (true) {
ret = avcodec_receive_packet(encoder->codec_context, encoder->packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
const AVFrame* source_frame = frame;
if (frame->hw_frames_ctx) {
if (!encoder->transfer_frame) {
encoder->transfer_frame = av_frame_alloc();
}
if (ret < 0) {
if (!encoder->transfer_frame) {
return false;
}
av_frame_unref(encoder->transfer_frame);
const int transfer_ret = av_hwframe_transfer_data(encoder->transfer_frame, frame, 0);
if (transfer_ret < 0) {
char errbuf[AV_ERROR_MAX_STRING_SIZE] = {0};
av_strerror(ret, errbuf, sizeof(errbuf));
CMVR_LOG(ERROR) << "[CameraStreamEncoder] error receiving packet from encoder: " << errbuf;
break;
av_strerror(transfer_ret, errbuf, sizeof(errbuf));
CMVR_LOG(ERROR) << "[CameraStreamEncoder] failed to download hardware frame: " << errbuf;
return false;
}
if (encoder->packet->flags & AV_PKT_FLAG_KEY) {
is_key = true;
const int props_ret = av_frame_copy_props(encoder->transfer_frame, frame);
if (props_ret < 0) {
char errbuf[AV_ERROR_MAX_STRING_SIZE] = {0};
av_strerror(props_ret, errbuf, sizeof(errbuf));
CMVR_LOG(ERROR) << "[CameraStreamEncoder] failed to copy hardware frame properties: " << errbuf;
return false;
}
encoded_frame.reserve(encoded_frame.size() + encoder->packet->size);
encoded_frame.insert(encoded_frame.end(),
encoder->packet->data,
encoder->packet->data + encoder->packet->size);
av_packet_unref(encoder->packet);
source_frame = encoder->transfer_frame;
}
if (encoded_frame.size() < 4) {
const auto source_format = static_cast<AVPixelFormat>(source_frame->format);
if (source_format == AV_PIX_FMT_NONE) {
CMVR_LOG(ERROR) << "[CameraStreamEncoder] source frame has no pixel format";
return false;
}
const bool has_start_code =
(encoded_frame[0] == 0 && encoded_frame[1] == 0 && encoded_frame[2] == 1) ||
(encoded_frame[0] == 0 && encoded_frame[1] == 0 && encoded_frame[2] == 0 && encoded_frame[3] == 1);
if (!has_start_code) {
CMVR_LOG(ERROR) << "[CameraStreamEncoder] invalid frame: no NALU start code";
const int writable_ret = av_frame_make_writable(encoder->frame);
if (writable_ret < 0) {
char errbuf[AV_ERROR_MAX_STRING_SIZE] = {0};
av_strerror(writable_ret, errbuf, sizeof(errbuf));
CMVR_LOG(ERROR) << "[CameraStreamEncoder] frame is not writable: " << errbuf;
return false;
}
return true;
int copy_ret = 0;
if (source_frame->width == encoder->codec_context->width
&& source_frame->height == encoder->codec_context->height
&& source_format == encoder->codec_context->pix_fmt) {
copy_ret = av_frame_copy(encoder->frame, source_frame);
} else {
encoder->sws_context = sws_getCachedContext(
encoder->sws_context,
source_frame->width,
source_frame->height,
source_format,
encoder->codec_context->width,
encoder->codec_context->height,
encoder->codec_context->pix_fmt,
SWS_BILINEAR,
nullptr,
nullptr,
nullptr);
if (!encoder->sws_context) {
CMVR_LOG(ERROR) << "[CameraStreamEncoder] failed to create native frame conversion context";
return false;
}
const int scaled_rows = sws_scale(
encoder->sws_context,
source_frame->data,
source_frame->linesize,
0,
source_frame->height,
encoder->frame->data,
encoder->frame->linesize);
copy_ret = scaled_rows == encoder->codec_context->height
? 0
: scaled_rows < 0 ? scaled_rows : AVERROR_INVALIDDATA;
}
if (copy_ret < 0) {
char errbuf[AV_ERROR_MAX_STRING_SIZE] = {0};
av_strerror(copy_ret, errbuf, sizeof(errbuf));
CMVR_LOG(ERROR) << "[CameraStreamEncoder] failed to copy native frame: " << errbuf;
return false;
}
if (options.draw_timestamp) {
const auto output_format = static_cast<AVPixelFormat>(encoder->frame->format);
if (output_format != AV_PIX_FMT_NV12 && output_format != AV_PIX_FMT_YUV420P) {
const char* format_name = av_get_pix_fmt_name(output_format);
CMVR_LOG(ERROR) << "[CameraStreamEncoder] cannot draw timestamp on pixel format "
<< (format_name ? format_name : "unknown");
return false;
}
if (!encoder->frame->data[0]
|| encoder->frame->linesize[0] < encoder->frame->width) {
CMVR_LOG(ERROR) << "[CameraStreamEncoder] invalid luma plane for timestamp";
return false;
}
cv::Mat luma_plane(
encoder->frame->height,
encoder->frame->width,
CV_8UC1,
encoder->frame->data[0],
static_cast<size_t>(encoder->frame->linesize[0]));
drawTimeStamp(luma_plane);
}
return encodePreparedFrame(*encoder, encoded_frame, is_key);
}
} // namespace cmvr::device

View File

@ -0,0 +1,122 @@
add_library(hikvision_camera SHARED src/hikvision_camera.cpp)
target_include_directories(hikvision_camera PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
set(HIKVISION_SDK_ROOT "${CMAKE_SOURCE_DIR}/dependency/${ARCH}/third_party/hikvision_sdk/v6.1.11.5")
set(HIKVISION_SDK_LIB_DIR "${HIKVISION_SDK_ROOT}/lib")
set(HIKVISION_SDK_COM_DIR "${HIKVISION_SDK_LIB_DIR}/HCNetSDKCom")
file(GLOB HIKVISION_SDK_LIBS CONFIGURE_DEPENDS
"${HIKVISION_SDK_LIB_DIR}/*.so"
"${HIKVISION_SDK_LIB_DIR}/*.so.*"
)
file(GLOB HIKVISION_SDK_COM_LIBS CONFIGURE_DEPENDS
"${HIKVISION_SDK_COM_DIR}/*.so"
"${HIKVISION_SDK_COM_DIR}/*.so.*"
)
set(HIKVISION_HCNETSDK_LIB "${HIKVISION_SDK_LIB_DIR}/libhcnetsdk.so")
if(NOT HIKVISION_SDK_LIBS)
message(FATAL_ERROR "Hikvision SDK libraries not found: ${HIKVISION_SDK_LIB_DIR}")
endif()
if(NOT HIKVISION_SDK_COM_LIBS)
message(FATAL_ERROR "Hikvision SDK component libraries not found: ${HIKVISION_SDK_COM_DIR}")
endif()
if(NOT EXISTS "${HIKVISION_HCNETSDK_LIB}")
message(FATAL_ERROR "Hikvision HCNetSDK library not found: ${HIKVISION_HCNETSDK_LIB}")
endif()
target_include_directories(hikvision_camera PRIVATE "${HIKVISION_SDK_ROOT}/include")
target_link_directories(hikvision_camera PRIVATE "${HIKVISION_SDK_LIB_DIR}" "${HIKVISION_SDK_COM_DIR}")
set(HIKVISION_JSONCPP_LINK jsoncpp)
if(TARGET jsoncpp)
get_target_property(HIKVISION_JSONCPP_IMPORTED_LOCATION jsoncpp IMPORTED_LOCATION)
get_target_property(HIKVISION_JSONCPP_IMPORTED_LOCATION_RELEASE jsoncpp IMPORTED_LOCATION_RELEASE)
get_target_property(HIKVISION_JSONCPP_INTERFACE_INCLUDES jsoncpp INTERFACE_INCLUDE_DIRECTORIES)
message(STATUS "[hikvision_camera] jsoncpp target: jsoncpp")
message(STATUS "[hikvision_camera] jsoncpp IMPORTED_LOCATION: ${HIKVISION_JSONCPP_IMPORTED_LOCATION}")
message(STATUS "[hikvision_camera] jsoncpp IMPORTED_LOCATION_RELEASE: ${HIKVISION_JSONCPP_IMPORTED_LOCATION_RELEASE}")
message(STATUS "[hikvision_camera] jsoncpp INTERFACE_INCLUDE_DIRECTORIES: ${HIKVISION_JSONCPP_INTERFACE_INCLUDES}")
else()
find_library(HIKVISION_JSONCPP_LIBRARY NAMES jsoncpp)
if(HIKVISION_JSONCPP_LIBRARY)
set(HIKVISION_JSONCPP_LINK "${HIKVISION_JSONCPP_LIBRARY}")
message(STATUS "[hikvision_camera] jsoncpp library: ${HIKVISION_JSONCPP_LIBRARY}")
else()
message(WARNING "[hikvision_camera] jsoncpp library not found by CMake; linker will resolve -ljsoncpp")
endif()
endif()
find_path(HIKVISION_JSONCPP_INCLUDE_DIR NAMES json/json.h PATH_SUFFIXES jsoncpp)
message(STATUS "[hikvision_camera] jsoncpp include dir: ${HIKVISION_JSONCPP_INCLUDE_DIR}")
if(HIKVISION_JSONCPP_INCLUDE_DIR)
target_include_directories(hikvision_camera PRIVATE "${HIKVISION_JSONCPP_INCLUDE_DIR}")
endif()
target_link_libraries(hikvision_camera
PUBLIC
glog
opencv_core
cmvr_es::proto
PRIVATE
"${HIKVISION_HCNETSDK_LIB}"
${HIKVISION_JSONCPP_LINK}
pthread
)
set_target_properties(hikvision_camera PROPERTIES
BUILD_RPATH "${HIKVISION_SDK_LIB_DIR};${HIKVISION_SDK_COM_DIR}"
INSTALL_RPATH "$ORIGIN;$ORIGIN/HCNetSDKCom"
)
add_library(cmvr_es::device::hikvision_camera ALIAS hikvision_camera)
install(TARGETS hikvision_camera LIBRARY DESTINATION lib)
install(FILES ${HIKVISION_SDK_LIBS} DESTINATION lib)
install(DIRECTORY "${HIKVISION_SDK_COM_DIR}" DESTINATION lib)
if(BUILD_TESTING)
add_executable(hikvision_camera_callback_test
tests/hikvision_camera_callback_test.cpp
src/hikvision_camera.cpp
)
target_include_directories(hikvision_camera_callback_test
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
"${HIKVISION_SDK_ROOT}/include"
)
if(HIKVISION_JSONCPP_INCLUDE_DIR)
target_include_directories(
hikvision_camera_callback_test
PRIVATE
"${HIKVISION_JSONCPP_INCLUDE_DIR}"
)
endif()
target_compile_definitions(hikvision_camera_callback_test
PRIVATE
HIKVISION_SDK_LIB_DIR="${HIKVISION_SDK_LIB_DIR}/"
)
# The test supplies a small in-process HCNetSDK fake, so it exercises the
# callback/stop ordering without requiring a camera or loading hcnetsdk.
target_link_libraries(hikvision_camera_callback_test
PRIVATE
glog
opencv_core
cmvr_es::proto
${HIKVISION_JSONCPP_LINK}
pthread
)
add_test(
NAME hikvision_camera_callback_test
COMMAND hikvision_camera_callback_test
)
set_tests_properties(hikvision_camera_callback_test PROPERTIES TIMEOUT 10)
if(UNIX AND NOT APPLE)
get_property(_hikvision_test_library_dirs DIRECTORY PROPERTY LINK_DIRECTORIES)
list(PREPEND _hikvision_test_library_dirs "${CMAKE_BINARY_DIR}/cmvr_compiler_runtime")
list(JOIN _hikvision_test_library_dirs ":" _hikvision_test_library_path)
set_tests_properties(hikvision_camera_callback_test PROPERTIES
ENVIRONMENT "LD_LIBRARY_PATH=${_hikvision_test_library_path}"
)
endif()
endif()

View File

@ -0,0 +1,117 @@
#ifndef CMVR_ES_HIKVISION_CAMERA_H
#define CMVR_ES_HIKVISION_CAMERA_H
#include <cstdint>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include "common/base/ring_buffer.h"
#include "devices/camera/abstract_camera.h"
namespace cmvr::device {
class HikvisionCamera final : public AbstractCamera {
public:
explicit HikvisionCamera(const cmvr::config::HikvisionCameraConfig& camera);
~HikvisionCamera() override;
std::string typeName() const override { return "HikvisionCamera"; }
bool init() override;
bool start() override;
bool stop() override;
void getState(CameraState& state) override;
void getRGBImage(cv::Mat& color, Rs2Intrinsics& intrinsics) override;
void getDepthImage(cv::Mat& depth, Rs2Intrinsics& intrinsics) override;
void getRGBDImages(cv::Mat& color, cv::Mat& depth, Rs2Intrinsics& intrinsics) override;
void startRecording(const std::string& video_path) override;
void stopRecording() override;
void pauseRecording() override;
void resumeRecording() override;
void getEncodedFrame(StreamFrameData& frame_data, size_t& index) override;
bool waitEncodedFrame(StreamFrameData& frame_data, size_t& index, std::chrono::milliseconds timeout) override;
bool getLatestEncodedFrame(StreamFrameData& frame_data, size_t& next_index) override;
bool startStreaming() override;
void stopStreaming() override;
bool controlPtz(PtzCommand command, bool stop, int speed) override;
bool executeJsonCommand(const std::string& request_json, std::string& response_json) override;
bool requestKeyFrame() override;
void onEsData(long real_handle,
unsigned int packet_type,
unsigned char* buffer,
unsigned int buffer_size,
unsigned int width,
unsigned int height,
uint64_t source_timestamp,
uint64_t source_frame_number,
unsigned int source_frame_rate,
unsigned int source_packet_mode);
private:
bool initSdk_();
void releaseSdk_();
bool login_();
bool startPreview_();
void stopPreview_();
bool requestKeyFrame_();
void stopRecordingUnlocked_();
void fillIntrinsics_(Rs2Intrinsics& intrinsics) const;
void setError_(const std::string& message);
std::string sdkError_(const std::string& action) const;
void pushEncodedFrame_(const unsigned char* buffer,
unsigned int buffer_size,
bool is_key_frame,
unsigned int width,
unsigned int height,
uint64_t source_timestamp,
uint64_t source_frame_number,
unsigned int source_frame_rate,
unsigned int source_packet_mode);
void resetStreamState_();
config::HikvisionCameraConfig camera_;
std::string ip_;
std::string username_;
std::string password_;
std::string sdk_path_;
int port_ = 8000;
int channel_ = 1;
int stream_type_ = 0;
int link_mode_ = 0;
int fps_ = 25;
int width_ = 0;
int height_ = 0;
size_t buffer_size_ = 30;
std::string codec_ = "H264";
int user_id_ = -1;
int real_handle_ = -1;
bool sdk_acquired_ = false;
std::shared_ptr<SPMCRingBuffer<StreamFrameData>> stream_frame_buffer_;
std::string current_video_path_;
mutable std::mutex ctrl_mtx_;
// SDK callbacks must never take ctrl_mtx_: NET_DVR_StopRealPlay may wait
// for an in-flight callback while stop() owns that mutex. This mutex is the
// single synchronization domain for callback publication and stream state.
mutable std::mutex callback_mtx_;
bool callback_publishing_enabled_ = false;
long callback_preview_handle_ = -1;
std::vector<uint8_t> es_stream_header_;
bool has_es_stream_header_ = false;
bool awaiting_key_frame_ = false;
int stream_count_ = 0;
uint64_t stream_epoch_ = 0;
uint64_t stream_sequence_ = 0;
uint32_t codec_config_generation_ = 0;
};
} // namespace cmvr::device
#endif // CMVR_ES_HIKVISION_CAMERA_H

View File

@ -0,0 +1,961 @@
#include "../include/hikvision_camera.h"
#include <algorithm>
#include <atomic>
#include <cctype>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <limits.h>
#include <sstream>
#include <string>
#include <vector>
#if defined(__linux__)
#include <unistd.h>
#endif
#include "HCNetSDK.h"
#include "common/base/logging/logger.h"
#include "json/json.h"
namespace {
std::mutex g_sdk_mutex;
int g_sdk_ref_count = 0;
bool g_sdk_initialized = false;
std::atomic<int> g_ignored_data_type_log_count{0};
std::atomic<int> g_es_video_log_count{0};
std::atomic<int> g_unexpected_packet_mode_log_count{0};
constexpr DWORD kHikvisionPacketFileHeader = 0;
constexpr DWORD kHikvisionPacketVideoIFrame = 1;
constexpr DWORD kHikvisionPacketVideoBFrame = 2;
constexpr DWORD kHikvisionPacketVideoPFrame = 3;
std::string normalizeSdkPath(std::string path)
{
if (path.empty()) {
return path;
}
const char last = path.back();
if (last != '/' && last != '\\') {
path.push_back('/');
}
return path;
}
std::string defaultRuntimeSdkPath()
{
#if defined(__linux__)
char executable_path[PATH_MAX] = {0};
const ssize_t count = readlink("/proc/self/exe", executable_path, PATH_MAX - 1);
if (count > 0) {
executable_path[count] = '\0';
const auto lib_path =
std::filesystem::path(executable_path).parent_path() / ".." / "lib";
return normalizeSdkPath(std::filesystem::weakly_canonical(lib_path).string());
}
#endif
return normalizeSdkPath((std::filesystem::current_path() / ".." / "lib").string());
}
void copyCString(char* dest, size_t dest_size, const std::string& source)
{
if (dest_size == 0) {
return;
}
std::snprintf(dest, dest_size, "%s", source.c_str());
}
DWORD toHikvisionPtzCommand(cmvr::device::PtzCommand command)
{
switch (command) {
case cmvr::device::PtzCommand::TiltUp:
return TILT_UP;
case cmvr::device::PtzCommand::TiltDown:
return TILT_DOWN;
case cmvr::device::PtzCommand::PanLeft:
return PAN_LEFT;
case cmvr::device::PtzCommand::PanRight:
return PAN_RIGHT;
case cmvr::device::PtzCommand::UpLeft:
return UP_LEFT;
case cmvr::device::PtzCommand::UpRight:
return UP_RIGHT;
case cmvr::device::PtzCommand::DownLeft:
return DOWN_LEFT;
case cmvr::device::PtzCommand::DownRight:
return DOWN_RIGHT;
case cmvr::device::PtzCommand::ZoomIn:
return ZOOM_IN;
case cmvr::device::PtzCommand::ZoomOut:
return ZOOM_OUT;
case cmvr::device::PtzCommand::PanAuto:
return PAN_AUTO;
}
return 0;
}
DWORD normalizePtzSpeed(int speed)
{
if (speed <= 0) {
return 4;
}
if (speed > 7) {
return 7;
}
return static_cast<DWORD>(speed);
}
std::string lowerString(std::string value)
{
std::transform(value.begin(), value.end(), value.begin(), [](unsigned char c) {
return static_cast<char>(std::tolower(c));
});
return value;
}
std::string jsonEscape(const std::string& value)
{
std::ostringstream out;
for (const char c : value) {
switch (c) {
case '\\':
out << "\\\\";
break;
case '"':
out << "\\\"";
break;
case '\n':
out << "\\n";
break;
case '\r':
out << "\\r";
break;
case '\t':
out << "\\t";
break;
default:
out << c;
break;
}
}
return out.str();
}
std::string jsonStringField(const Json::Value& value, const char* name)
{
const Json::Value* member = value.find(name, name + std::strlen(name));
return member && member->isString() ? member->asString() : "";
}
bool jsonBoolField(const Json::Value& value, const char* name, bool& out)
{
const Json::Value* member = value.find(name, name + std::strlen(name));
if (!member || !member->isBool()) {
return false;
}
out = member->asBool();
return true;
}
int jsonIntField(const Json::Value& value, const char* name, int default_value)
{
const Json::Value* member = value.find(name, name + std::strlen(name));
return member && member->isInt() ? member->asInt() : default_value;
}
bool parseJsonCommand(const std::string& request_json, Json::Value& root, std::string& error)
{
Json::CharReaderBuilder builder;
std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
return reader->parse(
request_json.data(), request_json.data() + request_json.size(), &root, &error);
}
bool parsePtzCommandName(const std::string& command, cmvr::device::PtzCommand& out)
{
const std::string normalized = lowerString(command);
if (normalized == "tilt_up" || normalized == "up") {
out = cmvr::device::PtzCommand::TiltUp;
} else if (normalized == "tilt_down" || normalized == "down") {
out = cmvr::device::PtzCommand::TiltDown;
} else if (normalized == "pan_left" || normalized == "left") {
out = cmvr::device::PtzCommand::PanLeft;
} else if (normalized == "pan_right" || normalized == "right") {
out = cmvr::device::PtzCommand::PanRight;
} else if (normalized == "up_left") {
out = cmvr::device::PtzCommand::UpLeft;
} else if (normalized == "up_right") {
out = cmvr::device::PtzCommand::UpRight;
} else if (normalized == "down_left") {
out = cmvr::device::PtzCommand::DownLeft;
} else if (normalized == "down_right") {
out = cmvr::device::PtzCommand::DownRight;
} else if (normalized == "zoom_in") {
out = cmvr::device::PtzCommand::ZoomIn;
} else if (normalized == "zoom_out") {
out = cmvr::device::PtzCommand::ZoomOut;
} else if (normalized == "pan_auto" || normalized == "auto") {
out = cmvr::device::PtzCommand::PanAuto;
} else {
return false;
}
return true;
}
std::string makeJsonResult(bool success, const std::string& error_message = "")
{
std::ostringstream out;
out << "{\"success\":" << (success ? "true" : "false");
if (!error_message.empty()) {
out << ",\"error_message\":\"" << jsonEscape(error_message) << "\"";
}
out << "}";
return out.str();
}
void CALLBACK hikvisionEsRealPlayCallback(
LONG real_handle, NET_DVR_PACKET_INFO_EX* packet_info, void* user)
{
auto* camera = static_cast<cmvr::device::HikvisionCamera*>(user);
if (!camera || !packet_info) {
return;
}
const uint64_t source_timestamp =
(static_cast<uint64_t>(packet_info->dwTimeStampHigh) << 32U) |
static_cast<uint64_t>(packet_info->dwTimeStamp);
camera->onEsData(real_handle,
packet_info->dwPacketType,
packet_info->pPacketBuffer,
packet_info->dwPacketSize,
packet_info->wWidth,
packet_info->wHeight,
source_timestamp,
packet_info->dwFrameNum,
packet_info->dwFrameRate,
packet_info->dwPacketMode);
}
} // namespace
namespace cmvr::device {
HikvisionCamera::HikvisionCamera(const config::HikvisionCameraConfig& camera)
: camera_(camera)
{
id_ = camera_.id();
ip_ = camera_.ip();
username_ = camera_.username().empty() ? "admin" : camera_.username();
password_ = camera_.password();
port_ = camera_.port() > 0 ? camera_.port() : 8000;
channel_ = camera_.channel() > 0 ? camera_.channel() : 1;
stream_type_ = camera_.stream_type() >= 0 ? camera_.stream_type() : 0;
link_mode_ = camera_.link_mode() >= 0 ? camera_.link_mode() : 0;
fps_ = camera_.fps() > 0 ? camera_.fps() : 25;
width_ = camera_.width();
height_ = camera_.height();
buffer_size_ = camera_.buffer_size() > 0 ? static_cast<size_t>(camera_.buffer_size()) : 30;
codec_ = camera_.codec().empty() ? "H264" : camera_.codec();
sdk_path_ = normalizeSdkPath(
camera_.sdk_path().empty() ? defaultRuntimeSdkPath() : camera_.sdk_path());
// Keep the shared_ptr itself immutable after construction. Readers and the
// SDK callback may use it concurrently; the ring buffer owns its locking.
stream_frame_buffer_ = std::make_shared<SPMCRingBuffer<StreamFrameData>>(buffer_size_);
state_.fps = fps_;
state_.width = width_;
state_.height = height_;
if (id_.empty()) {
setError_("[HikvisionCamera] camera id is empty");
} else if (ip_.empty()) {
setError_("[HikvisionCamera] ip is empty");
} else if (password_.empty()) {
setError_("[HikvisionCamera] password is empty");
}
}
HikvisionCamera::~HikvisionCamera()
{
stop();
}
bool HikvisionCamera::init()
{
std::lock_guard lock(ctrl_mtx_);
clear_error_();
state_.is_initialized = false;
if (ip_.empty() || username_.empty() || password_.empty()) {
setError_("ip, username or password is empty");
return false;
}
stream_frame_buffer_->clear();
resetStreamState_();
if (!initSdk_()) {
return false;
}
state_.is_initialized = true;
state_.is_error = false;
CMVR_LOG(INFO) << "[HikvisionCamera] initialized: id=" << id_
<< ", ip=" << ip_
<< ", channel=" << channel_
<< ", stream_type=" << stream_type_;
return true;
}
bool HikvisionCamera::start()
{
std::lock_guard lock(ctrl_mtx_);
clear_error_();
if (!state_.is_initialized) {
setError_("camera not initialized");
return false;
}
if (state_.is_opened) {
return true;
}
if (!login_()) {
return false;
}
if (!startPreview_()) {
if (user_id_ >= 0) {
NET_DVR_Logout(user_id_);
user_id_ = -1;
}
return false;
}
state_.is_opened = true;
return true;
}
bool HikvisionCamera::stop()
{
std::lock_guard lock(ctrl_mtx_);
clear_error_();
if (state_.is_recording) {
stopRecordingUnlocked_();
}
state_.is_streaming = false;
stream_count_ = 0;
resetStreamState_();
stopPreview_();
if (user_id_ >= 0) {
NET_DVR_Logout(user_id_);
user_id_ = -1;
}
state_.is_opened = false;
releaseSdk_();
return true;
}
void HikvisionCamera::getState(CameraState& state)
{
std::lock_guard lock(ctrl_mtx_);
state = state_;
}
void HikvisionCamera::getRGBImage(cv::Mat& color, Rs2Intrinsics& intrinsics)
{
std::lock_guard lock(ctrl_mtx_);
clear_error_();
fillIntrinsics_(intrinsics);
setError_("getRGBImage unsupported: HikvisionCamera only forwards stream data");
color.release();
}
void HikvisionCamera::getDepthImage(cv::Mat& depth, Rs2Intrinsics&)
{
std::lock_guard lock(ctrl_mtx_);
setError_("getDepthImage unsupported usage");
depth.release();
}
void HikvisionCamera::getRGBDImages(cv::Mat& color, cv::Mat& depth, Rs2Intrinsics& intrinsics)
{
getRGBImage(color, intrinsics);
depth.release();
}
void HikvisionCamera::startRecording(const std::string& video_path)
{
std::lock_guard lock(ctrl_mtx_);
clear_error_();
if (!state_.is_opened || real_handle_ < 0) {
setError_("camera not opened");
return;
}
if (state_.is_recording) {
setError_("already recording");
return;
}
if (video_path.empty()) {
setError_("video path is empty");
return;
}
current_video_path_ = video_path;
if (!NET_DVR_SaveRealData(real_handle_, const_cast<char*>(current_video_path_.c_str()))) {
setError_(sdkError_("NET_DVR_SaveRealData"));
current_video_path_.clear();
return;
}
state_.is_recording = true;
}
void HikvisionCamera::stopRecording()
{
std::lock_guard lock(ctrl_mtx_);
clear_error_();
stopRecordingUnlocked_();
}
void HikvisionCamera::stopRecordingUnlocked_()
{
clear_error_();
if (!state_.is_recording) {
return;
}
if (real_handle_ >= 0 && !NET_DVR_StopSaveRealData(real_handle_)) {
setError_(sdkError_("NET_DVR_StopSaveRealData"));
return;
}
state_.is_recording = false;
current_video_path_.clear();
}
void HikvisionCamera::pauseRecording()
{
std::lock_guard lock(ctrl_mtx_);
setError_("pauseRecording unsupported by Hikvision SDK recording");
}
void HikvisionCamera::resumeRecording()
{
std::lock_guard lock(ctrl_mtx_);
setError_("resumeRecording unsupported by Hikvision SDK recording");
}
void HikvisionCamera::getEncodedFrame(StreamFrameData& frame_data, size_t& index)
{
if (!stream_frame_buffer_) {
return;
}
auto frame = stream_frame_buffer_->pop(index);
if (frame) {
frame_data = std::move(*frame);
}
}
bool HikvisionCamera::waitEncodedFrame(
StreamFrameData& frame_data,
size_t& index,
const std::chrono::milliseconds timeout)
{
if (!stream_frame_buffer_) {
return false;
}
auto frame = stream_frame_buffer_->waitPop(index, timeout);
if (!frame) {
return false;
}
frame_data = std::move(*frame);
return !frame_data.rgbFrame.empty() || !frame_data.depthFrame.empty();
}
bool HikvisionCamera::getLatestEncodedFrame(StreamFrameData& frame_data, size_t& next_index)
{
if (!stream_frame_buffer_) {
return false;
}
auto frame = stream_frame_buffer_->getLatest(next_index);
if (!frame.has_value()) {
return false;
}
frame_data = frame.value();
return true;
}
bool HikvisionCamera::startStreaming()
{
std::lock_guard lock(ctrl_mtx_);
clear_error_();
if (!state_.is_opened) {
setError_("camera not opened");
return false;
}
const bool first_stream = stream_count_ == 0;
if (first_stream) {
if (stream_frame_buffer_) {
stream_frame_buffer_->clear();
}
std::lock_guard callback_lock(callback_mtx_);
if (callback_preview_handle_ < 0 ||
callback_preview_handle_ != static_cast<long>(real_handle_)) {
setError_("preview callback is not active");
return false;
}
++stream_epoch_;
stream_sequence_ = 0;
++codec_config_generation_;
awaiting_key_frame_ = true;
callback_publishing_enabled_ = true;
}
state_.is_streaming = true;
++stream_count_;
if (first_stream && !requestKeyFrame_()) {
// Keep waiting for the next natural I-frame. Publishing P/B frames
// immediately would make a newly attached decoder start corrupted.
CMVR_LOG(WARNING) << "[HikvisionCamera] key-frame request failed; "
<< "waiting for the next natural I-frame";
}
return true;
}
void HikvisionCamera::stopStreaming()
{
std::lock_guard lock(ctrl_mtx_);
if (stream_count_ > 0) {
--stream_count_;
}
if (stream_count_ == 0) {
// Wait for an already-running callback to finish its publication, then
// prevent both queued and future callbacks from publishing. No frame
// can be pushed after this critical section has completed.
std::lock_guard callback_lock(callback_mtx_);
callback_publishing_enabled_ = false;
awaiting_key_frame_ = false;
state_.is_streaming = false;
}
}
bool HikvisionCamera::controlPtz(PtzCommand command, bool stop, int speed)
{
std::lock_guard lock(ctrl_mtx_);
clear_error_();
if (!state_.is_opened || user_id_ < 0) {
setError_("camera not opened");
return false;
}
const DWORD sdk_command = toHikvisionPtzCommand(command);
if (sdk_command == 0) {
setError_("unsupported PTZ command");
return false;
}
const DWORD sdk_stop = stop ? 1 : 0;
const DWORD sdk_speed = normalizePtzSpeed(speed);
if (!NET_DVR_PTZControlWithSpeed_Other(user_id_, channel_, sdk_command, sdk_stop, sdk_speed)) {
setError_(sdkError_("NET_DVR_PTZControlWithSpeed_Other"));
return false;
}
return true;
}
bool HikvisionCamera::requestKeyFrame()
{
std::lock_guard lock(ctrl_mtx_);
clear_error_();
if (!state_.is_opened || user_id_ < 0) {
setError_("camera not opened");
return false;
}
if (!requestKeyFrame_()) {
setError_(sdkError_(
stream_type_ == 0 ? "NET_DVR_MakeKeyFrame" : "NET_DVR_MakeKeyFrameSub"));
return false;
}
return true;
}
bool HikvisionCamera::requestKeyFrame_()
{
if (user_id_ < 0) {
CMVR_LOG(WARNING) << "[HikvisionCamera] skip key frame request: camera not logged in";
return false;
}
const bool is_sub_stream = stream_type_ != 0;
const BOOL success = is_sub_stream
? NET_DVR_MakeKeyFrameSub(user_id_, channel_)
: NET_DVR_MakeKeyFrame(user_id_, channel_);
if (!success) {
CMVR_LOG(WARNING) << "[HikvisionCamera] "
<< (is_sub_stream ? "NET_DVR_MakeKeyFrameSub" : "NET_DVR_MakeKeyFrame")
<< " failed, error_code=" << NET_DVR_GetLastError();
return false;
}
CMVR_LOG(INFO) << "[HikvisionCamera] requested key frame"
<< ", channel=" << channel_
<< ", stream_type=" << stream_type_;
return true;
}
bool HikvisionCamera::executeJsonCommand(const std::string& request_json, std::string& response_json)
{
Json::Value root;
std::string parse_error;
if (!parseJsonCommand(request_json, root, parse_error) || !root.isObject()) {
response_json = makeJsonResult(false, "invalid json: " + parse_error);
return false;
}
const std::string command_type = lowerString(
!jsonStringField(root, "command").empty() ? jsonStringField(root, "command") :
!jsonStringField(root, "type").empty() ? jsonStringField(root, "type") :
jsonStringField(root, "action"));
if (command_type != "ptz") {
response_json = makeJsonResult(false, "unsupported json command: " + command_type);
return false;
}
const std::string ptz_command_name =
!jsonStringField(root, "direction").empty() ? jsonStringField(root, "direction") :
!jsonStringField(root, "ptz_command").empty() ? jsonStringField(root, "ptz_command") :
jsonStringField(root, "operation");
PtzCommand ptz_command{};
if (!parsePtzCommandName(ptz_command_name, ptz_command)) {
response_json = makeJsonResult(false, "invalid ptz command: " + ptz_command_name);
return false;
}
bool stop = false;
if (!jsonBoolField(root, "stop", stop)) {
const std::string ptz_action = lowerString(jsonStringField(root, "ptz_action"));
const std::string action = lowerString(jsonStringField(root, "action"));
stop = ptz_action == "stop" || action == "stop";
}
const int speed = jsonIntField(root, "speed", 0);
if (!controlPtz(ptz_command, stop, speed)) {
CameraState state;
getState(state);
response_json = makeJsonResult(
false,
state.error_message.empty() ? "failed to control PTZ" : state.error_message);
return false;
}
std::ostringstream result;
result << "{\"success\":true"
<< ",\"command\":\"ptz\""
<< ",\"direction\":\"" << jsonEscape(ptz_command_name) << "\""
<< ",\"stop\":" << (stop ? "true" : "false")
<< ",\"speed\":" << static_cast<int>(normalizePtzSpeed(speed))
<< "}";
response_json = result.str();
return true;
}
void HikvisionCamera::onEsData(
const long real_handle,
const unsigned int packet_type,
unsigned char* buffer,
const unsigned int buffer_size,
const unsigned int packet_width,
const unsigned int packet_height,
const uint64_t source_timestamp,
const uint64_t source_frame_number,
const unsigned int source_frame_rate,
const unsigned int source_packet_mode)
{
if (!buffer || buffer_size == 0) {
return;
}
// Never take ctrl_mtx_ from an SDK callback. NET_DVR_StopRealPlay may wait
// for this callback while stop() owns ctrl_mtx_. Holding callback_mtx_
// through publication also preserves the ring's single-producer contract.
std::lock_guard callback_lock(callback_mtx_);
if (callback_preview_handle_ != real_handle || !stream_frame_buffer_) {
return;
}
if (packet_type == kHikvisionPacketFileHeader) {
const bool changed =
!has_es_stream_header_ ||
es_stream_header_.size() != buffer_size ||
!std::equal(es_stream_header_.begin(), es_stream_header_.end(), buffer);
if (changed) {
es_stream_header_.assign(buffer, buffer + buffer_size);
has_es_stream_header_ = true;
++codec_config_generation_;
if (callback_publishing_enabled_) {
awaiting_key_frame_ = true;
}
CMVR_LOG(INFO) << "[HikvisionCamera] received ES stream header, size=" << buffer_size;
}
return;
}
const bool is_video_packet =
packet_type == kHikvisionPacketVideoIFrame ||
packet_type == kHikvisionPacketVideoBFrame ||
packet_type == kHikvisionPacketVideoPFrame;
if (!is_video_packet) {
const int log_count = g_ignored_data_type_log_count.fetch_add(1);
if (log_count < 10) {
CMVR_LOG(INFO) << "[HikvisionCamera] ignore ES packet_type="
<< packet_type << ", size=" << buffer_size;
}
return;
}
const int video_log_count = g_es_video_log_count.fetch_add(1);
if (video_log_count < 10) {
CMVR_LOG(INFO) << "[HikvisionCamera] ES video packet_type="
<< packet_type << ", size=" << buffer_size
<< ", source_timestamp=" << source_timestamp
<< ", source_frame_number=" << source_frame_number
<< ", source_frame_rate=" << source_frame_rate
<< ", source_packet_mode=" << source_packet_mode;
}
if (!callback_publishing_enabled_) {
return;
}
const bool is_key_frame = packet_type == kHikvisionPacketVideoIFrame;
if (awaiting_key_frame_) {
if (!is_key_frame) {
return;
}
awaiting_key_frame_ = false;
CMVR_LOG(INFO) << "[HikvisionCamera] received first key frame after stream start";
}
pushEncodedFrame_(buffer,
buffer_size,
is_key_frame,
packet_width,
packet_height,
source_timestamp,
source_frame_number,
source_frame_rate,
source_packet_mode);
}
void HikvisionCamera::pushEncodedFrame_(
const unsigned char* buffer,
unsigned int buffer_size,
bool is_key_frame,
unsigned int packet_width,
unsigned int packet_height,
uint64_t source_timestamp,
uint64_t source_frame_number,
unsigned int source_frame_rate,
unsigned int source_packet_mode)
{
if (!buffer || buffer_size == 0 || !stream_frame_buffer_) {
return;
}
StreamFrameData frame_data;
const auto capture_monotonic = std::chrono::steady_clock::now();
const auto capture_utc = std::chrono::system_clock::now();
frame_data.rgbFrame.assign(buffer, buffer + buffer_size);
frame_data.codec = codec_;
frame_data.fps =
source_frame_rate >= 1U && source_frame_rate <= 1000U
? static_cast<int>(source_frame_rate)
: fps_;
frame_data.width = packet_width > 0 ? static_cast<int>(packet_width) : width_;
frame_data.height = packet_height > 0 ? static_cast<int>(packet_height) : height_;
frame_data.bKey = is_key_frame;
frame_data.stream_epoch = stream_epoch_;
frame_data.sequence = stream_sequence_++;
frame_data.source_timestamp = source_timestamp;
frame_data.source_frame_number = source_frame_number;
frame_data.capture_monotonic_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
capture_monotonic.time_since_epoch()).count();
frame_data.capture_utc_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
capture_utc.time_since_epoch()).count();
frame_data.pts = static_cast<int64_t>(frame_data.sequence);
frame_data.dts = frame_data.pts;
frame_data.time_base_num = 1;
frame_data.time_base_den = std::max(1, frame_data.fps);
frame_data.duration = 1;
frame_data.codec_config_generation = codec_config_generation_;
// Hikvision labels packet type 0 as a "file header", but the bundled SDK
// does not guarantee that it is a decoder-ready VPS/SPS/PPS blob. Keep it
// only for change detection until its format is verified on real hardware.
fillIntrinsics_(frame_data.intrinsics);
if (source_packet_mode > 1U) {
const int log_count = g_unexpected_packet_mode_log_count.fetch_add(1);
if (log_count < 5) {
CMVR_LOG(WARNING) << "[HikvisionCamera] unexpected ES source_packet_mode="
<< source_packet_mode
<< ", source_frame_number=" << source_frame_number;
}
}
stream_frame_buffer_->push(frame_data);
}
void HikvisionCamera::resetStreamState_()
{
std::lock_guard callback_lock(callback_mtx_);
callback_publishing_enabled_ = false;
callback_preview_handle_ = -1;
awaiting_key_frame_ = false;
es_stream_header_.clear();
has_es_stream_header_ = false;
}
bool HikvisionCamera::initSdk_()
{
if (sdk_acquired_) {
return true;
}
std::lock_guard<std::mutex> lock(g_sdk_mutex);
if (g_sdk_ref_count == 0) {
if (!NET_DVR_Init()) {
setError_(sdkError_("NET_DVR_Init"));
return false;
}
NET_DVR_SetConnectTime(3000, 3);
NET_DVR_SetReconnect(10000, TRUE);
NET_DVR_SetLogToFile(3, nullptr, TRUE);
g_sdk_initialized = true;
}
++g_sdk_ref_count;
sdk_acquired_ = true;
return g_sdk_initialized;
}
void HikvisionCamera::releaseSdk_()
{
if (!sdk_acquired_) {
return;
}
std::lock_guard<std::mutex> lock(g_sdk_mutex);
sdk_acquired_ = false;
if (g_sdk_ref_count > 0) {
--g_sdk_ref_count;
}
if (g_sdk_ref_count == 0 && g_sdk_initialized) {
NET_DVR_Cleanup();
g_sdk_initialized = false;
}
}
bool HikvisionCamera::login_()
{
NET_DVR_USER_LOGIN_INFO login_info{};
NET_DVR_DEVICEINFO_V40 device_info{};
copyCString(login_info.sDeviceAddress, sizeof(login_info.sDeviceAddress), ip_);
copyCString(login_info.sUserName, sizeof(login_info.sUserName), username_);
copyCString(login_info.sPassword, sizeof(login_info.sPassword), password_);
login_info.wPort = static_cast<WORD>(port_);
login_info.bUseAsynLogin = FALSE;
user_id_ = NET_DVR_Login_V40(&login_info, &device_info);
if (user_id_ < 0) {
setError_(sdkError_("NET_DVR_Login_V40"));
return false;
}
return true;
}
bool HikvisionCamera::startPreview_()
{
NET_DVR_PREVIEWINFO preview_info{};
preview_info.lChannel = channel_;
preview_info.dwStreamType = static_cast<DWORD>(stream_type_);
preview_info.dwLinkMode = static_cast<DWORD>(link_mode_);
preview_info.hPlayWnd = 0;
preview_info.bBlocked = 1;
preview_info.dwDisplayBufNum = 1;
preview_info.byReconnect = 1;
real_handle_ = NET_DVR_RealPlay_V40(user_id_, &preview_info, nullptr, nullptr);
if (real_handle_ < 0) {
setError_(sdkError_("NET_DVR_RealPlay_V40"));
return false;
}
{
std::lock_guard callback_lock(callback_mtx_);
callback_publishing_enabled_ = false;
callback_preview_handle_ = static_cast<long>(real_handle_);
awaiting_key_frame_ = false;
}
if (!NET_DVR_SetESRealPlayCallBack(real_handle_, hikvisionEsRealPlayCallback, this)) {
setError_(sdkError_("NET_DVR_SetESRealPlayCallBack"));
{
std::lock_guard callback_lock(callback_mtx_);
callback_publishing_enabled_ = false;
callback_preview_handle_ = -1;
awaiting_key_frame_ = false;
}
NET_DVR_StopRealPlay(real_handle_);
real_handle_ = -1;
return false;
}
return true;
}
void HikvisionCamera::stopPreview_()
{
const int preview_handle = real_handle_;
{
// Invalidate before asking the SDK to stop. We intentionally release
// callback_mtx_ before NET_DVR_StopRealPlay because that function may
// wait for an SDK callback to return.
std::lock_guard callback_lock(callback_mtx_);
callback_publishing_enabled_ = false;
callback_preview_handle_ = -1;
awaiting_key_frame_ = false;
}
if (preview_handle >= 0) {
NET_DVR_StopRealPlay(preview_handle);
real_handle_ = -1;
}
}
void HikvisionCamera::fillIntrinsics_(Rs2Intrinsics& intrinsics) const
{
intrinsics.fx = camera_.fx();
intrinsics.fy = camera_.fy();
intrinsics.cx = camera_.cx() > 0.0f ? camera_.cx() : static_cast<float>(width_) * 0.5f;
intrinsics.cy = camera_.cy() > 0.0f ? camera_.cy() : static_cast<float>(height_) * 0.5f;
for (int i = 0; i < 5; ++i) {
intrinsics.coeffs[i] = i < camera_.coeffs_size() ? camera_.coeffs(i) : 0.0f;
}
}
void HikvisionCamera::setError_(const std::string& message)
{
state_.is_error = true;
state_.error_message = message;
CMVR_LOG(ERROR) << "[HikvisionCamera] " << message;
}
std::string HikvisionCamera::sdkError_(const std::string& action) const
{
return action + " failed, error_code=" + std::to_string(NET_DVR_GetLastError());
}
} // namespace cmvr::device

View File

@ -0,0 +1,425 @@
#include "../include/hikvision_camera.h"
#include <atomic>
#include <chrono>
#include <cstdint>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#include "HCNetSDK.h"
namespace {
using EsDataCallback =
void(CALLBACK*)(LONG, NET_DVR_PACKET_INFO_EX*, void*);
constexpr DWORD kFileHeader = 0;
constexpr DWORD kVideoIFrame = 1;
constexpr DWORD kVideoPFrame = 3;
std::mutex g_fake_sdk_mutex;
EsDataCallback g_es_data_callback = nullptr;
void* g_es_data_user = nullptr;
LONG g_real_handle = 42;
std::atomic<int> g_stop_callback_count{0};
std::atomic<int> g_key_frame_request_count{0};
std::atomic<int> g_ptz_call_count{0};
std::atomic<DWORD> g_last_ptz_command{0};
std::atomic<DWORD> g_last_ptz_stop{0};
std::atomic<DWORD> g_last_ptz_speed{0};
void resetFakeSdk()
{
std::lock_guard lock(g_fake_sdk_mutex);
g_es_data_callback = nullptr;
g_es_data_user = nullptr;
g_stop_callback_count = 0;
g_key_frame_request_count = 0;
g_ptz_call_count = 0;
g_last_ptz_command = 0;
g_last_ptz_stop = 0;
g_last_ptz_speed = 0;
}
void emitEsPacket(
const DWORD packet_type,
const LONG real_handle,
std::vector<BYTE> payload,
const WORD width = 640,
const WORD height = 360,
const DWORD timestamp_low = 0,
const DWORD timestamp_high = 0,
const DWORD frame_number = 0,
const DWORD frame_rate = 0,
const DWORD packet_mode = 0)
{
EsDataCallback callback = nullptr;
void* user = nullptr;
{
std::lock_guard lock(g_fake_sdk_mutex);
callback = g_es_data_callback;
user = g_es_data_user;
}
if (!callback) {
return;
}
NET_DVR_PACKET_INFO_EX packet{};
packet.wWidth = width;
packet.wHeight = height;
packet.dwTimeStamp = timestamp_low;
packet.dwTimeStampHigh = timestamp_high;
packet.dwFrameNum = frame_number;
packet.dwFrameRate = frame_rate;
packet.dwPacketType = packet_type;
packet.dwPacketSize = static_cast<DWORD>(payload.size());
packet.pPacketBuffer = payload.data();
packet.dwPacketMode = packet_mode;
callback(real_handle, &packet, user);
}
void emitIFrame(
const LONG real_handle = g_real_handle,
const DWORD timestamp_low = 0,
const DWORD timestamp_high = 0,
const DWORD frame_number = 0,
const DWORD frame_rate = 0,
const DWORD packet_mode = 0)
{
emitEsPacket(
kVideoIFrame,
real_handle,
{0x00, 0x00, 0x00, 0x01, 0x65, 0x88, 0x84, 0x21},
640,
360,
timestamp_low,
timestamp_high,
frame_number,
frame_rate,
packet_mode);
}
void emitPFrame(const LONG real_handle = g_real_handle)
{
emitEsPacket(
kVideoPFrame,
real_handle,
{0x00, 0x00, 0x00, 0x01, 0x41, 0x9A, 0x20});
}
bool check(const bool condition, const char* expression, const int line)
{
if (condition) {
return true;
}
std::cerr << "CHECK failed at line " << line << ": " << expression << '\n';
return false;
}
#define CHECK_TRUE(expression) \
do { \
if (!check(static_cast<bool>(expression), #expression, __LINE__)) { \
return false; \
} \
} while (false)
cmvr::config::HikvisionCameraConfig makeConfig()
{
cmvr::config::HikvisionCameraConfig config;
config.set_id("hikvision_callback_test");
config.set_ip("127.0.0.1");
config.set_username("admin");
config.set_password("test-only");
config.set_port(8000);
config.set_channel(1);
config.set_stream_type(0);
config.set_link_mode(0);
config.set_width(1920);
config.set_height(1080);
config.set_fps(25);
config.set_codec("H264");
config.set_buffer_size(32);
return config;
}
bool testCallbackPublicationLifecycle()
{
resetFakeSdk();
cmvr::device::HikvisionCamera camera(makeConfig());
CHECK_TRUE(camera.init());
CHECK_TRUE(camera.start());
CHECK_TRUE(camera.startStreaming());
CHECK_TRUE(g_key_frame_request_count.load() == 1);
size_t cursor = 0;
cmvr::device::StreamFrameData frame;
// A new subscriber must not receive an undecodable inter frame, and a
// delayed callback from an older preview handle must also be rejected.
emitPFrame();
emitIFrame(g_real_handle - 1);
CHECK_TRUE(!camera.waitEncodedFrame(
frame, cursor, std::chrono::milliseconds(10)));
emitIFrame(
g_real_handle,
0x89ABCDEFU,
0x01234567U,
42U,
30U,
1U);
CHECK_TRUE(camera.waitEncodedFrame(
frame, cursor, std::chrono::milliseconds(50)));
CHECK_TRUE(frame.stream_epoch == 1);
CHECK_TRUE(frame.sequence == 0);
CHECK_TRUE(frame.codec_config_generation == 1);
CHECK_TRUE(frame.bKey);
CHECK_TRUE(frame.width == 640);
CHECK_TRUE(frame.height == 360);
CHECK_TRUE(frame.source_timestamp == 0x0123456789ABCDEFULL);
CHECK_TRUE(frame.source_frame_number == 42U);
CHECK_TRUE(frame.fps == 30);
CHECK_TRUE(frame.time_base_den == 30);
CHECK_TRUE(camera.requestKeyFrame());
CHECK_TRUE(g_key_frame_request_count.load() == 2);
CHECK_TRUE(camera.controlPtz(
cmvr::device::PtzCommand::PanLeft, false, 99));
CHECK_TRUE(g_ptz_call_count.load() == 1);
CHECK_TRUE(g_last_ptz_command.load() == PAN_LEFT);
CHECK_TRUE(g_last_ptz_stop.load() == 0);
CHECK_TRUE(g_last_ptz_speed.load() == 7);
std::string json_response;
CHECK_TRUE(camera.executeJsonCommand(
R"({"command":"ptz","direction":"zoom_in","speed":3})",
json_response));
CHECK_TRUE(json_response.find(R"("success":true)") != std::string::npos);
CHECK_TRUE(g_ptz_call_count.load() == 2);
CHECK_TRUE(g_last_ptz_command.load() == ZOOM_IN);
CHECK_TRUE(g_last_ptz_speed.load() == 3);
// A changed SDK file header invalidates the descriptor generation, but is
// not exposed as decoder config until its vendor-specific format is known.
emitEsPacket(kFileHeader, g_real_handle, {0x01, 0x02, 0x03});
emitPFrame();
CHECK_TRUE(!camera.waitEncodedFrame(
frame, cursor, std::chrono::milliseconds(10)));
emitIFrame(
g_real_handle,
0x76543210U,
0xFEDCBA98U,
99U,
1001U,
0U);
CHECK_TRUE(camera.waitEncodedFrame(
frame, cursor, std::chrono::milliseconds(50)));
CHECK_TRUE(frame.sequence == 1);
CHECK_TRUE(frame.codec_config_generation == 2);
CHECK_TRUE(frame.codec_config.empty());
CHECK_TRUE(frame.source_timestamp == 0xFEDCBA9876543210ULL);
CHECK_TRUE(frame.source_frame_number == 99U);
CHECK_TRUE(frame.fps == 25);
CHECK_TRUE(frame.time_base_den == 25);
constexpr int kConcurrentCallbacks = 8;
std::vector<std::thread> producers;
producers.reserve(kConcurrentCallbacks);
for (int i = 0; i < kConcurrentCallbacks; ++i) {
producers.emplace_back(emitPFrame, g_real_handle);
}
for (auto& producer : producers) {
producer.join();
}
size_t latest_cursor = 0;
cmvr::device::StreamFrameData latest;
CHECK_TRUE(camera.getLatestEncodedFrame(latest, latest_cursor));
CHECK_TRUE(latest.stream_epoch == 1);
CHECK_TRUE(latest.sequence == static_cast<uint64_t>(kConcurrentCallbacks + 1));
CHECK_TRUE(latest_cursor == static_cast<size_t>(kConcurrentCallbacks + 2));
for (int sequence = 2; sequence <= kConcurrentCallbacks + 1; ++sequence) {
CHECK_TRUE(camera.waitEncodedFrame(
frame, cursor, std::chrono::milliseconds(50)));
CHECK_TRUE(frame.stream_epoch == 1);
CHECK_TRUE(frame.sequence == static_cast<uint64_t>(sequence));
}
camera.stopStreaming();
emitIFrame();
CHECK_TRUE(!camera.waitEncodedFrame(
frame, cursor, std::chrono::milliseconds(10)));
CHECK_TRUE(camera.startStreaming());
CHECK_TRUE(g_key_frame_request_count.load() == 3);
emitPFrame();
emitIFrame(g_real_handle - 1);
CHECK_TRUE(!camera.waitEncodedFrame(
frame, cursor, std::chrono::milliseconds(10)));
emitIFrame();
CHECK_TRUE(camera.waitEncodedFrame(
frame, cursor, std::chrono::milliseconds(50)));
CHECK_TRUE(frame.stream_epoch == 2);
CHECK_TRUE(frame.sequence == 0);
CHECK_TRUE(frame.codec_config_generation == 3);
// The fake StopRealPlay invokes the SDK callback synchronously. stop()
// owns ctrl_mtx_ here, proving the callback neither takes that mutex nor
// publishes after the preview handle has been invalidated.
CHECK_TRUE(camera.stop());
CHECK_TRUE(g_stop_callback_count.load() == 1);
CHECK_TRUE(!camera.waitEncodedFrame(
frame, cursor, std::chrono::milliseconds(10)));
return true;
}
} // namespace
extern "C" {
BOOL NET_DVR_Init()
{
return TRUE;
}
BOOL NET_DVR_Cleanup()
{
return TRUE;
}
BOOL NET_DVR_SetConnectTime(DWORD, DWORD)
{
return TRUE;
}
BOOL NET_DVR_SetReconnect(DWORD, BOOL)
{
return TRUE;
}
BOOL NET_DVR_SetLogToFile(DWORD, char*, BOOL)
{
return TRUE;
}
LONG NET_DVR_Login_V40(
LPNET_DVR_USER_LOGIN_INFO,
LPNET_DVR_DEVICEINFO_V40)
{
return 7;
}
BOOL NET_DVR_Logout(LONG)
{
return TRUE;
}
DWORD NET_DVR_GetLastError()
{
return 0;
}
LONG NET_DVR_RealPlay_V40(
LONG,
LPNET_DVR_PREVIEWINFO,
REALDATACALLBACK,
void*)
{
return g_real_handle;
}
BOOL NET_DVR_SetESRealPlayCallBack(
LONG,
EsDataCallback callback,
void* user)
{
std::lock_guard lock(g_fake_sdk_mutex);
g_es_data_callback = callback;
g_es_data_user = user;
return TRUE;
}
BOOL NET_DVR_StopRealPlay(const LONG real_handle)
{
EsDataCallback callback = nullptr;
void* user = nullptr;
{
std::lock_guard lock(g_fake_sdk_mutex);
callback = g_es_data_callback;
user = g_es_data_user;
}
if (callback) {
std::vector<BYTE> idr{
0x00, 0x00, 0x00, 0x01, 0x65, 0x88, 0x84, 0x21
};
NET_DVR_PACKET_INFO_EX packet{};
packet.wWidth = 640;
packet.wHeight = 360;
packet.dwPacketType = kVideoIFrame;
packet.dwPacketSize = static_cast<DWORD>(idr.size());
packet.pPacketBuffer = idr.data();
callback(real_handle, &packet, user);
++g_stop_callback_count;
}
{
std::lock_guard lock(g_fake_sdk_mutex);
g_es_data_callback = nullptr;
g_es_data_user = nullptr;
}
return TRUE;
}
BOOL NET_DVR_SaveRealData(LONG, char*)
{
return TRUE;
}
BOOL NET_DVR_StopSaveRealData(LONG)
{
return TRUE;
}
BOOL NET_DVR_MakeKeyFrame(LONG, LONG)
{
++g_key_frame_request_count;
return TRUE;
}
BOOL NET_DVR_MakeKeyFrameSub(LONG, LONG)
{
++g_key_frame_request_count;
return TRUE;
}
BOOL NET_DVR_PTZControlWithSpeed_Other(
LONG,
LONG,
const DWORD command,
const DWORD stop,
const DWORD speed)
{
++g_ptz_call_count;
g_last_ptz_command = command;
g_last_ptz_stop = stop;
g_last_ptz_speed = speed;
return TRUE;
}
} // extern "C"
int main()
{
if (!testCallbackPublicationLifecycle()) {
return 1;
}
std::cout << "hikvision_camera_callback_test: PASS\n";
return 0;
}

View File

@ -30,6 +30,7 @@ namespace cmvr::device
bool init() override;
bool start() override;
bool stop() override;
void getState(CameraState& state) override;
void getRGBImage(cv::Mat& color, Rs2Intrinsics& intrinsics) override;
void getDepthImage(cv::Mat& depth, Rs2Intrinsics& intrinsics) override;
void getRGBDImages(cv::Mat &color, cv::Mat &depth, Rs2Intrinsics& intrinsics) override;

View File

@ -50,6 +50,11 @@ bool MechmindCamera::init() {
return true;
}
void MechmindCamera::getState(CameraState& state) {
std::lock_guard lock(dev_mtx_);
state = state_;
}
bool MechmindCamera::start() {
try {
std::lock_guard lock(dev_mtx_);
@ -68,6 +73,7 @@ bool MechmindCamera::start() {
return true;
}
catch (const std::exception& e) {
std::lock_guard lock(dev_mtx_);
const string error_msg = "[MechmindCamera] (start): " + string(e.what());
CMVR_LOG(ERROR) << error_msg;
state_.is_error = true;
@ -85,6 +91,7 @@ bool MechmindCamera::stop() {
return true;
}
catch (const exception &e) {
std::lock_guard lock(dev_mtx_);
const string error_msg = "[MechmindCamera] (stop): " + string(e.what());
CMVR_LOG(ERROR) << error_msg;
state_.is_error = true;
@ -141,6 +148,7 @@ void MechmindCamera::getRGBImage(cv::Mat& color, Rs2Intrinsics& intrinsics) {
}
}
catch (const exception &e) {
std::lock_guard lock(dev_mtx_);
const string error_msg = "[MechmindCamera] (getRGBImage): " + string(e.what());
CMVR_LOG(ERROR) << error_msg;
state_.is_error = true;
@ -175,6 +183,7 @@ void MechmindCamera::getDepthImage(cv::Mat& depth, Rs2Intrinsics& intrinsics) {
depth = cv::Mat(depthMap.height(), depthMap.width(), CV_32FC1, depthMap.data());
}
catch (const exception &e) {
std::lock_guard lock(dev_mtx_);
const string error_msg = "[MechmindCamera] (getDepthImage): " + string(e.what());
CMVR_LOG(ERROR) << error_msg;
state_.is_error = true;
@ -258,6 +267,7 @@ void MechmindCamera::getRGBDImages(cv::Mat& color, cv::Mat& depth, Rs2Intrinsics
textured_plc_to_rgbd_(textured_pcl, color, depth);
}
catch (const exception &e) {
std::lock_guard lock(dev_mtx_);
const string error_msg = "[MechmindCamera] (getRGBDImages): " + string(e.what());
CMVR_LOG(ERROR) << error_msg;
state_.is_error = true;
@ -269,12 +279,14 @@ void MechmindCamera::getRGBDImages(cv::Mat& color, cv::Mat& depth, Rs2Intrinsics
}
void MechmindCamera::startRecording(const std::string& video_path) {
std::lock_guard lock(dev_mtx_);
state_.is_error = true;
state_.error_message = "startRecording is not implemented";
CMVR_LOG(ERROR) << "[MechmindCamera] (startRecording): " << state_.error_message;
}
void MechmindCamera::stopRecording() {
std::lock_guard lock(dev_mtx_);
state_.is_error = true;
state_.error_message = "stopRecording is not implemented";
CMVR_LOG(ERROR) << "[MechmindCamera] (stopRecording): " << state_.error_message;

View File

@ -39,6 +39,7 @@ public:
bool init() override;
bool start() override;
bool stop() override;
void getState(CameraState& state) override;
void setFetchRgbdFn(FetchRgbdFn fetch_rgbd_fn);
void setFovyDeg(double fovy_deg);

View File

@ -52,6 +52,12 @@ MujocoCamera::~MujocoCamera()
stop();
}
void MujocoCamera::getState(CameraState& state)
{
std::lock_guard<std::mutex> lock(mtx_);
state = state_;
}
bool MujocoCamera::init()
{
std::lock_guard<std::mutex> lock(mtx_);

View File

@ -24,6 +24,7 @@ namespace cmvr::device{
bool init() override;
bool start() override;
bool stop() override;
void getState(CameraState& state) override;
void getRGBImage(cv::Mat& color, Rs2Intrinsics& intrinsics) override;
void getDepthImage(cv::Mat& depth, Rs2Intrinsics& intrinsics) override;
void getRGBDImages(cv::Mat &color, cv::Mat &depth, Rs2Intrinsics& intrinsics) override;

View File

@ -5,6 +5,8 @@
#include "../include/realsense_camera.h"
#include <cstring>
using namespace std;
using namespace cmvr::device;
@ -143,6 +145,12 @@ RealsenseCamera::~RealsenseCamera() {
}
}
void RealsenseCamera::getState(CameraState& state)
{
std::lock_guard lock(ctrl_mtx_);
state = state_;
}
bool RealsenseCamera::init() {
try {
std::lock_guard lock(ctrl_mtx_);
@ -174,8 +182,9 @@ bool RealsenseCamera::init() {
//初始化编码器
// 初始化RGB编码器(示例参数:640x480,30fps,H.264)
if (!CameraStreamEncoder::init(rgbEncoder_, codec_, encode_width_, encode_height_, fps_)) {
CMVR_LOG(ERROR) << "[RealsenseCamera] (start): Failed to init RGB encoder!";
if (stream_mode_ != DEPTH_MODE &&
!CameraStreamEncoder::init(rgbEncoder_, codec_, encode_width_, encode_height_, fps_)) {
CMVR_LOG(ERROR) << "[RealsenseCamera] (init): Failed to init RGB encoder!";
state_.is_initialized = false;
state_.is_error = true;
state_.error_message = "Failed to init RGB encoder!";
@ -247,16 +256,20 @@ bool RealsenseCamera::start() {
intrinsics_ = Kd_;
}
if (align_mode_ == "color") {
align_ = std::make_shared<rs2::align>(RS2_STREAM_COLOR);
}
else if (align_mode_ == "depth") {
align_ = std::make_shared<rs2::align>(RS2_STREAM_DEPTH);
if (stream_mode_ == RGBD_MODE) {
if (align_mode_ == "color") {
align_ = std::make_shared<rs2::align>(RS2_STREAM_COLOR);
}
else if (align_mode_ == "depth") {
align_ = std::make_shared<rs2::align>(RS2_STREAM_DEPTH);
}
}
// 启动后做一次首帧探测,避免“start 成功但长期无帧”假阳性。
rs2::frameset probe = pipe_.wait_for_frames(kProbeTimeoutMs);
if (!probe.get_color_frame()) {
const bool needs_color = stream_mode_ == COLOR_MODE || stream_mode_ == RGBD_MODE;
const bool needs_depth = stream_mode_ == DEPTH_MODE || stream_mode_ == RGBD_MODE;
if (needs_color && !probe.get_color_frame()) {
last_error = "no color frame after start";
CMVR_LOG(WARNING) << "[RealsenseCamera] (start): " << last_error;
pipe_.stop();
@ -264,7 +277,7 @@ bool RealsenseCamera::start() {
std::this_thread::sleep_for(std::chrono::milliseconds(kRetrySleepMs));
continue;
}
if (stream_mode_ == RGBD_MODE && !probe.get_depth_frame()) {
if (needs_depth && !probe.get_depth_frame()) {
last_error = "no depth frame after start";
CMVR_LOG(WARNING) << "[RealsenseCamera] (start): " << last_error;
pipe_.stop();
@ -308,8 +321,20 @@ bool RealsenseCamera::stop() {
CMVR_LOG(WARNING) << "[RealsenseCamera] (stop): stopRecording failed: " << e.what();
}
}
std::shared_ptr<std::thread> stream_thread_to_join;
{
std::lock_guard lock(ctrl_mtx_);
clear_error_();
stream_count_ = 0;
state_.is_streaming = false;
state_.is_recording = false;
stream_thread_to_join = stream_thread_;
stream_thread_.reset();
}
if (stream_thread_to_join && stream_thread_to_join->joinable()) {
stream_thread_to_join->join();
}
std::lock_guard lock(ctrl_mtx_);
clear_error_();
if (!state_.is_opened || !state_.is_initialized) {
state_.is_opened = false;
return true;
@ -518,9 +543,12 @@ void RealsenseCamera::startRecording(const std::string &video_path) {
// 1. 确定编码格式对应的AVCodecID(H.264/H.265)
AVCodecID codec_id;
if (codec_ == "H264" || codec_ == "h264") {
if (codec_ == "H264" || codec_ == "h264" ||
codec_ == "H264_QSV" || codec_ == "h264_qsv") {
codec_id = AV_CODEC_ID_H264;
} else if (codec_ == "H265" || codec_ == "hevc") {
} else if (codec_ == "H265" || codec_ == "h265" || codec_ == "HEVC" || codec_ == "hevc" ||
codec_ == "H265_QSV" || codec_ == "h265_qsv" ||
codec_ == "HEVC_QSV" || codec_ == "hevc_qsv") {
codec_id = AV_CODEC_ID_HEVC;
} else {
state_.is_error = true;
@ -528,7 +556,8 @@ void RealsenseCamera::startRecording(const std::string &video_path) {
CMVR_LOG(ERROR) << "[RealsenseCamera] (startRecording): " << state_.error_message;
return;
}
const char* format_name = (codec_ == "H265" || codec_ == "h265" || codec_ == "HEVC" || codec_ == "hevc") ? "mp4" : "avi";
const bool is_hevc = codec_id == AV_CODEC_ID_HEVC;
const char* format_name = is_hevc ? "mp4" : "avi";
auto ret = avformat_alloc_output_context2(&format_context_, nullptr, format_name, output_path_.c_str());
// 2. 创建输出格式上下文(封装器核心)
if (ret < 0) {
@ -713,25 +742,31 @@ void RealsenseCamera::streaming_worker_() {
bool success = false;
is_streaming_running = true;
uint64_t frame_sequence = 0;
const uint64_t stream_epoch = static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count());
// 处于流传输或者录像状态时就不退出线程
while (state_.is_streaming || state_.is_recording) {
// 记录当前帧处理开始时间
auto frame_start_time = std::chrono::high_resolution_clock::now();
const auto frame_start_time = std::chrono::steady_clock::now();
rs2::frameset frames;
frames = get_frameset(true);
rs2::frame color_frame = frames.get_color_frame();
rs2::frame depth_frame = frames.get_depth_frame();
if (!color_frame) {
rs2::video_frame color_frame = frames.get_color_frame();
rs2::depth_frame depth_frame = frames.get_depth_frame();
const bool needs_color = stream_mode_ == COLOR_MODE || stream_mode_ == RGBD_MODE;
const bool needs_depth = stream_mode_ == DEPTH_MODE || stream_mode_ == RGBD_MODE;
if (needs_color && !color_frame) {
state_.is_error = true;
state_.error_message = "missing color frame";
CMVR_LOG(ERROR) << "[RealsenseCamera]streaming_worker_: " << state_.error_message;
break;
}
if (stream_mode_ == RGBD_MODE && !depth_frame) {
if (needs_depth && !depth_frame) {
state_.is_error = true;
state_.error_message = "missing depth frame in RGBD mode";
state_.error_message = "missing depth frame";
CMVR_LOG(ERROR) << "[RealsenseCamera]streaming_worker_: " << state_.error_message;
break;
}
@ -747,48 +782,78 @@ void RealsenseCamera::streaming_worker_() {
}
// 保存图像数据
{
cv::Mat temp(cv::Size(width_, height_), CV_8UC3, const_cast<void*>(color_frame.get_data()), cv::Mat::AUTO_STEP);
if (color_frame) {
cv::Mat temp(cv::Size(color_frame.get_width(), color_frame.get_height()),
CV_8UC3, const_cast<void*>(color_frame.get_data()), cv::Mat::AUTO_STEP);
temp.copyTo(frame_data.rgbImage); // 执行深拷贝
}
if (depth_frame) {
auto temp = cv::Mat(cv::Size(width_, height_), CV_16UC1, const_cast<void*>(depth_frame.get_data()));
cv::Mat temp(cv::Size(depth_frame.get_width(), depth_frame.get_height()),
CV_16UC1, const_cast<void*>(depth_frame.get_data()), cv::Mat::AUTO_STEP);
temp.copyTo(frame_data.depthImage); // 执行深拷贝
}
// 记录编码开始时间
if (depth_frame) {
frame_data.depth_width = frame_data.depthImage.cols;
frame_data.depth_height = frame_data.depthImage.rows;
frame_data.depthKey = true;
const size_t depth_bytes = frame_data.depthImage.total() * frame_data.depthImage.elemSize();
frame_data.depthFrame.resize(depth_bytes);
std::memcpy(frame_data.depthFrame.data(), frame_data.depthImage.data, depth_bytes);
}
auto encode_start_time = std::chrono::high_resolution_clock::now();
// rgb图像编码
cv::Mat rgb_to_encode = frame_data.rgbImage;
if (encode_width_ > 0 && encode_height_ > 0 &&
(frame_data.rgbImage.cols != encode_width_ || frame_data.rgbImage.rows != encode_height_)) {
cv::resize(frame_data.rgbImage,
rgb_to_encode,
cv::Size(encode_width_, encode_height_),
0.0,
0.0,
cv::INTER_LINEAR);
success = true;
if (needs_color) {
cv::Mat rgb_to_encode = frame_data.rgbImage;
if (encode_width_ > 0 && encode_height_ > 0 &&
(frame_data.rgbImage.cols != encode_width_ || frame_data.rgbImage.rows != encode_height_)) {
cv::resize(frame_data.rgbImage,
rgb_to_encode,
cv::Size(encode_width_, encode_height_),
0.0,
0.0,
cv::INTER_LINEAR);
}
CameraStreamEncodeOptions encode_options;
encode_options.draw_timestamp = enable_stream_timestamp_;
success = CameraStreamEncoder::encode(rgbEncoder_,
rgb_to_encode,
frame_data.rgbFrame,
frame_data.bKey,
encode_options);
}
CameraStreamEncodeOptions encode_options;
encode_options.draw_timestamp = enable_stream_timestamp_;
success = CameraStreamEncoder::encode(rgbEncoder_,
rgb_to_encode,
frame_data.rgbFrame,
frame_data.bKey,
encode_options);
// 深度图编码
// success = encodeFrameWithEncoder(depthEncoder_, frame_data.depthImage, frame_data.depthFrame, frame_data.depthKey);
if (needs_depth && frame_data.depthFrame.empty())
success = false;
if (success) {
const uint64_t sequence = frame_sequence++;
frame_data.stream_epoch = stream_epoch;
frame_data.sequence = sequence;
const rs2::frame source_frame = color_frame ? color_frame : depth_frame;
frame_data.source_timestamp = source_frame
? static_cast<uint64_t>(source_frame.get_timestamp()) : 0;
frame_data.source_frame_number = source_frame ? source_frame.get_frame_number() : 0;
frame_data.capture_monotonic_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
frame_start_time.time_since_epoch()).count();
frame_data.capture_utc_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
frame_data.pts = static_cast<int64_t>(sequence);
frame_data.dts = frame_data.pts;
frame_data.time_base_num = 1;
frame_data.time_base_den = fps_;
frame_data.duration = 1;
frame_data.fps = fps_;
frame_data.width = encode_width_;
frame_data.height = encode_height_;
frame_data.codec = codec_;
frame_data.width = needs_color ? encode_width_ : frame_data.depth_width;
frame_data.height = needs_color ? encode_height_ : frame_data.depth_height;
frame_data.codec = needs_color ? codec_ : "none";
stream_frame_buffer_->push(frame_data);
}
// 计算从帧开始到现在的总耗时
auto total_duration = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - frame_start_time
std::chrono::steady_clock::now() - frame_start_time
).count();
// 计算需要休眠的时间(确保总耗时达到frame_interval)
@ -955,12 +1020,24 @@ bool RealsenseCamera::startStreaming()
void RealsenseCamera::stopStreaming()
{
std::lock_guard lock(ctrl_mtx_);
stream_count_--;
if (stream_count_ == 0)
std::shared_ptr<std::thread> stream_thread_to_join;
{
std::lock_guard lock(ctrl_mtx_);
if (stream_count_ > 0) {
stream_count_--;
}
if (stream_count_ == 0)
{
// 当前已经没有正在使用的流了,编码采集线程状态修改
state_.is_streaming = false;
state_.is_streaming = false;
if (!state_.is_recording) {
stream_thread_to_join = stream_thread_;
stream_thread_.reset();
}
}
}
if (stream_thread_to_join && stream_thread_to_join->joinable()) {
stream_thread_to_join->join();
}
}

View File

@ -2,7 +2,18 @@ add_library(uvc_camera SHARED src/uvc_camera.cpp)
target_include_directories(uvc_camera PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(uvc_camera PUBLIC glog opencv_core opencv_imgproc cmvr_es::proto cmvr_es::device::camera_stream_encoder)
target_link_libraries(uvc_camera PUBLIC
glog
opencv_core
opencv_imgproc
avcodec
avdevice
avformat
avutil
swscale
cmvr_es::proto
cmvr_es::device::camera_stream_encoder
)
add_library(cmvr_es::device::uvc_camera ALIAS uvc_camera)
install(TARGETS uvc_camera LIBRARY DESTINATION lib)

View File

@ -5,6 +5,9 @@
#ifndef CMVR_ES_UVC_CAMERA_H
#define CMVR_ES_UVC_CAMERA_H
#include <atomic>
#include <condition_variable>
#include "common/base/ring_buffer.h"
#include "camera/abstract_camera.h"
#include "devices/camera/common/include/camera_stream_encoder.h"
@ -29,6 +32,7 @@ namespace cmvr::device {
bool init() override;
bool start() override;
bool stop() override;
void getState(CameraState& state) override;
void getRGBImage(cv::Mat& color, Rs2Intrinsics& intrinsics) override;
void getDepthImage(cv::Mat& depth, Rs2Intrinsics& intrinsics) override;
void getRGBDImages(cv::Mat &color, cv::Mat &depth, Rs2Intrinsics& intrinsics) override;
@ -44,6 +48,16 @@ namespace cmvr::device {
private:
void streaming_worker_();
void recording_worker_();
void cleanup_recording_resources_();
bool open_capture_();
void close_capture_();
void capture_worker_();
bool wait_for_capture_frame_(AVFrame* frame,
uint64_t& last_sequence,
int64_t& capture_monotonic_ns,
int64_t& capture_utc_ns);
bool convert_capture_frame_to_bgr_(const AVFrame* frame, cv::Mat& bgr_frame);
static int interrupt_capture_(void* opaque);
int fps_;
int width_;
@ -51,7 +65,6 @@ namespace cmvr::device {
int encode_width_;
int encode_height_;
std::string serial_;
cv::VideoCapture cap_;
size_t buffer_size_;
std::string codec_;
bool enable_stream_timestamp_{false};
@ -62,13 +75,24 @@ namespace cmvr::device {
std::string current_video_path_;
std::mutex ctrl_mtx_{};
std::unique_ptr<cv::VideoWriter> video_writer_;
std::shared_ptr<std::thread> stream_thread_;
std::shared_ptr<std::thread> recording_thread_;
std::shared_ptr<std::thread> capture_thread_;
cv::Mat latest_frame_; // 存储最新帧
std::mutex frame_mutex_; // 保护最新帧的访问
AVFormatContext* capture_format_context_ = nullptr;
AVCodecContext* capture_decoder_context_ = nullptr;
AVBufferRef* capture_hw_device_context_ = nullptr;
SwsContext* capture_sws_context_ = nullptr;
int capture_video_stream_index_ = -1;
std::atomic<bool> capture_running_{false};
std::mutex capture_frame_mutex_;
std::mutex capture_conversion_mutex_;
std::condition_variable capture_frame_cv_;
AVFrame* latest_capture_frame_ = nullptr;
uint64_t latest_capture_sequence_ = 0;
int64_t latest_capture_monotonic_ns_ = 0;
int64_t latest_capture_utc_ns_ = 0;
std::string capture_error_;
std::string output_path_;
bool is_recording_ = false;

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,9 @@
#ifndef CMVR_ES_DEVICE_TYPES_H
#define CMVR_ES_DEVICE_TYPES_H
#include <cstdint>
#include <string>
#include <vector>
namespace cmvr::device {

View File

@ -10,4 +10,4 @@ target_link_libraries(microphone
cmvr_es::proto
)
add_library(cmvr_es::device::microphone ALIAS microphone)
add_library(cmvr_es::device::microphone ALIAS microphone)

View File

@ -6,6 +6,9 @@
#define CMVR_ES_ABSTRACT_MICROPHONE_H
#pragma once
#include <chrono>
#include <cstddef>
#include "devices/abstract_device.h"
#include "cmvr/config/microphone_config/microphone_config.pb.h"
namespace cmvr::device{
@ -22,6 +25,20 @@ namespace cmvr::device{
virtual void resume() {}
virtual void setVolume(const int volume) {}
virtual int getVolume() {return 0;}
virtual bool startStreaming() {return true;}
virtual void stopStreaming() {}
virtual void getEncodedFrame(AudioStreamFrameData& frame_data, size_t& index) {}
virtual bool waitEncodedFrame(
AudioStreamFrameData& frame_data,
size_t& index,
std::chrono::milliseconds timeout) {
(void)timeout;
getEncodedFrame(frame_data, index);
return !frame_data.data.empty();
}
virtual bool getLatestEncodedFrame(AudioStreamFrameData& frame_data, size_t& next_index) {
return false;
}
protected:
MicrophoneState state_{};

View File

@ -3,26 +3,30 @@
#ifndef CMVR_ES_FFMPEG_MICROPHONE_H
#define CMVR_ES_FFMPEG_MICROPHONE_H
#include <thread>
#include <atomic>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <queue>
#include <condition_variable>
#include "microphone/abstract_microphone.h"
#include <thread>
#include <boost/lockfree/spsc_queue.hpp>
#include "common/base/ring_buffer.h"
#include "microphone/abstract_microphone.h"
#include "speaker/ffmpeg_speaker/include/ffmpeg_ptr.h"
namespace cmvr::device {
class ffmpegMicroPhone final : public AbstractMicrophone {
public:
// 录制状态
enum class RecordingState {
STOPPED,
RECORDING,
PAUSED
};
public:
ffmpegMicroPhone(const config::FFMpegMicroPhoneConfig& cfg);
};
explicit ffmpegMicroPhone(const config::FFMpegMicroPhoneConfig& cfg);
~ffmpegMicroPhone() override;
std::string typeName() const override { return "FFMpegMicroPhone"; }
bool init() override;
@ -36,49 +40,64 @@ namespace cmvr::device {
void setVolume(const int volume) override;
int getVolume() override;
bool startStreaming() override;
void stopStreaming() override;
void getEncodedFrame(AudioStreamFrameData& frame_data, size_t& index) override;
bool waitEncodedFrame(AudioStreamFrameData& frame_data, size_t& index, std::chrono::milliseconds timeout) override;
bool getLatestEncodedFrame(AudioStreamFrameData& frame_data, size_t& next_index) override;
private:
// 音频采集和编码线程
void audioThread();
// 初始化FFmpeg采集和编码
bool initFFmpeg();
// 关闭FFmpeg资源
void closeFFmpeg();
bool startCapture_(bool write_file);
void stopCapture_();
bool recoverStreamingCapture_(int read_error);
static int interruptCallback_(void* opaque);
AudioStreamFormat currentStreamFormat_() const;
void pushEncodedPacket_(const AVPacket* packet);
private:
// FFmpeg采集相关
AVFormatContext* input_fmt_ctx;
AVCodecContext* input_codec_ctx;
AVFrame* input_frame;
// FFmpeg编码相关
AVFormatContext* output_fmt_ctx;
AVOutputFormat* output_fmt;
const AVOutputFormat* output_fmt;
AVStream* audio_st;
AVCodecContext* audio_codec_ctx;
AVCodec* audio_codec;
const AVCodec* audio_codec;
SwrContext* swr_ctx;
AVFrame* audio_frame;
std::string output_file;
std::string format_name;
std::atomic<bool> is_recording;
std::atomic<bool> is_capturing_{false};
std::atomic<bool> interrupt_requested_{false};
std::atomic<bool> write_output_file_{false};
std::atomic<bool> is_paused;
std::mutex pause_mutex;
std::condition_variable pause_cv;
// 音频参数
int sample_rate_;
int channels_;
int64_t next_pts;
private:
std::string input_device_;
std::shared_ptr<std::thread> audio_thread_;
// Serializes FFmpeg context start/stop and keeps a new shared lease from
// starting until the previous capture thread has exited and been joined.
std::mutex capture_mutex_;
std::mutex mtx_;
std::shared_ptr<SPMCRingBuffer<AudioStreamFrameData>> stream_frame_buffer_;
// Protected by mtx_. The audio thread snapshots these values while it
// assigns metadata to an encoded frame.
int stream_count_ = 0;
size_t buffer_size_ = 256;
uint64_t stream_epoch_ = 0;
uint64_t stream_sequence_ = 0;
uint32_t codec_config_generation_ = 0;
config::FFMpegMicroPhoneConfig config_;
};

View File

@ -21,6 +21,8 @@ namespace cmvr::device{
virtual int getVolume() const {return 0;}
virtual void pause() {}
virtual void resume() {}
virtual bool pushAudioFrame(const AudioStreamFrameData& frame_data) { return false; }
virtual void stopStreaming() {}
protected:
SpeakerState state_{};

View File

@ -114,291 +114,6 @@ enum class ImageType {
DEPTH, // 深度图像
GRAYSCALE // 灰度图像
};
class HEVCEncoder {
public:
HEVCEncoder(int width, int height, int fps, int bitrate,
const std::string& deviceName = "/dev/dri/renderD128",
ImageType imageType = ImageType::COLOR)
: width_(width), height_(height), fps_(fps), bitrate_(bitrate),
deviceName_(deviceName), imageType_(imageType) {
initialized_ = init();
}
bool encodeFrame(const cv::Mat& image) {
if (!initialized_ || !codec_ctx_ || !frame_) {
CMVR_LOG(ERROR) << "HEVC encoder is not initialized";
return false;
}
if (!prepareFrame(image)) {
CMVR_LOG(ERROR) << "Failed to prepare frame";
return false;
}
if (avcodec_send_frame(codec_ctx_.get(), frame_.get()) < 0) {
CMVR_LOG(ERROR) << "Error sending frame to encoder";
return false;
}
return true;
}
std::vector<uint8_t> receivePacket(bool& is_key) {
std::vector<uint8_t> packetData;
if (!initialized_ || !codec_ctx_) {
CMVR_LOG(ERROR) << "HEVC encoder is not initialized";
return packetData;
}
FFmpegPtr<AVPacket> packet(av_packet_alloc());
while (true)
{
int ret = avcodec_receive_packet(codec_ctx_.get(), packet.get());
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
CMVR_LOG(ERROR) << "Error during encoding";
break;
}
is_key = (packet->flags & AV_PKT_FLAG_KEY) != 0;
// 预留足够空间,避免多次内存分配
packetData.reserve(packetData.size() + packet->size);
// 复制数据包内容到输出向量
packetData.insert(packetData.end(),
packet->data,
packet->data + packet->size);
av_packet_unref(packet.get());
}
return packetData;
}
private:
bool init() {
// 1. 创建硬件设备上下文
AVBufferRef* hw_device_ctx = nullptr;
int ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI,
deviceName_.c_str(), nullptr, 0);
if (ret < 0) {
char errbuf[AV_ERROR_MAX_STRING_SIZE] = {0};
av_strerror(ret, errbuf, sizeof(errbuf));
CMVR_LOG(ERROR) << "Failed to create VAAPI device context: " << errbuf;
return false;
}
hw_device_ctx_.reset(hw_device_ctx);
// 2. 查找编码器
const AVCodec* codec = avcodec_find_encoder_by_name("hevc_vaapi");
if (!codec) {
CMVR_LOG(ERROR) << "HEVC VAAPI encoder not found";
return false;
}
// 3. 设置编码器上下文
codec_ctx_.reset(avcodec_alloc_context3(codec));
codec_ctx_->width = width_;
codec_ctx_->height = height_;
codec_ctx_->time_base = {1, fps_};
codec_ctx_->pix_fmt = AV_PIX_FMT_VAAPI;
codec_ctx_->bit_rate = bitrate_;
codec_ctx_->gop_size = 1;
codec_ctx_->hw_device_ctx = av_buffer_ref(hw_device_ctx_.get());
// 4. 创建硬件帧上下文
hw_frames_ctx_.reset(av_hwframe_ctx_alloc(hw_device_ctx_.get()));
if (!hw_frames_ctx_) {
CMVR_LOG(ERROR) << "Failed to allocate hardware frame context";
return false;
}
AVHWFramesContext* frames_ctx = (AVHWFramesContext*)hw_frames_ctx_->data;
frames_ctx->format = AV_PIX_FMT_VAAPI;
frames_ctx->sw_format = AV_PIX_FMT_NV12;
frames_ctx->width = width_;
frames_ctx->height = height_;
frames_ctx->initial_pool_size = 20;
if (av_hwframe_ctx_init(hw_frames_ctx_.get()) < 0) {
CMVR_LOG(ERROR) << "Failed to initialize hardware frame context";
return false;
}
codec_ctx_->hw_frames_ctx = av_buffer_ref(hw_frames_ctx_.get());
// 5. 打开编码器
if (avcodec_open2(codec_ctx_.get(), codec, nullptr) < 0) {
CMVR_LOG(ERROR) << "Cannot open encoder";
return false;
}
// 6. 初始化硬件帧
frame_.reset(av_frame_alloc());
frame_->format = AV_PIX_FMT_VAAPI;
frame_->width = width_;
frame_->height = height_;
if (av_hwframe_get_buffer(hw_frames_ctx_.get(), frame_.get(), 0) < 0) {
CMVR_LOG(ERROR) << "Could not allocate hardware frame data";
return false;
}
// 7. 初始化软件帧
sw_frame_.reset(av_frame_alloc());
sw_frame_->width = width_;
sw_frame_->height = height_;
sw_frame_->format = (imageType_ == ImageType::DEPTH) ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_NV12;
if (av_frame_get_buffer(sw_frame_.get(), 0) < 0) {
CMVR_LOG(ERROR) << "Could not allocate software frame buffer";
return false;
}
return true;
}
bool prepareFrame(const cv::Mat& image) {
if (imageType_ == ImageType::DEPTH) {
return prepareDepthFrame(image);
} else {
return prepareColorOrGrayscaleFrame(image);
}
}
bool prepareDepthFrame(const cv::Mat& depthImage) {
cv::Mat processedDepth;
if (depthImage.channels() != 1) {
CMVR_LOG(ERROR) << "Depth image must be single-channel";
return false;
}
if (depthImage.depth() != CV_8U) {
if (depthImage.depth() == CV_16U) {
depthImage.convertTo(processedDepth, CV_8U, 255.0 / 65535.0);
} else {
CMVR_LOG(ERROR) << "Unsupported depth image format";
return false;
}
} else {
processedDepth = depthImage.clone();
}
// 确保图像尺寸匹配
if (processedDepth.cols != width_ || processedDepth.rows != height_) {
CMVR_LOG(ERROR) << "Depth image size mismatch";
return false;
}
// 设置软件帧属性
sw_frame_->pts = frame_counter_++;
sw_frame_->format = AV_PIX_FMT_GRAY8;
av_frame_make_writable(sw_frame_.get());
// 使用av_image_fill_arrays处理行对齐
av_image_fill_arrays(sw_frame_->data, sw_frame_->linesize,
processedDepth.data,
AV_PIX_FMT_GRAY8,
width_, height_, 1);
// 上传到硬件帧
if (av_hwframe_transfer_data(frame_.get(), sw_frame_.get(), 0) < 0) {
CMVR_LOG(ERROR) << "Error transferring depth data to hardware frame";
return false;
}
frame_->pts = sw_frame_->pts;
return true;
}
bool prepareColorOrGrayscaleFrame(const cv::Mat& image) {
if (image.cols != width_ || image.rows != height_) {
CMVR_LOG(ERROR) << "Image size mismatch: " << image.cols << "x" << image.rows
<< " vs " << width_ << "x" << height_;
return false;
}
// 1. 确定输入图像的像素格式(OpenCV的Mat格式)
AVPixelFormat src_pix_fmt;
if (image.channels() == 3) {
src_pix_fmt = AV_PIX_FMT_BGR24; // OpenCV默认是BGR格式(3通道)
} else if (image.channels() == 1) {
src_pix_fmt = AV_PIX_FMT_GRAY8; // 灰度图(1通道)
} else {
CMVR_LOG(ERROR) << "Unsupported channel count: " << image.channels();
return false;
}
// 2. 初始化格式转换上下文(swscale)
SwsContext* sws_ctx = sws_getContext(
width_, height_, src_pix_fmt, // 源宽高和格式
width_, height_, AV_PIX_FMT_NV12, // 目标宽高和格式(NV12)
SWS_BILINEAR, // 缩放算法(可根据需求调整)
nullptr, nullptr, nullptr
);
if (!sws_ctx) {
CMVR_LOG(ERROR) << "Failed to create sws context";
return false;
}
// 3. 配置源数据(OpenCV的Mat)
uint8_t* src_data[4] = {nullptr};
int src_linesize[4] = {0};
if (image.channels() == 3) {
src_data[0] = image.data; // BGR数据起始地址
src_linesize[0] = image.step; // 每行字节数(含OpenCV的对齐填充)
} else {
src_data[0] = image.data; // 灰度数据起始地址
src_linesize[0] = image.step; // 灰度图每行字节数
}
// 4. 配置目标数据(NV12格式的AVFrame)
av_frame_make_writable(sw_frame_.get());
sw_frame_->format = AV_PIX_FMT_NV12;
sw_frame_->width = width_;
sw_frame_->height = height_;
// 5. 执行格式转换(BGR/GRAY -> NV12)
int ret = sws_scale(
sws_ctx,
src_data, // 源图像数据
src_linesize, // 源图像每行字节数
0, // 起始行
height_, // 转换的行数
sw_frame_->data, // 目标图像数据(sw_frame_的data指针)
sw_frame_->linesize // 目标图像每行字节数
);
if (ret <= 0) {
CMVR_LOG(ERROR) << "sws_scale failed, ret=" << ret;
sws_freeContext(sws_ctx);
return false;
}
// 6. 释放转换上下文
sws_freeContext(sws_ctx);
// 7. 设置帧属性并上传到硬件
sw_frame_->pts = frame_counter_++;
if (av_hwframe_transfer_data(frame_.get(), sw_frame_.get(), 0) < 0) {
CMVR_LOG(ERROR) << "Error transferring data to hardware frame";
return false;
}
frame_->pts = sw_frame_->pts;
return true;
}
int width_;
int height_;
int fps_;
int bitrate_;
int frame_counter_ = 0;
std::string deviceName_;
ImageType imageType_;
bool initialized_{false};
FFmpegPtr<AVCodecContext> codec_ctx_;
FFmpegPtr<AVFrame> frame_;
FFmpegPtr<AVFrame> sw_frame_;
FFmpegPtr<AVBufferRef> hw_device_ctx_;
FFmpegPtr<AVBufferRef> hw_frames_ctx_;
};
} // namespace ffmpeg
#endif //FFMPEG_PTR_H

View File

@ -3,9 +3,14 @@
#ifndef CMVR_ES_FFMPEG_SPEAKER_H
#define CMVR_ES_FFMPEG_SPEAKER_H
#include <thread>
#include <mutex>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#include "speaker/abstract_speaker.h"
#include <boost/lockfree/spsc_queue.hpp>
#include <pulse/simple.h>
@ -33,6 +38,8 @@ namespace cmvr::device {
void pause() override;
void resume() override;
void getState(SpeakerState& state) override;
bool pushAudioFrame(const AudioStreamFrameData& frame_data) override;
void stopStreaming() override;
void resetPlayState();
bool initPulseDevice_();
@ -40,6 +47,12 @@ namespace cmvr::device {
private:
void decode_audio_();
void play_audio_();
bool startStreamingPlayback_(const AudioStreamFrameData& frame_data);
bool pushPcmFrame_(const AudioStreamFrameData& frame_data);
bool decodeStreamFrame_(const AudioStreamFrameData& frame_data);
bool initStreamDecoder_(const AudioStreamFrameData& frame_data);
void releaseStreamDecoder_();
void releaseStreamDecoderUnlocked_();
pa_simple* pulse_simple_ = nullptr; // 修改:PulseAudio 简单 API 句柄
pa_sample_spec sample_spec_{}; // 新增:PulseAudio 采样规格
@ -51,6 +64,7 @@ namespace cmvr::device {
std::shared_ptr<std::thread> decode_thread_;
std::shared_ptr<std::thread> play_thread_;
std::mutex mtx_;
std::mutex stop_mtx_;
std::string audio_path_;
// Boost 单生产者单消费者无锁队列
@ -60,10 +74,20 @@ namespace cmvr::device {
mutable std::mutex mtx_pause_;
std::condition_variable cv_pause_;
std::atomic<bool> is_paused_{false};
std::atomic<bool> is_stopping_{false};
// 音频时钟同步
std::atomic<int64_t> audio_pts_{0};
std::chrono::time_point<std::chrono::steady_clock> playback_start_time_;
std::mutex stream_decode_mtx_;
AVCodecContext* stream_decoder_ctx_ = nullptr;
SwrContext* stream_swr_ctx_ = nullptr;
AVFrame* stream_decode_frame_ = nullptr;
AVPacket* stream_decode_packet_ = nullptr;
std::string stream_codec_;
int stream_input_sample_rate_ = 0;
int stream_input_channels_ = 0;
bool is_streaming_input_ = false;
config::FFMpegSpeakerConfig config_;
};

View File

@ -4,6 +4,9 @@
//
#include <filesystem>
#include <algorithm>
#include <climits>
#include <cstring>
#include "../include/ffmpeg_speaker.h"
@ -18,7 +21,7 @@ ffmpegSpeaker::ffmpegSpeaker(const config::FFMpegSpeakerConfig& cfg):config_(cfg
try {
id_ = config_.id();
memset(&sample_spec_, 0, sizeof(sample_spec_));
// state_.volume = config_.volume();
state_.volume = 100;
}
catch (const exception& e) {
CMVR_LOG(ERROR) << "[ffmpegSpeaker] ([ffmpegSpeaker]): Failed to parse config: " << e.what();
@ -28,6 +31,7 @@ ffmpegSpeaker::ffmpegSpeaker(const config::FFMpegSpeakerConfig& cfg):config_(cfg
ffmpegSpeaker::~ffmpegSpeaker() {
is_stopping_ = true;
{
std::lock_guard<std::mutex> lock(mtx_);
state_.is_running = false;
@ -76,6 +80,8 @@ bool ffmpegSpeaker::start() {
void ffmpegSpeaker::resetPlayState()
{
releaseStreamDecoder_();
if (pulse_simple_) {
pa_simple_flush(pulse_simple_, nullptr);
pa_simple_free(pulse_simple_);
@ -89,19 +95,24 @@ void ffmpegSpeaker::resetPlayState()
}
state_.is_initialized = false;
is_streaming_input_ = false;
audio_path_.clear();
CMVR_LOG(INFO) << "[ffmpegSpeaker] (resetPlayState): Success, id=" << id_;
}
bool ffmpegSpeaker::stop() {
std::lock_guard<std::mutex> stop_lock(stop_mtx_);
is_stopping_ = true;
{
lock_guard lock(mtx_);
state_.is_running = false;
state_.is_decoding = false;
state_.is_paused = false;
is_streaming_input_ = false;
}
cv_pause_.notify_all();
// 等待线程结束
if (decode_thread_ && decode_thread_->joinable()) {
decode_thread_->join();
@ -114,6 +125,7 @@ bool ffmpegSpeaker::stop() {
}
resetPlayState();
is_stopping_ = false;
return true;
}
@ -150,6 +162,7 @@ void ffmpegSpeaker::resume() {
return;
}
state_.is_paused = false;
cv_pause_.notify_all();
CMVR_LOG(INFO) << "[ffmpegSpeaker] (resume): Success, id=" << id_;
}
catch (const exception& e) {
@ -291,7 +304,7 @@ void ffmpegSpeaker::decode_audio_() {
return;
}
AVCodec* decoder = nullptr;
const AVCodec* decoder = nullptr;
const int stream_index = av_find_best_stream(fmt_ctx.get(), AVMEDIA_TYPE_AUDIO, -1, -1, &decoder, 0);
if (stream_index < 0) {
{
@ -531,6 +544,310 @@ void ffmpegSpeaker::play_audio_() {
CMVR_LOG(INFO) << "[ffmpegSpeaker] Playback finished";
}
bool ffmpegSpeaker::pushAudioFrame(const AudioStreamFrameData& frame_data)
{
if (is_stopping_) {
return false;
}
if (frame_data.data.empty()) {
return true;
}
bool need_start = false;
bool need_restart = false;
{
std::lock_guard<std::mutex> lock(mtx_);
need_start = !state_.is_running;
need_restart = state_.is_running && is_streaming_input_ &&
(frame_data.sample_rate != sample_rate_ || frame_data.channels != channels_);
if (state_.is_running && !is_streaming_input_) {
CMVR_LOG(ERROR) << "[ffmpegSpeaker] cannot stream audio while file playback is active";
return false;
}
}
if (need_restart) {
stopStreaming();
need_start = true;
}
if (need_start && !startStreamingPlayback_(frame_data)) {
return false;
}
if (frame_data.format == AudioStreamFormat::PCM ||
frame_data.codec == "pcm_s16le" ||
frame_data.codec == "pcm") {
return pushPcmFrame_(frame_data);
}
return decodeStreamFrame_(frame_data);
}
void ffmpegSpeaker::stopStreaming()
{
std::lock_guard<std::mutex> stop_lock(stop_mtx_);
is_stopping_ = true;
{
std::lock_guard<std::mutex> lock(mtx_);
if (!is_streaming_input_) {
is_stopping_ = false;
return;
}
state_.is_decoding = false;
state_.is_paused = false;
is_streaming_input_ = false;
}
cv_pause_.notify_all();
if (play_thread_ && play_thread_->joinable()) {
play_thread_->join();
play_thread_.reset();
}
resetPlayState();
is_stopping_ = false;
}
bool ffmpegSpeaker::startStreamingPlayback_(const AudioStreamFrameData& frame_data)
{
std::lock_guard<std::mutex> lock(mtx_);
sample_rate_ = frame_data.sample_rate > 0 ? frame_data.sample_rate : 44100;
channels_ = frame_data.channels > 0 ? frame_data.channels : 2;
AudioFrame queued_frame;
while (audio_queue_.pop(queued_frame)) {
}
if (!initPulseDevice_()) {
CMVR_LOG(ERROR) << "[ffmpegSpeaker] PulseAudio device initialization failed";
return false;
}
state_.is_running = true;
state_.is_decoding = true;
state_.is_paused = false;
is_streaming_input_ = true;
play_thread_ = std::make_shared<std::thread>([this]() {
try {
this->play_audio_();
} catch (const std::exception& e) {
CMVR_LOG(ERROR) << "[ffmpegSpeaker] Stream play thread exception: " << e.what();
std::lock_guard<std::mutex> lock(mtx_);
state_.is_running = false;
state_.is_decoding = false;
}
});
return true;
}
bool ffmpegSpeaker::pushPcmFrame_(const AudioStreamFrameData& frame_data)
{
const size_t sample_count = frame_data.data.size() / sizeof(int16_t);
if (sample_count == 0) {
return true;
}
auto buffer = std::make_shared<std::vector<int16_t>>(sample_count);
std::memcpy(buffer->data(), frame_data.data.data(), sample_count * sizeof(int16_t));
float volume_scale = 1.0f;
{
std::lock_guard<std::mutex> lock(mtx_);
volume_scale = static_cast<float>(state_.volume) / 100.0f;
}
for (int16_t& sample : *buffer) {
const float scaled = static_cast<float>(sample) * volume_scale;
sample = static_cast<int16_t>(std::clamp(scaled, -32768.f, 32767.f));
}
while (true) {
{
std::lock_guard<std::mutex> lock(mtx_);
if (!state_.is_running || is_stopping_) {
return false;
}
}
if (audio_queue_.push(buffer)) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
return true;
}
bool ffmpegSpeaker::decodeStreamFrame_(const AudioStreamFrameData& frame_data)
{
std::lock_guard<std::mutex> decode_lock(stream_decode_mtx_);
if (!stream_decoder_ctx_ ||
stream_codec_ != frame_data.codec ||
stream_input_sample_rate_ != frame_data.sample_rate ||
stream_input_channels_ != frame_data.channels) {
releaseStreamDecoderUnlocked_();
if (!initStreamDecoder_(frame_data)) {
return false;
}
}
av_packet_unref(stream_decode_packet_);
if (av_new_packet(stream_decode_packet_, static_cast<int>(frame_data.data.size())) < 0) {
return false;
}
std::memcpy(stream_decode_packet_->data, frame_data.data.data(), frame_data.data.size());
stream_decode_packet_->pts = frame_data.pts;
int ret = avcodec_send_packet(stream_decoder_ctx_, stream_decode_packet_);
av_packet_unref(stream_decode_packet_);
if (ret < 0) {
return false;
}
while ((ret = avcodec_receive_frame(stream_decoder_ctx_, stream_decode_frame_)) == 0) {
if (!stream_swr_ctx_) {
stream_swr_ctx_ = swr_alloc_set_opts(nullptr,
av_get_default_channel_layout(channels_),
AV_SAMPLE_FMT_S16,
sample_rate_,
av_get_default_channel_layout(stream_decode_frame_->channels),
static_cast<AVSampleFormat>(stream_decode_frame_->format),
stream_decode_frame_->sample_rate,
0,
nullptr);
if (!stream_swr_ctx_ || swr_init(stream_swr_ctx_) < 0) {
CMVR_LOG(ERROR) << "[ffmpegSpeaker] Failed to init stream resampler";
av_frame_unref(stream_decode_frame_);
return false;
}
}
const int64_t max_samples64 = av_rescale_rnd(
swr_get_delay(stream_swr_ctx_, stream_decode_frame_->sample_rate) + stream_decode_frame_->nb_samples,
sample_rate_,
stream_decode_frame_->sample_rate,
AV_ROUND_UP);
const int max_samples = static_cast<int>(std::min<int64_t>(max_samples64, INT_MAX));
auto buffer = std::make_shared<std::vector<int16_t>>(max_samples * channels_);
uint8_t* out[] = {reinterpret_cast<uint8_t*>(buffer->data()), nullptr};
const int out_samples = swr_convert(stream_swr_ctx_,
out,
max_samples,
const_cast<const uint8_t**>(stream_decode_frame_->data),
stream_decode_frame_->nb_samples);
av_frame_unref(stream_decode_frame_);
if (out_samples <= 0) {
continue;
}
buffer->resize(out_samples * channels_);
float volume_scale = 1.0f;
{
std::lock_guard<std::mutex> lock(mtx_);
volume_scale = static_cast<float>(state_.volume) / 100.0f;
}
for (int16_t& sample : *buffer) {
const float scaled = static_cast<float>(sample) * volume_scale;
sample = static_cast<int16_t>(std::clamp(scaled, -32768.f, 32767.f));
}
while (true) {
{
std::lock_guard<std::mutex> lock(mtx_);
if (!state_.is_running || is_stopping_) {
return false;
}
}
if (audio_queue_.push(buffer)) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
return ret == AVERROR(EAGAIN) || ret == AVERROR_EOF;
}
bool ffmpegSpeaker::initStreamDecoder_(const AudioStreamFrameData& frame_data)
{
const AVCodec* decoder = nullptr;
if (!frame_data.codec.empty()) {
decoder = avcodec_find_decoder_by_name(frame_data.codec.c_str());
}
if (!decoder) {
AVCodecID codec_id = AV_CODEC_ID_NONE;
if (frame_data.format == AudioStreamFormat::AAC) {
codec_id = AV_CODEC_ID_AAC;
} else if (frame_data.format == AudioStreamFormat::MP3) {
codec_id = AV_CODEC_ID_MP3;
}
if (codec_id != AV_CODEC_ID_NONE) {
decoder = avcodec_find_decoder(codec_id);
}
}
if (!decoder) {
CMVR_LOG(ERROR) << "[ffmpegSpeaker] Unsupported stream audio codec: " << frame_data.codec;
return false;
}
stream_decoder_ctx_ = avcodec_alloc_context3(decoder);
if (!stream_decoder_ctx_) {
return false;
}
stream_decoder_ctx_->sample_rate = frame_data.sample_rate > 0 ? frame_data.sample_rate : sample_rate_;
stream_decoder_ctx_->channels = frame_data.channels > 0 ? frame_data.channels : channels_;
stream_decoder_ctx_->channel_layout =
stream_decoder_ctx_->channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
if (avcodec_open2(stream_decoder_ctx_, decoder, nullptr) < 0) {
releaseStreamDecoderUnlocked_();
return false;
}
stream_decode_frame_ = av_frame_alloc();
stream_decode_packet_ = av_packet_alloc();
if (!stream_decode_frame_ || !stream_decode_packet_) {
releaseStreamDecoderUnlocked_();
return false;
}
stream_codec_ = frame_data.codec;
stream_input_sample_rate_ = frame_data.sample_rate;
stream_input_channels_ = frame_data.channels;
return true;
}
void ffmpegSpeaker::releaseStreamDecoder_()
{
std::lock_guard<std::mutex> decode_lock(stream_decode_mtx_);
releaseStreamDecoderUnlocked_();
}
void ffmpegSpeaker::releaseStreamDecoderUnlocked_()
{
if (stream_swr_ctx_) {
swr_free(&stream_swr_ctx_);
}
if (stream_decode_frame_) {
av_frame_free(&stream_decode_frame_);
}
if (stream_decode_packet_) {
av_packet_free(&stream_decode_packet_);
}
if (stream_decoder_ctx_) {
avcodec_free_context(&stream_decoder_ctx_);
}
stream_codec_.clear();
stream_input_sample_rate_ = 0;
stream_input_channels_ = 0;
}
bool ffmpegSpeaker::initPulseDevice_() {
// 验证参数
if (sample_rate_ <= 0 || channels_ <= 0) {

View File

@ -6,12 +6,12 @@
#define CMVR_ES_STATE_DEFINE_H
#include <atomic>
#include <cstdint>
#include <cmath>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
#include <set>
#include <vector>
#include "common/types/geometry_types.h"
@ -34,10 +34,35 @@ namespace cmvr::device{
UNKNOWN
};
// ------------------------------------- AGV -------------------------------------
typedef struct{
enum class AudioStreamFormat {
PCM = 0,
MP3 = 1,
AAC = 2,
WAV = 3,
OPUS = 4,
UNKNOWN = 99
};
} AGVState;
struct AudioStreamFrameData {
std::vector<uint8_t> data;
int sample_rate = 44100;
int channels = 2;
AudioStreamFormat format = AudioStreamFormat::PCM;
std::string codec = "pcm_s16le";
int64_t pts = 0;
int64_t dts = 0;
int nb_samples = 0;
uint64_t stream_epoch = 0;
uint64_t sequence = 0;
int64_t capture_monotonic_ns = 0;
int64_t capture_utc_ns = 0;
int32_t time_base_num = 1;
int32_t time_base_den = 44100;
int64_t duration = 0;
bool discontinuity = false;
uint32_t codec_config_generation = 0;
std::vector<uint8_t> codec_config;
};
// ------------------------------------- robot -------------------------------------
typedef enum {
@ -150,6 +175,10 @@ namespace cmvr::device{
int volume; // 0~100, 支持软音量调节
} SpeakerState;
typedef struct {
}AGVState;
}

View File

@ -4,6 +4,7 @@
#include "common/base/logging/logger.h"
#include "runtime/include/cmvr_runtime.h"
namespace {
bool blockShutdownSignals(sigset_t& shutdown_signals)
@ -16,8 +17,57 @@ bool blockShutdownSignals(sigset_t& shutdown_signals)
} // namespace
namespace fs = std::filesystem;
static bool setupIntelMediaEnvironment()
{
char exe_path_buffer[PATH_MAX] = {};
const ssize_t length = readlink(
"/proc/self/exe",
exe_path_buffer,
sizeof(exe_path_buffer) - 1);
if (length <= 0) {
std::cerr << "Failed to resolve /proc/self/exe\n";
return false;
}
exe_path_buffer[length] = '\0';
const fs::path executable_path(exe_path_buffer);
const fs::path bin_directory = executable_path.parent_path();
const fs::path install_directory = bin_directory.parent_path();
const fs::path library_directory = install_directory / "lib";
const fs::path va_driver_directory = library_directory / "dri";
const fs::path ihd_driver = va_driver_directory / "iHD_drv_video.so";
if (!fs::exists(ihd_driver)) {
std::cerr << "Intel media driver not found: "
<< ihd_driver << '\n';
return false;
}
// overwrite=1:强制 cmvr-es 使用随项目发布的媒体运行时。
setenv("LIBVA_DRIVER_NAME", "iHD", 1);
setenv(
"LIBVA_DRIVERS_PATH",
va_driver_directory.c_str(),
1);
setenv(
"ONEVPL_SEARCH_PATH",
library_directory.c_str(),
1);
return true;
}
int main()
{
if (!setupIntelMediaEnvironment()) {
return EXIT_FAILURE;
}
sigset_t shutdown_signals;
if (!blockShutdownSignals(shutdown_signals)) {
return 1;

View File

@ -0,0 +1,61 @@
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
cmake_minimum_required(VERSION 3.22)
project(cmvr_media_source_hub LANGUAGES CXX)
enable_testing()
endif()
add_library(media_source_hub STATIC
src/media_source_hub.cpp
)
target_compile_features(media_source_hub PUBLIC cxx_std_17)
target_include_directories(media_source_hub
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../..
)
add_library(cmvr_es::media_source_hub ALIAS media_source_hub)
if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
add_library(device_media_source_adapter STATIC
src/device_media_source_adapter.cpp
)
target_compile_features(device_media_source_adapter PUBLIC cxx_std_17)
target_include_directories(device_media_source_adapter
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../..
)
target_link_libraries(device_media_source_adapter
PUBLIC
cmvr_es::media_source_hub
cmvr_es::common
cmvr_es::proto
cmvr_es::logging
)
add_library(cmvr_es::device_media_source_adapter ALIAS device_media_source_adapter)
endif()
option(CMVR_MEDIA_SOURCE_HUB_BUILD_TESTS
"Build the standalone MediaSourceHub self-test"
${PROJECT_IS_TOP_LEVEL})
if(CMVR_MEDIA_SOURCE_HUB_BUILD_TESTS)
find_package(Threads REQUIRED)
add_executable(media_source_hub_test
tests/media_source_hub_test.cpp
)
target_compile_features(media_source_hub_test PRIVATE cxx_std_17)
target_link_libraries(media_source_hub_test
PRIVATE
cmvr_es::media_source_hub
Threads::Threads
)
# This self-test only links the static Hub and pthreads. In the root build,
# the project-wide third-party RUNPATH can otherwise make the loader pick up
# a vendor libstdc++.so (for example from the AUBO SDK), even though the test
# has no dependency on that SDK.
set_target_properties(media_source_hub_test PROPERTIES
SKIP_BUILD_RPATH TRUE
)
add_test(NAME media_source_hub_test COMMAND media_source_hub_test)
endif()

View File

@ -0,0 +1,38 @@
#ifndef CMVR_ES_DEVICE_MEDIA_SOURCE_ADAPTER_H
#define CMVR_ES_DEVICE_MEDIA_SOURCE_ADAPTER_H
#pragma once
#include <cstddef>
#include <memory>
#include <string>
#include "devices/camera/abstract_camera.h"
#include "devices/microphone/abstract_microphone.h"
#include "manager/media_source_hub/include/media_source_hub.h"
namespace cmvr::media {
// Process-wide protocol-neutral media hub shared by gRPC and QUIC services.
MediaSourceHub& globalMediaSourceHub();
std::string cameraColorTrackId(const std::string& device_id);
std::string microphoneTrackId(const std::string& device_id);
// Registration is idempotent for an already registered track. The adapter owns a
// short-lived pump thread and one startStreaming()/stopStreaming() lease only while
// at least one Hub subscription is active. It ensures start() succeeds but deliberately
// does not call stop(), because the base device lifecycle can also be owned by control RPCs.
bool ensureCameraMediaSource(
MediaSourceHub& hub,
const std::shared_ptr<device::AbstractCamera>& camera,
size_t ring_capacity = 64);
bool ensureMicrophoneMediaSource(
MediaSourceHub& hub,
const std::shared_ptr<device::AbstractMicrophone>& microphone,
size_t ring_capacity = 256);
} // namespace cmvr::media
#endif // CMVR_ES_DEVICE_MEDIA_SOURCE_ADAPTER_H

View File

@ -0,0 +1,132 @@
#ifndef CMVR_ES_MANAGER_MEDIA_SOURCE_HUB_H
#define CMVR_ES_MANAGER_MEDIA_SOURCE_HUB_H
#pragma once
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "common/base/ring_buffer.h"
#include "common/media/media_frame.h"
namespace cmvr::media {
// MediaSourceHub owns no protocol-specific state. A device or capture adapter registers
// start/stop callbacks and receives a sink callback when the first consumer subscribes.
class MediaSourceHub final {
public:
using FrameRing = BroadcastFrameRing<MediaFrame>;
using FrameReadResult = FrameRing::ReadResult;
using StartPosition = FrameRing::StartPosition;
using FrameSink = std::function<void(MediaFramePtr)>;
// Cancellation checks run while MediaSourceHub protects source lifecycle
// state. Predicates must therefore be fast, non-blocking and must not call
// back into the same hub.
using CancelPredicate = std::function<bool()>;
struct SourceCallbacks {
// start() may run asynchronously. It must observe cancelled during any
// potentially blocking startup work and return false promptly once set.
// MediaSourceHub retains the callback state until a non-cooperative start
// eventually returns, so late completion cannot access destroyed state.
std::function<bool(
const FrameSink& sink,
const CancelPredicate& cancelled)> start;
// stop() is the synchronous publication barrier for the last lease and
// must unblock and join the source producer before returning.
std::function<void()> stop;
std::function<bool()> request_key_frame;
};
private:
struct SourceState;
public:
class Subscription final {
public:
Subscription() = default;
~Subscription();
Subscription(const Subscription&) = delete;
Subscription& operator=(const Subscription&) = delete;
Subscription(Subscription&& other) noexcept;
Subscription& operator=(Subscription&& other) noexcept;
// A Subscription owns one reader cursor and is single-consumer. Moving,
// resetting, or reading the same object concurrently is unsupported; use
// one independent subscription per consumer thread.
bool valid() const;
explicit operator bool() const { return valid(); }
// Returns the most recently observed immutable descriptor. A callback source may
// replace the initially registered UNKNOWN codec/config descriptor with the first
// real frame descriptor without invalidating existing subscriptions.
TrackDescriptorPtr descriptor() const;
std::optional<FrameReadResult> tryRead();
std::optional<FrameReadResult> waitRead(std::chrono::milliseconds timeout);
uint64_t discardPendingIfExceeds(size_t maximum_pending_frames);
uint64_t droppedCount() const noexcept;
void reset();
private:
friend class MediaSourceHub;
Subscription(std::shared_ptr<SourceState> source, FrameRing::Cursor cursor);
std::shared_ptr<SourceState> source_;
FrameRing::Cursor cursor_;
bool active_{false};
};
MediaSourceHub();
~MediaSourceHub();
MediaSourceHub(const MediaSourceHub&) = delete;
MediaSourceHub& operator=(const MediaSourceHub&) = delete;
bool registerSource(
TrackDescriptorPtr initial_descriptor,
SourceCallbacks callbacks,
size_t ring_capacity = 64);
// Active sources cannot be unregistered. Destroy/reset their subscriptions first.
bool unregisterSource(const std::string& track_id);
bool hasSource(const std::string& track_id) const;
std::vector<TrackDescriptorPtr> listTracks() const;
size_t subscriberCount(const std::string& track_id) const;
// Protocol adapters can request an IDR after a discontinuity without knowing the
// concrete camera implementation. Returns false when unsupported or not running.
bool requestKeyFrame(const std::string& track_id) const;
Subscription subscribe(
const std::string& track_id,
StartPosition start_position = StartPosition::NEXT_PUBLISHED,
CancelPredicate cancelled = {});
// Stops all registered sources and invalidates outstanding subscriptions. The
// subscriptions remain destructible and their waitRead calls are awakened.
// A cooperative in-progress start is cancelled; a callback that violates the
// cancellation contract is quarantined with retained state rather than blocking
// shutdown or risking a use-after-free.
void shutdown();
private:
struct Impl;
std::shared_ptr<Impl> impl_;
};
// Compatibility alias for older protocol tests and integrations. New code should
// use MediaSourceHub directly.
using MediaSourceManager = MediaSourceHub;
} // namespace cmvr::media
#endif // CMVR_ES_MANAGER_MEDIA_SOURCE_HUB_H

View File

@ -0,0 +1,687 @@
#include "manager/media_source_hub/include/device_media_source_adapter.h"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cctype>
#include <cstdint>
#include <exception>
#include <iterator>
#include <limits>
#include <mutex>
#include <thread>
#include <utility>
#include <vector>
#include "common/base/logging/logger.h"
namespace cmvr::media {
namespace {
std::string normalizedCodec(std::string codec) {
codec.erase(
std::remove_if(codec.begin(), codec.end(), [](const unsigned char c) {
return !std::isalnum(c);
}),
codec.end());
std::transform(codec.begin(), codec.end(), codec.begin(), [](const unsigned char c) {
return static_cast<char>(std::tolower(c));
});
return codec;
}
Codec videoCodec(const std::string& value) {
const std::string codec = normalizedCodec(value);
if (codec == "h264" || codec == "avc" || codec == "avc1" ||
codec == "libx264" || codec == "h264qsv") {
return Codec::H264;
}
if (codec == "h265" || codec == "hevc" || codec == "hvc1" ||
codec == "libx265" || codec == "h265qsv" || codec == "hevcqsv") {
return Codec::H265;
}
return Codec::UNKNOWN;
}
PayloadFormat videoPayloadFormat(
const Codec codec,
const std::vector<uint8_t>& payload) noexcept {
if (codec != Codec::H264 && codec != Codec::H265) {
return PayloadFormat::UNKNOWN;
}
const bool three_byte_start_code = payload.size() >= 3 &&
payload[0] == 0U && payload[1] == 0U && payload[2] == 1U;
const bool four_byte_start_code = payload.size() >= 4 &&
payload[0] == 0U && payload[1] == 0U && payload[2] == 0U && payload[3] == 1U;
return three_byte_start_code || four_byte_start_code
? PayloadFormat::ANNEX_B
: PayloadFormat::UNKNOWN;
}
Codec audioCodec(const std::string& value) {
const std::string codec = normalizedCodec(value);
if (codec == "opus") return Codec::OPUS;
if (codec == "aac") return Codec::AAC;
if (codec == "pcms16le") return Codec::PCM_S16LE;
return Codec::UNKNOWN;
}
Codec audioCodec(const device::AudioStreamFrameData& source) {
const Codec codec = audioCodec(source.codec);
if (codec != Codec::UNKNOWN || !normalizedCodec(source.codec).empty()) {
return codec;
}
switch (source.format) {
case device::AudioStreamFormat::PCM:
return Codec::PCM_S16LE;
case device::AudioStreamFormat::AAC:
return Codec::AAC;
case device::AudioStreamFormat::OPUS:
return Codec::OPUS;
default:
return Codec::UNKNOWN;
}
}
PayloadFormat audioPayloadFormat(
const Codec codec,
const std::vector<uint8_t>& payload) noexcept {
switch (codec) {
case Codec::OPUS:
return PayloadFormat::OPUS_PACKET;
case Codec::PCM_S16LE:
return PayloadFormat::RAW;
case Codec::AAC: {
// FFmpeg encoders commonly expose raw AAC access units plus AudioSpecificConfig;
// only advertise ADTS when the sync word and layer bits are actually present.
const bool has_adts_header = payload.size() >= 2 && payload[0] == 0xFFU &&
(payload[1] & 0xF6U) == 0xF0U;
return has_adts_header ? PayloadFormat::AAC_ADTS : PayloadFormat::RAW;
}
default:
return PayloadFormat::UNKNOWN;
}
}
Rational sanitizedTimeBase(
const int32_t numerator,
const int32_t denominator,
const int32_t fallback_denominator) noexcept {
return Rational{
numerator > 0 ? numerator : 1,
denominator > 0 ? denominator : std::max(1, fallback_denominator)};
}
uint64_t descriptorGeneration(
const uint64_t stream_epoch,
const uint32_t codec_generation) {
const uint64_t generation = (stream_epoch << 32U) | codec_generation;
return generation == 0 ? 1 : generation;
}
template<typename DeviceT>
struct PumpState : public std::enable_shared_from_this<PumpState<DeviceT>> {
explicit PumpState(std::shared_ptr<DeviceT> device_ptr)
: device(std::move(device_ptr)) {}
virtual ~PumpState() {
stop();
}
bool begin(
const MediaSourceHub::FrameSink& frame_sink,
const MediaSourceHub::CancelPredicate& cancelled) {
if (!frame_sink || !device) {
return false;
}
const auto cancellation_requested = [&cancelled] {
if (!cancelled) return false;
try {
return cancelled();
} catch (...) {
return true;
}
};
if (cancellation_requested()) {
return false;
}
std::unique_lock<std::mutex> lock(mutex);
if (running.load(std::memory_order_acquire)) {
return true;
}
if (worker.joinable()) {
// A previous worker must always be collected before a new capture lease starts.
std::thread stale_worker = std::move(worker);
lock.unlock();
collectThread(std::move(stale_worker));
lock.lock();
}
sink = frame_sink;
bool streaming_attempted = false;
try {
if (!device->start()) {
sink = {};
return false;
}
if (cancellation_requested()) {
sink = {};
return false;
}
streaming_attempted = true;
if (!device->startStreaming()) {
sink = {};
lock.unlock();
stopDeviceStreaming();
return false;
}
streaming_started = true;
if (cancellation_requested()) {
streaming_started = false;
sink = {};
lock.unlock();
stopDeviceStreaming();
return false;
}
running.store(true, std::memory_order_release);
try {
// The worker owns the pump while run() is active. This also
// makes the defensive self-stop/detach path lifetime-safe.
const auto self = this->shared_from_this();
worker = std::thread([self] { self->run(); });
} catch (...) {
running.store(false, std::memory_order_release);
streaming_started = false;
sink = {};
lock.unlock();
stopDeviceStreaming();
return false;
}
} catch (const std::exception& error) {
CMVR_LOG(ERROR) << "[DeviceMediaSourceAdapter] Failed to start media source: "
<< error.what();
sink = {};
lock.unlock();
if (streaming_attempted) stopDeviceStreaming();
return false;
} catch (...) {
CMVR_LOG(ERROR) << "[DeviceMediaSourceAdapter] Failed to start media source";
sink = {};
lock.unlock();
if (streaming_attempted) stopDeviceStreaming();
return false;
}
return true;
}
void stop() noexcept {
std::thread thread;
bool stop_streaming = false;
{
std::lock_guard<std::mutex> lock(mutex);
running.store(false, std::memory_order_release);
stop_streaming = streaming_started;
streaming_started = false;
sink = {};
thread = std::move(worker);
}
if (stop_streaming) {
stopDeviceStreaming();
}
if (thread.joinable()) {
collectThread(std::move(thread));
}
}
static void collectThread(std::thread thread) noexcept {
if (!thread.joinable()) {
return;
}
try {
if (thread.get_id() == std::this_thread::get_id()) {
thread.detach();
} else {
thread.join();
}
} catch (const std::exception& error) {
CMVR_LOG(ERROR) << "[DeviceMediaSourceAdapter] Failed to collect media pump: "
<< error.what();
if (thread.joinable()) {
try {
thread.detach();
} catch (...) {
// std::thread's destructor would terminate if this extremely rare
// platform error occurred; there is no recoverable ownership path.
}
}
}
}
virtual void run() = 0;
void stopDeviceStreaming() noexcept {
try {
if (device) {
device->stopStreaming();
}
} catch (const std::exception& error) {
CMVR_LOG(ERROR) << "[DeviceMediaSourceAdapter] Failed to stop media source: "
<< error.what();
} catch (...) {
CMVR_LOG(ERROR) << "[DeviceMediaSourceAdapter] Failed to stop media source";
}
}
std::shared_ptr<DeviceT> device;
std::atomic<bool> running{false};
std::mutex mutex;
std::thread worker;
MediaSourceHub::FrameSink sink;
bool streaming_started{false};
};
struct CameraPump final : PumpState<device::AbstractCamera> {
CameraPump(std::shared_ptr<device::AbstractCamera> camera, std::string id)
: PumpState(std::move(camera)), track_id(std::move(id)) {}
~CameraPump() override { stop(); }
void run() override {
size_t cursor = 0;
uint64_t last_epoch = 0;
uint64_t last_sequence = 0;
uint64_t cached_config_generation = 0;
std::vector<uint8_t> cached_codec_config;
TrackDescriptorPtr last_descriptor;
bool have_previous = false;
bool have_cached_config_generation = false;
bool waiting_for_key_frame = true;
bool pending_discontinuity = true;
bool requested_key_frame = false;
std::chrono::steady_clock::time_point last_key_frame_request;
const auto request_key_frame = [&] {
const auto now = std::chrono::steady_clock::now();
if (requested_key_frame &&
now - last_key_frame_request < std::chrono::milliseconds(250)) {
return;
}
requested_key_frame = true;
last_key_frame_request = now;
try {
device->requestKeyFrame();
} catch (...) {
// Unsupported/failed key-frame requests fall back to the encoder's GOP.
}
};
request_key_frame();
while (running.load(std::memory_order_acquire)) {
device::StreamFrameData source;
try {
if (!device->waitEncodedFrame(source, cursor, std::chrono::milliseconds(50))) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
} catch (const std::exception& error) {
CMVR_LOG(ERROR) << "[DeviceMediaSourceAdapter] Camera frame read failed for "
<< track_id << ": " << error.what();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
} catch (...) {
CMVR_LOG(ERROR) << "[DeviceMediaSourceAdapter] Camera frame read failed for "
<< track_id;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
}
if (source.rgbFrame.empty()) {
continue;
}
const uint64_t descriptor_generation = descriptorGeneration(
source.stream_epoch, source.codec_config_generation);
if (!have_cached_config_generation ||
cached_config_generation != descriptor_generation) {
cached_codec_config.clear();
cached_config_generation = descriptor_generation;
have_cached_config_generation = true;
}
if (!source.codec_config.empty()) {
cached_codec_config = source.codec_config;
}
TrackDescriptor::Config track;
track.id = track_id;
track.source_id = device->id();
track.kind = MediaKind::VIDEO;
track.codec = videoCodec(source.codec);
track.payload_format = videoPayloadFormat(track.codec, source.rgbFrame);
track.time_base = sanitizedTimeBase(
source.time_base_num,
source.time_base_den,
source.fps);
track.width = static_cast<uint32_t>(std::max(0, source.width));
track.height = static_cast<uint32_t>(std::max(0, source.height));
track.nominal_rate = static_cast<uint32_t>(std::max(0, source.fps));
track.fx = source.intrinsics.fx;
track.fy = source.intrinsics.fy;
track.cx = source.intrinsics.cx;
track.cy = source.intrinsics.cy;
track.distortion.assign(
std::begin(source.intrinsics.coeffs),
std::end(source.intrinsics.coeffs));
track.generation = descriptor_generation;
track.codec_config = cached_codec_config;
const bool epoch_changed = have_previous && source.stream_epoch != last_epoch;
const bool sequence_wrapped = have_previous &&
last_sequence == std::numeric_limits<uint64_t>::max() && source.sequence != 0;
const bool sequence_gap = have_previous && !epoch_changed &&
(sequence_wrapped ||
(last_sequence != std::numeric_limits<uint64_t>::max() &&
source.sequence != last_sequence + 1));
TrackDescriptorPtr descriptor;
try {
descriptor = makeTrackDescriptor(std::move(track));
} catch (const std::exception& error) {
CMVR_LOG(ERROR) << "[DeviceMediaSourceAdapter] Invalid camera descriptor for "
<< track_id << ": " << error.what();
pending_discontinuity = true;
last_epoch = source.stream_epoch;
last_sequence = source.sequence;
have_previous = true;
continue;
}
const bool descriptor_changed = last_descriptor &&
!equivalentTrackDescriptor(*last_descriptor, *descriptor);
const bool discontinuity = !have_previous || source.discontinuity || epoch_changed ||
sequence_gap || descriptor_changed;
const bool inter_frame_codec = descriptor->codec == Codec::H264 ||
descriptor->codec == Codec::H265;
if (discontinuity) {
pending_discontinuity = true;
if (inter_frame_codec) {
waiting_for_key_frame = true;
request_key_frame();
} else {
waiting_for_key_frame = false;
}
}
last_epoch = source.stream_epoch;
last_sequence = source.sequence;
last_descriptor = descriptor;
have_previous = true;
if (waiting_for_key_frame && !source.bKey) {
request_key_frame();
continue;
}
waiting_for_key_frame = false;
MediaFrame::Config frame;
frame.descriptor = std::move(descriptor);
frame.payload = std::move(source.rgbFrame);
frame.sequence = source.sequence;
frame.source_timestamp = source.source_timestamp;
frame.source_frame_number = source.source_frame_number;
frame.pts = source.pts;
frame.dts = source.dts;
frame.duration = source.duration;
frame.capture_time_ns = source.capture_monotonic_ns > 0
? static_cast<uint64_t>(source.capture_monotonic_ns)
: 0;
frame.capture_utc_ns = source.capture_utc_ns;
frame.key_frame = source.bKey;
frame.discontinuity = pending_discontinuity;
MediaSourceHub::FrameSink current_sink;
{
std::lock_guard<std::mutex> lock(mutex);
current_sink = sink;
}
if (current_sink) {
try {
current_sink(makeMediaFrame(std::move(frame)));
pending_discontinuity = false;
} catch (const std::exception& error) {
CMVR_LOG(ERROR) << "[DeviceMediaSourceAdapter] Invalid camera frame for "
<< track_id << ": " << error.what();
pending_discontinuity = true;
}
}
}
}
std::string track_id;
};
struct MicrophonePump final : PumpState<device::AbstractMicrophone> {
MicrophonePump(std::shared_ptr<device::AbstractMicrophone> microphone, std::string id)
: PumpState(std::move(microphone)), track_id(std::move(id)) {}
~MicrophonePump() override { stop(); }
void run() override {
size_t cursor = 0;
uint64_t last_epoch = 0;
uint64_t last_sequence = 0;
uint64_t cached_config_generation = 0;
std::vector<uint8_t> cached_codec_config;
TrackDescriptorPtr last_descriptor;
bool have_previous = false;
bool have_cached_config_generation = false;
bool pending_discontinuity = true;
while (running.load(std::memory_order_acquire)) {
device::AudioStreamFrameData source;
try {
if (!device->waitEncodedFrame(source, cursor, std::chrono::milliseconds(50))) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue;
}
} catch (const std::exception& error) {
CMVR_LOG(ERROR) << "[DeviceMediaSourceAdapter] Microphone frame read failed for "
<< track_id << ": " << error.what();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
} catch (...) {
CMVR_LOG(ERROR) << "[DeviceMediaSourceAdapter] Microphone frame read failed for "
<< track_id;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
}
if (source.data.empty()) {
continue;
}
const uint64_t descriptor_generation = descriptorGeneration(
source.stream_epoch, source.codec_config_generation);
if (!have_cached_config_generation ||
cached_config_generation != descriptor_generation) {
cached_codec_config.clear();
cached_config_generation = descriptor_generation;
have_cached_config_generation = true;
}
if (!source.codec_config.empty()) {
cached_codec_config = source.codec_config;
}
TrackDescriptor::Config track;
track.id = track_id;
track.source_id = device->id();
track.kind = MediaKind::AUDIO;
track.codec = audioCodec(source);
track.payload_format = audioPayloadFormat(track.codec, source.data);
track.time_base = sanitizedTimeBase(
source.time_base_num,
source.time_base_den,
source.sample_rate);
track.sample_rate = static_cast<uint32_t>(std::max(0, source.sample_rate));
track.channels = static_cast<uint32_t>(std::max(0, source.channels));
// Packet sample counts belong to MediaFrame::duration, not immutable track metadata.
track.nominal_rate = 0;
track.generation = descriptor_generation;
track.codec_config = cached_codec_config;
const bool epoch_changed = have_previous && source.stream_epoch != last_epoch;
const bool sequence_wrapped = have_previous &&
last_sequence == std::numeric_limits<uint64_t>::max() && source.sequence != 0;
const bool sequence_gap = have_previous && !epoch_changed &&
(sequence_wrapped ||
(last_sequence != std::numeric_limits<uint64_t>::max() &&
source.sequence != last_sequence + 1));
TrackDescriptorPtr descriptor;
try {
descriptor = makeTrackDescriptor(std::move(track));
} catch (const std::exception& error) {
CMVR_LOG(ERROR) << "[DeviceMediaSourceAdapter] Invalid microphone descriptor for "
<< track_id << ": " << error.what();
pending_discontinuity = true;
last_epoch = source.stream_epoch;
last_sequence = source.sequence;
have_previous = true;
continue;
}
const bool descriptor_changed = last_descriptor &&
!equivalentTrackDescriptor(*last_descriptor, *descriptor);
if (!have_previous || source.discontinuity || epoch_changed || sequence_gap ||
descriptor_changed) {
pending_discontinuity = true;
}
last_epoch = source.stream_epoch;
last_sequence = source.sequence;
last_descriptor = descriptor;
have_previous = true;
MediaFrame::Config frame;
frame.descriptor = std::move(descriptor);
frame.payload = std::move(source.data);
frame.sequence = source.sequence;
frame.pts = source.pts;
frame.dts = source.dts;
frame.duration = source.duration > 0 ? source.duration : source.nb_samples;
frame.capture_time_ns = source.capture_monotonic_ns > 0
? static_cast<uint64_t>(source.capture_monotonic_ns)
: 0;
frame.capture_utc_ns = source.capture_utc_ns;
frame.key_frame = true;
frame.discontinuity = pending_discontinuity;
MediaSourceHub::FrameSink current_sink;
{
std::lock_guard<std::mutex> lock(mutex);
current_sink = sink;
}
if (current_sink) {
try {
current_sink(makeMediaFrame(std::move(frame)));
pending_discontinuity = false;
} catch (const std::exception& error) {
CMVR_LOG(ERROR) << "[DeviceMediaSourceAdapter] Invalid microphone frame for "
<< track_id << ": " << error.what();
pending_discontinuity = true;
}
}
}
}
std::string track_id;
};
TrackDescriptorPtr initialTrack(
std::string track_id,
std::string source_id,
const MediaKind kind) {
TrackDescriptor::Config config;
config.id = std::move(track_id);
config.source_id = std::move(source_id);
config.kind = kind;
// A placeholder descriptor is never emitted as a media sample, but it
// still carries a mathematically valid neutral time base.
config.time_base = Rational{1, 1};
config.generation = 1;
return makeTrackDescriptor(std::move(config));
}
} // namespace
MediaSourceHub& globalMediaSourceHub() {
static MediaSourceHub hub;
return hub;
}
std::string cameraColorTrackId(const std::string& device_id) {
return device_id + "/video/color";
}
std::string microphoneTrackId(const std::string& device_id) {
return device_id + "/audio/main";
}
bool ensureCameraMediaSource(
MediaSourceHub& hub,
const std::shared_ptr<device::AbstractCamera>& camera,
const size_t ring_capacity) {
if (!camera || camera->id().empty()) {
return false;
}
const std::string track_id = cameraColorTrackId(camera->id());
if (hub.hasSource(track_id)) {
return true;
}
const auto pump = std::make_shared<CameraPump>(camera, track_id);
MediaSourceHub::SourceCallbacks callbacks;
callbacks.start = [pump](
const MediaSourceHub::FrameSink& sink,
const MediaSourceHub::CancelPredicate& cancelled) {
return pump->begin(sink, cancelled);
};
callbacks.stop = [pump] { pump->stop(); };
callbacks.request_key_frame = [camera] { return camera->requestKeyFrame(); };
const bool registered = hub.registerSource(
initialTrack(track_id, camera->id(), MediaKind::VIDEO),
std::move(callbacks),
ring_capacity);
if (!registered && !hub.hasSource(track_id)) {
CMVR_LOG(ERROR) << "[DeviceMediaSourceAdapter] Failed to register camera track: " << track_id;
return false;
}
return true;
}
bool ensureMicrophoneMediaSource(
MediaSourceHub& hub,
const std::shared_ptr<device::AbstractMicrophone>& microphone,
const size_t ring_capacity) {
if (!microphone || microphone->id().empty()) {
return false;
}
const std::string track_id = microphoneTrackId(microphone->id());
if (hub.hasSource(track_id)) {
return true;
}
const auto pump = std::make_shared<MicrophonePump>(microphone, track_id);
MediaSourceHub::SourceCallbacks callbacks;
callbacks.start = [pump](
const MediaSourceHub::FrameSink& sink,
const MediaSourceHub::CancelPredicate& cancelled) {
return pump->begin(sink, cancelled);
};
callbacks.stop = [pump] { pump->stop(); };
const bool registered = hub.registerSource(
initialTrack(track_id, microphone->id(), MediaKind::AUDIO),
std::move(callbacks),
ring_capacity);
if (!registered && !hub.hasSource(track_id)) {
CMVR_LOG(ERROR) << "[DeviceMediaSourceAdapter] Failed to register microphone track: " << track_id;
return false;
}
return true;
}
} // namespace cmvr::media

View File

@ -0,0 +1,596 @@
#include "manager/media_source_hub/include/media_source_hub.h"
#include <algorithm>
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <unordered_map>
#include <utility>
namespace cmvr::media {
struct MediaSourceHub::SourceState final : public std::enable_shared_from_this<SourceState> {
enum class Lifecycle {
STOPPED,
STARTING,
RUNNING,
STOPPING
};
struct StartAttempt {
size_t waiters{0};
bool completed{false};
bool succeeded{false};
std::atomic<bool> cancel_requested{false};
};
SourceState(
TrackDescriptorPtr initial_descriptor,
SourceCallbacks source_callbacks,
const size_t ring_capacity)
: track_id(initial_descriptor->id),
descriptor(std::move(initial_descriptor)),
callbacks(std::move(source_callbacks)),
ring(ring_capacity) {}
FrameSink makeSink() {
const std::weak_ptr<SourceState> weak_source = shared_from_this();
return [weak_source](MediaFramePtr frame) {
if (const auto source = weak_source.lock()) {
source->acceptFrame(std::move(frame));
}
};
}
void acceptFrame(MediaFramePtr frame) {
if (!frame || !frame->descriptor || frame->descriptor->id != track_id) {
return;
}
{
std::lock_guard<std::mutex> lock(lifecycle_mutex);
if (!registered ||
(lifecycle != Lifecycle::STARTING && lifecycle != Lifecycle::RUNNING)) {
return;
}
}
const TrackDescriptorPtr current = std::atomic_load(&descriptor);
if (!current || !equivalentTrackDescriptor(*current, *frame->descriptor)) {
// C++17 atomic shared_ptr free functions provide an atomic descriptor snapshot to
// all subscriptions while frames remain immutable.
std::atomic_store(&descriptor, frame->descriptor);
}
ring.publish(std::move(frame));
}
static bool isCancelled(const CancelPredicate& cancelled) noexcept {
if (!cancelled) return false;
try {
return cancelled();
} catch (...) {
return true;
}
}
bool invokeStart(
const FrameSink& sink,
const CancelPredicate& cancelled) noexcept {
std::lock_guard<std::mutex> callback_lock(callback_mutex);
try {
return callbacks.start && callbacks.start(sink, cancelled);
} catch (...) {
return false;
}
}
void invokeStop() noexcept {
std::lock_guard<std::mutex> callback_lock(callback_mutex);
try {
if (callbacks.stop) callbacks.stop();
} catch (...) {
}
}
void completeStart(
const std::shared_ptr<StartAttempt>& attempt,
const bool started) {
bool stop_abandoned_start = false;
{
std::lock_guard<std::mutex> lock(lifecycle_mutex);
if (start_attempt != attempt || lifecycle != Lifecycle::STARTING) {
return;
}
attempt->completed = true;
attempt->succeeded = started;
if (started && registered && attempt->waiters != 0U) {
lifecycle = Lifecycle::RUNNING;
} else if (started) {
lifecycle = Lifecycle::STOPPING;
ring.close();
stop_abandoned_start = true;
} else {
lifecycle = Lifecycle::STOPPED;
}
lifecycle_condition.notify_all();
}
if (stop_abandoned_start) {
invokeStop();
std::lock_guard<std::mutex> lock(lifecycle_mutex);
if (lifecycle == Lifecycle::STOPPING) {
lifecycle = Lifecycle::STOPPED;
}
lifecycle_condition.notify_all();
}
}
bool acquire(
const StartPosition start_position,
FrameRing::Cursor& cursor,
const CancelPredicate& cancelled) {
std::unique_lock<std::mutex> lock(lifecycle_mutex);
while (lifecycle == Lifecycle::STOPPING) {
if (!registered || isCancelled(cancelled)) return false;
lifecycle_condition.wait_for(lock, std::chrono::milliseconds(10));
}
if (!registered || isCancelled(cancelled)) return false;
if (lifecycle == Lifecycle::RUNNING) {
++subscriber_count;
lock.unlock();
cursor = ring.makeCursor(start_position);
return true;
}
std::shared_ptr<StartAttempt> attempt;
if (lifecycle == Lifecycle::STOPPED) {
lifecycle = Lifecycle::STARTING;
ring.reset();
const FrameSink sink = makeSink();
attempt = std::make_shared<StartAttempt>();
attempt->waiters = 1U;
start_attempt = attempt;
const auto self = shared_from_this();
try {
std::thread([self, attempt, sink]() {
const CancelPredicate cancelled = [attempt] {
return attempt->cancel_requested.load(
std::memory_order_acquire);
};
const bool started = self->invokeStart(sink, cancelled);
self->completeStart(attempt, started);
}).detach();
} catch (...) {
start_attempt.reset();
lifecycle = Lifecycle::STOPPED;
lifecycle_condition.notify_all();
return false;
}
} else if (lifecycle == Lifecycle::STARTING) {
attempt = start_attempt;
if (!attempt || attempt->cancel_requested.load(std::memory_order_acquire)) {
return false;
}
++attempt->waiters;
} else {
return false;
}
while (registered && !attempt->completed) {
if (isCancelled(cancelled)) {
if (attempt->waiters != 0U) --attempt->waiters;
if (attempt->waiters == 0U) {
attempt->cancel_requested.store(true, std::memory_order_release);
}
lifecycle_condition.notify_all();
return false;
}
lifecycle_condition.wait_for(lock, std::chrono::milliseconds(10));
}
const bool caller_cancelled = isCancelled(cancelled);
const bool acquired = !caller_cancelled && registered && attempt->completed &&
attempt->succeeded &&
lifecycle == Lifecycle::RUNNING;
if (attempt->waiters != 0U) --attempt->waiters;
if (!acquired) {
if (attempt->waiters == 0U) {
attempt->cancel_requested.store(true, std::memory_order_release);
}
const bool stop_unclaimed_source =
start_attempt == attempt && attempt->waiters == 0U &&
subscriber_count == 0U &&
lifecycle == Lifecycle::RUNNING;
if (stop_unclaimed_source) {
lifecycle = Lifecycle::STOPPING;
ring.close();
lock.unlock();
invokeStop();
lock.lock();
if (lifecycle == Lifecycle::STOPPING) {
lifecycle = Lifecycle::STOPPED;
}
lifecycle_condition.notify_all();
}
return false;
}
++subscriber_count;
lock.unlock();
cursor = ring.makeCursor(start_position);
return true;
}
void release() {
std::unique_lock<std::mutex> lock(lifecycle_mutex);
if (subscriber_count == 0) {
return;
}
--subscriber_count;
if (subscriber_count != 0 || lifecycle != Lifecycle::RUNNING) {
return;
}
lifecycle = Lifecycle::STOPPING;
ring.close();
lock.unlock();
invokeStop();
lock.lock();
lifecycle = Lifecycle::STOPPED;
lifecycle_condition.notify_all();
}
bool deactivateIfUnused() {
std::unique_lock<std::mutex> lock(lifecycle_mutex);
if (subscriber_count != 0) {
return false;
}
registered = false;
ring.close();
if (lifecycle == Lifecycle::STARTING) {
if (start_attempt) {
start_attempt->cancel_requested.store(true, std::memory_order_release);
}
lifecycle_condition.notify_all();
return true;
}
if (lifecycle == Lifecycle::STOPPING || lifecycle == Lifecycle::STOPPED) {
lifecycle_condition.notify_all();
return true;
}
lifecycle = Lifecycle::STOPPING;
lock.unlock();
invokeStop();
lock.lock();
if (lifecycle == Lifecycle::STOPPING) {
lifecycle = Lifecycle::STOPPED;
}
lifecycle_condition.notify_all();
return true;
}
void shutdown() {
std::unique_lock<std::mutex> lock(lifecycle_mutex);
registered = false;
ring.close();
if (lifecycle == Lifecycle::STARTING) {
if (start_attempt) {
start_attempt->cancel_requested.store(true, std::memory_order_release);
}
lifecycle_condition.notify_all();
return;
}
if (lifecycle == Lifecycle::STOPPING) {
lifecycle_condition.notify_all();
return;
}
if (lifecycle == Lifecycle::STOPPED) {
lifecycle_condition.notify_all();
return;
}
lifecycle = Lifecycle::STOPPING;
lock.unlock();
invokeStop();
lock.lock();
if (lifecycle == Lifecycle::STOPPING) {
lifecycle = Lifecycle::STOPPED;
}
lifecycle_condition.notify_all();
}
bool validForSubscription() const {
std::lock_guard<std::mutex> lock(lifecycle_mutex);
return registered && lifecycle == Lifecycle::RUNNING;
}
size_t subscriberCount() const {
std::lock_guard<std::mutex> lock(lifecycle_mutex);
return subscriber_count;
}
bool requestKeyFrame() const {
// Serialize with stop first, then re-check lifecycle. A stop that has
// already begun rejects the request; a stop that begins afterwards
// waits for this callback before invoking the device stop barrier.
std::lock_guard<std::mutex> callback_lock(callback_mutex);
{
std::lock_guard<std::mutex> lock(lifecycle_mutex);
if (!registered || lifecycle != Lifecycle::RUNNING || !callbacks.request_key_frame) {
return false;
}
}
try {
return callbacks.request_key_frame();
} catch (...) {
return false;
}
}
TrackDescriptorPtr currentDescriptor() const {
return std::atomic_load(&descriptor);
}
const std::string track_id;
mutable TrackDescriptorPtr descriptor;
const SourceCallbacks callbacks;
FrameRing ring;
mutable std::mutex callback_mutex;
mutable std::mutex lifecycle_mutex;
std::condition_variable lifecycle_condition;
Lifecycle lifecycle{Lifecycle::STOPPED};
size_t subscriber_count{0};
bool registered{true};
std::shared_ptr<StartAttempt> start_attempt;
};
struct MediaSourceHub::Impl final {
mutable std::mutex mutex;
std::unordered_map<std::string, std::shared_ptr<SourceState>> sources;
};
MediaSourceHub::Subscription::Subscription(
std::shared_ptr<SourceState> source,
FrameRing::Cursor cursor)
: source_(std::move(source)),
cursor_(std::move(cursor)),
active_(static_cast<bool>(source_)) {}
MediaSourceHub::Subscription::~Subscription() {
reset();
}
MediaSourceHub::Subscription::Subscription(Subscription&& other) noexcept
: source_(std::move(other.source_)),
cursor_(other.cursor_),
active_(other.active_) {
other.active_ = false;
}
MediaSourceHub::Subscription& MediaSourceHub::Subscription::operator=(Subscription&& other) noexcept {
if (this == &other) {
return *this;
}
reset();
source_ = std::move(other.source_);
cursor_ = other.cursor_;
active_ = other.active_;
other.active_ = false;
return *this;
}
bool MediaSourceHub::Subscription::valid() const {
return active_ && source_ && source_->validForSubscription();
}
TrackDescriptorPtr MediaSourceHub::Subscription::descriptor() const {
return source_ ? source_->currentDescriptor() : nullptr;
}
std::optional<MediaSourceHub::FrameReadResult> MediaSourceHub::Subscription::tryRead() {
if (!active_ || !source_) {
return std::nullopt;
}
return source_->ring.tryRead(cursor_);
}
std::optional<MediaSourceHub::FrameReadResult> MediaSourceHub::Subscription::waitRead(
const std::chrono::milliseconds timeout) {
if (!active_ || !source_) {
return std::nullopt;
}
return source_->ring.waitRead(cursor_, timeout);
}
uint64_t MediaSourceHub::Subscription::discardPendingIfExceeds(
const size_t maximum_pending_frames) {
if (!active_ || !source_) {
return 0;
}
return source_->ring.discardPendingIfExceeds(cursor_, maximum_pending_frames);
}
uint64_t MediaSourceHub::Subscription::droppedCount() const noexcept {
return cursor_.dropped_count;
}
void MediaSourceHub::Subscription::reset() {
if (active_ && source_) {
source_->release();
}
active_ = false;
source_.reset();
}
MediaSourceHub::MediaSourceHub()
: impl_(std::make_shared<Impl>()) {}
MediaSourceHub::~MediaSourceHub() {
shutdown();
}
bool MediaSourceHub::registerSource(
TrackDescriptorPtr initial_descriptor,
SourceCallbacks callbacks,
const size_t ring_capacity) {
if (!impl_ || !initial_descriptor || initial_descriptor->id.empty() ||
!callbacks.start || ring_capacity == 0) {
return false;
}
std::shared_ptr<SourceState> source;
try {
source = std::make_shared<SourceState>(
std::move(initial_descriptor), std::move(callbacks), ring_capacity);
} catch (...) {
return false;
}
std::lock_guard<std::mutex> lock(impl_->mutex);
return impl_->sources.emplace(source->track_id, std::move(source)).second;
}
bool MediaSourceHub::unregisterSource(const std::string& track_id) {
if (!impl_ || track_id.empty()) {
return false;
}
std::shared_ptr<SourceState> source;
{
std::lock_guard<std::mutex> lock(impl_->mutex);
const auto it = impl_->sources.find(track_id);
if (it == impl_->sources.end()) {
return false;
}
source = it->second;
}
if (!source->deactivateIfUnused()) {
return false;
}
std::lock_guard<std::mutex> lock(impl_->mutex);
const auto it = impl_->sources.find(track_id);
if (it != impl_->sources.end() && it->second == source) {
impl_->sources.erase(it);
return true;
}
return false;
}
bool MediaSourceHub::hasSource(const std::string& track_id) const {
if (!impl_) {
return false;
}
std::lock_guard<std::mutex> lock(impl_->mutex);
return impl_->sources.find(track_id) != impl_->sources.end();
}
std::vector<TrackDescriptorPtr> MediaSourceHub::listTracks() const {
std::vector<std::shared_ptr<SourceState>> sources;
if (!impl_) {
return {};
}
{
std::lock_guard<std::mutex> lock(impl_->mutex);
sources.reserve(impl_->sources.size());
for (const auto& [track_id, source] : impl_->sources) {
(void)track_id;
sources.push_back(source);
}
}
std::vector<TrackDescriptorPtr> descriptors;
descriptors.reserve(sources.size());
for (const auto& source : sources) {
descriptors.push_back(source->currentDescriptor());
}
std::sort(descriptors.begin(), descriptors.end(), [](const auto& lhs, const auto& rhs) {
if (!lhs) return static_cast<bool>(rhs);
if (!rhs) return false;
return lhs->id < rhs->id;
});
return descriptors;
}
size_t MediaSourceHub::subscriberCount(const std::string& track_id) const {
if (!impl_) {
return 0;
}
std::shared_ptr<SourceState> source;
{
std::lock_guard<std::mutex> lock(impl_->mutex);
const auto it = impl_->sources.find(track_id);
if (it == impl_->sources.end()) {
return 0;
}
source = it->second;
}
return source->subscriberCount();
}
bool MediaSourceHub::requestKeyFrame(const std::string& track_id) const {
if (!impl_) {
return false;
}
std::shared_ptr<SourceState> source;
{
std::lock_guard<std::mutex> lock(impl_->mutex);
const auto it = impl_->sources.find(track_id);
if (it == impl_->sources.end()) {
return false;
}
source = it->second;
}
return source->requestKeyFrame();
}
MediaSourceHub::Subscription MediaSourceHub::subscribe(
const std::string& track_id,
const StartPosition start_position,
CancelPredicate cancelled) {
if (!impl_) {
return {};
}
std::shared_ptr<SourceState> source;
{
std::lock_guard<std::mutex> lock(impl_->mutex);
const auto it = impl_->sources.find(track_id);
if (it == impl_->sources.end()) {
return {};
}
source = it->second;
}
FrameRing::Cursor cursor;
if (!source->acquire(start_position, cursor, cancelled)) {
return {};
}
return Subscription(std::move(source), std::move(cursor));
}
void MediaSourceHub::shutdown() {
if (!impl_) {
return;
}
std::vector<std::shared_ptr<SourceState>> sources;
{
std::lock_guard<std::mutex> lock(impl_->mutex);
sources.reserve(impl_->sources.size());
for (auto& [track_id, source] : impl_->sources) {
(void)track_id;
sources.push_back(std::move(source));
}
impl_->sources.clear();
}
for (const auto& source : sources) {
source->shutdown();
}
}
} // namespace cmvr::media

View File

@ -0,0 +1,683 @@
#include "manager/media_source_hub/include/media_source_hub.h"
#include <atomic>
#include <chrono>
#include <future>
#include <iostream>
#include <mutex>
#include <stdexcept>
#include <string>
#include <thread>
#include <type_traits>
#include <utility>
#include <vector>
namespace {
using namespace std::chrono_literals;
using cmvr::media::Codec;
using cmvr::media::MediaFrame;
using cmvr::media::MediaFramePtr;
using cmvr::media::MediaKind;
using cmvr::media::MediaSourceHub;
using cmvr::media::PayloadFormat;
using cmvr::media::Rational;
using cmvr::media::TrackDescriptor;
using cmvr::media::TrackDescriptorPtr;
static_assert(!std::is_copy_assignable<MediaFrame>::value, "MediaFrame must be immutable");
static_assert(!std::is_copy_assignable<TrackDescriptor>::value, "TrackDescriptor must be immutable");
static_assert(std::is_same<MediaFramePtr::element_type, const MediaFrame>::value,
"MediaFramePtr must share const frames");
int failures = 0;
#define CHECK_TRUE(expression) \
do { \
if (!(expression)) { \
std::cerr << __FILE__ << ':' << __LINE__ << " check failed: " #expression << '\n'; \
++failures; \
} \
} while (false)
TrackDescriptorPtr makeVideoDescriptor(
const Codec codec,
const uint64_t generation,
std::vector<uint8_t> codec_config = {}) {
TrackDescriptor::Config config;
config.id = "camera.front.video";
config.source_id = "camera.front";
config.kind = MediaKind::VIDEO;
config.codec = codec;
config.payload_format = codec == Codec::UNKNOWN ? PayloadFormat::UNKNOWN : PayloadFormat::ANNEX_B;
config.time_base = Rational{1, 90000};
config.width = 640;
config.height = 360;
config.nominal_rate = 30;
config.generation = generation;
config.codec_config = std::move(codec_config);
return cmvr::media::makeTrackDescriptor(std::move(config));
}
MediaFramePtr makeFrame(
TrackDescriptorPtr descriptor,
const uint64_t sequence,
const uint8_t marker) {
MediaFrame::Config config;
config.descriptor = std::move(descriptor);
config.payload = {marker, static_cast<uint8_t>(marker + 1)};
config.sequence = sequence;
config.pts = static_cast<int64_t>(sequence * 3000);
config.dts = config.pts;
config.duration = 3000;
config.capture_time_ns = sequence * 1000000;
config.capture_utc_ns = 1700000000000000000LL + static_cast<int64_t>(sequence);
config.key_frame = sequence == 0;
return cmvr::media::makeMediaFrame(std::move(config));
}
void testMediaMetadataValidation() {
TrackDescriptor::Config invalid;
invalid.id = "invalid.video";
invalid.source_id = "invalid";
invalid.kind = MediaKind::VIDEO;
invalid.time_base = Rational{0, 1};
bool rejected = false;
try {
(void)cmvr::media::makeTrackDescriptor(std::move(invalid));
} catch (const std::invalid_argument&) {
rejected = true;
}
CHECK_TRUE(rejected);
const auto frame = makeFrame(makeVideoDescriptor(Codec::H264, 1), 7, 1);
CHECK_TRUE(frame->capture_time_ns == 7000000);
CHECK_TRUE(frame->capture_utc_ns == 1700000000000000007LL);
CHECK_TRUE(frame->duration == 3000);
}
void testLegacySpmcCompatibility() {
bool ring_zero_capacity_rejected = false;
try {
RingBuffer<int> invalid_ring(0);
} catch (const std::invalid_argument&) {
ring_zero_capacity_rejected = true;
}
CHECK_TRUE(ring_zero_capacity_rejected);
bool spmc_zero_capacity_rejected = false;
try {
SPMCRingBuffer<int> invalid_ring(0);
} catch (const std::invalid_argument&) {
spmc_zero_capacity_rejected = true;
}
CHECK_TRUE(spmc_zero_capacity_rejected);
SPMCRingBuffer<int> ring(2);
ring.push(10);
ring.push(20);
CHECK_TRUE(ring.size() == 2);
CHECK_TRUE(ring.getHead() == 2);
CHECK_TRUE(ring.getTail() == 0);
CHECK_TRUE(ring.getLast().has_value() && *ring.getLast() == 20);
size_t reader = 0;
CHECK_TRUE(ring.pop(reader).has_value());
ring.push(30);
ring.push(40);
CHECK_TRUE(!ring.pop(reader).has_value());
CHECK_TRUE(reader == ring.getTail());
CHECK_TRUE(ring.pop(reader).has_value());
ring.clear();
CHECK_TRUE(ring.empty());
CHECK_TRUE(ring.getHead() == 4);
ring.push(50);
CHECK_TRUE(!ring.pop(reader).has_value());
const auto after_clear = ring.pop(reader);
CHECK_TRUE(after_clear.has_value() && *after_clear == 50);
}
void testBroadcastFrameRing() {
using Ring = BroadcastFrameRing<MediaFrame>;
Ring ring(2);
const auto descriptor = makeVideoDescriptor(Codec::H264, 1, {1, 2, 3});
auto cursor = ring.makeCursor(Ring::StartPosition::OLDEST_AVAILABLE);
CHECK_TRUE(ring.publish(makeFrame(descriptor, 0, 10)).value() == 0);
CHECK_TRUE(ring.publish(makeFrame(descriptor, 1, 20)).value() == 1);
CHECK_TRUE(ring.publish(makeFrame(descriptor, 2, 30)).value() == 2);
const auto first = ring.tryRead(cursor);
CHECK_TRUE(first.has_value());
CHECK_TRUE(first->sequence == 1);
CHECK_TRUE(first->value->sequence == 1);
CHECK_TRUE(first->dropped_count == 1);
CHECK_TRUE(first->dropped_since_last_read == 1);
CHECK_TRUE(ring.stats().dropped_count == 1);
const auto second = ring.tryRead(cursor);
CHECK_TRUE(second.has_value() && second->sequence == 2);
CHECK_TRUE(second->dropped_since_last_read == 0);
auto waiting_cursor = ring.makeCursor(Ring::StartPosition::NEXT_PUBLISHED);
auto waiting_read = std::async(std::launch::async, [&ring, &waiting_cursor] {
return ring.waitRead(waiting_cursor, 1s);
});
std::this_thread::sleep_for(10ms);
ring.publish(makeFrame(descriptor, 3, 40));
CHECK_TRUE(waiting_read.wait_for(500ms) == std::future_status::ready);
CHECK_TRUE(waiting_read.get().has_value());
const uint64_t next_generation = ring.reset();
CHECK_TRUE(next_generation == 2);
ring.publish(makeFrame(descriptor, 4, 50));
const auto after_reset = ring.tryRead(cursor);
CHECK_TRUE(after_reset.has_value());
CHECK_TRUE(after_reset->generation == 2);
CHECK_TRUE(after_reset->sequence == 0);
CHECK_TRUE(after_reset->generation_changed);
auto close_cursor = ring.makeCursor(Ring::StartPosition::NEXT_PUBLISHED);
auto close_wait = std::async(std::launch::async, [&ring, &close_cursor] {
return ring.waitRead(close_cursor, 2s);
});
ring.close();
CHECK_TRUE(close_wait.wait_for(500ms) == std::future_status::ready);
CHECK_TRUE(!close_wait.get().has_value());
CHECK_TRUE(!ring.publish(makeFrame(descriptor, 5, 60)).has_value());
}
void testBroadcastDiscardPending() {
using Ring = BroadcastFrameRing<MediaFrame>;
const auto descriptor = makeVideoDescriptor(Codec::H264, 1, {1, 2, 3});
{
Ring ring(8);
auto cursor = ring.makeCursor(Ring::StartPosition::OLDEST_AVAILABLE);
ring.publish(makeFrame(descriptor, 0, 10));
ring.publish(makeFrame(descriptor, 1, 20));
CHECK_TRUE(ring.discardPendingIfExceeds(cursor, 2) == 0);
const auto first = ring.tryRead(cursor);
CHECK_TRUE(first.has_value());
CHECK_TRUE(first->sequence == 0);
CHECK_TRUE(first->dropped_since_last_read == 0);
}
{
Ring ring(8);
auto cursor = ring.makeCursor(Ring::StartPosition::OLDEST_AVAILABLE);
ring.publish(makeFrame(descriptor, 0, 10));
ring.publish(makeFrame(descriptor, 1, 20));
ring.publish(makeFrame(descriptor, 2, 30));
CHECK_TRUE(ring.discardPendingIfExceeds(cursor, 2) == 3);
CHECK_TRUE(!ring.tryRead(cursor).has_value());
ring.publish(makeFrame(descriptor, 3, 40));
const auto after_discard = ring.tryRead(cursor);
CHECK_TRUE(after_discard.has_value());
CHECK_TRUE(after_discard->sequence == 3);
CHECK_TRUE(after_discard->dropped_count == 3);
CHECK_TRUE(after_discard->dropped_since_last_read == 3);
}
{
// Two frames are overwritten before the explicit three-frame discard.
// Both kinds of loss must be reported by the next successful read.
Ring ring(3);
auto cursor = ring.makeCursor(Ring::StartPosition::OLDEST_AVAILABLE);
for (uint64_t sequence = 0; sequence < 5; ++sequence) {
ring.publish(makeFrame(
descriptor,
sequence,
static_cast<uint8_t>(sequence)));
}
CHECK_TRUE(ring.discardPendingIfExceeds(cursor, 2) == 3);
CHECK_TRUE(cursor.dropped_count == 5);
ring.publish(makeFrame(descriptor, 5, 50));
const auto after_overwrite_and_discard = ring.tryRead(cursor);
CHECK_TRUE(after_overwrite_and_discard.has_value());
CHECK_TRUE(after_overwrite_and_discard->sequence == 5);
CHECK_TRUE(after_overwrite_and_discard->dropped_count == 5);
CHECK_TRUE(after_overwrite_and_discard->dropped_since_last_read == 5);
}
{
// An old-generation OLDEST_AVAILABLE cursor adopts the reset generation
// before deciding whether that generation's pending frames are excessive.
Ring ring(4);
auto cursor = ring.makeCursor(Ring::StartPosition::OLDEST_AVAILABLE);
ring.publish(makeFrame(descriptor, 0, 10));
ring.reset();
ring.publish(makeFrame(descriptor, 1, 20));
ring.publish(makeFrame(descriptor, 2, 30));
CHECK_TRUE(ring.discardPendingIfExceeds(cursor, 1) == 2);
ring.publish(makeFrame(descriptor, 3, 40));
const auto after_reset = ring.tryRead(cursor);
CHECK_TRUE(after_reset.has_value());
CHECK_TRUE(after_reset->generation == 2);
CHECK_TRUE(after_reset->sequence == 2);
CHECK_TRUE(after_reset->generation_changed);
CHECK_TRUE(after_reset->dropped_since_last_read == 2);
}
}
void testBroadcastDiscardConcurrentPublish() {
using Ring = BroadcastFrameRing<int>;
constexpr uint64_t frame_count = 4000;
Ring ring(64);
auto cursor = ring.makeCursor(Ring::StartPosition::NEXT_PUBLISHED);
std::atomic<bool> start{false};
std::atomic<bool> publisher_done{false};
std::thread publisher([&] {
while (!start.load(std::memory_order_acquire)) {
std::this_thread::yield();
}
for (uint64_t sequence = 0; sequence < frame_count; ++sequence) {
ring.publish(std::make_shared<const int>(static_cast<int>(sequence)));
if ((sequence & 7U) == 0U) {
std::this_thread::yield();
}
}
publisher_done.store(true, std::memory_order_release);
});
uint64_t read_count = 0;
uint64_t actively_discarded = 0;
start.store(true, std::memory_order_release);
while (true) {
actively_discarded += ring.discardPendingIfExceeds(cursor, 8);
if (ring.tryRead(cursor)) {
++read_count;
continue;
}
if (publisher_done.load(std::memory_order_acquire)) {
actively_discarded += ring.discardPendingIfExceeds(cursor, 8);
if (ring.tryRead(cursor)) {
++read_count;
continue;
}
break;
}
std::this_thread::yield();
}
publisher.join();
CHECK_TRUE(read_count + cursor.dropped_count == frame_count);
CHECK_TRUE(actively_discarded <= cursor.dropped_count);
}
void testBroadcastConcurrency() {
using Ring = BroadcastFrameRing<MediaFrame>;
constexpr uint64_t frame_count = 500;
Ring ring(frame_count);
const auto descriptor = makeVideoDescriptor(Codec::H264, 1, {1, 2, 3});
auto first_cursor = ring.makeCursor(Ring::StartPosition::OLDEST_AVAILABLE);
auto second_cursor = ring.makeCursor(Ring::StartPosition::OLDEST_AVAILABLE);
auto consume = [&ring](Ring::Cursor& cursor) {
uint64_t expected = 0;
while (expected < frame_count) {
const auto result = ring.waitRead(cursor, 1s);
if (!result || result->sequence != expected || result->value->sequence != expected) {
return false;
}
++expected;
}
return cursor.dropped_count == 0;
};
auto first_consumer = std::async(std::launch::async, consume, std::ref(first_cursor));
auto second_consumer = std::async(std::launch::async, consume, std::ref(second_cursor));
std::thread producer([&ring, &descriptor] {
for (uint64_t sequence = 0; sequence < frame_count; ++sequence) {
ring.publish(makeFrame(descriptor, sequence, static_cast<uint8_t>(sequence)));
}
});
producer.join();
CHECK_TRUE(first_consumer.get());
CHECK_TRUE(second_consumer.get());
}
void testHubLifecycleAndDescriptorRefresh() {
MediaSourceHub hub;
const auto initial_descriptor = makeVideoDescriptor(Codec::UNKNOWN, 1);
std::atomic<int> start_count{0};
std::atomic<int> stop_count{0};
std::atomic<int> key_frame_requests{0};
std::mutex sink_mutex;
MediaSourceHub::FrameSink sink;
MediaSourceHub::SourceCallbacks callbacks;
callbacks.start = [&](const MediaSourceHub::FrameSink& callback_sink,
const MediaSourceHub::CancelPredicate&) {
{
std::lock_guard<std::mutex> lock(sink_mutex);
sink = callback_sink;
}
++start_count;
return true;
};
callbacks.stop = [&] {
++stop_count;
std::lock_guard<std::mutex> lock(sink_mutex);
sink = {};
};
callbacks.request_key_frame = [&] {
++key_frame_requests;
return true;
};
CHECK_TRUE(hub.registerSource(initial_descriptor, callbacks, 4));
CHECK_TRUE(!hub.registerSource(initial_descriptor, callbacks, 4));
CHECK_TRUE(hub.hasSource(initial_descriptor->id));
CHECK_TRUE(hub.listTracks().size() == 1);
auto first = hub.subscribe(initial_descriptor->id);
auto second = hub.subscribe(initial_descriptor->id);
CHECK_TRUE(first.valid() && second.valid());
CHECK_TRUE(hub.requestKeyFrame(initial_descriptor->id));
CHECK_TRUE(key_frame_requests == 1);
CHECK_TRUE(start_count == 1);
CHECK_TRUE(hub.subscriberCount(initial_descriptor->id) == 2);
CHECK_TRUE(first.descriptor()->codec == Codec::UNKNOWN);
CHECK_TRUE(!hub.unregisterSource(initial_descriptor->id));
// Content changes at the same generation must atomically replace the initial descriptor.
const auto actual_descriptor = makeVideoDescriptor(Codec::H264, 1, {0, 0, 0, 1, 0x67});
MediaSourceHub::FrameSink producer;
{
std::lock_guard<std::mutex> lock(sink_mutex);
producer = sink;
}
CHECK_TRUE(static_cast<bool>(producer));
const auto shared_frame = makeFrame(actual_descriptor, 0, 70);
producer(shared_frame);
const auto first_read = first.waitRead(500ms);
const auto second_read = second.waitRead(500ms);
CHECK_TRUE(first_read.has_value() && first_read->value == shared_frame);
CHECK_TRUE(second_read.has_value() && second_read->value == shared_frame);
CHECK_TRUE(first.descriptor() == actual_descriptor);
CHECK_TRUE(second.descriptor()->codec_config == actual_descriptor->codec_config);
first.reset();
CHECK_TRUE(stop_count == 0);
CHECK_TRUE(hub.subscriberCount(initial_descriptor->id) == 1);
second.reset();
CHECK_TRUE(stop_count == 1);
CHECK_TRUE(!hub.requestKeyFrame(initial_descriptor->id));
CHECK_TRUE(hub.subscriberCount(initial_descriptor->id) == 0);
{
auto restarted = hub.subscribe(initial_descriptor->id);
CHECK_TRUE(restarted.valid());
CHECK_TRUE(start_count == 2);
}
CHECK_TRUE(stop_count == 2);
CHECK_TRUE(hub.unregisterSource(initial_descriptor->id));
CHECK_TRUE(!hub.hasSource(initial_descriptor->id));
}
void testSubscriptionDiscardPending() {
MediaSourceHub hub;
const auto descriptor = makeVideoDescriptor(Codec::H264, 1);
std::mutex sink_mutex;
MediaSourceHub::FrameSink sink;
MediaSourceHub::SourceCallbacks callbacks;
callbacks.start = [&](const MediaSourceHub::FrameSink& callback_sink,
const MediaSourceHub::CancelPredicate&) {
std::lock_guard<std::mutex> lock(sink_mutex);
sink = callback_sink;
return true;
};
callbacks.stop = [&] {
std::lock_guard<std::mutex> lock(sink_mutex);
sink = {};
};
CHECK_TRUE(hub.registerSource(descriptor, std::move(callbacks), 8));
auto subscription = hub.subscribe(descriptor->id);
CHECK_TRUE(subscription.valid());
MediaSourceHub::FrameSink producer;
{
std::lock_guard<std::mutex> lock(sink_mutex);
producer = sink;
}
CHECK_TRUE(static_cast<bool>(producer));
producer(makeFrame(descriptor, 0, 10));
producer(makeFrame(descriptor, 1, 20));
producer(makeFrame(descriptor, 2, 30));
CHECK_TRUE(subscription.discardPendingIfExceeds(2) == 3);
CHECK_TRUE(!subscription.tryRead().has_value());
producer(makeFrame(descriptor, 3, 40));
const auto next = subscription.tryRead();
CHECK_TRUE(next.has_value());
CHECK_TRUE(next->value->sequence == 3);
CHECK_TRUE(next->dropped_since_last_read == 3);
CHECK_TRUE(subscription.droppedCount() == 3);
}
void testHubFailedStartAndShutdown() {
MediaSourceHub hub;
const auto descriptor = makeVideoDescriptor(Codec::UNKNOWN, 1);
std::atomic<int> start_attempts{0};
std::atomic<int> retry_stop_count{0};
MediaSourceHub::SourceCallbacks failed_callbacks;
failed_callbacks.start = [&](const MediaSourceHub::FrameSink&,
const MediaSourceHub::CancelPredicate&) {
return ++start_attempts >= 2;
};
failed_callbacks.stop = [&] { ++retry_stop_count; };
CHECK_TRUE(hub.registerSource(descriptor, std::move(failed_callbacks), 2));
auto failed = hub.subscribe(descriptor->id);
CHECK_TRUE(!failed.valid());
auto retry = hub.subscribe(descriptor->id);
CHECK_TRUE(retry.valid());
retry.reset();
CHECK_TRUE(start_attempts == 2);
CHECK_TRUE(retry_stop_count == 1);
CHECK_TRUE(hub.unregisterSource(descriptor->id));
std::atomic<int> stop_count{0};
MediaSourceHub::SourceCallbacks callbacks;
callbacks.start = [](const MediaSourceHub::FrameSink&,
const MediaSourceHub::CancelPredicate&) {
return true;
};
callbacks.stop = [&] { ++stop_count; };
CHECK_TRUE(hub.registerSource(descriptor, std::move(callbacks), 2));
auto live = hub.subscribe(descriptor->id);
CHECK_TRUE(live.valid());
hub.shutdown();
CHECK_TRUE(stop_count == 1);
CHECK_TRUE(!live.valid());
CHECK_TRUE(!live.waitRead(50ms).has_value());
}
void testKeyFrameRequestIsOrderedBeforeStop() {
MediaSourceHub hub;
const auto descriptor = makeVideoDescriptor(Codec::H264, 1);
std::atomic<bool> key_frame_entered{false};
std::atomic<bool> release_key_frame{false};
std::atomic<int> stop_count{0};
MediaSourceHub::SourceCallbacks callbacks;
callbacks.start = [](const MediaSourceHub::FrameSink&,
const MediaSourceHub::CancelPredicate&) {
return true;
};
callbacks.stop = [&] { ++stop_count; };
callbacks.request_key_frame = [&] {
key_frame_entered.store(true, std::memory_order_release);
while (!release_key_frame.load(std::memory_order_acquire)) {
std::this_thread::sleep_for(1ms);
}
return true;
};
CHECK_TRUE(hub.registerSource(descriptor, std::move(callbacks), 2));
auto subscription = hub.subscribe(descriptor->id);
CHECK_TRUE(subscription.valid());
auto key_frame = std::async(std::launch::async, [&] {
return hub.requestKeyFrame(descriptor->id);
});
const auto enter_deadline = std::chrono::steady_clock::now() + 500ms;
while (!key_frame_entered.load(std::memory_order_acquire) &&
std::chrono::steady_clock::now() < enter_deadline) {
std::this_thread::sleep_for(1ms);
}
CHECK_TRUE(key_frame_entered.load(std::memory_order_acquire));
auto stop = std::async(std::launch::async, [&] { subscription.reset(); });
CHECK_TRUE(stop.wait_for(20ms) == std::future_status::timeout);
CHECK_TRUE(stop_count.load(std::memory_order_acquire) == 0);
release_key_frame.store(true, std::memory_order_release);
CHECK_TRUE(key_frame.get());
CHECK_TRUE(stop.wait_for(500ms) == std::future_status::ready);
stop.get();
CHECK_TRUE(stop_count.load(std::memory_order_acquire) == 1);
CHECK_TRUE(!hub.requestKeyFrame(descriptor->id));
}
void testHubCancelsBlockedStartWithoutBlockingShutdown() {
MediaSourceHub hub;
const auto descriptor = makeVideoDescriptor(Codec::UNKNOWN, 1);
std::atomic<bool> start_entered{false};
std::atomic<bool> start_exited{false};
MediaSourceHub::SourceCallbacks callbacks;
callbacks.start = [&](const MediaSourceHub::FrameSink&,
const MediaSourceHub::CancelPredicate& cancelled) {
start_entered.store(true, std::memory_order_release);
while (!cancelled()) {
std::this_thread::sleep_for(2ms);
}
start_exited.store(true, std::memory_order_release);
return false;
};
CHECK_TRUE(hub.registerSource(descriptor, std::move(callbacks), 2));
auto subscription_future = std::async(std::launch::async, [&] {
return hub.subscribe(descriptor->id);
});
const auto start_deadline = std::chrono::steady_clock::now() + 500ms;
while (!start_entered.load(std::memory_order_acquire) &&
std::chrono::steady_clock::now() < start_deadline) {
std::this_thread::sleep_for(2ms);
}
auto shutdown_future = std::async(std::launch::async, [&] { hub.shutdown(); });
const bool shutdown_completed = shutdown_future.wait_for(500ms) ==
std::future_status::ready;
if (shutdown_completed) shutdown_future.get();
const bool subscribe_completed = subscription_future.wait_for(500ms) ==
std::future_status::ready;
bool invalid_subscription = false;
if (subscribe_completed) {
invalid_subscription = !subscription_future.get().valid();
}
const auto exit_deadline = std::chrono::steady_clock::now() + 500ms;
while (!start_exited.load(std::memory_order_acquire) &&
std::chrono::steady_clock::now() < exit_deadline) {
std::this_thread::sleep_for(2ms);
}
CHECK_TRUE(start_entered.load(std::memory_order_acquire));
CHECK_TRUE(shutdown_completed);
CHECK_TRUE(subscribe_completed);
CHECK_TRUE(invalid_subscription);
CHECK_TRUE(start_exited.load(std::memory_order_acquire));
}
void testHubQuarantinesNonCooperativeStart() {
MediaSourceHub hub;
const auto descriptor = makeVideoDescriptor(Codec::UNKNOWN, 1);
std::atomic<bool> start_entered{false};
std::atomic<bool> release_start{false};
std::atomic<bool> start_exited{false};
std::atomic<int> stop_count{0};
MediaSourceHub::SourceCallbacks callbacks;
callbacks.start = [&](const MediaSourceHub::FrameSink&,
const MediaSourceHub::CancelPredicate&) {
start_entered.store(true, std::memory_order_release);
while (!release_start.load(std::memory_order_acquire)) {
std::this_thread::sleep_for(2ms);
}
start_exited.store(true, std::memory_order_release);
return true;
};
callbacks.stop = [&] { ++stop_count; };
CHECK_TRUE(hub.registerSource(descriptor, std::move(callbacks), 2));
auto subscription_future = std::async(std::launch::async, [&] {
return hub.subscribe(descriptor->id);
});
const auto start_deadline = std::chrono::steady_clock::now() + 500ms;
while (!start_entered.load(std::memory_order_acquire) &&
std::chrono::steady_clock::now() < start_deadline) {
std::this_thread::sleep_for(2ms);
}
const auto shutdown_started = std::chrono::steady_clock::now();
hub.shutdown();
const bool shutdown_was_bounded =
std::chrono::steady_clock::now() - shutdown_started < 500ms;
const bool subscribe_completed = subscription_future.wait_for(500ms) ==
std::future_status::ready;
bool invalid_subscription = false;
if (subscribe_completed) {
invalid_subscription = !subscription_future.get().valid();
}
// Release the deliberately non-cooperative test callback before its stack
// captures go out of scope. Late successful startup must be stopped once.
release_start.store(true, std::memory_order_release);
const auto exit_deadline = std::chrono::steady_clock::now() + 500ms;
while ((!start_exited.load(std::memory_order_acquire) || stop_count.load() != 1) &&
std::chrono::steady_clock::now() < exit_deadline) {
std::this_thread::sleep_for(2ms);
}
CHECK_TRUE(start_entered.load(std::memory_order_acquire));
CHECK_TRUE(shutdown_was_bounded);
CHECK_TRUE(subscribe_completed);
CHECK_TRUE(invalid_subscription);
CHECK_TRUE(start_exited.load(std::memory_order_acquire));
CHECK_TRUE(stop_count.load() == 1);
}
} // namespace
int main() {
testMediaMetadataValidation();
testLegacySpmcCompatibility();
testBroadcastFrameRing();
testBroadcastDiscardPending();
testBroadcastDiscardConcurrentPublish();
testBroadcastConcurrency();
testHubLifecycleAndDescriptorRefresh();
testSubscriptionDiscardPending();
testHubFailedStartAndShutdown();
testKeyFrameRequestIsOrderedBeforeStop();
testHubCancelsBlockedStartWithoutBlockingShutdown();
testHubQuarantinesNonCooperativeStart();
if (failures != 0) {
std::cerr << failures << " media_source_hub checks failed\n";
return 1;
}
std::cout << "media_source_hub self-test passed\n";
return 0;
}

View File

@ -20,6 +20,8 @@ target_link_libraries(service PRIVATE
cmvr_es::task_manager
cmvr_es::algorithms::controller
cmvr_es::task
cmvr_es::media_source_hub
cmvr_es::device_media_source_adapter
protobuf::libprotobuf
)

View File

@ -9,12 +9,14 @@
#include "common/base/grpc_utils.h"
#include "manager/device_manager/include/device_manager.h"
#include "devices/camera/abstract_camera.h"
#include "service/grpc/include/grpc_camera_stream_policy.h"
namespace cmvr::service {
class gRPCCameraServiceImpl final: public api::CameraService::Service {
public:
gRPCCameraServiceImpl();
explicit gRPCCameraServiceImpl(
CameraStreamLowLatencyConfig stream_config = {});
~gRPCCameraServiceImpl() override = default;
grpc::Status GetStatus(grpc::ServerContext* context, const api::GetCameraStateCommand_Request* request, api::GetCameraStateCommand_Feedback* response) override;
grpc::Status StartCamera(grpc::ServerContext* context, const api::StartCameraCommand_Request* request, api::StartCameraCommand_Feedback* response) override;
@ -24,11 +26,13 @@ namespace cmvr::service {
grpc::Status GetRGBDImages(grpc::ServerContext* context, const api::GetRGBDImagesCommand_Request* request, api::GetRGBDImagesCommand_Feedback* response) override;
grpc::Status StartRecording(grpc::ServerContext* context, const api::StartCameraRecordingCommand_Request* request, api::StartCameraRecordingCommand_Feedback* response) override;
grpc::Status StopRecording(grpc::ServerContext* context, const api::StopCameraRecordingCommand_Request* request, api::StopCameraRecordingCommand_Feedback* response) override;
grpc::Status ControlPtz(grpc::ServerContext* context, const api::ControlPtzCommand_Request* request, api::ControlPtzCommand_Feedback* response) override;
grpc::Status GetDepthImageStream(grpc::ServerContext* context, grpc::ServerReaderWriter<cmvr::api::GetDepthImageStreamCommand_Feedback, cmvr::api::GetDepthImageStreamCommand_Request>* stream) override;
grpc::Status GetRGBDImagesStream(grpc::ServerContext* context, grpc::ServerReaderWriter<cmvr::api::GetRGBDImagesStreamCommand_Feedback, cmvr::api::GetRGBDImagesStreamCommand_Request>* stream) override;
grpc::Status GetRGBImageStream(grpc::ServerContext* context, grpc::ServerReaderWriter<cmvr::api::GetRGBImageStreamCommand_Feedback, cmvr::api::GetRGBImageStreamCommand_Request>* stream) override;
private:
device::DeviceManager& dmgr_;
CameraStreamLowLatencyConfig stream_config_;
//双向流读写线程
std::shared_ptr<std::thread> read_thread_ = nullptr;

View File

@ -0,0 +1,60 @@
#ifndef CMVR_ES_GRPC_CAMERA_STREAM_POLICY_H
#define CMVR_ES_GRPC_CAMERA_STREAM_POLICY_H
#pragma once
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <optional>
namespace cmvr::service {
inline constexpr size_t kDefaultCameraStreamMaxPendingFrames = 2;
inline constexpr uint32_t kDefaultCameraStreamMaxFrameAgeMs = 250;
struct CameraStreamLowLatencyConfig {
size_t max_pending_frames{kDefaultCameraStreamMaxPendingFrames};
std::chrono::milliseconds max_frame_age{
kDefaultCameraStreamMaxFrameAgeMs};
};
inline CameraStreamLowLatencyConfig makeCameraStreamLowLatencyConfig(
const uint32_t max_pending_frames,
const uint32_t max_frame_age_ms) noexcept {
CameraStreamLowLatencyConfig config;
config.max_pending_frames = max_pending_frames == 0
? kDefaultCameraStreamMaxPendingFrames
: static_cast<size_t>(max_pending_frames);
config.max_frame_age = std::chrono::milliseconds(
max_frame_age_ms == 0
? kDefaultCameraStreamMaxFrameAgeMs
: max_frame_age_ms);
return config;
}
inline std::optional<uint64_t> cameraFrameAgeNs(
const uint64_t capture_time_ns,
const uint64_t now_ns) noexcept {
if (capture_time_ns == 0 || now_ns < capture_time_ns) {
return std::nullopt;
}
return now_ns - capture_time_ns;
}
inline bool cameraFrameExceedsAgeLimit(
const uint64_t capture_time_ns,
const uint64_t now_ns,
const std::chrono::milliseconds max_frame_age) noexcept {
const auto age_ns = cameraFrameAgeNs(capture_time_ns, now_ns);
if (!age_ns || max_frame_age.count() <= 0) {
return false;
}
const auto max_age_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
max_frame_age).count();
return *age_ns > static_cast<uint64_t>(max_age_ns);
}
} // namespace cmvr::service
#endif // CMVR_ES_GRPC_CAMERA_STREAM_POLICY_H

View File

@ -22,6 +22,7 @@ namespace cmvr::service
grpc::Status StopRecord(grpc::ServerContext* context, const api::StopMicRecordingCommand_Request* request,api::StopMicRecordingCommand_Feedback* response) override;
grpc::Status PauseRecord(grpc::ServerContext* context, const api::PauseMicRecordingCommand_Request* request,api::PauseMicRecordingCommand_Feedback* response) override;
grpc::Status ResumeRecord(grpc::ServerContext* context, const api::ResumeMicRecordingCommand_Request* request,api::ResumeMicRecordingCommand_Feedback* response) override;
grpc::Status StreamAudio(grpc::ServerContext* context, const api::StreamMicAudioCommand_Request* request, grpc::ServerWriter<api::StreamMicAudioCommand_Feedback>* writer) override;
grpc::Status SetVolume(grpc::ServerContext* context, const api::SetMicPhoneVolumeCommand_Request* request,api::SetMicPhoneVolumeCommand_Feedback* response) override;
grpc::Status GetVolume(grpc::ServerContext* context, const api::GetMicPhoneVolumeCommand_Request* request,api::GetMicPhoneVolumeCommand_Feedback* response) override;
private:

View File

@ -17,6 +17,7 @@ namespace cmvr::service {
~gRPCSpeakerServiceImpl() override = default;
grpc::Status GetStatus(grpc::ServerContext* context, const api::GetSpeakerStateCommand_Request* request,api::GetSpeakerStateCommand_Feedback* response) override;
grpc::Status PlayAudio(grpc::ServerContext* context, const api::PlayAudioCommand_Request* request,api::PlayAudioCommand_Feedback* response) override;
grpc::Status StreamAudio(grpc::ServerContext* context, grpc::ServerReader<api::StreamSpeakerAudioCommand_Request>* reader, api::StreamSpeakerAudioCommand_Feedback* response) override;
grpc::Status StopPlayback(grpc::ServerContext* context, const api::StopSpeakerCommand_Request* request,api::StopSpeakerCommand_Feedback* response) override;
grpc::Status PausePlayback(grpc::ServerContext* context, const api::PauseSpeakerCommand_Request* request,api::PauseSpeakerCommand_Feedback* response) override;
grpc::Status ResumePlayback(grpc::ServerContext* context, const api::ResumeSpeakerCommand_Request* request,api::ResumeSpeakerCommand_Feedback* response) override;

View File

@ -1,11 +1,17 @@
#include "common/base/logging/logger.h"
#include "manager/media_source_hub/include/device_media_source_adapter.h"
//
// Created by xtkuang on 2025/6/1.
//
#include "../include/grpc_camera_service.h"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <limits>
#include <thread>
using namespace std;
using namespace cmvr::service;
@ -20,9 +26,88 @@ grpc::Status failResponse(ResponseT* response, const std::string& message) {
setCurrentTimestamp(response->mutable_header()->mutable_timestamp());
return grpc::Status::OK;
}
bool toPtzCommand(cmvr::api::ControlPtzCommand_Command command, PtzCommand& out)
{
switch (command) {
case cmvr::api::ControlPtzCommand_Command_TILT_UP:
out = PtzCommand::TiltUp;
return true;
case cmvr::api::ControlPtzCommand_Command_TILT_DOWN:
out = PtzCommand::TiltDown;
return true;
case cmvr::api::ControlPtzCommand_Command_PAN_LEFT:
out = PtzCommand::PanLeft;
return true;
case cmvr::api::ControlPtzCommand_Command_PAN_RIGHT:
out = PtzCommand::PanRight;
return true;
case cmvr::api::ControlPtzCommand_Command_UP_LEFT:
out = PtzCommand::UpLeft;
return true;
case cmvr::api::ControlPtzCommand_Command_UP_RIGHT:
out = PtzCommand::UpRight;
return true;
case cmvr::api::ControlPtzCommand_Command_DOWN_LEFT:
out = PtzCommand::DownLeft;
return true;
case cmvr::api::ControlPtzCommand_Command_DOWN_RIGHT:
out = PtzCommand::DownRight;
return true;
case cmvr::api::ControlPtzCommand_Command_ZOOM_IN:
out = PtzCommand::ZoomIn;
return true;
case cmvr::api::ControlPtzCommand_Command_ZOOM_OUT:
out = PtzCommand::ZoomOut;
return true;
case cmvr::api::ControlPtzCommand_Command_PAN_AUTO:
out = PtzCommand::PanAuto;
return true;
default:
return false;
}
}
gRPCCameraServiceImpl::gRPCCameraServiceImpl(): dmgr_(DeviceManager::getInstance()) {}
// The legacy depth/RGBD RPCs acquire the camera's shared producer directly
// instead of going through MediaSourceHub. Keep that lease exception-safe:
// cancellation, a failed Write(), or any conversion error must release exactly
// the one startStreaming() reference acquired by this call.
class CameraStreamingLease final {
public:
explicit CameraStreamingLease(std::shared_ptr<AbstractCamera> camera)
: camera_(std::move(camera)) {
active_ = camera_ && camera_->startStreaming();
}
~CameraStreamingLease() {
if (!active_ || !camera_) {
return;
}
try {
camera_->stopStreaming();
} catch (const std::exception& e) {
CMVR_LOG(ERROR) << "[gRPCCameraServiceImpl] failed to release camera stream lease: "
<< e.what();
} catch (...) {
CMVR_LOG(ERROR) << "[gRPCCameraServiceImpl] failed to release camera stream lease";
}
}
CameraStreamingLease(const CameraStreamingLease&) = delete;
CameraStreamingLease& operator=(const CameraStreamingLease&) = delete;
explicit operator bool() const noexcept { return active_; }
private:
std::shared_ptr<AbstractCamera> camera_;
bool active_{false};
};
}
gRPCCameraServiceImpl::gRPCCameraServiceImpl(
CameraStreamLowLatencyConfig stream_config)
: dmgr_(DeviceManager::getInstance()),
stream_config_(stream_config) {}
grpc::Status gRPCCameraServiceImpl::GetStatus(grpc::ServerContext* context,
const api::GetCameraStateCommand_Request* request, api::GetCameraStateCommand_Feedback* response)
@ -47,14 +132,6 @@ grpc::Status gRPCCameraServiceImpl::GetStatus(grpc::ServerContext* context,
response->mutable_state()->set_fps(state.fps);
response->mutable_state()->set_width(state.width);
response->mutable_state()->set_height(state.height);
CMVR_LOG(DEBUG) << "[gRPCCameraServiceImpl] (GetStatus): success, id=" << dev_id
<< ", initialized=" << state.is_initialized
<< ", opened=" << state.is_opened
<< ", streaming=" << state.is_streaming
<< ", recording=" << state.is_recording
<< ", error=" << state.is_error
<< ", size=" << state.width << "x" << state.height
<< ", fps=" << state.fps;
return grpc::Status::OK;
}
catch(const exception &e) {
@ -80,7 +157,6 @@ grpc::Status gRPCCameraServiceImpl::StartCamera(grpc::ServerContext* context,
}
response->mutable_header()->set_success(true);
setCurrentTimestamp(response->mutable_header()->mutable_timestamp());
CMVR_LOG(DEBUG) << "[gRPCCameraServiceImpl] (StartCamera): success, id=" << dev_id;
return grpc::Status::OK;
}
catch (const exception &e) {
@ -106,7 +182,6 @@ grpc::Status gRPCCameraServiceImpl::StopCamera(grpc::ServerContext* context,
}
response->mutable_header()->set_success(true);
setCurrentTimestamp(response->mutable_header()->mutable_timestamp());
CMVR_LOG(DEBUG) << "[gRPCCameraServiceImpl] (StopCamera): success, id=" << dev_id;
return grpc::Status::OK;
}
catch (exception &e) {
@ -130,7 +205,9 @@ grpc::Status gRPCCameraServiceImpl::GetRGBImage(grpc::ServerContext* context,
}
Rs2Intrinsics intrinsics = {0};
dev->getRGBImage(image,intrinsics);
response->mutable_header()->set_success(true);
if (image.empty()) {
return failResponse(response, "Camera returned an empty RGB image: " + dev_id);
}
response->mutable_intrinsics()->set_fx(intrinsics.fx);
response->mutable_intrinsics()->set_fy(intrinsics.fy);
@ -160,10 +237,6 @@ grpc::Status gRPCCameraServiceImpl::GetRGBImage(grpc::ServerContext* context,
response->mutable_color_frame()->set_height(image.rows);
response->mutable_color_frame()->set_width(image.cols);
response->mutable_color_frame()->set_codec("none");
CMVR_LOG(DEBUG) << "[gRPCCameraServiceImpl] (GetRGBImage): success, id=" << dev_id
<< ", size=" << image.cols << "x" << image.rows
<< ", cv_type=" << image.type()
<< ", bytes=" << image.total() * image.elemSize();
return grpc::Status::OK;
}
catch (exception &e) {
@ -187,6 +260,9 @@ grpc::Status gRPCCameraServiceImpl::GetDepthImage(grpc::ServerContext* context,
}
Rs2Intrinsics intrinsics = {0};
dev->getDepthImage(image,intrinsics);
if (image.empty()) {
return failResponse(response, "Camera returned an empty depth image: " + dev_id);
}
response->mutable_header()->set_success(true);
response->mutable_intrinsics()->set_fx(intrinsics.fx);
@ -220,10 +296,6 @@ grpc::Status gRPCCameraServiceImpl::GetDepthImage(grpc::ServerContext* context,
response->mutable_depth_frame()->set_height(image.rows);
response->mutable_depth_frame()->set_width(image.cols);
response->mutable_depth_frame()->set_codec("none");
CMVR_LOG(DEBUG) << "[gRPCCameraServiceImpl] (GetDepthImage): success, id=" << dev_id
<< ", size=" << image.cols << "x" << image.rows
<< ", cv_type=" << image.type()
<< ", bytes=" << image.total() * image.elemSize();
return grpc::Status::OK;
}
catch (exception &e) {
@ -247,6 +319,12 @@ grpc::Status gRPCCameraServiceImpl::GetRGBDImages(grpc::ServerContext* context,
}
Rs2Intrinsics intrinsics = {0};
dev->getRGBDImages(color_image,depth_image, intrinsics);
if (color_image.empty()) {
return failResponse(response, "Camera returned an empty RGB image: " + dev_id);
}
if (depth_image.empty()) {
return failResponse(response, "Camera returned an empty depth image: " + dev_id);
}
response->mutable_header()->set_success(true);
response->mutable_intrinsics()->set_fx(intrinsics.fx);
@ -259,7 +337,8 @@ grpc::Status gRPCCameraServiceImpl::GetRGBDImages(grpc::ServerContext* context,
response->mutable_header()->set_success(true);
setCurrentTimestamp(response->mutable_header()->mutable_timestamp());
// color image
// color image is serialized in the existing OpenCV BGR byte order;
// clients convert it once when constructing an RGB image.
if (color_image.type() == CV_8UC3) {
response->mutable_color_frame()->set_type(api::FrameData::U8C3);
}
@ -288,16 +367,14 @@ grpc::Status gRPCCameraServiceImpl::GetRGBDImages(grpc::ServerContext* context,
else if (depth_image.type() == CV_32FC1) {
response->mutable_depth_frame()->set_type(api::FrameData::F32C1);
}
else {
return failResponse(response, "unsupported depth image type");
}
response->mutable_depth_frame()->set_data(
reinterpret_cast<const char*>(depth_image.data), depth_image.total() * depth_image.elemSize());
response->mutable_depth_frame()->set_height(depth_image.rows);
response->mutable_depth_frame()->set_width(depth_image.cols);
response->mutable_depth_frame()->set_codec("none");
CMVR_LOG(DEBUG) << "[gRPCCameraServiceImpl] (GetRGBDImages): success, id=" << dev_id
<< ", color_size=" << color_image.cols << "x" << color_image.rows
<< ", color_type=" << color_image.type()
<< ", depth_size=" << depth_image.cols << "x" << depth_image.rows
<< ", depth_type=" << depth_image.type();
return grpc::Status::OK;
}
catch (exception &e) {
@ -321,8 +398,6 @@ grpc::Status gRPCCameraServiceImpl::StartRecording(grpc::ServerContext* context,
dev->startRecording(request->video_path());
response->mutable_header()->set_success(true);
setCurrentTimestamp(response->mutable_header()->mutable_timestamp());
CMVR_LOG(DEBUG) << "[gRPCCameraServiceImpl] (StartRecording): success, id=" << dev_id
<< ", path=" << request->video_path();
return grpc::Status::OK;
}
catch (exception &e) {
@ -346,7 +421,50 @@ grpc::Status gRPCCameraServiceImpl::StopRecording(grpc::ServerContext* context,
dev->stopRecording();
response->mutable_header()->set_success(true);
setCurrentTimestamp(response->mutable_header()->mutable_timestamp());
CMVR_LOG(DEBUG) << "[gRPCCameraServiceImpl] (StopRecording): success, id=" << dev_id;
return grpc::Status::OK;
}
catch (exception &e) {
response->mutable_header()->set_success(false);
response->mutable_header()->set_error_message(e.what());
setCurrentTimestamp(response->mutable_header()->mutable_timestamp());
return grpc::Status::OK;
}
}
grpc::Status gRPCCameraServiceImpl::ControlPtz(grpc::ServerContext* context,
const api::ControlPtzCommand_Request* request, api::ControlPtzCommand_Feedback* response)
{
try {
string dev_id = request->header().device_id();
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] (ControlPtz): id=" << dev_id
<< ", command=" << request->command()
<< ", action=" << request->action()
<< ", speed=" << request->speed();
const auto dev = dmgr_.getDevice<AbstractCamera>(dev_id);
if (!dev) {
return failResponse(response, "Camera device not found: " + dev_id);
}
PtzCommand command{};
if (!toPtzCommand(request->command(), command)) {
return failResponse(response, "Invalid PTZ command");
}
if (request->action() != api::ControlPtzCommand_Action_START &&
request->action() != api::ControlPtzCommand_Action_STOP) {
return failResponse(response, "Invalid PTZ action");
}
const bool stop = request->action() == api::ControlPtzCommand_Action_STOP;
if (!dev->controlPtz(command, stop, static_cast<int>(request->speed()))) {
CameraState state{};
dev->getState(state);
const std::string error_message =
state.error_message.empty() ? "Failed to control PTZ: " + dev_id : state.error_message;
return failResponse(response, error_message);
}
response->mutable_header()->set_success(true);
setCurrentTimestamp(response->mutable_header()->mutable_timestamp());
return grpc::Status::OK;
}
catch (exception &e) {
@ -363,9 +481,11 @@ grpc::Status gRPCCameraServiceImpl::GetDepthImageStream(grpc::ServerContext* con
try {
//读取首次传递的数据,获取设备id
api::GetDepthImageStreamCommand_Request request;
stream->Read(&request);
if (!stream->Read(&request)) {
return grpc::Status::OK;
}
string dev_id = request.header().device_id();
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] (GetRGBImageStream): start,id=" << dev_id;
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] (GetDepthImageStream): start,id=" << dev_id;
const auto dev = dmgr_.getDevice<AbstractCamera>(dev_id);
if (!dev) {
api::GetDepthImageStreamCommand_Feedback response;
@ -375,9 +495,17 @@ grpc::Status gRPCCameraServiceImpl::GetDepthImageStream(grpc::ServerContext* con
stream->Write(response);
return grpc::Status::OK;
}
CameraStreamingLease stream_lease(dev);
if (!stream_lease) {
api::GetDepthImageStreamCommand_Feedback response;
response.mutable_header()->set_success(false);
response.mutable_header()->set_error_message("Failed to start camera stream: " + dev_id);
setCurrentTimestamp(response.mutable_header()->mutable_timestamp());
stream->Write(response);
return grpc::Status::OK;
}
int nFrameCount = 0;
dev->startStreaming();
CMVR_LOG(DEBUG) << "[gRPCCameraServiceImpl] (GetDepthImageStream): start streaming success, id=" << dev_id;
size_t index = 0;
while (true)
{
@ -389,39 +517,39 @@ grpc::Status gRPCCameraServiceImpl::GetDepthImageStream(grpc::ServerContext* con
api::GetDepthImageStreamCommand_Feedback response;
cmvr::device::StreamFrameData frame_data;
dev->getEncodedFrame(frame_data,index);
if (!frame_data.rgbFrame.empty()) {
if (dev->waitEncodedFrame(frame_data, index, std::chrono::milliseconds(100)) &&
!frame_data.depthFrame.empty()) {
response.mutable_header()->set_success(true);
setCurrentTimestamp(response.mutable_header()->mutable_timestamp());
response.mutable_depth_frame()->set_type(api::FrameData::U16C1);
response.mutable_depth_frame()->set_data(frame_data.depthFrame.data(), frame_data.depthFrame.size());
response.mutable_depth_frame()->set_is_key_frame(frame_data.depthKey);
response.mutable_depth_frame()->set_codec(frame_data.codec);
response.mutable_depth_frame()->set_width(frame_data.width);
response.mutable_depth_frame()->set_height(frame_data.height);
response.mutable_depth_frame()->set_codec("none");
response.mutable_depth_frame()->set_width(frame_data.depth_width > 0 ? frame_data.depth_width : frame_data.width);
response.mutable_depth_frame()->set_height(frame_data.depth_height > 0 ? frame_data.depth_height : frame_data.height);
response.mutable_intrinsics()->set_fx(frame_data.intrinsics.fx);
response.mutable_intrinsics()->set_fy(frame_data.intrinsics.fx);
response.mutable_intrinsics()->set_cx(frame_data.intrinsics.fx);
response.mutable_intrinsics()->set_cy(frame_data.intrinsics.fx);
response.mutable_intrinsics()->set_fy(frame_data.intrinsics.fy);
response.mutable_intrinsics()->set_cx(frame_data.intrinsics.cx);
response.mutable_intrinsics()->set_cy(frame_data.intrinsics.cy);
for (int i = 0; i < 5 ; i++) {
response.mutable_intrinsics()->add_coeffs(frame_data.intrinsics.coeffs[i]);
}
response.set_seq_no(nFrameCount++);
grpc::WriteOptions options;
options.set_last_message();
if (!stream->Write(response)) {
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] (stream->Write) failed,id=" << dev_id;
break;
}
}
}
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] (GetRGBImageStream): end,id=" << dev_id;
dev->stopStreaming();
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] (GetDepthImageStream): end,id=" << dev_id;
return grpc::Status::OK;
}
catch (exception &e) {
catch (const exception &e) {
api::GetDepthImageStreamCommand_Feedback response;
response.mutable_header()->set_success(false);
response.mutable_header()->set_error_message(e.what());
setCurrentTimestamp(response.mutable_header()->mutable_timestamp());
stream->Write(response);
@ -433,9 +561,11 @@ grpc::Status gRPCCameraServiceImpl::GetRGBDImagesStream(grpc::ServerContext* con
try {
//读取首次传递的数据,获取设备id
api::GetRGBDImagesStreamCommand_Request request;
stream->Read(&request);
if (!stream->Read(&request)) {
return grpc::Status::OK;
}
string dev_id = request.header().device_id();
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] (GetRGBImageStream): start,id=" << dev_id;
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] (GetRGBDImagesStream): start,id=" << dev_id;
const auto dev = dmgr_.getDevice<AbstractCamera>(dev_id);
if (!dev) {
api::GetRGBDImagesStreamCommand_Feedback response;
@ -445,9 +575,17 @@ grpc::Status gRPCCameraServiceImpl::GetRGBDImagesStream(grpc::ServerContext* con
stream->Write(response);
return grpc::Status::OK;
}
CameraStreamingLease stream_lease(dev);
if (!stream_lease) {
api::GetRGBDImagesStreamCommand_Feedback response;
response.mutable_header()->set_success(false);
response.mutable_header()->set_error_message("Failed to start camera stream: " + dev_id);
setCurrentTimestamp(response.mutable_header()->mutable_timestamp());
stream->Write(response);
return grpc::Status::OK;
}
int nFrameCount = 0;
dev->startStreaming();
CMVR_LOG(DEBUG) << "[gRPCCameraServiceImpl] (GetRGBDImagesStream): start streaming success, id=" << dev_id;
size_t index = 0;
while (true)
{
@ -459,33 +597,34 @@ grpc::Status gRPCCameraServiceImpl::GetRGBDImagesStream(grpc::ServerContext* con
api::GetRGBDImagesStreamCommand_Feedback response;
cmvr::device::StreamFrameData frame_data;
dev->getEncodedFrame(frame_data,index);
if (!frame_data.rgbFrame.empty()) {
if (dev->waitEncodedFrame(frame_data, index, std::chrono::milliseconds(100)) &&
!frame_data.rgbFrame.empty() &&
!frame_data.depthFrame.empty()) {
response.mutable_header()->set_success(true);
setCurrentTimestamp(response.mutable_header()->mutable_timestamp());
response.mutable_color_frame()->set_data(frame_data.rgbFrame.data(), frame_data.rgbFrame.size());
response.mutable_color_frame()->set_is_key_frame(frame_data.bKey);
response.mutable_color_frame()->set_codec(frame_data.codec);
response.mutable_color_frame()->set_width(frame_data.width);
response.mutable_color_frame()->set_height(frame_data.height);
response.mutable_depth_frame()->set_type(api::FrameData::U16C1);
response.mutable_depth_frame()->set_data(frame_data.depthFrame.data(), frame_data.depthFrame.size());
response.mutable_depth_frame()->set_is_key_frame(frame_data.depthKey);
response.mutable_depth_frame()->set_codec(frame_data.codec);
response.mutable_depth_frame()->set_width(frame_data.width);
response.mutable_depth_frame()->set_height(frame_data.height);
response.mutable_depth_frame()->set_codec("none");
response.mutable_depth_frame()->set_width(frame_data.depth_width > 0 ? frame_data.depth_width : frame_data.width);
response.mutable_depth_frame()->set_height(frame_data.depth_height > 0 ? frame_data.depth_height : frame_data.height);
response.mutable_intrinsics()->set_fx(frame_data.intrinsics.fx);
response.mutable_intrinsics()->set_fy(frame_data.intrinsics.fx);
response.mutable_intrinsics()->set_cx(frame_data.intrinsics.fx);
response.mutable_intrinsics()->set_cy(frame_data.intrinsics.fx);
response.mutable_intrinsics()->set_fy(frame_data.intrinsics.fy);
response.mutable_intrinsics()->set_cx(frame_data.intrinsics.cx);
response.mutable_intrinsics()->set_cy(frame_data.intrinsics.cy);
for (int i = 0; i < 5 ; i++) {
response.mutable_intrinsics()->add_coeffs(frame_data.intrinsics.coeffs[i]);
}
response.set_seq_no(nFrameCount++);
grpc::WriteOptions options;
options.set_last_message();
if (!stream->Write(response)) {
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] (stream->Write) failed,id=" << dev_id;
break;
@ -493,11 +632,11 @@ grpc::Status gRPCCameraServiceImpl::GetRGBDImagesStream(grpc::ServerContext* con
}
}
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] (GetRGBDImagesStream): end,id=" << dev_id;
dev->stopStreaming();
return grpc::Status::OK;
}
catch (exception &e) {
catch (const exception &e) {
api::GetRGBDImagesStreamCommand_Feedback response;
response.mutable_header()->set_success(false);
response.mutable_header()->set_error_message(e.what());
setCurrentTimestamp(response.mutable_header()->mutable_timestamp());
stream->Write(response);
@ -508,9 +647,13 @@ grpc::Status gRPCCameraServiceImpl::GetRGBImageStream(grpc::ServerContext* conte
try {
//读取首次传递的数据,获取设备id
api::GetRGBImageStreamCommand_Request request;
stream->Read(&request);
if (!stream->Read(&request)) {
return grpc::Status::OK;
}
string dev_id = request.header().device_id();
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] (GetRGBImageStream): start,id=" << dev_id;
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] (GetRGBImageStream): start,id=" << dev_id
<< ", max_pending_frames=" << stream_config_.max_pending_frames
<< ", max_frame_age_ms=" << stream_config_.max_frame_age.count();
const auto dev = dmgr_.getDevice<AbstractCamera>(dev_id);
if (!dev) {
api::GetRGBImageStreamCommand_Feedback response;
@ -520,57 +663,259 @@ grpc::Status gRPCCameraServiceImpl::GetRGBImageStream(grpc::ServerContext* conte
stream->Write(response);
return grpc::Status::OK;
}
int nFrameCount = 0;
dev->startStreaming();
CMVR_LOG(DEBUG) << "[gRPCCameraServiceImpl] (GetRGBImageStream): start streaming success, id=" << dev_id;
size_t last_sent_index = std::numeric_limits<size_t>::max();
auto& media_hub = cmvr::media::globalMediaSourceHub();
const std::string track_id = cmvr::media::cameraColorTrackId(dev_id);
if (!cmvr::media::ensureCameraMediaSource(media_hub, dev)) {
api::GetRGBImageStreamCommand_Feedback response;
response.mutable_header()->set_success(false);
response.mutable_header()->set_error_message("Failed to register camera media source: " + dev_id);
setCurrentTimestamp(response.mutable_header()->mutable_timestamp());
stream->Write(response);
return grpc::Status::OK;
}
auto subscription = media_hub.subscribe(
track_id,
cmvr::media::MediaSourceHub::StartPosition::NEXT_PUBLISHED,
[context] { return context->IsCancelled(); });
if (!subscription) {
api::GetRGBImageStreamCommand_Feedback response;
response.mutable_header()->set_success(false);
response.mutable_header()->set_error_message("Failed to subscribe camera media source: " + dev_id);
setCurrentTimestamp(response.mutable_header()->mutable_timestamp());
stream->Write(response);
return grpc::Status::OK;
}
std::atomic<bool> client_eof_requested{false};
std::atomic<bool> request_stream_closed{false};
std::atomic<uint64_t> control_requests_read{1};
std::thread request_reader([&] {
api::GetRGBImageStreamCommand_Request control_request;
while (stream->Read(&control_request)) {
++control_requests_read;
if (control_request.eof()) {
client_eof_requested.store(true, std::memory_order_release);
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] client requested RGB stream EOF"
<< ", id=" << dev_id
<< ", peer=" << context->peer()
<< ", control_requests=" << control_requests_read.load();
break;
}
}
request_stream_closed.store(true, std::memory_order_release);
});
struct RequestReaderJoiner {
std::thread& thread;
~RequestReaderJoiner() {
if (thread.joinable()) {
thread.join();
}
}
} request_reader_joiner{request_reader};
const auto join_request_reader = [&] {
if (request_reader.joinable()) {
request_reader.join();
}
};
bool waiting_for_key_frame = true;
const char* exit_reason = "unknown";
auto last_key_frame_request = std::chrono::steady_clock::now();
auto last_latency_log = std::chrono::steady_clock::time_point{};
uint64_t discarded_since_log = 0;
std::chrono::microseconds last_write_duration{0};
media_hub.requestKeyFrame(track_id);
const auto request_key_frame_if_due = [&] {
const auto now = std::chrono::steady_clock::now();
if (now - last_key_frame_request >= std::chrono::milliseconds(500)) {
media_hub.requestKeyFrame(track_id);
last_key_frame_request = now;
}
};
const auto request_key_frame_now = [&] {
waiting_for_key_frame = true;
media_hub.requestKeyFrame(track_id);
last_key_frame_request = std::chrono::steady_clock::now();
};
const auto log_latency_event = [&](
const char* reason,
const uint64_t discarded,
const uint64_t frame_age_ns,
const std::chrono::microseconds write_duration) {
discarded_since_log += discarded;
const auto now = std::chrono::steady_clock::now();
if (last_latency_log != std::chrono::steady_clock::time_point{} &&
now - last_latency_log < std::chrono::seconds(1)) {
return;
}
const double frame_age_ms = static_cast<double>(frame_age_ns) / 1'000'000.0;
const double write_ms = static_cast<double>(write_duration.count()) / 1'000.0;
CMVR_LOG(WARNING) << "[gRPCCameraServiceImpl] low-latency camera stream event"
<< ", id=" << dev_id
<< ", reason=" << reason
<< ", discarded=" << discarded_since_log
<< ", age_ms=" << frame_age_ms
<< ", write_ms=" << write_ms
<< ", max_pending_frames="
<< stream_config_.max_pending_frames
<< ", max_frame_age_ms="
<< stream_config_.max_frame_age.count();
discarded_since_log = 0;
last_latency_log = now;
};
while (true)
{
if (client_eof_requested.load(std::memory_order_acquire)) {
exit_reason = "client_eof";
break;
}
if (context->IsCancelled())
{
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl](GetRGBImageStream) context is cancelled,id=" << dev_id;
exit_reason = "context_cancelled";
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl](GetRGBImageStream) context is cancelled"
<< ", id=" << dev_id
<< ", peer=" << context->peer()
<< ", client_eof=" << client_eof_requested.load()
<< ", request_stream_closed=" << request_stream_closed.load();
break;
}
api::GetRGBImageStreamCommand_Feedback response;
cmvr::device::StreamFrameData frame_data;
size_t next_index = last_sent_index;
if (!dev->getLatestEncodedFrame(frame_data, next_index) ||
frame_data.rgbFrame.empty() ||
next_index == last_sent_index) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
const auto read = subscription.waitRead(std::chrono::milliseconds(100));
if (!read || !read->value || read->value->empty()) {
if (!subscription.valid()) {
exit_reason = "subscription_invalid";
break;
}
if (waiting_for_key_frame) {
request_key_frame_if_due();
}
continue;
}
response.mutable_header()->set_success(true);
response.mutable_color_frame()->set_data(frame_data.rgbFrame.data(), frame_data.rgbFrame.size());
response.mutable_color_frame()->set_is_key_frame(frame_data.bKey);
response.mutable_color_frame()->set_codec(frame_data.codec);
response.mutable_color_frame()->set_width(frame_data.width);
response.mutable_color_frame()->set_height(frame_data.height);
response.mutable_intrinsics()->set_fx(frame_data.intrinsics.fx);
response.mutable_intrinsics()->set_fy(frame_data.intrinsics.fy);
response.mutable_intrinsics()->set_cx(frame_data.intrinsics.cx);
response.mutable_intrinsics()->set_cy(frame_data.intrinsics.cy);
for (int i = 0; i < 5 ; i++) {
response.mutable_intrinsics()->add_coeffs(frame_data.intrinsics.coeffs[i]);
const auto& frame = *read->value;
const auto descriptor = frame.descriptor;
if (!descriptor) {
continue;
}
response.set_seq_no(nFrameCount++);
if (!stream->Write(response)) {
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] (stream->Write) failed,id=" << dev_id;
const uint64_t now_ns = static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count());
const auto frame_age = cameraFrameAgeNs(frame.capture_time_ns, now_ns);
if (frame_age &&
cameraFrameExceedsAgeLimit(
frame.capture_time_ns,
now_ns,
stream_config_.max_frame_age)) {
// This frame is already outside the latency budget. Flush all
// currently queued frames and wait for a fresh IDR; sending any
// P/B frame after an intentional gap would break decoder continuity.
const uint64_t discarded =
1 + subscription.discardPendingIfExceeds(0);
request_key_frame_now();
log_latency_event(
"stale_frame",
discarded,
*frame_age,
last_write_duration);
continue;
}
const bool inter_frame_codec = descriptor->codec == cmvr::media::Codec::H264 ||
descriptor->codec == cmvr::media::Codec::H265;
if (!inter_frame_codec || descriptor->payload_format != cmvr::media::PayloadFormat::ANNEX_B) {
api::GetRGBImageStreamCommand_Feedback response;
response.mutable_header()->set_success(false);
response.mutable_header()->set_error_message(
"Unsupported camera stream codec or payload format: " + dev_id);
setCurrentTimestamp(response.mutable_header()->mutable_timestamp());
stream->Write(response);
exit_reason = "unsupported_stream";
break;
}
last_sent_index = next_index;
if (read->dropped_since_last_read > 0 || read->generation_changed || frame.discontinuity) {
request_key_frame_now();
}
if (waiting_for_key_frame && !frame.key_frame) {
request_key_frame_if_due();
continue;
}
waiting_for_key_frame = false;
api::GetRGBImageStreamCommand_Feedback response;
response.mutable_header()->set_success(true);
setCurrentTimestamp(response.mutable_header()->mutable_timestamp());
response.mutable_color_frame()->set_data(frame.data(), frame.size());
response.mutable_color_frame()->set_is_key_frame(frame.key_frame);
response.mutable_color_frame()->set_codec(
descriptor->codec == cmvr::media::Codec::H264 ? "h264" :
descriptor->codec == cmvr::media::Codec::H265 ? "h265" : "unknown");
response.mutable_color_frame()->set_width(static_cast<int32_t>(descriptor->width));
response.mutable_color_frame()->set_height(static_cast<int32_t>(descriptor->height));
response.mutable_color_frame()->set_capture_utc_ns(frame.capture_utc_ns);
response.mutable_color_frame()->set_source_sequence(frame.sequence);
response.mutable_color_frame()->set_pts(frame.pts);
response.mutable_color_frame()->set_dts(frame.dts);
response.mutable_color_frame()->set_source_fps(descriptor->nominal_rate);
response.mutable_color_frame()->set_source_timestamp(frame.source_timestamp);
response.mutable_color_frame()->set_source_frame_number(frame.source_frame_number);
response.mutable_intrinsics()->set_fx(descriptor->fx);
response.mutable_intrinsics()->set_fy(descriptor->fy);
response.mutable_intrinsics()->set_cx(descriptor->cx);
response.mutable_intrinsics()->set_cy(descriptor->cy);
for (const float coefficient : descriptor->distortion) {
response.mutable_intrinsics()->add_coeffs(coefficient);
}
response.set_seq_no(static_cast<int32_t>(std::min<uint64_t>(
frame.sequence,
static_cast<uint64_t>(std::numeric_limits<int32_t>::max()))));
const auto write_started = std::chrono::steady_clock::now();
if (!stream->Write(response)) {
exit_reason = "write_failed";
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] (stream->Write) failed"
<< ", id=" << dev_id
<< ", peer=" << context->peer()
<< ", context_cancelled=" << context->IsCancelled()
<< ", client_eof=" << client_eof_requested.load()
<< ", request_stream_closed=" << request_stream_closed.load()
<< ", control_requests=" << control_requests_read.load()
<< ", write_ms="
<< std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now() - write_started).count() / 1000.0;
break;
}
last_write_duration = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now() - write_started);
// A successful synchronous Write may have been flow-controlled long
// enough for the source to outpace this consumer. Once the pending
// count crosses the configured trigger, discard the whole pending
// batch and require a fresh key frame before resuming.
const uint64_t discarded = subscription.discardPendingIfExceeds(
stream_config_.max_pending_frames);
if (discarded > 0) {
request_key_frame_now();
log_latency_event(
"write_backpressure",
discarded,
frame_age.value_or(0),
last_write_duration);
}
}
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] (GetRGBImageStream): end,id=" << dev_id;
dev->stopStreaming();
join_request_reader();
CMVR_LOG(INFO) << "[gRPCCameraServiceImpl] (GetRGBImageStream): end"
<< ", id=" << dev_id
<< ", reason=" << exit_reason
<< ", peer=" << context->peer()
<< ", context_cancelled=" << context->IsCancelled()
<< ", client_eof=" << client_eof_requested.load()
<< ", request_stream_closed=" << request_stream_closed.load()
<< ", control_requests=" << control_requests_read.load();
return grpc::Status::OK;
}
catch (exception &e) {
catch (const exception &e) {
api::GetRGBImageStreamCommand_Feedback response;
response.mutable_header()->set_success(false);
response.mutable_header()->set_error_message(e.what());
setCurrentTimestamp(response.mutable_header()->mutable_timestamp());
stream->Write(response);

View File

@ -1,4 +1,9 @@
#include "common/base/logging/logger.h"
#include "manager/media_source_hub/include/device_media_source_adapter.h"
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <limits>
//
// Created by linbo on 2025/6/13.
// Created by xtkuang on 2025/6/13.
@ -18,6 +23,7 @@ grpc::Status failResponse(ResponseT* response, const std::string& message) {
setCurrentTimestamp(response->mutable_header()->mutable_timestamp());
return grpc::Status::OK;
}
}
gRPCMicroPhoneServiceImpl::gRPCMicroPhoneServiceImpl(): dmgr_(DeviceManager::getInstance()) {}
@ -151,6 +157,111 @@ grpc::Status gRPCMicroPhoneServiceImpl::ResumeRecord(grpc::ServerContext* contex
}
}
grpc::Status gRPCMicroPhoneServiceImpl::StreamAudio(grpc::ServerContext* context,
const api::StreamMicAudioCommand_Request* request,
grpc::ServerWriter<api::StreamMicAudioCommand_Feedback>* writer) {
try {
const string dev_id = request->header().device_id();
CMVR_LOG(INFO) << "[gRPCMicroPhoneServiceImpl] (StreamAudio): id=" << dev_id;
const auto dev = dmgr_.getDevice<AbstractMicrophone>(dev_id);
if (!dev) {
api::StreamMicAudioCommand_Feedback feedback;
feedback.mutable_header()->set_success(false);
feedback.mutable_header()->set_error_message("Microphone device not found: " + dev_id);
setCurrentTimestamp(feedback.mutable_header()->mutable_timestamp());
writer->Write(feedback);
return grpc::Status::OK;
}
auto& media_hub = cmvr::media::globalMediaSourceHub();
const std::string track_id = cmvr::media::microphoneTrackId(dev_id);
if (!cmvr::media::ensureMicrophoneMediaSource(media_hub, dev)) {
api::StreamMicAudioCommand_Feedback feedback;
feedback.mutable_header()->set_success(false);
feedback.mutable_header()->set_error_message("Failed to register microphone media source: " + dev_id);
setCurrentTimestamp(feedback.mutable_header()->mutable_timestamp());
writer->Write(feedback);
return grpc::Status::OK;
}
auto subscription = media_hub.subscribe(
track_id,
cmvr::media::MediaSourceHub::StartPosition::NEXT_PUBLISHED,
[context] { return context->IsCancelled(); });
if (!subscription) {
api::StreamMicAudioCommand_Feedback feedback;
feedback.mutable_header()->set_success(false);
feedback.mutable_header()->set_error_message("Failed to subscribe microphone media source: " + dev_id);
setCurrentTimestamp(feedback.mutable_header()->mutable_timestamp());
writer->Write(feedback);
return grpc::Status::OK;
}
while (!context->IsCancelled()) {
const auto read = subscription.waitRead(std::chrono::milliseconds(100));
if (!read || !read->value || read->value->empty()) {
if (!subscription.valid()) {
break;
}
continue;
}
const auto& frame = *read->value;
const auto descriptor = frame.descriptor;
if (!descriptor) {
continue;
}
api::StreamMicAudioCommand_Feedback feedback;
feedback.mutable_header()->set_success(true);
setCurrentTimestamp(feedback.mutable_header()->mutable_timestamp());
auto* audio = feedback.mutable_audio();
audio->set_data(frame.data(), frame.size());
audio->set_sample_rate(static_cast<int32_t>(std::min<uint32_t>(
descriptor->sample_rate,
static_cast<uint32_t>(std::numeric_limits<int32_t>::max()))));
audio->set_channels(static_cast<int32_t>(std::min<uint32_t>(
descriptor->channels,
static_cast<uint32_t>(std::numeric_limits<int32_t>::max()))));
if (descriptor->codec == cmvr::media::Codec::PCM_S16LE) {
audio->set_format(cmvr::api::AudioData_AudioFormat_PCM);
audio->set_codec("pcm_s16le");
} else if (descriptor->codec == cmvr::media::Codec::OPUS) {
audio->set_format(cmvr::api::AudioData_AudioFormat_OPUS);
audio->set_codec("opus");
} else if (descriptor->codec == cmvr::media::Codec::AAC) {
audio->set_format(cmvr::api::AudioData_AudioFormat_AAC);
audio->set_codec("aac");
} else {
feedback.mutable_header()->set_success(false);
feedback.mutable_header()->set_error_message(
"Unsupported microphone stream codec: " + dev_id);
feedback.clear_audio();
writer->Write(feedback);
break;
}
audio->set_pts(frame.pts);
const int64_t sample_count = frame.duration > 0
? frame.duration
: static_cast<int64_t>(descriptor->nominal_rate);
audio->set_nb_samples(static_cast<int32_t>(std::clamp<int64_t>(
sample_count,
0,
std::numeric_limits<int32_t>::max())));
if (!writer->Write(feedback)) {
break;
}
}
return grpc::Status::OK;
} catch (const std::exception& error) {
api::StreamMicAudioCommand_Feedback feedback;
feedback.mutable_header()->set_success(false);
feedback.mutable_header()->set_error_message(error.what());
setCurrentTimestamp(feedback.mutable_header()->mutable_timestamp());
writer->Write(feedback);
return grpc::Status::OK;
}
}
grpc::Status gRPCMicroPhoneServiceImpl::SetVolume(grpc::ServerContext* context,
const api::SetMicPhoneVolumeCommand_Request* request, api::SetMicPhoneVolumeCommand_Feedback* response) {
try {

View File

@ -1,4 +1,5 @@
#include "common/base/logging/logger.h"
#include <memory>
//
// Created by xtkuang on 2025/6/10.
//
@ -18,6 +19,33 @@ grpc::Status failResponse(ResponseT* response, const std::string& message) {
setCurrentTimestamp(response->mutable_header()->mutable_timestamp());
return grpc::Status::OK;
}
AudioStreamFormat fromProtoAudioFormat(cmvr::api::AudioData_AudioFormat format) {
switch (format) {
case cmvr::api::AudioData_AudioFormat_PCM:
return AudioStreamFormat::PCM;
case cmvr::api::AudioData_AudioFormat_MP3:
return AudioStreamFormat::MP3;
case cmvr::api::AudioData_AudioFormat_AAC:
return AudioStreamFormat::AAC;
case cmvr::api::AudioData_AudioFormat_WAV:
return AudioStreamFormat::WAV;
default:
return AudioStreamFormat::UNKNOWN;
}
}
AudioStreamFrameData fromProtoAudioData(const cmvr::api::AudioData& audio) {
AudioStreamFrameData frame;
frame.data.assign(audio.data().begin(), audio.data().end());
frame.sample_rate = audio.sample_rate() > 0 ? audio.sample_rate() : 44100;
frame.channels = audio.channels() > 0 ? audio.channels() : 2;
frame.format = fromProtoAudioFormat(audio.format());
frame.codec = audio.codec();
frame.pts = audio.pts();
frame.nb_samples = audio.nb_samples();
return frame;
}
}
gRPCSpeakerServiceImpl::gRPCSpeakerServiceImpl(): dmgr_(DeviceManager::getInstance()) {}
@ -81,6 +109,48 @@ grpc::Status gRPCSpeakerServiceImpl::PlayAudio(grpc::ServerContext* context,
}
}
grpc::Status gRPCSpeakerServiceImpl::StreamAudio(grpc::ServerContext* context,
grpc::ServerReader<api::StreamSpeakerAudioCommand_Request>* reader,
api::StreamSpeakerAudioCommand_Feedback* response) {
try {
api::StreamSpeakerAudioCommand_Request request;
std::shared_ptr<AbstractSpeaker> dev;
std::string dev_id;
while (reader->Read(&request)) {
if (!dev) {
dev_id = request.header().device_id();
CMVR_LOG(INFO) << "[gRPCSpeakerServiceImpl] (StreamAudio): id=" << dev_id;
dev = dmgr_.getDevice<AbstractSpeaker>(dev_id);
if (!dev) {
return failResponse(response, "Speaker device not found: " + dev_id);
}
if (!dev->start()) {
return failResponse(response, "Failed to start speaker: " + dev_id);
}
}
if (!dev->pushAudioFrame(fromProtoAudioData(request.audio()))) {
dev->stopStreaming();
return failResponse(response, "Failed to push speaker audio frame: " + dev_id);
}
}
if (dev) {
dev->stopStreaming();
}
response->mutable_header()->set_success(true);
setCurrentTimestamp(response->mutable_header()->mutable_timestamp());
return grpc::Status::OK;
}
catch (const std::exception& e) {
response->mutable_header()->set_success(false);
response->mutable_header()->set_error_message(e.what());
setCurrentTimestamp(response->mutable_header()->mutable_timestamp());
return grpc::Status::OK;
}
}
grpc::Status gRPCSpeakerServiceImpl::StopPlayback(grpc::ServerContext* context,
const api::StopSpeakerCommand_Request* request, api::StopSpeakerCommand_Feedback* response) {
try {

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,403 @@
/** @file aubo_api.h
* @brief \~chinese 机器人及外部轴等控制API接口,如获取机器人列表、获取系统信息等等
* @brief \~english API for controlling the robot and external axis
*/
#ifndef AUBO_SDK_AUBO_API_INTERFACE_H
#define AUBO_SDK_AUBO_API_INTERFACE_H
#include <aubo/system_info.h>
#include <aubo/runtime_machine.h>
#include <aubo/register_control.h>
#include <aubo/robot_interface.h>
#include <aubo/global_config.h>
#include <aubo/math.h>
#include <aubo/socket.h>
#include <aubo/serial.h>
#include <aubo/axis_interface.h>
#include <aubo/gripper_interface.h>
namespace arcs {
namespace common_interface {
/**
* \chinese
* @defgroup AuboApi AuboApi (主入口)
* @ingroup AuboApi
* AuboApi
* \endchinese
*
* \english
* @defgroup AuboApi Main Entrance
* @ingroup AuboApi
* AuboApi
* \endenglish
*/
class ARCS_ABI_EXPORT AuboApi
{
public:
AuboApi();
virtual ~AuboApi();
/**
* @ingroup AuboApi
* @ref Math
* \chinese
* 获取纯数学相关接口
*
* @return MathPtr对象的指针
*
* @par Python函数原型
* getMath(self: pyaubo_sdk.AuboApi) -> pyaubo_sdk.Math
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* MathPtr ptr = rpc_cli->getMath();
* @endcode
* \endchinese
*
*\english
* Get pure mathematic related API
*
* @return Shared pointer to a Math object
*
* @par Python function prototype
* getMath(self: pyaubo_sdk.AuboApi) -> pyaubo_sdk.Math
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* MathPtr ptr = rpc_cli->getMath();
* @endcode
*\endenglish
*/
MathPtr getMath();
/**
* @ingroup AuboApi
* @ref SystemInfo
* \chinese
* 获取系统信息
*
* @return SystemInfoPtr对象的指针
*
* @par Python函数原型
* getSystemInfo(self: pyaubo_sdk.AuboApi) -> pyaubo_sdk.SystemInfo
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* SystemInfoPtr ptr = rpc_cli->getSystemInfo();
* @endcode
* \endchinese
*
* \english
* Get system info
*
* @return Shared pointer to SystemInfo object
*
* @par Python function prototype
* getSystemInfo(self: pyaubo_sdk.AuboApi) -> pyaubo_sdk.SystemInfo
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* SystemInfoPtr ptr = rpc_cli->getSystemInfo();
* @endcode
* \endenglish
*/
SystemInfoPtr getSystemInfo();
/**
* @ingroup AuboApi
* @ref RuntimeMachine
* \chinese
* 获取运行时接口
*
* @return RuntimeMachinePtr对象的指针
*
* @par Python函数原型
* getRuntimeMachine(self: pyaubo_sdk.AuboApi) -> pyaubo_sdk.RuntimeMachine
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* RuntimeMachinePtr ptr = rpc_cli->getRuntimeMachine();
* @endcode
* \endchinese
*
* \english
* Get runtime api
*
* @return Shared pointer to RuntimeMachine object
* Python function prototype
* getRuntimeMachine(self: pyaubo_sdk.AuboApi) -> pyaubo_sdk.RuntimeMachine
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* RuntimeMachinePtr ptr = rpc_cli->getRuntimeMachine();
* @endcode
* \endenglish
*/
RuntimeMachinePtr getRuntimeMachine();
/**
* @ingroup AuboApi
* @ref RegisterControl
* \chinese
* 对外寄存器接口
*
* @return RegisterControlPtr对象的指针
*
* @par Python函数原型
* getRegisterControl(self: pyaubo_sdk.AuboApi) ->
* pyaubo_sdk.RegisterControl
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* RegisterControlPtr ptr = rpc_cli->getRegisterControl();
* @endcode
* \endchinese
*
* \english
* External registers api
*
* @return Shared pointer to RegisterControl object
*
* @par Python function prototype
* getRegisterControl(self: pyaubo_sdk.AuboApi) ->
* pyaubo_sdk.RegisterControl
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* RegisterControlPtr ptr = rpc_cli->getRegisterControl();
* @endcode
* \endenglish
*/
RegisterControlPtr getRegisterControl();
/**
* @ingroup AuboApi
* \chinese
* 获取机器人列表
*
* @return 机器人列表
*
* @par Python函数原型
* getRobotNames(self: pyaubo_sdk.AuboApi) -> List[str]
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* @endcode
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"getRobotNames","params":[],"id":1}
*
* @par JSON-RPC响应示例
* {"id":1,"jsonrpc":"2.0","result":["rob1"]}
* \endchinese
*
* \english
* Get robot list
*
* @return robot list
*
* @par Python function prototype
* getRobotNames(self: pyaubo_sdk.AuboApi) -> List[str]
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* @endcode
*
* @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"getRobotNames","params":[],"id":1}
*
* @par JSON-RPC response example
* {"id":1,"jsonrpc":"2.0","result":["rob1"]}
* \endenglish
*/
std::vector<std::string> getRobotNames();
/**
* @ingroup AuboApi
* @ref RobotInterface
* \chinese
* 根据名字获取 RobotInterfacePtr 接口
*
* @param name 机器人名字
* @return RobotInterfacePtr对象的指针
*
* @par Python函数原型
* getRobotInterface(self: pyaubo_sdk.AuboApi, arg0: str) ->
* pyaubo_sdk.RobotInterface
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* RobotInterfacePtr ptr = rpc_cli->getRobotInterface(robot_name);
* @endcode
* \endchinese
*
* \english
* Get RobotInterfacePtr based on name
*
* @param name Robot name
* @return Shared pointer to a RobotInterface object
*
* @par Python function prototype
* getRobotInterface(self: pyaubo_sdk.AuboApi, arg0: str) ->
* pyaubo_sdk.RobotInterface
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* RobotInterfacePtr ptr = rpc_cli->getRobotInterface(robot_name);
* @endcode
* \endenglish
*/
RobotInterfacePtr getRobotInterface(const std::string &name);
/**
* @ingroup AuboApi
* \~chinese 获取外部轴列表 \~english Get external axis list
*
* @return
*/
std::vector<std::string> getAxisNames();
/**
* @ingroup AuboApi
* @ref AxisInterface
* \chinese
* 获取外部轴接口
*
* @param name
* @return
* \endchinese
*
* \english
* Get external axis interface
*
* @param name
* @return
* \endenglish
*/
AxisInterfacePtr getAxisInterface(const std::string &name);
/**
* @ingroup AuboApi
* @ref Socket
* \chinese
* 获取 socket
* @return SocketPtr对象的指针
*
* @par Python函数原型
* getSocket(self: pyaubo_sdk.AuboApi) -> arcs::common_interface::Socket
* @endcode
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* SocketPtr ptr = rpc_cli->getSocket();
* @endcode
* \endchinese
*
* \english
* Get socket
* @return Shared pointer to a socket object
*
* @par Python function prototype
* getSocket(self: pyaubo_sdk.AuboApi) -> arcs::common_interface::Socket
* @endcode
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* SocketPtr ptr = rpc_cli->getSocket();
* @endcode
* \endenglish
*/
SocketPtr getSocket();
/**
* @ingroup AuboApi
* @ref Serial
* \chinese
* 获取Serial串口
* @return SerialPtr对象的指针
*
* @par Python函数原型
* getSerial(self: pyaubo_sdk.AuboApi) -> arcs::common_interface::Serial
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* SerialPtr ptr = rpc_cli->getSerial();
* @endcode
* \endchinese
*
* \english
* @return Shared pointer to Serial object
*
* @par Python function prototype
* getSerial(self: pyaubo_sdk.AuboApi) -> arcs::common_interface::Serial
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* SerialPtr ptr = rpc_cli->getSerial();
* @endcode
* \endenglish
*/
SerialPtr getSerial();
/**
* @ingroup AuboApi
* @ref SyncMove
* \~chinese 获取同步运动接口 \~english Get syncronous move interface
*
* \~chinese @return SyncMovePtr对象的指针
* \~english @return Shared pointer to SyncMove object
*/
SyncMovePtr getSyncMove(const std::string &name);
/**
* @ingroup AuboApi
* @ref Trace
* \~chinese 获取告警信息接口
* \~english Get alert interface
*
* \~chinese @return TracePtr对象的指针
* \~english @return Shared pointer of trace object
*/
TracePtr getTrace(const std::string &name);
/**
* @ingroup AuboApi
* @ref GripperInterface
* \~chinese 获取通用夹爪接口
* \~english Get gripper interface
*
* \~chinese @return GripperInterfacePtr对象的指针
* \~english @return Shared pointer of gripper object
*/
GripperInterfacePtr getGripperInterface();
protected:
void *d_{ nullptr };
};
using AuboApiPtr = std::shared_ptr<AuboApi>;
} // namespace common_interface
} // namespace arcs
#endif // AUBO_SDK_AUBO_API_H

View File

@ -0,0 +1,650 @@
/** @file axes.h
* @brief 外部轴接口
*/
#ifndef AUBO_SDK_AXIS_INTERFACE_H
#define AUBO_SDK_AXIS_INTERFACE_H
#include <aubo/sync_move.h>
#include <aubo/trace.h>
namespace arcs {
namespace common_interface {
/**
* \chinese
* @defgroup AxisInterface AxisInterface (外部轴)
* 外部轴API接口
* \endchinese
*
* \english
* @defgroup AxisInterface External Axis
* External axis API interface
* \endenglish
*/
class ARCS_ABI_EXPORT AxisInterface
{
public:
AxisInterface();
virtual ~AxisInterface();
/**
* @ingroup AxisInterface
* \~chinese 通电
* \~english Power on
* @return
*/
int poweronExtAxis();
/**
* @ingroup AxisInterface
* \~chinese 断电
* \~english Power off
* @return
*/
int poweroffExtAxis();
/**
* @ingroup AxisInterface
* \~chinese 使能
* \~english Enable
* @return
*/
int enableExtAxis();
/**
* @ingroup AxisInterface
* \~chinese 设置外部轴的安装位姿(相对于世界坐标系)
* \~chinese @param pose
* \~english Set mounting pose of external axis (wrt world frame)
* \~english @param pose
* @return
*/
int setExtAxisMountingPose(const std::vector<double> &pose);
/**
* @ingroup AxisInterface
* \chinese 运动到指定点, 旋转或者平移
*
* @param pos
* @param v
* @param a
* @param duration
* @return
* \endchinese
*
* \english move to pos, rotation or linear
*
* @param pos
* @param v
* @param a
* @param duration
* @return
* \endenglish
*/
int moveExtJoint(double pos, double v, double a, double duration);
/**
* @ingroup AxisInterface
* \chinese
* 制定目标运动速度
*
* @param v
* @param a
* @param duration
* @return
* \endchinese
*
* \english
* Set target speed, acceleration and duration
* @param v
* @param a
* @param duration
* @return
* \endenglish
*/
int speedExtJoint(double v, double a, double duration);
/**
* @ingroup AxisInterface
* \chinese
* 停止外部轴运动
*
* @param a
* @return
* \endchinese
*
* \english
* stop ext joint
* @param a
* @return
* \endenglish
*/
int stopExtJoint(double a);
/**
* @ingroup AxisInterface
* \~chinese 获取外部轴的类型 0代表是旋转 1代表平移
* \~english Get external axis type: 0 for rotation, 1 for linear
* @return
*/
int getExtAxisType();
/**
* @ingroup AxisInterface
* \~chinese 设置外部轴的类型
* @param type 0代表是旋转 1代表平移
*
* \~english Set external axis type
* @param type: 0 for rotation, 1 for linear
* @return
*/
int setExtAxisType(int type);
/**
* @ingroup AxisInterface
* \chinese
* 获取当前外部轴的状态
*
* @return 当前外部轴的状态
* \endchinese
*
* \english
* Get external axis status
*
* @return Current exteral axis status
* \endenglish
*/
AxisModeType getAxisModeType();
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴安装位姿
*
* @return 外部轴安装位姿
* \endchinese
*
* \english
* Get external axis mounting pose
*
* @return External axis pose
* \endenglish
*/
std::vector<double> getExtAxisMountingPose();
/**
* @ingroup AxisInterface
* \chinese
* 获取相对于安装坐标系的位姿,外部轴可能为变位机或者导轨
*
* @return 相对于安装坐标系的位姿
* \endchinese
*
* \english
* Get pose wrt mounting coordinate system, axis can be positioner or linear
* rail
*
* @return Pose wrt mounting coordinate system
* \endenglish
*/
std::vector<double> getExtAxisPose();
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴位置
*
* @return 外部轴位置
* \endchinese
*
* \english
* Get external axis position
*
* @return External axis position
* \endenglish
*/
double getExtAxisPosition();
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴运行速度
*
* @return 外部轴运行速度
* \endchinese
*
* \english
* Get external axis speed
*
* @return External axis speed
* \endenglish
*/
double getExtAxisVelocity();
/**
* @ingroup AxisInterface
* \chinese
* 设置外部轴运行速度
*
* @return
* \endchinese
*
* \english
* Set external axis speed
*
* @return
* \endenglish
*/
int setExtAxisVelocity(double velocity);
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴运行加速度
*
* @return 外部轴运行加速度
* \endchinese
*
* \english
* Get external axis acceleration
*
* @return External axis acceleration
* \endenglish
*/
double getExtAxisAcceleration();
/**
* @ingroup AxisInterface
* \chinese
* 设置外部轴运行加速度
*
* @param acc
* @return
* \endchinese
*
* \english
* Set external axis acceleration
*
* @param acc
* @return
* \endenglish
*/
int setExtAxisAcceleration(double acc);
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴电流
*
* @return 外部轴电流
* \endchinese
*
* \english
* Get external axis current
*
* @return External axis current
* \endenglish
*/
double getExtAxisCurrent();
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴温度
*
* @return 外部轴温度
* \endchinese
*
* \english
* Get external axis temperature
*
* @return External axis temperature
* \endenglish
*/
double getExtAxisTemperature();
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴电压
*
* @return 外部轴电压
* \endchinese
*
* \english
* Get external axis voltage
*
* @return External axis voltage
* \endenglish
*/
double getExtAxisBusVoltage();
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴电流
*
* @return 外部轴电流
* \endchinese
*
* \english
* Get external axis current
*
* @return external axis current
* \endenglish
*/
double getExtAxisBusCurrent();
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴最大位置(物理极限)
*
* @return 外部轴最大位置
* \endchinese
*
* \english
* Get external axis max position
*
* @return External axis max position
* \endenglish
*/
double getExtAxisMaxPosition();
/**
* @ingroup AxisInterface
* \chinese
* 设置外部轴最大位置(软件/算法限制)
*
* @param q
* @return
* \endchinese
*
* \english
* Set external axis max position
*
* @param q
* @return
* \endenglish
*/
int setExtAxisMaxPositionLimit(double q);
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴最大位置(软件/算法限制)
*
* @return 外部轴最大位置
* \endchinese
*
* \english
* Get external axis max position
*
* @return External axis max position
* \endenglish
*/
double getExtAxisMaxPositionLimit();
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴最小位置(物理极限)
*
* @return 外部轴最小位置
* \endchinese
*
* \english
* Get external axis min position
*
* @return External axis min position
* \endenglish
*/
double getExtMinPosition();
/**
* @ingroup AxisInterface
* \chinese
* 设置外部轴最小位置(软件/算法限制)
*
* @param q
* @return
* \endchinese
*
* \english
* Set external axis min position
*
* @param q
* @return
* \endenglish
*/
int setExtMinPositionLimit(double q);
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴最小位置(软件/算法限制)
*
* @return 外部轴最小位置
* \endchinese
*
* \english
* Get external axis min position
*
* @return External axis min position
* \endenglish
*/
double getExtMinPositionLimit();
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴最大速度(物理极限)
*
* @return 外部轴最大速度
* \endchinese
*
* \english
* Get external axis max speed
*
* @return External axis max speed
* \endenglish
*/
double getExtAxisMaxVelocity();
/**
* @ingroup AxisInterface
* \chinese
* 设置外部轴最大速度(软件/算法限制)
*
* @param v
* @return
* \endchinese
*
* \english
* Set external axis max speed
*
* @param v
* @return
* \endenglish
*/
int setExtAxisMaxVelocityLimit(double v);
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴最大速度(软件/算法限制)
*
* @return 外部轴最大速度
* \endchinese
*
* \english
* Get external axis max speed
*
* @return External axis max speed
* \endenglish
*/
double getExtAxisMaxVelocityLimit();
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴最大加速度(物理极限)
*
* @return 外部轴最大加速度
* \endchinese
*
* \english
* Get external axis max acceleration
*
* @return External axis max acceleration
* \endenglish
*/
double getExtAxisMaxAcceleration();
/**
* @ingroup AxisInterface
* \chinese
* 设置外部轴最大加速度(软件/算法限制)
*
* @param acc
* @return
* \endchinese
*
* \english
* Set external axis max acceleration
*
* @param acc
* @return
* \endenglish
*/
int setExtAxisMaxAccelerationLimit(double acc);
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴最大加速度(软件/算法限制)
*
* @return 外部轴最大加速度
* \endchinese
*
* \english
* Get external axis max acceleration
*
* @return External axis max acceleration
* \endenglish
*/
double getExtAxisMaxAccelerationLimit();
/**
* @ingroup AxisInterface
* \chinese
* 跟踪另一个外部轴的运动(禁止运动过程中使用)
*
* @param target_name 目标的外部轴名字
* @param phase 相位差
* @param err 跟踪运行的最大误差
* @return
* \endchinese
*
* \english
* Follow motion of another external axis (not to be used during motion)
*
* @param target_name name of target axis
* @param phase phase difference
* @param err max error when following motion
* @return
* \endenglish
*/
int followAnotherAxis(const std::string &target_name, double phase,
double err);
/**
* @ingroup AxisInterface
* \~chinese @brief stopFollowAnotherAxis(禁止运动过程中使用)
* \~english @brief stopFollowAnotherAxis(not to be used during motion)
* @return
*/
int stopFollowAnotherAxis();
/**
* @ingroup AxisInterface
* \~chinese @brief getErrorCode(获取外部轴错误码)
* \~english @brief getErrorCode(Get raw external axis error code)
* @return
*/
int getErrorCode();
/**
* @ingroup AxisInterface
* \chinese
* 重置外部轴错误
*
* @return
* \endchinese
*
* \english
* Reset axis error
*
* @return
* \endenglish
*/
int clearAxisError();
/**
* @ingroup AxisInterface
* \chinese
* 将当前外部轴位置设置为零位
*
* @return
* \endchinese
*
* \english
* Set current external axis position as zero.
*
* @return
* \endenglish
*/
int setExtAxisZero();
/**
* @ingroup AxisInterface
* \chinese
* 设置外部轴减速比
*
* @return
* \endchinese
*
* \english
* Set axis reduction ratio
*
* @return
* \endenglish
*/
int setReductionRatio(double ratio);
/**
* @ingroup AxisInterface
* \chinese
* 获取外部轴减速比
*
* @return
* \endchinese
*
* \english
* Get axis reduction ratio
*
* @return
* \endenglish
*/
double getReductionRatio();
protected:
void *d_;
};
using AxisInterfacePtr = std::shared_ptr<AxisInterface>;
} // namespace common_interface
} // namespace arcs
#endif // AUBO_SDK_AXIS_INTERFACE_H

View File

@ -0,0 +1,118 @@
/** @file error_stack.h
* @brief 汇总错误码
*/
#ifndef AUBO_SDK_ERROR_STACK_H
#define AUBO_SDK_ERROR_STACK_H
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <string>
#include <sstream>
#include <iomanip>
#include <aubo/global_config.h>
// 格式化占位符,默认是 fmt 的格式
#ifndef _PH1_
#define _PH1_ "{}"
#define _PH2_ "{}"
#define _PH3_ "{}"
#define _PH4_ "{}"
#endif
namespace arcs {
namespace error_stack {
constexpr int ARCS_ABI_EXPORT codeCompose(int aa, int bb, int cccc)
{
return (int)((aa * 1000000) + (bb * 10000) + cccc);
}
constexpr int ARCS_ABI_EXPORT mod(int x)
{
return (x % 1000000);
}
#include <aubo/error_stack/hal_error.h>
#include <aubo/error_stack/rtm_error.h>
#include <aubo/error_stack/system_error.h>
#define ARCS_ERROR_CODES \
SYSTEM_ERRORS \
JOINT_ERRORS \
JOINT_EX_ERRORS \
EXT_AXIS_ERRORS \
SAFETY_INTERFACE_BOARD_ERRORS \
RTM_ERRORS \
TOOL_ERRORS \
EX_TOOL_ERRORS \
PEDSTRAL_ERRORS \
EX_PEDSTRAL_ERRORS \
HARDWARE_INTERFACE_ERRORS \
_D(ARCS_MAX_ERROR_CODE, -1, "Max error code", "suggest...")
// 错误代码枚举
enum ErrorCodes
{
#define _D(n, v, s, r, ...) n = (int)v,
ARCS_ERROR_CODES
#undef _D
};
inline int str2ErrorCode(const char *err_code_name)
{
#define _D(n, v, s, r, ...) \
if (strcmp(#n, err_code_name) == 0) \
return v;
ARCS_ERROR_CODES
#undef _D
return ARCS_MAX_ERROR_CODE;
}
inline const char *errorCode2Str(int err_code)
{
static const char *errcode_str[] = {
#define _D(n, v, s, r, ...) s,
ARCS_ERROR_CODES
#undef _D
};
enum arcs_index
{
#define _D(n, v, s, r, ...) n##_INDEX,
ARCS_ERROR_CODES
#undef _D
};
int index = -1;
#define _D(n, v, s, r, ...) \
if (err_code == v) \
index = n##_INDEX;
ARCS_ERROR_CODES
#undef _D
if (index == -1) {
index = ARCS_MAX_ERROR_CODE_INDEX;
}
return errcode_str[(unsigned)index];
}
inline std::ostream &dump(std::ostream &os)
{
#define _D(n, v, s, r, ...) \
os << std::setw(20) << #n << "\t" << v << "\t" << s << "\t" << r \
<< std::endl;
ARCS_ERROR_CODES
#undef _D
return os;
}
} // namespace error_stack
} // namespace arcs
#endif // AUBO_SDK_ERROR_STACK_H

View File

@ -0,0 +1,350 @@
/** @file hal_error.h
* @brief 定义硬件抽象层的错误码
*/
#ifndef AUBO_SDK_HAL_ERROR_H
#define AUBO_SDK_HAL_ERROR_H
// 缩写说明
// JNT: joint
// PDL: pedstral
// TP: teach pendant
// COMM: communication
// ENC: encoder
// CURR: current
// POS: position
// PKG: package
// PROG: program
// clang-format off
#define JOINT_ERRORS \
_D(JOINT_ERR_OVER_CURRENET, 10001, "joint" _PH1_ " error: over current", "(a) Check for short circuit. (b) Do a Complete rebooting sequence. (c) If this happens more than two times in a row, replace joint") \
_D(JOINT_ERR_OVER_VOLTAGE, 10002, "joint" _PH1_ " error: over voltage", "(a) Do a Complete rebooting sequence. (b) Check 48 V Power supply, current distributer, energy eater and Control Board for issues") \
_D(JOINT_ERR_LOW_VOLTAGE, 10003, "joint" _PH1_ " error: low voltage", "(a) Do a Complete rebooting sequence. (b) Check for short circuit in robot arm. (c) Check 48 V Power supply, current distributer, energy eater and Control Board for issues") \
_D(JOINT_ERR_OVER_TEMP, 10004, "joint" _PH1_ " error: over temperature", "(a) Check robot’s environment and make sure the robot is operating within recommended limits. (b) Do a Complete rebooting sequence") \
_D(JOINT_ERR_HALL, 10005, "joint" _PH1_ " error: hall", "suggest...") \
_D(JOINT_ERR_ENCODER, 10006, "joint" _PH1_ " error: encoder", "Check encoder connections") \
_D(JOINT_ERR_ABS_ENCODER, 10007, "joint" _PH1_ " error: abs encoder", "suggest...") \
_D(JOINT_ERR_Q_CURRENT, 10008, "joint" _PH1_ " error: detect current", "suggest...") \
_D(JOINT_ERR_ENC_POLL, 10009, "joint" _PH1_ " error: encoder pollustion", "suggest...") \
_D(JOINT_ERR_ENC_Z_SIGNAL, 10010, "joint" _PH1_ " error: enocder z signal", "suggest...") \
_D(JOINT_ERR_ENC_CAL, 10011, "joint" _PH1_ " error: encoder calibrate", "suggest...") \
_D(JOINT_ERR_IMU_SENS, 10012, "joint" _PH1_ " error: IMU sensor", "suggest...") \
_D(JOINT_ERR_TEMP_SENS, 10013, "joint" _PH1_ " error: TEMP sensor", "suggest...") \
_D(JOINT_ERR_CAN_BUS, 10014, "joint" _PH1_ " error: CAN bus error", "suggest...") \
_D(JOINT_ERR_SYS_CUR, 10015, "joint" _PH1_ " error: system current error", "suggest...") \
_D(JOINT_ERR_SYS_POS, 10016, "joint" _PH1_ " error: system position error","suggest...") \
_D(JOINT_ERR_OVER_SP, 10017, "joint" _PH1_ " error: over speed","suggest...") \
_D(JOINT_ERR_OVER_ACC, 10018, "joint" _PH1_ " error: over accelerate", "suggest...") \
_D(JOINT_ERR_TRACE, 10019, "joint" _PH1_ " error: trace accuracy", "suggest...") \
_D(JOINT_ERR_TAG_POS_OVER, 10020, "joint" _PH1_ " error: target position out of range", "suggest...") \
_D(JOINT_ERR_TAG_SP_OVER, 10021, "joint" _PH1_ " error: target speed out of range", "suggest...") \
_D(JOINT_ERR_COLLISION, 10022, "joint" _PH1_ " error: collision", "suggest...") \
_D(JOINT_ERR_COMMON, 10023, "joint" _PH1_ " error: unkown error. Check communication with joint.", "suggest...") \
_D(JOINT_ERR_SWITCH_SERVO_MODE, 10024, "joint" _PH1_ " error: switch servo mode timeout.", "suggest...") \
_D(JOINT_ERR_MOTOR_STUCK, 10025, "joint" _PH1_ " error: motor stucked.", "suggest...") \
_D(JOINT_ERR_REDUCER_OVER_TEMP, 10026, "joint" _PH1_ " error: reducer over temperature", "(a) Check robot’s environment and make sure the robot is operating within recommended limits. (b) Do a Complete rebooting sequence") \
_D(JOINT_ERR_REDUCER_NTC, 10027, "joint" _PH1_ " error: reducer TEMP sensor failure", "(a) Check robot’s environment and make sure the robot is operating within recommended limits. (b) Do a Complete rebooting sequence") \
_D(JOINT_ERR_ABS_MULTITURN, 10028, "joint" _PH1_ " error: absolute encoder multiturn error", "(a) Check robot’s environment and make sure the robot is operating within recommended limits. (b) Do a Complete rebooting sequence") \
_D(JOINT_ERR_ADC_ZERO_OFFSET, 10029, "joint" _PH1_ " error: ADC zero offset failure", "(a) Check robot’s environment and make sure the robot is operating within recommended limits. (b) Do a Complete rebooting sequence") \
_D(JOINT_ERR_SHORT_CIRCUIT, 10030, "joint" _PH1_ " error: short circuit", "(a) Check robot’s environment and make sure the robot is operating within recommended limits. (b) Do a Complete rebooting sequence") \
_D(JOINT_ERR_PHASE_LOST, 10031, "joint" _PH1_ " error: motor phase lost", "(a) Check robot’s environment and make sure the robot is operating within recommended limits. (b) Do a Complete rebooting sequence") \
_D(JOINT_ERR_BRAKE, 10032, "joint" _PH1_ " error: brake failure", "(a) Check robot’s environment and make sure the robot is operating within recommended limits. (b) Do a Complete rebooting sequence") \
_D(JOINT_ERR_FIRMWARE_UPDATE, 10033, "joint" _PH1_ " error: firmware update failure", "(a) Check robot’s environment and make sure the robot is operating within recommended limits. (b) Do a Complete rebooting sequence") \
_D(JOINT_ERR_BATTERY_LOW, 10034, "joint" _PH1_ " error: battery low", "(a) Check robot’s environment and make sure the robot is operating within recommended limits. (b) Do a Complete rebooting sequence") \
_D(JOINT_ERR_PHASE_ALIGN, 10035, "joint" _PH1_ " error: phase align", "(a) Check robot’s environment and make sure the robot is operating within recommended limits. (b) Do a Complete rebooting sequence") \
_D(JOINT_ERR_CAN_HW_FAULT, 10036, "joint" _PH1_ " error: CAN bus hw fault", "(a) Check robot’s environment and make sure the robot is operating within recommended limits. (b) Do a Complete rebooting sequence") \
_D(JOINT_ERR_POS_DISCONTINUOUS, 10037, "joint" _PH1_ " error: target position discontinuous", "(a) Check robot’s environment and make sure the robot is operating within recommended limits. (b) Do a Complete rebooting sequence") \
_D(JOINT_ERR_POS_INIT, 10038, "joint" _PH1_ " error: position initiallization failure", "(a) Check robot’s environment and make sure the robot is operating within recommended limits. (b) Do a Complete rebooting sequence") \
_D(JOINT_ERR_TORQUE_SENSOR, 10039, "joint" _PH1_ " error: torqure sensor failure", "(a) Check robot’s environment and make sure the robot is operating within recommended limits. (b) Do a Complete rebooting sequence") \
_D(JOINT_ERR_OFFLINE, 10040, "joint" _PH1_ " error: joint may be offline", "(a) Check joint's hardware. (b) Check joint's id.") \
_D(JOINT_ERR_BOOTLOADER, 10041, "joint" _PH1_ " error: The joint is in bootloader mode. Retry firmware update. ", "suggest...") \
_D(JOINT_ERR_SLAVE_OFFLINE, 10042, "slave joint" _PH1_ " error: slave joint may be offline", "(a) Check slave joint's hardware. (b) Check slave joint's id.") \
_D(JOINT_ERR_SLAVE_BOOTLOADER, 10043, "slave joint" _PH1_ " error: The slave joint is in bootloader mode. Retry firmware update. ", "suggest...") \
_D(JOINT_ERR_ETHERCAT_BUS, 10044, "joint" _PH1_ " error: ETHERCAT bus error", "suggest...")
// Joint extended error codes (ex_err_code), starting from 10401
#define JOINT_EX_ERRORS \
_D(EX_JOINT_EC_SHORT_CIRCUIT, 10401, "joint" _PH1_ " error: short circuit protection", "suggest...", 0x2130) \
_D(EX_JOINT_EC_SHORT_CURRENT, 10402, "joint" _PH1_ " error: over current", "suggest...", 0x2310) \
_D(EX_JOINT_EC_PHASEA_CURRENT, 10403, "joint" _PH1_ " error: phase A over current", "suggest...", 0x2311) \
_D(EX_JOINT_EC_PHASEB_CURRENT, 10404, "joint" _PH1_ " error: phase B over current", "suggest...", 0x2312) \
_D(EX_JOINT_EC_PHASEC_CURRENT, 10405, "joint" _PH1_ " error: phase C over current", "suggest...", 0x2313) \
_D(EX_JOINT_EC_PHASE_CURRENT, 10406, "joint" _PH1_ " error: phase over current", "suggest...", 0x2314) \
_D(EX_JOINT_EC_MOTOR_PHASE_LOSE, 10407, "joint" _PH1_ " error: motor phase loss", "suggest...", 0x3331) \
_D(EX_JOINT_EC_BUS_OVER_VOLTAGE, 10408, "joint" _PH1_ " error: bus over voltage", "suggest...", 0x3210) \
_D(EX_JOINT_EC_BUS_LOW_VOLTAGE, 10409, "joint" _PH1_ " error: bus under voltage", "suggest...", 0x3220) \
_D(EX_JOINT_EC_OVERLOAD, 10410, "joint" _PH1_ " error: overload", "suggest...", 0x3230) \
_D(EX_JOINT_EC_IPM_OVER_TEMP, 10411, "joint" _PH1_ " error: IPM over temperature", "suggest...", 0x4310) \
_D(EX_JOINT_EC_REDUCER_OVER_TEMP, 10412, "joint" _PH1_ " error: reducer over temperature", "suggest...", 0x4311) \
_D(EX_JOINT_EC_ADC_ZERO_OFFSET, 10413, "joint" _PH1_ " error: ADC zero offset", "suggest...", 0x5101) \
_D(EX_JOINT_EC_REDUCER_NTC, 10414, "joint" _PH1_ " error: reducer NTC fault", "suggest...", 0x5102) \
_D(EX_JOINT_EC_IPM_NTC, 10415, "joint" _PH1_ " error: IPM NTC fault", "suggest...", 0x5103) \
_D(EX_JOINT_EC_TORQUE_SENSOR, 10416, "joint" _PH1_ " error: torque sensor fault", "suggest...", 0x5104) \
_D(EX_JOINT_EC_TORQUE_SENSOR_COMM, 10417, "joint" _PH1_ " error: torque sensor communication fault", "suggest...", 0x5105) \
_D(EX_JOINT_EC_MOTOR_ABS_ENC_COMM, 10418, "joint" _PH1_ " error: motor-side absolute encoder communication fault", "suggest...", 0x5201) \
_D(EX_JOINT_EC_REDUCER_ABS_ENC_COMM, 10419, "joint" _PH1_ " error: reducer-side absolute encoder communication fault", "suggest...", 0x5202) \
_D(EX_JOINT_EC_REDUCER_ABS_ENC_DATA, 10420, "joint" _PH1_ " error: reducer-side absolute encoder data channel disabled warning", "suggest...", 0x5203) \
_D(EX_JOINT_EC_REDUCER_ABS_ENC_CMD, 10421, "joint" _PH1_ " error: reducer-side absolute encoder command invalid warning", "suggest...", 0x5204) \
_D(EX_JOINT_EC_REDUCER_ABS_ENC_ERR, 10422, "joint" _PH1_ " error: reducer-side absolute encoder fault", "suggest...", 0x5205) \
_D(EX_JOINT_EC_REDUCER_ABS_ENC_WARNING, 10423, "joint" _PH1_ " error: reducer-side absolute encoder warning", "suggest...", 0x5206) \
_D(EX_JOINT_EC_BRAKE, 10424, "joint" _PH1_ " error: brake fault", "suggest...", 0x5301) \
_D(EX_JOINT_EC_COMM_HWL, 10425, "joint" _PH1_ " error: communication hardware layer error", "suggest...", 0x5302) \
_D(EX_JOINT_EC_FIRMWARE_UPDATE, 10426, "joint" _PH1_ " error: firmware update failed", "suggest...", 0x6100) \
_D(EX_JOINT_EC_FLASH_OP, 10427, "joint" _PH1_ " error: flash operation failed", "suggest...", 0x6101) \
_D(EX_JOINT_EC_MU_SAVE, 10428, "joint" _PH1_ " error: multi-turn data error", "suggest...", 0x6102) \
_D(EX_JOINT_EC_DEMADATA_LOST, 10429, "joint" _PH1_ " error: calibration zero point data lost", "suggest...", 0x6103) \
_D(EX_JOINT_EC_PARAMETER, 10430, "joint" _PH1_ " error: parameter error", "suggest...", 0x6200) \
_D(EX_JOINT_EC_UVW_LOGIC, 10431, "joint" _PH1_ " error: hall signal fault", "suggest...", 0x7001) \
_D(EX_JOINT_EC_UVW_ABZ, 10432, "joint" _PH1_ " error: incremental encoder fault", "suggest...", 0x7002) \
_D(EX_JOINT_EC_ENC_Z_LOST, 10433, "joint" _PH1_ " error: encoder Z signal lost", "suggest...", 0x7305) \
_D(EX_JOINT_EC_ENC_POLLUTE, 10434, "joint" _PH1_ " error: encoder pollution", "suggest...", 0x7004) \
_D(EX_JOINT_EC_ENC_CALI, 10435, "joint" _PH1_ " error: encoder calibration failed", "suggest...", 0x7005) \
_D(EX_JOINT_EC_MT_ABS_DATA, 10436, "joint" _PH1_ " error: multi-turn absolute data error", "suggest...", 0x7006) \
_D(EX_JOINT_EC_ENC_TYPE_INFO, 10437, "joint" _PH1_ " error: encoder type identified and saved", "suggest...", 0x7007) \
_D(EX_JOINT_EC_ENC_TYPE_ERROR, 10438, "joint" _PH1_ " error: encoder type error", "suggest...", 0x7008) \
_D(EX_JOINT_EC_ENC_VERIFY, 10439, "joint" _PH1_ " error: encoder verification failed", "suggest...", 0x7009) \
_D(EX_JOINT_EC_DUAL_ENC_ERROR, 10440, "joint" _PH1_ " error: dual encoder deviation too large", "suggest...", 0x700A) \
_D(EX_JOINT_EC_DUAL_ENC_EANGLE, 10441, "joint" _PH1_ " error: dual encoder electrical angle deviation too large", "suggest...", 0x700B) \
_D(EX_JOINT_EC_OBJECT_DICT_ERROR, 10442, "joint" _PH1_ " error: object dictionary data error", "suggest...", 0x700C) \
_D(EX_JOINT_EC_MOTOR_STALL, 10443, "joint" _PH1_ " error: motor stall protection", "suggest...", 0x7121) \
_D(EX_JOINT_EC_ABS_ENC_LOW_VOLT, 10444, "joint" _PH1_ " error: absolute encoder low voltage", "suggest...", 0x7385) \
_D(EX_JOINT_EC_MT_BATTERY_LOW, 10445, "joint" _PH1_ " error: multi-turn battery low voltage", "suggest...", 0x7386) \
_D(EX_JOINT_EC_POS_CMD, 10446, "joint" _PH1_ " error: position command discontinuous", "suggest...", 0x8001) \
_D(EX_JOINT_EC_POS_OVER_LIMIT, 10447, "joint" _PH1_ " error: position over limit", "suggest...", 0x8002) \
_D(EX_JOINT_EC_COMM_PROTO, 10448, "joint" _PH1_ " error: communication protocol layer error", "suggest...", 0x8003) \
_D(EX_JOINT_EC_GRAVITY_PARA_WARNING, 10449, "joint" _PH1_ " error: gravity compensation parameter invalid", "suggest...", 0x8004) \
_D(EX_JOINT_EC_GRAVITY_COMPENSATE_ERROR, 10450, "joint" _PH1_ " error: gravity compensation value sudden change", "suggest...", 0x8005) \
_D(EX_JOINT_EC_LOW_RIGIDITY, 10451, "joint" _PH1_ " error: collision soft float abnormal", "suggest...", 0x8006) \
_D(EX_JOINT_EC_POS_CMD_WARNING, 10452, "joint" _PH1_ " error: position command unchanged during motion", "suggest...", 0x8007) \
_D(EX_JOINT_EC_SLAVE_COMM, 10453, "joint" _PH1_ " error: master-slave MCU communication fault", "suggest...", 0x8008) \
_D(EX_JOINT_EC_POS_ERR, 10454, "joint" _PH1_ " error: position following error too large", "suggest...", 0x8009) \
_D(EX_JOINT_EC_DUAL_POS_ERR, 10455, "joint" _PH1_ " error: dual servo position sync error too large", "suggest...", 0x800A) \
_D(EX_JOINT_EC_DUAL_COMM_ERR, 10456, "joint" _PH1_ " error: dual servo communication", "suggest...", 0x800B) \
_D(EX_JOINT_EC_CAN_BUSOFF, 10457, "joint" _PH1_ " error: CAN bus-off warning", "suggest...", 0x800C) \
_D(EX_JOINT_EC_SYNC_SNAKE, 10458, "joint" _PH1_ " error: sync frame jitter warning", "suggest...", 0x800D) \
_D(EX_JOINT_EC_SYNC_DISCON, 10459, "joint" _PH1_ " error: sync frame discontinuous warning", "suggest...", 0x800E) \
_D(EX_JOINT_EC_RPDO_LOST, 10460, "joint" _PH1_ " error: RPDO lost warning", "suggest...", 0x800F) \
_D(EX_JOINT_EC_RPDO_MANY, 10461, "joint" _PH1_ " error: multiple RPDO in one sync cycle warning", "suggest...", 0x8010) \
_D(EX_JOINT_EC_GRAVITY_LOST, 10462, "joint" _PH1_ " error: gravity compensation value lost", "suggest...", 0x8011) \
_D(EX_JOINT_EC_MAININT_TIME_WARN, 10463, "joint" _PH1_ " error: servo main interrupt runtime warning", "suggest...", 0x8012) \
_D(EX_JOINT_EC_MAININT_TIME_ERROR, 10464, "joint" _PH1_ " error: servo main interrupt runtime error", "suggest...", 0x8013) \
_D(EX_JOINT_EC_SPDINT_TIME_WARN, 10465, "joint" _PH1_ " error: servo speed loop interrupt runtime warning", "suggest...", 0x8014) \
_D(EX_JOINT_EC_SPDINT_TIME_ERROR, 10466, "joint" _PH1_ " error: servo speed loop interrupt runtime error", "suggest...", 0x8015) \
_D(EX_JOINT_EC_POS_CMD_JUMP_WARNING, 10467, "joint" _PH1_ " error: position command sudden jump during motion", "suggest...", 0x8016) \
_D(EX_JOINT_EC_SYNC_TIMCOARSE, 10468, "joint" _PH1_ " error: clock sync compensation status", "suggest...", 0x8017) \
_D(EX_JOINT_EC_ENC_Z_CNTS_ERR, 10469, "joint" _PH1_ " error: incremental encoder Z signal or CNTS abnormal", "suggest...", 0x8018) \
_D(EX_JOINT_EC_SLAVE_OVER_CURRENT, 10470, "joint" _PH1_ " error: slave MCU detected motor phase current over safe threshold", "suggest...", 0x8019) \
_D(EX_JOINT_EC_SLAVE_OVER_VOLTAGE, 10471, "joint" _PH1_ " error: slave MCU detected DC bus voltage over upper threshold", "suggest...", 0x801A) \
_D(EX_JOINT_EC_SLAVE_UNDER_VOLTAGE, 10472, "joint" _PH1_ " error: slave MCU detected DC bus voltage below lower threshold", "suggest...", 0x801B) \
_D(EX_JOINT_EC_SLAVE_POS_ERR, 10473, "joint" _PH1_ " error: slave MCU detected master-slave motor position deviation out of range", "suggest...", 0x801C) \
_D(EX_JOINT_EC_SLAVE_SPEED_ERR, 10474, "joint" _PH1_ " error: slave MCU detected master-slave motor speed deviation out of range", "suggest...", 0x801D) \
_D(EX_JOINT_EC_SLAVE_TRACE_ERR, 10475, "joint" _PH1_ " error: slave MCU detected position following error out of control range", "suggest...", 0x801E) \
_D(EX_JOINT_EC_SLAVE_ABS_ERR, 10476, "joint" _PH1_ " error: slave MCU detected absolute encoder feedback abnormal", "suggest...", 0x801F) \
_D(EX_JOINT_EC_SLAVE_ADC_ZERO_OFFSET, 10477, "joint" _PH1_ " error: slave MCU detected current sampling zero offset out of calibration range", "suggest...", 0x8020) \
_D(EX_JOINT_EC_SLAVE_ENC_POLLUTE, 10478, "joint" _PH1_ " error: slave MCU detected encoder signal quality degradation", "suggest...", 0x8021) \
_D(EX_JOINT_EC_SLAVE_ENC_Z_LOST, 10479, "joint" _PH1_ " error: slave MCU detected encoder Z reference signal lost", "suggest...", 0x8022) \
_D(EX_JOINT_EC_SLAVE_COMM_OVER_TM, 10480, "joint" _PH1_ " error: slave MCU detected master-slave communication timeout", "suggest...", 0x8023) \
_D(EX_JOINT_EC_SLAVE_TRQ_ERR, 10481, "joint" _PH1_ " error: slave MCU detected master-slave motor torque deviation out of range", "suggest...", 0x8024) \
_D(EX_JOINT_EC_BRAKE_TYPE_ERR, 10482, "joint" _PH1_ " error: brake type config mismatch with hardware", "suggest...", 0x8025) \
_D(EX_JOINT_EC_PHASE_ALIGN, 10483, "joint" _PH1_ " error: phase alignment failed", "suggest...", 0xFF02) \
_D(EX_JOINT_EC_POS_OVER_LIMIT_WARNING, 10484, "joint" _PH1_ " error: position over limit", "suggest...", 0x8026) \
_D(EX_JOINT_EC_PHASE_ALIGN_WARNING, 10485, "joint" _PH1_ " error: phase alignment warning", "suggest...", 0xFF03) \
_D(EX_JOINT_EC_TASK_STACK_SHORTAGE, 10486, "joint" _PH1_ " error: task stack insufficient", "suggest...", 0xFF04) \
_D(EX_JOINT_EC_TASK_STACK_OVERFLOW, 10487, "joint" _PH1_ " error: task stack overflow", "suggest...", 0xFF05) \
_D(EX_JOINT_EC_SERVO_STEP, 10488, "joint" _PH1_ " servo process step", "For information only. No action required.", 0x6105) \
_D(EX_JOINT_EC_UNKNOWN, 10800, "joint" _PH1_ " error: unknown error", "suggest...", 0xFFFF)
#define EXT_AXIS_ERRORS \
_D(EXT_AXIS_ERR_COMMON, 11001, "ext axis" _PH1_ " error: common", "Check communication with ext axis drive.") \
_D(EXT_AXIS_ERR_OVER_CURRENT, 11002, "ext axis" _PH1_ " error: over current", "Check wiring/short circuit; reboot; if repeated replace drive/motor.") \
_D(EXT_AXIS_ERR_OVER_VOLTAGE, 11003, "ext axis" _PH1_ " error: over voltage", "Check DC supply, regen, energy eater; reboot.") \
_D(EXT_AXIS_ERR_LOW_VOLTAGE, 11004, "ext axis" _PH1_ " error: low voltage", "Check DC supply and cabling; reboot.") \
_D(EXT_AXIS_ERR_OVER_TEMP, 11005, "ext axis" _PH1_ " error: over temperature", "Check environment/cooling; reboot.") \
_D(EXT_AXIS_ERR_HALL, 11006, "ext axis" _PH1_ " error: hall fault", "Check hall sensor and motor cabling.") \
_D(EXT_AXIS_ERR_ENCODER, 11007, "ext axis" _PH1_ " error: encoder fault", "Check encoder connection/cable/noise.") \
_D(EXT_AXIS_ERR_ABS_ENCODER, 11008, "ext axis" _PH1_ " error: absolute encoder fault", "Check abs encoder power/cable; reboot.") \
_D(EXT_AXIS_ERR_CUR_CALIB, 11009, "ext axis" _PH1_ " error: current calibration fault", "Reboot; check current sensing circuit.") \
_D(EXT_AXIS_ERR_Q_CURRENT, 11010, "ext axis" _PH1_ " error: current detect fault", "Reboot; check current sensing circuit.") \
_D(EXT_AXIS_ERR_ENC_POLL, 11011, "ext axis" _PH1_ " error: encoder pollution", "Check encoder contamination/noise; improve shielding.") \
_D(EXT_AXIS_ERR_ENC_Z_SIGNAL, 11012, "ext axis" _PH1_ " error: encoder Z signal fault", "Check encoder Z channel and wiring.") \
_D(EXT_AXIS_ERR_ENC_CAL, 11013, "ext axis" _PH1_ " error: encoder calibrate invalid", "Redo calibration; check encoder.") \
_D(EXT_AXIS_ERR_IMU, 11014, "ext axis" _PH1_ " error: IMU fault", "Check IMU sensor and connection.") \
_D(EXT_AXIS_ERR_TEMP_SENSOR, 11015, "ext axis" _PH1_ " error: temperature sensor fault", "Check temp sensor wiring; reboot.") \
_D(EXT_AXIS_ERR_ECAT_BUS, 11016, "ext axis" _PH1_ " error: EtherCAT bus error", "Check EtherCAT cabling/topology/sync; reboot master/drive.") \
_D(EXT_AXIS_ERR_ECAT_CONFIG, 11017, "ext axis" _PH1_ " error: EtherCAT config/ESI/SM/PDO fault", "Check ESI, Mailbox/SM/PDO mapping, vendor/product/revision match.") \
_D(EXT_AXIS_ERR_ECAT_SYNC, 11018, "ext axis" _PH1_ " error: EtherCAT sync/frame/period fault", "Check DC sync, cycle time, frame loss; verify NIC/IRQ affinity.") \
_D(EXT_AXIS_ERR_SYS_CUR, 11019, "ext axis" _PH1_ " error: system current fault", "Check current loop and load; reboot.") \
_D(EXT_AXIS_ERR_SYS_POS, 11020, "ext axis" _PH1_ " error: position out of range", "Check encoder/scale/limits; reboot.") \
_D(EXT_AXIS_ERR_OVER_SPEED, 11021, "ext axis" _PH1_ " error: over speed", "Check command limits and tuning parameters.") \
_D(EXT_AXIS_ERR_OVER_ACC, 11022, "ext axis" _PH1_ " error: over acceleration", "Reduce acceleration/jerk; check tuning.") \
_D(EXT_AXIS_ERR_FOLLOW_ERROR, 11023, "ext axis" _PH1_ " error: following error", "Check gains, load, saturation; verify feedback.") \
_D(EXT_AXIS_ERR_TAG_POS_OVER, 11024, "ext axis" _PH1_ " error: target position out of range", "Check target limits and homing.") \
_D(EXT_AXIS_ERR_TAG_SPEED_OVER, 11025, "ext axis" _PH1_ " error: target speed out of range", "Clamp speed; check profile settings.") \
_D(EXT_AXIS_ERR_TAG_CURRENT_OVER, 11026, "ext axis" _PH1_ " error: target current out of range", "Clamp current/torque; check load.") \
_D(EXT_AXIS_ERR_COLLISION, 11027, "ext axis" _PH1_ " error: collision", "Remove obstruction; check torque/force limits.") \
_D(EXT_AXIS_ERR_ADC_ZERO_OFFSET, 11028, "ext axis" _PH1_ " error: ADC zero offset", "Reboot; check ADC/current sensor offset.") \
_D(EXT_AXIS_ERR_IPM_NTC, 11029, "ext axis" _PH1_ " error: IPM NTC fault", "Check power module temperature sensing.") \
_D(EXT_AXIS_ERR_SHORT_CIRCUIT, 11030, "ext axis" _PH1_ " error: short circuit", "Check motor phase wiring; insulation test.") \
_D(EXT_AXIS_ERR_MOTOR_STALL, 11031, "ext axis" _PH1_ " error: motor stall", "Check mechanical jam/load; reduce accel; reboot.") \
_D(EXT_AXIS_ERR_ABS_MULTITURN, 11032, "ext axis" _PH1_ " error: abs encoder multiturn fault", "Check abs encoder battery/params; reboot.") \
_D(EXT_AXIS_ERR_PHASE_LOST, 11033, "ext axis" _PH1_ " error: motor phase lost", "Check phase wiring/connector; measure continuity.") \
_D(EXT_AXIS_ERR_BRAKE, 11034, "ext axis" _PH1_ " error: brake fault", "Check brake wiring/power; verify brake release.") \
_D(EXT_AXIS_ERR_REDUCER_OVER_TEMP, 11035, "ext axis" _PH1_ " error: reducer over temperature", "Check reducer temperature/cooling.") \
_D(EXT_AXIS_ERR_REDUCER_NTC, 11036, "ext axis" _PH1_ " error: reducer NTC fault", "Check reducer temperature sensor.") \
_D(EXT_AXIS_ERR_FIRMWARE_UPDATE, 11037, "ext axis" _PH1_ " error: firmware update fault", "Retry update; check power stability.") \
_D(EXT_AXIS_ERR_FLASH_OP, 11038, "ext axis" _PH1_ " error: flash operation fault", "Retry; if persistent replace drive.") \
_D(EXT_AXIS_ERR_EXT_ABS_ENC, 11039, "ext axis" _PH1_ " error: motor-side abs encoder comm fault", "Check external abs encoder link/power.") \
_D(EXT_AXIS_ERR_DRIVE_FAULT, 11040, "ext axis" _PH1_ " error: drive fault", "Check drive alarm code; reboot; replace if repeated.") \
_D(EXT_AXIS_ERR_OVERLOAD, 11041, "ext axis" _PH1_ " error: overload", "Reduce load; check mechanics and tuning.") \
_D(EXT_AXIS_ERR_HARDWARE_LIMIT, 11042, "ext axis" _PH1_ " error: hardware limit triggered", "Move away from limit; check limit switch.") \
_D(EXT_AXIS_ERR_SERVO_MODE_TIMEOUT, 11043, "ext axis" _PH1_ " error: switch servo mode timeout", "Check mode transition and comm; reboot.") \
_D(EXT_AXIS_ERR_UVW_ABZ, 11044, "ext axis" _PH1_ " error: UVW/ABZ fault", "Check phase/encoder signals wiring.") \
_D(EXT_AXIS_ERR_BATTERY_LOW, 11045, "ext axis" _PH1_ " error: battery low", "Replace encoder battery; reboot.") \
_D(EXT_AXIS_ERR_PHASE_ALIGN, 11046, "ext axis" _PH1_ " error: phase align fail", "Redo phase alignment; check motor params.") \
_D(EXT_AXIS_ERR_POS_DISCONTINUOUS, 11047, "ext axis" _PH1_ " error: position command discontinuous", "Check trajectory generation and limits.") \
_D(EXT_AXIS_ERR_POS_INIT, 11048, "ext axis" _PH1_ " error: position initialization failure", "Check encoder init/homing procedure.") \
_D(EXT_AXIS_ERR_TORQUE_SENSOR, 11049, "ext axis" _PH1_ " error: torque sensor fault", "Check torque sensor wiring/calibration.") \
_D(EXT_AXIS_ERR_ABS_ENC_LOW_VOLT, 11050, "ext axis" _PH1_ " error: abs encoder low voltage", "Check encoder supply voltage and cable.") \
_D(EXT_AXIS_ERR_OFFLINE, 11051, "ext axis" _PH1_ " error: ext axis offline", "Check hardware and axis id; check EtherCAT state.") \
_D(EXT_AXIS_ERR_BOOTLOADER, 11052, "ext axis" _PH1_ " error: ext axis in bootloader", "Retry firmware update.") \
_D(EXT_AXIS_ERR_SLAVE_OFFLINE, 11053, "ext axis slave" _PH1_ " error: slave offline", "Check slave hardware and id.") \
_D(EXT_AXIS_ERR_SLAVE_BOOTLOADER, 11054, "ext axis slave" _PH1_ " error: slave in bootloader", "Retry firmware update.") \
_D(EXT_AXIS_ERR_EEPROM, 11055, "ext axis" _PH1_ " error: EEPROM/param store fault", "Check EEPROM/parameter storage; power cycle.") \
_D(EXT_AXIS_ERR_PARAM_CONFIG, 11056, "ext axis" _PH1_ " error: parameter/config fault", "Verify parameter set; restore defaults if needed.") \
_D(EXT_AXIS_ERR_STO, 11057, "ext axis" _PH1_ " error: STO safety fault", "Check STO wiring/safety chain; reset safety.") \
_D(EXT_AXIS_ERR_ENCRYPT_CHIP, 11058, "ext axis" _PH1_ " error: encrypt chip/key fault", "Check encryption chip/keys/firmware compatibility.") \
_D(EXT_AXIS_ERR_BRAKE_RES_OVERLOAD, 11059, "ext axis" _PH1_ " error: brake resistor overload", "Check brake resistor/regen circuit; duty cycle.") \
_D(EXT_AXIS_ERR_POWER_LINE_OPEN, 11060, "ext axis" _PH1_ " error: motor power line open", "Check motor power cable continuity/connector.") \
_D(EXT_AXIS_ERR_HOMING, 11061, "ext axis" _PH1_ " error: homing fault", "Check homing sensor/origin procedure; retry.") \
_D(EXT_AXIS_ERR_TUNING_FAIL, 11062, "ext axis" _PH1_ " error: tuning fail", "Redo tuning; reduce resonance; check mechanics.") \
_D(EXT_AXIS_ERR_INERTIA_ID_FAIL, 11063, "ext axis" _PH1_ " error: inertia identification fail", "Check load; redo inertia ID; adjust conditions.") \
_D(EXT_AXIS_ERR_FLYAWAY, 11064, "ext axis" _PH1_ " error: flyaway", "Emergency stop; check feedback polarity/scale; inspect drive params.") \
_D(EXT_AXIS_ERR_SPEED_PULSE_OVER, 11065, "ext axis" _PH1_ " error: feedback pulse overspeed", "Check encoder feedback and scaling; reduce speed.") \
_D(EXT_AXIS_ERR_CTRL_LOOP, 11066, "ext axis" _PH1_ " error: control loop/timeout fault", "Check sampling/current loop/comm timeout; reboot.")
#define TOOL_ERRORS \
_D(TOOL_FLASH_VERIFY_FAILED, 40001, "Flash write verify failed", "suggest...") \
_D(TOOL_PROGRAM_CRC_FAILED, 40002, "Program flash checksum failed during bootloading", "suggest...") \
_D(TOOL_PROGRAM_CRC_FAILED2, 40003, "Program flash checksum failed at runtime", "suggest...") \
_D(TOOL_ID_UNDIFINED, 40004, "Tool ID is undefined", "suggest...") \
_D(TOOL_ILLEGAL_BL_CMD, 40005, "Illegal bootloader command", "suggest...") \
_D(TOOL_FW_WRONG, 40006, "Wrong firmware at the joint", "suggest...") \
_D(TOOL_HW_INVALID, 40007, "Invalid hardware revision", "suggest...") \
_D(TOOL_SHORT_CURCUIT_H, 40011, "Short circuit detected on Digital Output: " _PH1_ " high side", "suggest...") \
_D(TOOL_SHORT_CURCUIT_L, 40012, "Short circuit detected on Digital Output: " _PH1_ " low side", "suggest...") \
_D(TOOL_AVERAGE_CURR_HIGH, 40013, "10 second Average tool IO Current of " _PH1_ " A is outside of the allowed range.", "suggest...") \
_D(TOOL_POWER_PIN_OVER_CURR, 40014, "Current of " _PH1_ " A on the POWER pin is outside of the allowed range.", "suggest...") \
_D(TOOL_DOUT_PIN_OVER_CURR, 40015, "Current of " _PH1_ " A on the Digital Output pins is outside of the allowed range.", "suggest...") \
_D(TOOL_GROUND_PIN_OVER_CURR, 40016, "Current of " _PH1_ " A on the ground pin is outside of the allowed range.", "suggest...") \
_D(TOOL_RX_FRAMING, 40021, "RX framing error", "suggest...") \
_D(TOOL_RX_PARITY, 40022, "RX Parity error", "suggest...") \
_D(TOOL_48V_LOW, 40031, "48V input is too low", "suggest...") \
_D(TOOL_48V_HIGH, 40032, "48V input is too high", "suggest...") \
_D(TOOL_ERR_OFFLINE, 40033, "tool error: tool may be offline", "(a) Check tool's hardware. (b) Check joint's id.") \
_D(TOOL_ERR_BOOTLOADER, 40034, "tool error: The tool is in bootloader mode. Retry firmware update. ", "suggest...")
#define EX_TOOL_ERRORS \
_D(EX_TOOL_EC_LOW_VOLTAGE, 40101, "tool error: low voltage", "check tool power supply voltage") \
_D(EX_TOOL_EC_FORCESENSOR_COMM, 40102, "tool error: external force sensor communication error", "check external force sensor communication connection") \
_D(EX_TOOL_485_SENDFULL_COMM, 40103, "tool error: 485 transparent transmission buffer full", "check 485 communication load and transmission frequency") \
_D(EX_TOOL_FORCESENSOR_FILTER_ZERO, 40104, "tool error: force sensor filter parameter is zero", "check force sensor filter parameter configuration") \
_D(EX_TOOL_EC_UNKNOWN, 40200, "tool error: unknown error", "check controller logs and hardware status")
#define PEDSTRAL_ERRORS \
_D(PKG_LOST, 50001, "Lost package from pedestal", "suggest...") \
_D(PEDSTRAL_OFFLINE, 50002, "pedestal error: pedestal may be offline", "(a) Check pedestal's hardware. (b) Check pedestal's id.") \
_D(PEDESTAL_ERR_BOOTLOADER, 50003, "pedestal error: The pedestal is in bootloader mode. Retry firmware update. ", "suggest...")
#define EX_PEDSTRAL_ERRORS \
_D(EX_BASE_EC_LOW_VOLTAGE, 50101, "pedstral error: low voltage", "check power supply voltage and battery status") \
_D(EX_BASE_EC_OVER_TEMPERATURE_RES, 50102, "pedstral error: resistor over temperature", "check braking resistor temperature and cooling condition") \
_D(EX_BASE_EC_OVER_TARGET_BRAKE_OPEN_VOLT, 50103, "pedstral error: input voltage close to or exceeds regenerative brake activation voltage", "check input power voltage and regenerative braking configuration") \
_D(EX_BASE_EC_RES_BREAKAGE, 50104, "pedstral error: brake resistor breakage", "check whether the brake resistor is disconnected or damaged") \
_D(EX_BASE_EC_IMU_CALIBRATE, 50105, "pedstral error: IMU calibration required", "perform IMU calibration according to maintenance procedure") \
_D(EX_BASE_EC_TEMP_SENSOR_SHORT, 50106, "pedstral error: temperature sensor short circuit", "check temperature sensor wiring and solder joints for short circuit") \
_D(EX_BASE_EC_TEMP_SENSOR_BREAK, 50107, "pedstral error: temperature sensor open circuit", "check temperature sensor connection and cable continuity") \
_D(EX_BASE_EC_96V_INSTANT_OVER_VOLTAGE, 50108, "pedstral error: 96V instantaneous over voltage after regenerative braking", "check regenerative braking behavior and power bus voltage") \
_D(EX_BASE_EC_UNKNOWN, 50200, "pedstral error: unknown error", "check controller logs and hardware status")
#define SAFETY_INTERFACE_BOARD_ERRORS \
_D(IFB_ERR_ROBOTTYPE, 20001, "Robot error type!", "suggest...") \
_D(IFB_ERR_ADXL_SENS, 20002, "Base Acceleration sensor error!", "suggest...") \
_D(IFB_ERR_EN_LINE, 20003, "Encoder line error!", "suggest...") \
_D(IFB_ERR_ENTER_HDG_MODE, 20004, "Robot enter handguide mode!", "suggest...") \
_D(IFB_ERR_EXIT_HDG_MODE, 20005, "Robot exit handguide mode!", "suggest...") \
_D(IFB_ERR_MAC_DATA_BREAK, 20006, "MAC data break!", "suggest...") \
_D(IFB_ERR_DRV_FIRMWARE_VERSION, 20007, "Motor driver firmware version error!", "suggest...") \
_D(INIT_ERR_EN_DRV, 20008, "Motor driver enable failed!", "suggest...") \
_D(INIT_ERR_EN_AUTO_BACK, 20009, "Motor driver enable auto back failed!", "suggest...") \
_D(INIT_ERR_EN_CUR_LOOP, 20010, "Motor driver enable current loop failed!", "suggest...") \
_D(INIT_ERR_SET_TAG_CUR, 20011, "Motor driver set target current failed!", "suggest...") \
_D(INIT_ERR_RELEASE_BRAKE, 20012, "Motor driver release brake failed!", "suggest...") \
_D(INIT_ERR_EN_POS_LOOP, 20013, "Motor driver enable postion loop failed!", "suggest...") \
_D(INIT_ERR_SET_MAX_ACC, 20014, "Motor set max accelerate failed!", "suggest...") \
_D(SAFETY_ERR_PROTECTION_STOP_TIMEOUT, 20015, "Protective stop timeout!", "suggest...") \
_D(SAFETY_ERR_REDUCED_MODE_TIMEOUT, 20016, "Reduced mode timeout!", "suggest...") \
_D(SYS_ERR_MCU_COM, 20017, "Robot system error: mcu communication error!", "suggest...") \
_D(SYS_ERR_RS485_COM, 20018, "Robot system error: RS485 communication error!", "suggest...") \
_D(IFB_ERR_DISCONNECTED, 20019, "Interface board may be disconnected. Please check connection between IPC and Interface board.", "suggest...")\
_D(IFB_ERR_PAYLOAD_ERROR, 20020, "Payload error.", "suggest...") \
_D(IFB_OFFLINE, 20021, "ifaceboard error: ifaceboard may be offline", "(a) Check ifaceboard's hardware. (b) Check ifaceboard's id.") \
_D(IFB_ERR_BOOTLOADER, 20022, "ifaceboard error: The ifaceboard is in bootloader mode. Retry firmware update. ", "suggest...") \
_D(IFB_SLAVE_OFFLINE, 20023, "interface slave board error: interface slave board may be offline", "(a) Check interface slave board's hardware. (b) Check interface slave board's id.") \
_D(IFB_SLAVE_ERR_BOOTLOADER, 20024, "interface slave board error: The interface slave board is in bootloader mode. Retry firmware update. ", "suggest...") \
_D(IFB_TOOL_ERR_ADXL_SENS, 20025, "Tool Acceleration sensor error!", "suggest...") \
_D(HANDLE_OFFLINE, 20026, "handle error: handle may be offline", "(a) Check handle's hardware. (b) Check handle's id.") \
_D(HANDLE_ERR_BOOTLOADER, 20027, "handle error: The handle is in bootloader mode. Retry firmware update. ", "suggest...") \
_D(IFB_POWERLOSS_OFFLINE, 20028, "interface powerloss board error: interface powerloss board may be offline", "(a) Check interface powerloss board's hardware. (b) Check interface powerloss board's id.") \
_D(IFB_POWERLOSS_ERR_BOOTLOADER, 20029, "interface powerloss board error: The interface powerloss board is in bootloader mode. Retry firmware update. ", "suggest...") \
_D(HANDLE_COMM_ERROR, 20030, "handle error: handle comm error", "(a) Check handle's hardware. (b) Check handle's id.")
#define HARDWARE_INTERFACE_ERRORS \
_D(HW_SCB_SETUP_FAILED, 60001, "Setup of Interface Board failed", "suggest...") \
_D(HW_PKG_CNT_DISAGEE, 60002, "Packet counter disagreements", "suggest...") \
_D(HW_SCB_DISCONNECT, 60003, "Connection to Interface Board lost", "suggest...") \
_D(HW_SCB_PKG_LOST, 60004, "Package lost from Interface Board", "suggest...") \
_D(HW_SCB_CONN_INIT_FAILED, 60005, "Ethernet connection initialization with Interface Board failed", "suggest...") \
_D(HW_LOST_JOINT_PKG, 60006, "Lost package from joint " _PH1_ "", "suggest...") \
_D(HW_LOST_TOOL_PKG, 60007, "Lost package from tool", "suggest...") \
_D(HW_JOINT_PKG_CNT_DISAGREE, 60008, "Packet counter disagreement in packet from joint " _PH1_ "", "suggest...") \
_D(HW_TOOL_PKG_CNT_DISAGREE, 60009, "Packet counter disagreement in packet from tool", "suggest...") \
_D(HW_JOINTS_FAULT, 60011, "" _PH1_ " joint entered the Fault State", "suggest...") \
_D(HW_JOINTS_VIOLATION, 60012, "" _PH1_ " joint entered the Violation State", "suggest...") \
_D(HW_TP_FAULT, 60013, "Teach Pendant entered the Fault State", "suggest...") \
_D(HW_TP_VIOLATION, 60014, "Teach Pendant entered the Violation State", "suggest...") \
_D(HW_JOINT_MV_TOO_FAR, 60021, "" _PH1_ " joint moved too far before robot entered RUNNING State", "suggest...") \
_D(HW_JOINT_STOP_NOT_FAST, 60022, "Joint Not stopping fast enough", "suggest...") \
_D(HW_JOINT_MV_LIMIT, 60023, "Joint moved more than allowable limit", "suggest...") \
_D(HW_FT_SENSOR_DATA_INVALID, 60024, "Force-Torque Sensor data invalid", "suggest...") \
_D(HW_NO_FT_SENSOR, 60025, "Force-Torque sensor is expected, but it cannot be detected", "suggest...") \
_D(HW_FT_SENSOR_NOT_CALIB, 60026, "Force-Torque sensor is detected but not calibrated", "suggest...") \
_D(HW_RELEASE_BRAKE_FAILED, 60030, "Robot was not able to brake release, see log for details", "suggest...") \
_D(HW_OVERCURR_SHUTDOWN, 60040, "Overcurrent shutdown", "suggest...") \
_D(HW_ENERGEY_SURPLUS, 60050, "Energy surplus shutdown", "suggest...") \
_D(HW_IDLE_POWER_HIGH, 60060, "Idle power consumption to high", "suggest...") \
_D(HW_ENTER_COLLISION_TIMEOUT, 60071, "Enter collision stop procedure timeout", "suggest...") \
_D(HW_POWERON_TIMEOUT, 60072, "Poweron robot timeout", "suggest...") \
_D(HW_NO_NIC_FOUND, 60073, "No network cards found.", "suggest...") \
_D(HW_IFB_NOT_FOUND, 60074, "No Interface Board found.", "suggest...") \
_D(HW_IFB_BOOTLOAD, 60075, "The Interface Board is in bootloader mode. Update firmware firstly.", "suggest...") \
_D(HW_TOOL_NOT_FOUND, 60076, "No Tool Board found.", "suggest...") \
_D(HW_BASE_NOT_FOUND, 60077, "No Base Board found.", "suggest...") \
_D(HW_BRINGUP_TIMEOUT, 60078, "Poweron robot timeout", "suggest...") \
_D(HW_COLLISION_RECOVERY_FAILED, 60079, "Collision recovery failed", "suggest...") \
_D(HW_TP_ENABLED, 60080, "Teach pendant enabled status changed to " _PH1_, "suggest...")
// clang-format on
// 定义硬件抽象层的错误代码
#define HAL_ERRORS \
JOINT_ERRORS \
JOINT_EX_ERRORS \
EXT_AXIS_ERRORS \
SAFETY_INTERFACE_BOARD_ERRORS \
TOOL_ERRORS \
EX_TOOL_ERRORS \
PEDSTRAL_ERRORS \
EX_PEDSTRAL_ERRORS \
HARDWARE_INTERFACE_ERRORS
#endif // AUBO_SDK_JOINT_ERROR_H

View File

@ -0,0 +1,187 @@
/** @file rtm_error.h
* @brief 运行时错误码
*/
#ifndef AUBO_SDK_RTM_ERROR_H
#define AUBO_SDK_RTM_ERROR_H
// clang-format off
#define RTM_ERRORS \
_D(ROBOT_BE_PULLING, 30001, "Something is pulling the robot.","Please check TCP configuration,payload and mounting settings") \
_D(PSTOP_ELBOW_POS, 30002, "Protective Stop: Elbow position close to safety plane limits.","Please move robot Elbow joint away from the safety plane") \
_D(PSTOP_STOP_TIME, 30003, "Protective Stop: Exceeding user safety settings for stopping time.","(a) Check speeds and accelerations in the program (b) Check usage of TCP,payload and CoG correctly (c) Check external equipmentactivation if correctly set") \
_D(PSTOP_STOP_DISTANCE, 30004, "Protective Stop: Exceeding user safety settings for stopping distance.","(a) Check speeds and accelerations in the program (b) Check usage of TCP,payload and CoG correctly (c) Check external equipmentactivation if correctly set") \
_D(PSTOP_CLAMP, 30005, "Protective Stop: Danger of clamping between the Robot’s lower arm and tool.","(a) Check speeds and accelerations in the program (b) Check usage of TCP,payload and CoG correctly (c) Check external equipmentactivation if correctly set") \
_D(PSTOP_POS_LIMIT, 30006, "Protective Stop: Position close to joint limits", "suggest...") \
_D(PSTOP_ORI_LIMIT, 30007, "Protective Stop: Tool orientation close to limits", "suggest...") \
_D(PSTOP_PLANE_LIMIT, 30008, "Protective Stop: Position close to safety plane limits", "suggest...") \
_D(PSTOP_POS_DEVIATE, 30009, "Protective Stop: Position deviates from path", "Check payload, center of gravity and acceleration settings.") \
_D(JOINT_CHK_PAYLOAD, 30010, "Joint " _PH1_ ": Check payload, center of gravity and acceleration settings. Log screen may contain additional information.", "suggest...") \
_D(PSTOP_SINGULARITY, 30011, "Protective Stop: Position in singularity.","Please use MoveJ or change the motion") \
_D(PSTOP_CANNOT_MAINTAIN, 30012, "Protective Stop: Robot cannot maintain its position, check if payload is correct", "suggest...") \
_D(PSTOP_WRONG_PAYLOAD, 30013, "Protective Stop: Wrong payload or mounting detected, or something is pushing the robot when entering Freedrive mode","Verify that the TCP configuration and mounting in the used installation is correct") \
_D(PSTOP_JOINT_COLLISION, 30014, "Protective Stop: Collision detected by joint " _PH1_, "Make sure no objects are in the path of the robot and resume the program") \
_D(PSTOP_POS_DISAGREE, 30015, "Protective stop: The robot was powered off last time due to a joint position disagreement."," (a) Verify that the robot position in the 3D graphics matches the real robot, to ensure that the encoders function before releasing the brakes. Stand back and monitor the robot performing its first program cycle as expected. (b) If the position is not correct, the robot must be repaired. In this case, click Power Off Robot. (c) If the position is correct, please tick the check box below the 3D graphics and click Robot Position Verified") \
_D(TARGET_JOINT_SPEED_EXCEED, 30016, "Target joint speed exceed limits", "suggest...") \
_D(TARGET_POS_SUDDEN_CHG, 30017, "Sudden change in target position", "suggest...") \
_D(SUDDEN_STOP, 30018, "Sudden stop."," To abort a motion, use \"stopj\" or \"stopl\" script commands to generate a smooth deceleration before using \"wait\". Avoid aborting motions between waypoints with blend”") \
_D(ROBOT_STOP_ABNORMAL, 30019, "Robot has not stopped in the allowed reaction and braking time", "suggest...") \
_D(PROG_INVALID_SETP, 30020, "Robot program resulted in invalid setpoint.", "Please review waypoints in the program") \
_D(BLEND_INVALID_SETP, 30021, "Blending failed and resulted in an invalid setpoint.", "Try changing the blend radius or contact technical support") \
_D(APPROACH_SINGULARITY, 30022, "Robot approaching singularity – Acceleration threshold failed.","Review waypoints in the program, try using MoveJ instead of MoveL in the position close to singularity") \
_D(TSPEED_UNMATCH_POS, 30023, "Target speed does not match target position", "suggest...") \
_D(INCONSIS_TPOS_SPD, 30024, "Inconsistency between target position and speed", "suggest...") \
_D(JOINT_TSPD_UNMATCH_POS, 30025, "Target joint speed does not match target joint position change – Joint " _PH1_ "", "suggest...") \
_D(FIELDBUS_INPUT_DISCONN, 30026, "Fieldbus input disconnected.","Please check fieldbus connections (RTDE, ModBus, EtherNet/IP and Profinet) or disable the fieldbus in the installation. Check RTDE watchdog feature. Check if a URCap is using this feature.") \
_D(OPMODE_CHANGED, 30027, "Operational mode changed: " _PH1_ "", "suggest...") \
_D(NO_KIN_CALIB, 30028, "No Kinematic Calibration found (calibration.conf file is either corrupt or missing).","A new kinematics calibration may be needed if the robot needs to improve its kinematics, otherwise, ignore this message)") \
_D(KIN_CALIB_UNMATCH_JOINT, 30029, "Kinematic Calibration for the robot does not match the joint(s).", "If moving a program from a different robot to this one, rekinematic calibrate the second robot to improve kinematics, otherwise ignore this message.") \
_D(KIN_CALIB_UNMATCH_ROBOT, 30030, "Kinematic Calibration does not match the robot.","Please check if the serial number of the robot arm matches the Control Box") \
_D(JOINT_OFFSET_CHANGED, 30031, "Large movement of the robot detected while it was powered off. The joints were moved while it was powered off, or the encoders do not function", "suggest...") \
_D(OFFSET_CHANGE_HIGH, 30032, "Change in offset is too high", "suggest...") \
_D(JOINT_SPEED_LIMIT, 30033, "Close to joint speed safety limit.", "Review program speed and acceleration") \
_D(TOOL_SPEED_LIMIT, 30034, "Close to tool speed safety limit.", "Review program speed and acceleration") \
_D(MOMENTUM_LIMIT, 30035, "Close to momentum safety limit.", "Review program speed and acceleration") \
_D(ROBOT_MV_STOP, 30036, "Robot is moving when in Stop Mode", "suggest...") \
_D(HAND_PROTECTION, 30037, "Hand protection: Tool is too close to the lower arm: " _PH1_ " meter.","(a) Check wrist position. (b) Verify mounting (c) Do a Complete rebooting sequence (d) Update software (e) Contact your local AUBO Robots service provider for assistance") \
_D(WRONG_SAFETYMODE, 30038, "Wrong safety mode: " _PH1_, "suggest...") \
_D(SAFETYMODE_CHANGED, 30039, "Safety mode changed: " _PH1_, "suggest...") \
_D(JOINT_ACC_LIMIT, 30040, "Close to joint acceleration safety limit", "suggest...") \
_D(TOOL_ACC_LIMIT, 30041, "Close to tool acceleration safety limit", "suggest...") \
_D(JOINT_TEMPERATURE_LIMIT, 30042, "Joint " _PH1_ " temperature too high(>" _PH2_ "℃)", "suggest...") \
_D(CONTROL_BOX_TEMPERATURE_LIMIT, 30043, "Control box temperature too high(>" _PH1_ "℃)", "suggest...") \
_D(ROBOT_EMERGENCY_STOP, 30044, "Robot emergency stop", "suggest...") \
_D(ROBOTMODE_CHANGED, 30045, "Robot mode changed: " _PH1_, "suggest...") \
_D(ROBOTMODE_ERROR, 30046, "Wrong robot mode: " _PH1_, "suggest...") \
_D(POSE_OUT_OF_REACH, 30047, "Target pose [" _PH1_ "] out of reach", "suggest...") \
_D(TP_PLAN_FAILED, 30048, "Trajectory plan FAILED." , "suggest...") \
_D(START_FORCE_FAILED, 30049, "Start force control failed, because force sensor does not exist." , "suggest...") \
_D(OVER_SAFE_PLANE_LIMIT,30050, _PH1_ " axis exceeds the safety plane limit (Move_type:" _PH2_ " id:" _PH3_ ").","Please move the robot to the safety plane range.") \
_D(POWERON_FAIL_VIOLATION,30051, "Failed to power on because the robot safety mode is in violation", "suggest...") \
_D(POWERON_FAIL_SYSTEMEMERGENCYSTOP, 30052, "Failed to power on because the robot safety mode is in system emergency stop", "suggest...") \
_D(POWERON_FAIL_ROBOTEMERGENCYSTOP, 30053, "Failed to power on because the robot safety mode is in robot emergency stop", "Pop up the red emergency stop button on the teach pendant when the robot is in a safe range of motion") \
_D(POWERON_FAIL_FAULT, 30054, "Failed to power on because the robot safety mode is in fault", "suggest...") \
_D(STARTUP_FAIL_VIOLATION, 30055, "Failed to startup because the robot safety mode is in violation", "suggest...") \
_D(STARTUP_FAIL_SYSTEMEMERGENCYSTOP, 30056, "Failed to startup because the robot safety mode is in system emergency stop", "suggest...") \
_D(STARTUP_FAIL_ROBOTEMERGENCYSTOP, 30057, "Failed to startup because the robot safety mode is in robot emergency stop", "Pop up the red emergency stop button on the teach pendant when the robot is in a safe range of motion") \
_D(STARTUP_FAIL_FAULT, 30058, "Failed to startup because the robot safety mode is in fault", "suggest...") \
_D(BACKDRIVE_FAIL_VIOLATION, 30059, "Failed to backdrive because the robot safety mode is in violation", "suggest...") \
_D(BACKDRIVE_FAIL_SYSTEMEMERGENCYSTOP, 30060, "Failed to backdrive because the robot safety mode is in system emergency stop", "suggest...") \
_D(BACKDRIVE_FAIL_ROBOTEMERGENCYSTOP, 30061, "Failed to backdrive because the robot safety mode is in robot emergency stop", "Pop up the red emergency stop button on the teach pendant when the robot is in a safe range of motion") \
_D(BACKDRIVE_FAIL_FAULT, 30062, "Failed to backdrive because the robot safety mode is in fault", "suggest...") \
_D(SETSIM_FAIL_VIOLATION, 30063, "Switch sim mode failed because the robot safety mode is in violation", "suggest...") \
_D(SETSIM_FAIL_SYSTEMEMERGENCYSTOP, 30064, "Switch sim mode failed because the robot safety mode is in system emergency stop", "suggest...") \
_D(SETSIM_FAIL_ROBOTEMERGENCYSTOP, 30065, "Switch sim mode failed because the robot safety mode is in robot emergency stop", "Pop up the red emergency stop button on the teach pendant when the robot is in a safe range of motion") \
_D(SETSIM_FAIL_FAULT, 30066, "Switch sim mode failed because the robot safety mode is in fault", "suggest...") \
_D(FREEDRIVE_FAIL_VIOLATION, 30067, "Enable handguide mode failed because the robot safety mode is in violation", "suggest...") \
_D(FREEDRIVE_FAIL_SYSTEMEMERGENCYSTOP, 30068, "Enable handguide mode failed because the robot safety mode is in system emergency stop", "suggest...") \
_D(FREEDRIVE_FAIL_ROBOTEMERGENCYSTOP, 30069, "Enable handguide mode failed because the robot safety mode is in robot emergency stop", "Pop up the red emergency stop button on the teach pendant when the robot is in a safe range of motion") \
_D(FREEDRIVE_FAIL_FAULT, 30070, "Enable handguide mode failed because the robot safety mode is in fault", "suggest...") \
_D(UPFIRMWARE_FAIL_VIOLATION, 30071, "Firmware update failed because the robot safety mode is in violation", "suggest...") \
_D(UPFIRMWARE_FAIL_SYSTEMEMERGENCYSTOP, 30072, "Firmware update failed because the robot safety mode is in system emergency stop", "suggest...") \
_D(UPFIRMWARE_FAIL_ROBOTEMERGENCYSTOP, 30073, "Firmware update failed because the robot safety mode is in robot emergency stop", "Pop up the red emergency stop button on the teach pendant when the robot is in a safe range of motion") \
_D(UPFIRMWARE_FAIL_FAULT, 30074, "Firmware update failed because the robot safety mode is in fault", "suggest...") \
_D(SETPERSOSTENT_FAIL_VIOLATION, 30075, "Set persistent parameter failed because the robot safety mode is in violation", "suggest...") \
_D(SETPERSOSTENT_FAIL_SYSTEMEMERGENCYSTOP, 30076, "Set persistent parameter failed because the robot safety mode is in system emergency stop", "suggest...") \
_D(SETPERSOSTENT_FAIL_ROBOTEMERGENCYSTOP, 30077, "Set persistent parameter failed because the robot safety mode is in robot emergency stop", "Pop up the red emergency stop button on the teach pendant when the robot is in a safe range of motion") \
_D(SETPERSOSTENT_FAIL_FAULT, 30078, "Set persistent parameter failed because the robot safety mode is in fault", "suggest...") \
_D(SETPERSOSTENT_FAIL_PARAM_ERR, 30079, "Set persistent parameter failed", "(a) Check the parameter format, whether all are floating point numbers") \
_D(ROBOT_CABLE_DISCONN, 30080, "Robot cable not connected", "(a) Make sure the cable between Control Box and Robot Arm is correctly connected and it has no damage. (b) Check for loose connections (c) Do a Complete rebooting sequence (d) Update software (e) Contact your local AUBO Robots service provider for assistance Contact your local AUBO Robots service provider for assistance.") \
_D(TP_TOO_SHORT, 30081, "The generated trajectory is ignored because it is too short", "(a) Please check if the added waypoints are coincident (b) If it is an arc movement, please check whether the three points are collinear") \
_D(INV_KIN_FAIL, 30082, "Inverse kinematics solution failed. The target pose may be in a singular position or exceed the joint limits", "(a) Change the target pose and try moving again") \
_D(FREEDRIVE_ENABLED, 30083, "Freedrive status changed to " _PH1_ "", "suggest...") \
_D(TP_INV_FAIL_REFERENCE_JOINT_OUT_OF_LIMIT, 30084, "Inverse kinematics solution failed. Reference angle [" _PH1_ "] exceeds joint limit [" _PH2_ "].", "suggest...") \
_D(TP_INV_FAIL_NO_SOLUTION, 30085, "Inverse kinematics solution failed. The reference angle [" _PH1_ "] and the target angle [" _PH2_ "] are used as parameters. there is no solution in the calculation of the inverse solution process.", "suggest...")\
_D(SERVO_FAIL_VIOLATION, 30086, "Switch servo mode failed because the robot safety mode is in violation", "suggest...") \
_D(SERVO_FAIL_SYSTEMEMERGENCYSTOP, 30087, "Switch servo mode failed because the robot safety mode is in system emergency stop", "suggest...") \
_D(SERVO_FAIL_ROBOTEMERGENCYSTOP, 30088, "Switch servo mode failed because the robot safety mode is in robot emergency stop", "Pop up the red emergency stop button on the teach pendant when the robot is in a safe range of motion") \
_D(SERVO_FAIL_FAULT, 30089, "Switch servo mode failed because the robot safety mode is in fault", "suggest...") \
_D(FREEDRIVE_FAIL_NO_RUNNING, 30090, "Enable handguide mode failed because the robot mode type is " _PH1_ "(not running)", "suggest...") \
_D(RUNTIME_MACHINE_ERROR, 30091, "The state of the running machine is " _PH1_ ", not " _PH2_ ". " _PH3_ " function execution failed because the state is wrong." , "suggest...") \
_D(RESUME_FAR_PAUSE_PT, 30092, "Cannot resume from joint position [" _PH1_ "].\\nToo far away from paused point [" _PH2_ "]." , "suggest...") \
_D(PAYLOAD_LIGHTER_ERROR, 30093, "The payload setting is too small!" , "suggest...") \
_D(PAYLOAD_OVERLOAD_ERROR, 30094, "The payload setting is too large!" , "suggest...") \
_D(PAUSE_FAIL_NOT_POSITION_PLAN_MODE, 30095, "This motion does not support the pause function. The motion is stopping." , "suggest...") \
_D(TP_PLAN_FAILED_CIRCULAR_WAYPOINTS_COINCIDE, 30096, "The planning failed because the three waypoints of the arc were determined to coincide." , "Check the circular waypoints to make sure they are different.") \
_D(SERVO_WRONG_SAFETYMODE, 30097, "Switch servo mode failed because the robot safety mode is in " _PH1_ "." , "Check the circular waypoints to make sure they are different.") \
_D(SET_PERSTPARAM_WRONG_SAFETYMODE, 30098, "Set persistent parameter failed because the robot safety mode is in " _PH1_ , "suggest...") \
_D(SET_KINPARAM_WRONG_SAFETYMODE, 30099, "Set Kinematics Compensate parameters failed because the robot safety mode is in " _PH1_ , "suggest...") \
_D(SET_ROBOT_ZERO_WRONG_SAFETYMODE, 30100, "Set current joint angles to zero failed because the robot safety mode is in " _PH1_ , "suggest...") \
_D(UPFIRMWARE_WRONG_SAFETYMODE, 30101, "Firmware update failed because the robot safety mode is in " _PH1_, "suggest...") \
_D(POWERON_WRONG_SAFETYMODE, 30102, "Failed to power on because the robot safety mode is in " _PH1_, "suggest...") \
_D(STARTUP_WRONG_SAFETYMODE, 30103, "Failed to startup because the robot safety mode is in " _PH1_, "suggest...") \
_D(BACKDRIVE_WRONG_SAFETYMODE, 30104, "Failed to backdrive because the robot safety mode is in system emergency stop", "suggest...") \
_D(SETSIM_WRONG_SAFETYMODE, 30105, "Switch sim mode failed because the robot safety mode is in violation", "suggest...") \
_D(FREEDRIVE_WRONG_SAFETYMODE, 30106, "Enable handguide mode failed because the robot safety mode is in wrong safety mode: " _PH1_, "suggest...") \
_D(TP_PLAN_FAILED_JOINT_JUMP_BIGGER, 30107, "Inverse kinematics solution failed. The target point and the current point are in different robot configuration spaces.", "Add a few more points between the target point and the current point.") \
_D(RUN_PROGRAM_FAILED, 30108, "Run program " _PH1_ " failed.", "suggset...") \
_D(FREEDRIVE_FAIL_WRONG_RTMSTATE, 30109, "Unable to enter the HandGuide mode as the robot is not currently in a stopped or paused state.", "suggset...") \
_D(SAFEGUARDSTOP_CONFIGURABLE_INPUT, 30110, "Configurable safety input is triggered.", "suggset...") \
_D(SAFEGUARDSTOP_3PE, 30111, "3PE is triggered.", "suggset...") \
_D(SAFEGUARDSTOP_SI, 30112, "SI0/SI1 is triggered.", "suggset...") \
_D(ROBOT_TYPE_CHANGED, 30200, "Robot type changed to '" _PH1_ "', and robot subtype changed to '" _PH2_ "'", "suggest...") \
_D(LINKMODE_CHANGED, 30201, "Link mode changed to " _PH1_ "", "suggest...") \
_D(ROBOT_SELF_COLLISION, 30301, "Detect risk of robot self collision", "suggest...") \
_D(CONSTANT_INVALID, 30302, "Joint torque constants are invalid. HandGuide will be disabled, and the collision protection may be triggered by mistake.", "suggest...") \
_D(GRAVITY_INVALID, 30303, "Abnormal value of gravity acceleration sensor. HandGuide will be disabled, and the collision protection may be triggered by mistake.", "suggest...") \
_D(DYNAMICS_INVALID, 30304, "Robot dynamics parameters are invalid. HandGuide will be disabled, and the collision protection may be triggered by mistake.", "suggest...") \
_D(FRICTION_INVALID, 30305, "Joint friction parameters are invalid. HandGuide will be disabled, and the collision protection may be triggered by mistake.", "suggest...") \
_D(HANDGUIDE_UNDER_DEVELOP, 30306, "Robot type of " _PH1_ " function under development. HandGuide will be disabled, and the collision protection may be triggered by mistake.", "suggest...") \
_D(SLOW_DOWN_INFO, 30307, "Slow down level changed to " _PH1_ "(" _PH2_ "%)", "suggest...") \
_D(WRONG_JOINT_DESIGNED_LIMIT, 30308, "Joint designed ranges exceeds ranges read from hardware interface.", "suggest...") \
_D(FREEDRIVE_IN_SIMULATION, 30309, "Enable handguide mode failed because the robot is in simulation mode.", "suggest...") \
_D(ROBOT_STOPPING_TIMEOUT, 30310, "Robot stopping timeout.", "suggest...") \
_D(PSTOP_INCORRECT_FORCE_OFFSET, 30311, "Protective Stop: Sudden change in force control target position. Force sensor offset may be incorrect or force sensor fault.", "suggest...") \
_D(WRONG_JOINT_SAFETY_LIMIT, 30312, "Joint safety ranges exceeds designed ranges.", "suggest...") \
_D(PSTOP_TCP_PLANE_VIOLATION, 30401, "Protective Stop: TCP position close to safety plane limits.", "suggest...") \
_D(PSTOP_ELBOW_PLANE_VIOLATION, 30402, "Protective Stop: elbow position close to safety plane limits.", "suggest...") \
_D(PSTOP_JOINT_TORQUE_VIOLATION, 30403, "Protective Stop: joint" _PH1_ " exceeds torque limit.", "suggest...") \
_D(PSTOP_JOINT_POSITION_VIOLATION, 30404, "Protective Stop: joint" _PH1_ " exceeds position limit.", "suggest...") \
_D(PSTOP_JOINT_SPEED_VIOLATION, 30405, "Protective Stop: joint" _PH1_ " exceeds speed limit.", "suggest...") \
_D(PSTOP_TCP_SPEED_VIOLATION, 30406, "Protective Stop: TCP speed close to safety limits.", "suggest...") \
_D(PSTOP_ELBOW_SPEED_VIOLATION, 30407, "Protective Stop: elbow speed close to safety limits.", "suggest...") \
_D(PSTOP_TCP_FORCE_VIOLATION, 30408, "Protective Stop: TCP foece close to safety limits.", "suggest...") \
_D(PSTOP_ELBOW_TORQUE_VIOLATION, 30409, "Protective Stop: elbow torque close to safety limits.", "suggest...") \
_D(PSTOP_POWER_VIOLATION, 30410, "Protective Stop: robot power close to safety limits.", "suggest...") \
_D(PSTOP_MOMENTUM_VIOLATION, 30411, "Protective Stop: robot momentum close to safety limits.", "suggest...") \
_D(PSTOP_TCP_CUBE_VIOLATION, 30412, "Protective Stop: TCP position close to safety cube.", "suggest...") \
_D(PSTOP_ELBOW_CUBE_VIOLATION, 30413, "Protective Stop: TCP position close to safety cube.", "suggest...") \
_D(REDUCE_ELBOW_PLANE_TRIGGER, 30414, "Reduce mode: elbow close to safety plane triggers reduction mode.", "suggest...") \
_D(REDUCE_TCP_PLANE_TRIGGER, 30415, "Reduce mode: TCP close to safety plane triggers reduction mode.", "suggest...") \
_D(PSTOP_MOVE_OUT_RANGE, 30416, "Joint " _PH1_ " has exceeded the limit, please do not continue to move out of the range", "suggest...") \
_D(RESUME_PAUSE_FAILED, 30417, "Resume Failed: Safety mode type is " _PH1_ "", "suggest...") \
_D(FIRMWARE_UPDATE_FAIL_EMERGENCYSTOP, 30418, "Failed to firmware update because the robot safety mode is in " _PH1_ , "Release emergency stop when the robot is in a safe range of motion") \
_D(TOOL_SENSOR_CHANGED, 30419, "Tool sensor type changed to " _PH1_ "", "suggest...") \
_D(TOOL_SENSOR_REMOVED, 30420, "Tool sensor is removed.", "suggest...") \
_D(CAL_TARGET_CURRENT_ERR, 30421, "The calculation of the target current failed. Please try again later.", "suggest...") \
_D(CONVEYOR_MODE_CHANGED, 30422, "Conveyor" _PH1_ ": track mode changed to " _PH2_ ", track item id is " _PH3_, "suggest...") \
_D(CONVEYOR_ENQUEUE, 30423, "Conveyor" _PH1_ ": the queue has been changed, item" _PH2_ " is enqueue", "suggest...") \
_D(CONVEYOR_DEQUEUE_FINISH, 30424, "Conveyor" _PH1_ ": the queue has been changed, item" _PH2_ " dequeue due to track finished", "suggest...") \
_D(CONVEYOR_DEQUEUE_STARTWINDOW, 30425, "Conveyor" _PH1_ ": the queue has been changed, item" _PH2_ " dequeue due to exceeds startwindow", "suggest...") \
_D(CONVEYOR_DEQUEUE_LIMIT, 30426, "Conveyor" _PH1_ ": the queue has been changed, item" _PH2_ " dequeue due to exceed limit area", "suggest...") \
_D(CONVEYOR_DEQUEUE_CLEAR, 30427, "Conveyor" _PH1_ ": item queue is cleared", "suggest...") \
_D(CONVEYOR_NEXT_TRACK, 30428, "Conveyor" _PH1_ ": item" _PH2_ " inside the start window that can be tracked ", "suggest...") \
_D(CONVEYOR_EXCEED_LIMIT, 30429, "Conveyor" _PH1_ ": item" _PH2_ " exceeds the limit area during tracking", "suggest...") \
_D(WRONG_POWER_SAFETY_LIMIT, 30430, "Robot power safety value exceeds designed value.", "suggest...") \
_D(WRONG_POWER_DESIGNED_LIMIT, 30431, "Power designed value exceeds value read from hardware interface.", "suggest...") \
_D(TOOL_SENSOR_STATUS_CHANGED, 30432, "Tool sensor status changed to " _PH1_, "suggest...") \
_D(COLLISION_THRESHOLD_INVALID, 30433, "Robot collision threshold parameters are invalid.Please reidentify the threshold or modify the configuration to ensure that it does not cause accidental collisions.", "suggest...") \
_D(GRIPPER_DISCONNECT, 30434, "The gripper " _PH1_ " is disconnected.", "suggest...") \
_D(GRIPPER_UNKNOWN_FAULT, 30435, "There is an unknown fault with the gripper " _PH1_ , "suggest...") \
_D(GRIPPER_CURRENT_ANOMALY_FAULT, 30436, "There is an abnormal current fault with the gripper " _PH1_ , "suggest...") \
_D(GRIPPER_VOLTAGE_ANOMALY_FAULT, 30437, "There is an abnormal voltage fault with the gripper " _PH1_ , "suggest...") \
_D(GRIPPER_OVER_TEMPERATURE_FAULT, 30438, "There is an over-temperature fault with the gripper " _PH1_ , "suggest...") \
_D(GRIPPER_INTERNAL_FAULT, 30439, "There is an internal fault with the gripper " _PH1_ , "suggest...") \
_D(GRIPPER_COMMUNICATION_FAULT, 30440, "There is an communication fault with the gripper " _PH1_ , "suggest...") \
_D(GRIPPER_CONTROL_COMMAND_FAULT, 30441, "There is an control command fault with the gripper " _PH1_ , "suggest...") \
_D(GRIPPER_ENABLE_FAULT, 30442, "There is an enable fault with the gripper " _PH1_ , "suggest...") \
_D(WRIST_SINGULARITY_RISK, 30443,"Wrist singularity detected. Linear motion may cause excessive joint speed.Adjust robot posture to avoid J5 near 0° or 180°, or use joint motion instead of linear motion.","suggest...") \
_D(PSTOP_PATH_OFFSET_OVER_LIMIT, 30444, "Protective Stop: the offset of the robot has exceeded the limit.", "suggest...") \
_D(EXT_AXIS_SET_PARAM_FAILED_BUSY, 30445, "Set external axis parameter " _PH1_ " failed because the robot mode is in " _PH2_ ". Please power off the robot before changing this parameter.", "Power off the robot and try again.") \
_D(WRONG_JOINT_POS_LIMIT, 30446, "Joint limit max pos less than min pos.", "suggest...") \
_D(WRONG_JOINT_VEL_LIMIT, 30447, "Joint limit max vel is invalid.", "suggest...") \
_D(AUTO_RESUME_FAR_PAUSE_PT, 30448, "Robot is still paused after switching to automatic mode. Current joint position [" _PH1_ "] is too far away from pause position [" _PH2_ "]. Resuming directly may cause collision.", "Move the robot back to the pause position or stop the program before resuming.") \
_D(CONVEYOR_TRACK_CAPACITY_EXCEEDED, 30449, "Conveyor" _PH1_ ": item" _PH2_ " tracking capacity exceeded, tracking stop started. Capacity usage is " _PH3_ "%, actual conveyor speed is " _PH4_ " m/s.", "Reduce conveyor speed, shorten tracking distance, or adjust tracking posture.")
// clang-format on
#endif // AUBO_SDK_RTM_ERROR_H

View File

@ -0,0 +1,60 @@
/** @file system_error.h
* @brief 系统错误码
*/
#ifndef AUBO_SDK_SYSTEM_ERROR_H
#define AUBO_SDK_SYSTEM_ERROR_H
#define SYSTEM_ERRORS \
_D(DEBUG, 0, "Debug message " _PH1_, "suggest...") \
_D(POPUP, 1, "Popup title: " _PH1_ ", msg: " _PH2_ ", mode: " _PH3_, \
"suggest...") \
_D(POPUP_DISMISS, 2, _PH1_, "suggest...") \
_D(SYSTEM_HALT, 3, _PH1_, "suggest...") \
_D(INV_ARGUMENTS, 4, "Invalid arguments.", "suggest...") \
_D(USER_NOTIFY, 5, _PH1_, "suggest...") \
_D(POPUP_DISMISS_BY_ID, 6, _PH1_, "suggest...") \
_D(MODBUS_SIGNAL_CREATED, 10, "Modbus signal " _PH1_ " created.", \
"suggest...") \
_D(MODBUS_SIGNAL_REMOVED, 11, "Modbus signal " _PH1_ " removed.", \
"suggest...") \
_D(MODBUS_SIGNAL_VALUE_CHANGED, 12, \
"Modbus signal " _PH1_ " value changed to " _PH2_, "suggest...") \
_D(RUNTIME_CONTEXT, 13, \
"tid: " _PH1_ " lineno: " _PH2_ " index: " _PH3_ " comment: " _PH4_, \
"suggest...") \
_D(INTERP_CONTEXT, 14, \
"tid: " _PH1_ " lineno: " _PH2_ " index: " _PH3_ " comment: " _PH4_, \
"suggest...") \
_D(PROGRAM_LOADED, 15, "program loaded: " _PH1_, "suggest...") \
_D(TASK_DELETED, 16, "tid: " _PH1_, " was deleted") \
_D(MODBUS_SLAVE_BIT, 20, "Modbus slave address: " _PH1_ " value " _PH2_, \
"suggest...") \
_D(MODBUS_SLAVE_REG, 21, "Modbus slave address: " _PH1_ " value " _PH2_, \
"suggest...") \
_D(PNIO_SLAVE_SLOT_VALUE, 30, \
"PNIO slot: " _PH1_ " subslot " _PH2_ " index " _PH3_ " value " _PH4_, \
"suggest...") \
_D(PNIO_CONNECT_STATUS, 31, "PNIO connection status changed to " _PH1_, \
"suggest...") \
_D(PNIO_DEVICE_NAME, 32, "PNIO device name changed to " _PH1_, \
"suggest...") \
_D(PNIO_IP, 33, "PNIO ip " _PH1_ " mask " _PH2_ " gateway " _PH3_, \
"suggest...") \
_D(ICM_SERVER_STATUS, 40, " ICM server status changed to " _PH1_, \
"suggest...") \
_D(EIP_SLAVE_VALUE, 50, \
"EIP slave: trans_type " _PH1_ " index " _PH2_ " value " _PH3_, \
"suggest...") \
_D(EIP_SLAVE_CONNECT_STATUS, 51, \
"EIP slave connection status changed to " _PH1_, "suggest...") \
_D(LOG_PROGRAM_SUCCESS, 100, \
"[" _PH1_ "] Load program " _PH2_ " successful", "suggest...") \
_D(LOG_PROGRAM_FAILED, 101, \
"[" _PH1_ "] Load program " _PH2_ " failed, file not found", \
"suggest...") \
_D(LOG_PROGRAM_FAILED2, 102, \
"[" _PH1_ "] Load program " _PH2_ \
" failed, configuration file (.ins) does not match", \
"suggest...")
#endif // AUBO_SDK_SYSTEM_ERROR_H

View File

@ -0,0 +1,178 @@
/*
global_config.h
this file is generated. Do not change!
*/
#ifndef ARCS_GLOBALCONFIG_H
#define ARCS_GLOBALCONFIG_H
/* #undef ARCS_BUILD_SHARED_LIBS */
/* #undef ARCS_ENABLE_THREADING_SUPPORT */
//-------------------------------------------------------------------
// Header Availability
//-------------------------------------------------------------------
/* #undef ARCS_HAVE_CXXABI_H */
//-------------------------------------------------------------------
// Version information
//-------------------------------------------------------------------
#define INTERFACE_VERSION_MAJOR 0
#define INTERFACE_VERSION_MINOR 26
#define INTERFACE_VERSION_PATCH 0
#define INTERFACE_VERSION "0.26.0"
//-------------------------------------------------------------------
// Platform defines
//-------------------------------------------------------------------
#if defined(__APPLE__)
#define ARCS_PLATFORM_APPLE
#endif
#if defined(__linux__)
#define ARCS_PLATFORM_LINUX
#endif
#if defined(_WIN32) || defined(_WIN64)
#define ARCS_PLATFORM_WINDOWS
#else
#define ARCS_PLATFORM_POSIX
#endif
/* #undef ARCS_BIG_ENDIAN */
/* #undef ARCS_LITTLE_ENDIAN */
#define ARCS_LIB_PREFIX "lib"
#define ARCS_LIB_EXT ".so"
#define ARCS_EXE_EXT ""
#ifdef NDEBUG // Defined by cmake UNLESS Debug build type is chosen
#define ARCS_LIB_POSTFIX ""
#else
#define ARCS_LIB_POSTFIX \
"" // Set in top level CMakeList.txt
#endif
#define ARCS_FUNCTION // 函数接口
#define ARCS_INSTRUCT // 指令接口
///-------------------------------------------------------------------
// Macros for import/export declarations
//-------------------------------------------------------------------
#if defined(ARCS_PLATFORM_WINDOWS)
#define ARCS_ABI_EXPORT __declspec(dllexport)
#define ARCS_ABI_IMPORT __declspec(dllimport)
#define ARCS_ABI_LOCAL
#elif defined(ARCS_HAVE_VISIBILITY_ATTRIBUTE)
#define ARCS_ABI_EXPORT __attribute__((visibility("default")))
#define ARCS_ABI_IMPORT __attribute__((visibility("default")))
#define ARCS_ABI_LOCAL __attribute__((visibility("hidden")))
#else
#define ARCS_ABI_EXPORT
#define ARCS_ABI_IMPORT
#define ARCS_ABI_LOCAL
#endif
#ifdef ARCS_BUILDING_STAGE
#define ARCS_ABI ARCS_ABI_EXPORT
#else
#define ARCS_ABI ARCS_ABI_IMPORT
#endif
//-------------------------------------------------------------------
// Macros for suppressing warnings
//-------------------------------------------------------------------
#ifdef _MSC_VER
#define ARCS_MSVC_PUSH_DISABLE_WARNING(wn) \
__pragma(warning(push)) __pragma(warning(disable : wn))
#define ARCS_MSVC_POP_WARNING __pragma(warning(pop))
#define ARCS_MSVC_DISABLE_WARNING(wn) __pragma(warning(disable : wn))
#else
#define ARCS_MSVC_PUSH_DISABLE_WARNING(wn)
#define ARCS_MSVC_POP_WARNING
#define ARCS_MSVC_DISABLE_WARNING(wn)
#endif
#ifdef __GNUC__
#define aubo_gcc_pragma_expand(x) _Pragma(#x)
#define ARCS_GCC_PUSH_DISABLE_WARNING(wn) \
_Pragma("GCC diagnostic push") \
aubo_gcc_pragma_expand(GCC diagnostic ignored "-W" #wn)
#define ARCS_GCC_POP_WARNING _Pragma("GCC diagnostic pop")
#else
#define ARCS_GCC_PUSH_DISABLE_WARNING(wn)
#define ARCS_GCC_POP_WARNING
#endif
#if defined(__GNUC__)
#define ARCS_DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER)
#define ARCS_DEPRECATED __declspec(deprecated)
#else
#pragma message( \
"WARNING: You need to implement ARCS_DEPRECATED for your compiler!")
#define ARCS_DEPRECATED
#endif
// Do not warn about the usage of deprecated unsafe functions
ARCS_MSVC_DISABLE_WARNING(4996)
// Mark a variable or expression result as unused
#define ARCS_UNUSED(x) (void)(x)
//-------------------------------------------------------------------
// C++ Language features
//-------------------------------------------------------------------
/* #undef ARCS_HAVE_THREAD_LOCAL */
//-------------------------------------------------------------------
// C++ Library features
//-------------------------------------------------------------------
/* #undef ARCS_HAVE_REGEX */
//-------------------------------------------------------------------
// Hash Container
//-------------------------------------------------------------------
#define ARCS_HASH_FUNCTION_BEGIN(type) \
namespace std { \
template <> \
struct hash<type> \
{ \
std::size_t operator()(const type &arg) const \
{
#define ARCS_HASH_FUNCTION_END \
} \
} \
; \
}
//-------------------------------------------------------------------
// Utility macros
//-------------------------------------------------------------------
#define ARCS_STR_(x) #x
#define ARCS_STR(x) ARCS_STR_(x)
#define ARCS_CONCAT_(x, y) x##y
#define ARCS_CONCAT(x, y) ARCS_CONCAT_(x, y)
//-------------------------------------------------------------------
// Backwards compatibility macros
//-------------------------------------------------------------------
#if !defined(__clang__) && __GNUC__ == 4 && __GNUC_MINOR__ < 7
#define ARCS_FUTURE_READY true
#define ARCS_FUTURE_TIMEOUT false
#else
#define ARCS_FUTURE_READY std::future_status::ready
#define ARCS_FUTURE_TIMEOUT std::future_status::timeout
#endif
#endif // ARCS_GLOBALCONFIG_H

View File

@ -0,0 +1,320 @@
/** @file gripper_interface.h
* @brief 通用夹爪接口
*/
#ifndef AUBO_SDK_GRIPPER_INTERFACE_H
#define AUBO_SDK_GRIPPER_INTERFACE_H
#include <aubo/sync_move.h>
#include <aubo/trace.h>
namespace arcs {
namespace common_interface {
/**
* \chinese
* @defgroup GripperInterface GripperInterface (夹爪)
* 通用夹爪API接口
* \endchinese
*
* \english
* @defgroup GripperInterface Gripper Interface
* Common Gripper API interface
* \endenglish
*/
class ARCS_ABI_EXPORT GripperInterface
{
public:
GripperInterface();
virtual ~GripperInterface();
/**
* @ingroup GripperInterface
* \chinese
* 获取支持的所有夹爪
*
* @return
* \endchinese
*/
std::vector<std::string> gripperGetSupportedModels();
/**
* @ingroup GripperInterface
* \chinese
* 扫描该型号下的所有设备
*
* @param model 夹爪品牌型号
* @param device_name 设备名
* @return 成功返回设备列表和0,失败返回错误码
* \endchinese
*/
ResultWithErrno2 gripperScanDevices(const std::string &model,
const std::string &device_name);
/**
* @ingroup GripperInterface
* \chinese
* 获取已添加的夹爪
* @return
*/
std::vector<std::string> gripperGetNames();
/**
* @ingroup GripperInterface
* \chinese
* 添加夹爪
*
* @param name 用户自定义名字,唯一
* @param model 夹爪品牌及型号
* @return 成功返回0,失败返回错误码
* \endchinese
*/
int gripperAdd(const std::string &name, const std::string &model);
/**
* @ingroup GripperInterface
* \chinese
* 删除夹爪
*
* @param name 用户自定义名字
* @return 成功返回0,失败返回错误码
* \endchinese
*/
int gripperDelete(const std::string &name);
/**
* @ingroup GripperInterface
* \chinese
* 删除所有夹爪
*
* @return 成功返回0,失败返回错误码
* \endchinese
*/
int gripperDeleteAll();
/**
* @ingroup GripperInterface
* \chinese
* 修改夹爪名
*
* @param name
* @param new_name 新名字
* @return 成功返回0,失败返回错误码
* \endchinese
*/
int gripperRename(const std::string &name, const std::string &new_name);
/**
* @ingroup GripperInterface
* \chinese
* 夹爪连接
*
* @param name
* @param device_name 设备名
* @return 成功返回0,失败返回错误码
* \endchinese
*/
int gripperConnect(const std::string &name, const std::string &device_name);
/**
* @ingroup GripperInterface
* \chinese
* 夹爪断开连接
*
* @param name
* @return 成功返回0,失败返回错误码
* \endchinese
*/
int gripperDisconnect(const std::string &name);
/**
* @ingroup GripperInterface
* \chinese
* 夹爪是否连接
*
* @param name
* @return 已连接返回true,未连接返回false
* \endchinese
*/
bool gripperIsConnected(const std::string &name);
/**
* @ingroup GripperInterface
* \chinese
* 设置工作模式
*
* @param name
* @param work_mode 工作模式
* @return 成功返回0,失败返回错误码
* \endchinese
*/
int gripperSetWorkMode(const std::string &name, int work_mode);
/**
* @ingroup GripperInterface
* \chinese
* 获取工作模式
*
* @param name
* @return 返回工作模式
* \endchinese
*/
int gripperGetWorkMode(const std::string &name);
/**
* @ingroup GripperInterface
* \chinese
* 设置夹爪安装偏移
*
* @param name
* @param isVisibled 是否显示
* @param pose 末端(不包括手指)相对法兰的位姿
* @return 成功返回true,失败返回false
* \endchinese
*/
int gripperSetMountPose(const std::string &name,
const std::vector<double> &pose,
bool enable_collision);
/**
* @brief 获取夹爪安装偏移
* @param name
* @return 返回夹爪安装偏移
*/
std::vector<double> gripperGetMountPose(const std::string &name);
/**
* @ingroup GripperInterface
* \chinese
* 使能夹爪
*
* @param name
* @return
* \endchinese
*/
int gripperEnable(const std::string &name, bool enable);
/**
* \chinese
* 等待夹爪使能完成(硬件激活),超时15s
* 初始化成功后再添加 gripper_link 到运动学模型
*
* @param name 夹爪名称
* @return 成功返回0,超时返回错误码
* \endchinese
*/
int gripperWaitEnableFinished(const std::string &name);
/**
* @ingroup GripperInterface
* \chinese
* 夹爪是否使能
*
* @param name
* @return
* \endchinese
*/
bool gripperIsEnabled(const std::string &name);
/**
* @ingroup GripperInterface
* \chinese
* 设置运动参数
*
* @param name
* @param position 位置,单位:m/Pa
* @param velocity_percent 速度,单位:%,范围:[0, 1]
* @param force 夹持力,单位:N
* @param angle 旋转角度,单位:rad
* @param r_velocity_percent 旋转速度,单位:%,范围:[0, 1]
* @param torque_percent 旋转扭矩,单位:%,范围:[0, 1]
*
* @return
* \endchinese
*/
int gripperSetPosition(const std::string &name, const double position);
int gripperSetVelocity(const std::string &name,
const double velocity_percent);
int gripperSetForce(const std::string &name, const double force);
int gripperSetAngle(const std::string &name, const double angle);
int gripperSetRVelocity(const std::string &name,
const double r_velocity_percent);
int gripperSetTorque(const std::string &name, const double torque_percent);
/**
* @ingroup GripperInterface
* \chinese
* 开始运动
*
* @param name
* @return
* \endchinese
*/
int gripperMove(const std::string &name);
/**
* @ingroup GripperInterface
* \chinese
* 停止运动
*
* @param name
* @return
* \endchinese
*/
int gripperStop(const std::string &name);
/**
* @ingroup GripperInterface
* \chinese
* 获取夹爪状态
*
* @param name
* @return
*
* @note 位置,速度,夹持力,旋转角度,旋转速度,旋转扭矩的单位与Set接口一致
*
* \endchinese
*/
std::string gripperGetHardwareVersion(const std::string &name);
std::string gripperGetSoftwareVersion(const std::string &name);
double gripperGetPosition(const std::string &name);
double gripperGetVelocity(const std::string &name);
double gripperGetForce(const std::string &name);
double gripperGetAngle(const std::string &name);
double gripperGetRVelocity(const std::string &name);
double gripperGetTorque(const std::string &name);
bool gripperGetObjectDetection(const std::string &name);
bool gripperGetMotionState(const std::string &name);
double gripperGetVoltage(const std::string &name);
double gripperGetTemperature(const std::string &name);
/**
* @ingroup GripperInterface
* \chinese
* 重置modbus从站ID
*
* @param name
* @param slave_id 从站Id
* @return
* \endchinese
*/
int gripperResetSlaveId(const std::string &name, const int slave_id);
/**
* @ingroup GripperInterface
* \chinese
* 获取夹爪状态
* @param name
* @return 状态
* \endchinese
*/
int gripperGetStatusCode(const std::string &name);
protected:
void *d_;
};
using GripperInterfacePtr = std::shared_ptr<GripperInterface>;
} // namespace common_interface
} // namespace arcs
#endif // AUBO_SDK_GRIPPER_INTERFACE_H

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,403 @@
/** @file robot_interface.h
* @brief 机器人API 接口
*/
#ifndef AUBO_SDK_ROBOT_INTERFACE_H
#define AUBO_SDK_ROBOT_INTERFACE_H
#include <aubo/sync_move.h>
#include <aubo/trace.h>
#include <aubo/robot/motion_control.h>
#include <aubo/robot/force_control.h>
#include <aubo/robot/io_control.h>
#include <aubo/robot/robot_algorithm.h>
#include <aubo/robot/robot_state.h>
#include <aubo/robot/robot_manage.h>
#include <aubo/robot/robot_config.h>
#include <aubo/global_config.h>
namespace arcs {
namespace common_interface {
/**
* \chinese
* @defgroup RobotInterface RobotInterface(机器人模块)
* 机器人模块
* \endchinese
*
* \english
* @defgroup RobotInterface Robot Module
* Robot Module
* \endenglish
*/
class ARCS_ABI_EXPORT RobotInterface
{
public:
RobotInterface();
virtual ~RobotInterface();
/**
* @ingroup RobotInterface
* @ref RobotConfig
* \chinese
* 获取RobotConfig接口
*
* @return RobotConfigPtr对象的指针
*
* @par Python函数原型
* getRobotConfig(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::RobotConfig
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* RobotConfigPtr ptr =
* rpc_cli->getRobotInterface(robot_name)->getRobotConfig();
* @endcode
* \endchinese
* \english
* Get RobotConfig interface
*
* @return Pointer to RobotConfig object
*
* @par Python function prototype
* getRobotConfig(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::RobotConfig
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* RobotConfigPtr ptr =
* rpc_cli->getRobotInterface(robot_name)->getRobotConfig();
* @endcode
* \endenglish
*/
RobotConfigPtr getRobotConfig();
/**
* @ingroup RobotInterface
* @ref MotionControl
* \chinese
* 获取运动规划接口
*
* @return MotionControlPtr对象的指针
*
* @par Python函数原型
* getMotionControl(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::MotionControl
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* MotionControlPtr ptr =
* rpc_cli->getRobotInterface(robot_name)->getMotionControl();
* @endcode
* \endchinese
* \english
* Get motion planning interface
*
* @return Pointer to MotionControl object
*
* @par Python function prototype
* getMotionControl(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::MotionControl
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* MotionControlPtr ptr =
* rpc_cli->getRobotInterface(robot_name)->getMotionControl();
* @endcode
* \endenglish
*/
MotionControlPtr getMotionControl();
/**
* @ingroup RobotInterface
* @ref ForceControl
* \chinese
* 获取力控接口
*
* @return ForceControlPtr对象的指针
*
* @par Python函数原型
* getForceControl(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::ForceControl
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* ForceControlPtr ptr =
* rpc_cli->getRobotInterface(robot_name)->getForceControl();
* @endcode
* \endchinese
* \english
* Get force control interface
*
* @return Pointer to ForceControl object
*
* @par Python function prototype
* getForceControl(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::ForceControl
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* ForceControlPtr ptr =
* rpc_cli->getRobotInterface(robot_name)->getForceControl();
* @endcode
* \endenglish
*/
ForceControlPtr getForceControl();
/**
* @ingroup RobotInterface
* @ref IoControl
* \chinese
* 获取IO控制的接口
*
* @return IoControlPtr对象的指针
*
* @par Python函数原型
* getIoControl(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::IoControl
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* IoControlPtr ptr =
* rpc_cli->getRobotInterface(robot_name)->getIoControl();
* @endcode
* \endchinese
* \english
* Get IO control interface
*
* @return Pointer to IoControl object
*
* @par Python function prototype
* getIoControl(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::IoControl
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* IoControlPtr ptr =
* rpc_cli->getRobotInterface(robot_name)->getIoControl();
* @endcode
* \endenglish
*/
IoControlPtr getIoControl();
/**
* @ingroup RobotInterface
* @ref SyncMove
* \chinese
* 获取同步运动接口
*
* @return SyncMovePtr对象的指针
*
* @par Python函数原型
* getSyncMove(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::SyncMove
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* SyncMovePtr ptr = rpc_cli->getRobotInterface(robot_name)->getSyncMove();
* @endcode
* \endchinese
* \english
* Get synchronized motion interface
*
* @return Pointer to SyncMove object
*
* @par Python function prototype
* getSyncMove(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::SyncMove
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* SyncMovePtr ptr = rpc_cli->getRobotInterface(robot_name)->getSyncMove();
* @endcode
* \endenglish
*/
SyncMovePtr getSyncMove();
/**
* @ingroup RobotInterface
* @ref RobotAlgorithm
* \chinese
* 获取机器人实用算法接口
*
* @return RobotAlgorithmPtr对象的指针
*
* @par Python函数原型
* getRobotAlgorithm(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::RobotAlgorithm
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* RobotAlgorithmPtr ptr =
* rpc_cli->getRobotInterface(robot_name)->getRobotAlgorithm();
* @endcode
* \endchinese
* \english
* Get robot utility algorithm interface
*
* @return Pointer to RobotAlgorithm object
*
* @par Python function prototype
* getRobotAlgorithm(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::RobotAlgorithm
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* RobotAlgorithmPtr ptr =
* rpc_cli->getRobotInterface(robot_name)->getRobotAlgorithm();
* @endcode
* \endenglish
*/
RobotAlgorithmPtr getRobotAlgorithm();
/**
* @ingroup RobotInterface
* @ref RobotManage
* \chinese
* 获取机器人管理接口(上电、启动、停止等)
*
* @return RobotManagePtr对象的指针
*
* @par Python函数原型
* getRobotManage(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::RobotManage
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* RobotManagePtr ptr =
* rpc_cli->getRobotInterface(robot_name)->getRobotManage();
* @endcode
* \endchinese
* \english
* Get robot management interface (power on, start, stop, etc.)
*
* @return Pointer to RobotManage object
*
* @par Python function prototype
* getRobotManage(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::RobotManage
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* RobotManagePtr ptr =
* rpc_cli->getRobotInterface(robot_name)->getRobotManage();
* @endcode
* \endenglish
*/
RobotManagePtr getRobotManage();
/**
* @ingroup RobotInterface
* @ref RobotState
* \chinese
* 获取机器人状态接口
*
* @return RobotStatePtr对象的指针
*
* @par Python函数原型
* getRobotState(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::RobotState
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* RobotStatePtr ptr =
* rpc_cli->getRobotInterface(robot_name)->getRobotState();
* @endcode
* \endchinese
* \english
* Get robot state interface
*
* @return Pointer to RobotState object
*
* @par Python function prototype
* getRobotState(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::RobotState
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* RobotStatePtr ptr =
* rpc_cli->getRobotInterface(robot_name)->getRobotState();
* @endcode
* \endenglish
*/
RobotStatePtr getRobotState();
/**
* @ingroup RobotInterface
* @ref Trace
* \chinese
* 获取告警信息接口
*
* @return TracePtr对象的指针
*
* @par Python函数原型
* getTrace(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::Trace
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* TracePtr ptr = rpc_cli->getRobotInterface(robot_name)->getTrace();
* @endcode
* \endchinese
* \english
* Get alarm information interface
*
* @return Pointer to Trace object
*
* @par Python function prototype
* getTrace(self: pyaubo_sdk.RobotInterface) ->
* arcs::common_interface::Trace
*
* @par C++ example
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* TracePtr ptr = rpc_cli->getRobotInterface(robot_name)->getTrace();
* @endcode
* \endenglish
*/
TracePtr getTrace();
protected:
void *d_;
};
using RobotInterfacePtr = std::shared_ptr<RobotInterface>;
} // namespace common_interface
} // namespace arcs
#endif // AUBO_SDK_ROBOT_INTERFACE_H

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,417 @@
/** @file serial.h
* @brief 串口通信
*/
#ifndef AUBO_SDK_SERIAL_INTERFACE_H
#define AUBO_SDK_SERIAL_INTERFACE_H
#include <vector>
#include <memory>
#include <aubo/type_def.h>
#include <aubo/global_config.h>
namespace arcs {
namespace common_interface {
/**
* \chinese
* @defgroup Serial Serial (串口通信)
* 串口通信
* \endchinese
*
* \english
* @defgroup Serial Serial Communication
* Serial communication
* \endenglish
*/
class ARCS_ABI_EXPORT Serial
{
public:
Serial();
virtual ~Serial();
/**
* @ingroup Serial
* \chinese
* 打开TCP/IP以太网通信串口
*
* @param device 设备名
* @param baud 波特率
* @param stop_bits 停止位
* @param even 校验位
* @param serial_name 串口名称
* @return 返回值
*
* @par Python函数原型
* serialOpen(self: pyaubo_sdk.Serial, arg0: str, arg1: int, arg2: float,
* arg3: int, arg4: str) -> int
*
* @par Lua函数原型
* serialOpen(device: string, baud: number, stop_bits: number, even: number,
* serial_name: string) -> nil
* \endchinese
* \english
* Open TCP/IP ethernet communication serial
*
* @param device
* @param baud
* @param stop_bits
* @param even
* @param serial_name
* @return
*
* @par Python function prototype
* serialOpen(self: pyaubo_sdk.Serial, arg0: str, arg1: int, arg2: float,
* arg3: int, arg4: str) -> int
*
* @par Lua function prototype
* serialOpen(device: string, baud: number, stop_bits: number, even: number,
* serial_name: string) -> nil
* \endenglish
*/
int serialOpen(const std::string &device, int baud, float stop_bits,
int even, const std::string &serial_name = "serial_0");
/**
* @ingroup Serial
* \chinese
* 关闭TCP/IP串口通信
* 关闭与服务器的串口连接。
*
* @param serial_name 串口名称
* @return 返回值
*
* @par Python函数原型
* serialClose(self: pyaubo_sdk.Serial, arg0: str) -> int
*
* @par Lua函数原型
* serialClose(serial_name: string) -> nil
* \endchinese
* \english
* Close TCP/IP serial communication
* Close down the serial connection to the server.
*
* @param serial_name
* @return
*
* @par Python function prototype
* serialClose(self: pyaubo_sdk.Serial, arg0: str) -> int
*
* @par Lua function prototype
* serialClose(serial_name: string) -> nil
* \endenglish
*/
int serialClose(const std::string &serial_name = "serial_0");
/**
* @ingroup Serial
* \chinese
* 从串口读取指定数量的字节。字节为网络字节序。一次最多可读取30个值。
*
* @param variable 变量
* @param serial_name 串口名称
* @return 返回值
*
* @par Python函数原型
* serialReadByte(self: pyaubo_sdk.Serial, arg0: str, arg1: str) -> int
*
* @par Lua函数原型
* serialReadByte(variable: string, serial_name: string) -> number
* \endchinese
* \english
* Reads a number of bytes from the serial. Bytes are in network byte
* order. A maximum of 30 values can be read in one command.
*
* @param variable
* @param serial_name
* @return
*
* @par Python function prototype
* serialReadByte(self: pyaubo_sdk.Serial, arg0: str, arg1: str) -> int
*
* @par Lua function prototype
* serialReadByte(variable: string, serial_name: string) -> number
* \endenglish
*/
int serialReadByte(const std::string &variable,
const std::string &serial_name = "serial_0");
/**
* @ingroup Serial
* \chinese
* 从串口读取指定数量的字节。字节为网络字节序。一次最多可读取30个值。
* 返回读取到的数字列表(int列表,长度=number+1)。
*
* @param number 读取的字节数
* @param variable 变量
* @param serial_name 串口名称
* @return 返回值
*
* @par Python函数原型
* serialReadByteList(self: pyaubo_sdk.Serial, arg0: int, arg1: str, arg2: str) -> int
*
* @par Lua函数原型
* serialReadByteList(number: number, variable: string, serial_name: string) -> number
* \endchinese
* \english
* Reads a number of bytes from the serial. Bytes are in network byte
* order. A maximum of 30 values can be read in one command.
* A list of numbers read (list of ints, length=number+1)
*
* @param number Number of bytes to read
* @param variable
* @param serial_name Serial port name
* @return Return value
*
* @par Python function prototype
* serialReadByteList(self: pyaubo_sdk.Serial, arg0: int, arg1: str, arg2: str) -> int
*
* @par Lua function prototype
* serialReadByteList(number: number, variable: string, serial_name: string) -> number
* \endenglish
*/
int serialReadByteList(int number, const std::string &variable,
const std::string &serial_name = "serial_0");
/**
* @ingroup Serial
* \chinese
* 从串口读取所有数据,并将数据作为字符串返回。
* 字节为网络字节序。
*
* 可选参数 "prefix" 和 "suffix" 用于指定从串口提取的内容。
* "prefix" 指定提取子串(消息)的起始位置。直到 "prefix" 结束的数据会被忽略并从串口移除。
* "suffix" 指定提取子串(消息)的结束位置。串口中 "suffix" 之后的剩余数据会被保留。
* 例如,如果串口服务器发送字符串 "noise>hello<",控制器可以通过设置 prefix=">" 和 suffix="<" 来接收 "hello"。
* 通过使用 "prefix" 和 "suffix",还可以一次向控制器发送多条字符串,因为 "suffix" 定义了消息的结束位置。
* 例如发送 ">hello<>world<"
*
* @param variable 变量
* @param serial_name 串口名称
* @param prefix 前缀
* @param suffix 后缀
* @param interpret_escape 是否解释转义字符
* @return 返回值
*
* @par Python函数原型
* serialReadString(self: pyaubo_sdk.Serial, arg0: str, arg1: str, arg2: str, arg3: str, arg4: bool) -> int
*
* @par Lua函数原型
* serialReadString(variable: string, serial_name: string, prefix: string, suffix: string, interpret_escape: boolean) -> number
* \endchinese
* \english
* Reads all data from the serial and returns the data as a string.
* Bytes are in network byte order.
*
* The optional parameters "prefix" and "suffix", can be used to express
* what is extracted from the serial. The "prefix" specifies the start
* of the substring (message) extracted from the serial. The data up to
* the end of the "prefix" will be ignored and removed from the serial.
* The "suffix" specifies the end of the substring (message) extracted
* from the serial. Any remaining data on the serial, after the "suffix",
* will be preserved. E.g. if the serial server sends a string
* "noise>hello<", the controller can receive the "hello" by calling this
* script function with the prefix=">" and suffix="<". By using the
* "prefix" and "suffix" it is also possible send multiple string to the
* controller at once, because the suffix defines where the message ends.
* E.g. sending ">hello<>world<"
*
* @param variable
* @param serial_name
* @param prefix
* @param suffix
* @param interpret_escape
* @return
*
* @par Python function prototype
* serialReadString(self: pyaubo_sdk.Serial, arg0: str, arg1: str, arg2: str, arg3: str, arg4: bool) -> int
*
* @par Lua function prototype
* serialReadString(variable: string, serial_name: string, prefix: string, suffix: string, interpret_escape: boolean) -> number
* \endenglish
*/
int serialReadString(const std::string &variable,
const std::string &serial_name = "serial_0",
const std::string &prefix = "",
const std::string &suffix = "",
bool interpret_escape = false);
/**
* @ingroup Serial
* \chinese
* 发送一个字节到服务器
* 通过串口发送字节 <value>。不期望有响应。可用于发送特殊的ASCII字符;10为换行符,2为文本开始,3为文本结束。
*
* @param value 字节值
* @param serial_name 串口名称
* @return 返回值
*
* @par Python函数原型
* serialSendByte(self: pyaubo_sdk.Serial, arg0: str, arg1: str) -> int
*
* @par Lua函数原型
* serialSendByte(value: string, serial_name: string) -> nil
* \endchinese
* \english
* Sends a byte to the server
* Sends the byte <value> through the serial. Expects no response. Can
* be used to send special ASCII characters; 10 is newline, 2 is start of
* text, 3 is end of text.
*
* @param value
* @param serial_name
* @return
*
* @par Python function prototype
* serialSendByte(self: pyaubo_sdk.Serial, arg0: str, arg1: str) -> int
*
* @par Lua function prototype
* serialSendByte(value: string, serial_name: string) -> nil
* \endenglish
*/
int serialSendByte(char value, const std::string &serial_name = "serial_0");
/**
* @ingroup Serial
* \chinese
* 发送一个整数(int32_t)到服务器
* 通过串口发送整数 <value>。以网络字节序发送。不期望有响应。
*
* @param value 整数值
* @param serial_name 串口名称
* @return 返回值
*
* @par Python函数原型
* serialSendInt(self: pyaubo_sdk.Serial, arg0: int, arg1: str) -> int
*
* @par Lua函数原型
* serialSendInt(value: number, serial_name: string) -> nil
* \endchinese
* \english
* Sends an int (int32_t) to the server
* Sends the int <value> through the serial. Send in network byte order.
* Expects no response.
*
* @param value
* @param serial_name
* @return
*
* @par Python function prototype
* serialSendInt(self: pyaubo_sdk.Serial, arg0: int, arg1: str) -> int
*
* @par Lua function prototype
* serialSendInt(value: number, serial_name: string) -> nil
* \endenglish
*/
int serialSendInt(int value, const std::string &serial_name = "serial_0");
/**
* @ingroup Serial
* \chinese
* 发送带有换行符的字符串到服务器
* 以ASCII编码通过串口发送字符串<str>,并在末尾添加换行符。不期望有响应。
*
* @param str 字符串
* @param serial_name 串口名称
* @return 返回值
*
* @par Python函数原型
* serialSendLine(self: pyaubo_sdk.Serial, arg0: str, arg1: str) -> int
*
* @par Lua函数原型
* serialSendLine(str: string, serial_name: string) -> nil
* \endchinese
* \english
* Sends a string with a newline character to the server
* Sends the string <str> through the serial in ASCII coding, appending a newline at the end. Expects no response.
*
* @param str
* @param serial_name
* @return
*
* @par Python function prototype
* serialSendLine(self: pyaubo_sdk.Serial, arg0: str, arg1: str) -> int
*
* @par Lua function prototype
* serialSendLine(str: string, serial_name: string) -> nil
* \endenglish
*/
int serialSendLine(const std::string &str,
const std::string &serial_name = "serial_0");
/**
* @ingroup Serial
* \chinese
* 发送字符串到服务器
* 以ASCII编码通过串口发送字符串<str>。不期望有响应。
*
* @param str 字符串
* @param serial_name 串口名称
* @return 返回值
*
* @par Python函数原型
* serialSendString(self: pyaubo_sdk.Serial, arg0: str, arg1: str) -> int
*
* @par Lua函数原型
* serialSendString(str: string, serial_name: string) -> nil
* \endchinese
* \english
* Sends a string to the server
* Sends the string <str> through the serial in ASCII coding. Expects no
* response.
*
* @param str
* @param serial_name
* @return
*
* @par Python function prototype
* serialSendString(self: pyaubo_sdk.Serial, arg0: str, arg1: str) -> int
*
* @par Lua function prototype
* serialSendString(str: string, serial_name: string) -> nil
* \endenglish
*/
int serialSendString(const std::string &str,
const std::string &serial_name = "serial_0");
/**
* @ingroup Serial
* \chinese
*
* @param is_check 是否校验
* @param str 字符串数组
* @param serial_name 串口名称
* @return 返回值
*
* @par Python函数原型
* serialSendAllString(self: pyaubo_sdk.Serial, arg0: bool, arg1: List[str], arg2: str) -> int
*
* @par Lua函数原型
* serialSendAllString(is_check: boolean, str: table, serial_name: string) -> nil
* \endchinese
* \english
*
* @param is_check Whether to check
* @param str Array of strings
* @param serial_name Serial port name
* @return Return value
*
* @par Python function prototype
* serialSendAllString(self: pyaubo_sdk.Serial, arg0: bool, arg1: List[str], arg2: str) -> int
*
* @par Lua function prototype
* serialSendAllString(is_check: boolean, str: table, serial_name: string) -> nil
* \endenglish
*/
int serialSendAllString(bool is_check, const std::vector<char> &str,
const std::string &serial_name = "serial_0");
protected:
void *d_;
};
using SerialPtr = std::shared_ptr<Serial>;
} // namespace common_interface
} // namespace arcs
#endif

View File

@ -0,0 +1,667 @@
/** @file socket.h
* @brief socket通信
*/
#ifndef AUBO_SDK_SOCKET_INTERFACE_H
#define AUBO_SDK_SOCKET_INTERFACE_H
#include <vector>
#include <memory>
#include <aubo/type_def.h>
#include <aubo/global_config.h>
namespace arcs {
namespace common_interface {
/**
* \chinese
* @defgroup Socket Socket (socket网络通信)
* 串口通信
* \endchinese
*
* \english
* @defgroup Socket Socket Network Communication
* Socket communication
* \endenglish
*/
class ARCS_ABI_EXPORT Socket
{
public:
Socket();
virtual ~Socket();
/**
* @ingroup Socket
* \english
* Open TCP/IP ethernet communication socket
*
* Instruction
*
* @param address
* @param port
* @param socket_name
* @return
*
* @par Python function prototype
* socketOpen(self: pyaubo_sdk.Socket, arg0: str, arg1: int, arg2: str) -> int
*
* @par Lua function prototype
* socketOpen(address: string, port: number, socket_name: string) -> nil
*
* @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"Socket.socketOpen","params":["172.16.26.248",8000,"socket_0"],"id":1}
*
* @par JSON-RPC response example
* {"id":1,"jsonrpc":"2.0","result":0}
* \endenglish
* \chinese
* 打开TCP/IP以太网通信socket
*
* 指令
*
* @param address 地址
* @param port 端口
* @param socket_name 套接字名称
* @return 返回值
*
* @par Python函数原型
* socketOpen(self: pyaubo_sdk.Socket, arg0: str, arg1: int, arg2: str) -> int
*
* @par Lua函数原型
* socketOpen(address: string, port: number, socket_name: string) -> nil
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"Socket.socketOpen","params":["172.16.26.248",8000,"socket_0"],"id":1}
*
* @par JSON-RPC响应示例
* {"id":1,"jsonrpc":"2.0","result":0}
* \endchinese
*/
int socketOpen(const std::string &address, int port,
const std::string &socket_name = "socket_0");
/**
* @ingroup Socket
* \english
* Closes TCP/IP socket communication
* Closes down the socket connection to the server.
*
* Instruction
*
* @param socket_name
* @return
*
* @par Python function prototype
* socketClose(self: pyaubo_sdk.Socket, arg0: str) -> int
*
* @par Lua function prototype
* socketClose(socket_name: string) -> nil
*
* @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"Socket.socketClose","params":["socket_0"],"id":1}
*
* @par JSON-RPC response example
* {"id":1,"jsonrpc":"2.0","result":0}
* \endenglish
* \chinese
* 关闭TCP/IP socket 通信
* 关闭与服务器的 socket 连接。
*
* 指令
*
* @param socket_name 套接字名称
* @return 返回值
*
* @par Python函数原型
* socketClose(self: pyaubo_sdk.Socket, arg0: str) -> int
*
* @par Lua函数原型
* socketClose(socket_name: string) -> nil
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"Socket.socketClose","params":["socket_0"],"id":1}
*
* @par JSON-RPC响应示例
* {"id":1,"jsonrpc":"2.0","result":0}
* \endchinese
*/
int socketClose(const std::string &socket_name = "socket_0");
/**
* @ingroup Socket
* \english
* Reads a number of ascii formatted floats from the socket. A maximum
* of 30 values can be read in one command.
* A list of numbers read (list of floats, length=number+1)
*
* Result will be stored in a register named reg_key. Use getFloatVec
* to retrieve data
*
* @param number
* @param variable
* @param socket_name
* @return
*
* @par Python function prototype
* socketReadAsciiFloat(self: pyaubo_sdk.Socket, arg0: int, arg1: str, arg2:
* str) -> int
*
* @par Lua function prototype
* socketReadAsciiFloat(number: number, variable: string, socket_name:
* string) -> number
* \endenglish
* \chinese
* 从socket读取指定数量的ASCII格式浮点数。一次最多可读取30个值。
* 读取到的数字列表(浮点数列表,长度=number+1)
*
* 结果将存储在名为reg_key的寄存器中。使用getFloatVec获取数据
*
* @param number 数量
* @param variable 变量名
* @param socket_name 套接字名称
* @return 返回值
*
* @par Python函数原型
* socketReadAsciiFloat(self: pyaubo_sdk.Socket, arg0: int, arg1: str, arg2:
* str) -> int
*
* @par Lua函数原型
* socketReadAsciiFloat(number: number, variable: string, socket_name:
* string) -> number
* \endchinese
*/
int socketReadAsciiFloat(int number, const std::string &variable,
const std::string &socket_name = "socket_0");
/**
* @ingroup Socket
* \english
* Reads a number of 32 bit integers from the socket. Bytes are in
* network byte order. A maximum of 30 values can be read in one
* command.
* A list of numbers read (list of ints, length=number+1)
*
* Instruction
*
* std::vector<int>
*
* @param number
* @param variable
* @param socket_name
* @return
*
* @par Python function prototype
* socketReadBinaryInteger(self: pyaubo_sdk.Socket, arg0: int, arg1: str,
* arg2: str) -> int
*
* @par Lua function prototype
* socketReadBinaryInteger(number: number, variable: string, socket_name:
* string) -> number
* \endenglish
* \chinese
* 从socket读取指定数量的32位整数。字节为网络字节序。一次最多可读取30个值。
* 读取到的数字列表(整数列表,长度=number+1)
*
* 指令
*
* std::vector<int>
*
* @param number 数量
* @param variable 变量名
* @param socket_name 套接字名称
* @return 返回值
*
* @par Python函数原型
* socketReadBinaryInteger(self: pyaubo_sdk.Socket, arg0: int, arg1: str,
* arg2: str) -> int
*
* @par Lua函数原型
* socketReadBinaryInteger(number: number, variable: string, socket_name:
* string) -> number
* \endchinese
*/
int socketReadBinaryInteger(int number, const std::string &variable,
const std::string &socket_name = "socket_0");
/**
* @ingroup Socket
* \english
* Reads a number of bytes from the socket. Bytes are in network byte
* order. A maximum of 30 values can be read in one command.
* A list of numbers read (list of ints, length=number+1)
*
* Instruction
*
* std::vector<char>
*
* @param number
* @param variable
* @param socket_name
* @return
*
* @par Python function prototype
* socketReadByteList(self: pyaubo_sdk.Socket, arg0: int, arg1: str, arg2:
* str) -> int
*
* @par Lua function prototype
* socketReadByteList(number: number, variable: string, socket_name: string)
* -> number
* \endenglish
* \chinese
* 从socket读取指定数量的字节。字节为网络字节序。一次最多可读取30个值。
* 读取到的数字列表(整数列表,长度=number+1)
*
* 指令
*
* std::vector<char>
*
* @param number 数量
* @param variable 变量名
* @param socket_name 套接字名称
* @return 返回值
*
* @par Python函数原型
* socketReadByteList(self: pyaubo_sdk.Socket, arg0: int, arg1: str, arg2:
* str) -> int
*
* @par Lua函数原型
* socketReadByteList(number: number, variable: string, socket_name: string)
* -> number
* \endchinese
*/
int socketReadByteList(int number, const std::string &variable,
const std::string &socket_name = "socket_0");
/**
* @ingroup Socket
* \english
* Reads all data from the socket and returns the data as a string.
* Bytes are in network byte order.
*
* The optional parameters "prefix" and "suffix", can be used to express
* what is extracted from the socket. The "prefix" specifies the start
* of the substring (message) extracted from the socket. The data up to
* the end of the "prefix" will be ignored and removed from the socket.
* The "suffix" specifies the end of the substring (message) extracted
* from the socket. Any remaining data on the socket, after the "suffix",
* will be preserved. E.g. if the socket server sends a string
* "noise>hello<", the controller can receive the "hello" by calling this
* script function with the prefix=">" and suffix="<". By using the
* "prefix" and "suffix" it is also possible send multiple string to the
* controller at once, because the suffix defines where the message ends.
* E.g. sending ">hello<>world<"
*
* Instruction
*
* std::string
*
* @param variable
* @param socket_name
* @param prefix
* @param suffix
* @param interpret_escape
* @return
*
* @par Python function prototype
* socketReadString(self: pyaubo_sdk.Socket, arg0: str, arg1: str, arg2:
* str, arg3: str, arg4: bool) -> int
*
* @par Lua function prototype
* socketReadString(variable: string, socket_name: string, prefix: string,
* suffix: string, interpret_escape: boolean) -> number
*
* @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"Socket.socketReadString","params":["camera","socket_0","","",false],"id":1}
*
* @par JSON-RPC response example
* {"id":1,"jsonrpc":"2.0","result":0}
* \endenglish
* \chinese
* 从socket读取所有数据并将其作为字符串返回。
* 字节为网络字节序。
*
* 可选参数"prefix"和"suffix"可用于指定从socket中提取的内容。
* "prefix"指定提取子字符串(消息)的起始位置。直到"prefix"结尾的数据将被忽略并从socket中移除。
* "suffix"指定提取子字符串(消息)的结束位置。"suffix"之后的任何剩余数据将保留在socket中。
* 例如,如果socket服务器发送字符串"noise>hello<",控制器可以通过调用此脚本函数并设置prefix=">"和suffix="<"来接收"hello"。
* 通过使用"prefix"和"suffix",还可以一次向控制器发送多条字符串,因为"suffix"定义了消息的结束位置。例如发送">hello<>world<"
*
* 指令
*
* std::string
*
* @param variable 变量名
* @param socket_name 套接字名称
* @param prefix 前缀
* @param suffix 后缀
* @param interpret_escape 是否解释转义字符
* @return 返回值
*
* @par Python函数原型
* socketReadString(self: pyaubo_sdk.Socket, arg0: str, arg1: str, arg2:
* str, arg3: str, arg4: bool) -> int
*
* @par Lua函数原型
* socketReadString(variable: string, socket_name: string, prefix: string,
* suffix: string, interpret_escape: boolean) -> number
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"Socket.socketReadString","params":["camera","socket_0","","",false],"id":1}
*
* @par JSON-RPC响应示例
* {"id":1,"jsonrpc":"2.0","result":0}
* \endchinese
*/
int socketReadString(const std::string &variable,
const std::string &socket_name = "socket_0",
const std::string &prefix = "",
const std::string &suffix = "",
bool interpret_escape = false);
/**
* @ingroup Socket
* \english
* Reads all data from the socket and returns the data as a vector of chars.
*
* Instruction
* std::vector<char>
*
* @param variable
* @param socket_name
* @return
*
* @par Python function prototype
* socketReadAllString(self: pyaubo_sdk.Socket, arg0: str, arg1: str) -> int
*
* @par Lua function prototype
* socketReadAllString(variable: string, socket_name: string) -> number
*
* @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"Socket.socketReadAllString","params":["camera","socket_0"],"id":1}
*
* @par JSON-RPC response example
* {"id":1,"jsonrpc":"2.0","result":0}
* \endenglish
* \chinese
* 从socket读取所有数据并将其作为char向量返回。
*
* 指令
* std::vector<char>
*
* @param variable 变量名
* @param socket_name 套接字名称
* @return 返回值
*
* @par Python函数原型
* socketReadAllString(self: pyaubo_sdk.Socket, arg0: str, arg1: str) -> int
*
* @par Lua函数原型
* socketReadAllString(variable: string, socket_name: string) -> number
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"Socket.socketReadAllString","params":["camera","socket_0"],"id":1}
*
* @par JSON-RPC响应示例
* {"id":1,"jsonrpc":"2.0","result":0}
* \endchinese
*/
int socketReadAllString(const std::string &variable,
const std::string &socket_name = "socket_0");
/**
* @ingroup Socket
* \english
* Sends a byte to the server
* Sends the byte <value> through the socket. Expects no response. Can
* be used to send special ASCII characters; 10 is newline, 2 is start of
* text, 3 is end of text.
*
* Instruction
*
* @param value
* @param socket_name
* @return
*
* @par Python function prototype
* socketSendByte(self: pyaubo_sdk.Socket, arg0: str, arg1: str) -> int
*
* @par Lua function prototype
* socketSendByte(value: string, socket_name: string) -> nil
*
* \endenglish
* \chinese
* 发送一个字节到服务器
* 通过socket发送字节<value>,不期望响应。可用于发送特殊ASCII字符;10为换行符,2为文本开始,3为文本结束。
*
* 指令
*
* @param value 字节值
* @param socket_name 套接字名称
* @return 返回值
*
* @par Python函数原型
* socketSendByte(self: pyaubo_sdk.Socket, arg0: str, arg1: str) -> int
*
* @par Lua函数原型
* socketSendByte(value: string, socket_name: string) -> nil
*
* \endchinese
*/
int socketSendByte(char value, const std::string &socket_name = "socket_0");
/**
* @ingroup Socket
* \english
* Sends an int (int32_t) to the server
* Sends the int <value> through the socket. Send in network byte order.
* Expects no response
*
* Instruction
*
* @param value
* @param socket_name
* @return
*
* @par Python function prototype
* socketSendInt(self: pyaubo_sdk.Socket, arg0: int, arg1: str) -> int
*
* @par Lua function prototype
* socketSendInt(value: number, socket_name: string) -> nil
*
* \endenglish
* \chinese
* 发送一个int(int32_t)到服务器
* 通过socket发送int <value>,以网络字节序发送。不期望响应。
*
* 指令
*
* @param value 整数值
* @param socket_name 套接字名称
* @return 返回值
*
* @par Python函数原型
* socketSendInt(self: pyaubo_sdk.Socket, arg0: int, arg1: str) -> int
*
* @par Lua函数原型
* socketSendInt(value: number, socket_name: string) -> nil
*
* \endchinese
*/
int socketSendInt(int value, const std::string &socket_name = "socket_0");
/**
* @ingroup Socket
* \english
* Sends a string with a newline character to the server
* Sends the string <str> through the socket in ASCII coding. Expects no
* response.
*
* Instruction
*
* @param str
* @param socket_name
* @return
*
* @par Python function prototype
* socketSendLine(self: pyaubo_sdk.Socket, arg0: str, arg1: str) -> int
*
* @par Lua function prototype
* socketSendLine(str: string, socket_name: string) -> nil
*
* @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"Socket.socketSendLine","params":["abcd","socket_0"],"id":1}
*
* @par JSON-RPC response example
* {"id":1,"jsonrpc":"2.0","result":0}
* \endenglish
* \chinese
* 发送带有换行符的字符串到服务器.
* 通过socket以ASCII编码发送字符串<str>,不期望响应。
*
* 指令
*
* @param str 字符串
* @param socket_name 套接字名称
* @return 返回值
*
* @par Python函数原型
* socketSendLine(self: pyaubo_sdk.Socket, arg0: str, arg1: str) -> int
*
* @par Lua函数原型
* socketSendLine(str: string, socket_name: string) -> nil
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"Socket.socketSendLine","params":["abcd","socket_0"],"id":1}
*
* @par JSON-RPC响应示例
* {"id":1,"jsonrpc":"2.0","result":0}
* \endchinese
*/
int socketSendLine(const std::string &str,
const std::string &socket_name = "socket_0");
/**
* @ingroup Socket
* \english
* Sends a string to the server
* Sends the string <str> through the socket in ASCII coding. Expects no
* response.
*
* Instruction
*
* @param str
* @param socket_name
* @return
*
* @par Python function prototype
* socketSendString(self: pyaubo_sdk.Socket, arg0: str, arg1: str) -> int
*
* @par Lua function prototype
* socketSendString(str: string, socket_name: string) -> nil
*
* @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"Socket.socketSendString","params":["abcd","socket_0"],"id":1}
*
* @par JSON-RPC response example
* {"id":1,"jsonrpc":"2.0","result":0}
* \endenglish
* \chinese
* 发送字符串到服务器
* 通过socket以ASCII编码发送字符串<str>,不期望响应。
*
* 指令
*
* @param str 字符串
* @param socket_name 套接字名称
* @return 返回值
*
* @par Python函数原型
* socketSendString(self: pyaubo_sdk.Socket, arg0: str, arg1: str) -> int
*
* @par Lua函数原型
* socketSendString(str: string, socket_name: string) -> nil
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"Socket.socketSendString","params":["abcd","socket_0"],"id":1}
*
* @par JSON-RPC响应示例
* {"id":1,"jsonrpc":"2.0","result":0}
* \endchinese
*/
int socketSendString(const std::string &str,
const std::string &socket_name = "socket_0");
/**
* @ingroup Socket
* \english
* Sends all data in the given vector of chars to the server.
*
* @param is_check Whether to check the sending status
* @param str The data to send as a vector of chars
* @param socket_name The name of the socket
* @return Status code
*
* @par Python function prototype
* socketSendAllString(self: pyaubo_sdk.Socket, arg0: bool, arg1: List[str], arg2: str) -> int
*
* @par Lua function prototype
* socketSendAllString(is_check: boolean, str: table, socket_name: string) -> nil
* \endenglish
* \chinese
* 发送给定char向量中的所有数据到服务器。
*
* @param is_check 是否检查发送状态
* @param str 要发送的数据,char向量
* @param socket_name 套接字名称
* @return 状态码
*
* @par Python函数原型
* socketSendAllString(self: pyaubo_sdk.Socket, arg0: bool, arg1: List[str], arg2: str) -> int
*
* @par Lua函数原型
* socketSendAllString(is_check: boolean, str: table, socket_name: string) -> nil
* \endchinese
*/
int socketSendAllString(bool is_check, const std::vector<char> &str,
const std::string &socket_name = "socket_0");
/**
* @ingroup Socket
* \english
* Check if the socket is connected
* @brief socketHasConnected
* @param socket_name
*
* @return
*
* @par Python function prototype
* socketHasConnected(self: pyaubo_sdk.Socket, arg0: str) -> bool
*
* @par Lua function prototype
* socketHasConnected(socket_name: string) -> boolean
* \endenglish
* \chinese
* 检测 socket 连接是否成功
* @brief socketHasConnected
* @param socket_name
*
* @return
*
* @par Python函数原型
* socketHasConnected(self: pyaubo_sdk.Socket, arg0: str) -> bool
*
* @par Lua函数原型
* socketHasConnected(socket_name: string) -> boolean
* \endchinese
*/
bool socketHasConnected(const std::string &socket_name = "socket_0");
protected:
void *d_;
};
using SocketPtr = std::shared_ptr<Socket>;
} // namespace common_interface
} // namespace arcs
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,359 @@
/** @file system_info.h
* @brief 获取系统信息接口,如接口板的版本号、示教器软件的版本号
*/
#ifndef AUBO_SDK_SYSTEM_INFO_INTERFACE_H
#define AUBO_SDK_SYSTEM_INFO_INTERFACE_H
#include <stdint.h>
#include <string>
#include <memory>
#include <aubo/global_config.h>
namespace arcs {
namespace common_interface {
/**
* \chinese
* @defgroup SystemInfo SystemInfo (系统信息)
* 系统信息
* \endchinese
*
* \english
* @defgroup SystemInfo System Information
* System Information
* \endenglish
*/
class ARCS_ABI_EXPORT SystemInfo
{
public:
SystemInfo();
virtual ~SystemInfo();
/**
* @ingroup SystemInfo
* \chinese
* 获取控制器软件版本号
*
* @return 返回控制器软件版本号
*
* @par Python函数原型
* getControlSoftwareVersionCode(self: pyaubo_sdk.SystemInfo) -> int
*
* @par Lua函数原型
* getControlSoftwareVersionCode() -> number
*
* @par C++示例
* @code
* int control_version =
* rpc_cli->getSystemInfo()->getControlSoftwareVersionCode();
* @endcode
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"SystemInfo.getControlSoftwareVersionCode","params":[],"id":1}
*
* @par JSON-RPC响应示例
* {"id":1,"jsonrpc":"2.0","result":28003}
*
* \endchinese
* \english
* Get the controller software version code
*
* @return Returns the controller software version code
*
* @par Python prototype
* getControlSoftwareVersionCode(self: pyaubo_sdk.SystemInfo) -> int
*
* @par Lua prototype
* getControlSoftwareVersionCode() -> number
*
* @par C++ example
* @code
* int control_version =
* rpc_cli->getSystemInfo()->getControlSoftwareVersionCode();
* @endcode
*
* @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"SystemInfo.getControlSoftwareVersionCode","params":[],"id":1}
*
* @par JSON-RPC response example
* {"id":1,"jsonrpc":"2.0","result":28003}
*
* \endenglish
*/
int getControlSoftwareVersionCode();
/**
* @ingroup SystemInfo
* \chinese
* 获取完整控制器软件版本号
*
* @return 返回完整控制器软件版本号
*
* @par Python函数原型
* getControlSoftwareFullVersion(self: pyaubo_sdk.SystemInfo) -> str
*
* @par Lua函数原型
* getControlSoftwareFullVersion() -> string
*
* @par C++示例
* @code
* std::string control_version =
* rpc_cli->getSystemInfo()->getControlSoftwareFullVersion();
* @endcode
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"SystemInfo.getControlSoftwareFullVersion","params":[],"id":1}
*
* @par JSON-RPC响应示例
* {"id":1,"jsonrpc":"2.0","result":"0.31.0-alpha.16+20alc76"}
*
* \endchinese
* \english
* Get the full controller software version
*
* @return Returns the full controller software version
*
* @par Python prototype
* getControlSoftwareFullVersion(self: pyaubo_sdk.SystemInfo) -> str
*
* @par Lua prototype
* getControlSoftwareFullVersion() -> string
*
* @par C++ example
* @code
* std::string control_version =
* rpc_cli->getSystemInfo()->getControlSoftwareFullVersion();
* @endcode
*
* @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"SystemInfo.getControlSoftwareFullVersion","params":[],"id":1}
*
* @par JSON-RPC response example
* {"id":1,"jsonrpc":"2.0","result":"0.31.0-alpha.16+20alc76"}
*
* \endenglish
*/
std::string getControlSoftwareFullVersion();
/**
* @ingroup SystemInfo
* \chinese
* 获取接口版本号
*
* @return 返回接口版本号
*
* @par Python函数原型
* getInterfaceVersionCode(self: pyaubo_sdk.SystemInfo) -> int
*
* @par Lua函数原型
* getInterfaceVersionCode() -> number
*
* @par C++示例
* @code
* int interface_version =
* rpc_cli->getSystemInfo()->getInterfaceVersionCode();
* @endcode
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"SystemInfo.getInterfaceVersionCode","params":[],"id":1}
*
* @par JSON-RPC响应示例
* {"id":1,"jsonrpc":"2.0","result":22003}
*
* \endchinese
* \english
* Get the interface version code
*
* @return Returns the interface version code
*
* @par Python prototype
* getInterfaceVersionCode(self: pyaubo_sdk.SystemInfo) -> int
*
* @par Lua prototype
* getInterfaceVersionCode() -> number
*
* @par C++ example
* @code
* int interface_version =
* rpc_cli->getSystemInfo()->getInterfaceVersionCode();
* @endcode
*
* @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"SystemInfo.getInterfaceVersionCode","params":[],"id":1}
*
* @par JSON-RPC response example
* {"id":1,"jsonrpc":"2.0","result":22003}
*
* \endenglish
*/
int getInterfaceVersionCode();
/**
* @ingroup SystemInfo
* \chinese
* 获取控制器软件构建时间
*
* @return 返回控制器软件构建时间
*
* @par Python函数原型
* getControlSoftwareBuildDate(self: pyaubo_sdk.SystemInfo) -> str
*
* @par Lua函数原型
* getControlSoftwareBuildDate() -> string
*
* @par C++示例
* @code
* std::string build_date =
* rpc_cli->getSystemInfo()->getControlSoftwareBuildDate();
* @endcode
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"SystemInfo.getControlSoftwareBuildDate","params":[],"id":1}
*
* @par JSON-RPC响应示例
* {"id":1,"jsonrpc":"2.0","result":"2024-3-5 07:03:20"}
*
* \endchinese
* \english
* Get the controller software build date
*
* @return Returns the controller software build date
*
* @par Python prototype
* getControlSoftwareBuildDate(self: pyaubo_sdk.SystemInfo) -> str
*
* @par Lua prototype
* getControlSoftwareBuildDate() -> string
*
* @par C++ example
* @code
* std::string build_date =
* rpc_cli->getSystemInfo()->getControlSoftwareBuildDate();
* @endcode
*
* @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"SystemInfo.getControlSoftwareBuildDate","params":[],"id":1}
*
* @par JSON-RPC response example
* {"id":1,"jsonrpc":"2.0","result":"2024-3-5 07:03:20"}
*
* \endenglish
*/
std::string getControlSoftwareBuildDate();
/**
* @ingroup SystemInfo
* \chinese
* 获取控制器软件git版本
*
* @return 返回控制器软件git版本
*
* @par Python函数原型
* getControlSoftwareVersionHash(self: pyaubo_sdk.SystemInfo) -> str
*
* @par Lua函数原型
* getControlSoftwareVersionHash() -> string
*
* @par C++示例
* @code
* std::string git_version =
* rpc_cli->getSystemInfo()->getControlSoftwareVersionHash();
* @endcode
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"SystemInfo.getControlSoftwareVersionHash","params":[],"id":1}
*
* @par JSON-RPC响应示例
* {"id":1,"jsonrpc":"2.0","result":"fa4f64a"}
*
* \endchinese
* \english
* Get the controller software git version
*
* @return Returns the controller software git version
*
* @par Python prototype
* getControlSoftwareVersionHash(self: pyaubo_sdk.SystemInfo) -> str
*
* @par Lua prototype
* getControlSoftwareVersionHash() -> string
*
* @par C++ example
* @code
* std::string git_version =
* rpc_cli->getSystemInfo()->getControlSoftwareVersionHash();
* @endcode
*
* @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"SystemInfo.getControlSoftwareVersionHash","params":[],"id":1}
*
* @par JSON-RPC response example
* {"id":1,"jsonrpc":"2.0","result":"fa4f64a"}
*
* \endenglish
*/
std::string getControlSoftwareVersionHash();
/**
* @ingroup SystemInfo
* \chinese
* 获取系统时间(软件启动时间 ns 纳秒)
*
* @return 返回系统时间(软件启动时间 ns 纳秒)
*
* @par Python函数原型
* getControlSystemTime(self: pyaubo_sdk.SystemInfo) -> int
*
* @par Lua函数原型
* getControlSystemTime() -> number
*
* @par C++示例
* @code
* std::string system_time =
* rpc_cli->getSystemInfo()->getControlSystemTime();
* @endcode
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"SystemInfo.getControlSystemTime","params":[],"id":1}
*
* @par JSON-RPC响应示例
* {"id":1,"jsonrpc":"2.0","result":9287799079682}
*
* \endchinese
* \english
* Get the system time (software start time in nanoseconds)
*
* @return Returns the system time (software start time in nanoseconds)
*
* @par Python prototype
* getControlSystemTime(self: pyaubo_sdk.SystemInfo) -> int
*
* @par Lua prototype
* getControlSystemTime() -> number
*
* @par C++ example
* @code
* std::string system_time =
* rpc_cli->getSystemInfo()->getControlSystemTime();
* @endcode
*
* @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"SystemInfo.getControlSystemTime","params":[],"id":1}
*
* @par JSON-RPC response example
* {"id":1,"jsonrpc":"2.0","result":9287799079682}
*
* \endenglish
*/
uint64_t getControlSystemTime();
protected:
void *d_;
};
using SystemInfoPtr = std::shared_ptr<SystemInfo>;
} // namespace common_interface
} // namespace arcs
#endif

View File

@ -0,0 +1,243 @@
/** @file trace.h
* \~chinese @brief 向控制器日志系统注入日志方面的接口 \~english @brief Interface for injecting logs into the controller's logging system
*/
#ifndef AUBO_SDK_TRACE_INTERFACE_H
#define AUBO_SDK_TRACE_INTERFACE_H
#include <string>
#include <vector>
#include <memory>
#include <sstream>
#include <aubo/global_config.h>
#include <aubo/type_def.h>
namespace arcs {
namespace common_interface {
/**
* \chinese
* @defgroup Trace Trace (日志与弹窗)
* 提供给控制器扩展程序的日志记录系统
* \endchinese
*
* \english
* @defgroup Trace Log and Pop-up
* Log recording system for controller extension programs
* \endenglish
*/
class ARCS_ABI_EXPORT Trace
{
public:
Trace();
virtual ~Trace();
/**
* @ingroup Trace
* \~chinese 向 aubo_control 日志注入告警信息 \~english Injects alarm information into the aubo_control log
*
* TraceLevel: \n
* 0 - FATAL \n
* 1 - ERROR \n
* 2 - WARNING \n
* 3 - INFO \n
* 4 - DEBUG \n
*
* \~chinese code定义参考 error_stack \~english Code definitions refer to error_stack
*
* @param level
* @param code
* @param args
* @return
*
* \~chinese @par Python函数原型 \~english @par Python function prototype
* alarm(self: pyaubo_sdk.Trace, arg0: arcs::common_interface::TraceLevel,
* arg1: int, arg2: List[str]) -> int
*
* \~chinese @par Lua函数原型 \~english @par Lua function prototype
* alarm(level: number, code: number, args: table) -> nil
*
* \~chinese @par JSON-RPC请求示例 \~english @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"rob1.Trace.alarm","params":["",1,["Error","Trajectory
* planning failed!","1"]],"id":1}
*
* \~chinese @par JSON-RPC响应示例 \~engish @par JSON-RPC response example
* {"id":1,"jsonrpc":"2.0","result":0}
*
*/
int alarm(TraceLevel level, int code,
const std::vector<std::string> &args = {});
/**
* @ingroup Trace
* \chinese
* 打印文本信息到日志中
*
* @param msg 文本信息
* @return
*
* @par Python函数原型
* textmsg(self: pyaubo_sdk.Trace, arg0: str) -> int
*
* @par Lua函数原型
* textmsg(msg: string) -> nil
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"rob1.Trace.textmsg","params":["test"],"id":1}
*
* @par JSON-RPC响应示例
* {"id":1,"jsonrpc":"2.0","result":0}
* \endchinese
* \english
* print message into log
*
* @param msg message information
* @return
*
* @par Python function prototype
* textmsg(self: pyaubo_sdk.Trace, arg0: str) -> int
*
* @par Lua function prototype
* textmsg(msg: string) -> nil
*
* @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"rob1.Trace.textmsg","params":["test"],"id":1}
*
* @par JSON-RPC responose example
* {"id":1,"jsonrpc":"2.0","result":0}
* \endenglish
*/
int textmsg(const std::string &msg);
/**
* \~chinese 通知上位机 \~english Notify the system
*
* @param msg
* @return
*/
int notify(const std::string &msg);
/**
* @ingroup Trace
* \chinese
* 向连接的 RTDE 客户端发送弹窗请求
*
* @param level
* @param title
* @param msg
* @param mode 模式 \n
* 0: 普通模式 \n
* 1: 阻塞模式 \n
* 2: 输入模式 bool \n
* 3: 输入模式 int \n
* 4: 输入模式 double \n
* 5: 输入模式 string \n
* @return
*
* @par Python函数原型
* popup(self: pyaubo_sdk.Trace, arg0: arcs::common_interface::TraceLevel,
* arg1: str, arg2: str, arg3: int) -> int
*
* @par Lua函数原型
* popup(level: number, title: string, msg: string, mode: number) -> nil
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"rob1.Trace.popup","params":["","Error","Trajectory
* planning failed!",1],"id":1}
*
* @par JSON-RPC响应示例
* {"id":1,"jsonrpc":"2.0","result":0}
* \endchinese
* \english
* Send a popup request to the connected RTDE client
*
* @param level
* @param title
* @param msg
* @param mode mode \n
* 0: normal mode \n
* 1: blocking mode \n
* 2: input mode bool \n
* 3: input mode int \n
* 4: input mode double \n
* 5: input mode string \n
* @return
*
* @par Python function prototype
* popup(self: pyaubo_sdk.Trace, arg0: arcs::common_interface::TraceLevel,
* arg1: str, arg2: str, arg3: int) -> int
*
* @par Lua function prototype
* popup(level: number, title: string, msg: string, mode: number) -> nil
*
* @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"rob1.Trace.popup","params":["","Error","Trajectory
* planning failed!",1],"id":1}
*
* @par JSON-RPC response example
* {"id":1,"jsonrpc":"2.0","result":0}
* \endenglish
*/
int popup(TraceLevel level, const std::string &title,
const std::string &msg, int mode);
/**
* @ingroup Trace
* \chinese
* peek最新的 AlarmInfo(上次一获取之后)
*
* last_time设置为0时,可以获取到所有的AlarmInfo
*
* @param num
* @param last_time
* @return
*
* @par Python函数原型
* peek(self: pyaubo_sdk.Trace, arg0: int, arg1: int) ->
* List[arcs::common_interface::RobotMsg]
*
* @par Lua函数原型
* peek(num: number, last_time: number) -> table
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"rob1.Trace.peek","params":[1,0],"id":1}
*
* @par JSON-RPC响应示例
* {{"id":1,"jsonrpc":"2.0","result":[{"args":["RobotModeType.Running"],
* "code":30045,"level":"INFO","source":"rob1","timestamp":5102883064300}]}
* \endchinese
* \english
* peek the latest AlarmInfo (after the last retrieval)
*
* When last_time is set as 0, retrieve all AlarmInfo
*
* @param num
* @param last_time
* @return
*
* @par Python function prototype
* peek(self: pyaubo_sdk.Trace, arg0: int, arg1: int) ->
* List[arcs::common_interface::RobotMsg]
*
* @par Lua function prototype
* peek(num: number, last_time: number) -> table
*
* @par JSON-RPC request example
* {"jsonrpc":"2.0","method":"rob1.Trace.peek","params":[1,0],"id":1}
*
* @par JSON-RPC response example
* {{"id":1,"jsonrpc":"2.0","result":[{"args":["RobotModeType.Running"],
* "code":30045,"level":"INFO","source":"rob1","timestamp":5102883064300}]}
* \endenglish
*/
RobotMsgVector peek(size_t num, uint64_t last_time = 0);
protected:
void *d_;
};
using TracePtr = std::shared_ptr<Trace>;
} // namespace common_interface
} // namespace arcs
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,53 @@
#ifndef AUBO_SDK_Math_C_H
#define AUBO_SDK_Math_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI int poseAdd(MATH_HANDLER h, const double *p1, const double *p2,
double *result);
ARCS_ABI int poseSub(MATH_HANDLER h, const double *p1, const double *p2,
double *result);
ARCS_ABI int interpolatePose(MATH_HANDLER h, const double *p1, const double *p2,
double alpha, double *result);
ARCS_ABI int poseTrans(MATH_HANDLER h, const double *pose_from,
const double *pose_from_to, double *result);
ARCS_ABI int poseTransInv(MATH_HANDLER h, const double *pose_from,
const double *pose_to_from, double *result);
ARCS_ABI int poseInverse(MATH_HANDLER h, const double *pose, double *result);
ARCS_ABI double poseDistance(MATH_HANDLER h, const double *p1,
const double *p2);
ARCS_ABI double poseAngleDistance(MATH_HANDLER h, const double *p1,
const double *p2);
ARCS_ABI BOOL poseEqual(MATH_HANDLER h, const double *p1, const double *p2,
double eps);
ARCS_ABI int transferRefFrame(MATH_HANDLER h, const double *F_b_a_old,
Vector3d_C V_in_a, int type, double *result);
ARCS_ABI int poseRotation(MATH_HANDLER h, const double *pose,
const double *rotv, double *result);
ARCS_ABI int rpyToQuaternion(MATH_HANDLER h, const double *rpy, double *result);
ARCS_ABI int quaternionToRpy(MATH_HANDLER h, const double *quant,
double *result);
ARCS_ABI int tcpOffsetIdentify(MATH_HANDLER h, const double *poses, int rows,
double *result);
ARCS_ABI int calibrateCoordinate(MATH_HANDLER h, const double *poses, int rows,
int type, double *result);
ARCS_ABI int calculateCircleFourthPoint(MATH_HANDLER h, const double *p1,
const double *p2, const double *p3,
int mode, double *result);
ARCS_ABI int forceTrans(MATH_HANDLER h, const double *pose_a_in_b,
const double *force_in_a, double *result);
ARCS_ABI int getDeltaPoseBySensorDistance(MATH_HANDLER h,
const double *distances,
double position, double radius,
double track_scale, double *result);
ARCS_ABI int deltaPoseTrans(MATH_HANDLER h, const double *pose_a_in_b,
const double *ft_in_a, double *result);
ARCS_ABI int deltaPoseAdd(MATH_HANDLER h, const double *pose_a_in_b,
const double *v_in_b, double *result);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,120 @@
#ifndef AUBO_SDK_RegisterControl_C_H
#define AUBO_SDK_RegisterControl_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI BOOL getBoolInput(REGISTER_CONTROL_HANDLER h, uint32_t address);
ARCS_ABI int setBoolInput(REGISTER_CONTROL_HANDLER h, uint32_t address,
BOOL value);
ARCS_ABI int getInt32Input(REGISTER_CONTROL_HANDLER h, uint32_t address);
ARCS_ABI int setInt32Input(REGISTER_CONTROL_HANDLER h, uint32_t address,
int value);
ARCS_ABI float getFloatInput(REGISTER_CONTROL_HANDLER h, uint32_t address);
ARCS_ABI int setFloatInput(REGISTER_CONTROL_HANDLER h, uint32_t address,
float value);
ARCS_ABI double getDoubleInput(REGISTER_CONTROL_HANDLER h, uint32_t address);
ARCS_ABI int setDoubleInput(REGISTER_CONTROL_HANDLER h, uint32_t address,
double value);
ARCS_ABI BOOL getBoolOutput(REGISTER_CONTROL_HANDLER h, uint32_t address);
ARCS_ABI int setBoolOutput(REGISTER_CONTROL_HANDLER h, uint32_t address,
BOOL value);
ARCS_ABI int getInt32Output(REGISTER_CONTROL_HANDLER h, uint32_t address);
ARCS_ABI int setInt32Output(REGISTER_CONTROL_HANDLER h, uint32_t address,
int value);
ARCS_ABI float getFloatOutput(REGISTER_CONTROL_HANDLER h, uint32_t address);
ARCS_ABI int setFloatOutput(REGISTER_CONTROL_HANDLER h, uint32_t address,
float value);
ARCS_ABI double getDoubleOutput(REGISTER_CONTROL_HANDLER h, uint32_t address);
ARCS_ABI int setDoubleOutput(REGISTER_CONTROL_HANDLER h, uint32_t address,
double value);
ARCS_ABI int16_t getInt16Register(REGISTER_CONTROL_HANDLER h, uint32_t address);
ARCS_ABI int setInt16Register(REGISTER_CONTROL_HANDLER h, uint32_t address,
int16_t value);
ARCS_ABI BOOL variableUpdated(REGISTER_CONTROL_HANDLER h, const char *key,
uint64_t since);
ARCS_ABI BOOL hasNamedVariable(REGISTER_CONTROL_HANDLER h, const char *key);
ARCS_ABI int getNamedVariableType(REGISTER_CONTROL_HANDLER h, const char *key,
char *result);
ARCS_ABI BOOL getBool(REGISTER_CONTROL_HANDLER h, const char *key,
BOOL default_value);
ARCS_ABI int setBool(REGISTER_CONTROL_HANDLER h, const char *key, BOOL value);
ARCS_ABI int getVecChar(REGISTER_CONTROL_HANDLER h, const char *key,
const char *default_value, char *result, int sz);
ARCS_ABI int setVecChar(REGISTER_CONTROL_HANDLER h, const char *key,
const char *value, int sz);
ARCS_ABI int getInt32(REGISTER_CONTROL_HANDLER h, const char *key,
int default_value);
ARCS_ABI int setInt32(REGISTER_CONTROL_HANDLER h, const char *key, int value);
ARCS_ABI int getVecInt32(REGISTER_CONTROL_HANDLER h, const char *key,
int32_t *default_value, int *result);
ARCS_ABI int setVecInt32(REGISTER_CONTROL_HANDLER h, const char *key,
int32_t *value);
ARCS_ABI float getFloat(REGISTER_CONTROL_HANDLER h, const char *key,
float default_value);
ARCS_ABI int setFloat(REGISTER_CONTROL_HANDLER h, const char *key, float value);
ARCS_ABI int getVecFloat(REGISTER_CONTROL_HANDLER h, const char *key,
const float *default_value, float *result);
ARCS_ABI int setVecFloat(REGISTER_CONTROL_HANDLER h, const char *key,
const float *value);
ARCS_ABI double getDouble(REGISTER_CONTROL_HANDLER h, const char *key,
double default_value);
ARCS_ABI int setDouble(REGISTER_CONTROL_HANDLER h, const char *key,
double value);
ARCS_ABI int getVecDouble(REGISTER_CONTROL_HANDLER h, const char *key,
const double *default_value, double *result);
ARCS_ABI int setVecDouble(REGISTER_CONTROL_HANDLER h, const char *key,
const double *value);
ARCS_ABI int getString(REGISTER_CONTROL_HANDLER h, const char *key,
const char *default_value, char *result);
ARCS_ABI int setString(REGISTER_CONTROL_HANDLER h, const char *key,
const char *value);
ARCS_ABI int clearNamedVariable(REGISTER_CONTROL_HANDLER h, const char *key);
ARCS_ABI int setWatchDog(REGISTER_CONTROL_HANDLER h, const char *key,
double timeout, int action);
ARCS_ABI int getWatchDogAction(REGISTER_CONTROL_HANDLER h, const char *key);
ARCS_ABI int getWatchDogTimeout(REGISTER_CONTROL_HANDLER h, const char *key);
ARCS_ABI int modbusAddSignal(REGISTER_CONTROL_HANDLER h,
const char *device_info, int slave_number,
int signal_address, int signal_type,
const char *signal_name, BOOL sequential_mode);
ARCS_ABI int modbusDeleteSignal(REGISTER_CONTROL_HANDLER h,
const char *signal_name);
ARCS_ABI int modbusDeleteAllSignals(REGISTER_CONTROL_HANDLER h);
ARCS_ABI int modbusGetSignalStatus(REGISTER_CONTROL_HANDLER h,
const char *signal_name);
ARCS_ABI int modbusGetSignalNames(REGISTER_CONTROL_HANDLER h, char **result);
ARCS_ABI int modbusGetSignalTypes(REGISTER_CONTROL_HANDLER h, int *result);
ARCS_ABI int modbusGetSignalValues(REGISTER_CONTROL_HANDLER h, int *result);
ARCS_ABI int modbusGetSignalErrors(REGISTER_CONTROL_HANDLER h, int *result);
ARCS_ABI int modbusSendCustomCommand(REGISTER_CONTROL_HANDLER h, const char *IP,
int slave_number, int function_code,
uint8_t *data, int sz);
ARCS_ABI int modbusSetDigitalInputAction(REGISTER_CONTROL_HANDLER h,
const char *robot_name,
const char *signal_name,
StandardInputAction_C action);
ARCS_ABI int modbusSetOutputRunstate(REGISTER_CONTROL_HANDLER h,
const char *robot_name,
const char *signal_name,
StandardOutputRunState_C runstate);
ARCS_ABI int modbusSetOutputSignal(REGISTER_CONTROL_HANDLER h,
const char *signal_name, uint16_t value);
ARCS_ABI int modbusSetOutputSignalPulse(REGISTER_CONTROL_HANDLER h,
const char *signal_name, uint16_t value,
double duration);
ARCS_ABI int modbusSetSignalUpdateFrequency(REGISTER_CONTROL_HANDLER h,
const char *signal_name,
int update_frequency);
ARCS_ABI int modbusGetSignalIndex(REGISTER_CONTROL_HANDLER h,
const char *signal_name);
ARCS_ABI int modbusGetSignalError(REGISTER_CONTROL_HANDLER h,
const char *signal_name);
ARCS_ABI int getModbusDeviceStatus(REGISTER_CONTROL_HANDLER h,
const char *device_name);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,78 @@
#ifndef AUBO_SDK_ForceControl_C_H
#define AUBO_SDK_ForceControl_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI int fcEnable(FORCE_CONTROL_HANDLER h);
ARCS_ABI int fcDisable(FORCE_CONTROL_HANDLER h);
ARCS_ABI BOOL isFcEnabled(FORCE_CONTROL_HANDLER h);
ARCS_ABI int setTargetForce(FORCE_CONTROL_HANDLER h, const double *feature,
const uint8_t *compliance, const double *wrench,
const double *limits, TaskFrameType_C type);
ARCS_ABI int setDynamicModel(FORCE_CONTROL_HANDLER h, const double *m,
const double *d, const double *k);
ARCS_ABI DynamicsModel_C getDynamicModel(FORCE_CONTROL_HANDLER h);
ARCS_ABI int setDynamicModelContact(FORCE_CONTROL_HANDLER h,
const double *env_stiff,
const double *damp_scale,
const double *stiff_scale);
ARCS_ABI int setCondForce(FORCE_CONTROL_HANDLER h, const double *min,
const double *max, BOOL outside, double timeout);
ARCS_ABI int setCondOrient(FORCE_CONTROL_HANDLER h, const double *frame,
double max_angle, double max_rot, BOOL outside,
double timeout);
ARCS_ABI int setCondPlane(FORCE_CONTROL_HANDLER h, const double *plane,
double timeout);
ARCS_ABI int setCondCylinder(FORCE_CONTROL_HANDLER h, const double *axis,
double radius, BOOL outside, double timeout);
ARCS_ABI int setCondSphere(FORCE_CONTROL_HANDLER h, const double *center,
double radius, BOOL outside, double timeout);
ARCS_ABI int setCondTcpSpeed(FORCE_CONTROL_HANDLER h, const double *min,
const double *max, BOOL outside, double timeout);
ARCS_ABI int setCondActive(FORCE_CONTROL_HANDLER h);
ARCS_ABI int setCondDistance(FORCE_CONTROL_HANDLER h, double distance,
double timeout);
ARCS_ABI int setCondAdvanced(FORCE_CONTROL_HANDLER h, const char *type,
const double *args, double timeout);
ARCS_ABI BOOL isCondFullfiled(FORCE_CONTROL_HANDLER h);
ARCS_ABI int setSupvForce(FORCE_CONTROL_HANDLER h, const double *min,
const double *max);
ARCS_ABI int setSupvOrient(FORCE_CONTROL_HANDLER h, const double *frame,
double max_angle, double max_rot, BOOL outside);
ARCS_ABI int setSupvPosBox(FORCE_CONTROL_HANDLER h, const double *frame,
const double *box);
ARCS_ABI int setSupvPosCylinder(FORCE_CONTROL_HANDLER h, const double *frame,
const double *cylinder);
ARCS_ABI int setSupvPosSphere(FORCE_CONTROL_HANDLER h, const double *frame,
const double *sphere);
ARCS_ABI int setSupvReoriSpeed(FORCE_CONTROL_HANDLER h,
const double *speed_limit, BOOL outside,
double timeout);
ARCS_ABI int setSupvTcpSpeed(FORCE_CONTROL_HANDLER h, const double *speed_limit,
BOOL outside, double timeout);
ARCS_ABI int setLpFilter(FORCE_CONTROL_HANDLER h, const double *cutoff_freq);
ARCS_ABI int resetLpFilter(FORCE_CONTROL_HANDLER h);
ARCS_ABI int speedChangeTune(FORCE_CONTROL_HANDLER h, int speed_levels,
double speed_ratio_min);
ARCS_ABI int speedChangeEnable(FORCE_CONTROL_HANDLER h, double ref_force);
ARCS_ABI int speedChangeDisable(FORCE_CONTROL_HANDLER h);
ARCS_ABI int setDamping(FORCE_CONTROL_HANDLER h, const double *damping,
double ramp_time);
ARCS_ABI int resetDamping(FORCE_CONTROL_HANDLER h);
ARCS_ABI int softFloatEnable(FORCE_CONTROL_HANDLER h);
ARCS_ABI int softFloatDisable(FORCE_CONTROL_HANDLER h);
ARCS_ABI BOOL isSoftFloatEnabled(FORCE_CONTROL_HANDLER h);
ARCS_ABI int setSoftFloatParams(FORCE_CONTROL_HANDLER h, BOOL joint_space,
const uint8_t *select,
const double *stiff_percent,
const double *stiff_damp_ratio,
const double *force_threshold,
const double *force_limit);
ARCS_ABI int toolContact(FORCE_CONTROL_HANDLER h, const uint8_t *direction);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,113 @@
#ifndef AUBO_SDK_IoControl_C_H
#define AUBO_SDK_IoControl_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI int getStandardDigitalInputNum(IO_CONTROL_HANDLER h);
ARCS_ABI int getToolDigitalInputNum(IO_CONTROL_HANDLER h);
ARCS_ABI int getConfigurableDigitalInputNum(IO_CONTROL_HANDLER h);
ARCS_ABI int getStandardDigitalOutputNum(IO_CONTROL_HANDLER h);
ARCS_ABI int getToolDigitalOutputNum(IO_CONTROL_HANDLER h);
ARCS_ABI int setToolIoInput(IO_CONTROL_HANDLER h, int index, BOOL input);
ARCS_ABI BOOL isToolIoInput(IO_CONTROL_HANDLER h, int index);
ARCS_ABI int getConfigurableDigitalOutputNum(IO_CONTROL_HANDLER h);
ARCS_ABI int getStandardAnalogInputNum(IO_CONTROL_HANDLER h);
ARCS_ABI int getToolAnalogInputNum(IO_CONTROL_HANDLER h);
ARCS_ABI int getStandardAnalogOutputNum(IO_CONTROL_HANDLER h);
ARCS_ABI int getToolAnalogOutputNum(IO_CONTROL_HANDLER h);
ARCS_ABI int setDigitalInputActionDefault(IO_CONTROL_HANDLER h);
ARCS_ABI int setStandardDigitalInputAction(IO_CONTROL_HANDLER h, int index,
StandardInputAction_C action);
ARCS_ABI int setToolDigitalInputAction(IO_CONTROL_HANDLER h, int index,
StandardInputAction_C action);
ARCS_ABI int setConfigurableDigitalInputAction(IO_CONTROL_HANDLER h, int index,
StandardInputAction_C action);
ARCS_ABI StandardInputAction_C
getStandardDigitalInputAction(IO_CONTROL_HANDLER h, int index);
ARCS_ABI StandardInputAction_C getToolDigitalInputAction(IO_CONTROL_HANDLER h,
int index);
ARCS_ABI StandardInputAction_C
getConfigurableDigitalInputAction(IO_CONTROL_HANDLER h, int index);
ARCS_ABI int setDigitalOutputRunstateDefault(IO_CONTROL_HANDLER h);
ARCS_ABI int setStandardDigitalOutputRunstate(
IO_CONTROL_HANDLER h, int index, StandardOutputRunState_C runstate);
ARCS_ABI int setToolDigitalOutputRunstate(IO_CONTROL_HANDLER h, int index,
StandardOutputRunState_C runstate);
ARCS_ABI int setConfigurableDigitalOutputRunstate(
IO_CONTROL_HANDLER h, int index, StandardOutputRunState_C runstate);
ARCS_ABI StandardOutputRunState_C
getStandardDigitalOutputRunstate(IO_CONTROL_HANDLER h, int index);
ARCS_ABI StandardOutputRunState_C
getToolDigitalOutputRunstate(IO_CONTROL_HANDLER h, int index);
ARCS_ABI StandardOutputRunState_C
getConfigurableDigitalOutputRunstate(IO_CONTROL_HANDLER h, int index);
ARCS_ABI int setStandardAnalogOutputRunstate(IO_CONTROL_HANDLER h, int index,
StandardOutputRunState_C runstate);
ARCS_ABI int setToolAnalogOutputRunstate(IO_CONTROL_HANDLER h, int index,
StandardOutputRunState_C runstate);
ARCS_ABI StandardOutputRunState_C
getStandardAnalogOutputRunstate(IO_CONTROL_HANDLER h, int index);
ARCS_ABI StandardOutputRunState_C
getToolAnalogOutputRunstate(IO_CONTROL_HANDLER h, int index);
ARCS_ABI int setStandardAnalogInputDomain(IO_CONTROL_HANDLER h, int index,
int domain);
ARCS_ABI int setToolAnalogInputDomain(IO_CONTROL_HANDLER h, int index,
int domain);
ARCS_ABI int getStandardAnalogInputDomain(IO_CONTROL_HANDLER h, int index);
ARCS_ABI int getToolAnalogInputDomain(IO_CONTROL_HANDLER h, int index);
ARCS_ABI int setStandardAnalogOutputDomain(IO_CONTROL_HANDLER h, int index,
int domain);
ARCS_ABI int setToolAnalogOutputDomain(IO_CONTROL_HANDLER h, int index,
int domain);
ARCS_ABI int setToolVoltageOutputDomain(IO_CONTROL_HANDLER h, int domain);
ARCS_ABI int getToolVoltageOutputDomain(IO_CONTROL_HANDLER h);
ARCS_ABI int getStandardAnalogOutputDomain(IO_CONTROL_HANDLER h, int index);
ARCS_ABI int getToolAnalogOutputDomain(IO_CONTROL_HANDLER h, int index);
ARCS_ABI int setStandardDigitalOutput(IO_CONTROL_HANDLER h, int index,
BOOL value);
ARCS_ABI int setStandardDigitalOutputPulse(IO_CONTROL_HANDLER h, int index,
BOOL value, double duration);
ARCS_ABI int setToolDigitalOutput(IO_CONTROL_HANDLER h, int index, BOOL value);
ARCS_ABI int setToolDigitalOutputPulse(IO_CONTROL_HANDLER h, int index,
BOOL value, double duration);
ARCS_ABI int setConfigurableDigitalOutput(IO_CONTROL_HANDLER h, int index,
BOOL value);
ARCS_ABI int setConfigurableDigitalOutputPulse(IO_CONTROL_HANDLER h, int index,
BOOL value, double duration);
ARCS_ABI int setStandardAnalogOutput(IO_CONTROL_HANDLER h, int index,
double value);
ARCS_ABI int setToolAnalogOutput(IO_CONTROL_HANDLER h, int index, double value);
ARCS_ABI BOOL getStandardDigitalInput(IO_CONTROL_HANDLER h, int index);
ARCS_ABI uint32_t getStandardDigitalInputs(IO_CONTROL_HANDLER h);
ARCS_ABI BOOL getToolDigitalInput(IO_CONTROL_HANDLER h, int index);
ARCS_ABI uint32_t getToolDigitalInputs(IO_CONTROL_HANDLER h);
ARCS_ABI BOOL getConfigurableDigitalInput(IO_CONTROL_HANDLER h, int index);
ARCS_ABI uint32_t getConfigurableDigitalInputs(IO_CONTROL_HANDLER h);
ARCS_ABI double getStandardAnalogInput(IO_CONTROL_HANDLER h, int index);
ARCS_ABI double getToolAnalogInput(IO_CONTROL_HANDLER h, int index);
ARCS_ABI BOOL getStandardDigitalOutput(IO_CONTROL_HANDLER h, int index);
ARCS_ABI uint32_t getStandardDigitalOutputs(IO_CONTROL_HANDLER h);
ARCS_ABI BOOL getToolDigitalOutput(IO_CONTROL_HANDLER h, int index);
ARCS_ABI uint32_t getToolDigitalOutputs(IO_CONTROL_HANDLER h);
ARCS_ABI BOOL getConfigurableDigitalOutput(IO_CONTROL_HANDLER h, int index);
ARCS_ABI uint32_t getConfigurableDigitalOutputs(IO_CONTROL_HANDLER h);
ARCS_ABI double getStandardAnalogOutput(IO_CONTROL_HANDLER h, int index);
ARCS_ABI double getToolAnalogOutput(IO_CONTROL_HANDLER h, int index);
ARCS_ABI int getStaticLinkInputNum(IO_CONTROL_HANDLER h);
ARCS_ABI int getStaticLinkOutputNum(IO_CONTROL_HANDLER h);
ARCS_ABI uint32_t getStaticLinkInputs(IO_CONTROL_HANDLER h);
ARCS_ABI uint32_t getStaticLinkOutputs(IO_CONTROL_HANDLER h);
ARCS_ABI BOOL hasEncoderSensor(IO_CONTROL_HANDLER h);
ARCS_ABI int setEncDecoderType(IO_CONTROL_HANDLER h, int type, int range_id);
ARCS_ABI int setEncTickCount(IO_CONTROL_HANDLER h, int tick);
ARCS_ABI int getEncDecoderType(IO_CONTROL_HANDLER h);
ARCS_ABI int getEncTickCount(IO_CONTROL_HANDLER h);
ARCS_ABI int unwindEncDeltaTickCount(IO_CONTROL_HANDLER h, int delta_count);
ARCS_ABI BOOL getToolButtonStatus(IO_CONTROL_HANDLER h);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,157 @@
#ifndef AUBO_SDK_MotionControl_C_H
#define AUBO_SDK_MotionControl_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI double getEqradius(MOTION_CONTROL_HANDLER h);
ARCS_ABI int setEqradius(MOTION_CONTROL_HANDLER h, double eqradius);
ARCS_ABI double getSpeedFraction(MOTION_CONTROL_HANDLER h);
ARCS_ABI int setSpeedFraction(MOTION_CONTROL_HANDLER h, double fraction);
ARCS_ABI int speedFractionCritical(MOTION_CONTROL_HANDLER h, BOOL enable);
ARCS_ABI BOOL isSpeedFractionCritical(MOTION_CONTROL_HANDLER h);
ARCS_ABI BOOL isBlending(MOTION_CONTROL_HANDLER h);
ARCS_ABI int pathOffsetEnable(MOTION_CONTROL_HANDLER h);
ARCS_ABI int pathOffsetSet(MOTION_CONTROL_HANDLER h, const double *offset,
int type);
ARCS_ABI int pathOffsetDisable(MOTION_CONTROL_HANDLER h);
ARCS_ABI int jointOffsetEnable(MOTION_CONTROL_HANDLER h);
ARCS_ABI int jointOffsetSet(MOTION_CONTROL_HANDLER h, const double *offset,
int type);
ARCS_ABI int jointOffsetDisable(MOTION_CONTROL_HANDLER h);
ARCS_ABI int getTrajectoryQueueSize(MOTION_CONTROL_HANDLER h);
ARCS_ABI int getQueueSize(MOTION_CONTROL_HANDLER h);
ARCS_ABI int getExecId(MOTION_CONTROL_HANDLER h);
ARCS_ABI double getDuration(MOTION_CONTROL_HANDLER h, int id);
ARCS_ABI double getMotionLeftTime(MOTION_CONTROL_HANDLER h, int id);
ARCS_ABI double getProgress(MOTION_CONTROL_HANDLER h);
ARCS_ABI int setWorkObjectHold(MOTION_CONTROL_HANDLER h,
const char *module_name,
const double *mounting_pose);
ARCS_ABI char *getWorkObjectHold(MOTION_CONTROL_HANDLER h);
ARCS_ABI int getPauseJointPositions(MOTION_CONTROL_HANDLER h, double *result);
ARCS_ABI int setServoMode(MOTION_CONTROL_HANDLER h, BOOL enable);
ARCS_ABI BOOL isServoModeEnabled(MOTION_CONTROL_HANDLER h);
ARCS_ABI int setServoModeSelect(MOTION_CONTROL_HANDLER h, int mode);
ARCS_ABI int getServoModeSelect(MOTION_CONTROL_HANDLER h);
ARCS_ABI int servoJoint(MOTION_CONTROL_HANDLER h, const double *q, double a,
double v, double t, double lookahead_time, double gain);
ARCS_ABI int servoCartesian(MOTION_CONTROL_HANDLER h, const double *pose,
double a, double v, double t, double lookahead_time,
double gain);
ARCS_ABI int servoJointWithAxes(MOTION_CONTROL_HANDLER h, const double *q,
const double *extq, double a, double v,
double t, double lookahead_time, double gain);
ARCS_ABI int servoCartesianWithAxes(MOTION_CONTROL_HANDLER h,
const double *pose, const double *extq,
double a, double v, double t,
double lookahead_time, double gain);
ARCS_ABI int trackJoint(MOTION_CONTROL_HANDLER h, const double *q, double t,
double smooth_scale, double delay_sacle);
ARCS_ABI int trackCartesian(MOTION_CONTROL_HANDLER h, const double *pose,
double t, double smooth_scale, double delay_sacle);
ARCS_ABI int followJoint(MOTION_CONTROL_HANDLER h, const double *q);
ARCS_ABI int followLine(MOTION_CONTROL_HANDLER h, const double *pose);
ARCS_ABI int speedJoint(MOTION_CONTROL_HANDLER h, const double *qd, double a,
double t);
ARCS_ABI int resumeSpeedJoint(MOTION_CONTROL_HANDLER h, const double *qd,
double a, double t);
ARCS_ABI int speedLine(MOTION_CONTROL_HANDLER h, const double *xd, double a,
double t);
ARCS_ABI int resumeSpeedLine(MOTION_CONTROL_HANDLER h, const double *xd,
double a, double t);
ARCS_ABI int moveSpline(MOTION_CONTROL_HANDLER h, const double *q, double a,
double v, double duration);
ARCS_ABI int moveJoint(MOTION_CONTROL_HANDLER h, const double *q, double a,
double v, double blend_radius, double duration);
ARCS_ABI int resumeMoveJoint(MOTION_CONTROL_HANDLER h, const double *q,
double a, double v, double duration);
ARCS_ABI int moveLine(MOTION_CONTROL_HANDLER h, const double *pose, double a,
double v, double blend_radius, double duration);
ARCS_ABI int moveProcess(MOTION_CONTROL_HANDLER h, const double *pose, double a,
double v, double blend_radius);
ARCS_ABI int resumeMoveLine(MOTION_CONTROL_HANDLER h, const double *pose,
double a, double v, double duration);
ARCS_ABI int moveCircle(MOTION_CONTROL_HANDLER h, const double *via_pose,
const double *end_pose, double a, double v,
double blend_radius, double duration);
ARCS_ABI int setCirclePathMode(MOTION_CONTROL_HANDLER h, int mode);
ARCS_ABI int moveCircle2(MOTION_CONTROL_HANDLER h,
const CircleParameters_C *param);
ARCS_ABI int pathBufferAlloc(MOTION_CONTROL_HANDLER h, const char *name,
int type, int size);
ARCS_ABI int pathBufferAppend(MOTION_CONTROL_HANDLER h, const char *name,
const double *waypoints, int rows);
ARCS_ABI int pathBufferEval(MOTION_CONTROL_HANDLER h, const char *name,
const double *a, const double *v, double t);
ARCS_ABI BOOL pathBufferValid(MOTION_CONTROL_HANDLER h, const char *name);
ARCS_ABI int pathBufferFree(MOTION_CONTROL_HANDLER h, const char *name);
ARCS_ABI int pathBufferList(MOTION_CONTROL_HANDLER h, char **result);
ARCS_ABI int movePathBuffer(MOTION_CONTROL_HANDLER h, const char *name);
ARCS_ABI int moveIntersection(MOTION_CONTROL_HANDLER h, const double *poses,
int rows, double a, double v,
double main_pipe_radius, double sub_pipe_radius,
double normal_distance, double normal_alpha);
ARCS_ABI int stopJoint(MOTION_CONTROL_HANDLER h, double acc);
ARCS_ABI int resumeStopJoint(MOTION_CONTROL_HANDLER h, double acc);
ARCS_ABI int stopLine(MOTION_CONTROL_HANDLER h, double acc, double acc_rot);
ARCS_ABI int resumeStopLine(MOTION_CONTROL_HANDLER h, double acc,
double acc_rot);
ARCS_ABI int weaveStart(MOTION_CONTROL_HANDLER h, const char *params);
ARCS_ABI int weaveEnd(MOTION_CONTROL_HANDLER h);
ARCS_ABI int storePath(MOTION_CONTROL_HANDLER h, BOOL keep_sync);
ARCS_ABI int stopMove(MOTION_CONTROL_HANDLER h, BOOL quick, BOOL all_tasks);
ARCS_ABI int startMove(MOTION_CONTROL_HANDLER h);
ARCS_ABI int clearPath(MOTION_CONTROL_HANDLER h);
ARCS_ABI int restoPath(MOTION_CONTROL_HANDLER h);
ARCS_ABI int setFuturePointSamplePeriod(MOTION_CONTROL_HANDLER h,
double sample_time);
ARCS_ABI int getFuturePathPointsJoint(MOTION_CONTROL_HANDLER h,
double **result);
ARCS_ABI int conveyorTrackCircle(MOTION_CONTROL_HANDLER h, int encoder_id,
const double *center, BOOL rotate_tool);
ARCS_ABI int conveyorTrackLine(MOTION_CONTROL_HANDLER h, int encoder_id,
const double *direction);
ARCS_ABI int conveyorTrackStop(MOTION_CONTROL_HANDLER h, int encoder_id,
double a);
ARCS_ABI int setConveyorTrackEncoder(MOTION_CONTROL_HANDLER h, int encoder_id,
int tick_per_meter);
ARCS_ABI int setConveyorTrackLimit(MOTION_CONTROL_HANDLER h, int encoder_id,
double limit);
ARCS_ABI int setConveyorTrackStartWindow(MOTION_CONTROL_HANDLER h,
int encoder_id, double window_min,
double window_max);
ARCS_ABI int setConveyorTrackSensorOffset(MOTION_CONTROL_HANDLER h,
int encoder_id, double offset);
ARCS_ABI int setConveyorTrackSyncSeparation(MOTION_CONTROL_HANDLER h,
int encoder_id, double distance,
double time);
ARCS_ABI int setConveyorTrackCompensate(MOTION_CONTROL_HANDLER h,
int encoder_id, double comp);
ARCS_ABI BOOL isConveyorTrackSync(MOTION_CONTROL_HANDLER h, int encoder_id);
ARCS_ABI BOOL isConveyorTrackExceed(MOTION_CONTROL_HANDLER h, int encoder_id);
ARCS_ABI int getConveyorTrackQueue(MOTION_CONTROL_HANDLER h, int encoder_id,
double *result);
ARCS_ABI int getConveyorTrackNextItem(MOTION_CONTROL_HANDLER h, int encoder_id);
ARCS_ABI int conveyorTrackCreatItem(MOTION_CONTROL_HANDLER h, int encoder_id,
int item_id, const double *offset);
ARCS_ABI BOOL hasItemOnConveyorToTrack(MOTION_CONTROL_HANDLER h,
int encoder_id);
ARCS_ABI BOOL conveyorTrackSwitch(MOTION_CONTROL_HANDLER h, int encoder_id);
ARCS_ABI int conveyorTrackClearItems(MOTION_CONTROL_HANDLER h, int encoder_id);
ARCS_ABI int setConveyorItemTrigIO(MOTION_CONTROL_HANDLER h, int encoder_id,
RobotIOType_C io_type, int io_index,
BOOL trig_edge);
ARCS_ABI int moveSpiral(MOTION_CONTROL_HANDLER h,
const SpiralParameters_C *param, double blend_radius,
double v, double a, double t);
ARCS_ABI int pathOffsetLimits(MOTION_CONTROL_HANDLER h, double v, double a);
ARCS_ABI int pathOffsetCoordinate(MOTION_CONTROL_HANDLER h, int ref_coord);
ARCS_ABI int getLookAheadSize(MOTION_CONTROL_HANDLER h);
ARCS_ABI int setLookAheadSize(MOTION_CONTROL_HANDLER h, int size);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,73 @@
#ifndef AUBO_SDK_RobotAlgorithm_C_H
#define AUBO_SDK_RobotAlgorithm_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI ForceSensorCalibResult_C
calibrateTcpForceSensor(ROBOT_ALGORITHM_HANDLER h, const double *forces,
int forces_rows, const double *poses, int poses_rows);
ARCS_ABI ForceSensorCalibResult_C
calibrateTcpForceSensor2(ROBOT_ALGORITHM_HANDLER h, const double *forces,
int forces_rows, const double *poses, int poses_rows);
ARCS_ABI int payloadIdentify(ROBOT_ALGORITHM_HANDLER h,
const char *data_no_payload,
const char *data_with_payload);
ARCS_ABI int payloadIdentify1(ROBOT_ALGORITHM_HANDLER h, const char *file_name);
ARCS_ABI int payloadCalculateFinished(ROBOT_ALGORITHM_HANDLER h);
ARCS_ABI Payload_C getPayloadIdentifyResult(ROBOT_ALGORITHM_HANDLER h);
ARCS_ABI int generatePayloadIdentifyTraj(ROBOT_ALGORITHM_HANDLER h,
const char *name,
const TrajConfig_C *traj_config);
ARCS_ABI int payloadIdentifyTrajGenFinished(ROBOT_ALGORITHM_HANDLER h);
ARCS_ABI BOOL frictionModelIdentify(ROBOT_ALGORITHM_HANDLER h, const double *q,
int q_rows, const double *qd, int qd_rows,
const double *qdd, int qdd_rows,
const double *temp, int temp_rows);
ARCS_ABI int calibWorkpieceCoordinatePara(ROBOT_ALGORITHM_HANDLER h,
const double *q, int q_rows, int type,
double *result);
ARCS_ABI int forwardDynamics(ROBOT_ALGORITHM_HANDLER h, const double *q,
const double *torqs, double *result);
ARCS_ABI int forwardKinematics(ROBOT_ALGORITHM_HANDLER h, const double *q,
double *result);
ARCS_ABI int forwardToolKinematics(ROBOT_ALGORITHM_HANDLER h, const double *q,
double *result);
ARCS_ABI int forwardDynamics1(ROBOT_ALGORITHM_HANDLER h, const double *q,
const double *torqs, const double *tcp_offset,
double *result);
ARCS_ABI int forwardKinematics1(ROBOT_ALGORITHM_HANDLER h, const double *q,
const double *tcp_offset, double *result);
ARCS_ABI int inverseKinematics(ROBOT_ALGORITHM_HANDLER h, const double *qnear,
const double *pose, double *result);
ARCS_ABI int inverseKinematicsAll(ROBOT_ALGORITHM_HANDLER h, const double *pose,
double **result);
ARCS_ABI int inverseKinematics1(ROBOT_ALGORITHM_HANDLER h, const double *qnear,
const double *pose, const double *tcp_offset,
double *result);
ARCS_ABI int inverseKinematicsAll1(ROBOT_ALGORITHM_HANDLER h,
const double *pose, const double *tcp_offset,
double **result);
ARCS_ABI int inverseToolKinematics(ROBOT_ALGORITHM_HANDLER h,
const double *qnear, const double *pose,
double *result);
ARCS_ABI int inverseToolKinematicsAll(ROBOT_ALGORITHM_HANDLER h,
const double *pose, double **result);
ARCS_ABI int pathMovej(ROBOT_ALGORITHM_HANDLER h, const double *q1, double r1,
const double *q2, double r2, double d, double **result);
ARCS_ABI int pathBlend3Points(ROBOT_ALGORITHM_HANDLER h, int type,
const double *q_start, const double *q_via,
const double *q_to, double r, double d,
double **result);
ARCS_ABI int pathBlend3Points1(ROBOT_ALGORITHM_HANDLER h, int type,
const double *q_start, const double *q_via,
const double *q_to, double r, double d,
double **result);
ARCS_ABI int calcJacobian(ROBOT_ALGORITHM_HANDLER h, const double *q,
BOOL base_or_end, double *result);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,92 @@
#ifndef AUBO_SDK_RobotConfig_C_H
#define AUBO_SDK_RobotConfig_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI int getDof(ROBOT_CONFIG_HANDLER h);
ARCS_ABI int getName(ROBOT_CONFIG_HANDLER h, char *result);
ARCS_ABI double getCycletime(ROBOT_CONFIG_HANDLER h);
ARCS_ABI int setSlowDownFraction(ROBOT_CONFIG_HANDLER h, int level,
double fraction);
ARCS_ABI double getSlowDownFraction(ROBOT_CONFIG_HANDLER h, int level);
ARCS_ABI int getRobotType(ROBOT_CONFIG_HANDLER h, char *result);
ARCS_ABI int getRobotSubType(ROBOT_CONFIG_HANDLER h, char *result);
ARCS_ABI int getControlBoxType(ROBOT_CONFIG_HANDLER h, char *result);
ARCS_ABI double getDefaultToolAcc(ROBOT_CONFIG_HANDLER h);
ARCS_ABI double getDefaultToolSpeed(ROBOT_CONFIG_HANDLER h);
ARCS_ABI double getDefaultJointAcc(ROBOT_CONFIG_HANDLER h);
ARCS_ABI double getDefaultJointSpeed(ROBOT_CONFIG_HANDLER h);
ARCS_ABI int setMountingPose(ROBOT_CONFIG_HANDLER h, const double *pose);
ARCS_ABI int getMountingPose(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI int setCollisionLevel(ROBOT_CONFIG_HANDLER h, int level);
ARCS_ABI int getCollisionLevel(ROBOT_CONFIG_HANDLER h);
ARCS_ABI int setCollisionStopType(ROBOT_CONFIG_HANDLER h, int type);
ARCS_ABI int getCollisionStopType(ROBOT_CONFIG_HANDLER h);
ARCS_ABI int setHomePosition(ROBOT_CONFIG_HANDLER h, const double *positions);
ARCS_ABI int getHomePosition(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI int setFreedriveDamp(ROBOT_CONFIG_HANDLER h, const double *damp);
ARCS_ABI int getFreedriveDamp(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI int getTcpForceSensorNames(ROBOT_CONFIG_HANDLER h, char **result);
ARCS_ABI int selectTcpForceSensor(ROBOT_CONFIG_HANDLER h, const char *name);
ARCS_ABI BOOL hasTcpForceSensor(ROBOT_CONFIG_HANDLER h);
ARCS_ABI int setTcpForceOffset(ROBOT_CONFIG_HANDLER h,
const double *force_offset);
ARCS_ABI int getTcpForceOffset(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI int getBaseForceSensorNames(ROBOT_CONFIG_HANDLER h, char **result);
ARCS_ABI int selectBaseForceSensor(ROBOT_CONFIG_HANDLER h, const char *name);
ARCS_ABI BOOL hasBaseForceSensor(ROBOT_CONFIG_HANDLER h);
ARCS_ABI int setBaseForceOffset(ROBOT_CONFIG_HANDLER h,
const double *force_offset);
ARCS_ABI int getBaseForceOffset(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI int setPersistentParameters(ROBOT_CONFIG_HANDLER h, const char *param);
ARCS_ABI int setKinematicsCompensate(ROBOT_CONFIG_HANDLER h,
const DHParam_C *param);
ARCS_ABI int setHardwareCustomParameters(ROBOT_CONFIG_HANDLER h,
const char *param);
ARCS_ABI int getHardwareCustomParameters(ROBOT_CONFIG_HANDLER h,
const char *param, char *result);
ARCS_ABI int setRobotZero(ROBOT_CONFIG_HANDLER h);
ARCS_ABI DHParam_C *getKinematicsParam(ROBOT_CONFIG_HANDLER h, BOOL real);
ARCS_ABI DHComp_C *getKinematicsCompensate(ROBOT_CONFIG_HANDLER h,
double ref_temperature);
ARCS_ABI uint32_t getSafetyParametersCheckSum(ROBOT_CONFIG_HANDLER h);
ARCS_ABI int confirmSafetyParameters(
ROBOT_CONFIG_HANDLER h, const RobotSafetyParameterRange_C *parameters);
ARCS_ABI uint32_t calcSafetyParametersCheckSum(
ROBOT_CONFIG_HANDLER h, const RobotSafetyParameterRange_C *parameters);
ARCS_ABI int getJointMaxPositions(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI int getJointMinPositions(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI int getJointMaxSpeeds(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI int getJointMaxAccelerations(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI int getTcpMaxSpeeds(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI int getTcpMaxAccelerations(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI BOOL toolSpaceInRange(ROBOT_CONFIG_HANDLER h, const double *pose);
ARCS_ABI int setPayload(ROBOT_CONFIG_HANDLER h, double m, const double *cog,
const double *aom, const double *inertia);
ARCS_ABI Payload_C getPayload(ROBOT_CONFIG_HANDLER h);
ARCS_ABI int getTcpOffset(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI int getGravity(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI int setGravity(ROBOT_CONFIG_HANDLER h, const double *gravity);
ARCS_ABI int setTcpOffset(ROBOT_CONFIG_HANDLER h, const double *offset);
ARCS_ABI int setToolInertial(ROBOT_CONFIG_HANDLER h, double m,
const double *com, const double *inertial);
ARCS_ABI int firmwareUpdate(ROBOT_CONFIG_HANDLER h, const char *fw);
ARCS_ABI double getFirmwareUpdateProcess(ROBOT_CONFIG_HANDLER h);
ARCS_ABI int setTcpForceSensorPose(ROBOT_CONFIG_HANDLER h,
const double *sensor_pose);
ARCS_ABI int getTcpForceSensorPose(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI int getLimitJointMaxPositions(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI int getLimitJointMinPositions(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI int getLimitJointMaxSpeeds(ROBOT_CONFIG_HANDLER h, double *result);
ARCS_ABI int getLimitJointMaxAccelerations(ROBOT_CONFIG_HANDLER h,
double *result);
ARCS_ABI double getLimitTcpMaxSpeed(ROBOT_CONFIG_HANDLER h);
ARCS_ABI SafeguedStopType_C getSafeguardStopType(ROBOT_CONFIG_HANDLER h);
ARCS_ABI int getSafeguardStopSource(ROBOT_CONFIG_HANDLER h);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,22 @@
#ifndef AUBO_SDK_RobotInterface_C_H
#define AUBO_SDK_RobotInterface_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI ROBOT_CONFIG_HANDLER robot_getRobotConfig(ROBOT_HANDLER robot);
ARCS_ABI MOTION_CONTROL_HANDLER robot_getMotionControl(ROBOT_HANDLER robot);
ARCS_ABI FORCE_CONTROL_HANDLER robot_getForceControl(ROBOT_HANDLER robot);
ARCS_ABI IO_CONTROL_HANDLER robot_getIoControl(ROBOT_HANDLER robot);
ARCS_ABI SYNC_MOVE_HANDLER robot_getSyncMove(ROBOT_HANDLER robot);
ARCS_ABI ROBOT_ALGORITHM_HANDLER robot_getRobotAlgorithm(ROBOT_HANDLER robot);
ARCS_ABI ROBOT_MANAGE_HANDLER robot_getRobotManage(ROBOT_HANDLER robot);
ARCS_ABI ROBOT_STATE_HANDLER robot_getRobotState(ROBOT_HANDLER robot);
ARCS_ABI TRACE_HANDLER robot_getTrace(ROBOT_HANDLER robot);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,38 @@
#ifndef AUBO_SDK_RobotManage_C_H
#define AUBO_SDK_RobotManage_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI int poweron(ROBOT_MANAGE_HANDLER h);
ARCS_ABI int startup(ROBOT_MANAGE_HANDLER h);
ARCS_ABI int poweroff(ROBOT_MANAGE_HANDLER h);
ARCS_ABI int backdrive(ROBOT_MANAGE_HANDLER h, BOOL enable);
ARCS_ABI int freedrive(ROBOT_MANAGE_HANDLER h, BOOL enable);
ARCS_ABI int handguideMode(ROBOT_MANAGE_HANDLER h, int32_t *freeAxes,
const double *feature);
ARCS_ABI int exitHandguideMode(ROBOT_MANAGE_HANDLER h);
ARCS_ABI int getHandguideStatus(ROBOT_MANAGE_HANDLER h);
ARCS_ABI int getHandguideTrigger(ROBOT_MANAGE_HANDLER h);
ARCS_ABI BOOL isHandguideEnabled(ROBOT_MANAGE_HANDLER h);
ARCS_ABI int setSim(ROBOT_MANAGE_HANDLER h, BOOL enable);
ARCS_ABI int setOperationalMode(ROBOT_MANAGE_HANDLER h,
OperationalModeType_C mode);
ARCS_ABI OperationalModeType_C getOperationalMode(ROBOT_MANAGE_HANDLER h);
ARCS_ABI RobotControlModeType_C getRobotControlMode(ROBOT_MANAGE_HANDLER h);
ARCS_ABI BOOL isSimulationEnabled(ROBOT_MANAGE_HANDLER h);
ARCS_ABI BOOL isFreedriveEnabled(ROBOT_MANAGE_HANDLER h);
ARCS_ABI BOOL isBackdriveEnabled(ROBOT_MANAGE_HANDLER h);
ARCS_ABI int setUnlockProtectiveStop(ROBOT_MANAGE_HANDLER h);
ARCS_ABI int startRecord(ROBOT_MANAGE_HANDLER h, const char *file_name);
ARCS_ABI int stopRecord(ROBOT_MANAGE_HANDLER h);
ARCS_ABI int pauseRecord(ROBOT_MANAGE_HANDLER h, BOOL pause);
ARCS_ABI int restartInterfaceBoard(ROBOT_MANAGE_HANDLER h);
ARCS_ABI int setLinkModeEnable(ROBOT_MANAGE_HANDLER h, BOOL enable);
ARCS_ABI BOOL isLinkModeEnabled(ROBOT_MANAGE_HANDLER h);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,78 @@
#ifndef AUBO_SDK_RobotState_C_H
#define AUBO_SDK_RobotState_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI RobotModeType_C getRobotModeType(ROBOT_STATE_HANDLER h);
ARCS_ABI SafetyModeType_C getSafetyModeType(ROBOT_STATE_HANDLER h);
ARCS_ABI BOOL isPowerOn(ROBOT_STATE_HANDLER h);
ARCS_ABI BOOL isSteady(ROBOT_STATE_HANDLER h);
ARCS_ABI BOOL isCollisionOccurred(ROBOT_STATE_HANDLER h);
ARCS_ABI BOOL isWithinSafetyLimits(ROBOT_STATE_HANDLER h);
ARCS_ABI int getTcpPose(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getActualTcpOffset(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getTargetTcpPose(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getToolPose(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getTcpSpeed(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getTcpForce(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getElbowPosistion(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getElbowVelocity(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getBaseForce(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getTcpTargetPose(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getTcpTargetSpeed(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getTcpTargetForce(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getJointState(ROBOT_STATE_HANDLER h, JointStateType_C *result);
ARCS_ABI int getJointServoMode(ROBOT_STATE_HANDLER h,
JointServoModeType_C *result);
ARCS_ABI int getJointPositions(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getJointPositionsHistory(ROBOT_STATE_HANDLER h, int steps,
double *result);
ARCS_ABI int getJointSpeeds(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getJointAccelerations(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getJointTorqueSensors(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getJointContactTorques(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getBaseForceSensor(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getTcpForceSensors(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getJointCurrents(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getJointVoltages(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getJointTemperatures(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getJointUniqueIds(ROBOT_STATE_HANDLER h, char **result);
ARCS_ABI int getJointFirmwareVersions(ROBOT_STATE_HANDLER h, int *result);
ARCS_ABI int getJointHardwareVersions(ROBOT_STATE_HANDLER h, int *result);
ARCS_ABI int getMasterBoardUniqueId(ROBOT_STATE_HANDLER h, char *result);
ARCS_ABI int getMasterBoardFirmwareVersion(ROBOT_STATE_HANDLER h);
ARCS_ABI int getMasterBoardHardwareVersion(ROBOT_STATE_HANDLER h);
ARCS_ABI int getSlaveBoardUniqueId(ROBOT_STATE_HANDLER h, char *result);
ARCS_ABI int getSlaveBoardFirmwareVersion(ROBOT_STATE_HANDLER h);
ARCS_ABI int getSlaveBoardHardwareVersion(ROBOT_STATE_HANDLER h);
ARCS_ABI int getToolUniqueId(ROBOT_STATE_HANDLER h, char *result);
ARCS_ABI int getToolFirmwareVersion(ROBOT_STATE_HANDLER h);
ARCS_ABI int getToolHardwareVersion(ROBOT_STATE_HANDLER h);
ARCS_ABI int getToolCommMode(ROBOT_STATE_HANDLER h);
ARCS_ABI int getPedestalUniqueId(ROBOT_STATE_HANDLER h, char *result);
ARCS_ABI int getPedestalFirmwareVersion(ROBOT_STATE_HANDLER h);
ARCS_ABI int getPedestalHardwareVersion(ROBOT_STATE_HANDLER h);
ARCS_ABI int getJointTargetPositions(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getJointTargetSpeeds(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getJointTargetAccelerations(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getJointTargetTorques(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI int getJointTargetCurrents(ROBOT_STATE_HANDLER h, double *result);
ARCS_ABI double getControlBoxTemperature(ROBOT_STATE_HANDLER h);
ARCS_ABI double getControlBoxHumidity(ROBOT_STATE_HANDLER h);
ARCS_ABI double getMainVoltage(ROBOT_STATE_HANDLER h);
ARCS_ABI double getMainCurrent(ROBOT_STATE_HANDLER h);
ARCS_ABI double getRobotVoltage(ROBOT_STATE_HANDLER h);
ARCS_ABI double getRobotCurrent(ROBOT_STATE_HANDLER h);
ARCS_ABI int getSlowDownLevel(ROBOT_STATE_HANDLER h);
ARCS_ABI HandleStateType_C getHandleState(ROBOT_STATE_HANDLER h);
ARCS_ABI HandleModeType_C getHandleMode(ROBOT_STATE_HANDLER h);
ARCS_ABI int getHandleUniqueId(ROBOT_STATE_HANDLER h, char *result);
ARCS_ABI int getHandleFirmwareVersion(ROBOT_STATE_HANDLER h);
ARCS_ABI int getHandleHardwareVersion(ROBOT_STATE_HANDLER h);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,179 @@
/** @file rpc.h
* @brief 用于RPC模块的交互,如登录、连接等功能
*/
#ifndef AUBO_SDK_RPC_H
#define AUBO_SDK_RPC_H
#include <memory>
#include <aubo/aubo_api.h>
#include <aubo/global_config.h>
namespace arcs {
namespace aubo_sdk {
using namespace arcs::common_interface;
/// RPC客户端
class ARCS_ABI RpcClient : public AuboApi
{
public:
enum Event
{
Connected = 0,
Disconnected = 1,
};
/**
* @brief RpcClient
* @param mode 0-TCP 1-UDS
*/
RpcClient(int mode = 0);
~RpcClient();
/**
* 设置日志处理器
*
* 此函数可设置自定义的日志处理函数来处理日志消息。 \n
* Aubo SDK 有一套默认的日志系统,按照默认的格式输出到默认的文件。
* 如果用户不希望采用默认的格式或者不希望输出到默认的文件,那就可以通过这个接口重新自定义格式,或者输出路径。
* 这个函数可以将用户自定义的日志系统与 Aubo SDK 默认的日志系统合并。
*
* @note setLogHandler函数要放在即将触发的日志之前,
* 否则会按照默认的形式输出日志。
*
* @param handler 日志处理函数 \n
* 此日志处理函数的下定义如下: \n
* void handler(int level, const char* filename, int line, const
* std::string& message) \n
* level 表示日志等级 \n
* &nbsp; 0: LOGLEVEL_FATAL 严重的错误 \n
* &nbsp; 1: LOGLEVEL_ERROR 错误 \n
* &nbsp; 2: LOGLEVEL_WARNING 警告 \n
* &nbsp; 3: LOGLEVEL_INFO 通知 \n
* &nbsp; 4: LOGLEVEL_DEBUG 调试 \n
* &nbsp; 5: LOGLEVEL_BACKTRACE 跟踪 \n
* filename 表示文件名 \n
* line 表示代码行号 \n
* message 表示日志信息 \n
* @return 无
*/
void setLogHandler(
std::function<void(int /*level*/, const char * /*filename*/,
int /*line*/, const std::string & /*message*/)>
handler);
/**
* 连接到RPC服务
*
* @param ip IP地址
* @param port 端口号,RPC的端口号是30004
* @param ip和port为空时,采用unix domain sockets通讯方式
* @retval 0 RPC连接成功
* @retval -8 RPC连接失败,RPC连接被拒绝
* @retval -15 RPC连接失败,SDK版本与Server版本不兼容
*/
int connect(const std::string &ip = "", int port = 0);
/**
* 断开RPC连接
*
* @retval 0 成功
* @retval -1 失败
*/
int disconnect();
/**
* 判断是否连接RPC
*
* @retval true 已连接RPC
* @retval false 未连接RPC
*/
bool hasConnected() const;
/**
* 登录
*
* @param usrname 用户名
* @param passwd 密码
* @return 0
*/
int login(const std::string &usrname, const std::string &passwd);
/**
* 登出
*
* @return 0
*/
int logout();
/**
* 判断是否登录
*
* @retval true 已登录
* @retval false 未登录
*/
bool hasLogined();
/**
* 设置RPC请求超时时间
*
* @param timeout 请求超时时间,单位 ms
* @return 0
*/
int setRequestTimeout(int timeout = 100);
/**
* 设置事件处理
*
* @param cb
* @return
*/
int setEventHandler(std::function<void(int /*event*/)> cb);
/**
* 是否关闭异常抛出
*
* @param enable
* @return
*/
int setExceptionFree(bool enable);
/**
* 返回错误代码
*
* @return
*/
int errorCode() const;
/**
* 设备关机
*
* @return
*/
int shutdown();
/**
* 控制服务端RPC日志打印
*
* @param enable true 启用服务端日志打印,false 禁用服务端日志打印
* @return 0 成功
*/
int enableServerRpcLogging(bool enable);
};
using RpcClientPtr = std::shared_ptr<RpcClient>;
} // namespace aubo_sdk
} // namespace arcs
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI arcs::aubo_sdk::RpcClient *createRpcClient(int mode = 0);
ARCS_ABI void destroyRpcClient(arcs::aubo_sdk::RpcClient *cli);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,321 @@
/** @file rpc_c.h
* @brief 用于RPC模块的交互,如登录、连接等功能
*/
#ifndef AUBO_SDK_RPC_C_H
#define AUBO_SDK_RPC_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
enum Event
{
Event_Connected = 0,
Event_Disconnected = 1,
};
ARCS_ABI RPC_HANDLER rpc_create_client(int mode = 0);
ARCS_ABI void rpc_destroy_client(RPC_HANDLER cli);
/**
* 设置日志处理器
*
* 此函数可设置自定义的日志处理函数来处理日志消息。 \n
* Aubo SDK 有一套默认的日志系统,按照默认的格式输出到默认的文件。
* 如果用户不希望采用默认的格式或者不希望输出到默认的文件,那就可以通过这个接口重新自定义格式,或者输出路径。
* 这个函数可以将用户自定义的日志系统与 Aubo SDK 默认的日志系统合并。
*
* @note setLogHandler函数要放在即将触发的日志之前,
* 否则会按照默认的形式输出日志。
*
* @param handler 日志处理函数 \n
* 此日志处理函数的下定义如下: \n
* void handler(int level, const char* filename, int line, const
* std::string& message) \n
* level 表示日志等级 \n
* &nbsp; 0: LOGLEVEL_FATAL 严重的错误 \n
* &nbsp; 1: LOGLEVEL_ERROR 错误 \n
* &nbsp; 2: LOGLEVEL_WARNING 警告 \n
* &nbsp; 3: LOGLEVEL_INFO 通知 \n
* &nbsp; 4: LOGLEVEL_DEBUG 调试 \n
* &nbsp; 5: LOGLEVEL_BACKTRACE 跟踪 \n
* filename 表示文件名 \n
* line 表示代码行号 \n
* message 表示日志信息 \n
* @return 无
*/
ARCS_ABI void rpc_setLogHandler(RPC_HANDLER cli, LOG_HANDLER handler);
/**
* 连接到RPC服务
*
* @param ip IP地址
* @param port 端口号,RPC的端口号是30004
* @param ip和port为空时,采用unix domain sockets通讯方式
* @retval 0 RPC连接成功
* @retval -8 RPC连接失败,RPC连接被拒绝
* @retval -15 RPC连接失败,SDK版本与Server版本不兼容
*/
ARCS_ABI int rpc_connect(RPC_HANDLER cli, const char *ip = "", int port = 0);
/**
* 断开RPC连接
*
* @retval 0 成功
* @retval -1 失败
*/
ARCS_ABI int rpc_disconnect(RPC_HANDLER cli);
/**
* 判断是否连接RPC
*
* @retval true 已连接RPC
* @retval false 未连接RPC
*/
ARCS_ABI bool rpc_hasConnected(RPC_HANDLER cli);
/**
* 登录
*
* @param usrname 用户名
* @param passwd 密码
* @return 0
*/
ARCS_ABI int rpc_login(RPC_HANDLER cli, const char *usrname,
const char *passwd);
/**
* 登出
*
* @return 0
*/
ARCS_ABI int rpc_logout(RPC_HANDLER cli);
/**
* 判断是否登录
*
* @retval true 已登录
* @retval false 未登录
*/
ARCS_ABI bool rpc_hasLogined(RPC_HANDLER cli);
/**
* 设置RPC请求超时时间
*
* @param timeout 请求超时时间,单位 ms
* @return 0
*/
ARCS_ABI int rpc_setRequestTimeout(RPC_HANDLER cli, int timeout = 10);
/**
* 设置事件处理
*
* @param cb
* @return
*/
ARCS_ABI int rpc_setEventHandler(RPC_HANDLER cli, EVENT_CALLBACK cb);
/**
* 是否关闭异常抛出
*
* @param enable
* @return
*/
ARCS_ABI int rpc_set_exception_free(RPC_HANDLER cli, bool enable);
/**
* 返回错误代码
*
* @return
*/
ARCS_ABI int rpc_errorCode(RPC_HANDLER cli);
/**
* 设备关机
*
* @return
*/
ARCS_ABI int rpc_shutdown(RPC_HANDLER cli);
/**
* 获取纯数学相关接口
*
* @return MathPtr对象的指针
*
* @par Python函数原型
* getMath(self: pyaubo_sdk.AuboApi) -> pyaubo_sdk.Math
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* MathPtr ptr = rpc_cli->getMath();
* @endcode
*
*/
ARCS_ABI MATH_HANDLER rpc_getMath(RPC_HANDLER cli);
/**
* 获取系统信息
*
* @return SystemInfoPtr对象的指针
*
* @par Python函数原型
* getSystemInfo(self: pyaubo_sdk.AuboApi) -> pyaubo_sdk.SystemInfo
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* SystemInfoPtr ptr = rpc_cli->getSystemInfo();
* @endcode
*
*/
ARCS_ABI SYSTEM_INFO_HANDLER rpc_getSystemInfo(RPC_HANDLER cli);
/**
* 获取运行时接口
*
* @return RuntimeMachinePtr对象的指针
*
* @par Python函数原型
* getRuntimeMachine(self: pyaubo_sdk.AuboApi) -> pyaubo_sdk.RuntimeMachine
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* RuntimeMachinePtr ptr = rpc_cli->getRuntimeMachine();
* @endcode
*
*/
ARCS_ABI RUNTIME_MACHINE_HANDLER rpc_getRuntimeMachine(RPC_HANDLER cli);
/**
* 对外寄存器接口
*
* @return RegisterControlPtr对象的指针
*
* @par Python函数原型
* getRegisterControl(self: pyaubo_sdk.AuboApi) ->
* pyaubo_sdk.RegisterControl
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* RegisterControlPtr ptr = rpc_cli->getRegisterControl();
* @endcode
*
*/
ARCS_ABI REGISTER_CONTROL_HANDLER rpc_getRegisterControl(RPC_HANDLER cli);
/**
* 获取机器人列表
*
* @return 机器人列表
*
* @par Python函数原型
* getRobotNames(self: pyaubo_sdk.AuboApi) -> List[str]
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* @endcode
*
* @par JSON-RPC请求示例
* {"jsonrpc":"2.0","method":"getRobotNames","params":[],"id":1}
*
* @par JSON-RPC响应示例
* {"id":1,"jsonrpc":"2.0","result":["rob1"]}
*
*/
ARCS_ABI int rpc_getRobotNames(RPC_HANDLER cli, char **names);
/**
* 根据名字获取 RobotInterfacePtr 接口
*
* @param name 机器人名字
* @return RobotInterfacePtr对象的指针
*
* @par Python函数原型
* getRobotInterface(self: pyaubo_sdk.AuboApi, arg0: str) ->
* pyaubo_sdk.RobotInterface
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* auto robot_name = rpc_cli->getRobotNames().front();
* RobotInterfacePtr ptr = rpc_cli->getRobotInterface(robot_name);
* @endcode
*
*/
ARCS_ABI ROBOT_HANDLER rpc_getRobotInterface(RPC_HANDLER cli, const char *name);
/**
* 获取外部轴列表
*
* @return
*/
ARCS_ABI int rpc_getAxisNames(RPC_HANDLER cli, char **names);
/**
* 获取外部轴接口
*
* @param name
* @return
*/
ARCS_ABI AXIS_HANDLER rpc_getAxisInterface(RPC_HANDLER cli, const char *name);
/// 获取独立 IO 模块接口
/**
* 获取 socket
* @return SocketPtr对象的指针
*
* @par Python函数原型
* getSocket(self: pyaubo_sdk.AuboApi) -> arcs::common_interface::Socket
* @endcode
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* SocketPtr ptr = rpc_cli->getSocket();
* @endcode
*
*/
ARCS_ABI SOCKET_HANDLER rpc_getSocket(RPC_HANDLER cli);
/**
*
* @return SerialPtr对象的指针
*
* @par Python函数原型
* getSerial(self: pyaubo_sdk.AuboApi) -> arcs::common_interface::Serial
*
* @par C++示例
* @code
* auto rpc_cli = std::make_shared<RpcClient>();
* SerialPtr ptr = rpc_cli->getSerial();
* @endcode
*/
ARCS_ABI SERIAL_HANDLER rpc_getSerial(RPC_HANDLER cli);
/**
* 获取同步运动接口
*
* @return SyncMovePtr对象的指针
*/
ARCS_ABI SYNC_MOVE_HANDLER rpc_getSyncMove(RPC_HANDLER cli, const char *name);
/**
* 获取告警信息接口
*
* @return TracePtr对象的指针
*/
ARCS_ABI TRACE_HANDLER rpc_getTrace(RPC_HANDLER cli, const char *name);
#ifdef __cplusplus
}
#endif
#endif // #define AUBO_SDK_RPC_C_H

View File

@ -0,0 +1,305 @@
/** @file rtde.h
* @brief 用于RPC模块的交互,如订阅、发布等功能
*/
#ifndef AUBO_SDK_RTDE_H
#define AUBO_SDK_RTDE_H
#include <string>
#include <vector>
#include <functional>
#include <memory>
#include <map>
#include <unordered_map>
#include <aubo/type_def.h>
#include <aubo/global_config.h>
namespace arcs {
namespace aubo_sdk {
class RtdeClient;
/// 向输出数据中增加
class ARCS_ABI OutputBuilder
{
public:
OutputBuilder();
~OutputBuilder();
OutputBuilder &push(int val);
OutputBuilder &push(double val);
OutputBuilder &push(const std::vector<double> &val);
OutputBuilder &push(const std::tuple<int, bool> &val);
OutputBuilder &push(int16_t &val);
OutputBuilder &push(const std::vector<int16_t> &val);
OutputBuilder &push(const std::vector<int> &val);
OutputBuilder &push(const std::string &val);
OutputBuilder &push(char val);
OutputBuilder &push(const common_interface::RtdeRecipe &val);
private:
friend RtdeClient;
class Impl;
Impl *impl;
};
/// 解析输入
class ARCS_ABI InputParser
{
public:
InputParser();
~InputParser();
bool popBool();
int popInt32();
int64_t popInt64();
int16_t popInt16();
double popDouble();
char popChar();
std::vector<int> popVectorInt();
std::vector<int16_t> popVectorInt16();
std::vector<double> popVectorDouble();
std::vector<std::vector<double>> popVectorVectorDouble();
std::vector<common_interface::JointStateType> popVectorJointStateType();
common_interface::RobotModeType popRobotModeType();
common_interface::OperationalModeType popOperationalModeType();
common_interface::SafetyModeType popSafetyModeType();
common_interface::RuntimeState popRuntimeState();
common_interface::RobotMsgVector popRobotMsgVector();
common_interface::Payload popPayload();
common_interface::GripperStatusVector popGripperStatusVector();
common_interface::HandleStateType popHandleStateType();
common_interface::HandleModeType popHandleModeType();
private:
friend RtdeClient;
class Impl;
Impl *impl;
};
/// RTDE客户端
class ARCS_ABI RtdeClient
{
public:
enum Event
{
Connected,
Disconnected,
};
/**
* RTDE客户端初始化
*
* @param mode 设置通讯方式, 0 tcp通讯 1 uds通讯
*/
RtdeClient(int mode = 0);
~RtdeClient();
/**
* 设置日志处理器
*
* 此函数可设置自定义的日志处理函数来处理日志消息。 \n
* Aubo SDK 有一套默认的日志系统,按照默认的格式输出到默认的文件。
* 如果用户不希望采用默认的格式或者不希望输出到默认的文件,那就可以通过这个接口重新自定义格式,或者输出路径。
* 这个函数可以将用户自定义的日志系统与 Aubo SDK 默认的日志系统合并。
*
* @note setLogHandler函数要放在即将触发的日志之前,
* 否则会按照默认的形式输出日志。
*
* @param handler 日志处理函数 \n
* 此日志处理函数的下定义如下: \n
* void handler(int level, const char* filename, int line, const
* std::string& message) \n
* level 表示日志等级 \n
* &nbsp; 0: LOGLEVEL_FATAL 严重的错误 \n
* &nbsp; 1: LOGLEVEL_ERROR 错误 \n
* &nbsp; 2: LOGLEVEL_WARNING 警告 \n
* &nbsp; 3: LOGLEVEL_INFO 通知 \n
* &nbsp; 4: LOGLEVEL_DEBUG 调试 \n
* &nbsp; 5: LOGLEVEL_BACKTRACE 跟踪 \n
* filename 表示文件名 \n
* line 表示代码行号 \n
* message 表示日志信息 \n
* @return 无
*/
void setLogHandler(
std::function<void(int /*level*/, const char * /*filename*/,
int /*line*/, const std::string & /*message*/)>
handler);
/**
* 连接到服务器
*
* @param ip IP地址
* @param port 端口号,RTDE 端口号为30010
* @retval 0 连接成功
* @retval 1 在执行函数前,已连接
* @retval -1 连接失败
*/
int connect(const std::string &ip = "", int port = 0);
/**
* socket 是否已连接
*
* @retval true 已连接socket
* @retval false 未连接socket
*/
bool hasConnected() const;
/**
* socket 是否已连接
*
* @param callback
* @retval true 已连接socket
* @retval false 未连接socket
*/
bool hasConnected1(std::function<void(bool)> callback);
/**
* 登录
*
* @param usrname 用户名
* @param passwd 密码
* @retval 0 成功
* @retval -1 失败
*/
int login(const std::string &usrname, const std::string &passwd);
/**
* 是否已经登录
*
* @retval true 已登录
* @retval false 未登录
*/
bool hasLogined();
/**
* 登出
*
* @return 0
*/
int logout();
/**
* 断开连接
*
* @retval 0 成功
* @retval -1 失败
*/
int disconnect();
/**
* 获取协议版本号
*
* @return 协议版本号
*/
int getProtocolVersion();
/**
* 获取输入列表
*
* @return 输入列表
*/
std::map<std::string, int> getInputMaps();
/**
* 获取输出列表
*
* @return 输出列表
*/
std::map<std::string, int> getOutputMaps();
/**
* 设置话题
*
* @param to_server 数据流向。
* true 表示客户端给服务器发送消息,false 表示服务器给客户端发送消息
* @param names 服务器推送的信息列表
* @param freq 服务器推送信息的频率
* @param expected_chanel 通道。
* 取值范围:0~99,
* 发布不同的话题走不同的通道
* @retval expected_chanel参数的值 成功
* @retval -1 失败
*/
int setTopic(bool to_server, const std::vector<std::string> &names,
double freq, int expected_chanel);
/**
* 取消订阅
*
* @param to_server 数据流向
* true 表示客户端给服务器发送消息,false 表示服务器给客户端发送消息
* @param chanel 通道
* @retval 0 成功
* @retval 1 失败
*/
int removeTopic(bool to_server, int chanel);
/**
* 获取已注册的输入菜单
*
* @return 已注册的输入菜单
*/
std::unordered_map<int, common_interface::RtdeRecipe>
getRegisteredInputRecipe();
/**
* 获取已注册的输出菜单
*
* @return 已注册的输出菜单
*/
std::unordered_map<int, common_interface::RtdeRecipe>
getRegisteredOutputRecipe();
/**
* 订阅 subscribe from output
*
* @param chanel 通道
* @param callback 回调函数,用于处理订阅的输入信息。\n
* 回调函数的定义如下:
* void callback(InputParser &parser)
* @return 0
*/
int subscribe(int chanel, std::function<void(InputParser &)> callback);
/**
* 发布 publish to input
*
* @param chanel 通道
* @param callback 回调函数,用于构建发布的输出信息。\n
* 回调函数的定义如下:
* void callback(OutputBuilder &builder)
* @retval 0 成功
* @retval -1 失败
*/
int publish(int chanel, std::function<void(OutputBuilder &)> callback);
/**
* 设置事件处理
*
* @param cb
* @return
*/
int setEventHandler(std::function<void(int /*event*/)> cb);
private:
class Impl;
Impl *impl;
};
using RtdeClientPtr = std::shared_ptr<RtdeClient>;
} // namespace aubo_sdk
} // namespace arcs
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI arcs::aubo_sdk::RtdeClient *createRtdeClient(int mode = 0);
ARCS_ABI void destroyRtdeClient(arcs::aubo_sdk::RtdeClient *cli);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,180 @@
#ifndef AUBO_SDK_RTDE_C_H
#define AUBO_SDK_RTDE_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief 创建 RTDE 客户端实例。
*
* @param mode 设置通讯方式,0 表示 TCP 通讯,1 表示 UDS 通讯。
* @return 成功返回 RTDE 客户端句柄,失败返回 NULL。
*/
ARCS_ABI RTDE_HANDLE rtde_create_client(int mode);
/**
* @brief 销毁 RTDE 客户端实例。
*
* @param cli RTDE 客户端句柄。
*/
ARCS_ABI void rtde_destroy_client(RTDE_HANDLE cli);
/**
* @brief 设置日志处理器。
*
* @param cli RTDE 客户端句柄。
* @param handler 日志处理函数。
*/
ARCS_ABI void rtde_setLogHandler(RTDE_HANDLE cli,
void (*handler)(int level,
const char *filename, int line,
const char *message));
/**
* @brief 连接到服务器。
*
* @param cli RTDE 客户端句柄。
* @param ip IP 地址,NULL 表示使用默认地址。
* @param port 端口号,0 表示使用默认端口。
* @return 0 表示成功,-1 表示失败。
*/
ARCS_ABI int rtde_connect(RTDE_HANDLE cli, const char *ip, int port);
/**
* @brief 检查是否已连接。
*
* @param cli RTDE 客户端句柄。
* @return true 表示已连接,false 表示未连接。
*/
ARCS_ABI bool rtde_hasConnected(RTDE_HANDLE cli);
/**
* @brief 登录到服务器。
*
* @param cli RTDE 客户端句柄。
* @param username 用户名。
* @param password 密码。
* @return 0 表示成功,-1 表示失败。
*/
ARCS_ABI int rtde_login(RTDE_HANDLE cli, const char *username,
const char *password);
/**
* @brief 检查是否已登录。
*
* @param cli RTDE 客户端句柄。
* @return true 表示已登录,false 表示未登录。
*/
ARCS_ABI bool rtde_hasLogined(RTDE_HANDLE cli);
/**
* @brief 登出。
*
* @param cli RTDE 客户端句柄。
* @return 0 表示成功。
*/
ARCS_ABI int rtde_logout(RTDE_HANDLE cli);
/**
* @brief 断开连接。
*
* @param cli RTDE 客户端句柄。
* @return 0 表示成功,-1 表示失败。
*/
ARCS_ABI int rtde_disconnect(RTDE_HANDLE cli);
/**
* @brief 获取协议版本号。
*
* @param cli RTDE 客户端句柄。
* @return 协议版本号。
*/
ARCS_ABI int rtde_getProtocolVersion(RTDE_HANDLE cli);
/**
* @brief 设置话题。
*
* @param cli RTDE 客户端句柄。
* @param to_server 数据流向,true 表示客户端发送消息,false
* 表示服务器发送消息。
* @param names 信息列表(逗号分隔的字符串)。
* @param freq 推送频率。
* @param expected_chanel 通道号。
* @return 成功返回通道号,失败返回 -1。
*/
ARCS_ABI int rtde_setTopic(RTDE_HANDLE cli, bool to_server, const char *names,
double freq, int expected_chanel);
/**
* @brief 取消订阅。
*
* @param cli RTDE 客户端句柄。
* @param to_server 数据流向。
* @param chanel 通道号。
* @return 0 表示成功,-1 表示失败。
*/
ARCS_ABI int rtde_removeTopic(RTDE_HANDLE cli, bool to_server, int chanel);
/**
* @brief 订阅主题。
*
* @param cli RTDE 客户端句柄。
* @param chanel 通道号。
* @param callback 回调函数,用于处理订阅的数据。
* @return 0 表示成功,-1 表示失败。
*/
ARCS_ABI int rtde_subscribe(RTDE_HANDLE cli, int chanel,
void (*callback)(void *parser));
/**
* @brief 发布数据。
*
* @param cli RTDE 客户端句柄。
* @param chanel 通道号。
* @param callback 回调函数,用于生成发布数据。
* @return 0 表示成功,-1 表示失败。
*/
ARCS_ABI int rtde_publish(RTDE_HANDLE cli, int chanel,
void (*callback)(void *builder));
// OutputBuilder
ARCS_ABI int rtde_pushInt(OUTPUT_BUILDER_HANDLE builder, int val);
ARCS_ABI int rtde_pushDouble(OUTPUT_BUILDER_HANDLE builder, double val);
ARCS_ABI int rtde_pushVectorDouble(OUTPUT_BUILDER_HANDLE builder, const double* val, int size);
ARCS_ABI int rtde_pushTupleIntBool(OUTPUT_BUILDER_HANDLE builder, int first, bool second);
ARCS_ABI int rtde_pushInt16(OUTPUT_BUILDER_HANDLE builder, int16_t val);
ARCS_ABI int rtde_pushVectorInt16(OUTPUT_BUILDER_HANDLE builder, const int16_t* val, int size);
ARCS_ABI int rtde_pushVectorInt(OUTPUT_BUILDER_HANDLE builder, const int* val, int size);
ARCS_ABI int rtde_pushString(OUTPUT_BUILDER_HANDLE builder, const char* val);
ARCS_ABI int rtde_pushChar(OUTPUT_BUILDER_HANDLE builder, char val);
ARCS_ABI int rtde_pushRtdeRecipe(OUTPUT_BUILDER_HANDLE builder, const struct RtdeRecipe_C* val);
// InputParser
ARCS_ABI bool rtde_popBool(INPUT_PARSER_HANDLE parser);
ARCS_ABI int32_t rtde_popInt32(INPUT_PARSER_HANDLE parser);
ARCS_ABI int64_t rtde_popInt64(INPUT_PARSER_HANDLE parser);
ARCS_ABI int16_t rtde_popInt16(INPUT_PARSER_HANDLE parser);
ARCS_ABI double rtde_popDouble(INPUT_PARSER_HANDLE parser);
ARCS_ABI char rtde_popChar(INPUT_PARSER_HANDLE parser);
ARCS_ABI int rtde_popVectorInt(INPUT_PARSER_HANDLE* parser, int* data, int size);
ARCS_ABI int rtde_popVectorInt16(INPUT_PARSER_HANDLE parser, int16_t* data, int size);
ARCS_ABI int rtde_popVectorDouble(INPUT_PARSER_HANDLE parser, double* data, int size);
ARCS_ABI int rtde_popVectorVectorDouble(INPUT_PARSER_HANDLE parser, double*** data, int rows, int cols);
ARCS_ABI JointStateType_C rtde_popVectorJointStateType(INPUT_PARSER_HANDLE parser);
ARCS_ABI RobotModeType_C rtde_popRobotModeType(INPUT_PARSER_HANDLE parser);
ARCS_ABI OperationalModeType_C rtde_popOperationalModeType(INPUT_PARSER_HANDLE parser);
ARCS_ABI SafetyModeType_C rtde_popSafetyModeType(INPUT_PARSER_HANDLE parser);
ARCS_ABI RuntimeState_C rtde_popRuntimeState(INPUT_PARSER_HANDLE parser);
ARCS_ABI int rtde_popRobotMsgVector(INPUT_PARSER_HANDLE parser, struct RobotMsgVector_C* robot_msg_vec);
ARCS_ABI int rtde_popPayload(INPUT_PARSER_HANDLE parser, struct Payload_C* payload);
#ifdef __cplusplus
}
#endif
#endif // AUBO_SDK_RTDE_C_H

View File

@ -0,0 +1,60 @@
#ifndef AUBO_SDK_RuntimeMachine_C_H
#define AUBO_SDK_RuntimeMachine_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI int newTask(RUNTIME_MACHINE_HADNLER h, BOOL daemon);
ARCS_ABI int deleteTask(RUNTIME_MACHINE_HADNLER h, int tid);
ARCS_ABI int detachTask(RUNTIME_MACHINE_HADNLER h, int tid);
ARCS_ABI BOOL isTaskAlive(RUNTIME_MACHINE_HADNLER h, int tid);
ARCS_ABI int nop(RUNTIME_MACHINE_HADNLER h);
ARCS_ABI int switchTask(RUNTIME_MACHINE_HADNLER h, int tid);
ARCS_ABI int setLabel(RUNTIME_MACHINE_HADNLER h, int tid, const char *lineno);
ARCS_ABI int setPlanContext(RUNTIME_MACHINE_HADNLER h, int tid, int lineno,
const char *comment);
ARCS_ABI int gotoLine(RUNTIME_MACHINE_HADNLER h, int lineno);
ARCS_ABI int getAdvancePlanContext(RUNTIME_MACHINE_HADNLER h, int tid,
struct PlanContext_C *result);
ARCS_ABI int getAdvancePtr(RUNTIME_MACHINE_HADNLER h, int tid);
ARCS_ABI int getMainPtr(RUNTIME_MACHINE_HADNLER h, int tid);
ARCS_ABI int getInterpPtr(RUNTIME_MACHINE_HADNLER h, int tid);
ARCS_ABI int getPlanContext(RUNTIME_MACHINE_HADNLER h, int tid,
struct PlanContext_C *result);
ARCS_ABI int getExecutionStatus(RUNTIME_MACHINE_HADNLER h,
struct ExecutionStatus_C *result);
ARCS_ABI int getExecutionStatus1(RUNTIME_MACHINE_HADNLER h,
struct ExecutionStatus1_C *result);
ARCS_ABI int loadProgram(RUNTIME_MACHINE_HADNLER h, const char *program);
ARCS_ABI int runProgram(RUNTIME_MACHINE_HADNLER h);
ARCS_ABI int start(RUNTIME_MACHINE_HADNLER h);
ARCS_ABI int stop(RUNTIME_MACHINE_HADNLER h);
ARCS_ABI int abort1(RUNTIME_MACHINE_HADNLER h);
ARCS_ABI int pause(RUNTIME_MACHINE_HADNLER h);
ARCS_ABI int step(RUNTIME_MACHINE_HADNLER h);
ARCS_ABI int setResumeWait(RUNTIME_MACHINE_HADNLER h, BOOL wait);
ARCS_ABI int resume(RUNTIME_MACHINE_HADNLER h);
ARCS_ABI RuntimeState_C getStatus(RUNTIME_MACHINE_HADNLER h);
ARCS_ABI RuntimeState_C getRuntimeState(RUNTIME_MACHINE_HADNLER h);
ARCS_ABI int setBreakPoint(RUNTIME_MACHINE_HADNLER h, int lineno);
ARCS_ABI int removeBreakPoint(RUNTIME_MACHINE_HADNLER h, int lineno);
ARCS_ABI int clearBreakPoints(RUNTIME_MACHINE_HADNLER h);
ARCS_ABI int timerStart(RUNTIME_MACHINE_HADNLER h, const char *name);
ARCS_ABI int timerStop(RUNTIME_MACHINE_HADNLER h, const char *name);
ARCS_ABI int timerReset(RUNTIME_MACHINE_HADNLER h, const char *name);
ARCS_ABI int timerDelete(RUNTIME_MACHINE_HADNLER h, const char *name);
ARCS_ABI double getTimer(RUNTIME_MACHINE_HADNLER h, const char *name);
ARCS_ABI int triggBegin(RUNTIME_MACHINE_HADNLER h, double distance,
double delay);
ARCS_ABI int triggEnd(RUNTIME_MACHINE_HADNLER h);
ARCS_ABI int triggInterrupt(RUNTIME_MACHINE_HADNLER h, double distance,
double delay);
ARCS_ABI int getTriggInterrupts(RUNTIME_MACHINE_HADNLER h, int *result);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,209 @@
/** @file script.h
* @brief 用于SCRIPT模块的交互,如向服务器发送脚本
*/
#ifndef AUBO_SDK_SCRIPT_H
#define AUBO_SDK_SCRIPT_H
#include <string>
#include <memory>
#include <functional>
#include <aubo/global_config.h>
namespace arcs {
namespace aubo_sdk {
class ScriptWriter
{
public:
virtual ~ScriptWriter() = default;
virtual ScriptWriter &append(const std::string &line) = 0;
virtual ScriptWriter &append(const char *buf, size_t len) = 0;
virtual ScriptWriter &moveJoint() = 0;
virtual ScriptWriter &moveLine() = 0;
virtual ScriptWriter &ifCondition() = 0;
virtual ScriptWriter &elseCondition() = 0;
virtual ScriptWriter &elseIfCondition() = 0;
virtual ScriptWriter &whileCondition() = 0;
virtual ScriptWriter &end() = 0;
};
/// SCRIPT客户端
class ARCS_ABI ScriptClient
{
public:
enum Event
{
Connected,
Disconnected,
};
ScriptClient(int mode = 0);
~ScriptClient();
/**
* 设置日志处理器
*
* 此函数可设置自定义的日志处理函数来处理日志消息。 \n
* Aubo SDK 有一套默认的日志系统,按照默认的格式输出到默认的文件。
* 如果用户不希望采用默认的格式或者不希望输出到默认的文件,那就可以通过这个接口重新自定义格式,或者输出路径。
* 这个函数可以将用户自定义的日志系统与 Aubo SDK 默认的日志系统合并。
*
* @note setLogHandler函数要放在即将触发的日志之前,
* 否则会按照默认的形式输出日志。
*
* @param handler 日志处理函数 \n
* 此日志处理函数的下定义如下: \n
* void handler(int level, const char* filename, int line, const
* std::string& message) \n
* level 表示日志等级 \n
* &nbsp; 0: LOGLEVEL_FATAL 严重的错误 \n
* &nbsp; 1: LOGLEVEL_ERROR 错误 \n
* &nbsp; 2: LOGLEVEL_WARNING 警告 \n
* &nbsp; 3: LOGLEVEL_INFO 通知 \n
* &nbsp; 4: LOGLEVEL_DEBUG 调试 \n
* &nbsp; 5: LOGLEVEL_BACKTRACE 跟踪 \n
* filename 表示文件名 \n
* line 表示代码行号 \n
* message 表示日志信息 \n
* @return 无
*/
void setLogHandler(
std::function<void(int /*level*/, const char * /*filename*/,
int /*line*/, const std::string & /*message*/)>
handler);
/**
* 连接到服务器
*
* @param ip IP地址
* @param port 端口号。SCRIPT端口号为30004
* @retval 0 连接成功
* @retval 1 在执行函数前,已连接
* @retval -1 连接失败
*/
int connect(const std::string &ip = "", int port = 0);
/**
* 是否处于连接状态
*
* @retval true 已连接
* @retval false 未连接
*/
bool hasConnected() const;
/**
* 登录
*
* @param usrname 用户名
* @param passwd 密码
* @retval 0 成功
* @retval -1 失败
*/
int login(const std::string &usrname, const std::string &passwd);
/**
* 返回客户端是否登录
*
* @retval true 已登录
* @retval false 未登录
*/
bool hasLogined();
/**
* 登出
*
* @return 0
*/
int logout();
/**
* 断开连接
*
* @retval 0 成功
* @retval -1 失败
*/
int disconnect();
/**
* 发送脚本文件
*
* 远程调用机器人的脚本
*
* @param path 文件在机器人端的路径
* @retval 0 成功
* @retval -1 失败
*/
int sendFile(const std::string &path);
/**
* 发送脚本内容
*
* 调用本地的脚本
*
* @param script 脚本内容
* @retval 0 成功
* @retval -1 失败
*/
int sendString(const std::string &script);
/**
* 使用ScriptWriter构建服务器脚本
*
* @param chunck_name
* @param cb
* @retval 0 成功
* @retval -1 失败
*/
int send(const std::string &chunck_name,
std::function<int(ScriptWriter &)> cb);
/**
* 设置服务器脚本的全局变量
*
* @param cb 脚本的全局变量
* @return 0
*/
int subscribeVariableUpdate(
std::function<void(const std::string &, const std::string &)> cb);
/**
* 设置服务器脚本的错误码
*
* @param cb 脚本的错误码
* @return 0
*/
ARCS_DEPRECATED int subscribeScriptError(
std::function<void(const std::string &)> cb);
int subscribeScriptError2(
std::function<void(const std::string &, const std::string &)> cb);
/**
* 设置事件处理
*
* @param cb
* @return
*/
int setEventHandler(std::function<void(int /*event*/)> cb);
private:
class Impl;
Impl *impl;
};
using ScriptClientPtr = std::shared_ptr<ScriptClient>;
} // namespace aubo_sdk
} // namespace arcs
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI arcs::aubo_sdk::ScriptClient *createScriptClient(int mode = 0);
ARCS_ABI void destroyScriptClient(arcs::aubo_sdk::ScriptClient *cli);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,32 @@
#ifndef AUBO_SDK_Serial_C_H
#define AUBO_SDK_Serial_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI int serialOpen(SERIAL_HANDLER h, const char *device, int baud,
float stop_bits, int even, const char *serial_name);
ARCS_ABI int serialClose(SERIAL_HANDLER h, const char *serial_name);
ARCS_ABI int serialReadByte(SERIAL_HANDLER h, const char *variable,
const char *serial_name);
ARCS_ABI int serialReadByteList(SERIAL_HANDLER h, int number,
const char *variable, const char *serial_name);
ARCS_ABI int serialReadString(SERIAL_HANDLER h, const char *variable,
const char *serial_name, const char *prefix,
const char *suffix, BOOL interpret_escape);
ARCS_ABI int serialSendByte(SERIAL_HANDLER h, char value,
const char *serial_name);
ARCS_ABI int serialSendInt(SERIAL_HANDLER h, int value,
const char *serial_name);
ARCS_ABI int serialSendLine(SERIAL_HANDLER h, const char *str,
const char *serial_name);
ARCS_ABI int serialSendString(SERIAL_HANDLER h, const char *str,
const char *serial_name);
ARCS_ABI int serialSendAllString(SERIAL_HANDLER h, BOOL is_check,
const char *str, const char *serial_name);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,38 @@
#ifndef AUBO_SDK_Socket_C_H
#define AUBO_SDK_Socket_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI int socketOpen(SOCKET_HANDLER h, const char *address, int port,
const char *socket_name);
ARCS_ABI int socketClose(SOCKET_HANDLER h, const char *socket_name);
ARCS_ABI int socketReadAsciiFloat(SOCKET_HANDLER h, int number,
const char *variable,
const char *socket_name);
ARCS_ABI int socketReadBinaryInteger(SOCKET_HANDLER h, int number,
const char *variable,
const char *socket_name);
ARCS_ABI int socketReadByteList(SOCKET_HANDLER h, int number,
const char *variable, const char *socket_name);
ARCS_ABI int socketReadString(SOCKET_HANDLER h, const char *variable,
const char *socket_name, const char *prefix,
const char *suffix, BOOL interpret_escape);
ARCS_ABI int socketReadAllString(SOCKET_HANDLER h, const char *variable,
const char *socket_name);
ARCS_ABI int socketSendByte(SOCKET_HANDLER h, char value,
const char *socket_name);
ARCS_ABI int socketSendInt(SOCKET_HANDLER h, int value,
const char *socket_name);
ARCS_ABI int socketSendLine(SOCKET_HANDLER h, const char *str,
const char *socket_name);
ARCS_ABI int socketSendString(SOCKET_HANDLER h, const char *str,
const char *socket_name);
ARCS_ABI int socketSendAllString(SOCKET_HANDLER h, BOOL is_check,
const char *str, const char *socket_name);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,22 @@
#ifndef AUBO_SDK_SyncMove_C_H
#define AUBO_SDK_SyncMove_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI int syncMoveOn(SYNC_MOVE_HANDLER h, const char *syncident,
const char **taskset);
ARCS_ABI BOOL syncMoveSegment(SYNC_MOVE_HANDLER h, int id);
ARCS_ABI int syncMoveOff(SYNC_MOVE_HANDLER h, const char *syncident);
ARCS_ABI int syncMoveUndo(SYNC_MOVE_HANDLER h);
ARCS_ABI int waitSyncTasks(SYNC_MOVE_HANDLER h, const char *syncident,
const char **taskset);
ARCS_ABI BOOL isSyncMoveOn(SYNC_MOVE_HANDLER h);
ARCS_ABI int syncMoveSuspend(SYNC_MOVE_HANDLER h);
ARCS_ABI int syncMoveResume(SYNC_MOVE_HANDLER h);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,18 @@
#ifndef AUBO_SDK_SystemInfo_C_H
#define AUBO_SDK_SystemInfo_C_H
#include <aubo_sdk/type_def_c.h>
#ifdef __cplusplus
extern "C" {
#endif
ARCS_ABI int getControlSoftwareVersionCode(SYSTEM_INFO_HANDLER h);
ARCS_ABI int getControlSoftwareFullVersion(SYSTEM_INFO_HANDLER h, char *result);
ARCS_ABI int getInterfaceVersionCode(SYSTEM_INFO_HANDLER h);
ARCS_ABI int getControlSoftwareBuildDate(SYSTEM_INFO_HANDLER h, char *result);
ARCS_ABI int getControlSoftwareVersionHash(SYSTEM_INFO_HANDLER h, char *result);
ARCS_ABI uint64_t getControlSystemTime(SYSTEM_INFO_HANDLER h);
#ifdef __cplusplus
}
#endif
#endif

Some files were not shown because too many files have changed in this diff Show More