NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tscDataObserver.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 "tscDataObserver.h"
28 
29 #include "tscDataLogger.h"
30 
31 #include "core/tgCast.h"
32 #include "core/tgModel.h"
33 #include "core/tgRod.h"
34 #include "core/tgString.h"
35 
37 
39 
40 #include <iostream>
41 #include <sstream>
42 #include <time.h>
43 #include <stdexcept>
44 
45 tscDataObserver::tscDataObserver(std::string filePrefix) :
46 m_totalTime(0.0),
47 m_dataLogger(NULL),
48 m_filePrefix(filePrefix)
49 {
50 
51 }
52 
55 {
56  delete m_dataLogger;
57  tgOutput.close();
58 }
59 
62 {
63  /*
64  * Adapted from: http://www.cplusplus.com/reference/clibrary/ctime/localtime/
65  * Also http://www.cplusplus.com/forum/unices/2259/
66  */
67  time_t rawtime;
68  tm* currentTime;
69  int fileTimeSize = 64;
70  char fileTime [fileTimeSize];
71 
72  time (&rawtime);
73  currentTime = localtime(&rawtime);
74  strftime(fileTime, fileTimeSize, "%m%d%Y_%H%M%S.txt", currentTime);
75  m_fileName = m_filePrefix + fileTime;
76  std::cout << m_fileName << std::endl;
77 
78  if (m_dataLogger != NULL)
79  {
80  // prevent leaks on loop behavior (better than teardown?)
81  delete m_dataLogger;
82  }
83 
84  m_dataLogger = new tscDataLogger(m_fileName);
85 
86  m_totalTime = 0.0;
87 
88  // First time opening this, so nothing to append to
89  tgOutput.open(m_fileName.c_str());
90 
91  if (!tgOutput.is_open())
92  {
93  throw std::runtime_error("Logs does not exist. Please create a logs folder in your build directory or update your cmake file");
94  }
95 
96  std::vector<tgModel*> children = model.getDescendants();
97 
98  /*
99  * Numbers to ensure uniqueness of variable names in log
100  * May be redundant with tag
101  */
102  int stringNum = 0;
103  int rodNum = 0;
104 
105  tgOutput << "Time" << ",";
106 
107  for (std::size_t i = 0; i < children.size(); i++)
108  {
109  /* If its a type we'll be logging, record its name and the
110  * variable types we'll be logging later
111  */
112  std::stringstream name;
113 
114  if(tgCast::cast<tgModel, tgSpringCableActuator>(children[i]) != 0)
115  {
116  name << children[i]->getTags() << " " << stringNum;
117  tgOutput << name.str() << "_RL" << ","
118  << name.str() << "_AL" << ","
119  << name.str() << "_Ten" << ",";
120  stringNum++;
121  }
122  else if(tgCast::cast<tgModel, tgRod>(children[i]) != 0)
123  {
124 
125  }
126  // Else do nothing since tscDataLogger won't touch it
127  }
128 
129  int numSeg = model.getSegments();
130 
131  for (int i = 0; i < numSeg; i++)
132  {
133  std::stringstream name;
134 
135  name << "segment_" << i+1;
136  tgOutput << name.str() << "_X" << ","
137  << name.str() << "_Y" << ","
138  << name.str() << "_Z" << ",";
139  }
140 
141  tgOutput << std::endl;
142 
143  tgOutput.close();
144 }
145 
152 {
153  m_totalTime += dt;
154  tgOutput.open(m_fileName.c_str(), std::ios::app);
155  tgOutput << m_totalTime << ",";
156  tgOutput.close();
157 
158  model.onVisit(*m_dataLogger);
159 
160  tgOutput.open(m_fileName.c_str(), std::ios::app);
161 
162  int numSeg = model.getSegments();
163 
164  for (int i = 0; i < numSeg; i++)
165  {
166 
167  std::vector<double> com = model.getSegmentCOM(i);
168 
169  tgOutput << com[0] << ","
170  << com[1] << ","
171  << com[2] << ",";
172  }
173 
174  tgOutput << std::endl;
175  tgOutput.close();
176 }
virtual void onSetup(BaseSpineModelLearning &model)
virtual ~tscDataObserver()
Convenience function for combining strings with ints, mostly for naming structures.
Utility class for class casting and filtering collections by type.
A template base class for a tensegrity spine.
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...
virtual void onStep(BaseSpineModelLearning &model, double dt)
Definition of tgObserver class.
Contains the definition of interface class tscDataLogger.
Contains the definition of class tgRod.
std::vector< tgModel * > getDescendants() const
Definition: tgModel.cpp:170