📄 realtimedemo.cpp
字号:
//-----------------------------------------------------------------------------
// (c) 2002 by Basler Vision Technologies
// Section: Vision Components
// Project: BCAM
// $Header: RealtimeDemo.cpp, 10, 04.07.2006 12:40:13, Happe, A.$
//-----------------------------------------------------------------------------
/**
\file RealtimeDemo.cpp
\brief Main implementation file of the realtime demo.
* This program measures the jitter of the wake up time when waiting for
* a new buffer arriving at the completion port.
*
* The image acquisition is done by a separate thread. To reduce the jitter, the
* thread's priority level is raised above 15 (realtime class), without changing
* the priority class of the whole process.
*
* The main thread produces a high computational load.
*/
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <assert.h>
#define BUFFER_SIZE 645536
/// Number of buffers circulating
const int NumImageBuffers = 5;
/// Number of images to acquire
const int NumGrabs = 250;
/// Each NumSamples buffers the statistic will be evaluated
const int NumSamples = 25;
/// The absolute thread priority of the thread waiting at the completion port
const int GrabThreadPriority = 26;
/// The thread priority of the main thread doing some computatations
//const int MainThreadPriority = THREAD_PRIORITY_IDLE;
//const int MainThreadPriority = THREAD_PRIORITY_TIME_CRITICAL;
//const int MainThreadPriority = THREAD_PRIORITY_NORMAL;
const int MainThreadPriority = THREAD_PRIORITY_ABOVE_NORMAL;
/// The priority class of the process
const int ProcessPriorityClass = NORMAL_PRIORITY_CLASS;
//const int ProcessPriorityClass = ABOVE_NORMAL_PRIORITY_CLASS;
//const int ProcessPriorityClass = HIGH_PRIORITY_CLASS;
/// Reduces the stress.
const int SleepTime = 0;
// const int SleepTime = 10;
/// Number of loops the stress function will perform until it eventually sleeps for a while
const int Loops = 10000000L;
/// Enables the search for missing images. Ensure that the walking stripes test image
/// is activated, see below
const bool CheckMissingImages = true;
// const bool CheckMissingImages = false;
/// The walking stripes test image. The value depends on the
/// camera model. Examples: A101fc: TestImage_3, A301fc: TestImage_2
const ImageOnMode Testimage = TestImage_2;
// const ImageOnMode Testimage = TestImage_3;
/// Sets the process' priority class
void SetPriorityClass(int PriorityClass)
{
HANDLE hProcess = ::GetCurrentProcess();
BOOL res = ::SetPriorityClass(hProcess, PriorityClass);
assert(res);
int NewPriorityClass = ::GetPriorityClass(hProcess);
assert(NewPriorityClass == PriorityClass);
}
/// Sets the current thread's priority
void SetCurrentThreadPriority(int Priority)
{
HANDLE hThread = ::GetCurrentThread();
BOOL res = ::SetThreadPriority(hThread, Priority);
assert(res);
int NewPriotrity = ::GetThreadPriority(hThread);
assert(NewPriotrity == Priority);
}
/// Evaluates statistics
class CStatistics
{
public:
/// Constructor
CStatistics()
{
Reset();
}
/// Add a sample
void AddSample(double Value)
{
if(m_NumSamples == 0)
{
m_Min = Value;
m_Max = Value;
}
else if(Value > m_Max)
{
m_Max = Value;
}
else if(Value < m_Min)
{
m_Min = Value;
}
m_Sum += Value;
++m_NumSamples;
}
/// Reset the statistics
void Reset()
{
m_NumSamples = 0;
m_Sum = 0.0;
m_Min = 0.0;
m_Max = 0.0;
}
/// Computes the mean
double GetMean()
{
assert(m_NumSamples > 0);
return m_Sum / m_NumSamples;
}
/// Gets the minimum
double GetMin()
{
assert(m_NumSamples > 0);
return m_Min;
}
/// Gets the maximum
double GetMax()
{
assert(m_NumSamples > 0);
return m_Max;
}
protected:
/// Number of samples
long m_NumSamples;
/// Sum of samples
double m_Sum;
/// minimum sample
double m_Min;
/// maximum sample
double m_Max;
};
/// Takes the time
class CStopWatch
{
public:
/// constructor, starts time measurement
CStopWatch()
{
Start();
}
/// Start the stop watch
void Start()
{
QueryPerformanceCounter(&m_StartTime);
}
/// Stop. The elapsed time is returned. The stop watch may be started again
double Stop(bool StartAgain)
{
QueryPerformanceCounter(&m_StopTime);
double theElapsedTime = ElapsedTime();
if(StartAgain)
m_StartTime = m_StopTime;
return theElapsedTime;
}
/// Return the elapsed time in seconds between start() and stop()
double ElapsedTime()
{
LARGE_INTEGER timerFrequency;
QueryPerformanceFrequency(&timerFrequency);
__int64 oldTicks = ((__int64)m_StartTime.HighPart << 32) + (__int64)m_StartTime.LowPart;
__int64 newTicks = ((__int64)m_StopTime.HighPart << 32) + (__int64)m_StopTime.LowPart;
long double timeDifference = (long double) (newTicks - oldTicks);
long double ticksPerSecond = (long double) (((__int64)timerFrequency.HighPart << 32)
+ (__int64)timerFrequency.LowPart);
return (double)(timeDifference / ticksPerSecond);
}
protected:
/// zero-point for time measurment
LARGE_INTEGER m_StartTime;
/// last time stamp
LARGE_INTEGER m_StopTime;
};
/// Encapsulates the acquisition thread waiting at the completion port and feeding in new buffers
class CGrabThread
{
public:
/// constructor
CGrabThread( CBcam* pBcam, BYTE **ppBuffers, unsigned long ImageBufferSize, HANDLE running, HANDLE finished) :
m_pBcam(pBcam),
m_ppBuffers(ppBuffers),
m_ImageBufferSize(ImageBufferSize),
m_Running(running),
m_Finished(finished)
{
m_OutputBuffer = new char[BUFFER_SIZE];
if ( m_OutputBuffer != NULL )
ZeroMemory(m_OutputBuffer, BUFFER_SIZE);
}
/// Create the thread
void Create()
{
m_hThread = ::CreateThread(NULL, 0, ThreadMain, (LPVOID)this, 0, &m_ThreadId);
}
/// Wait until the thread terminates
DWORD WaitForDeath(DWORD Timeout)
{
return ::WaitForSingleObject(m_hThread, Timeout);
}
/// Return the thread handle
operator HANDLE() { return m_hThread; }
~CGrabThread()
{
if ( m_OutputBuffer != NULL )
delete [] m_OutputBuffer;
}
protected:
/// Prints to a buffer
int printf(const char* format, ...)
{
static unsigned int idx = 0;
int res = 0;
if ( m_OutputBuffer && idx < BUFFER_SIZE - 1 )
{
va_list arguments;
va_start (arguments, format);
res = _vsnprintf(m_OutputBuffer + idx, BUFFER_SIZE - idx - 1, format, arguments);
if ( res == -1 )
// buffer overflow
idx = BUFFER_SIZE;
else
idx += res;
}
return res;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -