NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgCPGLogger.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 
26 #include "tgCPGLogger.h"
27 
28 #include "BaseSpineCPGControl.h"
29 
30 #include <iostream>
31 #include <fstream>
32 #include <string>
33 
37 tgCPGLogger::tgCPGLogger(std::string fileName) :
38 time (0.0),
39 m_fileName(fileName)
40 {
41 }
42 
45 { }
46 
47 void tgCPGLogger::onStep(BaseSpineCPGControl& subject, double dt)
48 {
49  time += dt;
50 
51  // Then, open our output stream and render!
52  std::ofstream tgOutput;
53  tgOutput.open(m_fileName.c_str(), std::ios::app);
54 
55  tgOutput << time << ",";
56 
57  bool runLoop = true;
58  std::size_t i = 0;
59 
60  while (runLoop)
61  {
62  // Don't know how many nodes are in CPG, so just try until we fail
63  try
64  {
65  tgOutput << subject.getCPGValue(i) << ",";
66  i++;
67  }
68  catch (std::invalid_argument e)
69  {
70  // Got the error we expected, end the loop
71  tgOutput << std::endl;
72  runLoop = false;
73  }
74  }
75 
76  tgOutput.close();
77 }
tgCPGLogger(std::string fileName)
Definition: tgCPGLogger.cpp:37
virtual void onStep(BaseSpineCPGControl &subject, double dt)
Definition: tgCPGLogger.cpp:47
virtual ~tgCPGLogger()
Definition: tgCPGLogger.cpp:44
A controller for the template class BaseSpineModelLearning.
Contains the definition of interface class tgCPGLogger.