Difference between revisions of "Programming Tutorial:Implementing a Data Acquisition Module"

From BCI2000 Wiki
Jump to: navigation, search
(Writing the ADC Header File)
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
Data acquisition modules are factored into code required for any
+
Data acquisition modules are factored into  
hardware,
+
*code required for any hardware, and
and code required to access a specific hardware.
+
*code required to access a specific hardware.
You provide a function that waits for and reads A/D
+
You provide only specific code. This is in a function that waits for and reads A/D
data (line 3 in the EEG source pseudo code of [[Modules:Data Acquisition]]),
+
data (line 3 in the pseudo code shown at [[Technical Reference:Core Modules]]),
 
together with some helper functions that perform initialization and
 
together with some helper functions that perform initialization and
 
cleanup tasks.
 
cleanup tasks.
Line 9: Line 9:
 
<tt>GenericADC</tt>.
 
<tt>GenericADC</tt>.
  
==Scenario==
+
==Example Scenario==
 
Your ''Tachyon Corporation''  A/D card comes with a C-style
 
Your ''Tachyon Corporation''  A/D card comes with a C-style
 
software interface
 
software interface
Line 31: Line 31:
 
first argument.
 
first argument.
 
Each of the functions will return zero if everything went well, otherwise
 
Each of the functions will return zero if everything went well, otherwise
some error value will be given.
+
some error value will be returned.
 
Luckily, ''Tachyon Corporation''  gives you just
 
Luckily, ''Tachyon Corporation''  gives you just
 
what you need for a BCI2000 source module, so implementing the ADC
 
what you need for a BCI2000 source module, so implementing the ADC
Line 51: Line 51:
 
   ~TachyonADC();
 
   ~TachyonADC();
  
 +
  void Publish();
 +
  void AutoConfig( const SignalProperties& );
 
   void Preflight( const SignalProperties&, SignalProperties& ) const;
 
   void Preflight( const SignalProperties&, SignalProperties& ) const;
   void Initialize();
+
   void Initialize( const SignalProperties&, const SignalProperties& );
   void Process( const GenericSignal*, GenericSignal* );
+
   void Process( const GenericSignal&, GenericSignal& );
 
   void Halt();
 
   void Halt();
  
 
  private:
 
  private:
   int mSourceCh,
+
  void* mHandle;
 +
   int mSourceCh,
 
         mSampleBlockSize,
 
         mSampleBlockSize,
 
         mSamplingRate;
 
         mSamplingRate;
Line 110: Line 113:
 
<pre>
 
<pre>
 
