11 PhasorEstimator(
double sampleRate = 1e6,
double notchRadius = 0.5) : Fs(sampleRate), r(notchRadius)
14 UpdateFrequency(60.0);
31 void UpdateFrequency(
double freq)
33 if(std::abs(freq-lastFreq) < 1e-9)
38 double w0 = 2.0*M_PI*(2.0*freq)/Fs;
58 void Update(
double current,
62 UpdateFrequency(freq);
64 double s = sin(phase);
65 double c = cos(phase);
67 double IdRaw = current*s;
68 double IqRaw = current*c;
74 Id = b0*IdRaw + b1*x1Id + b2*x2Id - a1*y1Id - a2*y2Id;
86 Iq = b0*IqRaw + b1*x1Iq + b2*x2Iq - a1*y1Iq - a2*y2Iq;
105 double GetMagnitude()
const
110 return sqrt(ir*ir+ii*ii);
113 double GetAngle()
const
115 return atan2(GetIq(),GetId());
193HANDLE hPSPReady = NULL;
194HANDLE hATPReady = NULL;
195HANDLE hMapFile = NULL;
198const double PHASE_A = 0.0;
199const double PHASE_B = -2.0 * M_PI / 3.0;
200const double PHASE_C = 2.0 * M_PI / 3.0;
204const double phaseShift[3] =
211inline void SaveCurrent(
const double current[])
213 if(data->stepCount >= data->syncSteps)
215 printf(
"Current buffer full!\n");
219 for(
int i = 0; i < data->nPh; ++i)
220 data->samples[data->stepCount].current[i] = current[i];
223inline void UpdatePhasor(
const double current[])
225 for(
int i = 0; i < data->nPh; ++i)
232 data->phasor.Id[i] = estimator[i].GetId();
233 data->phasor.Iq[i] = estimator[i].GetIq();
237inline bool ProcessSharedData(
const double current[])
242 SaveCurrent(current);
246 UpdatePhasor(current);
250 SaveCurrent(current);
251 UpdatePhasor(current);
255 printf(
"Invalid acquisition mode.\n");
261 if(data->stepCount < data->syncSteps)
268void psp_model_m__(
double xdata_ar[],
double xin_ar[],
double xout_ar[],
double xvar_ar[])
272 printf(
"Shared memory not initialized.\n");
278 data->nPh = xdata_ar[0];
279 data->stoptime = xdata_ar[1];
280 data->atpStepsize = xdata_ar[2];
284 for(
int i = 0; i < data->nPh; ++i) {
286 rth[i] = xin_ar[i + data->nPh];
288 data->t = xin_ar[2* data->nPh];
290 double phase = xvar_ar[0];
291 double lastTime = xvar_ar[1];
293 double dt = data->t - lastTime;
297 phase += 2*M_PI*data->freq*dt;
298 phase = fmod(phase, 2.0*M_PI);
303 xvar_ar[1] = lastTime;
307 if (data->t >= data->stoptime - 1e-12) data->terminate = 1;
308 else data->terminate = -1;
311 for (
int i = 0; i < data->nPh; ++i )
312 v[i] = data->vrms * sqrt(2.0) * sin(data->phase + data->theta + phaseShift[i]);
314 double current[3] = {0.0, 0.0, 0.0};
315 for(
int i = 0; i < data->nPh; ++i) {
316 if(fabs(rth[i]) > 1e-12)
317 current[i] = (vth[i]-v[i])/rth[i];
318 xout_ar[i] = current[i];
321 if(ProcessSharedData(current))
323 if(!SetEvent(hATPReady))
325 printf(
"Error in SetEvent ATP: %lu\n", GetLastError());
328 DWORD ret = WaitForSingleObject(hPSPReady, INFINITE);
330 if(ret == WAIT_OBJECT_0)
332 printf(
"Event received!\n");
336 printf(
"Error in WaitForSingleObject: %lu\n", GetLastError());
345void psp_model_i__(
double xdata_ar[],
double xin_ar[],
double xout_ar[],
double xvar_ar[])
347 printf(
"Initializing model 'PSP MODEL'.\n");
349 hPSPReady = CreateEventW(
353 L
"PSPBridge_PSPReady");
355 if(hPSPReady == NULL)
357 printf(
"Error in CreateEvent: %lu\n", GetLastError());
360 hATPReady = CreateEventW(
364 L
"PSPBridge_ATPReady");
366 if(hATPReady == NULL)
368 printf(
"Error in CreateEvent: %lu\n", GetLastError());
371 if(!SetEvent(hATPReady))
373 printf(
"Error in SetEvent ATP: %lu\n", GetLastError());
376 double pspStepsize = xdata_ar[3];
377 double atpStepsize = xdata_ar[2];
379 const uint32_t syncSteps =
static_cast<uint32_t
>(std::ceil(pspStepsize / atpStepsize - 1e-12));
383 + syncSteps *
sizeof(
Sample);
385 hMapFile = CreateFileMappingW(
386 INVALID_HANDLE_VALUE,
391 L
"PSPBridgeSharedMemory");
395 printf(
"Error in CreateFileMapping: %lu\n", GetLastError());
408 printf(
"Error in MapViewOfFile: %lu\n", GetLastError());
409 CloseHandle(hMapFile);
413 for(
int i = 0; i < 3; ++i)
414 estimator[i].Reset();
417 memset(data, 0 , bytes);
418 data->nPh = xdata_ar[0];
419 data->stoptime = xdata_ar[1];
423 data->terminate = -1;
424 data->syncSteps = syncSteps;
430 printf(
"Waiting PSP to initialize...\n");
432 DWORD ret = WaitForSingleObject(hPSPReady, INFINITE);
434 if(ret != WAIT_OBJECT_0)
436 printf(
"Error waiting PSP.\n");