Go to Post How FIRST is helping: teaching me just how much I can achieve in 6 weeks. - Otaku [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 07-15-2018, 04:23 PM
CodeNinja CodeNinja is offline
Registered User
FRC #3997 (Screaming Chickens)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2017
Location: San Antonio
Posts: 66
CodeNinja is an unknown quantity at this point
Parameters in Command

Is it possible to pass in subsystems as parameters, instead of requiring them?

This is an example of what I want to do that did not work:

Code:
package org.usfirst.frc.team3997.robot.auto.actions;

import org.usfirst.frc.team3997.robot.MasterController;
import org.usfirst.frc.team3997.robot.controllers.ArmController;

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

public class ScaleAction extends Command {
	private ArmController arm;
	private Boolean setpointReached;
	public ScaleAction(MasterController controllers) {
		arm = controllers.getArmController();
		this.setpointReached = false;
	}

	protected boolean isFinished() {
		// TODO Auto-generated method stub
		return setpointReached;
	}

	protected void execute() {
		if(arm.armPIDController.onTarget()) {
			setpointReached = true;
		} else {
			setpointReached = false;
		}
		
	}

	protected void end() {
		arm.armPIDController.disable();
		
	}

	protected void intialize() {
		arm.goToScalePosition();
	}
	
	protected void interrupt() {
		end();
	}

}
Reply With Quote
  #2   Spotlight this post!  
Unread 07-15-2018, 04:26 PM
Mitch Stokes's Avatar
Mitch Stokes Mitch Stokes is offline
Berkeley Engineering
AKA: Mitchel Stokes
no team
Team Role: College Student
 
Join Date: Aug 2013
Rookie Year: 2014
Location: Berkeley, CA
Posts: 744
Mitch Stokes has a reputation beyond reputeMitch Stokes has a reputation beyond reputeMitch Stokes has a reputation beyond reputeMitch Stokes has a reputation beyond reputeMitch Stokes has a reputation beyond reputeMitch Stokes has a reputation beyond reputeMitch Stokes has a reputation beyond reputeMitch Stokes has a reputation beyond reputeMitch Stokes has a reputation beyond reputeMitch Stokes has a reputation beyond reputeMitch Stokes has a reputation beyond repute
Re: Parameters in Command

Quote:
Originally Posted by CodeNinja View Post
Is it possible to pass in subsystems as parameters, instead of requiring them?

This is an example of what I want to do that did not work:

Code:
package org.usfirst.frc.team3997.robot.auto.actions;

import org.usfirst.frc.team3997.robot.MasterController;
import org.usfirst.frc.team3997.robot.controllers.ArmController;

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

public class ScaleAction extends Command {
	private ArmController arm;
	private Boolean setpointReached;
	public ScaleAction(MasterController controllers) {
		arm = controllers.getArmController();
		this.setpointReached = false;
	}

	protected boolean isFinished() {
		// TODO Auto-generated method stub
		return setpointReached;
	}

	protected void execute() {
		if(arm.armPIDController.onTarget()) {
			setpointReached = true;
		} else {
			setpointReached = false;
		}
		
	}

	protected void end() {
		arm.armPIDController.disable();
		
	}

	protected void intialize() {
		arm.goToScalePosition();
	}
	
	protected void interrupt() {
		end();
	}

}
The point of requiring is to prevent multiple commands from using the same subsystem at the same time. In what way did this not work, i.e. did the code crash, did the arm not move?
__________________
1671: 2014-2015
5817: 2016-2017
5817 Mentor: 2018

Betts Company Engineering Intern: Summer 2018


"Criticism is easier to take when you realize that the only people who aren't criticized
are those who don't take risks."
Reply With Quote
  #3   Spotlight this post!  
Unread 07-15-2018, 04:27 PM
CodeNinja CodeNinja is offline
Registered User
FRC #3997 (Screaming Chickens)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2017
Location: San Antonio
Posts: 66
CodeNinja is an unknown quantity at this point
Re: Parameters in Command

By the way, Master Controller is just a container class for all the other subsystems.

Here is github link: https://github.com/codeNinjaDev/FRC-...basedprototype

I am trying to integrate existing architecture with command, but I am not sure if it is possible.
Reply With Quote
  #4   Spotlight this post!  
Unread 07-15-2018, 04:30 PM
CodeNinja CodeNinja is offline
Registered User
FRC #3997 (Screaming Chickens)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2017
Location: San Antonio
Posts: 66
CodeNinja is an unknown quantity at this point
Re: Parameters in Command

Quote:
Originally Posted by Mitch Stokes View Post
The point of requiring is to prevent multiple commands from using the same subsystem at the same time. In what way did this not work, i.e. did the code crash, did the arm not move?
Sorry, I meant this file:

[code]package org.usfirst.frc.team3997.robot.auto.actions;

import org.usfirst.frc.team3997.robot.MasterController;
import org.usfirst.frc.team3997.robot.controllers.ArmCont roller;

import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboar d;

public class SwitchAction extends Command {
private ArmController arm;
private Boolean setpointReached;
public SwitchAction(MasterController controllers) {
arm = controllers.getArmController();
this.setpointReached = false;
}
@Override
protected boolean isFinished() {
// TODO Auto-generated method stub
return setpointReached;
}

@Override
protected void execute() {
if(arm.armPIDController.onTarget()) {
setpointReached = true;
} else {
setpointReached = false;
}

}

protected void end() {
arm.armPIDController.disable();
}

protected void intialize() {
arm.goToSwitchPosition();
SmartDashboard.putString("PID Switch", "RUnning");
}

protected void interrupt() {
end();
}

}
[\CODE}

But, yeah nothing moved.


I used this command in this file: https://github.com/codeNinjaDev/FRC-2018/blob/feat/commandbasedprototype/src/org/usfirst/frc/team3997/robot/auto/routines/OneCubeCenterAutoRoutine.java

And then I set the autonomous in AutoSelector and robotInit.

Thanks for the quick response
Reply With Quote
  #5   Spotlight this post!  
Unread 07-15-2018, 04:32 PM
CodeNinja CodeNinja is offline
Registered User
FRC #3997 (Screaming Chickens)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2017
Location: San Antonio
Posts: 66
CodeNinja is an unknown quantity at this point
Re: Parameters in Command

Sorry, here is the correct formatting:

Code:
package org.usfirst.frc.team3997.robot.auto.actions;

import org.usfirst.frc.team3997.robot.MasterController;
import org.usfirst.frc.team3997.robot.controllers.ArmController;

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

public class SwitchAction extends Command {
	private ArmController arm;
	private Boolean setpointReached;
	public SwitchAction(MasterController controllers) {
		arm = controllers.getArmController();
		this.setpointReached = false;
	}
	@Override
	protected boolean isFinished() {
		// TODO Auto-generated method stub
		return setpointReached;
	}

	@Override
	protected void execute() {
		if(arm.armPIDController.onTarget()) {
			setpointReached = true;
		} else {
			setpointReached = false;
		}
		
	}

	protected void end() {
		arm.armPIDController.disable();
	}

	protected void intialize() {
		arm.goToSwitchPosition();
		SmartDashboard.putString("PID Switch", "RUnning");
	}
	
	protected void interrupt() {
		end();
	}

}
Reply With Quote
  #6   Spotlight this post!  
Unread 07-15-2018, 04:35 PM
Maxcr1's Avatar
Maxcr1 Maxcr1 is offline
Registered User
AKA: Max D "Code Boy"
FRC #2169 (KING TeC)
Team Role: Programmer
 
Join Date: Nov 2016
Rookie Year: 2016
Location: Savage, MN
Posts: 113
Maxcr1 is a splendid one to beholdMaxcr1 is a splendid one to beholdMaxcr1 is a splendid one to beholdMaxcr1 is a splendid one to beholdMaxcr1 is a splendid one to beholdMaxcr1 is a splendid one to beholdMaxcr1 is a splendid one to behold
Re: Parameters in Command

Quote:
Originally Posted by CodeNinja View Post
But, yeah nothing moved.
Could you please expand on that? We really don't have any way to help you unless we know where the error is coming from. Is the DriverStation printing out any stacktraces? How about the RIOLog inside of Eclipse? Is everything ported correctly? Are your Talons addressed correctly? Does the system work with other code? In order to help you properly we need more information.
__________________
Max DeVos | Programmer | KING TeC 2169

Reply With Quote
  #7   Spotlight this post!  
Unread 07-15-2018, 04:44 PM
CodeNinja CodeNinja is offline
Registered User
FRC #3997 (Screaming Chickens)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2017
Location: San Antonio
Posts: 66
CodeNinja is an unknown quantity at this point
Re: Parameters in Command

Quote:
Originally Posted by Maxcr1 View Post
Could you please expand on that? We really don't have any way to help you unless we know where the error is coming from. Is the DriverStation printing out any stacktraces? How about the RIOLog inside of Eclipse? Is everything ported correctly? Are your Talons addressed correctly? Does the system work with other code? In order to help you properly we need more information.
Sorry for the confusion,

There were no errors, but the robot did not physically move.
Reply With Quote
  #8   Spotlight this post!  
Unread 07-15-2018, 04:45 PM
CodeNinja CodeNinja is offline
Registered User
FRC #3997 (Screaming Chickens)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2017
Location: San Antonio
Posts: 66
CodeNinja is an unknown quantity at this point
Re: Parameters in Command

And Teleop works (not using command)
Reply With Quote
  #9   Spotlight this post!  
Unread 07-15-2018, 04:46 PM
CodeNinja CodeNinja is offline
Registered User
FRC #3997 (Screaming Chickens)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2017
Location: San Antonio
Posts: 66
CodeNinja is an unknown quantity at this point
Re: Parameters in Command

Basically, autonomous is not working, but everything else is
Reply With Quote
  #10   Spotlight this post!  
Unread 07-15-2018, 04:53 PM
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,976
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Parameters in Command

Do other commands work? In Robot.java, the scheduler isn't called, so I wouldn't expect any commands to work.

I'm struggling with why you want to structure your code like this. Passing in a subsystem doesn't seem to add any abstraction or increase the flexibility of your code.
Reply With Quote
  #11   Spotlight this post!  
Unread 07-15-2018, 05:06 PM
CodeNinja CodeNinja is offline
Registered User
FRC #3997 (Screaming Chickens)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2017
Location: San Antonio
Posts: 66
CodeNinja is an unknown quantity at this point
Re: Parameters in Command

No, other commands don't work.

I think I call Scheduler on line 153. I'm not very experience with command based.

I just have an irrational dislike of static objects, but I do use them sometimes. A lot like camel case vs underscores. I just want to know if we can pass them in, it is not a deal breaker.
Reply With Quote
  #12   Spotlight this post!  
Unread 07-15-2018, 05:44 PM
Oblarg Oblarg is offline
Registered User
AKA: Eli Barnett
FRC #0449 (The Blair Robot Project)
Team Role: Mentor
 
Join Date: Mar 2009
Rookie Year: 2008
Location: Philadelphia, PA
Posts: 1,885
Oblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond reputeOblarg has a reputation beyond repute
Re: Parameters in Command

Yes, it is possible to do this. What you are describing is a design pattern called dependency injection, and it is by far the best way (in my opinion) to add flexibility and reusability to the Command-Based paradigm.

It is still good practice to require the subsystem, and you can easily do this by just calling requires() on the passed-in subsystem.

Note that your example code seems to be passing in a much bigger object than what you're actually using. This is okay in some cases, but is generally inadvisable. You should pass in (or "inject") only what you need, unless you have a very good reason to do otherwise.

Quote:
Originally Posted by Joe Ross View Post
I'm struggling with why you want to structure your code like this. Passing in a subsystem doesn't seem to add any abstraction or increase the flexibility of your code.
It most certainly does - you simply have to take advantage of some sort of polymorphism in your subsystems (I recommend interfaces, but inheritance can work too). One can use the same "open claw" command on many different types of claws, for example, rather than tediously and messily writing a bunch of nearly-identical commands.

It also, as the OP noted, removes the need for global state, which is basically always a good idea.
__________________
"Mmmmm, chain grease and aluminum shavings..."
"The breakfast of champions!"

Member, FRC Team 449: 2007-2010
Drive Mechanics Lead, FRC Team 449: 2009-2010
Alumnus/Technical Mentor, FRC Team 449: 2010-Present
Lead Technical Mentor, FRC Team 4464: 2012-2015
Technical Mentor, FRC Team 5830: 2015-2016


FRC Drivetrain Characterization

Last edited by Oblarg : 07-15-2018 at 05:49 PM.
Reply With Quote
  #13   Spotlight this post!  
Unread 07-15-2018, 05:53 PM
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,976
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Parameters in Command

Quote:
Originally Posted by Oblarg View Post
It most certainly does - you simply have to take advantage of some sort of polymorphism in your subsystems (I recommend interfaces, but inheritance can work too). One can use the same "open claw" command on many different types of claws, for example, rather than tediously and messily writing a bunch of nearly-identical commands.

It also, as the OP noted, removes the need for global state, which is basically always a good idea.
But the OPs code doesn't do this, which is what prompted the question.
Reply With Quote
  #14   Spotlight this post!  
Unread 07-15-2018, 06:05 PM
CodeNinja CodeNinja is offline
Registered User
FRC #3997 (Screaming Chickens)
Team Role: Programmer
 
Join Date: Jan 2017
Rookie Year: 2017
Location: San Antonio
Posts: 66
CodeNinja is an unknown quantity at this point
Re: Parameters in Command

Yeah, the reason I used Master controller was to simplify the competition code. Before master controller, I had to pass in 7 params per command plus our architecture was redundant. I just changed the structure to command as an offseason project. But I might individualize the params, now that it is much less redundant.

Thanks your advice
Reply With Quote
Reply


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 10:36 AM.

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


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