Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2011-2017. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <list> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "Commands/Command.h" |
| 15 | #include "Commands/CommandGroupEntry.h" |
| 16 | |
| 17 | namespace frc { |
| 18 | |
| 19 | /** |
| 20 | * A {@link CommandGroup} is a list of commands which are executed in sequence. |
| 21 | * |
| 22 | * <p>Commands in a {@link CommandGroup} are added using the {@link |
| 23 | * CommandGroup#AddSequential(Command) AddSequential(...)} method and are |
| 24 | * called sequentially. {@link CommandGroup CommandGroups} are themselves |
| 25 | * {@link Command Commands} and can be given to other |
| 26 | * {@link CommandGroup CommandGroups}.</p> |
| 27 | * |
| 28 | * <p>{@link CommandGroup CommandGroups} will carry all of the requirements of |
| 29 | * their {@link Command subcommands}. Additional requirements can be specified |
| 30 | * by calling {@link CommandGroup#Requires(Subsystem) Requires(...)} normally |
| 31 | * in the constructor.</P> |
| 32 | * |
| 33 | * <p>CommandGroups can also execute commands in parallel, simply by adding them |
| 34 | * using {@link CommandGroup#AddParallel(Command) AddParallel(...)}.</p> |
| 35 | * |
| 36 | * @see Command |
| 37 | * @see Subsystem |
| 38 | */ |
| 39 | class CommandGroup : public Command { |
| 40 | public: |
| 41 | CommandGroup() = default; |
| 42 | explicit CommandGroup(const std::string& name); |
| 43 | virtual ~CommandGroup() = default; |
| 44 | |
| 45 | void AddSequential(Command* command); |
| 46 | void AddSequential(Command* command, double timeout); |
| 47 | void AddParallel(Command* command); |
| 48 | void AddParallel(Command* command, double timeout); |
| 49 | bool IsInterruptible() const; |
| 50 | int GetSize() const; |
| 51 | |
| 52 | protected: |
| 53 | virtual void Initialize(); |
| 54 | virtual void Execute(); |
| 55 | virtual bool IsFinished(); |
| 56 | virtual void End(); |
| 57 | virtual void Interrupted(); |
| 58 | virtual void _Initialize(); |
| 59 | virtual void _Interrupted(); |
| 60 | virtual void _Execute(); |
| 61 | virtual void _End(); |
| 62 | |
| 63 | private: |
| 64 | void CancelConflicts(Command* command); |
| 65 | |
| 66 | /** The commands in this group (stored in entries) */ |
| 67 | std::vector<CommandGroupEntry> m_commands; |
| 68 | |
| 69 | /** The active children in this group (stored in entries) */ |
| 70 | std::list<CommandGroupEntry> m_children; |
| 71 | |
| 72 | /** The current command, -1 signifies that none have been run */ |
| 73 | int m_currentCommandIndex = -1; |
| 74 | }; |
| 75 | |
| 76 | } // namespace frc |