Math: Add PidController class

This commit is contained in:
Jérôme Leclercq 2021-06-20 13:34:35 +02:00
parent 0eda2d0721
commit dbe4abefb8
3 changed files with 60 additions and 0 deletions

View File

@ -40,6 +40,7 @@
#include <Nazara/Math/Frustum.hpp> #include <Nazara/Math/Frustum.hpp>
#include <Nazara/Math/Matrix4.hpp> #include <Nazara/Math/Matrix4.hpp>
#include <Nazara/Math/OrientedBox.hpp> #include <Nazara/Math/OrientedBox.hpp>
#include <Nazara/Math/PidController.hpp>
#include <Nazara/Math/Plane.hpp> #include <Nazara/Math/Plane.hpp>
#include <Nazara/Math/Quaternion.hpp> #include <Nazara/Math/Quaternion.hpp>
#include <Nazara/Math/Ray.hpp> #include <Nazara/Math/Ray.hpp>

View File

@ -0,0 +1,31 @@
// Copyright (C) 2021 Jérôme Leclercq
// This file is part of the "Nazara Engine - Mathematics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_PIDCONTROLLER_HPP
#define NAZARA_PIDCONTROLLER_HPP
namespace Nz
{
template<typename T>
class PidController
{
public:
PidController(float p, float i, float d);
T Update(const T& currentError, float elapsedTime);
private:
T m_lastError;
T m_integral;
float m_dFactor;
float m_iFactor;
float m_pFactor;
};
}
#include <Nazara/Math/PidController.inl>
#endif

View File

@ -0,0 +1,28 @@
// Copyright (C) 2021 Jérôme Leclercq
// This file is part of the "Nazara Engine - Mathematics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Math/PidController.hpp>
namespace Nz
{
template<typename T>
PidController<T>::PidController(float p, float i, float d) :
m_lastError(0),
m_integral(0),
m_dFactor(d),
m_iFactor(i),
m_pFactor(p)
{
}
template<typename T>
T PidController<T>::Update(const T& currentError, float elapsedTime)
{
m_integral += currentError * elapsedTime;
T deriv = (currentError - m_lastError) / elapsedTime;
m_lastError = currentError;
return currentError * m_pFactor + m_integral * m_iFactor + deriv * m_dFactor;
}
}