NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
CPGEquations.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 
27 #include "CPGEquations.h"
28 
29 #include "boost/array.hpp"
30 #include "boost/numeric/odeint.hpp"
31 
32 using namespace boost::numeric::odeint;
33 
34 typedef std::vector<double > cpgVars_type;
35 
36 CPGEquations::CPGEquations() :
37 stepSize(0.1)
38  {}
39 CPGEquations::CPGEquations(std::vector<CPGNode*> newNodeList) :
40 nodeList(newNodeList),
41 stepSize(0.1) //TODO: specify as a parameter somewhere
42 {
43 }
44 
45 CPGEquations::~CPGEquations()
46 {
47  for (std::size_t i = 0; i < nodeList.size(); i++)
48  {
49  delete nodeList[i];
50  }
51  nodeList.clear();
52 }
53 
54 // Params needs size 7 to fill all of the params.
55 // TODO: consider changing to a config struct
56 int CPGEquations::addNode(std::vector<double> newParams)
57 {
58  int index = nodeList.size();
59  CPGNode* newNode = new CPGNode(index, newParams);
60  nodeList.push_back(newNode);
61 
62  return index;
63 }
64 
65 void CPGEquations::connectNode( int nodeIndex,
66  std::vector<CPGEdge*> connectivityList)
67 {
68  nodeList[nodeIndex]->addCoupling(connectivityList);
69 }
70 
71 void CPGEquations::defineConnections ( int nodeIndex,
72  std::vector<int> connections,
73  std::vector<double> newWeights,
74  std::vector<double> newPhaseOffsets)
75 {
76  assert(connections.size() == newWeights.size());
77  assert(connections.size() == newPhaseOffsets.size());
78  assert(nodeList[nodeIndex] != NULL);
79 
80  for(int i = 0; i != connections.size(); i++){
81  nodeList[nodeIndex]->addCoupling(nodeList[connections[i]], newWeights[i], newPhaseOffsets[i]);
82  }
83 }
84 
85 std::vector<double> CPGEquations::getXVars() {
86  std::vector<double> newXVars;
87 
88  for (int i = 0; i != nodeList.size(); i++){
89  newXVars.push_back(nodeList[i]->phiValue);
90  newXVars.push_back(nodeList[i]->rValue);
91  newXVars.push_back(nodeList[i]->rDotValue);
92  }
93 
94  return newXVars;
95 }
96 
97 std::vector<double> CPGEquations::getDXVars() {
98  std::vector<double> newDXVars;
99 
100  for (int i = 0; i != nodeList.size(); i++){
101  newDXVars.push_back(nodeList[i]->phiDotValue);
102  newDXVars.push_back(nodeList[i]->rDotValue);
103  newDXVars.push_back(nodeList[i]->rDoubleDotValue);
104  }
105 
106  return newDXVars;
107 }
108 
109 void CPGEquations::updateNodes(std::vector<double> descCom)
110 {
111  for(int i = 0; i != nodeList.size(); i++){
112  nodeList[i]->updateDTs(descCom[i]);
113  }
114 }
115 
116 void CPGEquations::updateNodeData(std::vector<double> newXVals)
117 {
118  assert(newXVals.size()==3*nodeList.size());
119 
120  for(int i = 0; i!=nodeList.size(); i++){
121  nodeList[i]->updateNodeValues(newXVals[3*i], newXVals[3*i+1], newXVals[3*i+2]);
122  }
123 }
124 
129  public:
130 
131  integrate_function(CPGEquations* pCPGs, std::vector<double> newComs) :
132  theseCPGs(pCPGs),
133  descCom(newComs)
134  {
135 
136  }
137 
138  void operator() (const cpgVars_type &x ,
139  cpgVars_type &dxdt ,
140  double t )
141  {
142  theseCPGs->updateNodeData(x);
143  theseCPGs->updateNodes(descCom);
144 
148  std::vector<double> dXVars = theseCPGs->getDXVars();
153  for(std::size_t i = 0; i != x.size(); i++){
154  dxdt[i] = dXVars[i];
155  }
156  //std::cout<<"operator call"<<std::endl;
157 
158  }
159 
160  private:
161  CPGEquations* theseCPGs;
162  std::vector<double> descCom;
163 };
164 
169  public:
170 
171  output_function(CPGEquations* pCPGs) :
172  theseCPGs(pCPGs)
173  {
174  }
175 
176  void operator() ( const cpgVars_type &x ,
177  const double t )
178  {
182  theseCPGs->updateNodeData(x);
183  #if (0) // Suppress output
184  std::cout << t << '\t' << (*theseCPGs)[0] << '\t' << (*theseCPGs)[1] << '\t' << (*theseCPGs)[2] << std::endl;
185  #endif
186  }
187 
188  private:
189  CPGEquations* theseCPGs;
190 };
191 
192 void CPGEquations::update(std::vector<double> descCom, double dt)
193 {
194  if (dt <= 0.1){ //TODO: specify default step size as a parameter during construction
195  stepSize = dt;
196  }
197  else{
198  stepSize = 0.1;
199  }
200 
204  std::vector<double> xVars = getXVars();
205 
209  integrate(integrate_function(this, descCom), xVars, 0.0, dt, stepSize, output_function(this));
210 
211  #if (0)
212  std::cout << dt << '\t' << nodeList[0]->nodeValue <<
213  '\t' << nodeList[1]->nodeValue <<
214  '\t' << nodeList[2]->nodeValue << std::endl;
215  #endif
216 
217 }
218 
219 std::string CPGEquations::toString(const std::string& prefix) const
220 {
221  std::string p = " ";
222  std::ostringstream os;
223  os << prefix << "CPGEquations(" << std::endl;
224 
225  os << prefix << p << "Nodes:" << std::endl;
226  for(int i = 0; i < nodeList.size(); i++) {
227  os << prefix << p << p << *(nodeList[i]) << std::endl;
228  }
229 
230  os << prefix << ")" << std::endl;
231  return os.str();
232 }
void operator()(const cpgVars_type &x, cpgVars_type &dxdt, double t)
void update(std::vector< double > descCom, double dt)
void operator()(const cpgVars_type &x, const double t)
Definition of class CPGEquations.