NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgSCASineControl.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 
19 #include "tgSCASineControl.h"
20 
22 
23 #include <iostream>
24 #include <stdexcept>
25 #include <cmath>
26 
27 tgSCASineControl::tgSCASineControl(const double controlStep,
28  tgImpedanceController* p_ipc,
29  tgPIDController::Config pidConfig,
30  const double amplitude,
31  const double frequency,
32  const double phase,
33  const double offset,
34  const double length) :
35 m_controlTime(0.0),
36 m_totalTime(0.0),
37 m_controlStep(controlStep),
38 m_commandedTension(0.0),
39 cpgAmplitude(amplitude),
40 cpgFrequency(frequency),
41 phaseOffset(phase),
42 offsetSpeed(offset),
43 cycle(0.0),
44 target (0.0),
45 m_controlLength(length),
46 m_tempConfig(pidConfig),
47 m_PIDController(NULL),
48 m_pMotorControl(p_ipc)
49 {
50  if (m_controlStep < 0.0)
51  {
52  throw std::invalid_argument("Negative control step");
53  }
54 
56 
57  assert(p_ipc);
58 }
59 
60 tgSCASineControl::~tgSCASineControl()
61 {
62  delete m_PIDController;
63 }
64 
66 {
67  m_PIDController = new tgPIDController(&subject, m_tempConfig);
68 }
69 
71 {
72  assert(&subject == m_PIDController->getControllable());
73 
74  m_controlTime += dt;
75  m_totalTime += dt;
76 
77  if (m_controlTime >= m_controlStep)
78  {
79  // Yep, its a misnomer. Had to change it for In Won
80  cycle = cos(m_totalTime * 2.0 * M_PI * cpgFrequency + phaseOffset);
81  target = cycle*cpgAmplitude + offsetSpeed;
82 
83  // dt is just passed through to PID controller
84  m_commandedTension = m_pMotorControl->control(*m_PIDController, dt, m_controlLength, target);
85  //std::cout << m_commandedTension << std::endl;
86  m_controlTime = 0;
87  }
88  else
89  {
90  const double currentTension = subject.getTension();
91  m_PIDController->control(dt, m_commandedTension, currentTension);
92  }
93 
94 }
95 
96 void tgSCASineControl::updateTensionSetpoint(double newTension)
97 {
98  if (newTension >= 0.0)
99  {
100  m_pMotorControl->setOffsetTension(newTension);
101  }
102  else
103  {
104  throw std::runtime_error("Tension setpoint is less than zero!");
105  }
106 }
107 
108 void tgSCASineControl::updateControlLength(double newControlLength)
109 {
110  if (newControlLength >= 0.0)
111  {
112  m_controlLength = newControlLength;
113  }
114  else
115  {
116  throw std::runtime_error("Length setpoint is less than zero!");
117  }
118 }
Contains the definition of class ImpedanceControl. $Id$.
virtual const double getTension() const
tgSCASineControl(const double controlStep, tgImpedanceController *p_ipc, tgPIDController::Config pidConfig, const double amplitude, const double frequency, const double phase, const double offset, const double length)
virtual void control(double dt)
const tgControllable *const getControllable() const
virtual void onStep(tgSpringCableActuator &subject, double dt)
virtual void onAttach(tgSpringCableActuator &subject)
double control(tgBasicController &mLocalController, double deltaTimeSeconds, double newPosition, double offsetVel=0)
Control Functions.
void setOffsetTension(double offsetTension)