LightLib
PROS library for VEX V5: EKF/MCL localization, RAMSETE path following, high-level chassis API
Loading...
Searching...
No Matches
controllerRunner.hpp
Go to the documentation of this file.
1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6#pragma once
7
14#include <memory>
15
16namespace okapi {
17template <typename Input, typename Output> class ControllerRunner {
18 public:
25 explicit ControllerRunner(const TimeUtil &itimeUtil,
26 const std::shared_ptr<Logger> &ilogger = Logger::getDefaultLogger())
27 : logger(ilogger), rate(itimeUtil.getRate()) {
28 }
29
37 virtual Output runUntilSettled(const Input itarget, AsyncController<Input, Output> &icontroller) {
38 LOG_INFO("ControllerRunner: runUntilSettled(AsyncController): Set target to " +
39 std::to_string(itarget));
40 icontroller.setTarget(itarget);
41
42 while (!icontroller.isSettled()) {
43 rate->delayUntil(10_ms);
44 }
45
46 LOG_INFO("ControllerRunner: runUntilSettled(AsyncController): Done waiting to settle");
47 return icontroller.getError();
48 }
49
58 virtual Output runUntilSettled(const Input itarget,
60 ControllerOutput<Output> &ioutput) {
61 LOG_INFO("ControllerRunner: runUntilSettled(IterativeController): Set target to " +
62 std::to_string(itarget));
63 icontroller.setTarget(itarget);
64
65 while (!icontroller.isSettled()) {
66 ioutput.controllerSet(icontroller.getOutput());
67 rate->delayUntil(10_ms);
68 }
69
70 LOG_INFO("ControllerRunner: runUntilSettled(IterativeController): Done waiting to settle");
71 return icontroller.getError();
72 }
73
81 virtual Output runUntilAtTarget(const Input itarget,
82 AsyncController<Input, Output> &icontroller) {
83 LOG_INFO("ControllerRunner: runUntilAtTarget(AsyncController): Set target to " +
84 std::to_string(itarget));
85 icontroller.setTarget(itarget);
86
87 double error = icontroller.getError();
88 double lastError = error;
89 while (error != 0 && std::copysign(1.0, error) == std::copysign(1.0, lastError)) {
90 lastError = error;
91 rate->delayUntil(10_ms);
92 error = icontroller.getError();
93 }
94
95 LOG_INFO("ControllerRunner: runUntilAtTarget(AsyncController): Done waiting to settle");
96 return icontroller.getError();
97 }
98
107 virtual Output runUntilAtTarget(const Input itarget,
109 ControllerOutput<Output> &ioutput) {
110 LOG_INFO("ControllerRunner: runUntilAtTarget(IterativeController): Set target to " +
111 std::to_string(itarget));
112 icontroller.setTarget(itarget);
113
114 double error = icontroller.getError();
115 double lastError = error;
116 while (error != 0 && std::copysign(1.0, error) == std::copysign(1.0, lastError)) {
117 ioutput.controllerSet(icontroller.getOutput());
118 lastError = error;
119 rate->delayUntil(10_ms);
120 error = icontroller.getError();
121 }
122
123 LOG_INFO("ControllerRunner: runUntilAtTarget(IterativeController): Done waiting to settle");
124 return icontroller.getError();
125 }
126
127 protected:
128 std::shared_ptr<Logger> logger;
129 std::unique_ptr<AbstractRate> rate;
130};
131} // namespace okapi
Closed-loop controller that steps on its own in another thread and automatically writes to the output...
virtual Output getError() const =0
Returns the last error of the controller.
virtual void setTarget(Input itarget)=0
Sets the target for the controller.
virtual bool isSettled()=0
Returns whether the controller has settled at the target.
virtual void controllerSet(T ivalue)=0
Writes the value of the controller output.
std::shared_ptr< Logger > logger
ControllerRunner(const TimeUtil &itimeUtil, const std::shared_ptr< Logger > &ilogger=Logger::getDefaultLogger())
A utility class that runs a closed-loop controller.
virtual Output runUntilSettled(const Input itarget, AsyncController< Input, Output > &icontroller)
Runs the controller until it has settled.
virtual Output runUntilAtTarget(const Input itarget, AsyncController< Input, Output > &icontroller)
Runs the controller until it has reached its target, but not necessarily settled.
virtual Output runUntilSettled(const Input itarget, IterativeController< Input, Output > &icontroller, ControllerOutput< Output > &ioutput)
Runs the controller until it has settled.
std::unique_ptr< AbstractRate > rate
virtual Output runUntilAtTarget(const Input itarget, IterativeController< Input, Output > &icontroller, ControllerOutput< Output > &ioutput)
Runs the controller until it has reached its target, but not necessarily settled.
Closed-loop controller that steps iteratively using the step method below.
virtual Output getOutput() const =0
Returns the last calculated output of the controller.
static std::shared_ptr< Logger > getDefaultLogger()
Utility class for holding an AbstractTimer, AbstractRate, and SettledUtil together in one class since...
Definition timeUtil.hpp:18
#define LOG_INFO(msg)
Definition logging.hpp:20