Fang-Robotics-MCB
Fang Robotics Team Codebase
Loading...
Searching...
No Matches
concurrent_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) 2024-2025 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_CONCURRENT_COMMAND_HPP_
25#define TAPROOT_CONCURRENT_COMMAND_HPP_
26
27#include <array>
28
29#include "modm/architecture/interface/assert.hpp"
30
31#include "command.hpp"
33
34namespace tap
35{
36namespace control
37{
44template <size_t COMMANDS, bool RACE>
46{
47public:
48 ConcurrentTemplateCommand(std::array<Command*, COMMANDS> commands, const char* name)
49 : Command(),
50 commands(commands),
51 name(name),
52 finishedCommands(0),
53 allCommands(0)
54 {
55 for (Command* command : commands)
56 {
57 modm_assert(
58 command != nullptr,
59 "ConcurrentCommand::ConcurrentCommand",
60 "Null pointer command passed into concurrent command.");
61 auto requirements = command->getRequirementsBitwise();
62 modm_assert(
63 (this->commandRequirementsBitwise & requirements) == 0,
64 "ConcurrentCommand::ConcurrentCommand",
65 "Multiple commands to concurrent command have overlapping requirements.");
66 this->commandRequirementsBitwise |= requirements;
67 this->allCommands |= (1ull << command->getGlobalIdentifier());
68 }
69 }
70
71 const char* getName() const override { return this->name; }
72
73 bool isReady() override
74 {
75 for (Command* command : commands)
76 {
77 if (!command->isReady())
78 {
79 return false;
80 }
81 }
82 return true;
83 }
84
85 void initialize() override
86 {
87 for (Command* command : commands)
88 {
89 command->initialize();
90 }
91 }
92
93 void execute() override
94 {
95 for (Command* command : commands)
96 {
97 if (!(this->finishedCommands & (1ull << command->getGlobalIdentifier())))
98 {
99 command->execute();
100 if (command->isFinished())
101 {
102 command->end(false);
103 this->finishedCommands |= 1ull << command->getGlobalIdentifier();
104 }
105 }
106 }
107 }
108
109 void end(bool interrupted) override
110 {
111 for (Command* command : commands)
112 {
113 if (!(this->finishedCommands & (1ull << command->getGlobalIdentifier())))
114 {
115 if (RACE)
116 {
117 command->end(true);
118 }
119 else
120 {
121 command->end(interrupted);
122 }
123 }
124 }
125 }
126
127 bool isFinished() const override
128 {
129 if (RACE)
130 {
131 return this->finishedCommands != 0;
132 }
133 return this->finishedCommands == this->allCommands;
134 }
135
136private:
137 std::array<Command*, COMMANDS> commands;
138 const char* name;
139 command_scheduler_bitmap_t finishedCommands;
140 command_scheduler_bitmap_t allCommands;
141}; // class ConcurrentTemplateCommand
142
146template <size_t COMMANDS>
148
152template <size_t COMMANDS>
154
155} // namespace control
156
157} // namespace tap
158
159#endif // TAPROOT_CONCURRENT_COMMAND_HPP_
Definition command.hpp:44
command_scheduler_bitmap_t commandRequirementsBitwise
Definition command.hpp:139
Definition concurrent_command.hpp:46
void execute() override
Definition concurrent_command.hpp:93
bool isFinished() const override
Definition concurrent_command.hpp:127
void initialize() override
Definition concurrent_command.hpp:85
ConcurrentTemplateCommand(std::array< Command *, COMMANDS > commands, const char *name)
Definition concurrent_command.hpp:48
bool isReady() override
Definition concurrent_command.hpp:73
const char * getName() const override
Definition concurrent_command.hpp:71
void end(bool interrupted) override
Definition concurrent_command.hpp:109
uint64_t command_scheduler_bitmap_t
Definition command_scheduler_types.hpp:31
Definition ballistics.cpp:29