NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
SerializedSineWaves.cpp
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 // This module
28 #include "SerializedSineWaves.h"
29 
30 // Its subject
32 
33 // NTRTSim
36 #include "tgcreator/tgUtil.h"
37 #include "core/tgCast.h"
38 #include "core/tgBasicActuator.h"
39 
40 // The C++ Standard Library
41 #include <stdexcept>
42 #include <string>
43 
44 // JSON Serialization
45 #include "helpers/FileHelpers.h"
46 #include <json/json.h>
47 
49 {
50 
51  //BEGIN DESERIALIZING
52 
53  Json::Value root; // will contains the root value after parsing.
54  Json::Reader reader;
55 
56  bool parsingSuccessful = reader.parse( FileHelpers::getFileString("controlVars.json"), root );
57  if ( !parsingSuccessful )
58  {
59  // report to the user the failure and their locations in the document.
60  std::cout << "Failed to parse configuration\n"
61  << reader.getFormattedErrorMessages();
63  return;
64  }
65  // Get the value of the member of root named 'encoding', return 'UTF-8' if there is no
66  // such member.
67  double kTen = root.get("inside_imp_ten", "UTF-8").asDouble();
68  double kPos = root.get("inside_imp_pos", "UTF-8").asDouble();
69  double kVel = root.get("inside_imp_vel", "UTF-8").asDouble();
70  in_controller = new tgImpedanceController(kTen, kPos, kVel);
71 
72  kTen = root.get("outside_imp_ten", "UTF-8").asDouble();
73  kPos = root.get("outside_imp_pos", "UTF-8").asDouble();
74  kVel = root.get("outside_imp_vel", "UTF-8").asDouble();
75  out_controller = new tgImpedanceController(kTen, kPos, kVel);
76 
77  insideLength = root.get("inside_length", "UTF-8").asDouble();
78  outsideLength = root.get("outside_length", "UTF-8").asDouble();
79  offsetSpeed = root.get("offset_speed", "UTF-8").asDouble();
80  cpgAmplitude = root.get("cpg_amplitude", "UTF-8").asDouble();
81  cpgFrequency = root.get("cpg_frequency", "UTF-8").asDouble();
82  bodyWaves = root.get("bodyWaves", "UTF-8").asDouble();
83  insideMod = root.get("insideMod", "UTF-8").asDouble();
84 
85  updateFrequency = root.get("updateFrequency", "UTF-8").asDouble();
86 
87  phaseOffsets.clear();
88  phaseOffsets.push_back(root.get("top_offset", "UTF-8").asDouble());
89  phaseOffsets.push_back(root.get("left_offset", "UTF-8").asDouble());
90  phaseOffsets.push_back(root.get("right_offset", "UTF-8").asDouble());
91 
92  //END SERIALIZING
93 
95 }
96 
97 SerializedSineWaves::Config::~Config()
98 {
99  delete in_controller;
100  delete out_controller;
101 }
102 
103 
105 m_config(fileName),
106 segments(1.0),
107 simTime(0.0),
108 updateTime(0.0),
109 cycle(0.0),
110 target(0.0)
111 {
112 
113 }
114 
116 {
117 }
118 
119 void SerializedSineWaves::applyImpedanceControlInside(const std::vector<tgSpringCableActuator*> stringList,
120  double dt,
121  std::size_t phase)
122 {
123  std::vector<tgBasicActuator* > stringList_ba = tgCast::filter<tgSpringCableActuator, tgBasicActuator>(stringList);
124 
125  for(std::size_t i = 0; i < stringList_ba.size(); i++)
126  {
127  // This will reproduce the same value until simTime is updated. See onStep
128  cycle = sin(simTime * m_config.cpgFrequency + 2 * m_config.bodyWaves * M_PI * i / (segments) + m_config.phaseOffsets[phase]);
129  target = m_config.offsetSpeed + cycle*m_config.cpgAmplitude;
130 
131 
132  double setTension = m_config.in_controller->control(*(stringList_ba[i]),
133  dt,
134  m_config.insideLength,
135  m_config.insideMod * target
136  );
137  #if (0) // Conditional compile for verbose control
138  std::cout << "Inside String " << i << " tension " << setTension
139  << " act tension " << stringList[i]->getMuscle()->getTension()
140  << " length " << stringList[i]->getMuscle()->getActualLength() << std::endl;
141  #endif
142  }
143 }
144 
145 void SerializedSineWaves::applyImpedanceControlOutside(const std::vector<tgSpringCableActuator*> stringList,
146  double dt,
147  std::size_t phase)
148 {
149  std::vector<tgBasicActuator* > stringList_ba = tgCast::filter<tgSpringCableActuator, tgBasicActuator>(stringList);
150 
151  for(std::size_t i = 0; i < stringList_ba.size(); i++)
152  {
153  // This will reproduce the same value until simTime is updated. See onStep
154  cycle = sin(simTime * m_config.cpgFrequency + 2 * m_config.bodyWaves * M_PI * i / (segments) + m_config.phaseOffsets[phase]);
155  target = m_config.offsetSpeed + cycle*m_config.cpgAmplitude;
156 
157  double setTension = m_config.out_controller->control(*( stringList_ba[i]),
158  dt,
159  m_config.outsideLength,
160  target
161  );
162  #if(0) // Conditional compile for verbose control
163  std::cout << "Outside String " << i << " com tension " << setTension
164  << " act tension " << stringList[i]->getMuscle()->getTension()
165  << " length " << stringList[i]->getMuscle()->getActualLength() << std::endl;
166  #endif
167  }
168 }
169 
171 {
172 
173  updateTime += dt;
174 
175  if (updateTime >= 1.0/m_config.updateFrequency)
176  {
177  simTime += updateTime;
178  updateTime = 0.0;
179  }
180 
181  segments = subject.getSegments();
182 
183  applyImpedanceControlInside(subject.getMuscles("inner top"), dt, 0);
184  applyImpedanceControlInside(subject.getMuscles("inner left") , dt, 1);
185  applyImpedanceControlInside(subject.getMuscles("inner right"), dt, 2);
186 
187  applyImpedanceControlOutside(subject.getMuscles("outer top"), dt, 0);
188  applyImpedanceControlOutside(subject.getMuscles("outer left"), dt, 1);
189  applyImpedanceControlOutside(subject.getMuscles("outer right"), dt, 2);
190 
191 }
192 
193 
Contains the definition of class ImpedanceControl. $Id$.
SerializedSineWaves(std::string fileName)
virtual void onStep(BaseSpineModelLearning &subject, double dt)
Utility class for class casting and filtering collections by type.
void applyImpedanceControlOutside(const std::vector< tgSpringCableActuator * > stringList, double dt, std::size_t phase)
Contains the definition of abstract base class tgSpringCableActuator. Assumes that the string is line...
A series of functions to assist with file input/output.
std::vector< double > phaseOffsets
Contains the definition of class tgBasicActuator.
void applyImpedanceControlInside(const std::vector< tgSpringCableActuator * > stringList, double dt, std::size_t phase)
A Sine Wave controller for TetraSpine using JSON serialization.
tgImpedanceController * in_controller
Tetraspine, configured for learning in the NTRT simulator.
Contains the definition of class tgUtil and overloaded operator<<() free functions.
double control(tgBasicController &mLocalController, double deltaTimeSeconds, double newPosition, double offsetVel=0)
Control Functions.