NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
SpineOnlineControl.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 "SpineOnlineControl.h"
28 
30 
31 // Should include tgString, but compiler complains since its been
32 // included from BaseSpineModelLearning. Perhaps we should move things
33 // to a cpp over there
35 #include "core/tgBasicActuator.h"
36 #include "tgcreator/tgUtil.h"
39 #include "examples/learningSpines/tgCPGCableControl.h"
40 
41 #include "helpers/FileHelpers.h"
42 
45 
46 #include "util/CPGEquationsFB.h"
47 #include "examples/learningSpines/tgCPGCableControl.h"
48 
49 #include <iterator> // std::iterator
50 #include <string>
51 
52 //#define LOGGING
53 #define USE_KINEMATIC
54 
55 using namespace std;
56 
58  int tm,
59  int om,
60  int param,
61  int segnum,
62  double ct,
63  double la,
64  double ha,
65  double lp,
66  double hp,
67  double kt,
68  double kp,
69  double kv,
70  bool def,
71  double cl,
72  double lf,
73  double hf,
74  double ffMin,
75  double ffMax,
76  double afMin,
77  double afMax,
78  double pfMin,
79  double pfMax,
80  double tf,
81  double feedTime ) :
82 SpineGoalControl::Config::Config(ss, tm, om, param, segnum, ct, la, ha,
83  lp, hp, kt, kp, kv, def, cl, lf, hf,
84  ffMin, ffMax, afMin, afMax, pfMin, pfMax, tf),
85 feedbackTime(feedTime)
86 {
87 
88 }
95  std::string args,
96  std::string resourcePath,
97  std::string ec,
98  std::string nc,
99  std::string fc,
100  std::string gc) :
101 SpineGoalControl(config, args, resourcePath, ec, nc, fc, gc),
102 m_config(config),
103 m_feedbackControlTime(0.0)
104 {
105 }
106 
108 {
109  m_feedbackControlTime = 0.0;
110 
111  const BaseSpineModelGoal* goalSubject = tgCast::cast<BaseSpineModelLearning, BaseSpineModelGoal>(subject);
112 
113  m_lastGoalDist = getGoalDist(goalSubject);
114  m_controllerStartDist = m_lastGoalDist;
115 
116  SpineGoalControl::onSetup(subject);
117 }
118 
120 {
121  m_updateTime += dt;
122  m_feedbackControlTime += dt;
123  if (m_updateTime >= m_config.controlTime)
124  {
125  if(m_feedbackControlTime > m_config.feedbackTime)
126  {
127  const BaseSpineModelGoal* goalSubject = tgCast::cast<BaseSpineModelLearning, BaseSpineModelGoal>(subject);
128 
129  const double dist = getGoalDist(goalSubject);
130 
131  if (dist > m_lastGoalDist)
132  {
133 #if (0)
134  // Moved away from the goal, get a new controller
135  std::vector<double> tempScores;
136  tempScores.push_back(m_controllerStartDist - dist);
137  tempScores.push_back(0.0);
138  goalAdapter.endEpisode(tempScores);
139 
140  goalAdapter.initialize(&goalEvolution,
141  goalLearning,
142  goalConfigData);
143  m_controllerStartDist = dist;
144 #else
145  throw std::runtime_error("Moved away from goal");
146 #endif
147  }
148 
149  m_feedbackControlTime = 0;
150  m_lastGoalDist = dist;
151  }
152 #if (1)
153  const BaseSpineModelGoal* goalSubject = tgCast::cast<BaseSpineModelLearning, BaseSpineModelGoal>(subject);
154  std::vector<double> desComs = getGoalFeedback(goalSubject);
155 #else
156  std::vector<double> desComs = getFeedback(subject);
157 
158 #endif
159  try
160  {
161  m_pCPGSys->update(desComs, m_updateTime);
162  }
163  catch (std::runtime_error& e)
164  {
165  // Stops the trial immediately, lets teardown know it broke
166  bogus = true;
167  throw (e);
168  }
169 
170 #ifdef LOGGING // Conditional compile for data logging
171  m_dataObserver.onStep(subject, m_updateTime);
172 #endif
173  notifyStep(m_updateTime);
174  m_updateTime = 0;
175  }
176 
177  double currentHeight = subject.getSegmentCOM(m_config.segmentNumber)[1];
178 
180  if (currentHeight > 25 || currentHeight < 1.0)
181  {
183  bogus = true;
184  throw std::runtime_error("Height out of range");
185  }
186 }
187 
189 {
190  scores.clear();
191  // @todo - check to make sure we ran for the right amount of time
192 
193  const BaseSpineModelGoal* goalSubject = tgCast::cast<BaseSpineModelLearning, BaseSpineModelGoal>(subject);
194 
195  const double distanceMoved = calculateDistanceMoved(goalSubject);
196 
197  if (bogus)
198  {
199  scores.push_back(-1.0);
200  }
201  else
202  {
203  scores.push_back(distanceMoved);
204  }
205 
208  double totalEnergySpent=0;
209 
210  std::vector<tgSpringCableActuator* > tmpStrings = subject.getAllMuscles();
211 
212  for(int i=0; i<tmpStrings.size(); i++)
213  {
214  tgSpringCableActuator::SpringCableActuatorHistory stringHist = tmpStrings[i]->getHistory();
215 
216  for(int j=1; j<stringHist.tensionHistory.size(); j++)
217  {
218  const double previousTension = stringHist.tensionHistory[j-1];
219  const double previousLength = stringHist.restLengths[j-1];
220  const double currentLength = stringHist.restLengths[j];
221  //TODO: examine this assumption - free spinning motor may require more power
222  double motorSpeed = (currentLength-previousLength);
223  if(motorSpeed > 0) // Vestigial code
224  motorSpeed = 0;
225  const double workDone = previousTension * motorSpeed;
226  totalEnergySpent += workDone;
227  }
228  }
229 
230  scores.push_back(totalEnergySpent);
231 
232  edgeAdapter.endEpisode(scores);
233  nodeAdapter.endEpisode(scores);
234  feedbackAdapter.endEpisode(scores);
235 
236  const double dist = getGoalDist(goalSubject);
237  std::vector<double> tempScores;
238  tempScores.push_back(m_controllerStartDist - dist);
239  tempScores.push_back(0.0);
240  goalAdapter.endEpisode(tempScores);
241 
242  delete m_pCPGSys;
243  m_pCPGSys = NULL;
244 
245  for(size_t i = 0; i < m_allControllers.size(); i++)
246  {
247  delete m_allControllers[i];
248  }
249  m_allControllers.clear();
250 }
251 
252 double SpineOnlineControl::getGoalDist(const BaseSpineModelGoal* subject) const
253 {
254  // TODO: consider comparing all segments instead of just the one specified by config
255  std::vector<double> finalConditions = subject->getSegmentCOM(m_config.segmentNumber);
256 
257  const btVector3 goalPos = subject->goalBoxPosition();
258 
259  double x= finalConditions[0] - goalPos.getX();
260  double z= finalConditions[2] - goalPos.getZ();
261  double distanceNew=sqrt(x*x + z*z);
262 
263  return distanceNew;
264 }
Contains the definition of class ImpedanceControl. $Id$.
void initialize(NeuroEvolution *evo, bool isLearning, configuration config)
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 tf=0.0, double feedTime=1.0)
void update(std::vector< double > &descCom, double dt)
A controller for the template class BaseSpineModelLearning.
virtual void onSetup(BaseSpineModelLearning &subject)
virtual void onStep(BaseSpineModelLearning &subject, double dt)
Definition of the tgCPGStringControl observer class.
Implementing the tetrahedral complex spine inspired by Tom Flemons.
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 void onStep(tgModel &model, double dt)
virtual void onSetup(BaseSpineModelLearning &subject)
virtual void onTeardown(BaseSpineModelLearning &subject)
Definition of class CPGEquationsFB.
Rand seeding simular to the evolution and terrain classes.
SpineOnlineControl(SpineOnlineControl::Config config, std::string args, std::string resourcePath="", std::string ec="edgeConfig.ini", std::string nc="nodeConfig.ini", std::string fc="feedbackConfig.ini", std::string gc="goalConfig.ini")