NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
VerticalSpineBendingController.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 
26 // This module
28 // This application
29 #include "VerticalSpineModel.h"
30 // This library
31 //#include "core/tgBasicActuator.h"
33 #include "core/tgString.h"
34 #include "sensors/tgDataObserver.h"
35 // The C++ Standard Library
36 #include <cassert>
37 #include <stdexcept>
38 #include <vector>
39 
40 #include "helpers/FileHelpers.h"
41 
43  verticalRLA1(4.0),
44  verticalRLA2(4.0),
45  verticalRLA3(4.0),
46  verticalRLA4(4.0),
47  verticalRLB1(4.0),
48  verticalRLB2(4.0),
49  verticalRLB3(4.0),
50  verticalRLB4(4.0),
51  dL(0.002), // Length Change, 0.01
52  state(-1.0),
53  m_updateTime(0.0),
54  m_dataObserver("logs/vertspine_1-2-3-4_")
55 {
56  // verticalRL = 1; // cm
57  //verticalRL = 7.38; //cm
58  //saddleRL1 = 13.037 ; // cm
59  //saddleRL2 = 13.613 ; // cm
60  //saddleRL3 = 14.189 ; // cm
61  //saddleRL4 = 14.766 ; // cm
62 
63 }
64 
66  m_dataObserver.onSetup(subject);
67 }
68 
69 
70  //Gear Ratio 1: 1-2-3-4
71 
73  {
74  if (dt <= 0.0)
75  {
76  throw std::invalid_argument("dt is not positive");
77  }
78  else
79  {
80  updateTime += dt;
81  if (updateTime >= 1.0/100) //Speed of actuators
82  {
83  updateTime = 0.0;
84 
85  // logging
86  notifyStep(m_updateTime);
87  m_dataObserver.onStep(subject, m_updateTime);
88 
89  // Bend & Unbend
90  if(verticalRLA1 <= 2.0 && state == -1.0) //min length of cable
91  {
92  state = 1.0;
93  }
94  else if (verticalRLA1 >= 4.0 && state == 1.0) //stop at upright position
95  {
96  state = -1.0;
97  }
98 
99  if (state == -1.0)
100  {
101  verticalRLA1 -= dL;
102  verticalRLB1 += dL;
103  }
104  else if (state == 1.0)
105  {
106  verticalRLA1 += dL;
107  verticalRLB1 -= dL;
108  }
109  }
110 
111  // First, get all muscles (cables)
112  const std::vector<tgSpringCableActuator*> muscles = subject.getAllMuscles();
113 
114  // get all vertical muscles
115  const std::vector<tgSpringCableActuator*> v_musclesA = subject.getMuscles("vertical a");
116  const std::vector<tgSpringCableActuator*> v_musclesB = subject.getMuscles("vertical b");
117  const std::vector<tgSpringCableActuator*> v_musclesC = subject.getMuscles("vertical c");
118  const std::vector<tgSpringCableActuator*> v_musclesD = subject.getMuscles("vertical d");
119 
120 
121  // set string length for vertical muscles
122  for (size_t i = 0; i < v_musclesA.size(); ++ i)
123  {
124  //A **Contracting Cable
125  tgSpringCableActuator * const pMuscleA = v_musclesA[i];
126  assert(pMuscleA != NULL);
127  pMuscleA->setControlInput(verticalRLA1,dt);
128 
129  //(*(v_musclesA[i])).setControlInput(verticalRLA1,dt);
130  //(v_musclesA[i])->setControlInput(verticalRLA1, dt);
131  //Need assert for NULL
132 
133 
134 
135  //B **Elongating Cable
136  tgSpringCableActuator * const pMuscleB = v_musclesB[i];
137  assert(pMuscleB != NULL);
138  pMuscleB->setControlInput(verticalRLB1,dt);
139 
140  // //C
141  // tgBasicActuator * const pMuscleC = v_musclesC[i];
142  // assert(pMuscleC != NULL);
143  // pMuscleC->setControlInput(verticalRL);
144 
145  // //D
146  // tgBasicActuator * const pMuscleD = v_musclesD[i];
147  // assert(pMuscleD != NULL);
148  // pMuscleD->setControlInput(verticalRL);
149  }
150 
151  }
152 }
153 
154 
155 
156 
157 //Gear Ratio 2: 1-2-3.2-4.66
158 /*
159 void VerticalSpineBendingController::onStep(VerticalSpineModel& subject, double dt)
160 {
161  if (dt <= 0.0)
162  {
163  throw std::invalid_argument("dt is not positive");
164  }
165  else
166  {
167  updateTime += dt;
168  if (updateTime >= 1.0/100) //Speed of actuators
169  {
170  updateTime = 0.0;
171 
172  // logging
173  notifyStep(m_updateTime);
174 
175  // Bend & Unbend
176  if(verticalRLA1 <= 2.0 && state == -1.0) //min length of cable
177  {
178  state = 1.0;
179  }
180  else if (verticalRLA1 >= 4.0 && state == 1.0) //stop at upright position
181  {
182  state = -1.0;
183  }
184 
185  if (state == -1.0)
186  {
187  verticalRLA1 -= dL;
188  verticalRLA2 -= dL;
189  verticalRLA3 -= dL*1.26;
190  verticalRLA4 -= dL*1.4;
191 
192  verticalRLB1 += dL;
193  verticalRLB2 += dL;
194  verticalRLB3 += dL*1.26;
195  verticalRLB4 += dL*1.4;
196  }
197  else if (state == 1.0)
198  {
199  verticalRLA1 += dL;
200  verticalRLA2 += dL;
201  verticalRLA3 += dL*1.26;
202  verticalRLA4 += dL*1.4;
203 
204  verticalRLB1 -= dL;
205  verticalRLB2 -= dL;
206  verticalRLB3 -= dL*1.26;
207  verticalRLB4 -= dL*1.4;
208  }
209  }
210 
211  // First, get all muscles (cables)
212  const std::vector<tgSpringCableActuator*> muscles = subject.getAllMuscles();
213 
214  // get all vertical muscles
215  const std::vector<tgSpringCableActuator*> v_musclesA = subject.getMuscles("vertical a");
216  const std::vector<tgSpringCableActuator*> v_musclesB = subject.getMuscles("vertical b");
217  const std::vector<tgSpringCableActuator*> v_musclesC = subject.getMuscles("vertical c");
218  const std::vector<tgSpringCableActuator*> v_musclesD = subject.getMuscles("vertical d");
219 
220 
221  // set string length for vertical muscles
222  //A **Contracting Cable
223 
224  tgSpringCableActuator * const pMuscleA1 = v_musclesA[0];
225  tgSpringCableActuator * const pMuscleA2 = v_musclesA[1];
226  tgSpringCableActuator * const pMuscleA3 = v_musclesA[2];
227  tgSpringCableActuator * const pMuscleA4 = v_musclesA[3];
228  assert(pMuscleA1 != NULL);
229  assert(pMuscleA2 != NULL);
230  assert(pMuscleA3 != NULL);
231  assert(pMuscleA4 != NULL);
232  pMuscleA1->setControlInput(verticalRLA1,dt);
233  pMuscleA2->setControlInput(verticalRLA2,dt);
234  pMuscleA3->setControlInput(verticalRLA3,dt);
235  pMuscleA4->setControlInput(verticalRLA4,dt);
236 
237  //B **Elongating Cable
238  tgSpringCableActuator * const pMuscleB1 = v_musclesB[0];
239  tgSpringCableActuator * const pMuscleB2 = v_musclesB[1];
240  tgSpringCableActuator * const pMuscleB3 = v_musclesB[2];
241  tgSpringCableActuator * const pMuscleB4 = v_musclesB[3];
242  assert(pMuscleB1 != NULL);
243  assert(pMuscleB2 != NULL);
244  assert(pMuscleB3 != NULL);
245  assert(pMuscleB4 != NULL);
246  pMuscleB1->setControlInput(verticalRLB1,dt);
247  pMuscleB2->setControlInput(verticalRLB2,dt);
248  pMuscleB3->setControlInput(verticalRLB3,dt);
249  pMuscleB4->setControlInput(verticalRLB4,dt);
250 
251  // //C
252  // tgBasicActuator * const pMuscleC = v_musclesC[i];
253  // assert(pMuscleC != NULL);
254  // pMuscleC->setControlInput(verticalRL);
255 
256  // //D
257  // tgBasicActuator * const pMuscleD = v_musclesD[i];
258  // assert(pMuscleD != NULL);
259  // pMuscleD->setControlInput(verticalRL);
260 
261  }
262 
263 }
264 */
265 
const std::vector< tgSpringCableActuator * > & getMuscles(const std::string &key) const
virtual void setControlInput(double input)=0
Convenience function for combining strings with ints, mostly for naming structures.
Contains the definition of class VerticalSpineModel.
Contains the definition of abstract base class tgSpringCableActuator. Assumes that the string is line...
A series of functions to assist with file input/output.
Definition of tgObserver class.
virtual void onStep(VerticalSpineModel &subject, double dt)
virtual void onSetup(VerticalSpineModel &subject)
Contains the definition of class VerticalSpineBendingController.
virtual void onStep(tgModel &model, double dt)
virtual void onSetup(tgModel &model)
const std::vector< tgSpringCableActuator * > & getAllMuscles() const