Exemplo n.º 1
0
        /// <summary>
        /// Runs a single iteration of the scheduler loop.
        /// </summary>
        /// <remarks>This method should be called often in order to have a functioning
        /// <see cref="Command"/> system. The loop has 5 stages
        /// <list type="ordered">
        /// <listItem><para>Poll the buttons.</para></listItem>
        /// <listItem><para>Execute/Remove the commands.</para></listItem>
        /// <listItem><para>Send values to the <see cref="SmartDashboard.SmartDashboard"/>.</para></listItem>
        /// <listItem><para>Add Commands.</para></listItem>
        /// <listItem><para>Add Defaults.</para></listItem>
        /// </list></remarks>
        public void Run()
        {
            m_runningCommandsChanged = false;

            if (!m_enabled)
            {
                return;
            }
            if (m_buttons != null)
            {
                for (int i = m_buttons.Count - 1; i >= 0; i--)
                {
                    m_buttons[i].Execute();
                }
            }

            LinkedListElement e = m_firstCommand;

            while (e != null)
            {
                Command c = e.GetData();
                e = e.GetNext();
                if (!c.Run())
                {
                    Remove(c);
                    m_runningCommandsChanged = true;
                }
            }

            foreach (Command t in m_additions)
            {
                _Add(t);
            }

            m_additions.Clear();

            foreach (var subsystem in m_subsystems)
            {
                if (subsystem.GetCurrentCommand() == null)
                {
                    _Add(subsystem.GetDefaultCommand());
                }
                subsystem.ConfirmCommand();
            }
            UpdateTable();
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        internal override void _Execute()
        {
            Entry   entry    = null;
            Command cmd      = null;
            bool    firstRun = false;

            if (m_currentCommandIndex == -1)
            {
                firstRun = true;
                m_currentCommandIndex = 0;
            }

            while (m_currentCommandIndex < m_commands.Count())
            {
                if (cmd != null)
                {
                    if (entry.IsTimedOut())
                    {
                        cmd._Cancel();
                    }
                    if (cmd.Run())
                    {
                        break;
                    }
                    else
                    {
                        cmd.Removed();
                        m_currentCommandIndex++;
                        firstRun = true;
                        cmd      = null;
                        continue;
                    }
                }

                entry = m_commands[m_currentCommandIndex];
                cmd   = null;

                switch (entry.state)
                {
                case Entry.IN_SEQUENCE:
                    cmd = entry.command;
                    if (firstRun)
                    {
                        cmd.StartRunning();
                        CancelConflicts(cmd);
                    }
                    firstRun = false;
                    break;

                case Entry.BRANCH_PEER:
                    m_currentCommandIndex++;
                    entry.command.Start();
                    break;

                case Entry.BRANCH_CHILD:
                    m_currentCommandIndex++;
                    CancelConflicts(entry.command);
                    entry.command.StartRunning();
                    m_children.Add(entry);
                    break;
                }
            }
            for (int i = 0; i < m_children.Count; i++)
            {
                entry = m_children[i];
                Command child = entry.command;
                if (entry.IsTimedOut())
                {
                    child._Cancel();
                }
                if (!child.Run())
                {
                    child.Removed();
                    m_children.RemoveAt(i--);
                }
            }
        }