Go to Post I voted to keep Stop Build Day but only for students. Mentors get to keep working on the robot. - marshall [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #12   Spotlight this post!  
Unread 02-15-2014, 04:50 AM
Ginto8's Avatar
Ginto8 Ginto8 is offline
Programming Lead
AKA: Joe Doyle
FRC #2729 (Storm)
Team Role: Programmer
 
Join Date: Oct 2010
Rookie Year: 2010
Location: Marlton, NJ
Posts: 174
Ginto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of light
Re: Using Coniditionals to Build Custom Command Groups

We will not be testing this until later today, but I wanted to share my expected solution to Conditional cancelling itself. It requires an extra class, edu.wpi.first.wpilibj.command.PublicCommand, which just exposes a number of Command's methods that are protected or package-private normally:
Code:
package edu.wpi.first.wpilibj.command;

import java.util.Enumeration;

/** A Command that exposes more internals to the world. */
public class PublicCommand extends Command {
    private final Command _c;

    public PublicCommand(Command c) {
        _c = c;
        for(Enumeration e = c.getRequirements();e.hasMoreElements();) {
            requires((Subsystem) e.nextElement());
        }
    }
    public Enumeration getRequirements() {
        return _c.getRequirements();
    }

    public void initialize() {
        if(_c != null) {
            _c.initialize();
        }
    }
    public void _initialize() {
        if(_c != null) {
            _c._initialize();
        }
    }

    public void execute() {
        if(_c != null) {
            _c.execute();
        }
    }
    public void _execute() {
        if(_c != null) {
            _c._execute();
        }
    }

    public boolean isFinished() {
        return _c == null || _c.isFinished();
    }

    public void end() {
        if(_c != null) {
            _c.end();
        }
    }
    public void _end() {
        if(_c != null) {
            _c._end();
        }
    }

    public void interrupted() {
        if(_c != null) {
            _c.interrupted();
        }
    }
    public void _interrupted() {
        if(_c != null) {
            _c._interrupted();
        }
    }
    
}
Then, Conditional becomes:
Code:
package storm2014.commands.control;

import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.command.PublicCommand;
import edu.wpi.first.wpilibj.command.Subsystem;
import java.util.Enumeration;

public abstract class Conditional extends Command {
    private final PublicCommand _ifTrue,_ifFalse;
    private PublicCommand _running = null;

    public Conditional(final Command ifTrue,final Command ifFalse) {
        // Wrap the Commands to expose protected methods
        if(ifTrue != null) {
            _ifTrue  = new PublicCommand(ifTrue);
            for(Enumeration e = _ifTrue.getRequirements();e.hasMoreElements();) {
                requires((Subsystem) e.nextElement());
            }
        } else {
            _ifTrue = null;
        }
        if(ifFalse != null) {
            _ifFalse  = new PublicCommand(ifFalse);
            for(Enumeration e = _ifFalse.getRequirements();e.hasMoreElements();) {
                requires((Subsystem) e.nextElement());
            }
        } else {
            _ifFalse = null;
        }
    }
    
    protected abstract boolean condition();

    protected void initialize() {
        if(condition()) {
            _running = _ifTrue;
        } else {
            _running = _ifFalse;
        }
        if(_running != null) {
            _running._initialize();
            _running.initialize();
        }
    }

    protected void execute() {
        if(_running != null) {
            _running._execute();
            _running.execute();
        }
    }

    protected boolean isFinished() {
        return _running == null || _running.isFinished();
    }

    protected void end() {
        if(_running != null) {
            _running._end();
            _running.end();
        }
    }

    protected void interrupted() {
        if(_running != null) {
            _running._interrupted();
            _running.interrupted();
        }
    }
}
Instead of start()ing the Commands, it wraps them entirely. The Conditional has all the combined requirements of ifTrue and ifFalse, and each of initialize(), execute(), isFinished(), end(), and interrupted() call the corresponding method in the selected Command. Because of that, running it should have the same effect as start()ing, except that it will not cancel a CommandGroup it is in.
__________________
I code stuff.

Last edited by Ginto8 : 02-15-2014 at 05:00 AM.
Reply With Quote
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 08:48 AM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi