38 #include "examples/learningSpines/tgCPGCableControl.h"
40 #include "dev/dhustigschultz/BigPuppy_SpineOnly_Stats/BaseQuadModelLearning.h"
47 #include "examples/learningSpines/tgCPGCableControl.h"
49 #include "neuralNet/Neural Network v2/neuralNetwork.h"
51 #include <json/json.h>
99 lp, hp, kt, kp, kv, def, cl, lf, hf),
100 freqFeedbackMin(ffMin),
101 freqFeedbackMax(ffMax),
102 ampFeedbackMin(afMin),
103 ampFeedbackMax(afMax),
104 phaseFeedbackMin(pfMin),
105 phaseFeedbackMax(pfMax),
109 theirHipMuscles(thm),
111 theirLegMuscles(tlm),
112 ourAchillesMuscles(oam),
113 theirAchillesMuscles(tam),
114 ourHighMuscles(ohighm),
115 theirHighMuscles(thighm),
117 freqFeedbackMax2(ffMax2)
128 std::string resourcePath) :
136 JSONAchillesHierarchyControl::~JSONAchillesHierarchyControl()
148 bool parsingSuccessful = reader.parse( FileHelpers::getFileString(controlFilename.c_str()), root );
149 if ( !parsingSuccessful )
152 std::cout <<
"Failed to parse configuration\n"
153 << reader.getFormattedErrorMessages();
154 throw std::invalid_argument(
"Bad filename for JSON");
159 Json::Value spineNodeVals = root.get(
"spineNodeVals",
"UTF-8");
160 Json::Value legNodeVals = root.get(
"legNodeVals",
"UTF-8");
161 Json::Value spineEdgeVals = root.get(
"spineEdgeVals",
"UTF-8");
162 Json::Value hipEdgeVals = root.get(
"hipEdgeVals",
"UTF-8");
163 Json::Value legEdgeVals = root.get(
"legEdgeVals",
"UTF-8");
164 Json::Value achillesEdgeVals = root.get(
"achillesEdgeVals",
"UTF-8");
166 std::cout << spineNodeVals << std::endl;
168 spineNodeVals = spineNodeVals.get(
"params",
"UTF-8");
169 legNodeVals = legNodeVals.get(
"params",
"UTF-8");
170 spineEdgeVals = spineEdgeVals.get(
"params",
"UTF-8");
171 hipEdgeVals = hipEdgeVals.get(
"params",
"UTF-8");
172 legEdgeVals = legEdgeVals.get(
"params",
"UTF-8");
173 achillesEdgeVals = achillesEdgeVals.get(
"params",
"UTF-8");
176 array_4D spineEdgeParams =
scaleEdgeActions(spineEdgeVals,m_config.segmentSpan,m_config.theirMuscles,m_config.ourMuscles);
177 array_4D hipEdgeParams =
scaleEdgeActions(hipEdgeVals,m_config.segmentSpan,m_config.theirHipMuscles,m_config.ourHipMuscles);
178 array_4D legEdgeParams =
scaleEdgeActions(legEdgeVals,m_config.segmentSpan,m_config.theirLegMuscles,m_config.ourLegMuscles);
179 array_4D achillesEdgeParams =
scaleEdgeActions(achillesEdgeVals,m_config.segmentSpan,m_config.theirAchillesMuscles,m_config.ourAchillesMuscles);
180 array_2D spineNodeParams = scaleNodeActions(spineNodeVals, m_config.highFreq, m_config.freqFeedbackMax);
181 array_2D legNodeParams = scaleNodeActions(legNodeVals, m_config.highFreq, m_config.freqFeedbackMax);
185 Json::Value highNodeVals = root.get(
"highNodeVals",
"UTF-8");
186 Json::Value highEdgeVals = root.get(
"highEdgeVals",
"UTF-8");
188 highNodeVals = highNodeVals.get(
"params",
"UTF-8");
189 highEdgeVals = highEdgeVals.get(
"params",
"UTF-8");
192 Json::Value highLowEdgeVals = root.get(
"hLowVals",
"UTF-8");
193 highLowEdgeVals = highLowEdgeVals.get(
"params",
"UTF-8");
195 array_4D highEdgeParams =
scaleEdgeActions(highEdgeVals,2,m_config.theirHighMuscles,m_config.ourHighMuscles);
196 array_2D highNodeParams = scaleNodeActions(highNodeVals, m_config.highFreq2, m_config.freqFeedbackMax2);
199 setupHighCPGs(subject, highNodeParams, highEdgeParams);
202 setupCPGs(subject, spineNodeParams, legNodeParams, spineEdgeParams, hipEdgeParams, legEdgeParams, achillesEdgeParams);
205 setupHighLowCouplings(subject, highLowEdgeVals);
207 Json::Value feedbackParams = root.get(
"feedbackVals",
"UTF-8");
208 feedbackParams = feedbackParams.get(
"params",
"UTF-8");
211 m_config.numStates = feedbackParams.get(
"numStates",
"UTF-8").asInt();
212 m_config.numActions = feedbackParams.get(
"numActions",
"UTF-8").asInt();
215 std::string nnFile = controlFilePath + feedbackParams.get(
"neuralFilename",
"UTF-8").asString();
217 nn =
new neuralNetwork(m_config.numStates, m_config.numStates*2, m_config.numActions);
219 nn->loadWeights(nnFile.c_str());
221 initConditions = subject.getSegmentCOM(m_config.segmentNumber);
222 for (
int i = 0; i < initConditions.size(); i++)
224 std::cout << initConditions[i] <<
" ";
226 std::cout << std::endl;
227 #ifdef LOGGING // Conditional compile for data logging
228 m_dataObserver.
onSetup(subject);
231 #if (0) // Conditional Compile for debug info
232 std::cout << *m_pCPGSys << std::endl;
241 std::vector<double> structureCOM = subject.getCOM(m_config.segmentNumber);
243 for(std::size_t i=0; i<3; i++)
245 metrics.push_back(structureCOM[i]);
250 Json::Value prevMetrics = root.get(
"metrics", Json::nullValue);
252 Json::Value subMetrics;
253 subMetrics[
"initial COM x"] = metrics[0];
254 subMetrics[
"initial COM y"] = metrics[1];
255 subMetrics[
"initial COM z"] = metrics[2];
257 prevMetrics.append(subMetrics);
258 root[
"metrics"] = prevMetrics;
261 payloadLog.open(controlFilename.c_str(),ofstream::out);
263 payloadLog << root << std::endl;
271 if (m_updateTime >= m_config.controlTime)
274 std::vector<double> desComs = getFeedback(subject);
277 std::size_t numControllers = subject.getNumberofMuslces() * 3;
279 double descendingCommand = 0.0;
280 std::vector<double> desComs (numControllers, descendingCommand);
284 m_pCPGSys->
update(desComs, m_updateTime);
286 catch (std::runtime_error& e)
293 #ifdef LOGGING // Conditional compile for data logging
294 m_dataObserver.
onStep(subject, m_updateTime);
300 double currentHeight = subject.getSegmentCOM(m_config.segmentNumber)[1];
301 double currentHeightRear = subject.getSegmentCOM(6)[1];
304 if (currentHeight > m_config.maxHeight || currentHeight < m_config.minHeight || currentHeightRear > m_config.maxHeight || currentHeightRear < m_config.minHeight)
308 throw std::runtime_error(
"Height out of range");
319 std::vector<double> finalConditions = subject.getSegmentCOM(m_config.segmentNumber);
321 const double newX = finalConditions[0];
322 const double newZ = finalConditions[2];
323 const double oldX = initConditions[0];
324 const double oldZ = initConditions[2];
326 const double distanceMoved = sqrt((newX-oldX) * (newX-oldX) +
327 (newZ-oldZ) * (newZ-oldZ));
331 scores.push_back(-1.0);
335 scores.push_back(distanceMoved);
340 double totalEnergySpent=0;
344 for(std::size_t i=0; i<tmpStrings.size(); i++)
351 const double previousLength = stringHist.
restLengths[j-1];
352 const double currentLength = stringHist.
restLengths[j];
354 double motorSpeed = (currentLength-previousLength);
357 const double workDone = previousTension * motorSpeed;
358 totalEnergySpent += workDone;
362 scores.push_back(totalEnergySpent);
365 std::vector<double> structureCOM = subject.getCOM(m_config.segmentNumber);
367 for(std::size_t i=0; i<3; i++)
369 metrics.push_back(structureCOM[i]);
372 std::cout <<
"Dist travelled " << scores[0] << std::endl;
377 bool parsingSuccessful = reader.parse( FileHelpers::getFileString(controlFilename.c_str()), root );
378 if ( !parsingSuccessful )
381 std::cout <<
"Failed to parse configuration\n"
382 << reader.getFormattedErrorMessages();
383 throw std::invalid_argument(
"Bad filename for JSON");
386 Json::Value prevScores = root.get(
"scores", Json::nullValue);
387 Json::Value prevMetrics = root.get(
"metrics", Json::nullValue);
389 Json::Value subScores;
390 subScores[
"distance"] = scores[0];
391 subScores[
"energy"] = scores[1];
393 Json::Value subMetrics;
394 subMetrics[
"final COM x"] = metrics[0];
395 subMetrics[
"final COM y"] = metrics[1];
396 subMetrics[
"final COM z"] = metrics[2];
398 prevScores.append(subScores);
399 prevMetrics.append(subMetrics);
401 root[
"scores"] = prevScores;
402 root[
"metrics"] = prevMetrics;
405 payloadLog.open(controlFilename.c_str(),ofstream::out);
407 payloadLog << root << std::endl;
413 for(
size_t i = 0; i < m_spineControllers.size(); i++)
415 delete m_spineControllers[i];
417 m_spineControllers.clear();
419 for(
size_t i = 0; i < m_leftShoulderControllers.size(); i++)
421 delete m_leftShoulderControllers[i];
423 m_leftShoulderControllers.clear();
425 for(
size_t i = 0; i < m_rightShoulderControllers.size(); i++)
427 delete m_rightShoulderControllers[i];
429 m_rightShoulderControllers.clear();
431 for(
size_t i = 0; i < m_leftHipControllers.size(); i++)
433 delete m_leftHipControllers[i];
435 m_leftHipControllers.clear();
437 for(
size_t i = 0; i < m_rightHipControllers.size(); i++)
439 delete m_rightHipControllers[i];
441 m_rightHipControllers.clear();
443 for(
size_t i = 0; i < m_leftForelegControllers.size(); i++)
445 delete m_leftForelegControllers[i];
447 m_leftForelegControllers.clear();
449 for(
size_t i = 0; i < m_rightForelegControllers.size(); i++)
451 delete m_rightForelegControllers[i];
453 m_rightForelegControllers.clear();
455 for(
size_t i = 0; i < m_leftHindlegControllers.size(); i++)
457 delete m_leftHindlegControllers[i];
459 m_leftHindlegControllers.clear();
461 for(
size_t i = 0; i < m_rightHindlegControllers.size(); i++)
463 delete m_rightHindlegControllers[i];
465 m_rightHindlegControllers.clear();
468 for(
size_t i = 0; i < m_leftFrontAchillesControllers.size(); i++)
470 delete m_leftFrontAchillesControllers[i];
472 m_leftFrontAchillesControllers.clear();
474 for(
size_t i = 0; i < m_rightFrontAchillesControllers.size(); i++)
476 delete m_rightFrontAchillesControllers[i];
478 m_rightFrontAchillesControllers.clear();
480 for(
size_t i = 0; i < m_leftRearAchillesControllers.size(); i++)
482 delete m_leftRearAchillesControllers[i];
484 m_leftRearAchillesControllers.clear();
486 for(
size_t i = 0; i < m_rightRearAchillesControllers.size(); i++)
488 delete m_rightRearAchillesControllers[i];
490 m_rightRearAchillesControllers.clear();
493 for(
size_t i = 0; i < m_highControllers.size(); i++)
495 delete m_highControllers[i];
497 m_highControllers.clear();
508 void JSONAchillesHierarchyControl::setupCPGs(
BaseQuadModelLearning& subject, array_2D spineNodeActions, array_2D legNodeActions, array_4D spineEdgeActions, array_4D hipEdgeActions, array_4D legEdgeActions, array_4D achillesEdgeActions)
528 std::vector <tgSpringCableActuator*> leftFrontAchillesMuscles = subject.
find<
tgSpringCableActuator> (
"left_front_achilles_tendon");
529 std::vector <tgSpringCableActuator*> rightFrontAchillesMuscles = subject.
find<
tgSpringCableActuator> (
"right_front_achilles_tendon");
531 std::vector <tgSpringCableActuator*> leftRearAchillesMuscles = subject.
find<
tgSpringCableActuator> (
"left_rear_achilles_tendon");
532 std::vector <tgSpringCableActuator*> rightRearAchillesMuscles = subject.
find<
tgSpringCableActuator> (
"right_rear_achilles_tendon");
534 CPGEquationsFB& m_CPGFBSys = *(tgCast::cast<CPGEquations, CPGEquationsFB>(m_pCPGSys));
540 for (std::size_t i = 0; i < spineMuscles.size(); i++)
546 spineMuscles[i]->attach(pStringControl);
551 m_spineControllers.push_back(pStringControl);
555 for (std::size_t i = 0; i < m_spineControllers.size(); i++)
558 assert(pStringInfo != NULL);
565 if (m_config.useDefault)
567 pStringInfo->setupControl(*p_ipc);
571 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
580 for (std::size_t i = 0; i < leftShoulderMuscles.size(); i++)
586 leftShoulderMuscles[i]->attach(pStringControl);
591 m_leftShoulderControllers.push_back(pStringControl);
595 for (std::size_t i = 0; i < m_leftShoulderControllers.size(); i++)
598 assert(pStringInfo != NULL);
599 pStringInfo->
setConnectivity(m_leftShoulderControllers, hipEdgeActions);
605 if (m_config.useDefault)
607 pStringInfo->setupControl(*p_ipc);
611 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
616 for (std::size_t i = 0; i < rightShoulderMuscles.size(); i++)
622 rightShoulderMuscles[i]->attach(pStringControl);
627 m_rightShoulderControllers.push_back(pStringControl);
631 for (std::size_t i = 0; i < m_rightShoulderControllers.size(); i++)
634 assert(pStringInfo != NULL);
635 pStringInfo->
setConnectivity(m_rightShoulderControllers, hipEdgeActions);
641 if (m_config.useDefault)
643 pStringInfo->setupControl(*p_ipc);
647 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
652 for (std::size_t i = 0; i < leftHipMuscles.size(); i++)
658 leftHipMuscles[i]->attach(pStringControl);
663 m_leftHipControllers.push_back(pStringControl);
667 for (std::size_t i = 0; i < m_leftHipControllers.size(); i++)
670 assert(pStringInfo != NULL);
677 if (m_config.useDefault)
679 pStringInfo->setupControl(*p_ipc);
683 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
688 for (std::size_t i = 0; i < rightHipMuscles.size(); i++)
694 leftHipMuscles[i]->attach(pStringControl);
699 m_rightHipControllers.push_back(pStringControl);
703 for (std::size_t i = 0; i < m_rightHipControllers.size(); i++)
706 assert(pStringInfo != NULL);
713 if (m_config.useDefault)
715 pStringInfo->setupControl(*p_ipc);
719 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
728 for (std::size_t i = 0; i < leftForelegMuscles.size(); i++)
734 leftForelegMuscles[i]->attach(pStringControl);
739 m_leftForelegControllers.push_back(pStringControl);
743 for (std::size_t i = 0; i < m_leftForelegControllers.size(); i++)
746 assert(pStringInfo != NULL);
747 pStringInfo->
setConnectivity(m_leftForelegControllers, legEdgeActions);
753 if (m_config.useDefault)
755 pStringInfo->setupControl(*p_ipc);
759 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
764 for (std::size_t i = 0; i < rightForelegMuscles.size(); i++)
770 rightForelegMuscles[i]->attach(pStringControl);
775 m_rightForelegControllers.push_back(pStringControl);
779 for (std::size_t i = 0; i < m_rightForelegControllers.size(); i++)
782 assert(pStringInfo != NULL);
783 pStringInfo->
setConnectivity(m_rightForelegControllers, legEdgeActions);
789 if (m_config.useDefault)
791 pStringInfo->setupControl(*p_ipc);
795 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
800 for (std::size_t i = 0; i < leftHindlegMuscles.size(); i++)
806 leftHindlegMuscles[i]->attach(pStringControl);
811 m_leftHindlegControllers.push_back(pStringControl);
815 for (std::size_t i = 0; i < m_leftHindlegControllers.size(); i++)
818 assert(pStringInfo != NULL);
819 pStringInfo->
setConnectivity(m_leftHindlegControllers, legEdgeActions);
825 if (m_config.useDefault)
827 pStringInfo->setupControl(*p_ipc);
831 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
836 for (std::size_t i = 0; i < rightHindlegMuscles.size(); i++)
842 rightHindlegMuscles[i]->attach(pStringControl);
847 m_rightHindlegControllers.push_back(pStringControl);
851 for (std::size_t i = 0; i < m_rightHindlegControllers.size(); i++)
854 assert(pStringInfo != NULL);
855 pStringInfo->
setConnectivity(m_rightHindlegControllers, legEdgeActions);
861 if (m_config.useDefault)
863 pStringInfo->setupControl(*p_ipc);
867 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
876 for (std::size_t i = 0; i < leftFrontAchillesMuscles.size(); i++)
882 leftFrontAchillesMuscles[i]->attach(pStringControl);
887 m_leftFrontAchillesControllers.push_back(pStringControl);
891 for (std::size_t i = 0; i < m_leftFrontAchillesControllers.size(); i++)
894 assert(pStringInfo != NULL);
895 pStringInfo->
setConnectivity(m_leftFrontAchillesControllers, achillesEdgeActions);
901 if (m_config.useDefault)
903 pStringInfo->setupControl(*p_ipc);
907 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
912 for (std::size_t i = 0; i < rightFrontAchillesMuscles.size(); i++)
918 rightFrontAchillesMuscles[i]->attach(pStringControl);
923 m_rightFrontAchillesControllers.push_back(pStringControl);
927 for (std::size_t i = 0; i < m_rightFrontAchillesControllers.size(); i++)
930 assert(pStringInfo != NULL);
931 pStringInfo->
setConnectivity(m_rightFrontAchillesControllers, achillesEdgeActions);
937 if (m_config.useDefault)
939 pStringInfo->setupControl(*p_ipc);
943 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
948 for (std::size_t i = 0; i < leftRearAchillesMuscles.size(); i++)
954 leftRearAchillesMuscles[i]->attach(pStringControl);
959 m_leftRearAchillesControllers.push_back(pStringControl);
963 for (std::size_t i = 0; i < m_leftRearAchillesControllers.size(); i++)
966 assert(pStringInfo != NULL);
967 pStringInfo->
setConnectivity(m_leftRearAchillesControllers, achillesEdgeActions);
973 if (m_config.useDefault)
975 pStringInfo->setupControl(*p_ipc);
979 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
984 for (std::size_t i = 0; i < rightRearAchillesMuscles.size(); i++)
990 rightRearAchillesMuscles[i]->attach(pStringControl);
995 m_rightRearAchillesControllers.push_back(pStringControl);
999 for (std::size_t i = 0; i < m_rightRearAchillesControllers.size(); i++)
1002 assert(pStringInfo != NULL);
1003 pStringInfo->
setConnectivity(m_rightRearAchillesControllers, achillesEdgeActions);
1008 m_config.kVelocity);
1009 if (m_config.useDefault)
1011 pStringInfo->setupControl(*p_ipc);
1015 pStringInfo->setupControl(*p_ipc, m_config.controlLength);
1023 void JSONAchillesHierarchyControl::setupHighLowCouplings(
BaseQuadModelLearning& subject, Json::Value highLowEdgeActions)
1029 double lowerLimit = m_config.lowPhase;
1030 double upperLimit = m_config.highPhase;
1031 double range = upperLimit - lowerLimit;
1033 CPGEquationsFB& m_CPGFBSys = *(tgCast::cast<CPGEquations, CPGEquationsFB>(m_pCPGSys));
1035 Json::Value::iterator edgeIt = highLowEdgeActions.end();
1044 std::vector<int> spineConnectivityList;
1045 std::vector<double> spineWeights;
1046 std::vector<double> spinePhases;
1048 std::vector<int> leftShoulderConnectivityList;
1049 std::vector<double> leftShoulderWeights;
1050 std::vector<double> leftShoulderPhases;
1052 std::vector<int> leftForelegConnectivityList;
1053 std::vector<double> leftForelegWeights;
1054 std::vector<double> leftForelegPhases;
1056 std::vector<int> leftFrontAchillesConnectivityList;
1057 std::vector<double> leftFrontAchillesWeights;
1058 std::vector<double> leftFrontAchillesPhases;
1060 std::vector<int> rightShoulderConnectivityList;
1061 std::vector<double> rightShoulderWeights;
1062 std::vector<double> rightShoulderPhases;
1064 std::vector<int> rightForelegConnectivityList;
1065 std::vector<double> rightForelegWeights;
1066 std::vector<double> rightForelegPhases;
1068 std::vector<int> rightFrontAchillesConnectivityList;
1069 std::vector<double> rightFrontAchillesWeights;
1070 std::vector<double> rightFrontAchillesPhases;
1072 std::vector<int> leftHipConnectivityList;
1073 std::vector<double> leftHipWeights;
1074 std::vector<double> leftHipPhases;
1076 std::vector<int> leftHindlegConnectivityList;
1077 std::vector<double> leftHindlegWeights;
1078 std::vector<double> leftHindlegPhases;
1080 std::vector<int> leftRearAchillesConnectivityList;
1081 std::vector<double> leftRearAchillesWeights;
1082 std::vector<double> leftRearAchillesPhases;
1084 std::vector<int> rightHipConnectivityList;
1085 std::vector<double> rightHipWeights;
1086 std::vector<double> rightHipPhases;
1088 std::vector<int> rightHindlegConnectivityList;
1089 std::vector<double> rightHindlegWeights;
1090 std::vector<double> rightHindlegPhases;
1092 std::vector<int> rightRearAchillesConnectivityList;
1093 std::vector<double> rightRearAchillesWeights;
1094 std::vector<double> rightRearAchillesPhases;
1096 Json::Value param = *edgeIt;
1097 assert(param.size() == 2);
1101 for (
size_t i = 0; i < m_spineControllers.size(); i++)
1104 int nodeNum = m_spineControllers[i]->getNodeNumber();
1107 spineConnectivityList.push_back(nodeNum);
1108 spineWeights.push_back(param[0].asDouble());
1109 spinePhases.push_back(param[1].asDouble() * (range) + lowerLimit);
1111 m_CPGFBSys.defineConnections(b, spineConnectivityList, spineWeights, spinePhases);
1116 for (
size_t i = 0; i < m_leftShoulderControllers.size(); i++)
1119 int nodeNum = m_leftShoulderControllers[i]->getNodeNumber();
1122 leftShoulderConnectivityList.push_back(nodeNum);
1123 leftShoulderWeights.push_back(param[0].asDouble());
1124 leftShoulderPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
1126 m_CPGFBSys.defineConnections(b, leftShoulderConnectivityList, leftShoulderWeights, leftShoulderPhases);
1130 for (
size_t i = 0; i < m_leftForelegControllers.size(); i++)
1133 int nodeNum = m_leftForelegControllers[i]->getNodeNumber();
1136 leftForelegConnectivityList.push_back(nodeNum);
1137 leftForelegWeights.push_back(param[0].asDouble());
1138 leftForelegPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
1140 m_CPGFBSys.defineConnections(b, leftForelegConnectivityList, leftForelegWeights, leftForelegPhases);
1144 for (
size_t i = 0; i < m_leftFrontAchillesControllers.size(); i++)
1147 int nodeNum = m_leftFrontAchillesControllers[i]->getNodeNumber();
1150 leftFrontAchillesConnectivityList.push_back(nodeNum);
1151 leftFrontAchillesWeights.push_back(param[0].asDouble());
1152 leftFrontAchillesPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
1154 m_CPGFBSys.defineConnections(b, leftFrontAchillesConnectivityList, leftFrontAchillesWeights, leftFrontAchillesPhases);
1161 for (
size_t i = 0; i < m_rightShoulderControllers.size(); i++)
1164 int nodeNum = m_rightShoulderControllers[i]->getNodeNumber();
1167 rightShoulderConnectivityList.push_back(nodeNum);
1168 rightShoulderWeights.push_back(param[0].asDouble());
1169 rightShoulderPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
1171 m_CPGFBSys.defineConnections(b, rightShoulderConnectivityList, rightShoulderWeights, rightShoulderPhases);
1175 for (
size_t i = 0; i < m_rightForelegControllers.size(); i++)
1178 int nodeNum = m_rightForelegControllers[i]->getNodeNumber();
1181 rightForelegConnectivityList.push_back(nodeNum);
1182 rightForelegWeights.push_back(param[0].asDouble());
1183 rightForelegPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
1185 m_CPGFBSys.defineConnections(b, rightForelegConnectivityList, rightForelegWeights, rightForelegPhases);
1189 for (
size_t i = 0; i < m_rightFrontAchillesControllers.size(); i++)
1192 int nodeNum = m_rightFrontAchillesControllers[i]->getNodeNumber();
1195 rightFrontAchillesConnectivityList.push_back(nodeNum);
1196 rightFrontAchillesWeights.push_back(param[0].asDouble());
1197 rightFrontAchillesPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
1199 m_CPGFBSys.defineConnections(b, rightFrontAchillesConnectivityList, rightFrontAchillesWeights, rightFrontAchillesPhases);
1206 for (
size_t i = 0; i < m_leftHipControllers.size(); i++)
1209 int nodeNum = m_leftHipControllers[i]->getNodeNumber();
1212 leftHipConnectivityList.push_back(nodeNum);
1213 leftHipWeights.push_back(param[0].asDouble());
1214 leftHipPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
1216 m_CPGFBSys.defineConnections(b, leftHipConnectivityList, leftHipWeights, leftHipPhases);
1220 for (
size_t i = 0; i < m_leftHindlegControllers.size(); i++)
1223 int nodeNum = m_leftHindlegControllers[i]->getNodeNumber();
1226 leftHindlegConnectivityList.push_back(nodeNum);
1227 leftHindlegWeights.push_back(param[0].asDouble());
1228 leftHindlegPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
1230 m_CPGFBSys.defineConnections(b, leftHindlegConnectivityList, leftHindlegWeights, leftHindlegPhases);
1234 for (
size_t i = 0; i < m_leftRearAchillesControllers.size(); i++)
1237 int nodeNum = m_leftRearAchillesControllers[i]->getNodeNumber();
1240 leftRearAchillesConnectivityList.push_back(nodeNum);
1241 leftRearAchillesWeights.push_back(param[0].asDouble());
1242 leftRearAchillesPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
1244 m_CPGFBSys.defineConnections(b, leftRearAchillesConnectivityList, leftRearAchillesWeights, leftRearAchillesPhases);
1251 for (
size_t i = 0; i < m_rightHipControllers.size(); i++)
1254 int nodeNum = m_rightHipControllers[i]->getNodeNumber();
1257 rightHipConnectivityList.push_back(nodeNum);
1258 rightHipWeights.push_back(param[0].asDouble());
1259 rightHipPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
1261 m_CPGFBSys.defineConnections(b, rightHipConnectivityList, rightHipWeights, rightHipPhases);
1266 for (
size_t i = 0; i < m_rightHindlegControllers.size(); i++)
1269 int nodeNum = m_rightHindlegControllers[i]->getNodeNumber();
1272 rightHindlegConnectivityList.push_back(nodeNum);
1273 rightHindlegWeights.push_back(param[0].asDouble());
1274 rightHindlegPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
1276 m_CPGFBSys.defineConnections(b, rightHindlegConnectivityList, rightHindlegWeights, rightHindlegPhases);
1281 for (
size_t i = 0; i < m_rightRearAchillesControllers.size(); i++)
1284 int nodeNum = m_rightRearAchillesControllers[i]->getNodeNumber();
1287 rightRearAchillesConnectivityList.push_back(nodeNum);
1288 rightRearAchillesWeights.push_back(param[0].asDouble());
1289 rightRearAchillesPhases.push_back(param[1].asDouble() * (range) + lowerLimit);
1291 m_CPGFBSys.defineConnections(b, rightRearAchillesConnectivityList, rightRearAchillesWeights, rightRearAchillesPhases);
1294 assert(highLowEdgeActions.begin() == edgeIt);
1297 void JSONAchillesHierarchyControl::setupHighCPGs(
BaseQuadModelLearning& subject, array_2D highNodeActions, array_4D highEdgeActions)
1299 CPGEquationsFB& m_CPGFBSys = *(tgCast::cast<CPGEquations, CPGEquationsFB>(m_pCPGSys));
1302 for (std::size_t b = 0; b < 5; b++)
1316 m_highControllers.push_back(pStringControl);
1320 for (std::size_t i = 0; i < m_highControllers.size(); i++)
1323 assert(pStringInfo != NULL);
1329 array_2D JSONAchillesHierarchyControl::scaleNodeActions (Json::Value actions,
double highFreq,
double freqFeedbackMax)
1331 std::size_t numControllers = actions.size();
1332 std::size_t numActions = actions[0].size();
1334 array_2D nodeActions(boost::extents[numControllers][numActions]);
1336 array_2D limits(boost::extents[2][numActions]);
1339 assert(numActions == 5);
1341 limits[0][0] = m_config.lowFreq;
1342 limits[1][0] = highFreq;
1343 limits[0][1] = m_config.lowAmp;
1344 limits[1][1] = m_config.highAmp;
1345 limits[0][2] = m_config.freqFeedbackMin;
1346 limits[1][2] = freqFeedbackMax;
1347 limits[0][3] = m_config.ampFeedbackMin;
1348 limits[1][3] = m_config.ampFeedbackMax;
1349 limits[0][4] = m_config.phaseFeedbackMin;
1350 limits[1][4] = m_config.phaseFeedbackMax;
1352 Json::Value::iterator nodeIt = actions.begin();
1355 for( std::size_t i = 0; i < numControllers; i++)
1357 Json::Value nodeParam = *nodeIt;
1358 for( std::size_t j = 0; j < numActions; j++)
1360 nodeActions[i][j] = ( (nodeParam.get(j, 0.0)).asDouble() *
1361 (limits[1][j] - limits[0][j])) + limits[0][j];
1370 (Json::Value edgeParam,
int segmentSpan,
int theirMuscles,
int ourMuscles)
1372 assert(edgeParam[0].size() == 2);
1374 double lowerLimit = m_config.lowPhase;
1375 double upperLimit = m_config.highPhase;
1376 double range = upperLimit - lowerLimit;
1378 array_4D actionList(boost::extents[segmentSpan][theirMuscles][ourMuscles][m_config.params]);
1391 Json::Value::iterator edgeIt = edgeParam.end();
1395 while (i < segmentSpan)
1397 while(j < theirMuscles)
1399 while(k < ourMuscles)
1401 if (edgeIt == edgeParam.begin())
1403 std::cout <<
"ran out before table populated!"
1410 if (i == 1 && j == k)
1418 Json::Value edgeParam = *edgeIt;
1419 assert(edgeParam.size() == 2);
1421 actionList[i][j][k][0] = edgeParam[0].asDouble();
1424 actionList[i][j][k][1] = edgeParam[1].asDouble() *
1425 (range) + lowerLimit;
1441 std::cout<<
"Params used: " << count << std::endl;
1443 assert(edgeParam.begin() == edgeIt);
1451 std::vector<double> feedback;
1455 double *inputs =
new double[m_config.numStates];
1457 std::size_t n = allCables.size();
1458 for(std::size_t i = 0; i != n; i++)
1460 std::vector< std::vector<double> > actions;
1463 std::vector<double > state = getCableState(cable);
1466 for (std::size_t i = 0; i < state.size(); i++)
1468 inputs[i]=state[i] / 2.0 + 0.5;
1471 double *output = nn->feedForwardPattern(inputs);
1472 vector<double> tmpAct;
1473 for(
int j=0;j<m_config.numActions;j++)
1475 tmpAct.push_back(output[j]);
1477 actions.push_back(tmpAct);
1479 std::vector<double> cableFeedback = transformFeedbackActions(actions);
1481 feedback.insert(feedback.end(), cableFeedback.begin(), cableFeedback.end());
1485 std::size_t n2 = m_highControllers.size();
1486 for (
size_t i = 0; i != n2; i++)
1488 std::vector< std::vector<double> > actions;
1491 for (std::size_t i = 0; i < m_config.numStates; i++)
1496 double *output = nn->feedForwardPattern(inputs);
1497 vector<double> tmpAct;
1498 for(
int j=0;j<m_config.numActions;j++)
1500 tmpAct.push_back(output[j]);
1502 actions.push_back(tmpAct);
1504 std::vector<double> cableFeedback = transformFeedbackActions(actions);
1506 feedback.insert(feedback.end(), cableFeedback.begin(), cableFeedback.end());
1516 std::vector<double> state;
1522 const double maxTension = cable.
getConfig().maxTens;
1523 state.push_back((cable.
getTension() - maxTension / 2.0) / maxTension);
1528 std::vector<double> JSONAchillesHierarchyControl::transformFeedbackActions(std::vector< std::vector<double> >& actions)
1531 std::vector<double> feedback;
1534 const std::size_t numControllers = 1;
1535 const std::size_t numActions = m_config.numActions;
1537 assert( actions.size() == numControllers);
1538 assert( actions[0].size() == numActions);
1541 for( std::size_t i = 0; i < numControllers; i++)
1543 for( std::size_t j = 0; j < numActions; j++)
1545 feedback.push_back(actions[i][j] * 2.0 - 1.0);
Contains the definition of class ImpedanceControl. $Id$.
virtual void onTeardown(BaseQuadModelLearning &subject)
JSONAchillesHierarchyControl(JSONAchillesHierarchyControl::Config config, std::string args, std::string resourcePath="")
void update(std::vector< double > &descCom, double dt)
std::deque< double > tensionHistory
virtual const double getTension() const
virtual const double getStartLength() const
void setConnectivity(const std::vector< tgCPGActuatorControl * > &allStrings, array_4D edgeParams)
virtual void onStep(BaseQuadModelLearning &subject, double dt)
std::deque< double > restLengths
Definition of the tgCPGStringControl observer class.
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...
Contains the definition of class tgBasicActuator.
virtual array_4D scaleEdgeActions(Json::Value actions, int segmentSpan, int theirMuscles, int ourMuscles)
const Config & getConfig() const
std::vector< T * > find(const tgTagSearch &tagSearch)
virtual void onStep(tgModel &model, double dt)
A controller for the template class BaseQuadModelLearning Implementing a CPG hierachy for MountainGoa...
Definition of class CPGEquationsFB.
virtual void onSetup(tgModel &model)
Config(int ss, int tm, int om, int param, int segnum=6, double ct=0.1, double la=0, double ha=30, double lp=-1 *M_PI, double hp=M_PI, double kt=0.0, double kp=1000.0, double kv=100.0, bool def=true, double cl=10.0, double lf=0.0, double hf=30.0, double ffMin=0.0, double ffMax=0.0, double afMin=0.0, double afMax=0.0, double pfMin=0.0, double pfMax=0.0, double maxH=60.0, double minH=1.0, int ohm=10, int thm=10, int olm=10, int tlm=10, int oam=2, int tam=2, int ohighm=5, int thighm=5, double hf2=20.0, double ffMax2=0.0)
virtual const double getCurrentLength() const
virtual void onSetup(BaseQuadModelLearning &subject)
void notifyStep(double dt)
void assignNodeNumberFB(CPGEquationsFB &CPGSys, array_2D nodeParams)