3 // Written by Thorsten Brehm, started August 2011.
5 // Copyright (C) 2011 Thorsten Brehm - brehmt (at) gmail com
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 ///////////////////////////////////////////////////////////////////////////////
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/props/props_io.hxx>
33 #include <simgear/misc/ResourceManager.hxx>
34 #include <simgear/misc/strutils.hxx>
35 #include <simgear/structure/exception.hxx>
36 #include <simgear/math/SGMath.hxx>
37 #include <Main/fg_props.hxx>
38 #include "flightrecorder.hxx"
40 using namespace FlightRecorder;
43 FGFlightRecorder::FGFlightRecorder(const char* pConfigName) :
44 m_RecorderNode(fgGetNode("/sim/flight-recorder", true)),
46 m_ConfigName(pConfigName)
50 FGFlightRecorder::~FGFlightRecorder()
55 FGFlightRecorder::reinit(void)
59 SGPropertyNode_ptr ConfigNode;
60 int Selected = m_RecorderNode->getIntValue(m_ConfigName, 0);
61 SG_LOG(SG_SYSTEMS, SG_INFO, "FlightRecorder: Recorder configuration #" << Selected);
63 ConfigNode = m_RecorderNode->getChild("config", Selected);
65 if (!ConfigNode.valid())
66 ConfigNode = getDefault();
72 FGFlightRecorder::reinit(SGPropertyNode_ptr ConfigNode)
74 m_TotalRecordSize = 0;
76 m_CaptureDouble.clear();
77 m_CaptureFloat.clear();
78 m_CaptureInteger.clear();
79 m_CaptureInt16.clear();
80 m_CaptureInt8.clear();
81 m_CaptureBool.clear();
83 m_ConfigNode = ConfigNode;
85 if (!m_ConfigNode.valid())
87 SG_LOG(SG_SYSTEMS, SG_ALERT, "FlightRecorder: Configuration is invalid. Flight recorder disabled.");
91 // set name of active flight recorder type
92 const char* pRecorderName =
93 m_ConfigNode->getStringValue("name",
94 "aircraft-specific flight recorder");
95 SG_LOG(SG_SYSTEMS, SG_INFO, "FlightRecorder: Using custom recorder configuration: " << pRecorderName);
96 m_RecorderNode->setStringValue("active-config-name", pRecorderName);
99 initSignalList("double", m_CaptureDouble, m_ConfigNode );
100 initSignalList("float", m_CaptureFloat , m_ConfigNode );
101 initSignalList("int", m_CaptureInteger, m_ConfigNode );
102 initSignalList("int16", m_CaptureInt16 , m_ConfigNode );
103 initSignalList("int8", m_CaptureInt8 , m_ConfigNode );
104 initSignalList("bool", m_CaptureBool , m_ConfigNode );
107 // calculate size of a single record
108 m_TotalRecordSize = sizeof(double) * 1 /* sim time */ +
109 sizeof(double) * m_CaptureDouble.size() +
110 sizeof(float) * m_CaptureFloat.size() +
111 sizeof(int) * m_CaptureInteger.size() +
112 sizeof(short int) * m_CaptureInt16.size() +
113 sizeof(signed char) * m_CaptureInt8.size() +
114 sizeof(unsigned char) * ((m_CaptureBool.size()+7)/8); // 8 bools per byte
116 // expose size of actual flight recorder record
117 m_RecorderNode->setIntValue("record-size", m_TotalRecordSize);
118 SG_LOG(SG_SYSTEMS, SG_INFO, "FlightRecorder: record size is " << m_TotalRecordSize << " bytes");
121 /** Check if SignalList already contains the given property */
123 FGFlightRecorder::haveProperty(FlightRecorder::TSignalList& SignalList,SGPropertyNode* pProperty)
125 unsigned int Count = SignalList.size();
126 for (unsigned int i=0; i<Count; i++)
128 if (SignalList[i].Signal.get() == pProperty)
136 /** Check if any signal list already contains the given property */
138 FGFlightRecorder::haveProperty(SGPropertyNode* pProperty)
140 if (haveProperty(m_CaptureDouble, pProperty))
142 if (haveProperty(m_CaptureFloat, pProperty))
144 if (haveProperty(m_CaptureInteger, pProperty))
146 if (haveProperty(m_CaptureInt16, pProperty))
148 if (haveProperty(m_CaptureInt8, pProperty))
150 if (haveProperty(m_CaptureBool, pProperty))
155 /** Read default flight-recorder configuration.
156 * Default should match properties as hard coded for versions up to FG2.4.0. */
158 FGFlightRecorder::getDefault(void)
160 SGPropertyNode_ptr ConfigNode;
162 // set name of active flight recorder type
163 SG_LOG(SG_SYSTEMS, SG_INFO, "FlightRecorder: No custom configuration. Loading generic default recorder.");
165 const char* Path = m_RecorderNode->getStringValue("default-config",NULL);
168 SG_LOG(SG_SYSTEMS, SG_ALERT, "FlightRecorder: No default flight recorder specified! Check preferences.xml!");
172 SGPath path = globals->resolve_aircraft_path(Path);
175 SG_LOG(SG_SYSTEMS, SG_ALERT, "FlightRecorder: Cannot find file '" << Path << "'.");
181 readProperties(path.str(), m_RecorderNode->getChild("config", 0 ,true), 0);
182 ConfigNode = m_RecorderNode->getChild("config", 0 ,false);
183 } catch (sg_io_exception &e)
185 SG_LOG(SG_SYSTEMS, SG_ALERT, "FlightRecorder: Error reading file '" <<
186 Path << ": " << e.getFormattedMessage());
194 /** Read signal list below given base node.
195 * Only process properties of given signal type and add all signals to the given list.
196 * This method is called for all supported signal types - properties of each type are
197 * kept in separate lists for efficiency reasons. */
199 FGFlightRecorder::initSignalList(const char* pSignalType, TSignalList& SignalList, SGPropertyNode_ptr BaseNode)
204 processSignalList(pSignalType, SignalList, BaseNode);
206 SG_LOG(SG_SYSTEMS, SG_DEBUG, "FlightRecorder: " << SignalList.size() << " signals of type " << pSignalType );
209 /** Process signal list below given base node.
210 * Only process properties of given signal type and add all signals to the given list.
211 * This method is called for all supported signal types - properties of each type are
212 * kept in separate lists for efficiency reasons. */
214 FGFlightRecorder::processSignalList(const char* pSignalType, TSignalList& SignalList, SGPropertyNode_ptr SignalListNode,
215 string PropPrefix, int Count)
217 // get the list of signal sources (property paths) for this signal type
218 SGPropertyNode_ptr SignalNode;
221 Count = SignalListNode->getIntValue("count",Count);
222 PropPrefix = simgear::strutils::strip(SignalListNode->getStringValue("prefix",PropPrefix.c_str()));
223 if ((!PropPrefix.empty())&&(PropPrefix[PropPrefix.size()-1] != '/'))
228 SignalNode = SignalListNode->getChild("signal",Index,false);
229 if (SignalNode.valid()&&
230 (0==strcmp(pSignalType, SignalNode->getStringValue("type","float"))))
232 string PropertyPath = SignalNode->getStringValue("property","");
233 if (!PropertyPath.empty())
235 PropertyPath = PropPrefix + PropertyPath;
236 const char* pInterpolation = SignalNode->getStringValue("interpolation","linear");
238 // Check if current signal has a "%i" place holder. Otherwise count is 1.
239 string::size_type IndexPos = PropertyPath.find("%i");
240 int SignalCount = Count;
241 if (IndexPos == string::npos)
244 for (int IndexValue=0;IndexValue<SignalCount;IndexValue++)
246 string PPath = PropertyPath;
247 if (IndexPos != string::npos)
250 snprintf(strbuf, 20, "%d", IndexValue);
251 PPath = PPath.replace(IndexPos,2,strbuf);
254 Capture.Signal = fgGetNode(PPath.c_str(),false);
255 if (!Capture.Signal.valid())
257 // warn user: we're maybe going to record useless data
258 // Or maybe the data is only initialized later. Warn anyway, so we can catch useless data.
259 SG_LOG(SG_SYSTEMS, SG_INFO, "FlightRecorder: Recording non-existent property '" << PPath << "'.");
260 Capture.Signal = fgGetNode(PPath.c_str(),true);
263 if (0==strcmp(pInterpolation,"discrete"))
264 Capture.Interpolation = discrete;
266 if ((0==strcmp(pInterpolation,"angular"))||
267 (0==strcmp(pInterpolation,"angular-rad")))
268 Capture.Interpolation = angular_rad;
270 if (0==strcmp(pInterpolation,"angular-deg"))
271 Capture.Interpolation = angular_deg;
273 if (0==strcmp(pInterpolation,"linear"))
274 Capture.Interpolation = linear;
277 SG_LOG(SG_SYSTEMS, SG_ALERT, "FlightRecorder: Unsupported interpolation type '"
278 << pInterpolation<< "' of signal '" << PPath << "'");
279 Capture.Interpolation = linear;
281 if (haveProperty(Capture.Signal))
283 SG_LOG(SG_SYSTEMS, SG_ALERT, "FlightRecorder: Property '"
284 << PPath << "' specified multiple times. Check flight recorder configuration.");
287 SignalList.push_back(Capture);
292 } while (SignalNode.valid());
294 // allow recursive definition of signal lists
295 simgear::PropertyList Nodes = SignalListNode->getChildren("signals");
296 for (unsigned int i=0;i<Nodes.size();i++)
298 processSignalList(pSignalType, SignalList, Nodes[i], PropPrefix, Count);
302 /** Get an empty container for a single capture. */
304 FGFlightRecorder::createEmptyRecord(void)
306 if (!m_TotalRecordSize)
308 FGReplayData* p = (FGReplayData*) new unsigned char[m_TotalRecordSize];
312 /** Free given container with capture data. */
314 FGFlightRecorder::deleteRecord(FGReplayData* pRecord)
320 * When pBuffer==NULL new memory is allocated.
321 * If pBuffer!=NULL memory of given buffer is reused.
324 FGFlightRecorder::capture(double SimTime, FGReplayData* pRecycledBuffer)
326 if (!pRecycledBuffer)
328 pRecycledBuffer = createEmptyRecord();
329 if (!pRecycledBuffer)
332 unsigned char* pBuffer = (unsigned char*) pRecycledBuffer;
335 pRecycledBuffer->sim_time = SimTime;
336 Offset += sizeof(double);
338 // 64bit aligned data first!
341 double* pDoubles = (double*) &pBuffer[Offset];
342 unsigned int SignalCount = m_CaptureDouble.size();
343 for (unsigned int i=0; i<SignalCount; i++)
345 pDoubles[i] = m_CaptureDouble[i].Signal->getDoubleValue();
347 Offset += SignalCount * sizeof(double);
350 // 32bit aligned data comes second...
353 float* pFloats = (float*) &pBuffer[Offset];
354 unsigned int SignalCount = m_CaptureFloat.size();
355 for (unsigned int i=0; i<SignalCount; i++)
357 pFloats[i] = m_CaptureFloat[i].Signal->getFloatValue();
359 Offset += SignalCount * sizeof(float);
363 // capture integers (32bit aligned)
364 int* pInt = (int*) &pBuffer[Offset];
365 unsigned int SignalCount = m_CaptureInteger.size();
366 for (unsigned int i=0; i<SignalCount; i++)
368 pInt[i] = m_CaptureInteger[i].Signal->getIntValue();
370 Offset += SignalCount * sizeof(int);
373 // 16bit aligned data is next...
375 // capture 16bit short integers
376 short int* pShortInt = (short int*) &pBuffer[Offset];
377 unsigned int SignalCount = m_CaptureInt16.size();
378 for (unsigned int i=0; i<SignalCount; i++)
380 pShortInt[i] = (short int) m_CaptureInt16[i].Signal->getIntValue();
382 Offset += SignalCount * sizeof(short int);
385 // finally: byte aligned data is last...
387 // capture 8bit chars
388 signed char* pChar = (signed char*) &pBuffer[Offset];
389 unsigned int SignalCount = m_CaptureInt8.size();
390 for (unsigned int i=0; i<SignalCount; i++)
392 pChar[i] = (signed char) m_CaptureInt8[i].Signal->getIntValue();
394 Offset += SignalCount * sizeof(signed char);
398 // capture 1bit booleans (8bit aligned)
399 unsigned char* pFlags = (unsigned char*) &pBuffer[Offset];
400 unsigned int SignalCount = m_CaptureBool.size();
401 int Size = (SignalCount+7)/8;
403 memset(pFlags,0,Size);
404 for (unsigned int i=0; i<SignalCount; i++)
406 if (m_CaptureBool[i].Signal->getBoolValue())
407 pFlags[i>>3] |= 1 << (i&7);
411 assert(Offset == m_TotalRecordSize);
413 return (FGReplayData*) pBuffer;
416 /** Do interpolation as defined by given interpolation type and weighting ratio. */
418 weighting(TInterpolation interpolation, double ratio, double v1,double v2)
420 switch (interpolation)
423 return v1 + ratio*(v2-v1);
427 // special handling of angular data
428 double tmp = v2 - v1;
431 else if ( tmp < -180 )
433 return v1 + tmp * ratio;
438 // special handling of angular data
439 double tmp = v2 - v1;
442 else if ( tmp < -SGD_PI )
444 return v1 + tmp * ratio;
455 * Restore all properties with data from given buffer. */
457 FGFlightRecorder::replay(double SimTime, const FGReplayData* _pNextBuffer, const FGReplayData* _pLastBuffer)
459 const char* pLastBuffer = (const char*) _pLastBuffer;
460 const char* pBuffer = (const char*) _pNextBuffer;
468 double NextSimTime = _pNextBuffer->sim_time;
469 double LastSimTime = _pLastBuffer->sim_time;
470 double Numerator = SimTime - LastSimTime;
471 double dt = NextSimTime - LastSimTime;
472 // avoid divide by zero and other quirks
473 if ((Numerator > 0.0)&&(dt != 0.0))
475 ratio = Numerator / dt;
481 Offset += sizeof(double);
483 // 64bit aligned data first!
486 const double* pDoubles = (const double*) &pBuffer[Offset];
487 const double* pLastDoubles = (const double*) &pLastBuffer[Offset];
488 unsigned int SignalCount = m_CaptureDouble.size();
489 for (unsigned int i=0; i<SignalCount; i++)
491 double v = pDoubles[i];
494 v = weighting(m_CaptureDouble[i].Interpolation, ratio,
497 m_CaptureDouble[i].Signal->setDoubleValue(v);
499 Offset += SignalCount * sizeof(double);
502 // 32bit aligned data comes second...
505 const float* pFloats = (const float*) &pBuffer[Offset];
506 const float* pLastFloats = (const float*) &pLastBuffer[Offset];
507 unsigned int SignalCount = m_CaptureFloat.size();
508 for (unsigned int i=0; i<SignalCount; i++)
510 float v = pFloats[i];
513 v = weighting(m_CaptureFloat[i].Interpolation, ratio,
516 m_CaptureFloat[i].Signal->setDoubleValue(v);//setFloatValue
518 Offset += SignalCount * sizeof(float);
522 // restore integers (32bit aligned)
523 const int* pInt = (const int*) &pBuffer[Offset];
524 unsigned int SignalCount = m_CaptureInteger.size();
525 for (unsigned int i=0; i<SignalCount; i++)
527 m_CaptureInteger[i].Signal->setIntValue(pInt[i]);
529 Offset += SignalCount * sizeof(int);
532 // 16bit aligned data is next...
534 // restore 16bit short integers
535 const short int* pShortInt = (const short int*) &pBuffer[Offset];
536 unsigned int SignalCount = m_CaptureInt16.size();
537 for (unsigned int i=0; i<SignalCount; i++)
539 m_CaptureInt16[i].Signal->setIntValue(pShortInt[i]);
541 Offset += SignalCount * sizeof(short int);
544 // finally: byte aligned data is last...
546 // restore 8bit chars
547 const signed char* pChar = (const signed char*) &pBuffer[Offset];
548 unsigned int SignalCount = m_CaptureInt8.size();
549 for (unsigned int i=0; i<SignalCount; i++)
551 m_CaptureInt8[i].Signal->setIntValue(pChar[i]);
553 Offset += SignalCount * sizeof(signed char);
557 // restore 1bit booleans (8bit aligned)
558 const unsigned char* pFlags = (const unsigned char*) &pBuffer[Offset];
559 unsigned int SignalCount = m_CaptureBool.size();
560 int Size = (SignalCount+7)/8;
562 for (unsigned int i=0; i<SignalCount; i++)
564 m_CaptureBool[i].Signal->setBoolValue(0 != (pFlags[i>>3] & (1 << (i&7))));
570 FGFlightRecorder::getConfig(SGPropertyNode* root, const char* typeStr, const FlightRecorder::TSignalList& SignalList)
572 static const char* InterpolationTypes[] = {"discrete", "linear", "angular-rad", "angular-deg"};
573 size_t SignalCount = SignalList.size();
574 SGPropertyNode* Signals = root->getNode("signals", true);
575 for (size_t i=0; i<SignalCount; i++)
577 SGPropertyNode* SignalProp = Signals->addChild("signal");
578 SignalProp->setStringValue("type", typeStr);
579 SignalProp->setStringValue("interpolation", InterpolationTypes[SignalList[i].Interpolation]);
580 SignalProp->setStringValue("property", SignalList[i].Signal->getPath());
582 SG_LOG(SG_SYSTEMS, SG_DEBUG, "FlightRecorder: Have " << SignalCount << " signals of type " << typeStr);
583 root->setIntValue(typeStr, SignalCount);
588 FGFlightRecorder::getConfig(SGPropertyNode* root)
590 root->setStringValue("name", m_RecorderNode->getStringValue("active-config-name", ""));
592 SignalCount += getConfig(root, "double", m_CaptureDouble);
593 SignalCount += getConfig(root, "float", m_CaptureFloat);
594 SignalCount += getConfig(root, "int", m_CaptureInteger);
595 SignalCount += getConfig(root, "int16", m_CaptureInt16);
596 SignalCount += getConfig(root, "int8", m_CaptureInt8);
597 SignalCount += getConfig(root, "bool", m_CaptureBool);
599 root->setIntValue("recorder/record-size", getRecordSize());
600 root->setIntValue("recorder/signal-count", SignalCount);