<commit_before>/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2014. All Rights Reserved.                        */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;
import java.util.logging.Logger;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import edu.wpi.first.wpilibj.PIDSource.PIDSourceParameter;
import edu.wpi.first.wpilibj.fixtures.MotorEncoderFixture;
import edu.wpi.first.wpilibj.test.AbstractComsSetup;
import edu.wpi.first.wpilibj.test.TestBench;


/**
 * @author <NAME>
 * @author <NAME>
 *
 */

@RunWith(Parameterized.class)
public class PIDTest extends AbstractComsSetup {
	private static final Logger logger = Logger.getLogger(PIDTest.class.getName());
	
	private PIDController controller = null;
	private static MotorEncoderFixture me = null;
	
	public PIDTest(MotorEncoderFixture mef){
		logger.fine("Constructor with: " + mef.getType());
		if(PIDTest.me != null && !PIDTest.me.equals(mef)) PIDTest.me.teardown();
		PIDTest.me = mef;
	}
	
	@Parameters
	public static Collection<MotorEncoderFixture[]> generateData(){
		//logger.fine("Loading the MotorList");
		return Arrays.asList(new MotorEncoderFixture[][]{
				 {TestBench.getInstance().getTalonPair()},
				 {TestBench.getInstance().getVictorPair()},
				 {TestBench.getInstance().getJaguarPair()}
			//	 TestBench.getInstance().getCanJaguarPair()
		});
	}

	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		logger.fine("TearDownAfterClass: " + me.getType());
		me.teardown();
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		logger.fine("Setup: " + me.getType());
		me.setup();
		controller = new PIDController(0.003, .001, 0, me.getEncoder(), me.getMotor());
		controller.setAbsoluteTolerance(15);
		controller.setOutputRange(-0.2, 0.2);
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
		logger.fine("Teardown: " + me.getType());
		controller.disable();
		controller.free();
		controller = null;
		me.reset();
	}
	
	@Test
	public void testInitialSettings(){
		controller.disable();
		assertTrue("PID did not start at 0", controller.getError() == 0);
	}
	
	@Test
	public void testSetSetpoint(){
		Double setpoint = 2500.0;
		controller.disable();
		controller.setSetpoint(setpoint);
		controller.enable();
		assertEquals(setpoint, new Double(controller.getSetpoint()));
	}

	@Test (timeout = 6000)
	public void testRotateToTarget() {
		double setpoint = 2500.0;
		System.out.println("Entering testRotateToTarget");
		assertEquals("PID did not start at 0", 0, controller.get(), 0);
		controller.setSetpoint(setpoint);
		assertEquals("PID did not have an error of " + setpoint, setpoint, controller.getError(), 0);
		controller.enable();
		Timer.delay(5);
		controller.disable();
		assertTrue("PID Controller Error: " + controller.getError(), controller.onTarget());
	}
	
}<commit_msg>Makes the PID Test run 10 times in order to determine how many times it fails out of 10 runs per motor. (Unstable Test!)<commit_after>/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2014. All Rights Reserved.                        */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.logging.Logger;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import edu.wpi.first.wpilibj.PIDSource.PIDSourceParameter;
import edu.wpi.first.wpilibj.fixtures.MotorEncoderFixture;
import edu.wpi.first.wpilibj.test.AbstractComsSetup;
import edu.wpi.first.wpilibj.test.TestBench;


/**
 * @author <NAME>
 * @author <NAME>
 *
 */

@RunWith(Parameterized.class)
public class PIDTest extends AbstractComsSetup {
	private static final Logger logger = Logger.getLogger(PIDTest.class.getName());
	
	private PIDController controller = null;
	private static MotorEncoderFixture me = null;
	
	private final Double p;
	private final Double i;
	private final Double d;
	
	@Rule
	public TestWatcher testWatcher = new TestWatcher() {
		protected void failed(Throwable e, Description description) {
			System.out.println();
			logger.severe("" + description.getDisplayName() + " failed " + e.getMessage());
			super.failed(e, description);
			}
		};

	
	public PIDTest(Double p, Double i, Double d, MotorEncoderFixture mef){
		logger.fine("Constructor with: " + mef.getType());
		if(PIDTest.me != null && !PIDTest.me.equals(mef)) PIDTest.me.teardown();
		PIDTest.me = mef;
		this.p = p;
		this.i = i;
		this.d = d;
	}
	
	@Parameters
	public static Collection<Object[]> generateData(){
		//logger.fine("Loading the MotorList");
		Collection<Object[]> data = new ArrayList<Object[]>();
		double kp = 0.0035;
		double ki = 0.001;
		double kd = 0.0;
		for(int i = 0; i < 10; i++){
			data.addAll(Arrays.asList(new Object[][]{
				 {kp, ki, kd, TestBench.getInstance().getTalonPair()},
				 {kp, ki, kd, TestBench.getInstance().getVictorPair()},
				 {kp, ki, kd, TestBench.getInstance().getJaguarPair()}}));
		}
		return data;
	}

	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		logger.fine("TearDownAfterClass: " + me.getType());
		me.teardown();
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		logger.fine("Setup: " + me.getType());
		me.setup();
		controller = new PIDController(p, i, d, me.getEncoder(), me.getMotor());
		controller.setAbsoluteTolerance(15);
		controller.setOutputRange(-0.2, 0.2);
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
		logger.fine("Teardown: " + me.getType());
		controller.disable();
		controller.free();
		controller = null;
		me.reset();
	}
	
	@Test
	public void testInitialSettings(){
		controller.disable();
		assertTrue("PID did not start at 0", controller.getError() == 0);
	}
	
	@Test
	public void testSetSetpoint(){
		Double setpoint = 2500.0;
		controller.disable();
		controller.setSetpoint(setpoint);
		controller.enable();
		assertEquals(setpoint, new Double(controller.getSetpoint()));
	}

	@Test (timeout = 6000)
	public void testRotateToTarget() {
		double setpoint = 2500.0;
		assertEquals(pidData() + "did not start at 0", 0, controller.get(), 0);
		controller.setSetpoint(setpoint);
		assertEquals(pidData() +"did not have an error of " + setpoint, setpoint, controller.getError(), 0);
		controller.enable();
		Timer.delay(5);
		controller.disable();
		assertTrue(pidData() + "Controller Error: " + controller.getError(), controller.onTarget());
	}
	
	private String pidData(){
		return me.getType() + " PID {P:" +controller.getP() + " I:" + controller.getI() + " D:" + controller.getD() +"} ";
	}
	
}
{'fraction_non_alphanumeric': 0.17240143369175628, 'fraction_numerical': 0.011469534050179211, 'mean_word_length': 3.2730985196528843, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2657603', 'n_tokens_mistral': 2816, 'n_tokens_neox': 2534, 'n_words': 610}