NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
colSpineSine.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 
28 #include "colSpineSine.h"
29 
30 #include <string>
31 
32 
33 // Should include tgString, but compiler complains since its been
34 // included from TetraSpineLearningModel. Perhaps we should move things
35 // to a cpp over there
38 
41 
42 #include "examples/IROS_2015/hardwareSineWaves/tgSineStringControl.h"
43 
44 // JSON Serialization
45 #include "helpers/FileHelpers.h"
46 #include <json/json.h>
47 
48 // The C++ Standard Library
49 #include <stdexcept>
50 
51 //#define LOGGING
52 
58 colSpineSine::colSpineSine(std::string args,
59  std::string resourcePath)
60 {
61  if (resourcePath != "")
62  {
63  controlFilePath = FileHelpers::getResourcePath(resourcePath);
64  }
65  else
66  {
67  controlFilePath = "";
68  }
69 
70  controlFilename = controlFilePath + args;
71 
72 }
73 
75 {
76 
77  initConditions = subject.getSegmentCOM(0);
78 
79  setupWaves(subject);
80 
81 
82  m_updateTime = 0.0;
83 }
84 
86 {
88  m_updateTime += dt;
89  if (m_updateTime >= m_controlTime)
90  {
91 
92  m_updateTime = 0;
93  }
94 }
95 
97 {
98 
99  std::vector<double> finalConditions = subject.getSegmentCOM(0);
100 
101  const double newX = finalConditions[0];
102  const double newZ = finalConditions[2];
103  const double oldX = initConditions[0];
104  const double oldZ = initConditions[2];
105 
106  const double distanceMoved = sqrt((newX-oldX) * (newX-oldX) +
107  (newZ-oldZ) * (newZ-oldZ));
108 
109  std::cout << "Dist travelled " << distanceMoved << std::endl;
110 
111  Json::Value root; // will contains the root value after parsing.
112  Json::Reader reader;
113 
114  bool parsingSuccessful = reader.parse( FileHelpers::getFileString(controlFilename.c_str()), root );
115  if ( !parsingSuccessful )
116  {
117  // report to the user the failure and their locations in the document.
118  std::cout << "Failed to parse configuration\n"
119  << reader.getFormattedErrorMessages();
120  throw std::invalid_argument("Bad filename for JSON");
121  }
122 
123  root["scores"] = distanceMoved;
124 
125  std::ofstream payloadLog;
126  payloadLog.open(controlFilename.c_str(),std::ofstream::out);
127 
128  payloadLog << root << std::endl;
129 
130  for(size_t i = 0; i < m_sineControllers.size(); i++)
131  {
132  delete m_sineControllers[i];
133  }
134  m_sineControllers.clear();
135 }
136 
138 {
139  std::vector <tgSpringCableActuator*> allMuscles = subject.getAllMuscles();
140 
141  double tension;
142  double kPosition;
143  double kVelocity;
144  double controlLength;
145 
146  double amplitude;
147  double phase;
148  double phaseOffset;
149  double offset;
150 
151  Json::Value root; // will contains the root value after parsing.
152  Json::Reader reader;
153 
154  bool parsingSuccessful = reader.parse( FileHelpers::getFileString(controlFilename.c_str()), root );
155  if ( !parsingSuccessful )
156  {
157  // report to the user the failure and their locations in the document.
158  std::cout << "Failed to parse configuration\n"
159  << reader.getFormattedErrorMessages();
161  throw std::invalid_argument("Bad JSON filename");
162  }
163 
164  m_controlTime = root.get("updateFrequency", "UTF-8").asDouble();
165  double frequency = root.get("cpg_frequency", "UTF-8").asDouble();
166  double bodywaves = root.get("body_waves", "UTF-8").asDouble() * 2.0 * M_PI / (allMuscles.size() / 6.0);
167 
168  for (std::size_t i = 0; i < allMuscles.size(); i++)
169  {
170  if (allMuscles[i]->hasTag("inner top"))
171  {
172  tension = 100.0;
173  kPosition = 600.0;
174 
175  controlLength = allMuscles[i]->getStartLength();
176 
177  amplitude = root.get("in_top_amp_a", "UTF-8").asDouble();
179  phase = i * bodywaves + root.get("in_top_offset", "UTF-8").asDouble();
180  kVelocity = 50.0;
181 
182  }
183  else if (allMuscles[i]->hasTag("outer top"))
184  {
185  tension = 200.0;
186  kPosition = 500.0;
187  kVelocity = 100.0;
188 
189 
190  amplitude = root.get("out_top_amp_a", "UTF-8").asDouble();
191  phase = i * bodywaves + root.get("out_top_offset", "UTF-8").asDouble();
192  controlLength = allMuscles[i]->getStartLength();
193 
194  }
195  else if (allMuscles[i]->hasTag("inner left"))
196  {
197  tension = 100.0;
198  kPosition = 600.0;
199  kVelocity = 50.0;
200  controlLength = allMuscles[i]->getStartLength();
201 
202  amplitude = root.get("in_bottom_amp_a", "UTF-8").asDouble();
203  phase = i * bodywaves + root.get("in_left_offset", "UTF-8").asDouble();
204 
205  }
206  else if (allMuscles[i]->hasTag("outer left"))
207  {
208  tension = 50.0;
209  kPosition = 500.0;
210  kVelocity = 100.0;
211  controlLength = allMuscles[i]->getStartLength();
212 
213  amplitude = root.get("out_bottom_amp_a", "UTF-8").asDouble();
214  phase = i * bodywaves + root.get("out_left_offset", "UTF-8").asDouble();
215 
216  //2 * bodyWaves * M_PI * i / (segments) + phaseOffsets[phase]
217 
218  }
219  else if (allMuscles[i]->hasTag("inner right"))
220  {
221  tension = 100.0;
222  kPosition = 600.0;
223  kVelocity = 50.0;
224  controlLength = allMuscles[i]->getStartLength();
225 
226  amplitude = root.get("in_bottom_amp_a", "UTF-8").asDouble();
227  phase = i * bodywaves + root.get("in_right_offset", "UTF-8").asDouble();
228 
229  }
230  else if (allMuscles[i]->hasTag("outer right"))
231  {
232  tension = 50.0;
233  kPosition = 500.0;
234  kVelocity = 100.0;
235  controlLength = allMuscles[i]->getStartLength();
236 
237  amplitude = root.get("out_bottom_amp_a", "UTF-8").asDouble();
238  phase = i * bodywaves + root.get("out_right_offset", "UTF-8").asDouble();
239 
240  //2 * bodyWaves * M_PI * i / (segments) + phaseOffsets[phase]
241 
242  }
243  else
244  {
245  throw std::runtime_error("Missing tags!");
246  }
247 
248  tgImpedanceController* p_ipc = new tgImpedanceController( tension,
249  kPosition,
250  kVelocity);
251 
252  offset = 0.0;
253 
254  tgSineStringControl* pStringControl = new tgSineStringControl(m_controlTime,
255  p_ipc,
256  amplitude,
257  frequency,
258  phase,
259  offset,
260  controlLength);
261 
262 
263  allMuscles[i]->attach(pStringControl);
264  m_sineControllers.push_back(pStringControl);
265  }
266 
267  assert(m_sineControllers.size() == allMuscles.size());
268 
269 }
270 
Contains the definition of class ImpedanceControl. $Id$.
colSpineSine(std::string args, std::string resourcePath="")
A class to read a learning configuration from a .ini file.
Contains the definition of abstract base class tgSpringCableActuator. Assumes that the string is line...
A series of functions to assist with file input/output.
virtual void onTeardown(BaseSpineModelLearning &subject)
static std::string getResourcePath(std::string relPath)
Definition: FileHelpers.cpp:40
Contains the definition of class AnnealEvolution. Adapting NeuroEvolution to do Simulated Annealing...
Controller for TetraSpineCollisions.
virtual void onSetup(BaseSpineModelLearning &subject)
virtual void setupWaves(BaseSpineModelLearning &subject)
virtual void onStep(BaseSpineModelLearning &subject, double dt)