forked from robotpy/robotpy-commands-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequentialcommandgroup.py
90 lines (70 loc) · 3.12 KB
/
sequentialcommandgroup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from __future__ import annotations
from typing import List
from commands2.command import Command, InterruptionBehavior
from .command import Command, InterruptionBehavior
from .commandgroup import *
from .commandscheduler import CommandScheduler
from .util import flatten_args_commands
class SequentialCommandGroup(CommandGroup):
"""
A command composition that runs a list of commands in sequence.
The rules for command compositions apply: command instances that are passed to it cannot be
added to any other composition or scheduled individually, and the composition requires all
subsystems its components require."""
def __init__(self, *commands: Command):
"""
Creates a new SequentialCommandGroup. The given commands will be run sequentially, with the
composition finishing when the last command finishes.
:param commands: the commands to include in this composition."""
super().__init__()
self._commands: List[Command] = []
self._runsWhenDisabled = True
self._interruptBehavior = InterruptionBehavior.kCancelIncoming
self._currentCommandIndex = -1
self.addCommands(*commands)
def addCommands(self, *commands: Command):
commands = flatten_args_commands(commands)
if self._currentCommandIndex != -1:
raise IllegalCommandUse(
"Commands cannot be added to a composition while it is running"
)
CommandScheduler.getInstance().registerComposedCommands(commands)
for command in commands:
self._commands.append(command)
self.requirements.update(command.getRequirements())
self._runsWhenDisabled = (
self._runsWhenDisabled and command.runsWhenDisabled()
)
if command.getInterruptionBehavior() == InterruptionBehavior.kCancelSelf:
self._interruptBehavior = InterruptionBehavior.kCancelSelf
def initialize(self):
self._currentCommandIndex = 0
if self._commands:
self._commands[0].initialize()
def execute(self):
if not self._commands:
return
currentCommand = self._commands[self._currentCommandIndex]
currentCommand.execute()
if currentCommand.isFinished():
currentCommand.end(False)
self._currentCommandIndex += 1
if self._currentCommandIndex < len(self._commands):
self._commands[self._currentCommandIndex].initialize()
def end(self, interrupted: bool):
if not interrupted:
return
if not self._commands:
return
if not self._currentCommandIndex > -1:
return
if not self._currentCommandIndex < len(self._commands):
return
self._commands[self._currentCommandIndex].end(True)
self._currentCommandIndex = -1
def isFinished(self) -> bool:
return self._currentCommandIndex == len(self._commands)
def runsWhenDisabled(self) -> bool:
return self._runsWhenDisabled
def getInterruptionBehavior(self) -> InterruptionBehavior:
return self._interruptBehavior