42 #include "examples/IROS_2015/hardwareSineWaves/tgSineStringControl.h"
46 #include <json/json.h>
59 std::string resourcePath)
61 if (resourcePath !=
"")
70 controlFilename = controlFilePath + args;
77 initConditions = subject.getSegmentCOM(0);
89 if (m_updateTime >= m_controlTime)
99 std::vector<double> finalConditions = subject.getSegmentCOM(0);
101 const double newX = finalConditions[0];
102 const double newZ = finalConditions[2];
103 const double oldX = initConditions[0];
104 const double oldZ = initConditions[2];
106 const double distanceMoved = sqrt((newX-oldX) * (newX-oldX) +
107 (newZ-oldZ) * (newZ-oldZ));
109 std::cout <<
"Dist travelled " << distanceMoved << std::endl;
114 bool parsingSuccessful = reader.parse( FileHelpers::getFileString(controlFilename.c_str()), root );
115 if ( !parsingSuccessful )
118 std::cout <<
"Failed to parse configuration\n"
119 << reader.getFormattedErrorMessages();
120 throw std::invalid_argument(
"Bad filename for JSON");
123 root[
"scores"] = distanceMoved;
125 std::ofstream payloadLog;
126 payloadLog.open(controlFilename.c_str(),std::ofstream::out);
128 payloadLog << root << std::endl;
130 for(
size_t i = 0; i < m_sineControllers.size(); i++)
132 delete m_sineControllers[i];
134 m_sineControllers.clear();
139 std::vector <tgSpringCableActuator*> allMuscles = subject.getAllMuscles();
144 double controlLength;
154 bool parsingSuccessful = reader.parse( FileHelpers::getFileString(controlFilename.c_str()), root );
155 if ( !parsingSuccessful )
158 std::cout <<
"Failed to parse configuration\n"
159 << reader.getFormattedErrorMessages();
161 throw std::invalid_argument(
"Bad JSON filename");
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);
168 for (std::size_t i = 0; i < allMuscles.size(); i++)
170 if (allMuscles[i]->hasTag(
"inner top"))
175 controlLength = allMuscles[i]->getStartLength();
177 amplitude = root.get(
"in_top_amp_a",
"UTF-8").asDouble();
179 phase = i * bodywaves + root.get(
"in_top_offset",
"UTF-8").asDouble();
183 else if (allMuscles[i]->hasTag(
"outer top"))
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();
195 else if (allMuscles[i]->hasTag(
"inner left"))
200 controlLength = allMuscles[i]->getStartLength();
202 amplitude = root.get(
"in_bottom_amp_a",
"UTF-8").asDouble();
203 phase = i * bodywaves + root.get(
"in_left_offset",
"UTF-8").asDouble();
206 else if (allMuscles[i]->hasTag(
"outer left"))
211 controlLength = allMuscles[i]->getStartLength();
213 amplitude = root.get(
"out_bottom_amp_a",
"UTF-8").asDouble();
214 phase = i * bodywaves + root.get(
"out_left_offset",
"UTF-8").asDouble();
219 else if (allMuscles[i]->hasTag(
"inner right"))
224 controlLength = allMuscles[i]->getStartLength();
226 amplitude = root.get(
"in_bottom_amp_a",
"UTF-8").asDouble();
227 phase = i * bodywaves + root.get(
"in_right_offset",
"UTF-8").asDouble();
230 else if (allMuscles[i]->hasTag(
"outer right"))
235 controlLength = allMuscles[i]->getStartLength();
237 amplitude = root.get(
"out_bottom_amp_a",
"UTF-8").asDouble();
238 phase = i * bodywaves + root.get(
"out_right_offset",
"UTF-8").asDouble();
245 throw std::runtime_error(
"Missing tags!");
263 allMuscles[i]->attach(pStringControl);
264 m_sineControllers.push_back(pStringControl);
267 assert(m_sineControllers.size() == allMuscles.size());
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)
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)