void TachyonADC::Preflight( const SignalProperties&,
 
void TachyonADC::Preflight( const SignalProperties&,
                              SignalProperties& outputProperties )
+
                            SignalProperties& outputProperties ) const
                              const
 
 
{
 
{
   if( TACHYON_NO_ERROR !=  
+
   if( TACHYON_NO_ERROR != TachyonStart( Parameter( "SamplingRate" ), Parameter( "SourceCh" ) ) )
        TachyonStart( Parameter( "SamplingRate" ), Parameter( "SourceCh" ) ) )
 
 
     bcierr << "SamplingRate and/or SourceCh parameters are not compatible"
 
     bcierr << "SamplingRate and/or SourceCh parameters are not compatible"
 
           << " with the A/D card"
 
           << " with the A/D card"
Line 129: Line 130:
 
but also the format of the <tt>dat</tt> file written by the source
 
but also the format of the <tt>dat</tt> file written by the source
 
module.
 
module.
For the <tt>Initialize</tt> function, it will only be
+
 
called if
+
You might want to write <tt>SignalType::int32</tt> or <tt>SignalType::float32</tt> instead if your data acquisition hardware acquires data in one of those formats.
<tt>Preflight</tt> did not report any errors. If everything works
+
 
fine with
+
The actual <tt>Initialize</tt> function will only be called if <tt>Preflight</tt> did not report any errors.  
the parameters, you may skip any checks, and write  
+
Thus, you may skip any further checks, and write  
 
<pre>
 
<pre>
void TachyonADC::Initialize()
+
void TachyonADC::Initialize( const SignalProperties&, const SignalProperties& )
 
{
 
{
 
   mSourceCh = Parameter( "SourceCh" );
 
   mSourceCh = Parameter( "SourceCh" );
Line 154: Line 155:
  
 
==Data Acquisition==
 
==Data Acquisition==
Note that the function may not return unless the output signal is filled with data, so it is
+
Note that the <tt>Process</tt> function may not return unless the output signal is filled with data, so it is
 
crucial that <tt>TachyonWaitForData</tt> is a blocking function.
 
crucial that <tt>TachyonWaitForData</tt> is a blocking function.
 
(If your card does not provide such a function, and you need to poll
 
(If your card does not provide such a function, and you need to poll
Line 160: Line 161:
 
avoid tying up the CPU.)
 
avoid tying up the CPU.)
 
<pre>
 
<pre>
void TachyonADC::Process( const GenericSignal*, GenericSignal* outputSignal )
+
void TachyonADC::Process( const GenericSignal&, GenericSignal& outputSignal )
 
{
 
{
 
   int valuesToRead = mSampleBlockSize * mSourceCh;
 
   int valuesToRead = mSampleBlockSize * mSourceCh;
Line 169: Line 170:
 
     for( int channel = 0; channel < mSourceCh; ++channel )
 
     for( int channel = 0; channel < mSourceCh; ++channel )
 
       for( int sample = 0; sample < mSampleBlockSize; ++sample )
 
       for( int sample = 0; sample < mSampleBlockSize; ++sample )
         ( *outputSignal )( channel, sample ) = buffer[ i++ ];
+
         outputSignal( channel, sample ) = buffer[ i++ ];
 
   }
 
   }
 
   else
 
   else
Line 175: Line 176:
 
}
 
}
 
</pre>
 
</pre>
 +
 +
==Adding the ''SourceFilter''==
 +
Most measurement equipment comes with hardware filters that allow you to filter out line noise.
 +
For equipment that does not offer such an option, consider adding the [[User Reference:SourceFilter|SourceFilter]] to your data acquisition module as described [[User_Reference:SourceFilter#Remarks|here]].
  
 
==Finished==
 
==Finished==
Line 181: Line 186:
 
descendant in an existing source module, add the
 
descendant in an existing source module, add the
 
<tt>TachyonADC.lib</tt> shipped
 
<tt>TachyonADC.lib</tt> shipped
with your card to the project, compile, link, and find the bugs...
+
with your card to the project, compile, and link.
 +
 
 +
[[Category:Tutorial]][[Category:Development]][[Category:Framework API]][[Category:Data Acquisition]]

Latest revision as of 19:18, 12 June 2013

Data acquisition modules are factored into

  • code required for any hardware, and
  • code required to access a specific hardware.

You provide only specific code. This is in a function that waits for and reads A/D data (line 3 in the pseudo code shown at Technical Reference:Core Modules), together with some helper functions that perform initialization and cleanup tasks. Together these functions form a class derived from GenericADC.

Example Scenario

Your Tachyon Corporation A/D card comes with a C-style software interface declared in a header file "TachyonLib.h" that consists of three functions

#define TACHYON_NO_ERROR 0
int TachyonStart( int inSamplingRate, int inNumberOfChannels );
int TachyonStop( void );
int TachyonWaitForData( short** outBuffer, int inCount );

From the library help file, you learn that TachyonStart configures the card and starts acquisition to some internal buffer; that TachyonStop stops acquisition to the buffer, and that TachyonWaitForData will block execution until the specified amount of data has been acquired, and that it will return a pointer to a buffer containing the data in its first argument. Each of the functions will return zero if everything went well, otherwise some error value will be returned. Luckily, Tachyon Corporation gives you just what you need for a BCI2000 source module, so implementing the ADC class is quite straightforward.

Writing the ADC Header File

In your class' header file, "TachyonADC.h", you write

#ifndef TACHYON_ADC_H
#define TACHYON_ADC_H

#include "GenericADC.h"

class TachyonADC : public GenericADC
{
 public:
   TachyonADC();
   ~TachyonADC();

   void Publish();
   void AutoConfig( const SignalProperties& );
   void Preflight( const SignalProperties&, SignalProperties& ) const;
   void Initialize( const SignalProperties&, const SignalProperties& );
   void Process( const GenericSignal&, GenericSignal& );
   void Halt();

 private:
   void* mHandle;
   int mSourceCh,
        mSampleBlockSize,
        mSamplingRate;
};
#endif // TACHYON_ADC_H

ADC Implementation

In the .cpp file, you will need some #includes, and a filter registration:

#include "TachyonADC.h"
#include "Tachyon/TachyonLib.h"
#include "BCIError.h"

using namespace std;

RegisterFilter( TachyonADC, 1 );

From the constructor, you request parameters and states that your ADC needs; from the destructor, you call Halt to make sure that your board stops acquiring data whenever your class instance gets destructed:

TachyonADC::TachyonADC()
: mSourceCh( 0 ),
  mSampleBlockSize( 0 ),
  mSamplingRate( 0 )
{
  BEGIN_PARAMETER_DEFINITIONS
    "Source int SourceCh=        64 64 1 128 "
        "// this is the number of digitized channels",
    "Source int SampleBlockSize= 16 5 1 128 "
        "// this is the number of samples transmitted at a time",
    "Source int SamplingRate=    128 128 1 4000 "
        "// this is the sample rate",
  END_PARAMETER_DEFINITIONS
}

TachyonADC::~TachyonADC()
{
  Halt();
}

ADC Initialization

Your Preflight function will check whether the board works with the parameters requested, and communicate the dimensions of its output signal:

void TachyonADC::Preflight( const SignalProperties&,
                            SignalProperties& outputProperties ) const
{
  if( TACHYON_NO_ERROR != TachyonStart( Parameter( "SamplingRate" ), Parameter( "SourceCh" ) ) )
    bcierr << "SamplingRate and/or SourceCh parameters are not compatible"
           << " with the A/D card"
           << endl;
  TachyonStop();
  outputProperties = SignalProperties( Parameter( "SourceCh" ),
                          Parameter( "SampleBlockSize" ),
                          SignalType::int16 );
}

Here, the last argument of the SignalProperties constructor determines not only the type of the signal propagated to the BCI2000 filters but also the format of the dat file written by the source module.

You might want to write SignalType::int32 or SignalType::float32 instead if your data acquisition hardware acquires data in one of those formats.

The actual Initialize function will only be called if Preflight did not report any errors. Thus, you may skip any further checks, and write

void TachyonADC::Initialize( const SignalProperties&, const SignalProperties& )
{
  mSourceCh = Parameter( "SourceCh" );
  mSampleBlockSize = Parameter( "SampleBlockSize" );
  mSamplingRate = Parameter( "SamplingRate" );
  TachyonStart( mSamplingRate, mSourceCh );
}

Balancing the TachyonStart call in the Initialize function, your Halt function should stop all asynchronous activity that your ADC code initiates:

void TachyonADC::Halt()
{
  TachyonStop();
}

Data Acquisition

Note that the Process function may not return unless the output signal is filled with data, so it is crucial that TachyonWaitForData is a blocking function. (If your card does not provide such a function, and you need to poll for data, don't forget to call Sleep( 0 ) inside your polling loop to avoid tying up the CPU.)

void TachyonADC::Process( const GenericSignal&, GenericSignal& outputSignal )
{
  int valuesToRead = mSampleBlockSize * mSourceCh;
  short* buffer;
  if( TACHYON_NO_ERROR == TachyonWaitForData( &buffer, valuesToRead ) )
  {
    int i = 0;
    for( int channel = 0; channel < mSourceCh; ++channel )
      for( int sample = 0; sample < mSampleBlockSize; ++sample )
        outputSignal( channel, sample ) = buffer[ i++ ];
  }
  else
    bcierr << "Error reading data" << endl;
}

Adding the SourceFilter

Most measurement equipment comes with hardware filters that allow you to filter out line noise. For equipment that does not offer such an option, consider adding the SourceFilter to your data acquisition module as described here.

Finished

You are done! Use your TachyonADC.cpp to replace the GenericADC descendant in an existing source module, add the TachyonADC.lib shipped with your card to the project, compile, and link.