NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
tgDataObserver.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2012, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The NASA Tensegrity Robotics Toolkit (NTRT) v1 platform is licensed
7  * under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * http://www.apache.org/licenses/LICENSE-2.0.
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language
16  * governing permissions and limitations under the License.
17 */
18 
27 #include "tgDataObserver.h"
28 
29 #include "tgDataLogger.h"
30 
31 #include "core/tgCast.h"
32 #include "core/tgModel.h"
33 #include "core/tgRod.h"
34 #include "core/tgString.h"
35 
36 #include "core/tgLinearString.h"
37 
38 #include <iostream>
39 #include <sstream>
40 #include <time.h>
41 
42 tgDataObserver::tgDataObserver(std::string filePrefix) :
43 m_totalTime(0.0),
44 m_dataLogger(NULL),
45 m_filePrefix(filePrefix)
46 {
47 
48 }
49 
52 {
53  delete m_dataLogger;
54  tgOutput.close();
55 }
56 
59 {
60  /*
61  * Adapted from: http://www.cplusplus.com/reference/clibrary/ctime/localtime/
62  * Also http://www.cplusplus.com/forum/unices/2259/
63  */
64  time_t rawtime;
65  tm* currentTime;
66  int fileTimeSize = 64;
67  char fileTime [fileTimeSize];
68 
69  time (&rawtime);
70  currentTime = localtime(&rawtime);
71  strftime(fileTime, fileTimeSize, "%m%d%Y_%H%M%S.txt", currentTime);
72  m_fileName = m_filePrefix + fileTime;
73  std::cout << m_fileName << std::endl;
74 
75  if (m_dataLogger != NULL)
76  {
77  // prevent leaks on loop behavior (better than teardown?)
78  delete m_dataLogger;
79  }
80 
81  m_dataLogger = new tgDataLogger(m_fileName);
82 
83  m_totalTime = 0.0;
84 
85  // First time opening this, so nothing to append to
86  tgOutput.open(m_fileName.c_str());
87 
88  std::vector<tgModel*> children = model.getDescendants();
89 
90  /*
91  * Numbers to ensure uniqueness of variable names in log
92  * May be redundant with tag
93  */
94  int stringNum = 0;
95  int rodNum = 0;
96 
97  tgOutput << "Time" << ",";
98 
99  for (std::size_t i = 0; i < children.size(); i++)
100  {
101  /* If its a type we'll be logging, record its name and the
102  * variable types we'll be logging later
103  */
104  std::stringstream name;
105 
106  if(tgCast::cast<tgModel, tgLinearString>(children[i]) != 0)
107  {
108  name << children[i]->getTags() << " " << stringNum;
109  tgOutput << name.str() << "_RL" << ","
110  << name.str() << "_AL" << ","
111  << name.str() << "_Ten" << ",";
112  stringNum++;
113  }
114  else if(tgCast::cast<tgModel, tgRod>(children[i]) != 0)
115  {
116  name << children[i]->getTags() << " " << rodNum;
117  tgOutput << name.str() << "_X" << ","
118  << name.str() << "_Y" << ","
119  << name.str() << "_Z" << ","
120  << name.str() << "_mass" << ",";
121  rodNum++;
122  }
123  // Else do nothing since tgDataLogger won't touch it
124  }
125 
126  tgOutput << std::endl;
127 
128  tgOutput.close();
129 }
130 
136 void tgDataObserver::onStep(tgModel& model, double dt)
137 {
138  m_totalTime += dt;
139  tgOutput.open(m_fileName.c_str(), std::ios::app);
140  tgOutput << m_totalTime << ",";
141  tgOutput.close();
142 
143  model.onVisit(*m_dataLogger);
144 
145  tgOutput.open(m_fileName.c_str(), std::ios::app);
146  tgOutput << std::endl;
147  tgOutput.close();
148 }
Contains the definition of interface class tgDataLogger.
Convenience function for combining strings with ints, mostly for naming structures.
Utility class for class casting and filtering collections by type.
virtual void onVisit(const tgModelVisitor &r) const
Definition: tgModel.cpp:109
Contains the definition of class tgModel. $Id$.
Contains the definition of class tgLinearString.
Definition of tgObserver class.
virtual void onStep(tgModel &model, double dt)
virtual void onSetup(tgModel &model)
Contains the definition of class tgRod $Id$.
virtual ~tgDataObserver()
std::vector< tgModel * > getDescendants() const
Definition: tgModel.cpp:174