NTRT Simulator  v1.1
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgImpedanceController.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 
25 // This module
26 #include "tgImpedanceController.h"
27 // This library
28 #include "tgBasicController.h"
29 #include "tgTensionController.h"
31 #include "core/tgBasicActuator.h"
32 #include "core/tgCast.h"
33 
39 static const double kDefaultOffsetTension = 0.001;
40 
46 static const double kDefaultLengthStiffness = 0.0;
47 
53 static const double kDefaultVelocityStiffness = 0.0;
54 
56  m_offsetTension(kDefaultOffsetTension),
57  m_lengthStiffness(kDefaultLengthStiffness),
58  m_velStiffness(kDefaultVelocityStiffness)
59 {
60  // Postcondition
61  assert(invariant());
62  assert(m_offsetTension == kDefaultOffsetTension);
63  assert(m_lengthStiffness == kDefaultLengthStiffness);
64  assert(m_velStiffness == kDefaultVelocityStiffness);
65 }
66 
68  double lengthStiffness,
69  double velStiffness) :
70  m_offsetTension(offsetTension),
71  m_lengthStiffness(lengthStiffness),
72  m_velStiffness(velStiffness)
73 {
74  // Precondition
75  assert(offsetTension >= 0.0);
76  assert(lengthStiffness >= 0.0);
77  assert(velStiffness >= 0.0);
78 
79  // Postcondition
80  assert(invariant());
81  assert(m_offsetTension == offsetTension);
82  assert(m_lengthStiffness == lengthStiffness);
83  assert(m_velStiffness == velStiffness);
84 }
85 
98 static inline double determineSetTension(double offset,
99  double displacement,
100  double velocity)
101 {
102  return std::max(static_cast<double>(0.0), offset + displacement + velocity);
103 }
104 
105 double
107  double deltaTimeSeconds,
108  double newPosition,
109  double offsetVel)
110 {
111  return controlTension( mLocalController,
112  deltaTimeSeconds,
113  newPosition,
115  offsetVel);
116 }
117 
118 double
119 tgImpedanceController::controlTension(tgBasicController& mLocalController,
120  double deltaTimeSeconds,
121  double newPosition ,
122  double offsetTension,
123  double offsetVel)
124 {
125 
126  const tgSpringCableActuator* mString = tgCast::cast<tgControllable, tgSpringCableActuator>
127  (mLocalController.getControllable());
128 
129  assert (mString);
130 
131  const double actualLength = mString->getCurrentLength();
132  const double vel = mString->getVelocity();
133 
134  const double setTension =
135  determineSetTension(offsetTension,
136  m_lengthStiffness * (actualLength - newPosition),
137  m_velStiffness * (vel - offsetVel));
138 
139  const double currentTension = mString->getTension();
140 
141  mLocalController.control(deltaTimeSeconds, setTension, currentTension);
142 
143  //std::cout << "Commanded tension " << setTension << " actual tension " << currentTension << " rest length " << mString->getRestLength() << std::endl;
144 
145  // Postcondition
146  assert(setTension >= 0.0);
147 
148  return setTension;
149 }
150 
151 double
153  double deltaTimeSeconds,
154  double newPosition,
155  double offsetVel)
156 {
157  return controlTension( mBasicActuator,
158  deltaTimeSeconds,
159  newPosition,
161  offsetVel);
162 }
163 
164 double
165 tgImpedanceController::controlTension(tgBasicActuator& mBasicActuator,
166  double deltaTimeSeconds,
167  double newPosition ,
168  double offsetTension,
169  double offsetVel)
170 {
171  const double actualLength = mBasicActuator.getCurrentLength();
172  const double vel = mBasicActuator.getVelocity();
173 
174  const double setTension =
175  determineSetTension(offsetTension,
176  m_lengthStiffness * (actualLength - newPosition),
177  m_velStiffness * (vel - offsetVel));
178 
179  tgTensionController::control(mBasicActuator, deltaTimeSeconds, setTension);
180 
181  // Postcondition
182  assert(setTension >= 0.0);
183 
184  return setTension;
185 }
186 
187 void tgImpedanceController::setOffsetTension(double offsetTension)
188 {
189  // Precondition
190  assert(offsetTension >= 0.0);
191 
192  m_offsetTension = offsetTension;
193 
194  // Postcondition
195  assert(invariant());
196  assert(m_offsetTension == offsetTension);
197 }
198 
199 void tgImpedanceController::setLengthStiffness(double lengthStiffness)
200 {
201  // Precondition
202  assert(lengthStiffness >= 0.0);
203 
204  m_lengthStiffness = lengthStiffness;
205 
206  // Postcondition
207  assert(invariant());
208  assert(m_lengthStiffness == lengthStiffness);
209 }
210 
211 void tgImpedanceController::setVelStiffness(double velStiffness)
212 {
213  // Precondition
214  assert(velStiffness >= 0.0);
215 
216  m_velStiffness = velStiffness;
217 
218  // Postcondition
219  assert(invariant());
220  assert(m_velStiffness == velStiffness);
221 }
222 
223 bool tgImpedanceController::invariant() const
224 {
225  return
226  (m_offsetTension >= 0.0) &&
227  (m_lengthStiffness >= 0.0) &&
228  (m_velStiffness >= 0.0);
229 }
Contains the definition of class ImpedanceControl. $Id$.
void setLengthStiffness(double lengthStiffness)
Definition of the tgTensionController base class.
virtual const double getTension() const
Utility class for class casting and filtering collections by type.
virtual void control(double dt)
Contains the definition of abstract base class tgSpringCableActuator. Assumes that the string is line...
virtual const double getVelocity() const
const tgControllable *const getControllable() const
Contains the definition of class tgBasicActuator.
tgImpedanceController()
Constructors.
void setVelStiffness(double velStiffness)
virtual void control(double dt)
Definition of the tgBasicController base class.
double control(tgBasicController &mLocalController, double deltaTimeSeconds, double newPosition, double offsetVel=0)
Control Functions.
void setOffsetTension(double offsetTension)
virtual const double getCurrentLength() const