NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgCPGJSONLogger.cpp
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 "tgCPGJSONLogger.h"
27 
28 #include "JSONCPGControl.h"
29 
30 #include <iostream>
31 #include <fstream>
32 #include <string>
33 
37 tgCPGJSONLogger::tgCPGJSONLogger(std::string fileName) :
38 time (0.0),
39 m_fileName(fileName)
40 {
41 }
42 
45 { }
46 
47 void tgCPGJSONLogger::onStep(JSONCPGControl& 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 }
An adaptation of JSONCPGControl that utilizes JSON for parameters.
tgCPGJSONLogger(std::string fileName)
virtual ~tgCPGJSONLogger()
virtual void onStep(JSONCPGControl &subject, double dt)