Package com.grt192.sensor

Source Code of com.grt192.sensor.GRTEncoder

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.grt192.sensor;

import com.grt192.core.Sensor;
import com.grt192.event.component.EncoderEvent;
import com.grt192.event.component.EncoderListener;
import edu.wpi.first.wpilibj.Encoder;
import java.util.Vector;

/**
*
* @author grtstudent
*/
public class GRTEncoder extends Sensor{
    private Encoder rotaryEncoder;
    private Vector encoderListeners;
    public static final double DIST_PER_PULSE = Math.PI * 16/(360 * 4 *12);


    public GRTEncoder(int channela, int channelb, int pollTime, String id){
        rotaryEncoder = new Encoder(channela, channelb);
        rotaryEncoder.start();
        rotaryEncoder.setDistancePerPulse(DIST_PER_PULSE);
        setSleepTime(pollTime);
        encoderListeners = new Vector();
        this.id = id;
    }

    public void poll() {
        double previous = getState("Direction");
        setState("Direction", rotaryEncoder.getDirection());
        if(previous != getState("Direction"))
            this.notifyDirectionChanged();
       
        setState("Rate", rotaryEncoder.getRate());

        setState("Count", rotaryEncoder.get());

        setState("Distance", rotaryEncoder.getDistance());
        if(previous != getState("Distance"))
            this.notifyEncoderChange();

        setState("Stopped", rotaryEncoder.getStopped());
        if(previous != getState("Stopped"))
            if(getState("Stopped") == Sensor.TRUE)
                this.notifyEncoderStopped();
            else
                this.notifyEncoderStarted();

    }

    public void addEncoderListener(EncoderListener a){
        encoderListeners.addElement(a);
    }
    public void removeEncoderListener(EncoderListener a){
        encoderListeners.removeElement(a);
    }

    protected void notifyEncoderChange(){
        for(int i=0; i<encoderListeners.size(); i++){
            ((EncoderListener)
                    encoderListeners.elementAt(i)).countDidChange(
                            new EncoderEvent(this,
                                                   EncoderEvent.DEFAULT,
                                                   getState("Distance"),
                                                   (getState("Direction")
                                                            == Sensor.TRUE)
                                                   )
                            );
        }
    }

    protected void notifyEncoderStarted(){
        for(int i=0; i<encoderListeners.size(); i++){
            ((EncoderListener)
                    encoderListeners.elementAt(i)).rotationDidStart(
                            new EncoderEvent(this,
                                                   EncoderEvent.DEFAULT,
                                                   getState("Distance"),
                                                   (getState("Direction")
                                                            == Sensor.TRUE)
                                                   )
                            );
        }
    }

    protected void notifyEncoderStopped(){
        for(int i=0; i<encoderListeners.size(); i++){
            ((EncoderListener)
                    encoderListeners.elementAt(i)).rotationDidStop(
                            new EncoderEvent(this,
                                                   EncoderEvent.DEFAULT,
                                                   getState("Distance"),
                                                   (getState("Direction")
                                                            == Sensor.TRUE)
                                                   )
                            );
        }
    }
    protected void notifyDirectionChanged(){
        for(int i=0; i<encoderListeners.size(); i++){
            ((EncoderListener)
                    encoderListeners.elementAt(i)).rotationDidStop(
                            new EncoderEvent(this,
                                                   EncoderEvent.DEFAULT,
                                                   getState("Distance"),
                                                   (getState("Direction")
                                                            == Sensor.TRUE)
                                                   )
                            );
        }
    }

}
TOP

Related Classes of com.grt192.sensor.GRTEncoder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.