OpenWAM
TSensor.cpp
1 /* --------------------------------------------------------------------------------*\
2 |==========================|
3  |\\ /\ /\ // O pen | OpenWAM: The Open Source 1D Gas-Dynamic Code
4  | \\ | X | // W ave |
5  | \\ \/_\/ // A ction | CMT-Motores Termicos / Universidad Politecnica Valencia
6  | \\/ \// M odel |
7  ----------------------------------------------------------------------------------
8  License
9 
10  This file is part of OpenWAM.
11 
12  OpenWAM is free software: you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation, either version 3 of the License, or
15  (at your option) any later version.
16 
17  OpenWAM is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with OpenWAM. If not, see <http://www.gnu.org/licenses/>.
24 
25 
26  \*-------------------------------------------------------------------------------- */
27 
28 // ---------------------------------------------------------------------------
29 #pragma hdrstop
30 
31 #include "TSensor.h"
32 #include "TTubo.h"
33 #include "TDeposito.h"
34 #include "TBloqueMotor.h"
35 
36 // ---------------------------------------------------------------------------
37 
39  FNumeroSensor = i;
40  FInicia = false;
41  FPeriod = 0.;
42  FTime0 = 0.;
43 }
44 
46 
47 }
48 
49 void TSensor::ReadSensor(const char *FileWAM, fpos_t &filepos) {
50  int obj = 0, prm = 0;
51 
52  FILE *fich = fopen(FileWAM, "r");
53  fsetpos(fich, &filepos);
54 
55  fscanf(fich, "%d %d", &obj, &prm);
56  if(obj != 0)
57  fscanf(fich, "%d ", &FObjectID);
58  switch(obj) {
59  case 0:
60  FObjectSensed = nmSensEjec;
61  switch(prm) {
62  case 0:
63  FParameterSensed = nmTime;
64  break;
65  default:
66  std::cout << "ERROR: Parametro " << prm << "en el objeto " << obj << "no valido, sensor: " << FNumeroSensor <<
67  std::endl;
68  }
69  break;
70  case 1:
71  FObjectSensed = nmSensTubo;
72  fscanf(fich, "%lf ", &FDistancia);
73  switch(prm) {
74  case 1:
75  FParameterSensed = nmPressure;
76  break;
77  case 2:
78  FParameterSensed = nmTemperature;
79  break;
80  case 3:
81  FParameterSensed = nmMassFlow;
82  break;
83  default:
84  std::cout << "ERROR: Parametro " << prm << "en el objeto " << obj << "no valido, sensor: " << FNumeroSensor <<
85  std::endl;
86  }
87  break;
88  case 2:
89  FObjectSensed = nmSensDeposito;
90  switch(prm) {
91  case 1:
92  FParameterSensed = nmPressure;
93  break;
94  case 2:
95  FParameterSensed = nmTemperature;
96  break;
97  default:
98  std::cout << "ERROR: Parametro " << prm << "en el objeto " << obj << "no valido, sensor: " << FNumeroSensor <<
99  std::endl;
100  }
101  break;
102  case 3:
103  FObjectSensed = nmSensMotor;
104  switch(prm) {
105  case 4:
106  FParameterSensed = nmFuel;
107  break;
108  case 5:
109  FParameterSensed = nmEngSpeed;
110  break;
111  default:
112  std::cout << "ERROR: Parametro " << prm << "en el objeto " << obj << "no valido, sensor: " << FNumeroSensor <<
113  std::endl;
114  }
115  break;
116  default:
117  std::cout << "ERROR: Objeto " << obj << "no valido, sensor: " << FNumeroSensor << std::endl;
118  }
119  fscanf(fich, "%lf %lf ", &FDelay, &FGain);
120 
121  fgetpos(fich, &filepos);
122  fclose(fich);
123 }
124 
126  int nin = 0;
127  double tmp = 0.;
128 
129  FObjectPointer = Object;
130  if(FObjectSensed == nmSensTubo) {
131  tmp = (double)(((TTubo*) FObjectPointer)->getNin() - 1) * FDistancia / ((TTubo*) FObjectPointer)->getLongitudTotal();
132  FNode0 = floor(tmp);
133  FNode1 = ceil(tmp);
134  FDelta = (FDistancia - (double) FNode0 * ((TTubo*) FObjectPointer)->getXRef()) / ((TTubo*) FObjectPointer)->getXRef();
135  }
136 }
137 
138 double TSensor::Output() {
139  return FCurrentValue;
140 }
141 
142 void TSensor::ActualizaMedida(double Time) {
143  double tmp0 = 0., tmp1 = 0.;
144  bool Update = false;
145  double deltaT = 0.;
146 
147  double TimeStep = Time - FTime0;
148 
149  FPeriod = FPeriod + TimeStep;
150 
151  if(FPeriod >= 0.001) {
152  Update = true;
153  deltaT = FPeriod;
154  FPeriod = 0;
155  }
156 
157  if(Update || !FInicia) {
158  switch(FObjectSensed) {
159  case nmSensEjec:
160  switch(FParameterSensed) {
161  case nmTime:
162  FRealValue = Time;
163  break;
164  }
165  FCurrentValue = FRealValue;
166  break;
167  case nmSensTubo:
168  switch(FParameterSensed) {
169  case nmPressure:
170  FRealValue = ((TTubo*) FObjectPointer)->GetPresion(FNode0) * (1 - FDelta) + ((TTubo*) FObjectPointer)->GetPresion(
171  FNode1) * FDelta;
172  break;
173  case nmTemperature:
174  tmp0 = pow2(((TTubo*) FObjectPointer)->GetAsonido(FNode0) * __cons::ARef) / ((TTubo*) FObjectPointer)->GetGamma(
175  FNode0) / ((TTubo*) FObjectPointer)->GetRMezcla(FNode0);
176  tmp1 = pow2(((TTubo*) FObjectPointer)->GetAsonido(FNode1) * __cons::ARef) / ((TTubo*) FObjectPointer)->GetGamma(
177  FNode1) / ((TTubo*) FObjectPointer)->GetRMezcla(FNode1);
178  FRealValue = tmp0 * (1 - FDelta) + tmp1 * FDelta;
179  break;
180  case nmMassFlow:
181  tmp0 = ((TTubo*) FObjectPointer)->GetDensidad(FNode0) * ((TTubo*) FObjectPointer)->GetVelocidad(
182  FNode0) * __cons::ARef * ((TTubo*) FObjectPointer)->GetArea(FNode0);
183  tmp1 = ((TTubo*) FObjectPointer)->GetDensidad(FNode1) * ((TTubo*) FObjectPointer)->GetVelocidad(
184  FNode1) * __cons::ARef * ((TTubo*) FObjectPointer)->GetArea(FNode1);
185  FRealValue = tmp0 * (1 - FDelta) + tmp1 * FDelta;
186  break;
187  }
188  break;
189  case nmSensDeposito:
190  switch(FParameterSensed) {
191  case nmPressure:
192  FRealValue = ((TDeposito*) FObjectPointer)->getPressure();
193  break;
194  case nmTemperature:
195  FRealValue = ((TDeposito*) FObjectPointer)->getTemperature();
196  break;
197  }
198  break;
199  case nmSensMotor:
200  switch(FParameterSensed) {
201  case nmFuel:
202  FRealValue = ((TBloqueMotor*) FObjectPointer)->getMasaFuel();
203  break;
204  case nmEngSpeed:
205  FRealValue = ((TBloqueMotor*) FObjectPointer)->getRegimen();
206  break;
207  }
208  }
209  }
210 
211  if(!FInicia) {
212  FPrevValue = FRealValue;
213  FInicia = true;
214  }
215  if(!Update) {
216  FCurrentValue = FPrevValue;
217  } else {
218  FCurrentValue = ((2 * FDelay - deltaT) * FPrevValue + deltaT * FGain * (FRealValue + FPrevRealValue)) /
219  (2 * FDelay + deltaT);
220  }
221  FPrevValue = FCurrentValue;
222  FPrevRealValue = FRealValue;
223  FTime0 = Time;
224 
226 
227  // out=FCurrentValue;
228  // return out;
229 }
230 
231 void TSensor::LeeResultadosMedSensor(const char *FileWAM, fpos_t &filepos) {
232  try {
233  int nvars = 0, var = 0;
234 
235  FILE *fich = fopen(FileWAM, "r");
236  fsetpos(fich, &filepos);
237 
238  fscanf(fich, "%d ", &nvars);
239  for(int i = 0; i < nvars; i++) {
240  fscanf(fich, "%d ", &var);
241  switch(var) {
242  case 0:
243  FResMediosSensor.Output = true;
244  break;
245  case 1:
246  FResMediosSensor.Input = true;
247  break;
248  default:
249  std::cout << "Resultados medios en Controlador " << FNumeroSensor << " no implementados " << std::endl;
250  }
251  }
252 
253  fgetpos(fich, &filepos);
254  fclose(fich);
255  } catch(exception & N) {
256  std::cout << "ERROR: TPIDController::LeeResultadosMedSensor en el controlador " << FNumeroSensor << std::endl;
257  std::cout << "Tipo de error: " << N.what() << std::endl;
258  throw Exception(N.what());
259  }
260 }
261 
262 void TSensor::LeeResultadosInsSensor(const char *FileWAM, fpos_t &filepos) {
263  try {
264  int nvars = 0, var = 0;
265 
266  FILE *fich = fopen(FileWAM, "r");
267  fsetpos(fich, &filepos);
268 
269  fscanf(fich, "%d ", &nvars);
270  for(int i = 0; i < nvars; i++) {
271  fscanf(fich, "%d ", &var);
272  switch(var) {
273  case 0:
274  FResInstantSensor.Output = true;
275  break;
276  case 1:
277  FResInstantSensor.Input = true;
278  break;
279  default:
280  std::cout << "Resultados instantaneos en Sensor " << FNumeroSensor << " no implementados " << std::endl;
281  }
282  }
283 
284  fgetpos(fich, &filepos);
285  fclose(fich);
286  } catch(exception & N) {
287  std::cout << "ERROR: TPIDController::LeeResultadosInsSensor en el Sensor " << FNumeroSensor << std::endl;
288  std::cout << "Tipo de error: " << N.what() << std::endl;
289  throw Exception(N.what());
290  }
291 }
292 
293 void TSensor::CabeceraResultadosMedSensor(stringstream& medoutput) {
294  try {
295  std::string Label;
296 
297  if(FResMediosSensor.Output) {
298  Label = "\t" + PutLabel(707) + std::to_string(FNumeroSensor) + PutLabel(901);
299  medoutput << Label.c_str();
300  }
301  if(FResMediosSensor.Input) {
302  Label = "\t" + PutLabel(708) + std::to_string(FNumeroSensor) + PutLabel(901);
303  medoutput << Label.c_str();
304  }
305 
306  } catch(exception & N) {
307  std::cout << "ERROR: TPIDController::CabeceraResultadosMedSensor en el Sensor " << FNumeroSensor << std::endl;
308  std::cout << "Tipo de error: " << N.what() << std::endl;
309  throw Exception(N.what());
310  }
311 }
312 
313 void TSensor::CabeceraResultadosInsSensor(stringstream& insoutput) {
314  try {
315  std::string Label;
316 
317  if(FResInstantSensor.Output) {
318  Label = "\t" + PutLabel(707) + std::to_string(FNumeroSensor) + PutLabel(901);
319  insoutput << Label.c_str();
320  }
321  if(FResInstantSensor.Input) {
322  Label = "\t" + PutLabel(708) + std::to_string(FNumeroSensor) + PutLabel(901);
323  insoutput << Label.c_str();
324  }
325  } catch(exception & N) {
326  std::cout << "ERROR: TPIDController::CabeceraResultadosInsSensor en el Sensor " << FNumeroSensor << std::endl;
327  std::cout << "Tipo de error: " << N.what() << std::endl;
328  throw Exception(N.what());
329  }
330 }
331 
332 void TSensor::ImprimeResultadosMedSensor(stringstream& medoutput) {
333  try {
334  std::string Label;
335 
336  if(FResMediosSensor.Output) {
337  medoutput << "\t" << FResMediosSensor.OutputMED;
338  }
339  if(FResMediosSensor.Input) {
340  medoutput << "\t" << FResMediosSensor.InputMED;
341  }
342 
343  } catch(exception & N) {
344  std::cout << "ERROR: TPIDController::ImprimeResultadosMedSensor en el Sensor " << FNumeroSensor << std::endl;
345  std::cout << "Tipo de error: " << N.what() << std::endl;
346  throw Exception(N.what());
347  }
348 }
349 
350 void TSensor::ImprimeResultadosInsSensor(stringstream& insoutput) {
351  try {
352  std::string Label;
353 
354  if(FResInstantSensor.Output) {
355  insoutput << "\t" << FResInstantSensor.OutputINS;
356  }
357  if(FResInstantSensor.Input) {
358  insoutput << "\t" << FResInstantSensor.InputINS;
359  }
360  } catch(exception & N) {
361  std::cout << "ERROR: TPIDController::CabeceraResultadosInsSensor en el Sensor " << FNumeroSensor << std::endl;
362  std::cout << "Tipo de error: " << N.what() << std::endl;
363  throw Exception(N.what());
364  }
365 }
366 
368  try {
369 
370  FResMediosSensor.OutputSUM = 0.;
371  FResMediosSensor.InputSUM = 0.;
372  FResMediosSensor.TiempoSUM = 0.;
373  FResMediosSensor.Tiempo0 = 0.;
374 
375  } catch(exception & N) {
376  std::cout << "ERROR: TPIDController::IniciaMedias en el Sensor: " << FNumeroSensor << std::endl;
377  // std::cout << "Tipo de error: " << N.what() << std::endl;
378  throw Exception(N.what());
379  }
380 }
381 
383  try {
384 
385  if(FResMediosSensor.Output) {
386  FResMediosSensor.OutputMED = FResMediosSensor.OutputSUM / FResMediosSensor.TiempoSUM;
387  FResMediosSensor.OutputSUM = 0.;
388  }
389  if(FResMediosSensor.Input) {
390  FResMediosSensor.InputMED = FResMediosSensor.InputSUM / FResMediosSensor.TiempoSUM;
391  FResMediosSensor.InputSUM = 0.;
392  }
393  FResMediosSensor.TiempoSUM = 0;
394 
395  } catch(exception & N) {
396  std::cout << "ERROR: TPIDController::ResultadosMediosSensor en el eje: " << FNumeroSensor << std::endl;
397  // std::cout << "Tipo de error: " << N.what() << std::endl;
398  throw Exception(N.what());
399  }
400 }
401 
403  try {
404  /* Lo que se hace en esta funcion se realiza dentro del calculo del eje, para asi poder
405  llevar a cabo la salida de resultados medios por pantalla. */
406  double Delta = Actual - FResMediosSensor.Tiempo0;
407 
408  if(FResMediosSensor.Output) {
409  FResMediosSensor.OutputSUM += FCurrentValue * Delta;
410  }
411  if(FResMediosSensor.Input) {
412  FResMediosSensor.InputSUM += FRealValue * Delta;
413  }
414  FResMediosSensor.TiempoSUM += Delta;
415  FResMediosSensor.Tiempo0 = Actual;
416 
417  } catch(exception & N) {
418  std::cout << "ERROR: TSensor::AcumulaResultadosMediosSensor en el eje: " << FNumeroSensor << std::endl;
419  // std::cout << "Tipo de error: " << N.what() << std::endl;
420  throw Exception(N.what());
421  }
422 }
423 
425  try {
426  if(FResInstantSensor.Output)
427  FResInstantSensor.OutputINS = FCurrentValue;
428  if(FResInstantSensor.Input)
429  FResInstantSensor.InputINS = FRealValue;
430 
431  } catch(exception & N) {
432  std::cout << "ERROR: TPIDController::ResultadosInstantSensor en el eje " << FNumeroSensor << std::endl;
433  std::cout << "Tipo de error: " << N.what() << std::endl;
434  throw Exception(N.what());
435  }
436 }
437 
439  return FObjectID;
440 }
441 ;
442 
443 #pragma package(smart_init)
TSensor::ResultadosInstantSensor
void ResultadosInstantSensor()
Definition: TSensor.cpp:424
TTubo
a Finite differences pipe.
Definition: TTubo.h:116
TSensor::LeeResultadosMedSensor
void LeeResultadosMedSensor(const char *FileWAM, fpos_t &filepos)
Definition: TSensor.cpp:231
TSensor::CabeceraResultadosInsSensor
void CabeceraResultadosInsSensor(std::stringstream &insoutput)
Definition: TSensor.cpp:313
TSensor::ObjectID
int ObjectID()
Definition: TSensor.cpp:438
TSensor::CabeceraResultadosMedSensor
void CabeceraResultadosMedSensor(std::stringstream &medoutput)
Definition: TSensor.cpp:293
TSensor::ImprimeResultadosMedSensor
void ImprimeResultadosMedSensor(std::stringstream &medoutput)
Definition: TSensor.cpp:332
TSensor::AsignaObjeto
void AsignaObjeto(TObject *Object)
Definition: TSensor.cpp:125
TDeposito
Definition: TDeposito.h:44
TSensor::Output
double Output()
Definition: TSensor.cpp:138
PutLabel
std::string PutLabel(int idx)
Returns an integer.
Definition: labels.cpp:475
TSensor::ImprimeResultadosInsSensor
void ImprimeResultadosInsSensor(std::stringstream &insoutput)
Definition: TSensor.cpp:350
TSensor::ReadSensor
void ReadSensor(const char *FileWAM, fpos_t &filepos)
Definition: TSensor.cpp:49
Exception
Custom exception class.
Definition: Exception.hpp:39
TSensor::LeeResultadosInsSensor
void LeeResultadosInsSensor(const char *FileWAM, fpos_t &filepos)
Definition: TSensor.cpp:262
TSensor::ActualizaMedida
void ActualizaMedida(double Time)
Definition: TSensor.cpp:142
TTubo.h
TBloqueMotor
Definition: TBloqueMotor.h:43
TSensor::AcumulaResultadosMediosSensor
void AcumulaResultadosMediosSensor(double Actual)
Definition: TSensor.cpp:402
pow2
T pow2(T x)
Returns x to the power of 2.
Definition: Math_wam.h:88
TObject
Definition: Globales.h:2031
TSensor::ResultadosMediosSensor
void ResultadosMediosSensor()
Definition: TSensor.cpp:382
TSensor::TSensor
TSensor(int i)
Definition: TSensor.cpp:38
TSensor::IniciaMedias
void IniciaMedias()
Definition: TSensor.cpp:367
TSensor::~TSensor
~TSensor()
Definition: TSensor.cpp:45