rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
closed_loop_controller.h
Go to the documentation of this file.
1/**
2 * @file closed_loop_controller.h
3 */
4
5#pragma once
6
7#include <rusefi/expected.h>
8
9template <typename TInput, typename TOutput>
11public:
12 void update() {
13 expected<TOutput> outputValue = getOutput();
14 setOutput(outputValue);
15 }
16
17private:
18 expected<TOutput> getOutput() {
19 expected<TInput> setpoint = getSetpoint();
20 // If we don't know the setpoint, return failure.
21 if (!setpoint) {
22 return unexpected;
23 }
24
25 expected<TInput> observation = observePlant();
26 // If we couldn't observe the plant, return failure.
27 if (!observation) {
28 return unexpected;
29 }
30
31 expected<TOutput> openLoopResult = getOpenLoop(setpoint.Value);
32 // If we couldn't compute open loop, return failure.
33 if (!openLoopResult) {
34 return unexpected;
35 }
36
37 expected<TOutput> closedLoopResult = getClosedLoop(setpoint.Value, observation.Value);
38 // If we couldn't compute closed loop, return failure.
39 if (!closedLoopResult) {
40 return unexpected;
41 }
42
43 return openLoopResult.Value + closedLoopResult.Value;
44 }
45
46 // Get the setpoint: where should the controller put the plant?
47 virtual expected<TInput> getSetpoint() = 0;
48
49 // Get the current observation: what is the current state of the world?
50 virtual expected<TInput> observePlant() = 0;
51
52 // Get the open-loop output: output state based on only the setpoint
53 virtual expected<TOutput> getOpenLoop(TInput setpoint) = 0;
54
55 // Get the closed-loop output: output state based on setpoint and observation
56 virtual expected<TOutput> getClosedLoop(TInput setpoint, TInput observation) = 0;
57
58 // Set the output: Drive whatever output system will perturb the plant in the real world
59 virtual void setOutput(expected<TOutput> outputValue) = 0;
60};
virtual expected< TOutput > getOpenLoop(TInput setpoint)=0
virtual expected< TInput > observePlant()=0
virtual expected< TInput > getSetpoint()=0
expected< TOutput > getOutput()
virtual void setOutput(expected< TOutput > outputValue)=0
virtual expected< TOutput > getClosedLoop(TInput setpoint, TInput observation)=0