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