jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/
|
| 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/Scheduler.h"
|
| 8 |
|
| 9 | #include "Buttons/ButtonScheduler.h"
|
| 10 | #include "Commands/Subsystem.h"
|
| 11 | #include "NetworkCommunication/UsageReporting.h"
|
| 12 | #include "Synchronized.h"
|
| 13 | #include "WPIErrors.h"
|
| 14 | #include <iostream>
|
| 15 | #include <set>
|
| 16 | #include <semLib.h>
|
| 17 | #include <algorithm>
|
| 18 |
|
| 19 | Scheduler *Scheduler::_instance = NULL;
|
| 20 |
|
| 21 | Scheduler::Scheduler() :
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 22 | m_buttonsLock(NULL), m_additionsLock(NULL), m_adding(false) {
|
| 23 | m_buttonsLock = semMCreate(
|
| 24 | SEM_Q_PRIORITY | SEM_INVERSION_SAFE | SEM_DELETE_SAFE);
|
| 25 | m_additionsLock = semMCreate(
|
| 26 | SEM_Q_PRIORITY | SEM_INVERSION_SAFE | SEM_DELETE_SAFE);
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 27 |
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 28 | nUsageReporting::report(nUsageReporting::kResourceType_Command,
|
| 29 | nUsageReporting::kCommand_Scheduler);
|
| 30 |
|
| 31 | m_table = NULL;
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 32 | m_enabled = true;
|
| 33 | }
|
| 34 |
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 35 | Scheduler::~Scheduler() {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 36 | semTake(m_additionsLock, WAIT_FOREVER);
|
| 37 | semDelete(m_additionsLock);
|
| 38 |
|
| 39 | semTake(m_buttonsLock, WAIT_FOREVER);
|
| 40 | semDelete(m_buttonsLock);
|
| 41 | }
|
| 42 |
|
| 43 | /**
|
| 44 | * Returns the {@link Scheduler}, creating it if one does not exist.
|
| 45 | * @return the {@link Scheduler}
|
| 46 | */
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 47 | Scheduler *Scheduler::GetInstance() {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 48 | if (_instance == NULL)
|
| 49 | _instance = new Scheduler();
|
| 50 | return _instance;
|
| 51 | }
|
| 52 |
|
| 53 | void Scheduler::SetEnabled(bool enabled) {
|
| 54 | m_enabled = enabled;
|
| 55 | }
|
| 56 |
|
| 57 | /**
|
| 58 | * Add a command to be scheduled later.
|
| 59 | * In any pass through the scheduler, all commands are added to the additions list, then
|
| 60 | * at the end of the pass, they are all scheduled.
|
| 61 | * @param command The command to be scheduled
|
| 62 | */
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 63 | void Scheduler::AddCommand(Command *command) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 64 | Synchronized sync(m_additionsLock);
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 65 | if (std::find(m_additions.begin(), m_additions.end(), command)
|
| 66 | != m_additions.end())
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 67 | return;
|
| 68 | m_additions.push_back(command);
|
| 69 | }
|
| 70 |
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 71 | void Scheduler::AddButton(ButtonScheduler *button) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 72 | Synchronized sync(m_buttonsLock);
|
| 73 | m_buttons.push_back(button);
|
| 74 | }
|
| 75 |
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 76 | void Scheduler::ProcessCommandAddition(Command *command) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 77 | if (command == NULL)
|
| 78 | return;
|
| 79 |
|
| 80 | // Check to make sure no adding during adding
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 81 | if (m_adding) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 82 | wpi_setWPIErrorWithContext(IncompatibleState, "Can not start command from cancel method");
|
| 83 | return;
|
| 84 | }
|
| 85 |
|
| 86 | // Only add if not already in
|
| 87 | CommandSet::iterator found = m_commands.find(command);
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 88 | if (found == m_commands.end()) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 89 | // Check that the requirements can be had
|
| 90 | Command::SubsystemSet requirements = command->GetRequirements();
|
| 91 | Command::SubsystemSet::iterator iter;
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 92 | for (iter = requirements.begin(); iter != requirements.end(); iter++) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 93 | Subsystem *lock = *iter;
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 94 | if (lock->GetCurrentCommand() != NULL
|
| 95 | && !lock->GetCurrentCommand()->IsInterruptible())
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 96 | return;
|
| 97 | }
|
| 98 |
|
| 99 | // Give it the requirements
|
| 100 | m_adding = true;
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 101 | for (iter = requirements.begin(); iter != requirements.end(); iter++) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 102 | Subsystem *lock = *iter;
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 103 | if (lock->GetCurrentCommand() != NULL) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 104 | lock->GetCurrentCommand()->Cancel();
|
| 105 | Remove(lock->GetCurrentCommand());
|
| 106 | }
|
| 107 | lock->SetCurrentCommand(command);
|
| 108 | }
|
| 109 | m_adding = false;
|
| 110 |
|
| 111 | m_commands.insert(command);
|
| 112 |
|
| 113 | command->StartRunning();
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 114 | m_runningCommandsChanged = true;
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | /**
|
| 119 | * Runs a single iteration of the loop. This method should be called often in order to have a functioning
|
| 120 | * {@link Command} system. The loop has five stages:
|
| 121 | *
|
| 122 | * <ol>
|
| 123 | * <li> Poll the Buttons </li>
|
| 124 | * <li> Execute/Remove the Commands </li>
|
| 125 | * <li> Send values to SmartDashboard </li>
|
| 126 | * <li> Add Commands </li>
|
| 127 | * <li> Add Defaults </li>
|
| 128 | * </ol>
|
| 129 | */
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 130 | void Scheduler::Run() {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 131 | // Get button input (going backwards preserves button priority)
|
| 132 | {
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 133 | if (!m_enabled)
|
| 134 | return;
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 135 |
|
| 136 | Synchronized sync(m_buttonsLock);
|
| 137 | ButtonVector::reverse_iterator rButtonIter = m_buttons.rbegin();
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 138 | for (; rButtonIter != m_buttons.rend(); rButtonIter++) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 139 | (*rButtonIter)->Execute();
|
| 140 | }
|
| 141 | }
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 142 |
|
| 143 | m_runningCommandsChanged = false;
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 144 |
|
| 145 | // Loop through the commands
|
| 146 | CommandSet::iterator commandIter = m_commands.begin();
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 147 | for (; commandIter != m_commands.end();) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 148 | Command *command = *commandIter;
|
| 149 | // Increment before potentially removing to keep the iterator valid
|
| 150 | commandIter++;
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 151 | if (!command->Run()) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 152 | Remove(command);
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 153 | m_runningCommandsChanged = true;
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | // Add the new things
|
| 158 | {
|
| 159 | Synchronized sync(m_additionsLock);
|
| 160 | CommandVector::iterator additionsIter = m_additions.begin();
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 161 | for (; additionsIter != m_additions.end(); additionsIter++) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 162 | ProcessCommandAddition(*additionsIter);
|
| 163 | }
|
| 164 | m_additions.clear();
|
| 165 | }
|
| 166 |
|
| 167 | // Add in the defaults
|
| 168 | Command::SubsystemSet::iterator subsystemIter = m_subsystems.begin();
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 169 | for (; subsystemIter != m_subsystems.end(); subsystemIter++) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 170 | Subsystem *lock = *subsystemIter;
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 171 | if (lock->GetCurrentCommand() == NULL) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 172 | ProcessCommandAddition(lock->GetDefaultCommand());
|
| 173 | }
|
| 174 | lock->ConfirmCommand();
|
| 175 | }
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 176 |
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 177 | UpdateTable();
|
| 178 | }
|
| 179 |
|
| 180 | /**
|
| 181 | * Registers a {@link Subsystem} to this {@link Scheduler}, so that the {@link Scheduler} might know
|
| 182 | * if a default {@link Command} needs to be run. All {@link Subsystem Subsystems} should call this.
|
| 183 | * @param system the system
|
| 184 | */
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 185 | void Scheduler::RegisterSubsystem(Subsystem *subsystem) {
|
| 186 | if (subsystem == NULL) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 187 | wpi_setWPIErrorWithContext(NullParameter, "subsystem");
|
| 188 | return;
|
| 189 | }
|
| 190 | m_subsystems.insert(subsystem);
|
| 191 | }
|
| 192 |
|
| 193 | /**
|
| 194 | * Removes the {@link Command} from the {@link Scheduler}.
|
| 195 | * @param command the command to remove
|
| 196 | */
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 197 | void Scheduler::Remove(Command *command) {
|
| 198 | if (command == NULL) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 199 | wpi_setWPIErrorWithContext(NullParameter, "command");
|
| 200 | return;
|
| 201 | }
|
| 202 |
|
| 203 | if (!m_commands.erase(command))
|
| 204 | return;
|
| 205 |
|
| 206 | Command::SubsystemSet requirements = command->GetRequirements();
|
| 207 | Command::SubsystemSet::iterator iter = requirements.begin();
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 208 | for (; iter != requirements.end(); iter++) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 209 | Subsystem *lock = *iter;
|
| 210 | lock->SetCurrentCommand(NULL);
|
| 211 | }
|
| 212 |
|
| 213 | command->Removed();
|
| 214 | }
|
| 215 |
|
| 216 | void Scheduler::RemoveAll() {
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 217 | while (m_commands.size() > 0) {
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 218 | Remove(*m_commands.begin());
|
| 219 | }
|
| 220 | }
|
| 221 |
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 222 | /**
|
| 223 | * Update the network tables associated with the Scheduler object on the SmartDashboard
|
| 224 | */
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 225 | void Scheduler::UpdateTable() {
|
| 226 | CommandSet::iterator commandIter;
|
| 227 | if (m_table != NULL) {
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 228 | // Get the list of possible commands to cancel
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 229 | m_table->RetrieveValue("Cancel", *toCancel);
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 230 | // m_table->RetrieveValue("Ids", *ids);
|
| 231 |
|
| 232 | // cancel commands that have had the cancel buttons pressed
|
| 233 | // on the SmartDashboad
|
| 234 | if (toCancel->size() > 0) {
|
| 235 | for (commandIter = m_commands.begin(); commandIter
|
| 236 | != m_commands.end(); ++commandIter) {
|
| 237 | for (unsigned i = 0; i < toCancel->size(); i++) {
|
| 238 | Command *c = *commandIter;
|
| 239 | if (c->GetID() == toCancel->get(i)) {
|
| 240 | c->Cancel();
|
| 241 | }
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 242 | }
|
| 243 | }
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 244 | toCancel->setSize(0);
|
| 245 | m_table->PutValue("Cancel", *toCancel);
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 246 | }
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 247 |
|
| 248 | // Set the running commands
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 249 | if (m_runningCommandsChanged) {
|
| 250 | commands->setSize(0);
|
| 251 | ids->setSize(0);
|
| 252 | for (commandIter = m_commands.begin(); commandIter != m_commands.end(); ++commandIter) {
|
| 253 | Command *c = *commandIter;
|
| 254 | commands->add(c->GetName());
|
| 255 | ids->add(c->GetID());
|
| 256 | }
|
| 257 | m_table->PutValue("Names", *commands);
|
| 258 | m_table->PutValue("Ids", *ids);
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 259 | }
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 260 | }
|
| 261 | }
|
| 262 |
|
| 263 | std::string Scheduler::GetName() {
|
| 264 | return "Scheduler";
|
| 265 | }
|
| 266 |
|
| 267 | std::string Scheduler::GetType() {
|
| 268 | return "Scheduler";
|
| 269 | }
|
| 270 |
|
| 271 | std::string Scheduler::GetSmartDashboardType() {
|
| 272 | return "Scheduler";
|
| 273 | }
|
| 274 |
|
| 275 | void Scheduler::InitTable(ITable *subTable) {
|
| 276 | m_table = subTable;
|
| 277 | commands = new StringArray();
|
| 278 | ids = new NumberArray();
|
| 279 | toCancel = new NumberArray();
|
jerrym | 42dedc0 | 2013-02-25 01:59:14 +0000 | [diff] [blame^] | 280 |
|
jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 281 | m_table->PutValue("Names", *commands);
|
| 282 | m_table->PutValue("Ids", *ids);
|
| 283 | m_table->PutValue("Cancel", *toCancel);
|
| 284 | }
|
| 285 |
|
| 286 | ITable * Scheduler::GetTable() {
|
| 287 | return m_table;
|
| 288 | }
|