Log in

View Full Version : Capping Speed in Java


iggy_gim
02-06-2015, 18:41
Hello all,

I thought I posted this topic here yesterday but I don't see it. Forgive me if I actually did post it somewhere else and am now repeating myself.

Our team is going to be doing a demonstration at a local elementary school and we are going to let the students drive our robot for a while. Is it possible to cap the speed of the robot via programming (for a quick fix) via Java. Thank you!

thatprogrammer
02-06-2015, 18:55
Yes. If you're using tank or arcade, just make it so the output of the drive motors is hapf, or whatever you need the max to be, of the joysticks' x and y values. If you need further explication or an example, just ask and I can pm this or or post it here.

dubiousSwain
02-06-2015, 21:42
dividing the speed in half also works for mechanum

GeeTwo
03-06-2015, 00:09
If you want to be really slick, leverage object orientation to the fullest, and reduce the number of places you'll have to modify code, you could create a new class called (for example) "limited_controller" that extends your current motor controller class. The code for this class basically recreates all of the methods of the superclass through use of super, and modifies only the "set" method to enforce the top limit, or divide by 2, or whatever. Then, when you initialize your motor controllers, use "new limited_controller([args])" rather than (for example) "new Talon([args])".

kiningsdd
18-06-2015, 02:06
Dividing mechanism may that help you.

Arhowk
26-06-2015, 17:18
I'll also leave this code here. Just initialize this class with the joystick port you desire and any button on that controller (or left/right trigger for xbox controllers, axes drifted too much) and the robot stops until you release said button. (To do this, call pollForPressed() than if(!good()){kill robot})

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package org.chimeras1684.year2014.iterative.aaroot;
import edu.wpi.first.wpilibj.Joystick;

/**
*
* @author Arhowk
*/
public class DeadSwitch {

final static int MAX_NUM_BUTTONS = 10;
final static int MAX_NUM_AXES = 6;
final static double TRIGGER_POLL = 0.5;

m_DR reciever;

private static class m_DR{
private Joystick djoy = null;

public boolean pollButton(int axis){
if(djoy != null) return djoy.getRawButton(axis);
else return false;
}

public boolean pollRightTrigger(){
if(djoy != null){
// System.out.println("right trigger : " + (djoy.getRawAxis(3) < -TRIGGER_POLL));
return djoy.getRawAxis(3) < -TRIGGER_POLL;
}
return false;
}
public boolean pollLeftTrigger(){
if(djoy != null){
// System.out.println("left trigger : " + (djoy.getRawAxis(3) > TRIGGER_POLL));
return djoy.getRawAxis(4) > TRIGGER_POLL;
}
return false;
}

public static m_DR joystick(Joystick j){
m_DR m = new m_DR();
m.djoy = j;
return m;
}
}

private boolean [] axes;
private double [] joys;

private boolean leftTrigger, rightTrigger;

private int last = -1;

public DeadSwitch(int port){
reciever = m_DR.joystick(new Joystick(port));
axes = new boolean [MAX_NUM_BUTTONS];
joys = new double [MAX_NUM_AXES];
}

public int pollForPressed(){

for(int i = 0; i < MAX_NUM_BUTTONS; i++){
if(reciever.pollButton(i)){
if(!axes[i]){
last = i;
axes[i] = true;
}
}else axes[i] = false;
}

if(reciever.pollLeftTrigger()){
if(!leftTrigger){
leftTrigger = true;
last = 100;
}
}else leftTrigger = false;

if(reciever.pollRightTrigger()){
if(!rightTrigger){
rightTrigger = true;
last = 99;
}
}else rightTrigger = false;

return last;
}

public boolean good(){
if(last == -1) return true;
else if(last == 99) return reciever.pollRightTrigger();
else if(last == 100) return reciever.pollLeftTrigger();
else return reciever.pollButton(last);
}

public void disabledInit(){
pollForPressed();
last = -1;
}

}


Our team used this in tandem with speed controls for demos, mainly because a half speed robot can still cause some serious damage.