Move all floats to doubles
The code was developed initially around a pid loop implemented using
floats. Therefore, the code was converting back and forth between
double for sensor values as inputs and outputs from this PID loop.
Change-Id: I2d2919e1165103040729c9f16bb84fde3dd6b81b
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/pid/builderconfig.cpp b/pid/builderconfig.cpp
index 665e2dd..f85e1c1 100644
--- a/pid/builderconfig.cpp
+++ b/pid/builderconfig.cpp
@@ -92,10 +92,10 @@
/*
* Mysteriously if you use lookupValue on these, and the type
- * is float. It won't work right.
+ * is double. It won't work right.
*
* If the configuration file value doesn't look explicitly like
- * a float it won't let you assign it to one.
+ * a double it won't let you assign it to one.
*/
name = pid.lookup("name").c_str();
info.type = pid.lookup("type").c_str();
diff --git a/pid/controller.hpp b/pid/controller.hpp
index bf79937..5530843 100644
--- a/pid/controller.hpp
+++ b/pid/controller.hpp
@@ -15,9 +15,9 @@
{
virtual ~Controller() = default;
- virtual float inputProc(void) = 0;
+ virtual double inputProc(void) = 0;
- virtual void outputProc(float value) = 0;
+ virtual void outputProc(double value) = 0;
virtual void process(void) = 0;
diff --git a/pid/ec/pid.cpp b/pid/ec/pid.cpp
index c2236c2..1ded7ac 100644
--- a/pid/ec/pid.cpp
+++ b/pid/ec/pid.cpp
@@ -23,7 +23,7 @@
* clamp
*
*/
-static float clamp(float x, float min, float max)
+static double clamp(double x, double min, double max)
{
if (x < min)
{
@@ -40,15 +40,15 @@
* pid code
* Note: Codes assumes the ts field is non-zero
*/
-float pid(pid_info_t* pidinfoptr, float input, float setpoint)
+double pid(pid_info_t* pidinfoptr, double input, double setpoint)
{
- float error;
+ double error;
- float p_term;
- float i_term = 0.0f;
- float ff_term = 0.0f;
+ double p_term;
+ double i_term = 0.0f;
+ double ff_term = 0.0f;
- float output;
+ double output;
// calculate P, I, D, FF
@@ -79,7 +79,7 @@
if (pidinfoptr->slew_neg != 0.0f)
{
// Don't decrease too fast
- float min_out =
+ double min_out =
pidinfoptr->last_output + pidinfoptr->slew_neg * pidinfoptr->ts;
if (output < min_out)
{
@@ -89,7 +89,7 @@
if (pidinfoptr->slew_pos != 0.0f)
{
// Don't increase too fast
- float max_out =
+ double max_out =
pidinfoptr->last_output + pidinfoptr->slew_pos * pidinfoptr->ts;
if (output > max_out)
{
diff --git a/pid/ec/pid.hpp b/pid/ec/pid.hpp
index 6b1030a..779ced5 100644
--- a/pid/ec/pid.hpp
+++ b/pid/ec/pid.hpp
@@ -7,8 +7,8 @@
typedef struct
{
- float min;
- float max;
+ double min;
+ double max;
} limits_t;
/* Note: If you update these structs you need to update the copy code in
@@ -18,35 +18,35 @@
{
bool initialized; // has pid been initialized
- float ts; // sample time in seconds
- float integral; // intergal of error
- float last_output; // value of last output
+ double ts; // sample time in seconds
+ double integral; // intergal of error
+ double last_output; // value of last output
- float p_c; // coeff for P
- float i_c; // coeff for I
- float ff_off; // offset coeff for feed-forward term
- float ff_gain; // gain for feed-forward term
+ double p_c; // coeff for P
+ double i_c; // coeff for I
+ double ff_off; // offset coeff for feed-forward term
+ double ff_gain; // gain for feed-forward term
limits_t i_lim; // clamp of integral
limits_t out_lim; // clamp of output
- float slew_neg;
- float slew_pos;
+ double slew_neg;
+ double slew_pos;
} pid_info_t;
-float pid(pid_info_t* pidinfoptr, float input, float setpoint);
+double pid(pid_info_t* pidinfoptr, double input, double setpoint);
/* Condensed version for use by the configuration. */
struct pidinfo
{
- float ts; // sample time in seconds
- float p_c; // coeff for P
- float i_c; // coeff for I
- float ff_off; // offset coeff for feed-forward term
- float ff_gain; // gain for feed-forward term
+ double ts; // sample time in seconds
+ double p_c; // coeff for P
+ double i_c; // coeff for I
+ double ff_off; // offset coeff for feed-forward term
+ double ff_gain; // gain for feed-forward term
ec::limits_t i_lim; // clamp of integral
ec::limits_t out_lim; // clamp of output
- float slew_neg;
- float slew_pos;
+ double slew_neg;
+ double slew_pos;
};
} // namespace ec
diff --git a/pid/ec/stepwise.cpp b/pid/ec/stepwise.cpp
index 4a71532..0a5c0b0 100644
--- a/pid/ec/stepwise.cpp
+++ b/pid/ec/stepwise.cpp
@@ -22,10 +22,10 @@
namespace ec
{
-float stepwise(const ec::StepwiseInfo& info, float input)
+double stepwise(const ec::StepwiseInfo& info, double input)
{
- float value = info.output[0]; // if we are below the lowest
- // point, we set the lowest value
+ double value = info.output[0]; // if we are below the lowest
+ // point, we set the lowest value
for (size_t ii = 1; ii < ec::maxStepwisePoints; ii++)
{
diff --git a/pid/ec/stepwise.hpp b/pid/ec/stepwise.hpp
index 4034b47..bc7c204 100644
--- a/pid/ec/stepwise.hpp
+++ b/pid/ec/stepwise.hpp
@@ -26,13 +26,13 @@
struct StepwiseInfo
{
- float ts; // sample time in seconds
- float reading[maxStepwisePoints];
- float output[maxStepwisePoints];
- float positiveHysteresis;
- float negativeHysteresis;
+ double ts; // sample time in seconds
+ double reading[maxStepwisePoints];
+ double output[maxStepwisePoints];
+ double positiveHysteresis;
+ double negativeHysteresis;
};
-float stepwise(const ec::StepwiseInfo& info, float value);
+double stepwise(const ec::StepwiseInfo& info, double value);
} // namespace ec
\ No newline at end of file
diff --git a/pid/fancontroller.cpp b/pid/fancontroller.cpp
index ce08185..4a61def 100644
--- a/pid/fancontroller.cpp
+++ b/pid/fancontroller.cpp
@@ -39,7 +39,7 @@
return fan;
}
-float FanController::inputProc(void)
+double FanController::inputProc(void)
{
double value = 0;
std::vector<int64_t> values;
@@ -82,15 +82,15 @@
value = *result;
}
- return static_cast<float>(value);
+ return value;
}
-float FanController::setptProc(void)
+double FanController::setptProc(void)
{
- float maxRPM = _owner->getMaxRPMRequest();
+ double maxRPM = _owner->getMaxRPMRequest();
// store for reference, and check if more or less.
- float prev = getSetpoint();
+ double prev = getSetpoint();
if (maxRPM > prev)
{
@@ -110,9 +110,9 @@
return (maxRPM);
}
-void FanController::outputProc(float value)
+void FanController::outputProc(double value)
{
- float percent = value;
+ double percent = value;
/* If doing tuning logging, don't go into failsafe mode. */
#ifndef __TUNING_LOGGING__
@@ -133,7 +133,7 @@
for (const auto& it : _inputs)
{
auto sensor = _owner->getSensor(it);
- sensor->write(static_cast<double>(percent));
+ sensor->write(percent);
}
return;
diff --git a/pid/fancontroller.hpp b/pid/fancontroller.hpp
index 821c900..f3d3f4e 100644
--- a/pid/fancontroller.hpp
+++ b/pid/fancontroller.hpp
@@ -28,9 +28,9 @@
{
}
- float inputProc(void) override;
- float setptProc(void) override;
- void outputProc(float value) override;
+ double inputProc(void) override;
+ double setptProc(void) override;
+ void outputProc(double value) override;
FanSpeedDirection getFanDirection(void) const
{
diff --git a/pid/pidcontroller.cpp b/pid/pidcontroller.cpp
index cbe2452..7be6ceb 100644
--- a/pid/pidcontroller.cpp
+++ b/pid/pidcontroller.cpp
@@ -28,9 +28,9 @@
void PIDController::process(void)
{
- float input;
- float setpt;
- float output;
+ double input;
+ double setpt;
+ double output;
// Get setpt value
setpt = setptProc();
diff --git a/pid/pidcontroller.hpp b/pid/pidcontroller.hpp
index b7aaba6..9ed3be2 100644
--- a/pid/pidcontroller.hpp
+++ b/pid/pidcontroller.hpp
@@ -25,9 +25,9 @@
{
}
- virtual float inputProc(void) override = 0;
- virtual float setptProc(void) = 0;
- virtual void outputProc(float value) override = 0;
+ virtual double inputProc(void) override = 0;
+ virtual double setptProc(void) = 0;
+ virtual void outputProc(double value) override = 0;
void process(void) override;
@@ -35,11 +35,11 @@
{
return _id;
}
- float getSetpoint(void)
+ double getSetpoint(void)
{
return _setpoint;
}
- void setSetpoint(float setpoint)
+ void setSetpoint(double setpoint)
{
_setpoint = setpoint;
}
@@ -55,6 +55,6 @@
private:
// parameters
ec::pid_info_t _pid_info;
- float _setpoint;
+ double _setpoint;
std::string _id;
};
diff --git a/pid/stepwisecontroller.cpp b/pid/stepwisecontroller.cpp
index 875a470..b81f8b1 100644
--- a/pid/stepwisecontroller.cpp
+++ b/pid/stepwisecontroller.cpp
@@ -32,11 +32,11 @@
void StepwiseController::process(void)
{
// Get input value
- float input = inputProc();
+ double input = inputProc();
ec::StepwiseInfo info = get_stepwise_info();
- float output = lastOutput;
+ double output = lastOutput;
// Calculate new output if hysteresis allows
if (std::isnan(output))
@@ -81,13 +81,13 @@
return thermal;
}
-float StepwiseController::inputProc(void)
+double StepwiseController::inputProc(void)
{
double value = _owner->getCachedValue(_inputs.at(0));
- return static_cast<float>(value);
+ return value;
}
-void StepwiseController::outputProc(float value)
+void StepwiseController::outputProc(double value)
{
_owner->addRPMSetPoint(value);
diff --git a/pid/stepwisecontroller.hpp b/pid/stepwisecontroller.hpp
index 37606c3..4aa8116 100644
--- a/pid/stepwisecontroller.hpp
+++ b/pid/stepwisecontroller.hpp
@@ -26,9 +26,9 @@
{
}
- float inputProc(void) override;
+ double inputProc(void) override;
- void outputProc(float value) override;
+ void outputProc(double value) override;
void process(void) override;
@@ -50,6 +50,6 @@
ec::StepwiseInfo _stepwise_info;
std::string _id;
std::vector<std::string> _inputs;
- float lastInput = std::numeric_limits<float>::quiet_NaN();
- float lastOutput = std::numeric_limits<float>::quiet_NaN();
+ double lastInput = std::numeric_limits<double>::quiet_NaN();
+ double lastOutput = std::numeric_limits<double>::quiet_NaN();
};
diff --git a/pid/thermalcontroller.cpp b/pid/thermalcontroller.cpp
index 5388823..485688a 100644
--- a/pid/thermalcontroller.cpp
+++ b/pid/thermalcontroller.cpp
@@ -21,7 +21,7 @@
std::unique_ptr<PIDController> ThermalController::createThermalPid(
ZoneInterface* owner, const std::string& id,
- const std::vector<std::string>& inputs, float setpoint,
+ const std::vector<std::string>& inputs, double setpoint,
const ec::pidinfo& initial)
{
// ThermalController currently only supports precisely one input.
@@ -40,21 +40,21 @@
return thermal;
}
-// bmc_host_sensor_value_float
-float ThermalController::inputProc(void)
+// bmc_host_sensor_value_double
+double ThermalController::inputProc(void)
{
/*
* This only supports one thermal input because it doesn't yet know how to
* handle merging them, probably max?
*/
double value = _owner->getCachedValue(_inputs.at(0));
- return static_cast<float>(value);
+ return value;
}
// bmc_get_setpt
-float ThermalController::setptProc(void)
+double ThermalController::setptProc(void)
{
- float setpoint = getSetpoint();
+ double setpoint = getSetpoint();
/* TODO(venture): Thermal setpoint invalid? */
#if 0
@@ -71,7 +71,7 @@
}
// bmc_set_pid_output
-void ThermalController::outputProc(float value)
+void ThermalController::outputProc(double value)
{
_owner->addRPMSetPoint(value);
diff --git a/pid/thermalcontroller.hpp b/pid/thermalcontroller.hpp
index 040b222..8089da1 100644
--- a/pid/thermalcontroller.hpp
+++ b/pid/thermalcontroller.hpp
@@ -16,8 +16,8 @@
public:
static std::unique_ptr<PIDController>
createThermalPid(ZoneInterface* owner, const std::string& id,
- const std::vector<std::string>& inputs, float setpoint,
- const ec::pidinfo& initial);
+ const std::vector<std::string>& inputs,
+ double setpoint, const ec::pidinfo& initial);
ThermalController(const std::string& id,
const std::vector<std::string>& inputs,
@@ -27,9 +27,9 @@
{
}
- float inputProc(void) override;
- float setptProc(void) override;
- void outputProc(float value) override;
+ double inputProc(void) override;
+ double setptProc(void) override;
+ void outputProc(double value) override;
private:
std::vector<std::string> _inputs;
diff --git a/pid/zone.cpp b/pid/zone.cpp
index f04a68d..cc135bf 100644
--- a/pid/zone.cpp
+++ b/pid/zone.cpp
@@ -35,7 +35,7 @@
using tstamp = std::chrono::high_resolution_clock::time_point;
using namespace std::literals::chrono_literals;
-float PIDZone::getMaxRPMRequest(void) const
+double PIDZone::getMaxRPMRequest(void) const
{
return _maximumRPMSetPt;
}
@@ -61,7 +61,7 @@
return _zoneId;
}
-void PIDZone::addRPMSetPoint(float setpoint)
+void PIDZone::addRPMSetPoint(double setpoint)
{
_RPMSetPoints.push_back(setpoint);
}
@@ -71,12 +71,12 @@
_RPMSetPoints.clear();
}
-float PIDZone::getFailSafePercent(void) const
+double PIDZone::getFailSafePercent(void) const
{
return _failSafePercent;
}
-float PIDZone::getMinThermalRPMSetpoint(void) const
+double PIDZone::getMinThermalRPMSetpoint(void) const
{
return _minThermalRpmSetPt;
}
@@ -108,8 +108,8 @@
void PIDZone::determineMaxRPMRequest(void)
{
- float max = 0;
- std::vector<float>::iterator result;
+ double max = 0;
+ std::vector<double>::iterator result;
if (_RPMSetPoints.size() > 0)
{
@@ -140,7 +140,7 @@
ifs >> value;
/* expecting RPM set-point, not pwm% */
- max = static_cast<float>(value);
+ max = static_cast<double>(value);
}
}
catch (const std::exception& e)
diff --git a/pid/zone.hpp b/pid/zone.hpp
index 32b6e22..223f3cb 100644
--- a/pid/zone.hpp
+++ b/pid/zone.hpp
@@ -27,10 +27,10 @@
virtual ~ZoneInterface() = default;
virtual double getCachedValue(const std::string& name) = 0;
- virtual void addRPMSetPoint(float setpoint) = 0;
- virtual float getMaxRPMRequest() const = 0;
+ virtual void addRPMSetPoint(double setpoint) = 0;
+ virtual double getMaxRPMRequest() const = 0;
virtual bool getFailSafeMode() const = 0;
- virtual float getFailSafePercent() const = 0;
+ virtual double getFailSafePercent() const = 0;
virtual Sensor* getSensor(const std::string& name) = 0;
};
@@ -42,7 +42,7 @@
class PIDZone : public ZoneInterface, public ModeObject
{
public:
- PIDZone(int64_t zone, float minThermalRpm, float failSafePercent,
+ PIDZone(int64_t zone, double minThermalRpm, double failSafePercent,
const SensorManager& mgr, sdbusplus::bus::bus& bus,
const char* objPath, bool defer) :
ModeObject(bus, objPath, defer),
@@ -54,7 +54,7 @@
#endif
}
- float getMaxRPMRequest(void) const override;
+ double getMaxRPMRequest(void) const override;
bool getManualMode(void) const;
/* Could put lock around this since it's accessed from two threads, but
@@ -63,10 +63,10 @@
void setManualMode(bool mode);
bool getFailSafeMode(void) const override;
int64_t getZoneID(void) const;
- void addRPMSetPoint(float setpoint) override;
+ void addRPMSetPoint(double setpoint) override;
void clearRPMSetPoints(void);
- float getFailSafePercent(void) const override;
- float getMinThermalRPMSetpoint(void) const;
+ double getFailSafePercent(void) const override;
+ double getMinThermalRPMSetpoint(void) const;
Sensor* getSensor(const std::string& name) override;
void determineMaxRPMRequest(void);
@@ -99,14 +99,14 @@
#endif
const int64_t _zoneId;
- float _maximumRPMSetPt = 0;
+ double _maximumRPMSetPt = 0;
bool _manualMode = false;
- const float _minThermalRpmSetPt;
- const float _failSafePercent;
+ const double _minThermalRpmSetPt;
+ const double _failSafePercent;
std::set<std::string> _failSafeSensors;
- std::vector<float> _RPMSetPoints;
+ std::vector<double> _RPMSetPoints;
std::vector<std::string> _fanInputs;
std::vector<std::string> _thermalInputs;
std::map<std::string, double> _cachedValuesByName;