Skip to content

Commit 2e4a02f

Browse files
authored
Merge pull request #36 from urbytes21/dev_branch
id 1770361563
2 parents ed328a7 + 07d031f commit 2e4a02f

File tree

18 files changed

+877
-1
lines changed

18 files changed

+877
-1
lines changed

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ set(APP_SOURCES
120120
"src/patterns/creational/AbstractFactory.cpp"
121121
"src/patterns/creational/Builder.cpp"
122122
"src/patterns/creational/Prototype.cpp"
123+
## Exceptions
124+
"src/core/exception/BasicHandle.cpp"
125+
"src/core/exception/ThrowNoexcept.cpp"
123126
## Streams
124127
"src/core/filehandle/IOStream.cpp"
125128
"src/core/filehandle/StringStream.cpp"
@@ -137,6 +140,14 @@ set(APP_SOURCES
137140
"src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.cpp"
138141
"src/leetcode/arrays/longest_common_prefix/Solution.cpp"
139142
"src/leetcode/arrays/3sum/Solution.cpp"
143+
"src/leetcode/arrays/4sum/Solution.cpp"
144+
## Controller
145+
"src/controller/pid/pid.cpp"
146+
"src/controller/pid/PIDSim.cpp"
147+
## Smart Pointers
148+
"src/core/datatypes/smart_pointer/Unique.cpp"
149+
"src/core/datatypes/smart_pointer/Shared.cpp"
150+
"src/core/datatypes/smart_pointer/Weak.cpp"
140151
)
141152

142153
# Test files
@@ -147,6 +158,7 @@ set(APP_TESTS
147158
"tests/container_with_most_water_ut.cpp"
148159
"tests/longest_common_prefix_ut.cpp"
149160
"tests/three_sum_ut.cpp"
161+
"tests/four_sum_ut.cpp"
150162
)
151163

152164
# ----------------------------------------------------------------------------------------

src/controller/pid/PIDSim.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
#include "pid.h"
3+
4+
namespace {
5+
void run() {
6+
PID pid = PID(0.1, 100, -100, 0.1, 0.01, 0.5);
7+
double setpoint = 100;
8+
double val = 20;
9+
for (int i = 0; i < 200; i++) {
10+
double inc = pid.calculate(setpoint, val);
11+
std::cout << "setpoint:" << setpoint << " - val:" << val << " - inc:" << inc
12+
<< std::endl;
13+
val += inc;
14+
}
15+
}
16+
} // namespace
17+
18+
struct PIDSimRunner {
19+
PIDSimRunner() { run(); }
20+
};
21+
22+
static PIDSimRunner autoRunner;

src/controller/pid/pid.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* Copyright 2019 Bradley J. Snyder <snyder.bradleyj@gmail.com>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
23+
#ifndef _PID_SOURCE_
24+
#define _PID_SOURCE_
25+
26+
#include <iostream>
27+
#include <cmath>
28+
#include "pid.h"
29+
30+
using namespace std;
31+
32+
class PIDImpl
33+
{
34+
public:
35+
PIDImpl( double dt, double max, double min, double Kp, double Kd, double Ki );
36+
~PIDImpl();
37+
double calculate( double setpoint, double pv );
38+
39+
private:
40+
double _dt;
41+
double _max;
42+
double _min;
43+
double _Kp;
44+
double _Kd;
45+
double _Ki;
46+
double _pre_error;
47+
double _integral;
48+
};
49+
50+
51+
PID::PID( double dt, double max, double min, double Kp, double Kd, double Ki )
52+
{
53+
pimpl = new PIDImpl(dt,max,min,Kp,Kd,Ki);
54+
}
55+
double PID::calculate( double setpoint, double pv )
56+
{
57+
return pimpl->calculate(setpoint,pv);
58+
}
59+
PID::~PID()
60+
{
61+
delete pimpl;
62+
}
63+
64+
/**
65+
* Implementation
66+
*/
67+
PIDImpl::PIDImpl( double dt, double max, double min, double Kp, double Kd, double Ki ) :
68+
_dt(dt),
69+
_max(max),
70+
_min(min),
71+
_Kp(Kp),
72+
_Kd(Kd),
73+
_Ki(Ki),
74+
_pre_error(0),
75+
_integral(0)
76+
{
77+
}
78+
79+
double PIDImpl::calculate( double setpoint, double pv )
80+
{
81+
82+
// Calculate error
83+
double error = setpoint - pv;
84+
85+
// Proportional term
86+
double Pout = _Kp * error;
87+
88+
// Integral term
89+
_integral += error * _dt;
90+
double Iout = _Ki * _integral;
91+
92+
// Derivative term
93+
double derivative = (error - _pre_error) / _dt;
94+
double Dout = _Kd * derivative;
95+
96+
// Calculate total output
97+
double output = Pout + Iout + Dout;
98+
99+
// Restrict to max/min
100+
if( output > _max )
101+
output = _max;
102+
else if( output < _min )
103+
output = _min;
104+
105+
// Save error to previous error
106+
_pre_error = error;
107+
108+
return output;
109+
}
110+
111+
PIDImpl::~PIDImpl()
112+
{
113+
}
114+
115+
#endif

src/controller/pid/pid.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright 2019 Bradley J. Snyder <snyder.bradleyj@gmail.com>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
23+
#ifndef _PID_H_
24+
#define _PID_H_
25+
26+
class PIDImpl;
27+
class PID {
28+
public:
29+
// Kp - proportional gain
30+
// Ki - Integral gain
31+
// Kd - derivative gain
32+
// dt - loop interval time
33+
// max - maximum value of manipulated variable
34+
// min - minimum value of manipulated variable
35+
PID(double dt, double max, double min, double Kp, double Kd, double Ki);
36+
37+
// Returns the manipulated variable given a setpoint and current process value
38+
double calculate(double setpoint, double pv);
39+
~PID();
40+
41+
PID() = delete;
42+
PID(const PID& other) = delete;
43+
PID& operator=(const PID& other) = delete;
44+
45+
private:
46+
PIDImpl* pimpl;
47+
};
48+
49+
#endif

src/core/datatypes/TypeConVersions.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ void explicitConversion() {
6565
const void* pVoid = reinterpret_cast<const void*>(&pi);
6666
cout << "reinterpret_cast: address of pi = " << pVoid << "\n";
6767

68+
// ** Use case: Memory-Mapped I/O **
69+
// C
70+
// #define REG_ADDR 0x000fff01
71+
// volatile uint8_t* reg = reinterpret_cast<volatile uint8_t*>(REG_ADDR);
72+
// *reg = 0xF;
73+
// *reg = 0x1;
74+
75+
// C++
76+
// #include <cstdint>
77+
// constexpr std::uintptr_t REG_ADDR = 0x000fff02;
78+
// auto* const reg = reinterpret_cast<volatile uint8_t*>(REG_ADDR);
79+
// *reg = 0xF;
80+
81+
// *reg = 0x1;
82+
6883
// *5. dynamic_cast: safe cast between related classes (runtime checked)
6984
Base* basePtr = new Derived();
7085
const Derived* derivedPtr = dynamic_cast<Derived*>(basePtr);

0 commit comments

Comments
 (0)