Valkyrie 2026
Loading...
Searching...
No Matches
CommandGraph.h
1#pragma once
2
3#include <functional>
4#include <memory>
5#include <vector>
6
7#include <frc2/command/CommandPtr.h>
8#include <frc2/command/Commands.h>
9#include <units/time.h>
10
11namespace valor {
12
13class CommandGraph {
14 public:
15 struct CommandNode;
16
17 enum class EdgeType { SEQUENTIAL, PARALLEL, DEADLINE, RACE, DELAYED, CONDITIONAL, BRANCH, INTERRUPT };
18
19 struct Edge {
20 EdgeType type;
21 CommandNode* target;
22
23 std::function<bool()> condition = nullptr;
24 units::second_t delay = 0_s;
25
26 CommandNode* altTarget = nullptr;
27 };
28
29 struct CommandNode {
30 frc2::CommandPtr command = frc2::cmd::None();
31 std::vector<Edge> edges;
32 };
33
34 CommandGraph();
35 CommandNode* CreateNode(frc2::CommandPtr cmd);
36
37 void AddSequential(CommandNode* from, CommandNode* to);
38 void AddParallel(CommandNode* from, CommandNode* to);
39 void AddDeadline(CommandNode* from, CommandNode* to);
40 void AddRace(CommandNode* from, CommandNode* to);
41 void AddDelayed(CommandNode* from, CommandNode* to, units::second_t delay);
42 void AddConditional(CommandNode* from, CommandNode* to, std::function<bool()> condition);
43 void AddBranch(CommandNode* from, CommandNode* ifTrue, CommandNode* ifFalse, std::function<bool()> condition);
44 void AddInterrupt(CommandNode* from, std::function<bool()> interruptCondition);
45 frc2::CommandPtr Build(CommandNode* root);
46
47 class Builder {
48 public:
49 Builder(CommandGraph& graph, CommandNode* node);
50
51 Builder& Sequential(frc2::CommandPtr cmd);
52 Builder& After(frc2::CommandPtr cmd);
53 Builder& Before(frc2::CommandPtr cmd);
54
55 Builder& Parallel(frc2::CommandPtr cmd);
56 Builder& Deadline(frc2::CommandPtr cmd);
57 Builder& Race(frc2::CommandPtr cmd);
58
59 Builder& Delay(units::second_t delay, frc2::CommandPtr cmd);
60 Builder& When(std::function<bool()> condition, frc2::CommandPtr cmd);
61
62 Builder& If(std::function<bool()> condition, frc2::CommandPtr ifTrue, frc2::CommandPtr ifFalse);
63
64 Builder& Until(std::function<bool()> condition);
65
66 CommandNode* GetNode();
67
68 private:
69 CommandGraph& m_graph;
70 CommandNode* m_node;
71 };
72
73 Builder Start(frc2::CommandPtr rootCommand);
74
75 private:
76 frc2::CommandPtr BuildNode(CommandNode* node);
77 std::vector<std::unique_ptr<CommandNode>> m_nodes;
78};
79
80} // namespace valor
Definition CommandGraph.h:47
Definition CommandGraph.h:29
Definition CommandGraph.h:19