29 #include "boost/array.hpp"
30 #include "boost/numeric/odeint.hpp"
33 #include "LinearMath/btQuickprof.h"
40 using namespace boost::numeric::odeint;
42 typedef std::vector<double > cpgVars_type;
44 CPGEquations::CPGEquations(
int maxSteps) :
49 CPGEquations::CPGEquations(std::vector<CPGNode*>& newNodeList,
int maxSteps) :
50 nodeList(newNodeList),
57 CPGEquations::~CPGEquations()
59 for (std::size_t i = 0; i < nodeList.size(); i++)
68 int CPGEquations::addNode(std::vector<double>& newParams)
70 int index = nodeList.size();
72 nodeList.push_back(newNode);
77 void CPGEquations::defineConnections (
int nodeIndex,
78 std::vector<int> connections,
79 std::vector<double> newWeights,
80 std::vector<double> newPhaseOffsets)
82 assert(connections.size() == newWeights.size());
83 assert(connections.size() == newPhaseOffsets.size());
84 assert(nodeList[nodeIndex] != NULL);
86 for(
int i = 0; i != connections.size(); i++){
87 nodeList[nodeIndex]->addCoupling(nodeList[connections[i]], newWeights[i], newPhaseOffsets[i]);
91 const double CPGEquations::operator[](
const std::size_t i)
const
94 BT_PROFILE(
"CPGEquations::[]");
95 #endif //BT_NO_PROFILE
97 if (i >= nodeList.size())
100 throw std::invalid_argument(
"Node index out of bounds");
104 nodeValue = (*nodeList[i]).nodeValue;
110 std::vector<double>& CPGEquations::getXVars() {
111 #ifndef BT_NO_PROFILE
112 BT_PROFILE(
"CPGEquations::getXVars");
113 #endif //BT_NO_PROFILE
116 for (
int i = 0; i != nodeList.size(); i++){
117 XVars.push_back(nodeList[i]->phiValue);
118 XVars.push_back(nodeList[i]->rValue);
119 XVars.push_back(nodeList[i]->rDotValue);
125 std::vector<double>& CPGEquations::getDXVars() {
128 for (
int i = 0; i != nodeList.size(); i++){
129 DXVars.push_back(nodeList[i]->phiDotValue);
130 DXVars.push_back(nodeList[i]->rDotValue);
131 DXVars.push_back(nodeList[i]->rDoubleDotValue);
137 void CPGEquations::updateNodes(std::vector<double>& descCom)
139 #ifndef BT_NO_PROFILE
140 BT_PROFILE(
"CPGEquations::updateNodes");
141 #endif //BT_NO_PROFILE
142 for(
int i = 0; i != nodeList.size(); i++){
143 nodeList[i]->updateDTs(descCom[i]);
147 void CPGEquations::updateNodeData(std::vector<double> newXVals)
149 #ifndef BT_NO_PROFILE
150 BT_PROFILE(
"CPGEquations::updateNodeData");
151 #endif //BT_NO_PROFILE
152 assert(newXVals.size()==3*nodeList.size());
154 for(
int i = 0; i!=nodeList.size(); i++){
155 nodeList[i]->updateNodeValues(newXVals[3*i], newXVals[3*i+1], newXVals[3*i+2]);
176 #ifndef BT_NO_PROFILE
177 BT_PROFILE(
"CPGEquations::integrate_function");
178 #endif //BT_NO_PROFILE
179 theseCPGs->updateNodeData(x);
180 theseCPGs->updateNodes(descCom);
185 std::vector<double> dXVars = theseCPGs->getDXVars();
190 for(std::size_t i = 0; i != x.size(); i++){
195 theseCPGs->countStep();
201 std::vector<double> descCom;
221 theseCPGs->updateNodeData(x);
222 #if (0) // Suppress output
223 std::cout << t <<
'\t' << (*theseCPGs)[0] <<
'\t' << (*theseCPGs)[1] <<
'\t' << (*theseCPGs)[2] << std::endl;
233 #ifndef BT_NO_PROFILE
234 BT_PROFILE(
"CPGEquations::update");
235 #endif //BT_NO_PROFILE
248 std::vector<double>& xVars = getXVars();
255 if (numSteps > m_maxSteps)
257 std::cout <<
"Ending trial due to inefficient equations " << numSteps << std::endl;
258 throw std::runtime_error(
"Inefficient CPG Parameters");
262 std::cout << dt <<
'\t' << nodeList[0]->nodeValue <<
263 '\t' << nodeList[1]->nodeValue <<
264 '\t' << nodeList[2]->nodeValue << std::endl;
269 std::string CPGEquations::toString(
const std::string& prefix)
const
272 std::ostringstream os;
273 os << prefix <<
"CPGEquations(" << std::endl;
275 os << prefix << p <<
"Nodes:" << std::endl;
276 for(
int i = 0; i < nodeList.size(); i++) {
277 os << prefix << p << p << *(nodeList[i]) << std::endl;
280 os << prefix <<
")" << std::endl;
void update(std::vector< double > &descCom, double dt)
void operator()(const cpgVars_type &x, cpgVars_type &dxdt, double t)
void operator()(const cpgVars_type &x, const double t)
Definition of class CPGEquations.