View Single Post
  #3   Spotlight this post!  
Unread 30-01-2011, 10:14
davidthefat davidthefat is offline
Alumni
AKA: David Yoon
FRC #0589 (Falkons)
Team Role: Alumni
 
Join Date: Jan 2011
Rookie Year: 2010
Location: California
Posts: 792
davidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud ofdavidthefat has much to be proud of
Re: Java Encoder Problem

Yes: the encoder.start()... 3 programmers literally spent 3 hours trying to figure that out... But it turned out that we also wired it up all wrong because I am an idiot... But the team captain said my soldering was pretty good.

Here are the current revisions of our code: you will be surprised how long I had to fight to get the other programmers to agree on using arrays... They thought arrays will just complicate things. Now since I was the lead programmer, I pretty much said "I am doing it" and after I did it, they liked it.... -__-;;

BTW the DataStructure is abridged; I only included the encoder stuff and took the rest out.

CVEncoder.java:
Code:
package edu.wpi.first.wpilibj.Robot2011.Perceive;

import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.Robot2011.DataStructure;

/**
 *
 * @author Robotics2011
 */
public class CVEncoder
{
    private Encoder spin[];
    private int numOfEncoders;

    public CVEncoder(int n)//takes in number of encoders
    {
        numOfEncoders = n;
        if(numOfEncoders > 8)
        {
            numOfEncoders = 8;
        }
        else if(numOfEncoders < 0)
        {
            numOfEncoders = 0;
        }

        spin = new Encoder[numOfEncoders];
        int counter = 1;
        for(int i = 0; i < numOfEncoders; i ++)
        {
            spin[i] = new Encoder(i + counter, i + counter + 1);
            counter ++;
        }
        for(int i = 0; i < numOfEncoders; i ++)
        {
            spin[i].start();//took 3 hours to figure this out, but electronics was still at fault
        }
    }

    public void setRotation(double circumference, int c)//to caculate rate and dist; circumference in feet
    {
        spin[c].setDistancePerPulse(circumference / 360.0);//cirumference divided by 360 degrees [per pulse]
    }
    //rate - ft/s
    //dist - ft
    //direction - 1 is forward, -1 if backward
    public void update(DataStructure dat)//update inhited privates with dist, dir, and rate
    {
        for(int num = 0; num < numOfEncoders; num ++)
        {
            //Direction formatting
            double direction = 0;
            if(spin[num].getStopped() == false && spin[num].getDirection() == true)//allegedly, Encoder class returns negative int for forward
                direction = 1.0;
            else if(spin[num].getStopped() == false && spin[num].getDirection() == false)
                direction = -1.0;
            dat.setDirection(num, direction);
            dat.setDistance(num, spin[num].getDistance());
            dat.setRate(num, spin[num].getRate());
            dat.setStopped(num, spin[num].getStopped());
            dat.setPulses(num, spin[num].get());
        }
    }
    public void neutralize(DataStructure dat)
    {
        for(int num = 0; num < numOfEncoders; num ++)
        {
            spin[num].stop();
            spin[num].start();
            spin[num].reset();
        }
        update(dat);
    }
    public int numberOfEncoders()
    {
        return numOfEncoders;
    }
}
DataStructure.java:
Code:
package edu.wpi.first.wpilibj.Robot2011;
/**
 *
 * @author Robotics2011
 */
public class DataStructure
{
    //Encoder Variables
    private int numEncoders;//set number of encoders
    private double rate[];
    private double distance[];
    private double direction[];
    private double pulses[];
    private boolean stopped[];

  public DataStructure()
    {
        //Setting Encoder Variables to 0
        numEncoders = 0;
    }
public void constructEncoders(int num)
    {
        numEncoders = num;
        rate = new double[numEncoders];
        distance = new double[numEncoders];
        direction = new double[numEncoders];
        pulses = new double[numEncoders];
        stopped = new boolean[numEncoders];
        for(int i = 0; i < numEncoders; i ++)
        {
            rate[i] = 0.0;
            distance[i] = 0.0;
            direction[i] = 0.0;
            pulses[i] = 0.0;
            stopped[i] = true;
        }
    }

//Start Encoder Accessors and Manipulators
    public void setRate(int num, double x)
    {
        rate[num] = x;
    }
    public double getRate(int num)
    {
        return rate[num];
    }
    public void setDistance(int num, double d)
    {
        distance[num] = d;
    }
    public double getDistance(int num)
    {
        return distance[num];
    }
    public void setDirection(int num, double d)
    {
        direction[num] = d;
    }
    public double getDirection(int num)
    {
        return direction[num];
    }
    public void setPulses(int num, double i)
    {
        pulses[num] = i;
    }
    public double getPulses(int num)
    {
        return pulses[num];
    }
    public void setStopped(int num, boolean b)
    {
        stopped[num] = b;
    }
    public boolean getStopped(int num)
    {
        return stopped[num];
    }
    //End Encoder Accessors and Manipulators
}
__________________
Do not say what can or cannot be done, but, instead, say what must be done for the task at hand must be accomplished.
Reply With Quote