blob: dfd559ce8530289db33ae93e728e50b639f04a36 [file] [log] [blame]
jerrymf1579332013-02-07 01:56:28 +00001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2011. 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 $(WIND_BASE)/WPILib. */
5/*----------------------------------------------------------------------------*/
6
7#include "Commands/Subsystem.h"
8
9#include "Commands/Command.h"
10#include "Commands/Scheduler.h"
11#include "WPIErrors.h"
12
13/**
14 * Creates a subsystem with the given name
15 * @param name the name of the subsystem
16 */
17Subsystem::Subsystem(const char *name) :
18 m_currentCommand(NULL),
19 m_defaultCommand(NULL),
20 m_initializedDefaultCommand(false)
21{
22 m_name = name;
23 Scheduler::GetInstance()->RegisterSubsystem(this);
24 m_table = NULL;
25}
26/**
27 * Initialize the default command for this subsystem
28 * This is meant to be the place to call SetDefaultCommand in a subsystem and will be called
29 * on all the subsystems by the CommandBase method before the program starts running by using
30 * the list of all registered Subsystems inside the Scheduler.
31 *
32 * This should be overridden by a Subsystem that has a default Command
33 */
34void Subsystem::InitDefaultCommand() {
35
36}
37
38/**
39 * Sets the default command. If this is not called or is called with null,
40 * then there will be no default command for the subsystem.
41 *
42 * <p><b>WARNING:</b> This should <b>NOT</b> be called in a constructor if the subsystem is a
43 * singleton.</p>
44 *
45 * @param command the default command (or null if there should be none)
46 */
47void Subsystem::SetDefaultCommand(Command *command)
48{
49 if (command == NULL)
50 {
51 m_defaultCommand = NULL;
52 }
53 else
54 {
55 bool found = false;
56 Command::SubsystemSet requirements = command->GetRequirements();
57 Command::SubsystemSet::iterator iter = requirements.begin();
58 for (; iter != requirements.end(); iter++)
59 {
60 if (*iter == this)
61 {
62 found = true;
63 break;
64 }
65 }
66
67 if (!found)
68 {
69 wpi_setWPIErrorWithContext(CommandIllegalUse, "A default command must require the subsystem");
70 return;
71 }
72
73 m_defaultCommand = command;
74 }
75 if (m_table != NULL)
76 {
77 if (m_defaultCommand != NULL)
78 {
79 m_table->PutBoolean("hasDefault", true);
80 m_table->PutString("default", m_defaultCommand->GetName());
81 }
82 else
83 {
84 m_table->PutBoolean("hasDefault", false);
85 }
86 }
87}
88
89/**
90 * Returns the default command (or null if there is none).
91 * @return the default command
92 */
93Command *Subsystem::GetDefaultCommand()
94{
95 if (!m_initializedDefaultCommand) {
96 m_initializedDefaultCommand = true;
97 InitDefaultCommand();
98 }
99 return m_defaultCommand;
100}
101
102/**
103 * Sets the current command
104 * @param command the new current command
105 */
106void Subsystem::SetCurrentCommand(Command *command)
107{
108 m_currentCommand = command;
109}
110
111/**
112 * Returns the command which currently claims this subsystem.
113 * @return the command which currently claims this subsystem
114 */
115Command *Subsystem::GetCurrentCommand()
116{
117 return m_currentCommand;
118}
119
120/**
121 * Call this to alert Subsystem that the current command is actually the command.
122 * Sometimes, the {@link Subsystem} is told that it has no command while the {@link Scheduler}
123 * is going through the loop, only to be soon after given a new one. This will avoid that situation.
124 */
125void Subsystem::ConfirmCommand()
126{
127 if (m_table != NULL)
128 {
129 if (m_currentCommand != NULL)
130 {
131 m_table->PutBoolean("hasCommand", true);
132 m_table->PutString("command", m_currentCommand->GetName());
133 }
134 else
135 {
136 m_table->PutBoolean("hasCommand", false);
137 }
138 }
139}
140
141
142
143std::string Subsystem::GetName()
144{
145 return m_name;
146}
147
148std::string Subsystem::GetSmartDashboardType()
149{
150 return "Subsystem";
151}
152
153void Subsystem::InitTable(ITable* table)
154{
155 m_table = table;
156 if(m_table!=NULL){
157 if (m_defaultCommand != NULL) {
158 m_table->PutBoolean("hasDefault", true);
159 m_table->PutString("default", m_defaultCommand->GetName());
160 } else {
161 m_table->PutBoolean("hasDefault", false);
162 }
163 if (m_currentCommand != NULL) {
164 m_table->PutBoolean("hasCommand", true);
165 m_table->PutString("command", m_currentCommand->GetName());
166 } else {
167 m_table->PutBoolean("hasCommand", false);
168 }
169 }
170}
171
172ITable* Subsystem::GetTable(){
173 return m_table;
174}