Go to Post Competition and inspiration go hand in hand. Those who inspire will be pushed to do it again and again by those who see this as a healthy competition. Those who are inspired will compete to be an inspiration to others. You can have your cake (competition) and eat it (inspire) too. - Andy Baker [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rating: Thread Rating: 13 votes, 4.69 average. Display Modes
Prev Previous Post   Next Post Next
  #4   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
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 12:10.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi