NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
SpineKinematicsTestController.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 
26 // This module
28 // This application
29 #include "yamlbuilder/TensegrityModel.h"
30 // This library
31 #include "core/tgBasicActuator.h"
33 #include "core/tgString.h"
34 #include "core/tgTags.h"
35 
36 // The C++ Standard Library
37 #include <cassert>
38 #include <stdexcept>
39 #include <vector>
40 #include <iostream>
41 #include "helpers/FileHelpers.h"
42 #include <stdexcept>
43 
44 // Constructor assigns variables, does some simple sanity checks.
45 // Also, initializes the accumulator variable timePassed so that it can
46 // be incremented in onStep.
48  double minLength,
49  double rate,
50  std::vector<std::string> tagsToControl) :
51  m_startTime(startTime),
52  m_minLength(minLength),
53  m_rate(rate),
54  m_tagsToControl(tagsToControl),
55  m_timePassed(0.0)
56 {
57  // start time must be greater than or equal to zero
58  if( m_startTime < 0.0 ) {
59  throw std::invalid_argument("Start time must be greater than or equal to zero.");
60  }
61  // min length must be between 1 and 0
62  else if( m_minLength > 1 ) {
63  throw std::invalid_argument("minLength is a percent, must be less than 1. (100%)");
64  }
65  else if( m_minLength < 0.0) {
66  throw std::invalid_argument("minLength is a percent, must be greater than 0.");
67  }
68  // rate must be greater than zero
69  else if( rate < 0.0 ) {
70  throw std::invalid_argument("Rate cannot be negative.");
71  }
72  // @TODO: what checks to make on tags?
73 }
74 
81  std::string tag) {
82  //DEBUGGING
83  std::cout << "Finding cables with the tag: " << tag << std::endl;
84  // Pick out the actuators with the specified tag
85  std::vector<tgBasicActuator*> foundActuators = subject.find<tgBasicActuator>(tag);
86  std::cout << "The following cables were found and will be controlled: "
87  << std::endl;
88  //Iterate through array and output strings to command line
89  for (std::size_t i = 0; i < foundActuators.size(); i ++) {
90  std::cout << foundActuators[i]->getTags() << std::endl;
91  // Also, add the rest length of the actuator at this time
92  // to the list of all initial rest lengths.
93  initialRL[foundActuators[i]->getTags()] = foundActuators[i]->getRestLength();
94  //DEBUGGING:
95  std::cout << "Cable rest length at t=0 is "
96  << initialRL[foundActuators[i]->getTags()] << std::endl;
97  }
98  // Add this list of actuators to the full list. Thanks to:
99  // http://stackoverflow.com/questions/201718/concatenating-two-stdvectors
100  cablesWithTags.insert( cablesWithTags.end(), foundActuators.begin(),
101  foundActuators.end() );
102 }
103 
110 {
111  std::cout << "Setting up the HorizontalSpine controller." << std::endl;
112  // << "Finding cables with tags: " << m_tagsToControl
113  // << std::endl;
114  cablesWithTags = {};
115  // For all the strings in the list, call initializeActuators.
116  std::vector<std::string>::iterator it;
117  for( it = m_tagsToControl.begin(); it < m_tagsToControl.end(); it++ ) {
118  // Call the helper for this tag.
119  initializeActuators(subject, *it);
120  }
121  std::cout << "Finished setting up the controller." << std::endl;
122 }
123 
125 {
126  // First, increment the accumulator variable.
127  m_timePassed += dt;
128  // Then, if it's passed the time to start the controller,
129  if( m_timePassed > m_startTime ) {
130  // For each cable, check if its rest length is past the minimum,
131  // otherwise adjust its length according to m_rate and dt.
132  for (std::size_t i = 0; i < cablesWithTags.size(); i ++) {
133  double currRestLength = cablesWithTags[i]->getRestLength();
134  // Calculate the minimum rest length for this cable.
135  // Remember that m_minLength is a percent.
136  double minRestLength = initialRL[cablesWithTags[i]->getTags()] * m_minLength;
137  // If the current rest length is still greater than the minimum,
138  if( currRestLength > minRestLength ) {
139  // output a progress bar for the controller, to track when control occurs.
140  std::cout << "." << i;
141  // Then, adjust the rest length of the actuator itself, according to
142  // m_rate and dt.
143  double nextRestLength = currRestLength - m_rate * dt;
144  //DEBUGGING
145  //std::cout << "Next Rest Length: " << nextRestLength << std::endl;
146  cablesWithTags[i]->setControlInput(nextRestLength,dt);
147  }
148  }
149  }
150 }
151 
152 
void initializeActuators(TensegrityModel &subject, std::string tag)
Convenience function for combining strings with ints, mostly for naming structures.
SpineKinematicsTestController(double startTime, double minLength, double rate, std::vector< std::string > tagsToControl)
Contains the definition of class SpineKinematicsTestController.
Contains the definition of abstract base class tgSpringCableActuator. Assumes that the string is line...
Contains the definition of class tgTags.
A series of functions to assist with file input/output.
Contains the definition of class tgBasicActuator.
virtual void onSetup(TensegrityModel &subject)
std::vector< T * > find(const tgTagSearch &tagSearch)
Definition: tgModel.h:128
virtual void onStep(TensegrityModel &subject, double dt)