blob: 9dc433aed7a4d62b8cfbefd38d9500aae768ad5f [file] [log] [blame]
Patrick Ventured8012182018-03-08 08:21:38 -08001#pragma once
2
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07003#include "conf.hpp"
4#include "controller.hpp"
James Feist22c257a2018-08-31 14:07:12 -07005#include "pidcontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07006#include "sensors/manager.hpp"
7#include "sensors/sensor.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -08008#include "tuning.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07009
Patrick Venturea83a3ec2020-08-04 09:52:05 -070010#include <sdbusplus/bus.hpp>
11#include <sdbusplus/server.hpp>
12#include <xyz/openbmc_project/Control/Mode/server.hpp>
13
Patrick Ventured8012182018-03-08 08:21:38 -080014#include <fstream>
15#include <map>
16#include <memory>
17#include <set>
18#include <string>
19#include <vector>
20
Patrick Ventured8012182018-03-08 08:21:38 -080021template <typename... T>
22using ServerObject = typename sdbusplus::server::object::object<T...>;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070023using ModeInterface = sdbusplus::xyz::openbmc_project::Control::server::Mode;
Patrick Ventured8012182018-03-08 08:21:38 -080024using ModeObject = ServerObject<ModeInterface>;
25
Patrick Venturea0764872020-08-08 07:48:43 -070026namespace pid_control
27{
28
Patrick Venturea58197c2018-06-11 15:29:45 -070029class ZoneInterface
30{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070031 public:
32 virtual ~ZoneInterface() = default;
Patrick Venturea58197c2018-06-11 15:29:45 -070033
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070034 virtual double getCachedValue(const std::string& name) = 0;
Patrick Venture9bbf3332019-07-16 10:50:37 -070035 virtual void addSetPoint(double setpoint) = 0;
James Feist608304d2019-02-25 10:01:42 -080036 virtual void addRPMCeiling(double ceiling) = 0;
Patrick Venturef7a2dd52019-07-16 14:31:13 -070037 virtual double getMaxSetPointRequest() const = 0;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070038 virtual bool getFailSafeMode() const = 0;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080039 virtual double getFailSafePercent() const = 0;
Patrick Venture2d8e7852018-10-30 19:14:07 -070040 virtual Sensor* getSensor(const std::string& name) = 0;
Patrick Venturea58197c2018-06-11 15:29:45 -070041};
42
Patrick Ventured8012182018-03-08 08:21:38 -080043/*
44 * The PIDZone inherits from the Mode object so that it can listen for control
45 * mode changes. It primarily holds all PID loops and holds the sensor value
46 * cache that's used per iteration of the PID loops.
47 */
Patrick Venturea58197c2018-06-11 15:29:45 -070048class PIDZone : public ZoneInterface, public ModeObject
Patrick Ventured8012182018-03-08 08:21:38 -080049{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070050 public:
James Feist3484bed2019-02-25 13:28:18 -080051 PIDZone(int64_t zone, double minThermalOutput, double failSafePercent,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070052 const SensorManager& mgr, sdbusplus::bus::bus& bus,
53 const char* objPath, bool defer) :
54 ModeObject(bus, objPath, defer),
Patrick Venturef7a2dd52019-07-16 14:31:13 -070055 _zoneId(zone), _maximumSetPoint(),
James Feist3484bed2019-02-25 13:28:18 -080056 _minThermalOutputSetPt(minThermalOutput),
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070057 _failSafePercent(failSafePercent), _mgr(mgr)
58 {
Patrick Venturede79ee02019-05-08 14:50:00 -070059 if (loggingEnabled)
Patrick Venturec32e3fc2019-02-28 10:01:11 -080060 {
Patrick Venture89002db2019-05-08 15:02:55 -070061 _log.open(loggingPath + "/zone_" + std::to_string(zone) + ".log");
Patrick Venturec32e3fc2019-02-28 10:01:11 -080062 }
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070063 }
Patrick Ventured8012182018-03-08 08:21:38 -080064
Patrick Venturef7a2dd52019-07-16 14:31:13 -070065 double getMaxSetPointRequest(void) const override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070066 bool getManualMode(void) const;
Patrick Ventured8012182018-03-08 08:21:38 -080067
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070068 /* Could put lock around this since it's accessed from two threads, but
69 * only one reader/one writer.
70 */
71 void setManualMode(bool mode);
72 bool getFailSafeMode(void) const override;
Patrick Venture0bbeaf82018-10-30 18:50:31 -070073 int64_t getZoneID(void) const;
Patrick Venture9bbf3332019-07-16 10:50:37 -070074 void addSetPoint(double setpoint) override;
James Feist608304d2019-02-25 10:01:42 -080075 void addRPMCeiling(double ceiling) override;
Patrick Venture9bbf3332019-07-16 10:50:37 -070076 void clearSetPoints(void);
James Feist608304d2019-02-25 10:01:42 -080077 void clearRPMCeilings(void);
Patrick Venture5f59c0f2018-11-11 12:55:14 -080078 double getFailSafePercent(void) const override;
Patrick Venturef7a2dd52019-07-16 14:31:13 -070079 double getMinThermalSetpoint(void) const;
Patrick Ventured8012182018-03-08 08:21:38 -080080
Patrick Venture2d8e7852018-10-30 19:14:07 -070081 Sensor* getSensor(const std::string& name) override;
Patrick Venturef7a2dd52019-07-16 14:31:13 -070082 void determineMaxSetPointRequest(void);
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070083 void updateFanTelemetry(void);
84 void updateSensors(void);
85 void initializeCache(void);
86 void dumpCache(void);
Patrick Venture563a3562018-10-30 09:31:26 -070087 void processFans(void);
88 void processThermals(void);
Patrick Ventured8012182018-03-08 08:21:38 -080089
James Feist22c257a2018-08-31 14:07:12 -070090 void addFanPID(std::unique_ptr<Controller> pid);
91 void addThermalPID(std::unique_ptr<Controller> pid);
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070092 double getCachedValue(const std::string& name) override;
Patrick Venturec399f6f2018-10-30 19:24:47 -070093 void addFanInput(const std::string& fan);
94 void addThermalInput(const std::string& therm);
Patrick Ventured8012182018-03-08 08:21:38 -080095
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070096 void initializeLog(void);
97 std::ofstream& getLogHandle(void);
Patrick Ventured8012182018-03-08 08:21:38 -080098
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070099 /* Method for setting the manual mode over dbus */
100 bool manual(bool value) override;
101 /* Method for reading whether in fail-safe mode over dbus */
102 bool failSafe() const override;
Patrick Ventured8012182018-03-08 08:21:38 -0800103
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700104 private:
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700105 std::ofstream _log;
Patrick Ventured8012182018-03-08 08:21:38 -0800106
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700107 const int64_t _zoneId;
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700108 double _maximumSetPoint = 0;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700109 bool _manualMode = false;
James Feist3484bed2019-02-25 13:28:18 -0800110 const double _minThermalOutputSetPt;
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800111 const double _failSafePercent;
Patrick Ventured8012182018-03-08 08:21:38 -0800112
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700113 std::set<std::string> _failSafeSensors;
Patrick Ventured8012182018-03-08 08:21:38 -0800114
Patrick Venture9bbf3332019-07-16 10:50:37 -0700115 std::vector<double> _SetPoints;
James Feist608304d2019-02-25 10:01:42 -0800116 std::vector<double> _RPMCeilings;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700117 std::vector<std::string> _fanInputs;
118 std::vector<std::string> _thermalInputs;
119 std::map<std::string, double> _cachedValuesByName;
120 const SensorManager& _mgr;
Patrick Ventured8012182018-03-08 08:21:38 -0800121
James Feist22c257a2018-08-31 14:07:12 -0700122 std::vector<std::unique_ptr<Controller>> _fans;
123 std::vector<std::unique_ptr<Controller>> _thermals;
Patrick Ventured8012182018-03-08 08:21:38 -0800124};
Patrick Venturea0764872020-08-08 07:48:43 -0700125
126} // namespace pid_control