Scope
OutputsDAQmxResonance.cpp
1 #include "stdafx.h"
2 #include "OutputsDAQmxResonance.h"
3 #include "parameters/IO.h"
4 #include "parameters/Scope.h"
5 #include "ScopeDatatypes.h"
6 #include "helpers/ScopeException.h"
7 
8 namespace scope {
9 
10 OutputsDAQmxResonance::OutputsDAQmxResonance(const uint32_t& _area, const parameters::OutputsDAQmxResonance& _outputparams, const parameters::Scope& _params)
11  : Outputs(_area) {
12 
13  int32_t samplingtype = (_params.requested_mode()==DaqModeHelper::continuous)?DAQmx_Val_ContSamps:DAQmx_Val_FiniteSamps;
14 
15  std::wstring commontrig = _params.commontrigger();
16 
17  task.CreateTask();
19 
20  // Create channel for analog outputs
22  , L"XYZPOut"
23  , -_outputparams.range()
24  , _outputparams.range());
25 
26  // Create channel for digital output (resonance scanner zoom factor)
28 
29  // Calculate pixelrate and number of pixels to generate
30  double pixelrate = 1/(_params.areas[area]->daq.pixeltime()*1E-6);
31  int32_t pixelsperchan = _params.areas[area]->Currentframe().TotalPixels();
32  if ( _params.requested_mode() == DaqModeHelper::nframes )
33  pixelsperchan = _params.areas[area]->Currentframe().TotalPixels() * _params.areas[area]->daq.requested_frames() * _params.areas[ThisAreaOrMasterArea(area)]->daq.averages();
34 
35  // Configure timing (if using ReferenceClock timing ClockString gives "")
37  , _params.areas[area]->daq.resonance_frequency()
38  , _params.areas[area]->Currentframe().YTotalLines()
39  , samplingtype
40  , DAQmx_Val_Falling);
41 
42  if ( DaqTimingHelper::Mode::ReferenceClock ==_outputparams.daq_timing() )
43  task.ConfigureReferenceClock(_outputparams.referenceclocksource(), _outputparams.referenceclockrate());
44 
45  task.ConfigureBuffer( _params.areas[area]->Currentframe().YTotalLines() / 2);
46 
47  // Regenerate frame samples if we are in nframes mode
48  if ( _params.requested_mode() == DaqModeHelper::nframes )
49  task.SetRegeneration(true);
50 
51  // Export the pixel/sample clock
52  task.ExportSignal(DAQmx_Val_SampleClock, _outputparams.exportpixelclockterminal());
53 
54  // All (exept the first aouttask) wait for the first tasks start trigger
55  if ( area != 0 ) {
56  task.ConfigureDigStartTrigger(commontrig, DAQmx_Val_Rising);
57  }
58  // If T-Series triggered first output task waits for external trigger (and all other tasks waiting for first output task)
59  else if ( _params.timeseries.triggered() )
60  task.ConfigureDigStartTrigger(_params.timeseries.triggerchannel(), DAQmx_Val_Rising);
61 }
62 
64  Stop();
65  task.Clear();
67 }
68 
70  task.Start();
72 }
73 
75  writeabort = true;
76  task.Stop();
78 }
79 
80 int32_t OutputsDAQmxResonance::Write(const std::vector<int16_t>& _xyzp, const uint32_t& _blocks) {
81  int32_t written = 0;
82  assert(_blocks!=0);
83  int32_t sizeperchannel = static_cast<int32_t>(_xyzp.size() / 4); // number of samples in each output channel
84  int32_t blocksize = sizeperchannel/_blocks;
85  int32_t lastblocksize = blocksize + (sizeperchannel - blocksize*_blocks); // adjust size of last block if sizeperchannel is not a multiple of blocksize
86  DBOUT(L"OutputsDAQmxResonance::Write blocksize " << blocksize << L" last block " << lastblocksize);
87  DBOUT(L"OutputsDAQmxResonance::Write blocks " << _blocks << L" sizeperchannel " << sizeperchannel << L" totblocksize " << (_blocks-1)*blocksize+lastblocksize);
88  try {
89  // resonance scanner zoom factor
90  const uint8_t digitalValue[2] = {(_reszoomfactor-1)/2, (_reszoomfactor-1)%2};
91  taskResonanceZoom.WriteDigitalLines(digitalValue, 1, true);
92 
93  // xyzp vector
94  for ( uint32_t b = 0 ; b < _blocks ; b++ ) {
95  if ( writeabort == true ) {
96  DBOUT(L"OutputsDAQmxResonance::Write writeabort");
97  break;
98  }
99  DBOUT(L"OutputsDAQmxResonance::Write block " << b);
100  task.SetWriteOffset(b*blocksize); // set write offset in the device buffer
101  written += task.WriteAnalogI16((_xyzp.data()+4*b*blocksize) // set offset in the data vector
102  , ((b==_blocks-1)?lastblocksize:blocksize), false, 2, DAQmx_Val_GroupByScanNumber); // data layout is by sample
103  }
104  } catch (...) { ScopeExceptionHandler(__FUNCTION__); }
105  writeabort = false;
106  return written;
107 }
108 
110  // resonance scanner zoom factor
111  DAQmx::CDAQmxDigitalOutTask taskResonanceZoom;
112  try {
113  taskResonanceZoom.CreateTask();
114  taskResonanceZoom.CreateDOChannel(_params.zoomchannelstring());
115  taskResonanceZoom.WaitUntilDone(500);
116  taskResonanceZoom.Clear();
117  } catch (...) { ScopeExceptionHandler(__FUNCTION__); }
118 
119  // xyzp vector
121  try {
122  task.CreateTask();
124  , L"XYZPOut"
125  , -_params.range()
126  , _params.range());
127  task.WriteAnalogI16(std::make_shared<std::vector<int16_t>>(4,0)->data(), 1, true);
128  task.WaitUntilDone(500);
129  task.Clear();
130  } catch (...) { ScopeExceptionHandler(__FUNCTION__); }
131 }
132 
133 }
ScopeNumber< double > referenceclockrate
rate in Hertz of the reference clock.
Definition: IO.h:727
int32_t WriteDigitalLines(const uInt8 *_data, int32 _sampsperchan, bool _autostart=false, float64 _timeout=2, bool32 _layout=DAQmx_Val_GroupByChannel)
Writes to up to 8 digital lines (in one port, I suppose), depending on which lines are configured int...
Definition: DAQmxTask.cpp:296
ScopeString commontrigger
The trigger channel which is the internal common master trigger for all devices.
Definition: Scope.h:262
ScopeString zoomchannelstring
the digital channel to use for the resonance scanner zoom factor
Definition: IO.h:714
void SetRegeneration(const bool &_regenerate)
Sets the mode of sample regeneration.
Definition: DAQmxTask.cpp:177
The master parameters class.
Definition: Scope.h:204
void ConfigureReferenceClock(const std::wstring &_src, const float64 &_rate)
Configures the reference clock for the task.
Definition: DAQmxTask.cpp:128
int32_t WaitUntilDone(const float64 &_waittime)
Waits until the task is done.
Definition: DAQmxTask.cpp:163
std::array< std::unique_ptr< Area >, SCOPE_NAREAS > areas
holds AreaParameters for all areas.
Definition: Scope.h:231
std::wstring ClockString(const scope::DaqTimingHelper::Mode &_timing, const std::wstring &_externalclocksource)
Generates a string for the sample clock from daq_timing.
Definition: DAQmxTask.cpp:46
std::atomic< bool > writeabort
Signal for aborting a write operation (since write is performed in several blocks, abort can happen in between)
Definition: Outputs.h:13
ScopeString triggerchannel
channel name for trigger input
Definition: Runstates.h:78
uInt32 ConfigureBuffer(const uInt32 &_sampsperchannel)
Configure the daqmx output buffer on the PC.
Definition: DAQmxTask.cpp:201
void CreateDOChannel(const std::wstring &_devicelines, const std::wstring &_channelname=L"")
Creates a digital output channel.
Definition: DAQmxTask.cpp:270
~OutputsDAQmxResonance()
Stop and clear output task.
void ExportSignal(int32 _signal, const std::wstring &_terminal)
Exports a signal to an output terminal.
Definition: DAQmxTask.cpp:167
Timeseries timeseries
the TimeseriesParameters
Definition: Scope.h:240
int32 WriteAnalogI16(const int16 *_data, int32 _sampsperchan, bool _autostart=false, int32 _timeout=2, bool32 _layout=DAQmx_Val_GroupByChannel)
Writes several samples to the buffer of an analog output task.
Definition: DAQmxTask.cpp:258
ScopeString exportpixelclockterminal
terminal to which the pixel/sampleclock is exported
Definition: IO.h:733
DAQmx::CDAQmxAnalogOutTask task
The DAQmx task for x/y-scanners/fast z/Pockels clocked by a pixel clock.
void SetWriteOffset(int32 posreltofirst)
Sets the write offset relative to the first buffer position.
Definition: DAQmxTask.cpp:172
ScopeString externalclocksource
source terminal of an external clock
Definition: IO.h:730
This is the include file for standard system include files, or project specific include files that ar...
ScopeString referenceclocksource
source terminal of the reference clock
Definition: IO.h:723
ScopeValue< DaqMode > requested_mode
requested acquisition mode (see DaqModeHelper)
Definition: Scope.h:279
void Start(void)
Starts the task.
Definition: DAQmxTask.cpp:145
ScopeNumber< DaqTiming > daq_timing
where to get the sampling clock from? Onboard, from external reference clock, or from external source...
Definition: IO.h:717
OutputsDAQmxResonance(const uint32_t &_area, const parameters::OutputsDAQmxResonance &_outputparams, const parameters::Scope &_params)
Creates the task for scanner, pockels, and fast-z output with NI DAQmx.
ScopeNumber< bool > triggered
the current/first repeat is triggered on triggerchannel
Definition: Runstates.h:81
In here all declarations for all kinds of datatypes Scope needs.
ScopeString channelsstring
the four analog output channels to use for x, y, z, Pockels
Definition: IO.h:708
Wraps a DAQmx Digital Output task.
Definition: DAQmxTask.h:224
#define DBOUT(s)
A debug output to the debug console.
Definition: helpers.h:153
void ScopeExceptionHandler(const std::string &_origin, const bool &_log, const bool &_showmessagebox, const bool &_trace, const bool &_rethrow)
Handles all exceptions and does nice logging.
void CreateAOVoltageChannel(const std::wstring &_devices, const std::wstring &_channelname=L"", float64 _minval=-10.0, float64 _maxval=10.0, int32 _units=DAQmx_Val_Volts, const std::wstring &_customscalename=L"")
Creates an analog output channel Allowed values for terminal configuration (from NiDAQmx.h): Values for DAQmx_PhysicalChan_AI_TermCfgs and DAQmx_PhysicalChan_AO_TermCfgs Value set TerminalConfigurationBits .
Definition: DAQmxTask.cpp:182
Wraps a DAQmx Analog Output task.
Definition: DAQmxTask.h:137
void Clear(void)
Clear the task.
Definition: DAQmxTask.cpp:153
void CreateTask(const std::wstring &_name=L"")
Definition: DAQmxTask.cpp:91
ZeroOutputsDAQmxResonance(const parameters::OutputsDAQmxResonance &_params)
Create a simple tasks for writing a scalar zero to all channels.
void ConfigureSampleTiming(const std::wstring &_src, const float64 &_rate, const int32 &_samplesperchan, const int32 &_samplingtype=DAQmx_Val_ContSamps, const int32 &_actedge=DAQmx_Val_Rising)
Configures a sample timing for the task.
Definition: DAQmxTask.cpp:109
const uint32_t area
which area this one is for
Definition: Outputs.h:10
ScopeNumber< double > range
Output range of the DAC to use for all outputs.
Definition: IO.h:526
int32_t Write(const std::vector< int16_t > &_xyzp, const uint32_t &_blocks=1) override
Writes values for one frame of X-Y-fastZ and pockels data to the device buffer.
Parameters for signal generation for y scanner and digital zoom with NI-DAQmx when the x scanner is a...
Definition: IO.h:696
void Stop() override
Stop task.
DAQmx::CDAQmxDigitalOutTask taskResonanceZoom
The DAQmx task for the resenance scanner zoom factor.
void ConfigureDigStartTrigger(const std::wstring &_src, const int32 &_trigedge=DAQmx_Val_Rising)
Configures the start trigger.
Definition: DAQmxTask.cpp:134
void Stop(void)
Stops the task.
Definition: DAQmxTask.cpp:149
void Start() override
Start task, last the first output task (all other are waiting for it as their start trigger)...
Wraps hardware connection for signal output to scanners, fast z control, and pockels cell...
Definition: Outputs.h:6