NTRT Simulator  v1.1
 All Classes Namespaces 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 
37 
38 #include <iostream>
39 #include <sstream>
40 #include <time.h>
41 #include <stdexcept>
42 
43 tgDataObserver::tgDataObserver(std::string filePrefix) :
44 m_totalTime(0.0),
45 m_dataLogger(NULL),
46 m_filePrefix(filePrefix)
47 {
48 
49 }
50 
53 {
54  delete m_dataLogger;
55  tgOutput.close();
56 }
57 
60 {
61  /*
62  * Adapted from: http://www.cplusplus.com/reference/clibrary/ctime/localtime/
63  * Also http://www.cplusplus.com/forum/unices/2259/
64  */
65  time_t rawtime;
66  tm* currentTime;
67  int fileTimeSize = 64;
68  char fileTime [fileTimeSize];
69 
70  time (&rawtime);
71  currentTime = localtime(&rawtime);
72  strftime(fileTime, fileTimeSize, "%m%d%Y_%H%M%S.txt", currentTime);
73  m_fileName = m_filePrefix + fileTime;
74  std::cout << m_fileName << std::endl;
75 
76  if (m_dataLogger != NULL)
77  {
78  // prevent leaks on loop behavior (better than teardown?)
79  delete m_dataLogger;
80  }
81 
82  m_dataLogger = new tgDataLogger(m_fileName);
83 
84  m_totalTime = 0.0;
85 
86  // First time opening this, so nothing to append to
87  tgOutput.open(m_fileName.c_str());
88 
89  if (!tgOutput.is_open())
90  {
91  throw std::runtime_error("Logs does not exist. Please create a logs folder in your build directory or update your cmake file");
92  }
93 
94  std::vector<tgModel*> children = model.getDescendants();
95 
96  /*
97  * Numbers to ensure uniqueness of variable names in log
98  * May be redundant with tag
99  */
100  int stringNum = 0;
101  int rodNum = 0;
102 
103  tgOutput << "Time" << ",";
104 
105  for (std::size_t i = 0; i < children.size(); i++)
106  {
107  /* If its a type we'll be logging, record its name and the
108  * variable types we'll be logging later
109  */
110  std::stringstream name;
111 
112  if(tgCast::cast<tgModel, tgSpringCableActuator>(children[i]) != 0)
113  {
114  name << children[i]->getTags() << " " << stringNum;
115  tgOutput << name.str() << "_RL" << ","
116  << name.str() << "_AL" << ","
117  << name.str() << "_Ten" << ",";
118  stringNum++;
119  }
120  else if(tgCast::cast<tgModel, tgRod>(children[i]) != 0)
121  {
122  name << children[i]->getTags() << " " << rodNum;
123  tgOutput << name.str() << "_X" << ","
124  << name.str() << "_Y" << ","
125  << name.str() << "_Z" << ","
126  << name.str() << "_mass" << ",";
127  rodNum++;
128  }
129  // Else do nothing since tgDataLogger won't touch it
130  }
131 
132  tgOutput << std::endl;
133 
134  tgOutput.close();
135 }
136 
142 void tgDataObserver::onStep(tgModel& model, double dt)
143 {
144  m_totalTime += dt;
145  tgOutput.open(m_fileName.c_str(), std::ios::app);
146  tgOutput << m_totalTime << ",";
147  tgOutput.close();
148 
149  model.onVisit(*m_dataLogger);
150 
151  tgOutput.open(m_fileName.c_str(), std::ios::app);
152  tgOutput << std::endl;
153  tgOutput.close();
154 }
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:107
Contains the definition of class tgModel.
Contains the definition of abstract base class tgSpringCableActuator. Assumes that the string is line...
Definition of tgObserver class.
virtual void onStep(tgModel &model, double dt)
virtual void onSetup(tgModel &model)
Contains the definition of class tgRod.
virtual ~tgDataObserver()
std::vector< tgModel * > getDescendants() const
Definition: tgModel.cpp:172