Fang-Robotics-MCB
Fang Robotics Team Codebase
Loading...
Searching...
No Matches
governor_with_fallback_command.hpp
Go to the documentation of this file.
1/*****************************************************************************/
2/********** !!! WARNING: CODE GENERATED BY TAPROOT. DO NOT EDIT !!! **********/
3/*****************************************************************************/
4
5/*
6 * Copyright (c) 2022 Advanced Robotics at the University of Washington <robomstr@uw.edu>
7 *
8 * This file is part of Taproot.
9 *
10 * Taproot is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Taproot is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Taproot. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#ifndef TAPROOT_GOVERNOR_WITH_FALLBACK_COMMAND_HPP_
25#define TAPROOT_GOVERNOR_WITH_FALLBACK_COMMAND_HPP_
26
27#include <algorithm>
28#include <array>
29#include <cassert>
30#include <cinttypes>
31#include <vector>
32
33#include "../command.hpp"
34
36
38{
46template <std::size_t NUM_CONDITIONS>
48{
49public:
51 std::vector<Subsystem *> subRequirements,
52 Command &commandWhenGovernorsReady,
53 Command &fallbackCommand,
54 const std::array<CommandGovernorInterface *, NUM_CONDITIONS> &commandGovernorList,
55 const bool stopFallbackCommandIfGovernorsReady = false)
56 : commandWhenGovernorsReady(commandWhenGovernorsReady),
57 fallbackCommand(fallbackCommand),
58 commandGovernorList(commandGovernorList),
59 stopFallbackCommandIfGovernorsReady(stopFallbackCommandIfGovernorsReady)
60 {
61 std::for_each(subRequirements.begin(), subRequirements.end(), [&](auto sub) {
62 addSubsystemRequirement(sub);
63 });
64
65 assert(
66 commandWhenGovernorsReady.getRequirementsBitwise() == this->getRequirementsBitwise());
67 assert(fallbackCommand.getRequirementsBitwise() == this->getRequirementsBitwise());
68 }
69
70 const char *getName() const override
71 {
72 return governedCommandSelected ? commandWhenGovernorsReady.getName()
73 : fallbackCommand.getName();
74 }
75
76 bool isReady() override
77 {
78 governedCommandSelected = checkGovernorReadiness();
79
80 return (governedCommandSelected && commandWhenGovernorsReady.isReady()) ||
81 (!governedCommandSelected && fallbackCommand.isReady());
82 }
83
84 void initialize() override
85 {
86 if (governedCommandSelected)
87 {
88 commandWhenGovernorsReady.initialize();
89 }
90 else
91 {
92 fallbackCommand.initialize();
93 }
94 }
95
96 void execute() override
97 {
98 if (governedCommandSelected)
99 {
100 commandWhenGovernorsReady.execute();
101 }
102 else
103 {
104 fallbackCommand.execute();
105 }
106 }
107
108 void end(bool interrupted) override
109 {
110 if (governedCommandSelected)
111 {
112 commandWhenGovernorsReady.end(interrupted);
113 }
114 else
115 {
116 fallbackCommand.end(interrupted);
117 }
118 }
119
120 bool isFinished() const override
121 {
122 if (governedCommandSelected)
123 {
124 return commandWhenGovernorsReady.isFinished() || checkAnyGovernorFinished();
125 }
126 return fallbackCommand.isFinished() ||
127 (stopFallbackCommandIfGovernorsReady && checkGovernorReadiness());
128 }
129
130private:
131 bool governedCommandSelected = false;
132 Command &commandWhenGovernorsReady;
133 Command &fallbackCommand;
134
135 std::array<CommandGovernorInterface *, NUM_CONDITIONS> commandGovernorList;
136 const bool stopFallbackCommandIfGovernorsReady;
137
138 bool checkGovernorReadiness() const
139 {
140 return std::all_of(
141 commandGovernorList.begin(),
142 commandGovernorList.end(),
143 [](auto governor) { return governor->isReady(); });
144 }
145
146 bool checkAnyGovernorFinished() const
147 {
148 return std::any_of(
149 commandGovernorList.begin(),
150 commandGovernorList.end(),
151 [](auto governor) { return governor->isFinished(); });
152 }
153};
154} // namespace tap::control::governor
155
156#endif // TAPROOT_GOVERNOR_WITH_FALLBACK_COMMAND_HPP_
Definition command.hpp:44
virtual void initialize()=0
virtual bool isFinished() const =0
virtual void execute()=0
virtual const char * getName() const =0
mockable subsystem_scheduler_bitmap_t getRequirementsBitwise() const
Definition command.hpp:64
virtual bool isReady()
Definition command.cpp:46
virtual void end(bool interrupted)=0
Definition governor_with_fallback_command.hpp:48
void execute() override
Definition governor_with_fallback_command.hpp:96
GovernorWithFallbackCommand(std::vector< Subsystem * > subRequirements, Command &commandWhenGovernorsReady, Command &fallbackCommand, const std::array< CommandGovernorInterface *, NUM_CONDITIONS > &commandGovernorList, const bool stopFallbackCommandIfGovernorsReady=false)
Definition governor_with_fallback_command.hpp:50
const char * getName() const override
Definition governor_with_fallback_command.hpp:70
void end(bool interrupted) override
Definition governor_with_fallback_command.hpp:108
bool isFinished() const override
Definition governor_with_fallback_command.hpp:120
bool isReady() override
Definition governor_with_fallback_command.hpp:76
void initialize() override
Definition governor_with_fallback_command.hpp:84
Definition command_governor_interface.hpp:28