Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ set(APP_SOURCES
"src/patterns/creational/AbstractFactory.cpp"
"src/patterns/creational/Builder.cpp"
"src/patterns/creational/Prototype.cpp"
## Exceptions
"src/core/exception/BasicHandle.cpp"
"src/core/exception/ThrowNoexcept.cpp"
## Streams
"src/core/filehandle/IOStream.cpp"
"src/core/filehandle/StringStream.cpp"
Expand All @@ -137,6 +140,14 @@ set(APP_SOURCES
"src/leetcode/arrays/container_with_most_water/ContainerWithMostWater.cpp"
"src/leetcode/arrays/longest_common_prefix/Solution.cpp"
"src/leetcode/arrays/3sum/Solution.cpp"
"src/leetcode/arrays/4sum/Solution.cpp"
## Controller
"src/controller/pid/pid.cpp"
"src/controller/pid/PIDSim.cpp"
## Smart Pointers
"src/core/datatypes/smart_pointer/Unique.cpp"
"src/core/datatypes/smart_pointer/Shared.cpp"
"src/core/datatypes/smart_pointer/Weak.cpp"
)

# Test files
Expand All @@ -147,6 +158,7 @@ set(APP_TESTS
"tests/container_with_most_water_ut.cpp"
"tests/longest_common_prefix_ut.cpp"
"tests/three_sum_ut.cpp"
"tests/four_sum_ut.cpp"
)

# ----------------------------------------------------------------------------------------
Expand Down
22 changes: 22 additions & 0 deletions src/controller/pid/PIDSim.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>
#include "pid.h"

namespace {
void run() {
PID pid = PID(0.1, 100, -100, 0.1, 0.01, 0.5);
double setpoint = 100;
double val = 20;
for (int i = 0; i < 200; i++) {
double inc = pid.calculate(setpoint, val);
std::cout << "setpoint:" << setpoint << " - val:" << val << " - inc:" << inc
<< std::endl;
val += inc;
}
}
} // namespace

struct PIDSimRunner {
PIDSimRunner() { run(); }
};

static PIDSimRunner autoRunner;
115 changes: 115 additions & 0 deletions src/controller/pid/pid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* Copyright 2019 Bradley J. Snyder <snyder.bradleyj@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef _PID_SOURCE_
#define _PID_SOURCE_

#include <iostream>
#include <cmath>
#include "pid.h"

using namespace std;

class PIDImpl
{
public:
PIDImpl( double dt, double max, double min, double Kp, double Kd, double Ki );
~PIDImpl();
double calculate( double setpoint, double pv );

private:
double _dt;
double _max;
double _min;
double _Kp;
double _Kd;
double _Ki;
double _pre_error;
double _integral;
};


PID::PID( double dt, double max, double min, double Kp, double Kd, double Ki )
{
pimpl = new PIDImpl(dt,max,min,Kp,Kd,Ki);
}
double PID::calculate( double setpoint, double pv )
{
return pimpl->calculate(setpoint,pv);
}
PID::~PID()
{
delete pimpl;
}

/**
* Implementation
*/
PIDImpl::PIDImpl( double dt, double max, double min, double Kp, double Kd, double Ki ) :
_dt(dt),
_max(max),
_min(min),
_Kp(Kp),
_Kd(Kd),
_Ki(Ki),
_pre_error(0),
_integral(0)
{
}

double PIDImpl::calculate( double setpoint, double pv )
{

// Calculate error
double error = setpoint - pv;

// Proportional term
double Pout = _Kp * error;

// Integral term
_integral += error * _dt;
double Iout = _Ki * _integral;

// Derivative term
double derivative = (error - _pre_error) / _dt;
double Dout = _Kd * derivative;

// Calculate total output
double output = Pout + Iout + Dout;

// Restrict to max/min
if( output > _max )
output = _max;
else if( output < _min )
output = _min;

// Save error to previous error
_pre_error = error;

return output;
}

PIDImpl::~PIDImpl()
{
}

#endif
49 changes: 49 additions & 0 deletions src/controller/pid/pid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright 2019 Bradley J. Snyder <snyder.bradleyj@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef _PID_H_
#define _PID_H_

class PIDImpl;
class PID {
public:
// Kp - proportional gain
// Ki - Integral gain
// Kd - derivative gain
// dt - loop interval time
// max - maximum value of manipulated variable
// min - minimum value of manipulated variable
PID(double dt, double max, double min, double Kp, double Kd, double Ki);

// Returns the manipulated variable given a setpoint and current process value
double calculate(double setpoint, double pv);
~PID();

PID() = delete;
PID(const PID& other) = delete;
PID& operator=(const PID& other) = delete;

private:
PIDImpl* pimpl;
};

#endif
15 changes: 15 additions & 0 deletions src/core/datatypes/TypeConVersions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ void explicitConversion() {
const void* pVoid = reinterpret_cast<const void*>(&pi);
cout << "reinterpret_cast: address of pi = " << pVoid << "\n";

// ** Use case: Memory-Mapped I/O **
// C
// #define REG_ADDR 0x000fff01
// volatile uint8_t* reg = reinterpret_cast<volatile uint8_t*>(REG_ADDR);
// *reg = 0xF;
// *reg = 0x1;

// C++
// #include <cstdint>
// constexpr std::uintptr_t REG_ADDR = 0x000fff02;
// auto* const reg = reinterpret_cast<volatile uint8_t*>(REG_ADDR);
// *reg = 0xF;

// *reg = 0x1;

// *5. dynamic_cast: safe cast between related classes (runtime checked)
Base* basePtr = new Derived();
const Derived* derivedPtr = dynamic_cast<Derived*>(basePtr);
Expand Down
Loading