Skip to content

Commit

Permalink
Going through and cleaning things up
Browse files Browse the repository at this point in the history
  • Loading branch information
bign8 committed May 1, 2012
1 parent 5d5cb7b commit abd9a97
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/Controller.java
Expand Up @@ -45,7 +45,7 @@ public static void main(String args[]) {
brainThr.setPriority(Thread.MAX_PRIORITY-2);
brainThr.start();

// Debugging - can comment out for production
// Debugging
Debugger debug = new Debugger(engine, wheel, headlight, brain, garmin, remote);
Thread debugThr = new Thread(debug);
debugThr.setPriority(Thread.MAX_PRIORITY-5);
Expand Down
20 changes: 20 additions & 0 deletions src/Debuggable.java
@@ -1,5 +1,25 @@
/*
* This interface allows a constant implementation of runnable thread
* A better implementation would be to have an abstract interface that all the classes could extend
* Unfortunantly, its a day away from the due date, so this is how its going to stay
*/

public interface Debuggable {
public void setRunning( boolean run );
public String[] toDebugString(String in[]);
}


class RobotThread extends Thread implements Debuggable {

public void setRunning(boolean run) {
// TODO Auto-generated method stub

}

public String[] toDebugString(String[] in) {
// TODO Auto-generated method stub
return null;
}

}
77 changes: 36 additions & 41 deletions src/Engine.java
Expand Up @@ -5,28 +5,28 @@
import com.ridgesoft.robotics.PIDController;

public class Engine implements Runnable, Debuggable {
private boolean running;

// Port Reservations
private static final int ServoOutputPort = 3;
private static final int AnologueInput1 = 11;
private static final int AnologueInput2 = 10;

// PID Controller gain constants
public float pGain = 0.060f; // 0.053
public float iGain = 0.001f; // 0.001
public float dGain = 0.005f; // 0.01
public float iCapp = 16.0f; // 40.0

// Port Reservations
private static int ServoOutputPort = 3;
private static int AnologueInput1 = 11;
private static int AnologueInput2 = 10;

// Stored devices
private boolean running = true;
private Motor motor;
private IntelliBrainShaftEncoder encoder;
private PIDController controller = new PIDController(pGain, iGain, dGain, iCapp, false);

// Algorithum variables
private int rpm = 0, velocity = 0, power = 0, desired = 0;
//private int[] arrayOfSpeeds = {-120, -90, -60, 0, 30, 60, 90};

// Constructor - get all required variables and execute a quick startup on motor controler
public Engine(){

// get what I need (or want)
Expand All @@ -43,57 +43,52 @@ public Engine(){
try { Thread.sleep(2000); } catch (Throwable t) { t.printStackTrace(); }
}

public void setRunning(boolean run) {
running = run;
setSpeed(0);
}

public void updatePID() {
controller.setConstants(pGain, iGain, dGain, iCapp);
}

// Main running thread - calculate rpm and use PID controler to set power based on set velocity
public void run(){
running = true;
int previousCounts = 0, counts = 0;//, counter = 0;
int previousCounts = 0, counts = 0;
long time = System.currentTimeMillis();

while (true) {
try {

if (!running) {
time += 2000;
Thread.sleep(time - System.currentTimeMillis());
} else {

//600 count intervals are taken per minute.
//128 counts per revolution.
counts = encoder.getCounts();
rpm = ((counts - previousCounts) * 600) / 128;
previousCounts = counts;

desired = velocity * 40;
power = (int)controller.control(desired, rpm);

motor.setPower(power);

// Pause thread execution
time += 100;
Thread.sleep(time - System.currentTimeMillis());
}
if (running) {

//600 count intervals are taken per minute.
//128 counts per revolution.
counts = encoder.getCounts();
rpm = ((counts - previousCounts) * 600) / 128;
previousCounts = counts;

desired = velocity * 30;
power = (int)controller.control(desired, rpm);

motor.setPower(power);

} else {
motor.setPower(0);
}

// Pause thread execution
try {
time += 100;
Thread.sleep(time - System.currentTimeMillis());
} catch (Throwable t) { t.printStackTrace(); }
}

}

// Method to pause the execution of this thread
public void setRunning(boolean run) { running = run; }

// Method to define speed of the motor
public void setSpeed( int v ) {
velocity = v;
if (v == 0) motor.brake();
}
//public int getRPM() { return rpm; }
//public void brake() { motor.brake(); this.velocity = 0; } // something else should probably happen here

// Debug Methods (private accessorss)
public int getSpeed() { return velocity; }
//public int getRPM() { return rpm; }
public void updatePID() { controller.setConstants(pGain, iGain, dGain, iCapp); }

public String[] toDebugString(String in[]) {
in[0] = "Engine " + desired;
Expand Down
59 changes: 46 additions & 13 deletions src/Remote.java
Expand Up @@ -2,32 +2,65 @@
import com.ridgesoft.robotics.AnalogInput;
import com.ridgesoft.robotics.Smoother;

/*
* Note: This class is designed for reading input from an "AR-1 2 channel reciever with reverse" from axial
* This device usally is directly connected to the servos, and thus produces a square wave
* The frequency of this square wave is to fast for the Intellibrain 2 processor to 'count' the lenght of a set or cleared bit
* This problem was fixed using a low pass filter that was turned the square wave into a mostly linear function we could sample
*
* X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X
* X X X X
* X o-----/\/\/\/\------+-------o X V | +---+ +---+ X V | X
* X 15 k Ohm | X | | | | | X | X
* X --+-- X | | | in | | X | out X
* X in 10 uF out X | | | | | X | X
* X --+-- X | | | | | X |-------------------------------- X
* X | X |---+ +-------------+ +------ X | X
* X o-------------------+-------o X 0-------------------------------- X 0-------------------------------- X
* X X X X
* X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X
*/

public class Remote implements Runnable, Debuggable {
private boolean running = true; // important

// Port Reservations
private static final int AnologueInput1 = 2;
private static final int AnologueInput2 = 3;

// Algorithm available variables
private boolean running = true, remoteOn = false;
private AnalogInput input1, input2;
private boolean remoteOn;
private int port1, port2;

private Smoother sampleP1, sampleP2;

// Remote Constructor - optain analog input ports and initiate smoother
public Remote(){
input1 = IntelliBrain.getAnalogInput(2);
input2 = IntelliBrain.getAnalogInput(3);
input1 = IntelliBrain.getAnalogInput(AnologueInput1);
input2 = IntelliBrain.getAnalogInput(AnologueInput2);

sampleP1 = new Smoother(0.2f, input1.sample());
sampleP2 = new Smoother(0.2f, input2.sample());
}

// Main running thread - recieve data from remote and smooth
public void run(){
int temp1, temp2;
long time = System.currentTimeMillis();

while(true){
if (running) {
// TODO look at getting wider data ranges

port1 = (int) (sampleP1.smooth(input1.sample())/5 - 12);
port2 = (int) (sampleP2.smooth(input2.sample())/5 - 12);
temp1 = input1.sample();
temp2 = input2.sample();

remoteOn = ( temp1 > 35 ) && ( temp2 > 35 ) && ( temp1 < 85 ) && ( temp2 < 85 );

remoteOn = ( port1 > -5 ) && ( port2 > -5 ) && ( port1 < 5 ) && ( port2 < 5 ); // added cap - absolutely necessary!
port1 = (int) ( sampleP1.smooth(temp1) / 5 - 12 );
port2 = (int) ( sampleP2.smooth(temp2) / 5 - 12 );

} else {
remoteOn = false;
}

try {
Expand All @@ -37,14 +70,14 @@ public void run(){
}
}

// Debugging methods and accessors
public boolean isOn(){ return remoteOn; }
public int getPort1(){ return port1; }
public int getPort2(){ return port2; }
public void setRunning(boolean run) { running = run; }
public String[] toDebugString(String in[]) {
in[0] = "Remote Debug";
in[1] = ( remoteOn ? "On" : "Off" ) + " " + port1 + " " + port2;
return in;
}

public boolean isOn(){ return remoteOn; }
public int getPort1(){ return port1; }
public int getPort2(){ return port2; }
public void setRunning(boolean run) { running = run; }
}

0 comments on commit abd9a97

Please sign in to comment.