NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
htSpineSine.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 "htSpineSine.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 
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 offset;
112 
113  Json::Value root; // will contains the root value after parsing.
114  Json::Reader reader;
115 
116  bool parsingSuccessful = reader.parse( FileHelpers::getFileString("controlVars.json"), root );
117  if ( !parsingSuccessful )
118  {
119  // report to the user the failure and their locations in the document.
120  std::cout << "Failed to parse configuration\n"
121  << reader.getFormattedErrorMessages();
123  throw std::invalid_argument("Bad JSON filename");
124  }
125 
126  m_controlTime = root.get("updateFrequency", "UTF-8").asDouble();
127  double frequency = root.get("cpg_frequency", "UTF-8").asDouble();
128 
129  for (std::size_t i = 0; i < allMuscles.size(); i++)
130  {
131  if (allMuscles[i]->hasTag("inner top"))
132  {
133  tension = 2400.0;
134  kPosition = 500.0;
135 
136  //controlLength = allMuscles[i]->getStartLength();
137 
138  if (allMuscles[i]->hasTag("seg1"))
139  {
140  amplitude = root.get("in_top_amp_a", "UTF-8").asDouble();
141  phase = root.get("front_offset", "UTF-8").asDouble();
142  controlLength = 19.0;
143  kVelocity = 150;
144  }
145  else if(allMuscles[i]->hasTag("seg2"))
146  {
147  amplitude = root.get("in_top_amp_b", "UTF-8").asDouble();
148  phase = root.get("back_offset", "UTF-8").asDouble();
149  controlLength = 18.0;
150  kVelocity = 150.0;
151  }
152  else
153  {
154  throw std::runtime_error("Missing tags!");
155  }
156  }
157  else if (allMuscles[i]->hasTag("outer top"))
158  {
159  tension = 1000.0;
160  kPosition = 500.0;
161  kVelocity = 150.0;
162 
163  if (allMuscles[i]->hasTag("seg1"))
164  {
165  amplitude = root.get("out_top_amp_a", "UTF-8").asDouble();
166  phase = root.get("front_offset", "UTF-8").asDouble();
167 
168  controlLength = 18.5;
169  }
170  else if(allMuscles[i]->hasTag("seg2"))
171  {
172  amplitude = root.get("out_top_amp_b", "UTF-8").asDouble();
173  phase = root.get("back_offset", "UTF-8").asDouble();
174  controlLength = 19.8;
175  }
176  else
177  {
178  throw std::runtime_error("Missing tags!");
179  }
180  }
181  else if (allMuscles[i]->hasTag("inner"))
182  {
183  tension = 1000.0;
184  kPosition = 300.0;
185  kVelocity = 100.0;
186  //controlLength = allMuscles[i]->getStartLength();
187 
188  if (allMuscles[i]->hasTag("seg1"))
189  {
190  amplitude = root.get("in_bottom_amp_a", "UTF-8").asDouble();
191  phase = root.get("front_offset", "UTF-8").asDouble();
192  controlLength = 21.5;
193  }
194  else if(allMuscles[i]->hasTag("seg2"))
195  {
196  amplitude = root.get("in_bottom_amp_b", "UTF-8").asDouble();
197  phase = root.get("back_offset", "UTF-8").asDouble();
198  controlLength = 22.5;
199  }
200  else
201  {
202  throw std::runtime_error("Missing tags!");
203  }
204  }
205  else if (allMuscles[i]->hasTag("outer"))
206  {
207  tension = 500.0;
208  kPosition = 100.0;
209  kVelocity = 100.0;
210  controlLength = 19.0;
211  if (allMuscles[i]->hasTag("seg1"))
212  {
213  amplitude = root.get("out_bottom_amp_a", "UTF-8").asDouble();
214  phase = root.get("front_offset", "UTF-8").asDouble();
215  }
216  else if(allMuscles[i]->hasTag("seg2"))
217  {
218  amplitude = root.get("out_bottom_amp_b", "UTF-8").asDouble();
219  phase = root.get("back_offset", "UTF-8").asDouble();
220  }
221  else
222  {
223  throw std::runtime_error("Missing tags!");
224  }
225  }
226  else
227  {
228  throw std::runtime_error("Missing tags!");
229  }
230 
231  tgImpedanceController* p_ipc = new tgImpedanceController( tension,
232  kPosition,
233  kVelocity);
234 
235  // In Won's convention
236 
237  offset = -amplitude;
238 
239  tgSineStringControl* pStringControl = new tgSineStringControl(m_controlTime,
240  p_ipc,
241  amplitude,
242  frequency,
243  phase,
244  offset,
245  controlLength);
246 
247 
248  allMuscles[i]->attach(pStringControl);
249  m_sineControllers.push_back(pStringControl);
250  }
251 
252  assert(m_sineControllers.size() == allMuscles.size());
253 
254 }
255 
Contains the definition of class ImpedanceControl. $Id$.
virtual void setupWaves(BaseSpineModelLearning &subject)
virtual void onTeardown(BaseSpineModelLearning &subject)
Definition: htSpineSine.cpp:90
Controller for TetraSpineLearningModel.
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.
Contains the definition of class AnnealEvolution. Adapting NeuroEvolution to do Simulated Annealing...
virtual void onStep(tgModel &model, double dt)
virtual void onSetup(BaseSpineModelLearning &subject)
Definition: htSpineSine.cpp:63
virtual void onSetup(tgModel &model)
virtual void onStep(BaseSpineModelLearning &subject, double dt)
Definition: htSpineSine.cpp:76