NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
tgPIDController.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 
28 #include "tgPIDController.h"
29 
30 #include "core/tgControllable.h"
31 
32 // The C++ Standard Library
33 #include <stdexcept>
34 #include <cassert>
35 
37  double i,
38  double d,
39  bool tensControl,
40  double setPoint) :
41 kP(tensControl ? -p : p),
42 kI(tensControl ? -i : i),
43 kD(tensControl ? -d : d),
44 startingSetPoint(setPoint)
45 {
46  if (p < 0.0)
47  {
48  throw std::invalid_argument("Value for p is negative");
49  }
50  else if (i < 0.0)
51  {
52  throw std::invalid_argument("Integral gain is negative.");
53  }
54  else if (d < 0.0)
55  {
56  throw std::invalid_argument("Derivative gain is negative.");
57  }
58 
59  // Use either standard gains or inverted gains for tension control
60  assert ( (kP >= 0.0 && kI >= 0.0 && kD >= 0.0) ||
61  (kP <= 0.0 && kI <= 0.0 && kD <= 0.0));
62 }
63 
64 
66 m_sensorData(0.0),
67 m_prevError(0.0),
68 m_intError(0.0),
69 m_config(config),
70 tgBasicController(controllable, config.startingSetPoint)
71 {
72  assert(controllable != NULL);
73 }
74 
76 {
77  // tgBasicController owns m_controllable
78 }
79 
80 void tgPIDController::control(double dt)
81 {
82  if (dt <= 0.0)
83  {
84  throw std::runtime_error ("Timestep must be positive.");
85  }
86 
87  double error = m_setPoint - m_sensorData;
88 
90  m_intError += (error + m_prevError) / 2.0 * dt;
91  double dError = (error - m_prevError) / dt;
92  double result = m_config.kP * error + m_config.kI * m_intError +
93  m_config.kD * dError;
94 
96 
97  m_prevError = error;
98 }
99 
100 void tgPIDController::control(double dt, double setPoint, double sensorData)
101 {
102  if (dt <= 0.0)
103  {
104  throw std::runtime_error ("Timestep must be positive.");
105  }
106 
107  setSensorData(sensorData);
108  setNewSetPoint(setPoint);
109  control(dt);
110 }
111 
112 void tgPIDController::setSensorData(double sensorData)
113 {
115  m_sensorData = sensorData;
116 }
Config(double p=1.0, double i=0.0, double d=0.0, bool tensControl=false, double setPoint=0.0)
tgControllable * m_controllable
virtual void setControlInput(double input)=0
Definition of the tgControllable abstract base class.
virtual void control(double dt)
virtual void setNewSetPoint(double newSetPoint)
virtual ~tgPIDController()
Definition of the tgPIDController class.
virtual void setSensorData(double sensorData)
tgPIDController(tgControllable *controllable, tgPIDController::Config config)