|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Need some quick advice
We are doing a demonstration at the halftime show of a school basketball game in a few hours, and I am trying to code up a button to make it easier on us.
Last night we tested and it works fine, but I do not have an intuitive control system set up. Our robot uses a belt to raise the ball to our shooter, and then we run the shooter, and fire a pneumatic kicker to kick the ball into the shooter. the main problem is with people forgetting to retract the kicker, which will stop the belt if you forget (I have done this many times), so I want it to automatically retract. I want to use the simple .get() button command to run the program, and this is what I would like it to do in pseudocode. if (joystickbutton.get()){//runs the program when button is held down if (programrun? = false){//will run program if has not been run before shootermotors.set(1);//turns on the shooter wait (untilshootersatmaxspeed);//usually about 3-4 seconds solenoid.extend;//kicks the ball into the shooter wait (untilsolenoidisextended);//waits until the solenoid is kicked fully solenoid.retract;//puts the solenoid back in position shootermotors.set(0);//turns off the motors programrun = true;//sets it so the program doesnt run more than once } } else{ programrun = false;//so that when i take finger off button program can be run again } here is my actual code which does not work as intended, I have declared everything before. if (zero.get()){//if joystick is held down if (MYVAL = false){//if program hasnt been run topshoot.set(1);//sets speed of top part of shooter bottomshoot.set(-1);//sets speed of bottom part of shooter Timer.delay(4);//waits im assuming 4 seconds s1.set(true);//sets the solenoid to the right value to extend s2.set(false);//sets the solenoid to the right value to extend Timer.delay(3);//waits im assuming 3 seconds s1.set(false);//sets the solenoid to the right value to retract s2.set(true);//sets the solenoid to the right value to retract topshoot.set(0);//turns off the top motor bottomshoot.set(0);//turns off the bottom motor MYVAL = true;//makes it so program cant be run more than once } } else{ MYVAL = false;//sets it so the program can be run again } any quick ideas about what could be wrong would be appreciated. tyvm in advance |
|
#2
|
|||
|
|||
|
Re: Need some quick advice
First, Timer.delay(x) waits for x milliseconds.
Second, I can see a lot of potential for watchdog errors with this code, since it is written like normal, top-to-bottom code as opposed to the looping thread that is used for FRC. I'm not the best at articulating my ideas so feel free to ask any other questions. Your simplest bet would be to make a button/control for each aspect(belts, kicker,shooter), even though that may be harder for the driver to remember. |
|
#3
|
||||
|
||||
|
Re: Need some quick advice
Quote:
Just a tip. |
|
#4
|
||||
|
||||
|
Re: Need some quick advice
Sounds to me like this is a job for... a Command with a state machine!
Since you want it to work easily with a .get() (presumably without hanging your robot or killing the watchdog, the poor beast), I'd use a StartCommand to initialize a Command in .get(). This command should have some sort of member determining which "state" it's in (raising belt, extending piston, retracting piston, etc.), and each execute() it should act upon this state (by continuing to do something like raising the belt, or possibly doing nothing), then check to see if it's complete (enough time has gone by, another sensor has been tripped, etc.). If the current state is complete, it advances to the next state, and if there are no more states, it either starts over (returning to its initial state) or finishes (which I think is the better choice in this situation). If you set up your command like a state machine, it can perform actions in sequence without getting in the way of the rest of the robot. Just as a possible example of a state machine, I have some code I wrote early in the season which runs a sequence of Commands (feel free to use it if it suits your needs): Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package commands;
import edu.wpi.first.wpilibj.command.Command;
/**
*
* @author Ginto8
*/
public class CommandSequence extends Command {
Command[] commands_;
int current_;
public CommandSequence(Command[] commands) {
commands_ = commands;
}
protected void initialize() {
if(commands_ == null || commands_.length == 0)
current_ = -1;
else
current_ = 0;
}
protected void execute() {
if(!commands_[current_].isRunning()) {
++current_;
if(!isFinished())
commands_[current_].start();
}
}
protected boolean isFinished() {
return current_ >= 0 && current_ <= commands_.length;
}
protected void end() {
current_ = -1;
}
protected void interrupted() {
end();
}
}
|
|
#5
|
|||
|
|||
|
Re: Need some quick advice
in OI:
Code:
JoystickButton doStuff = new JoystickButton(joysticknumber, buttonnumber); doStuff.whenReleased(new Shoot()); Code:
package edu.wpi.first.wpilibj.templates.commands;
/**
*
* @author Thundrio
*/
public class ExampleCommand extends CommandBase {
Timer t = new Timer();
protected void initialize() {
shooterMotors.set(1);
t.reset();
t.start();
}
protected void execute() {
shooterMotors.set(1);
if (t.get() >= 1)
solenoid.extend();
}
protected boolean isFinished() {
if (solenoid.isExtended())
return true;
else
return false;
}
protected void end() {
shooterMotors.set(0);
}
}
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|