NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
colSpineSine.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 
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 "dev/btietz/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 
59 m_dataObserver("logs/TCData")
60 {
61 }
62 
64 {
65 
66  setupWaves(subject);
67 
68 #ifdef LOGGING // Conditional compile for data logging
69  m_dataObserver.onSetup(subject);
70 #endif
71 
72 
73  m_updateTime = 0.0;
74 }
75 
77 {
79  m_updateTime += dt;
80  if (m_updateTime >= m_controlTime)
81  {
82 
83 #ifdef LOGGING // Conditional compile for data logging
84  m_dataObserver.onStep(subject, m_updateTime);
85 #endif
86  m_updateTime = 0;
87  }
88 }
89 
91 {
92 
93  for(size_t i = 0; i < m_sineControllers.size(); i++)
94  {
95  delete m_sineControllers[i];
96  }
97  m_sineControllers.clear();
98 }
99 
101 {
102  std::vector <tgSpringCableActuator*> allMuscles = subject.getAllMuscles();
103 
104  double tension;
105  double kPosition;
106  double kVelocity;
107  double controlLength;
108 
109  double amplitude;
110  double phase;
111  double phaseOffset;
112  double offset;
113 
114  Json::Value root; // will contains the root value after parsing.
115  Json::Reader reader;
116 
117  bool parsingSuccessful = reader.parse( FileHelpers::getFileString("controlVars.json"), root );
118  if ( !parsingSuccessful )
119  {
120  // report to the user the failure and their locations in the document.
121  std::cout << "Failed to parse configuration\n"
122  << reader.getFormattedErrorMessages();
124  throw std::invalid_argument("Bad JSON filename");
125  }
126 
127  m_controlTime = root.get("updateFrequency", "UTF-8").asDouble();
128  double frequency = root.get("cpg_frequency", "UTF-8").asDouble();
129  double bodywaves = root.get("body_waves", "UTF-8").asDouble() * 2.0 * M_PI / (allMuscles.size() / 6.0);
130 
131  for (std::size_t i = 0; i < allMuscles.size(); i++)
132  {
133  if (allMuscles[i]->hasTag("inner top"))
134  {
135  tension = 100.0;
136  kPosition = 300.0;
137 
138  controlLength = allMuscles[i]->getStartLength();
139 
140  amplitude = root.get("in_top_amp_a", "UTF-8").asDouble();
142  phase = i * bodywaves + root.get("in_top_offset", "UTF-8").asDouble();
143  kVelocity = 50.0;
144 
145  }
146  else if (allMuscles[i]->hasTag("outer top"))
147  {
148  tension = 200.0;
149  kPosition = 200.0;
150  kVelocity = 100.0;
151 
152 
153  amplitude = root.get("out_top_amp_a", "UTF-8").asDouble();
154  phase = i * bodywaves + root.get("out_top_offset", "UTF-8").asDouble();
155  controlLength = allMuscles[i]->getStartLength();
156 
157  }
158  else if (allMuscles[i]->hasTag("inner left"))
159  {
160  tension = 100.0;
161  kPosition = 300.0;
162  kVelocity = 50.0;
163  controlLength = allMuscles[i]->getStartLength();
164 
165  amplitude = root.get("in_bottom_amp_a", "UTF-8").asDouble();
166  phase = i * bodywaves + root.get("in_left_offset", "UTF-8").asDouble();
167 
168  }
169  else if (allMuscles[i]->hasTag("outer left"))
170  {
171  tension = 50.0;
172  kPosition = 200.0;
173  kVelocity = 100.0;
174  controlLength = allMuscles[i]->getStartLength();
175 
176  amplitude = root.get("out_bottom_amp_a", "UTF-8").asDouble();
177  phase = i * bodywaves + root.get("out_left_offset", "UTF-8").asDouble();
178 
179  //2 * bodyWaves * M_PI * i / (segments) + phaseOffsets[phase]
180 
181  }
182  else if (allMuscles[i]->hasTag("inner right"))
183  {
184  tension = 100.0;
185  kPosition = 300.0;
186  kVelocity = 50.0;
187  controlLength = allMuscles[i]->getStartLength();
188 
189  amplitude = root.get("in_bottom_amp_a", "UTF-8").asDouble();
190  phase = i * bodywaves + root.get("in_right_offset", "UTF-8").asDouble();
191 
192  }
193  else if (allMuscles[i]->hasTag("outer right"))
194  {
195  tension = 50.0;
196  kPosition = 200.0;
197  kVelocity = 100.0;
198  controlLength = allMuscles[i]->getStartLength();
199 
200  amplitude = root.get("out_bottom_amp_a", "UTF-8").asDouble();
201  phase = i * bodywaves + root.get("out_right_offset", "UTF-8").asDouble();
202 
203  //2 * bodyWaves * M_PI * i / (segments) + phaseOffsets[phase]
204 
205  }
206  else
207  {
208  throw std::runtime_error("Missing tags!");
209  }
210 
211  tgImpedanceController* p_ipc = new tgImpedanceController( tension,
212  kPosition,
213  kVelocity);
214 
215  offset = 0.0;
216 
217  tgSineStringControl* pStringControl = new tgSineStringControl(m_controlTime,
218  p_ipc,
219  amplitude,
220  frequency,
221  phase,
222  offset,
223  controlLength);
224 
225 
226  allMuscles[i]->attach(pStringControl);
227  m_sineControllers.push_back(pStringControl);
228  }
229 
230  assert(m_sineControllers.size() == allMuscles.size());
231 
232 }
233 
Contains the definition of class ImpedanceControl. $Id$.
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)
Contains the definition of class AnnealEvolution. Adapting NeuroEvolution to do Simulated Annealing...
Controller for TetraSpineCollisions.
virtual void onSetup(BaseSpineModelLearning &subject)
virtual void onStep(tgModel &model, double dt)
virtual void onSetup(tgModel &model)
virtual void setupWaves(BaseSpineModelLearning &subject)
virtual void onStep(BaseSpineModelLearning &subject, double dt)