Add proc period support

This commit is contained in:
John Zhao 2018-06-01 10:32:36 +08:00
parent e4fddf2b8d
commit 5a836b2ae3
13 changed files with 80 additions and 25 deletions

View File

@ -15,10 +15,13 @@
#include <glog/logging.h> #include <glog/logging.h>
#include <utility>
MYNTEYE_BEGIN_NAMESPACE MYNTEYE_BEGIN_NAMESPACE
DepthProcessor::DepthProcessor() : Processor() { DepthProcessor::DepthProcessor(std::int32_t proc_period)
VLOG(2) << __func__; : Processor(std::move(proc_period)) {
VLOG(2) << __func__ << ": proc_period=" << proc_period;
} }
DepthProcessor::~DepthProcessor() { DepthProcessor::~DepthProcessor() {

View File

@ -25,7 +25,7 @@ class DepthProcessor : public Processor {
public: public:
static constexpr auto &&NAME = "DepthProcessor"; static constexpr auto &&NAME = "DepthProcessor";
DepthProcessor(); explicit DepthProcessor(std::int32_t proc_period = 0);
virtual ~DepthProcessor(); virtual ~DepthProcessor();
std::string Name() override; std::string Name() override;

View File

@ -17,10 +17,14 @@
#include <glog/logging.h> #include <glog/logging.h>
#include <utility>
MYNTEYE_BEGIN_NAMESPACE MYNTEYE_BEGIN_NAMESPACE
DisparityNormalizedProcessor::DisparityNormalizedProcessor() : Processor() { DisparityNormalizedProcessor::DisparityNormalizedProcessor(
VLOG(2) << __func__; std::int32_t proc_period)
: Processor(std::move(proc_period)) {
VLOG(2) << __func__ << ": proc_period=" << proc_period;
} }
DisparityNormalizedProcessor::~DisparityNormalizedProcessor() { DisparityNormalizedProcessor::~DisparityNormalizedProcessor() {

View File

@ -25,7 +25,7 @@ class DisparityNormalizedProcessor : public Processor {
public: public:
static constexpr auto &&NAME = "DisparityNormalizedProcessor"; static constexpr auto &&NAME = "DisparityNormalizedProcessor";
DisparityNormalizedProcessor(); explicit DisparityNormalizedProcessor(std::int32_t proc_period = 0);
virtual ~DisparityNormalizedProcessor(); virtual ~DisparityNormalizedProcessor();
std::string Name() override; std::string Name() override;

View File

@ -17,10 +17,13 @@
#include <glog/logging.h> #include <glog/logging.h>
#include <utility>
MYNTEYE_BEGIN_NAMESPACE MYNTEYE_BEGIN_NAMESPACE
DisparityProcessor::DisparityProcessor() : Processor() { DisparityProcessor::DisparityProcessor(std::int32_t proc_period)
VLOG(2) << __func__; : Processor(std::move(proc_period)) {
VLOG(2) << __func__ << ": proc_period=" << proc_period;
int sgbmWinSize = 3; int sgbmWinSize = 3;
int numberOfDisparities = 64; int numberOfDisparities = 64;

View File

@ -31,7 +31,7 @@ class DisparityProcessor : public Processor {
public: public:
static constexpr auto &&NAME = "DisparityProcessor"; static constexpr auto &&NAME = "DisparityProcessor";
DisparityProcessor(); explicit DisparityProcessor(std::int32_t proc_period = 0);
virtual ~DisparityProcessor(); virtual ~DisparityProcessor();
std::string Name() override; std::string Name() override;

View File

@ -21,8 +21,9 @@
MYNTEYE_BEGIN_NAMESPACE MYNTEYE_BEGIN_NAMESPACE
PointsProcessor::PointsProcessor(cv::Mat Q) : Processor(), Q_(std::move(Q)) { PointsProcessor::PointsProcessor(cv::Mat Q, std::int32_t proc_period)
VLOG(2) << __func__; : Processor(std::move(proc_period)), Q_(std::move(Q)) {
VLOG(2) << __func__ << ": proc_period=" << proc_period;
} }
PointsProcessor::~PointsProcessor() { PointsProcessor::~PointsProcessor() {

View File

@ -27,7 +27,7 @@ class PointsProcessor : public Processor {
public: public:
static constexpr auto &&NAME = "PointsProcessor"; static constexpr auto &&NAME = "PointsProcessor";
explicit PointsProcessor(cv::Mat Q); explicit PointsProcessor(cv::Mat Q, std::int32_t proc_period = 0);
virtual ~PointsProcessor(); virtual ~PointsProcessor();
std::string Name() override; std::string Name() override;

View File

@ -17,10 +17,13 @@
#include <utility> #include <utility>
#include "internal/times.h"
MYNTEYE_BEGIN_NAMESPACE MYNTEYE_BEGIN_NAMESPACE
Processor::Processor() Processor::Processor(std::int32_t proc_period)
: activated_(false), : proc_period_(std::move(proc_period)),
activated_(false),
input_ready_(false), input_ready_(false),
idle_(true), idle_(true),
dropped_count_(0), dropped_count_(0),
@ -146,10 +149,34 @@ std::uint64_t Processor::GetDroppedCount() {
void Processor::Run() { void Processor::Run() {
VLOG(2) << Name() << " thread start"; VLOG(2) << Name() << " thread start";
auto sleep = [this](const times::system_clock::time_point &time_beg) {
if (proc_period_ > 0) {
static times::system_clock::time_point time_prev = time_beg;
auto &&time_elapsed_ms =
times::count<times::milliseconds>(times::now() - time_prev);
time_prev = time_beg;
if (time_elapsed_ms < proc_period_) {
VLOG(2) << Name() << " process cost "
<< times::count<times::milliseconds>(times::now() - time_beg)
<< " ms, sleep " << (proc_period_ - time_elapsed_ms) << " ms";
std::this_thread::sleep_for(
std::chrono::milliseconds(proc_period_ - time_elapsed_ms));
return;
}
}
VLOG(2) << Name() << " process cost "
<< times::count<times::milliseconds>(times::now() - time_beg)
<< " ms";
};
while (true) { while (true) {
std::unique_lock<std::mutex> lk(mtx_input_ready_); std::unique_lock<std::mutex> lk(mtx_input_ready_);
cond_input_ready_.wait(lk, [this] { return input_ready_; }); cond_input_ready_.wait(lk, [this] { return input_ready_; });
auto &&time_beg = times::now();
if (!activated_) { if (!activated_) {
SetIdle(true); SetIdle(true);
input_ready_ = false; input_ready_ = false;
@ -194,6 +221,8 @@ void Processor::Run() {
SetIdle(true); SetIdle(true);
input_ready_ = false; input_ready_ = false;
sleep(time_beg);
} }
VLOG(2) << Name() << " thread end"; VLOG(2) << Name() << " thread end";
} }

View File

@ -37,7 +37,7 @@ class Processor /*: public std::enable_shared_from_this<Processor>*/ {
using ProcessCallback = std::function<bool( using ProcessCallback = std::function<bool(
Object *const in, Object *const out, Processor *const parent)>; Object *const in, Object *const out, Processor *const parent)>;
Processor(); explicit Processor(std::int32_t proc_period = 0);
virtual ~Processor(); virtual ~Processor();
virtual std::string Name(); virtual std::string Name();
@ -80,6 +80,8 @@ class Processor /*: public std::enable_shared_from_this<Processor>*/ {
void SetIdle(bool idle); void SetIdle(bool idle);
std::int32_t proc_period_;
bool activated_; bool activated_;
bool input_ready_; bool input_ready_;

View File

@ -18,13 +18,16 @@
#include <glog/logging.h> #include <glog/logging.h>
#include <utility>
#include "device/device.h" #include "device/device.h"
MYNTEYE_BEGIN_NAMESPACE MYNTEYE_BEGIN_NAMESPACE
RectifyProcessor::RectifyProcessor(std::shared_ptr<Device> device) RectifyProcessor::RectifyProcessor(
: Processor() { std::shared_ptr<Device> device, std::int32_t proc_period)
VLOG(2) << __func__; : Processor(std::move(proc_period)) {
VLOG(2) << __func__ << ": proc_period=" << proc_period;
InitParams( InitParams(
device->GetIntrinsics(Stream::LEFT), device->GetIntrinsics(Stream::RIGHT), device->GetIntrinsics(Stream::LEFT), device->GetIntrinsics(Stream::RIGHT),
device->GetExtrinsics(Stream::LEFT, Stream::RIGHT)); device->GetExtrinsics(Stream::LEFT, Stream::RIGHT));

View File

@ -31,7 +31,8 @@ class RectifyProcessor : public Processor {
public: public:
static constexpr auto &&NAME = "RectifyProcessor"; static constexpr auto &&NAME = "RectifyProcessor";
explicit RectifyProcessor(std::shared_ptr<Device> device); RectifyProcessor(
std::shared_ptr<Device> device, std::int32_t proc_period = 0);
virtual ~RectifyProcessor(); virtual ~RectifyProcessor();
std::string Name() override; std::string Name() override;

View File

@ -29,6 +29,12 @@
#include "api/processor/rectify_processor.h" #include "api/processor/rectify_processor.h"
#include "device/device.h" #include "device/device.h"
#define RECTIFY_PROC_PERIOD 0
#define DISPARITY_PROC_PERIOD 0
#define DISPARITY_NORM_PROC_PERIOD 0
#define POINTS_PROC_PERIOD 0
#define DEPTH_PROC_PERIOD 0
MYNTEYE_BEGIN_NAMESPACE MYNTEYE_BEGIN_NAMESPACE
namespace { namespace {
@ -383,13 +389,16 @@ void Synthetic::DisableStreamData(const Stream &stream, std::uint32_t depth) {
} }
void Synthetic::InitProcessors() { void Synthetic::InitProcessors() {
auto &&rectify_processor = std::make_shared<RectifyProcessor>(api_->device()); auto &&rectify_processor =
auto &&disparity_processor = std::make_shared<DisparityProcessor>(); std::make_shared<RectifyProcessor>(api_->device(), RECTIFY_PROC_PERIOD);
auto &&disparity_processor =
std::make_shared<DisparityProcessor>(DISPARITY_PROC_PERIOD);
auto &&disparitynormalized_processor = auto &&disparitynormalized_processor =
std::make_shared<DisparityNormalizedProcessor>(); std::make_shared<DisparityNormalizedProcessor>(
auto &&points_processor = DISPARITY_NORM_PROC_PERIOD);
std::make_shared<PointsProcessor>(rectify_processor->Q); auto &&points_processor = std::make_shared<PointsProcessor>(
auto &&depth_processor = std::make_shared<DepthProcessor>(); rectify_processor->Q, POINTS_PROC_PERIOD);
auto &&depth_processor = std::make_shared<DepthProcessor>(DEPTH_PROC_PERIOD);
using namespace std::placeholders; // NOLINT using namespace std::placeholders; // NOLINT
rectify_processor->SetProcessCallback( rectify_processor->SetProcessCallback(