123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream>
- #include <math.h>
- #include "../inc/compatibility.h"
- #include "../inc/bdaqctrl.h"
- using namespace std;
- using namespace Automation::BDaq;
- #define ONE_WAVE_POINT_COUNT 512
-
- #define deviceDescription L"PCI-1724,BID#0"
- const wchar_t* profilePath = L"../../profile/PCI-1724.xml";
- int32 channelStart = 0;
- int32 channelCount = 1;
-
- enum WaveStyle{ Sine, Sawtooth, Square };
- ErrorCode GenerateWaveform( InstantAoCtrl * instantAoCtrl,int32 channelStart,int32 channelCount, double * waveBuffer, int32 SamplesCount,WaveStyle style);
-
- inline void waitAnyKey()
- {
- do{SLEEP(1);} while(!kbhit());
- }
- int main(int argc, char* argv[])
- {
- ErrorCode ret = Success;
-
- InstantAoCtrl * instantAoCtrl = InstantAoCtrl::Create();
- do
- {
-
-
- DeviceInformation devInfo(deviceDescription);
- ret = instantAoCtrl->setSelectedDevice(devInfo);
- CHK_RESULT(ret);
- ret = instantAoCtrl->LoadProfile(profilePath);
- CHK_RESULT(ret);
-
-
-
- double *waveform = (double*)malloc( channelCount*ONE_WAVE_POINT_COUNT*sizeof(double));
- if( NULL == waveform )
- {
- printf( "Insufficient memory available\n" );
- break;
- }
- ret = GenerateWaveform( instantAoCtrl,channelStart,channelCount, waveform,channelCount*ONE_WAVE_POINT_COUNT,Sine);
- CHK_RESULT(ret);
-
- printf("\n Outputting data... any key to quit!\n");
- bool enforced = false;
- do
- {
- for( int32 i = 0; i < ONE_WAVE_POINT_COUNT; i++ )
- {
- ret = instantAoCtrl->Write(channelStart, channelCount, &waveform[channelCount*i]);
- CHK_RESULT(ret);
- SLEEP(1);
- cout << "当前输出数据点数:" << i << endl;
- if(kbhit())
- {
- printf("\n Static AO is over compulsorily");
- enforced = true;
- break;
- }
- }
- } while (false);
- free(waveform);
- if (!enforced)
- {
- printf("\n Static AO is over, press any key to quit!\n");
- }
- }while(false);
-
-
- instantAoCtrl->Dispose();
-
-
- if(BioFailed(ret))
- {
- wchar_t enumString[256];
- AdxEnumToString(L"ErrorCode", (int32)ret, 256, enumString);
- printf("Some error occurred. And the last error code is 0x%X. [%ls]\n", ret, enumString);
- waitAnyKey();
- }
- return 0;
- }
-
-
- ErrorCode GenerateWaveform( InstantAoCtrl * instantAoCtrl, int32 channelStart,int32 channelCount,double * waveBuffer,int32 SamplesCount,WaveStyle style)
- {
- ErrorCode ret = Success;
- int32 channel = 0;
- int32 channelCountMax = 0;
- int32 oneWaveSamplesCount = SamplesCount/channelCount;
- int32 i = 0;
-
- MathInterval ranges[64] ;
- ValueRange valRange;
- channelCountMax = instantAoCtrl->getFeatures()->getChannelCountMax();
- for(i = 0;i < channelCountMax ;i++ )
- {
- valRange = instantAoCtrl->getChannels()->getItem(i).getValueRange();
- if ( V_ExternalRefBipolar == valRange || valRange == V_ExternalRefUnipolar )
- {
- if (instantAoCtrl->getFeatures()->getExternalRefAntiPolar())
- {
- double referenceValue;
-
- if (valRange == V_ExternalRefBipolar)
- {
- referenceValue = instantAoCtrl->getChannels()->getItem(i).getExtRefBipolar();
- if (referenceValue >= 0) {
- ranges[i].Max = referenceValue;
- ranges[i].Min = 0 - referenceValue;
- } else {
- ranges[i].Max = 0 - referenceValue;
- ranges[i].Min = referenceValue;
- }
- }
- else
- {
- referenceValue = instantAoCtrl->getChannels()->getItem(i).getExtRefUnipolar();
- if (referenceValue >= 0) {
- ranges[i].Max = 0;
- ranges[i].Min = 0 - referenceValue;
- } else {
- ranges[i].Max = 0 - referenceValue;
- ranges[i].Min = 0;
- }
- }
- }
- else
- {
- double referenceValue;
-
- if (valRange == V_ExternalRefBipolar)
- {
- referenceValue = instantAoCtrl->getChannels()->getItem(i).getExtRefBipolar();
- if (referenceValue >= 0) {
- ranges[i].Max = referenceValue;
- ranges[i].Min = 0 - referenceValue;
- } else {
- ranges[i].Max = 0 - referenceValue;
- ranges[i].Min = referenceValue;
- }
- }
- else
- {
- referenceValue = instantAoCtrl->getChannels()->getItem(i).getExtRefUnipolar();
- if (referenceValue >= 0) {
- ranges[i].Max = referenceValue;
- ranges[i].Min = 0;
- } else {
- ranges[i].Max = 0;
- ranges[i].Min = referenceValue;
- }
- }
- }
- }
- else {
- ret = AdxGetValueRangeInformation( valRange,0,NULL,&ranges[i],NULL);
- if(BioFailed(ret))
- {
- return ret;
- }
- }
- }
-
-
- for(i = 0; i < oneWaveSamplesCount; i++ )
- {
- for( int32 j = channelStart; j < channelStart+channelCount; j++ )
- {
-
- channel = j%channelCountMax;
-
- double amplitude = (ranges[channel].Max - ranges[channel].Min) / 2;
- double offset = (ranges[channel].Max + ranges[channel].Min) / 2;
-
- switch ( style)
- {
- case Sine:
- *waveBuffer++ = amplitude*(sin((double)i*2.0*( 3.14159 )/oneWaveSamplesCount )) + offset;
- break;
- case Sawtooth:
- if ((i >= 0) && (i < (oneWaveSamplesCount / 4.0)))
- {
- *waveBuffer++ = amplitude*( i/(oneWaveSamplesCount/4.0)) + offset;
- }
- else
- {
- if ((i >= (oneWaveSamplesCount / 4.0)) && (i < 3 * (oneWaveSamplesCount/4.0)))
- {
- *waveBuffer++ = amplitude* ((2.0*(oneWaveSamplesCount/4.0)-i)/(oneWaveSamplesCount/4.0)) + offset;
- }
- else
- {
- *waveBuffer++ = amplitude* ((i-oneWaveSamplesCount)/(oneWaveSamplesCount/4.0)) + offset;
- }
- }
- break;
- case Square:
- if ((i >= 0) && (i < (oneWaveSamplesCount / 2)))
- {
- *waveBuffer++ = amplitude * 1 + offset;
- }
- else
- {
- *waveBuffer++ = amplitude * (-1) + offset;
- }
- break;
- default:
- printf("invalid wave style,generate waveform error !");
- ret = ErrorUndefined;
- }
- }
- }
- return ret;
- };
|