Go to Post Wow. Just wow. Never did I think the mentor built robot debate would make it on to a tv show. - ASD20 [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
  #1   Spotlight this post!  
Unread 15-11-2015, 10:58
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,713
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Java Conditional Command

Here is an idea I had recently for conditional wait commands. Thoughts?

With just a couple of static imports you could be writing CommandGroups that look like this.

Code:
package org.usfirst.frc.team2363.robot.commands;

import static org.usfirst.frc.team2363.robot.Robot.drivetrain;
import static org.usfirst.frc.team2363.robot.commands.ConditionalWaitCommand.Operation.*;
import static org.usfirst.frc.team2363.robot.commands.ConditionalWaitCommand.*;

import edu.wpi.first.wpilibj.command.CommandGroup;

public class DriveToLine extends CommandGroup {
    
    public  DriveToLine() {
    	addSequential(new DriveCommand(0.5));
        addSequential(waitUntil(drivetrain.getDistance(), GREATER_THAN, 50));
        addSequential(new DriveCommand(0));
    }
}
Code:
package org.usfirst.frc.team2363.robot.commands;

import edu.wpi.first.wpilibj.command.Command;

public class ConditionalWaitCommand extends Command {
	
	public enum Operation {
		LESS_THAN, LESS_THAN_EQUAL, EQUAL, GREATER_THAN_EQUAL, GREATER_THAN
	}
	
	public interface IProvidesValue {
		double getValue();
	}
	
	public static ConditionalWaitCommand waitUntil(IProvidesValue operand1, Operation operation, double operand2) {
		return new ConditionalWaitCommand(operand1, operation, operand2, true);
	}
	
	public static ConditionalWaitCommand waitWhile(IProvidesValue operand1, Operation operation, double operand2) {
		return new ConditionalWaitCommand(operand1, operation, operand2, false);
	}
	
	private IProvidesValue operand1;
	private Operation operation;
	private double operand2;
	private boolean isUntil;

    private ConditionalWaitCommand(IProvidesValue operand1, Operation operation, double operand2, boolean isUntil) {
        this.operand1 = operand1;
        this.operation = operation;
        this.operand2 = operand2;
        this.isUntil = isUntil;
    }
    
    @Override
	protected void initialize() { }

	@Override
	protected void execute() { }

    protected boolean isFinished() {
    	switch (operation) {
			case EQUAL:
				if (operand1.getValue() == operand2) {
					return isUntil;
				}
			case GREATER_THAN:
				if (operand1.getValue() > operand2) {
					return isUntil;
				}
			case GREATER_THAN_EQUAL:
				if (operand1.getValue() >= operand2) {
					return isUntil;
				}
			case LESS_THAN:
				if (operand1.getValue() < operand2) {
					return isUntil;
				}
			case LESS_THAN_EQUAL:
				if (operand1.getValue() <= operand2) {
					return isUntil;
				}
    	}
        return !isUntil;
    }

	@Override
	protected void end() { }

	@Override
	protected void interrupted() { }
}
Code:
package org.usfirst.frc.team2363.robot.subsystems;


import org.usfirst.frc.team2363.robot.commands.ConditionalWaitCommand.IProvidesValue;

import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.command.Subsystem;

public class Drivetrain extends Subsystem {
    
    private Distance distance;
    private Encoder encoder = new Encoder(1, 2);

    public void initDefaultCommand() { }
    
    public IProvidesValue getDistance() {
    	if (distance == null) {
    		distance = new Distance();
    	}
    	return distance;
    }
    
    private class Distance implements IProvidesValue {

		@Override
		public double getValue() {
			return encoder.getDistance();
		}
    }
}
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:44.

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