NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
T6RestLengthController.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 // This module
28 #include "T6RestLengthController.h"
29 // This application
30 #include "T6Model.h"
31 // This library
32 #include "core/tgLinearString.h"
33 // The C++ Standard Library
34 #include <cassert>
35 #include <stdexcept>
36 #include <vector>
37 
38 T6RestLengthController::T6RestLengthController(const double restLengthDiff) :
39  m_restLengthDiff(restLengthDiff)
40 {
41  if (restLengthDiff < 0.0)
42  {
43  throw std::invalid_argument("You tried to push a rope!");
44  }
45 }
46 
48 {
49  // Do a one-time update of all cable rest lengths
50  // First, get all muscles (cables)
51  const std::vector<tgLinearString*> muscles = subject.getAllMuscles();
52 
53  // then, iterate over all muscles
54  for (size_t i = 0; i < muscles.size(); ++i)
55  {
56  tgLinearString * const pMuscle = muscles[i];
57  assert(pMuscle != NULL);
58 
59  // set rest length of the i-th muscle
60  // NOTE that we don't technically have a dt here: this is pre-simualtion.
61  // However, the dt is only used in tgLinearString for upper-bounding
62  // velocity and acceleration of our imaginary "motors," so we can
63  // choose whatever we want here.
64  double dt = 0.01;
65  double desiredRestLength = pMuscle->getStartLength() - m_restLengthDiff;
66  pMuscle->setRestLength(desiredRestLength, dt);
67  }
68 }
69 
70 // This method is included for now: this SHOULD really work without the update
71 // each timestep... but just including it in onSetup doesn't seem to work.
72 void T6RestLengthController::onStep(T6Model& subject, double dt)
73 {
74  if (dt <= 0.0)
75  {
76  throw std::invalid_argument("dt is not positive");
77  }
78  else
79  {
80  // Do an update of all cable rest lengths
81  // First, get all muscles (cables)
82  const std::vector<tgLinearString*> muscles = subject.getAllMuscles();
83 
84  // then, iterate over all muscles
85  for (size_t i = 0; i < muscles.size(); ++i)
86  {
87  tgLinearString * const pMuscle = muscles[i];
88  assert(pMuscle != NULL);
89 
90  // set rest length of the i-th muscle
91  double desiredRestLength = pMuscle->getStartLength() - m_restLengthDiff;
92  pMuscle->setRestLength(desiredRestLength, dt);
93  }
94  }
95 }
T6RestLengthController(const double restLengthDiff=4)
Contains the definition of class T6Model. $Id$.
void setRestLength(double newLength, float dt)
Contains the definition of class T6RestLengthController.
Contains the definition of class tgLinearString.
virtual void onSetup(T6Model &subject)
const std::vector< tgLinearString * > & getAllMuscles() const
Definition: T6Model.cpp:234
virtual const double getStartLength() const
virtual void onStep(T6Model &subject, double dt)