Skip to content

Commit

Permalink
Migrate FakeSpeedController from aerbot-junit. This closes #56
Browse files Browse the repository at this point in the history
  • Loading branch information
jwong101 committed Sep 28, 2016
1 parent 4e984d5 commit 83eb73a
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 0 deletions.
107 changes: 107 additions & 0 deletions src/org/impact2585/lib2585/testing/FakeSpeedController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package org.impact2585.lib2585.testing;

import edu.wpi.first.wpilibj.SpeedController;

/**
* SpeedController used for testing
*/
public class FakeSpeedController implements SpeedController {

private double speed, pidOutput;
private byte sync;
private boolean inverted;

/* (non-Javadoc)
* @see edu.wpi.first.wpilibj.PIDOutput#pidWrite(double)
*/
@Override
public void pidWrite(double output) {
pidOutput = output;
}

/* (non-Javadoc)
* @see edu.wpi.first.wpilibj.SpeedController#disable()
*/
@Override
public void disable() {
speed = 0;
}

/* (non-Javadoc)
* @see edu.wpi.first.wpilibj.SpeedController#get()
*/
@Override
public double get() {
return speed;
}

/* (non-Javadoc)
* @see edu.wpi.first.wpilibj.SpeedController#set(double)
*/
@Override
public void set(double speed) {
this.speed = speed;
}

/* (non-Javadoc)
* @see edu.wpi.first.wpilibj.SpeedController#set(double, byte)
*/
@Override
public void set(double speed, byte syncGroup) {
set(speed);
sync = syncGroup;
}

/**
* @return the pidOutput
*/
public double getPidOutput() {
return pidOutput;
}

/**
* @param pidOutput the pidOutput to set
*/
protected void setPidOutput(double pidOutput) {
this.pidOutput = pidOutput;
}

/**
* @return the sync
*/
public byte getSync() {
return sync;
}

/**
* @param sync the sync to set
*/
protected void setSync(byte sync) {
this.sync = sync;
}

/* (non-Javadoc)
* @see edu.wpi.first.wpilibj.SpeedController#setInverted(boolean)
*/
@Override
public void setInverted(boolean isInverted) {
inverted = isInverted;
}

/* (non-Javadoc)
* @see edu.wpi.first.wpilibj.SpeedController#getInverted()
*/
@Override
public boolean getInverted() {
return inverted;
}

/* (non-Javadoc)
* @see edu.wpi.first.wpilibj.SpeedController#stopMotor()
*/
@Override
public void stopMotor() {
speed = 0;
}

}
70 changes: 70 additions & 0 deletions test/org/impact2585/lib2585/tests/FakeSpeedControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.impact2585.lib2585.tests;


import org.impact2585.lib2585.testing.FakeSpeedController;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;


/**
*Class for testing the FakeSpeedController
*/
public class FakeSpeedControllerTest {

private FakeSpeedController controller;
private double speed;
private boolean invert;
/**
* Set up the FakeSpeedControllerTest
*/
@Before
public void setUp() {
controller = new FakeSpeedController();
invert = false;
speed = 0;
}


/**
*Test the FakeSpeedController
*/
@Test
public void test() {
//test setting the speed
speed = 0.3;
controller.set(speed);
Assert.assertTrue(controller.get() == speed);

//test inversion
invert = true;
controller.setInverted(invert);
//for verbosity
Assert.assertTrue(controller.getInverted() == true);

//test inversion
invert = false;
controller.setInverted(invert);
Assert.assertTrue(controller.getInverted() == false);

//test negative speed
speed = -0.2;
controller.set(speed);
Assert.assertTrue(controller.get() == speed);

//test PIDController
speed = 1.0;
controller.pidWrite(speed);
Assert.assertTrue(controller.getPidOutput() == speed);

//test disabling the motor
controller.disable();
Assert.assertTrue(controller.get() == 0);

//test stopping the motor
controller.set(speed);
controller.stopMotor();
Assert.assertTrue(controller.get() == 0);
}

}

0 comments on commit 83eb73a

Please sign in to comment.