pid.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <ros/ros.h>
4 
5 class PID
6 {
7 public:
8  PID(
9  float kp,
10  float kd,
11  float ki,
12  float minOutput,
13  float maxOutput,
14  float integratorMin,
15  float integratorMax,
16  const std::string& name)
17  : m_kp(kp)
18  , m_kd(kd)
19  , m_ki(ki)
20  , m_minOutput(minOutput)
21  , m_maxOutput(maxOutput)
22  , m_integratorMin(integratorMin)
23  , m_integratorMax(integratorMax)
24  , m_integral(0)
25  , m_previousError(0)
26  , m_previousTime(ros::Time::now())
27  {
28  }
29 
30  void reset()
31  {
32  m_integral = 0;
33  m_previousError = 0;
35  }
36 
37  void setIntegral(float integral)
38  {
39  m_integral = integral;
40  }
41 
42  float ki() const
43  {
44  return m_ki;
45  }
46 
47  float update(float value, float targetValue)
48  {
49  ros::Time time = ros::Time::now();
50  float dt = time.toSec() - m_previousTime.toSec();
51  float error = targetValue - value;
52  m_integral += error * dt;
53  m_integral = std::max(std::min(m_integral, m_integratorMax), m_integratorMin);
54  float p = m_kp * error;
55  float d = 0;
56  if (dt > 0)
57  {
58  d = m_kd * (error - m_previousError) / dt;
59  }
60  float i = m_ki * m_integral;
61  float output = p + d + i;
62  m_previousError = error;
63  m_previousTime = time;
64  // self.pubOutput.publish(output)
65  // self.pubError.publish(error)
66  // self.pubP.publish(p)
67  // self.pubD.publish(d)
68  // self.pubI.publish(i)
69  return std::max(std::min(output, m_maxOutput), m_minOutput);
70  }
71 
72 private:
73  float m_kp;
74  float m_kd;
75  float m_ki;
76  float m_minOutput;
77  float m_maxOutput;
80  float m_integral;
83 };
float m_ki
Definition: pid.hpp:75
d
float m_integratorMax
Definition: pid.hpp:79
float update(float value, float targetValue)
Definition: pid.hpp:47
float m_minOutput
Definition: pid.hpp:76
PID(float kp, float kd, float ki, float minOutput, float maxOutput, float integratorMin, float integratorMax, const std::string &name)
Definition: pid.hpp:8
Definition: pid.hpp:5
void reset()
Definition: pid.hpp:30
ros::Time m_previousTime
Definition: pid.hpp:82
float m_kp
Definition: pid.hpp:73
float m_kd
Definition: pid.hpp:74
float m_maxOutput
Definition: pid.hpp:77
float m_previousError
Definition: pid.hpp:81
static Time now()
float ki() const
Definition: pid.hpp:42
float m_integratorMin
Definition: pid.hpp:78
void setIntegral(float integral)
Definition: pid.hpp:37
float m_integral
Definition: pid.hpp:80


crazyflie_controller
Author(s): Wolfgang Hoenig
autogenerated on Fri Nov 1 2019 03:39:55