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., 675 Mass Ave, Cambridge, MA 02139, 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 <Main/fg_props.hxx>
37 #include "flightrecorder.hxx"
39 using namespace FlightRecorder;
41 FGFlightRecorder::FGFlightRecorder(const char* pConfigName) :
42 m_RecorderNode(fgGetNode("/sim/flight-recorder", true)),
44 m_ConfigName(pConfigName)
48 FGFlightRecorder::~FGFlightRecorder()
53 FGFlightRecorder::reinit(void)
57 m_TotalRecordSize = 0;
59 m_CaptureDouble.clear();
60 m_CaptureFloat.clear();
61 m_CaptureInteger.clear();
62 m_CaptureInt16.clear();
63 m_CaptureInt8.clear();
64 m_CaptureBool.clear();
66 int Selected = m_RecorderNode->getIntValue(m_ConfigName, 0);
67 SG_LOG(SG_SYSTEMS, SG_INFO, "FlightRecorder: Recorder configuration #" << Selected);
69 m_ConfigNode = m_RecorderNode->getChild("config", Selected);
71 if (!m_ConfigNode.valid())
74 if (!m_ConfigNode.valid())
76 SG_LOG(SG_SYSTEMS, SG_ALERT, "FlightRecorder: Configuration is invalid. Flight recorder disabled.");
80 // set name of active flight recorder type
81 const char* pRecorderName =
82 m_ConfigNode->getStringValue("name",
83 "aircraft-specific flight recorder");
84 SG_LOG(SG_SYSTEMS, SG_INFO, "FlightRecorder: Using custom recorder configuration: " << pRecorderName);
85 m_RecorderNode->setStringValue("active-config-name", pRecorderName);
88 initSignalList("double", m_CaptureDouble, m_ConfigNode );
89 initSignalList("float", m_CaptureFloat , m_ConfigNode );
90 initSignalList("int", m_CaptureInteger, m_ConfigNode );
91 initSignalList("int16", m_CaptureInt16 , m_ConfigNode );
92 initSignalList("int8", m_CaptureInt8 , m_ConfigNode );
93 initSignalList("bool", m_CaptureBool , m_ConfigNode );
96 // calculate size of a single record
97 m_TotalRecordSize = sizeof(double) * 1 /* sim time */ +
98 sizeof(double) * m_CaptureDouble.size() +
99 sizeof(float) * m_CaptureFloat.size() +
100 sizeof(int) * m_CaptureInteger.size() +
101 sizeof(short int) * m_CaptureInt16.size() +
102 sizeof(signed char) * m_CaptureInt8.size() +
103 sizeof(unsigned char) * ((m_CaptureBool.size()+7)/8); // 8 bools per byte
105 // expose size of actual flight recorder record
106 m_RecorderNode->setIntValue("record-size", m_TotalRecordSize);
107 SG_LOG(SG_SYSTEMS, SG_INFO, "FlightRecorder: record size is " << m_TotalRecordSize << " bytes");
110 /** Check if SignalList already contains the given property */
112 FGFlightRecorder::haveProperty(FlightRecorder::TSignalList& SignalList,SGPropertyNode* pProperty)
114 unsigned int Count = SignalList.size();
115 for (unsigned int i=0; i<Count; i++)
117 if (SignalList[i].Signal.get() == pProperty)
125 /** Check if any signal list already contains the given property */
127 FGFlightRecorder::haveProperty(SGPropertyNode* pProperty)
129 if (haveProperty(m_CaptureDouble, pProperty))
131 if (haveProperty(m_CaptureFloat, pProperty))
133 if (haveProperty(m_CaptureInteger, pProperty))
135 if (haveProperty(m_CaptureInt16, pProperty))
137 if (haveProperty(m_CaptureInt8, pProperty))
139 if (haveProperty(m_CaptureBool, pProperty))
144 /** Read default flight-recorder configuration.
145 * Default should match properties as hard coded for versions up to FG2.4.0. */
147 FGFlightRecorder::initDefault(void)
149 // set name of active flight recorder type
150 SG_LOG(SG_SYSTEMS, SG_INFO, "FlightRecorder: No custom configuration. Loading generic default recorder.");
152 const char* Path = m_RecorderNode->getStringValue("default-config",NULL);
155 SG_LOG(SG_SYSTEMS, SG_ALERT, "FlightRecorder: No default flight recorder specified! Check preferences.xml!");
159 SGPath path = globals->resolve_aircraft_path(Path);
162 SG_LOG(SG_SYSTEMS, SG_ALERT, "FlightRecorder: Cannot find file '" << Path << "'.");
168 readProperties(path.str(), m_RecorderNode->getChild("config", 0 ,true), 0);
169 m_ConfigNode = m_RecorderNode->getChild("config", 0 ,false);
170 } catch (sg_io_exception &e)
172 SG_LOG(SG_SYSTEMS, SG_ALERT, "FlightRecorder: Error reading file '" <<
173 Path << ": " << e.getFormattedMessage());
179 /** Read signal list below given base node.
180 * Only process properties of given signal type and add all signals to the given list.
181 * This method is called for all supported signal types - properties of each type are
182 * kept in separate lists for efficiency reasons. */
184 FGFlightRecorder::initSignalList(const char* pSignalType, TSignalList& SignalList, SGPropertyNode_ptr BaseNode)
189 processSignalList(pSignalType, SignalList, BaseNode);
191 SG_LOG(SG_SYSTEMS, SG_DEBUG, "FlightRecorder: " << SignalList.size() << " signals of type " << pSignalType );
194 /** Process 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::processSignalList(const char* pSignalType, TSignalList& SignalList, SGPropertyNode_ptr SignalListNode,
200 string PropPrefix, int Count)
202 // get the list of signal sources (property paths) for this signal type
203 SGPropertyNode_ptr SignalNode;
206 Count = SignalListNode->getIntValue("count",Count);
207 PropPrefix = simgear::strutils::strip(SignalListNode->getStringValue("prefix",PropPrefix.c_str()));
208 if ((!PropPrefix.empty())&&(PropPrefix[PropPrefix.size()-1] != '/'))
213 SignalNode = SignalListNode->getChild("signal",Index,false);
214 if (SignalNode.valid()&&
215 (0==strcmp(pSignalType, SignalNode->getStringValue("type","float"))))
217 string PropertyPath = SignalNode->getStringValue("property","");
218 if (!PropertyPath.empty())
220 PropertyPath = PropPrefix + PropertyPath;
221 const char* pInterpolation = SignalNode->getStringValue("interpolation","linear");
223 // Check if current signal has a "%i" place holder. Otherwise count is 1.
224 string::size_type IndexPos = PropertyPath.find("%i");
225 int SignalCount = Count;
226 if (IndexPos == string::npos)
229 for (int IndexValue=0;IndexValue<SignalCount;IndexValue++)
231 string PPath = PropertyPath;
232 if (IndexPos != string::npos)
235 snprintf(strbuf, 20, "%d", IndexValue);
236 PPath = PPath.replace(IndexPos,2,strbuf);
239 Capture.Signal = fgGetNode(PPath.c_str(),false);
240 if (!Capture.Signal.valid())
242 // warn user: we're maybe going to record useless data
243 // Or maybe the data is only initialized later. Warn anyway, so we can catch useless data.
244 SG_LOG(SG_SYSTEMS, SG_INFO, "FlightRecorder: Recording non-existent property '" << PPath << "'.");
245 Capture.Signal = fgGetNode(PPath.c_str(),true);
248 if (0==strcmp(pInterpolation,"discrete"))
249 Capture.Interpolation = discrete;
251 if ((0==strcmp(pInterpolation,"angular"))||
252 (0==strcmp(pInterpolation,"angular-rad")))
253 Capture.Interpolation = angular_rad;
255 if (0==strcmp(pInterpolation,"angular-deg"))
256 Capture.Interpolation = angular_deg;
258 if (0==strcmp(pInterpolation,"linear"))
259 Capture.Interpolation = linear;
262 SG_LOG(SG_SYSTEMS, SG_ALERT, "FlightRecorder: Unsupported interpolation type '"
263 << pInterpolation<< "' of signal '" << PPath << "'");
264 Capture.Interpolation = linear;
266 if (haveProperty(Capture.Signal))
268 SG_LOG(SG_SYSTEMS, SG_ALERT, "FlightRecorder: Property '"
269 << PPath << "' specified multiple times. Check flight recorder configuration.");
272 SignalList.push_back(Capture);
277 } while (SignalNode.valid());
279 // allow recursive definition of signal lists
280 simgear::PropertyList Nodes = SignalListNode->getChildren("signals");
281 for (unsigned int i=0;i<Nodes.size();i++)
283 processSignalList(pSignalType, SignalList, Nodes[i], PropPrefix, Count);
287 /** Get an empty container for a single capture. */
289 FGFlightRecorder::createEmptyRecord(void)
291 if (!m_TotalRecordSize)
293 FGReplayData* p = (FGReplayData*) new unsigned char[m_TotalRecordSize];
297 /** Free given container with capture data. */
299 FGFlightRecorder::deleteRecord(FGReplayData* pRecord)
305 * When pBuffer==NULL new memory is allocated.
306 * If pBuffer!=NULL memory of given buffer is reused.
309 FGFlightRecorder::capture(double SimTime, FGReplayData* pRecycledBuffer)
311 if (!pRecycledBuffer)
313 pRecycledBuffer = createEmptyRecord();
314 if (!pRecycledBuffer)
317 unsigned char* pBuffer = (unsigned char*) pRecycledBuffer;
320 pRecycledBuffer->sim_time = SimTime;
321 Offset += sizeof(double);
323 // 64bit aligned data first!
326 double* pDoubles = (double*) &pBuffer[Offset];
327 unsigned int SignalCount = m_CaptureDouble.size();
328 for (unsigned int i=0; i<SignalCount; i++)
330 pDoubles[i] = m_CaptureDouble[i].Signal->getDoubleValue();
332 Offset += SignalCount * sizeof(double);
335 // 32bit aligned data comes second...
338 float* pFloats = (float*) &pBuffer[Offset];
339 unsigned int SignalCount = m_CaptureFloat.size();
340 for (unsigned int i=0; i<SignalCount; i++)
342 pFloats[i] = m_CaptureFloat[i].Signal->getFloatValue();
344 Offset += SignalCount * sizeof(float);
348 // capture integers (32bit aligned)
349 int* pInt = (int*) &pBuffer[Offset];
350 unsigned int SignalCount = m_CaptureInteger.size();
351 for (unsigned int i=0; i<SignalCount; i++)
353 pInt[i] = m_CaptureInteger[i].Signal->getIntValue();
355 Offset += SignalCount * sizeof(int);
358 // 16bit aligned data is next...
360 // capture 16bit short integers
361 short int* pShortInt = (short int*) &pBuffer[Offset];
362 unsigned int SignalCount = m_CaptureInt16.size();
363 for (unsigned int i=0; i<SignalCount; i++)
365 pShortInt[i] = (short int) m_CaptureInt16[i].Signal->getIntValue();
367 Offset += SignalCount * sizeof(short int);
370 // finally: byte aligned data is last...
372 // capture 8bit chars
373 signed char* pChar = (signed char*) &pBuffer[Offset];
374 unsigned int SignalCount = m_CaptureInt8.size();
375 for (unsigned int i=0; i<SignalCount; i++)
377 pChar[i] = (signed char) m_CaptureInt8[i].Signal->getIntValue();
379 Offset += SignalCount * sizeof(signed char);
383 // capture 1bit booleans (8bit aligned)
384 unsigned char* pFlags = (unsigned char*) &pBuffer[Offset];
385 unsigned int SignalCount = m_CaptureBool.size();
386 int Size = (SignalCount+7)/8;
388 memset(pFlags,0,Size);
389 for (unsigned int i=0; i<SignalCount; i++)
391 if (m_CaptureBool[i].Signal->getBoolValue())
392 pFlags[i>>3] |= 1 << (i&7);
396 assert(Offset == m_TotalRecordSize);
398 return (FGReplayData*) pBuffer;
401 /** Do interpolation as defined by given interpolation type and weighting ratio. */
403 weighting(TInterpolation interpolation, double ratio, double v1,double v2)
405 switch (interpolation)
408 return v1 + ratio*(v2-v1);
412 // special handling of angular data
413 double tmp = v2 - v1;
416 else if ( tmp < -180 )
418 return v1 + tmp * ratio;
423 // special handling of angular data
424 double tmp = v2 - v1;
427 else if ( tmp < -SGD_PI )
429 return v1 + tmp * ratio;
440 * Restore all properties with data from given buffer. */
442 FGFlightRecorder::replay(double SimTime, const FGReplayData* _pNextBuffer, const FGReplayData* _pLastBuffer)
444 const char* pLastBuffer = (const char*) _pLastBuffer;
445 const char* pBuffer = (const char*) _pNextBuffer;
453 double NextSimTime = _pNextBuffer->sim_time;
454 double LastSimTime = _pLastBuffer->sim_time;
455 ratio = (SimTime - LastSimTime) / (NextSimTime - LastSimTime);
462 Offset += sizeof(double);
464 // 64bit aligned data first!
467 const double* pDoubles = (const double*) &pBuffer[Offset];
468 const double* pLastDoubles = (const double*) &pLastBuffer[Offset];
469 unsigned int SignalCount = m_CaptureDouble.size();
470 for (unsigned int i=0; i<SignalCount; i++)
472 double v = pDoubles[i];
475 v = weighting(m_CaptureDouble[i].Interpolation, ratio,
478 m_CaptureDouble[i].Signal->setDoubleValue(v);
480 Offset += SignalCount * sizeof(double);
483 // 32bit aligned data comes second...
486 const float* pFloats = (const float*) &pBuffer[Offset];
487 const float* pLastFloats = (const float*) &pLastBuffer[Offset];
488 unsigned int SignalCount = m_CaptureFloat.size();
489 for (unsigned int i=0; i<SignalCount; i++)
491 float v = pFloats[i];
494 v = weighting(m_CaptureFloat[i].Interpolation, ratio,
497 m_CaptureFloat[i].Signal->setDoubleValue(v);//setFloatValue
499 Offset += SignalCount * sizeof(float);
503 // restore integers (32bit aligned)
504 const int* pInt = (const int*) &pBuffer[Offset];
505 unsigned int SignalCount = m_CaptureInteger.size();
506 for (unsigned int i=0; i<SignalCount; i++)
508 m_CaptureInteger[i].Signal->setIntValue(pInt[i]);
510 Offset += SignalCount * sizeof(int);
513 // 16bit aligned data is next...
515 // restore 16bit short integers
516 const short int* pShortInt = (const short int*) &pBuffer[Offset];
517 unsigned int SignalCount = m_CaptureInt16.size();
518 for (unsigned int i=0; i<SignalCount; i++)
520 m_CaptureInt16[i].Signal->setIntValue(pShortInt[i]);
522 Offset += SignalCount * sizeof(short int);
525 // finally: byte aligned data is last...
527 // restore 8bit chars
528 const signed char* pChar = (const signed char*) &pBuffer[Offset];
529 unsigned int SignalCount = m_CaptureInt8.size();
530 for (unsigned int i=0; i<SignalCount; i++)
532 m_CaptureInt8[i].Signal->setIntValue(pChar[i]);
534 Offset += SignalCount * sizeof(signed char);
538 // restore 1bit booleans (8bit aligned)
539 const unsigned char* pFlags = (const unsigned char*) &pBuffer[Offset];
540 unsigned int SignalCount = m_CaptureBool.size();
541 int Size = (SignalCount+7)/8;
543 for (unsigned int i=0; i<SignalCount; i++)
545 m_CaptureBool[i].Signal->setBoolValue(0 != (pFlags[i>>3] & (1 << (i&7))));