View Single Post
  #6   Spotlight this post!  
Unread 26-06-2015, 17:18
Arhowk's Avatar
Arhowk Arhowk is offline
FiM CSA
AKA: Jake Niman
FRC #1684 (The Chimeras) (5460 Mentor)
 
Join Date: Jan 2013
Rookie Year: 2013
Location: Lapeer
Posts: 542
Arhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to behold
Re: Capping Speed in Java

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})

Code:
/*
 * 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.
__________________
FRC Team 1684 - Head Programmer (2013-2016)
FRC Team 5460 - Programming Mentor (2015-2016)

FIRST in Michigan - Technical Crew (2015-continuing)
Reply With Quote