2017-01-04 23:48:13 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
|
2017-01-04 23:48:13 -08:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Commands/ConditionalCommand.h"
|
|
|
|
|
|
2018-01-18 20:04:33 -08:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2017-01-04 23:48:13 -08:00
|
|
|
#include "Commands/Scheduler.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
2017-06-30 22:01:21 -07:00
|
|
|
static void RequireAll(Command& command, Command* onTrue, Command* onFalse) {
|
|
|
|
|
if (onTrue != nullptr) {
|
|
|
|
|
for (auto requirement : onTrue->GetRequirements())
|
|
|
|
|
command.Requires(requirement);
|
|
|
|
|
}
|
|
|
|
|
if (onFalse != nullptr) {
|
|
|
|
|
for (auto requirement : onFalse->GetRequirements())
|
|
|
|
|
command.Requires(requirement);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 23:48:13 -08:00
|
|
|
ConditionalCommand::ConditionalCommand(Command* onTrue, Command* onFalse) {
|
|
|
|
|
m_onTrue = onTrue;
|
|
|
|
|
m_onFalse = onFalse;
|
|
|
|
|
|
2017-06-30 22:01:21 -07:00
|
|
|
RequireAll(*this, onTrue, onFalse);
|
2017-01-04 23:48:13 -08:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
ConditionalCommand::ConditionalCommand(const wpi::Twine& name, Command* onTrue,
|
2017-01-04 23:48:13 -08:00
|
|
|
Command* onFalse)
|
|
|
|
|
: Command(name) {
|
|
|
|
|
m_onTrue = onTrue;
|
|
|
|
|
m_onFalse = onFalse;
|
|
|
|
|
|
2017-06-30 22:01:21 -07:00
|
|
|
RequireAll(*this, onTrue, onFalse);
|
2017-01-04 23:48:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConditionalCommand::_Initialize() {
|
|
|
|
|
if (Condition()) {
|
|
|
|
|
m_chosenCommand = m_onTrue;
|
|
|
|
|
} else {
|
|
|
|
|
m_chosenCommand = m_onFalse;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-30 22:01:21 -07:00
|
|
|
if (m_chosenCommand != nullptr) {
|
2017-11-16 00:33:51 -08:00
|
|
|
// This is a hack to make cancelling the chosen command inside a
|
|
|
|
|
// CommandGroup work properly
|
2017-06-30 22:01:21 -07:00
|
|
|
m_chosenCommand->ClearRequirements();
|
2017-01-04 23:48:13 -08:00
|
|
|
|
2017-06-30 22:01:21 -07:00
|
|
|
m_chosenCommand->Start();
|
|
|
|
|
}
|
2018-01-18 20:04:33 -08:00
|
|
|
Command::_Initialize();
|
2017-01-04 23:48:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConditionalCommand::_Cancel() {
|
|
|
|
|
if (m_chosenCommand != nullptr && m_chosenCommand->IsRunning()) {
|
|
|
|
|
m_chosenCommand->Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Command::_Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ConditionalCommand::IsFinished() {
|
2018-01-18 20:04:33 -08:00
|
|
|
if (m_chosenCommand != nullptr) {
|
|
|
|
|
return m_chosenCommand->IsCompleted();
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-01-04 23:48:13 -08:00
|
|
|
}
|
|
|
|
|
|
2018-01-18 20:04:33 -08:00
|
|
|
void ConditionalCommand::_Interrupted() {
|
2017-01-04 23:48:13 -08:00
|
|
|
if (m_chosenCommand != nullptr && m_chosenCommand->IsRunning()) {
|
|
|
|
|
m_chosenCommand->Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-18 20:04:33 -08:00
|
|
|
Command::_Interrupted();
|
2017-01-04 23:48:13 -08:00
|
|
|
}
|