Commit 12fbd24a authored by Jussi Lindgren's avatar Jussi Lindgren
Browse files

Applications: Code cleanup of drift correction in the AS

parent 38cdf18d
......@@ -292,7 +292,7 @@ CAcquisitionServerGUI::~CAcquisitionServerGUI(void)
::fprintf(l_pFile, "# Last Preferences set in the acquisition server\n");
::fprintf(l_pFile, "AcquisitionServer_DriftCorrectionPolicy = %s\n", m_pAcquisitionServer->m_oDriftCorrection.getDriftCorrectionPolicyStr().toASCIIString());
::fprintf(l_pFile, "AcquisitionServer_JitterEstimationCountForDrift = %llu\n", m_pAcquisitionServer->m_oDriftCorrection.getJitterEstimationCountForDrift());
::fprintf(l_pFile, "AcquisitionServer_DriftToleranceDuration = %llu\n", m_pAcquisitionServer->m_oDriftCorrection.getDriftToleranceDuration());
::fprintf(l_pFile, "AcquisitionServer_DriftToleranceDuration = %llu\n", m_pAcquisitionServer->m_oDriftCorrection.getDriftToleranceDurationMs());
::fprintf(l_pFile, "AcquisitionServer_OverSamplingFactor = %llu\n", m_pAcquisitionServer->getOversamplingFactor());
::fprintf(l_pFile, "AcquisitionServer_ChannelSelection = %s\n", (m_pAcquisitionServer->isChannelSelectionRequested() ? "True" : "False"));
::fprintf(l_pFile, "AcquisitionServer_NaNReplacementPolicy = %s\n", m_pAcquisitionServer->getNaNReplacementPolicyStr().toASCIIString());
......@@ -501,10 +501,10 @@ void CAcquisitionServerGUI::setClientCount(uint32 ui32ClientCount)
::gtk_label_set_label(GTK_LABEL(gtk_builder_get_object(m_pBuilderInterface, "label_connected_host_count")), l_sLabel);
}
void CAcquisitionServerGUI::setDrift(float64 f64Drift)
void CAcquisitionServerGUI::setDriftMs(float64 f64DriftMs)
{
const uint64 l_ui64DriftToleranceDuration = m_pAcquisitionServer->m_oDriftCorrection.getDriftToleranceDuration();
float64 l_f64DriftRatio = f64Drift / static_cast<float64>(l_ui64DriftToleranceDuration);
const uint64 l_ui64DriftToleranceDurationMs = m_pAcquisitionServer->m_oDriftCorrection.getDriftToleranceDurationMs();
float64 l_f64DriftRatio = f64DriftMs / static_cast<float64>(l_ui64DriftToleranceDurationMs);
boolean l_bDriftWarning=false;
char l_sLabel[1024];
......@@ -537,11 +537,11 @@ void CAcquisitionServerGUI::setDrift(float64 f64Drift)
if(l_bDriftWarning)
{
::sprintf(l_sLabel, "<b>Device drift is too high</b> : %3.2lf ms\n<small>late &lt;-- (tolerance is set to %llu ms) --&gt; early</small>", f64Drift, l_ui64DriftToleranceDuration);
::sprintf(l_sLabel, "<b>Device drift is too high</b> : %3.2lf ms\n<small>late &lt;-- (tolerance is set to %llu ms) --&gt; early</small>", f64DriftMs, l_ui64DriftToleranceDurationMs);
}
else
{
::sprintf(l_sLabel, "Device drift : %3.2lf ms\n<small>late &lt;-- (tolerance is set to %llu ms) --&gt; early</small>", f64Drift, l_ui64DriftToleranceDuration);
::sprintf(l_sLabel, "Device drift : %3.2lf ms\n<small>late &lt;-- (tolerance is set to %llu ms) --&gt; early</small>", f64DriftMs, l_ui64DriftToleranceDurationMs);
}
::gtk_label_set_markup(GTK_LABEL(gtk_builder_get_object(m_pBuilderInterface, "label_drift")), l_sLabel);
}
......@@ -761,7 +761,7 @@ void CAcquisitionServerGUI::buttonPreferencePressedCB(::GtkButton* pButton)
::GtkToggleButton* l_ChannelSelection=GTK_TOGGLE_BUTTON(::gtk_builder_get_object(l_pInterface, "checkbutton_channel_selection"));
::gtk_combo_box_set_active(l_pDriftCorrectionPolicy, (int)m_pAcquisitionServer->m_oDriftCorrection.getDriftCorrectionPolicy());
::gtk_spin_button_set_value(l_pDriftTolerance, (gdouble)m_pAcquisitionServer->m_oDriftCorrection.getDriftToleranceDuration());
::gtk_spin_button_set_value(l_pDriftTolerance, (gdouble)m_pAcquisitionServer->m_oDriftCorrection.getDriftToleranceDurationMs());
::gtk_spin_button_set_value(l_pJitterMeasureCount, (gdouble)m_pAcquisitionServer->m_oDriftCorrection.getJitterEstimationCountForDrift());
::gtk_spin_button_set_value(l_pOverSamplingFactor, (gdouble)m_pAcquisitionServer->getOversamplingFactor());
::gtk_toggle_button_set_active(l_ChannelSelection, m_pAcquisitionServer->isChannelSelectionRequested()?TRUE:FALSE);
......@@ -829,13 +829,13 @@ void CAcquisitionServerGUI::buttonPreferencePressedCB(::GtkButton* pButton)
case GTK_RESPONSE_YES:
m_pAcquisitionServer->setNaNReplacementPolicy((ENaNReplacementPolicy)::gtk_combo_box_get_active(l_pNaNReplacementPolicy));
m_pAcquisitionServer->m_oDriftCorrection.setDriftCorrectionPolicy((EDriftCorrectionPolicy)::gtk_combo_box_get_active(l_pDriftCorrectionPolicy));
m_pAcquisitionServer->m_oDriftCorrection.setDriftToleranceDuration(::gtk_spin_button_get_value_as_int(l_pDriftTolerance));
m_pAcquisitionServer->m_oDriftCorrection.setDriftToleranceDurationMs(::gtk_spin_button_get_value_as_int(l_pDriftTolerance));
m_pAcquisitionServer->m_oDriftCorrection.setJitterEstimationCountForDrift(::gtk_spin_button_get_value_as_int(l_pJitterMeasureCount));
m_pAcquisitionServer->setOversamplingFactor(::gtk_spin_button_get_value_as_int(l_pOverSamplingFactor));
m_pAcquisitionServer->setChannelSelectionRequest(::gtk_toggle_button_get_active(l_ChannelSelection)?true:false);
// Side-effect: Update the tolerance ms
setDrift(0);
setDriftMs(0);
for (size_t setting_index = 0; setting_index < m_vPluginProperties.size(); ++setting_index)
{
......
......@@ -37,7 +37,7 @@ namespace OpenViBEAcquisitionServer
OpenViBE::uint32 getTCPPort(void);
OpenViBEAcquisitionServer::IHeader& getHeaderCopy(void);
void setClientCount(OpenViBE::uint32 ui32ClientCount);
void setDrift(OpenViBE::float64 f64Drift);
void setDriftMs(OpenViBE::float64 f64DriftMs);
void setImpedance(OpenViBE::uint32 ui32ChannelIndex, OpenViBE::float64 f64Impedance);
void disconnect(void);
......
......@@ -31,14 +31,14 @@ namespace OpenViBEAcquisitionServer
void main(void)
{
OpenViBE::float64 l_f64LastDrift=-1;
OpenViBE::float64 l_f64LastDriftMs=-1;
OpenViBE::boolean l_bFinished=false;
while(!l_bFinished)
{
OpenViBE::boolean l_bShouldSleep=false;
OpenViBE::boolean l_bShouldDisconnect=false;
OpenViBE::uint32 i, l_ui32ClientCount;
OpenViBE::float64 l_f64Drift;
OpenViBE::float64 l_f64DriftMs;
{
boost::mutex::scoped_lock m_oProtectionLock(m_rAcquisitionServer.m_oProtectionMutex);
......@@ -46,7 +46,7 @@ namespace OpenViBEAcquisitionServer
m_oProtectionLock.unlock();
l_ui32ClientCount=m_rAcquisitionServer.getClientCount();
l_f64Drift=(m_ui32Status==Status_Started?m_rAcquisitionServer.m_oDriftCorrection.getDrift():0);
l_f64DriftMs=(m_ui32Status==Status_Started?m_rAcquisitionServer.m_oDriftCorrection.getDriftMs():0);
switch(m_ui32Status)
{
......@@ -101,12 +101,12 @@ namespace OpenViBEAcquisitionServer
m_vImpedanceLast=m_vImpedance;
}
if(l_f64Drift!=l_f64LastDrift)
if(l_f64DriftMs!=l_f64LastDriftMs)
{
gdk_threads_enter();
m_rGUI.setDrift(l_f64Drift);
m_rGUI.setDriftMs(l_f64DriftMs);
gdk_threads_leave();
l_f64LastDrift=l_f64Drift;
l_f64LastDriftMs=l_f64DriftMs;
}
if(l_bShouldDisconnect)
......
......@@ -6,9 +6,6 @@
#include <vector>
#include <list>
// ifdef OV_ESTIMATE_BOOST
// #include <boost/thread.hpp>
namespace OpenViBEAcquisitionServer
{
typedef enum
......@@ -36,19 +33,29 @@ namespace OpenViBEAcquisitionServer
virtual OpenViBE::boolean start(OpenViBE::uint32 ui32SamplingFrequency, OpenViBE::uint64 ui64StartTime);
virtual void stop(void);
// The main functions to estimate and correct drift
// Estimate the drift
//
// \param ui64NewSamples [in] : How many samples the driver returned
virtual OpenViBE::boolean estimateDrift(const OpenViBE::uint64 ui64NewSamples);
// i64correction, ui64TotalSamples, vPendingBuffers and oPendingStimulationSet may be modified by the correction.
virtual OpenViBE::boolean correctDrift(OpenViBE::int64 i64Correction, OpenViBE::uint64& ui64TotalSamples,
// Request a drift correction
//
// \param i64correction [in] : the number of samples to correct
// \param ui64TotalSamples [out] : Number of total samples after correction for drift
// \param vPendingBuffers [in/out] : The sample buffer to be corrected
// \param oPendingStimulationSet [in/out] : The stimulation set to be realigned
// \param vPaddingBuffer[in] : The sample to repeatedly add if i64Correction > 0
virtual OpenViBE::boolean correctDrift(const OpenViBE::int64 i64Correction, OpenViBE::uint64& ui64TotalSamples,
std::vector < std::vector < OpenViBE::float32 > >& vPendingBuffers, OpenViBE::CStimulationSet& oPendingStimulationSet,
const std::vector < OpenViBE::float32 >& vPaddingBuffer );
// Status functions
virtual OpenViBE::boolean isActive(void) const { return m_bIsActive; };
// Prints various statistics but only if drift tolerance was exceeded
virtual void printStats(void) const;
// Result getters
virtual OpenViBE::float64 getDrift(void) const; // current drift, in ms.
virtual OpenViBE::float64 getDriftMs(void) const; // current drift, in ms.
virtual OpenViBE::float64 getDriftTooFastMax(void) const; // maximum positive drift observed, in ms. (driver gave too many samples)
virtual OpenViBE::float64 getDriftTooSlowMax(void) const; // maximum negative drift observed, in ms. (driver gave too few samples)
virtual OpenViBE::int64 getDriftSampleCount(void) const; // current drift, in samples
......@@ -63,8 +70,8 @@ namespace OpenViBEAcquisitionServer
OpenViBE::CString getDriftCorrectionPolicyStr(void) const;
OpenViBE::boolean setDriftCorrectionPolicy(OpenViBEAcquisitionServer::EDriftCorrectionPolicy eDriftCorrectionPolicy);
OpenViBE::uint64 getDriftToleranceDuration(void) const;
OpenViBE::boolean setDriftToleranceDuration(OpenViBE::uint64 ui64DriftToleranceDuration);
OpenViBE::uint64 getDriftToleranceDurationMs(void) const; // In milliseconds
OpenViBE::boolean setDriftToleranceDurationMs(OpenViBE::uint64 ui64DriftToleranceDuration);
OpenViBE::uint64 getJitterEstimationCountForDrift(void) const;
OpenViBE::boolean setJitterEstimationCountForDrift(OpenViBE::uint64 ui64JitterEstimationCountForDrift);
......@@ -73,6 +80,9 @@ namespace OpenViBEAcquisitionServer
void reset(void);
// Computes jitter in fractional samples
OpenViBE::float64 computeJitter(const OpenViBE::uint64 ui64CurrentTime);
const OpenViBE::Kernel::IKernelContext& m_rKernelContext;
// State of the drift correction
......@@ -82,7 +92,7 @@ namespace OpenViBEAcquisitionServer
// Parameters
OpenViBEAcquisitionServer::EDriftCorrectionPolicy m_eDriftCorrectionPolicy;
OpenViBE::uint64 m_ui64DriftToleranceDuration;
OpenViBE::uint64 m_ui64DriftToleranceDurationMs;
OpenViBE::int64 m_i64DriftToleranceSampleCount;
OpenViBE::int64 m_i64InnerLatencySampleCount;
OpenViBE::uint64 m_ui64JitterEstimationCountForDrift;
......@@ -93,7 +103,7 @@ namespace OpenViBEAcquisitionServer
OpenViBE::uint64 m_ui64CorrectedSampleCount;
OpenViBE::float64 m_f64DriftEstimate; // In subsample accuracy, e.g. 1.2 samples.
OpenViBE::float64 m_f64DriftEstimateTooFastMax; // maximum over time
OpenViBE::float64 m_f64DriftEstimateTooSlowMax; // minumum over time
OpenViBE::float64 m_f64DriftEstimateTooSlowMax; // minimum over time
// Stats
OpenViBE::int64 m_i64DriftCorrectionSampleCountAdded;
......@@ -103,9 +113,6 @@ namespace OpenViBEAcquisitionServer
OpenViBE::uint64 m_ui64StartTime;
OpenViBE::uint64 m_ui64LastEstimationTime;
// ifdef OV_ESTIMATE_BOOST
// boost::posix_time::ptime m_oStartTime;
// Jitter estimation buffer. Each entry is the difference between the expected number of
// samples and the received number of samples per each call to estimateDrift(). The buffer has subsample accuracy to avoid rounding errors.
// -> The average of the buffer items is the current aggregated drift estimate (in samples), convertable to ms by getDrift().
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment