[wpilib] Move motor controllers to motorcontrol package (#3302)

Also deprecate SpeedController in favor of motorcontrol.MotorController and
SpeedControllerGroup in favor of motorcontrol.MotorControllerGroup.

The MotorController interface is derived from the SpeedController interface
so that code such as SpeedController x = new VictorSP(1) continues to
compile (just with a warning).

SpeedControllerGroup and MotorControllerGroup are independent classes;
both implement the MotorController interface.
This commit is contained in:
Peter Johnson
2021-04-17 11:27:16 -07:00
committed by GitHub
parent b7b178f49c
commit 0abf6c9045
194 changed files with 1096 additions and 696 deletions

View File

@@ -1,101 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.wpilibj;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class SpeedControllerGroupTest {
private static Stream<Arguments> speedControllerArguments() {
return IntStream.of(1, 2, 3)
.mapToObj(
number -> {
SpeedController[] speedControllers =
Stream.generate(MockSpeedController::new)
.limit(number)
.toArray(SpeedController[]::new);
SpeedControllerGroup group =
new SpeedControllerGroup(
speedControllers[0],
Arrays.copyOfRange(speedControllers, 1, speedControllers.length));
return Arguments.of(group, speedControllers);
});
}
@ParameterizedTest
@MethodSource("speedControllerArguments")
void setTest(final SpeedControllerGroup group, final SpeedController[] speedControllers) {
group.set(1.0);
assertArrayEquals(
DoubleStream.generate(() -> 1.0).limit(speedControllers.length).toArray(),
Arrays.stream(speedControllers).mapToDouble(SpeedController::get).toArray(),
0.00005);
}
@ParameterizedTest
@MethodSource("speedControllerArguments")
void getInvertedTest(final SpeedControllerGroup group, final SpeedController[] speedControllers) {
group.setInverted(true);
assertTrue(group.getInverted());
}
@ParameterizedTest
@MethodSource("speedControllerArguments")
void setInvertedDoesNotModifySpeedControllersTest(
final SpeedControllerGroup group, final SpeedController[] speedControllers) {
group.setInverted(true);
assertArrayEquals(
Stream.generate(() -> false).limit(speedControllers.length).toArray(),
Arrays.stream(speedControllers).map(SpeedController::getInverted).toArray());
}
@ParameterizedTest
@MethodSource("speedControllerArguments")
void setInvertedDoesInvertTest(
final SpeedControllerGroup group, final SpeedController[] speedControllers) {
group.setInverted(true);
group.set(1.0);
assertArrayEquals(
DoubleStream.generate(() -> -1.0).limit(speedControllers.length).toArray(),
Arrays.stream(speedControllers).mapToDouble(SpeedController::get).toArray(),
0.00005);
}
@ParameterizedTest
@MethodSource("speedControllerArguments")
void disableTest(final SpeedControllerGroup group, final SpeedController[] speedControllers) {
group.set(1.0);
group.disable();
assertArrayEquals(
DoubleStream.generate(() -> 0.0).limit(speedControllers.length).toArray(),
Arrays.stream(speedControllers).mapToDouble(SpeedController::get).toArray(),
0.00005);
}
@ParameterizedTest
@MethodSource("speedControllerArguments")
void stopMotorTest(final SpeedControllerGroup group, final SpeedController[] speedControllers) {
group.set(1.0);
group.stopMotor();
assertArrayEquals(
DoubleStream.generate(() -> 0.0).limit(speedControllers.length).toArray(),
Arrays.stream(speedControllers).mapToDouble(SpeedController::get).toArray(),
0.00005);
}
}

View File

@@ -2,9 +2,9 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.wpilibj;
package edu.wpi.first.wpilibj.motorcontrol;
public class MockSpeedController implements SpeedController {
public class MockMotorController implements MotorController {
private double m_speed;
private boolean m_isInverted;

View File

@@ -0,0 +1,101 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package edu.wpi.first.wpilibj.motorcontrol;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class MotorControllerGroupTest {
private static Stream<Arguments> motorControllerArguments() {
return IntStream.of(1, 2, 3)
.mapToObj(
number -> {
MotorController[] motorControllers =
Stream.generate(MockMotorController::new)
.limit(number)
.toArray(MotorController[]::new);
MotorControllerGroup group =
new MotorControllerGroup(
motorControllers[0],
Arrays.copyOfRange(motorControllers, 1, motorControllers.length));
return Arguments.of(group, motorControllers);
});
}
@ParameterizedTest
@MethodSource("motorControllerArguments")
void setTest(final MotorControllerGroup group, final MotorController[] motorControllers) {
group.set(1.0);
assertArrayEquals(
DoubleStream.generate(() -> 1.0).limit(motorControllers.length).toArray(),
Arrays.stream(motorControllers).mapToDouble(MotorController::get).toArray(),
0.00005);
}
@ParameterizedTest
@MethodSource("motorControllerArguments")
void getInvertedTest(final MotorControllerGroup group, final MotorController[] motorControllers) {
group.setInverted(true);
assertTrue(group.getInverted());
}
@ParameterizedTest
@MethodSource("motorControllerArguments")
void setInvertedDoesNotModifyMotorControllersTest(
final MotorControllerGroup group, final MotorController[] motorControllers) {
group.setInverted(true);
assertArrayEquals(
Stream.generate(() -> false).limit(motorControllers.length).toArray(),
Arrays.stream(motorControllers).map(MotorController::getInverted).toArray());
}
@ParameterizedTest
@MethodSource("motorControllerArguments")
void setInvertedDoesInvertTest(
final MotorControllerGroup group, final MotorController[] motorControllers) {
group.setInverted(true);
group.set(1.0);
assertArrayEquals(
DoubleStream.generate(() -> -1.0).limit(motorControllers.length).toArray(),
Arrays.stream(motorControllers).mapToDouble(MotorController::get).toArray(),
0.00005);
}
@ParameterizedTest
@MethodSource("motorControllerArguments")
void disableTest(final MotorControllerGroup group, final MotorController[] motorControllers) {
group.set(1.0);
group.disable();
assertArrayEquals(
DoubleStream.generate(() -> 0.0).limit(motorControllers.length).toArray(),
Arrays.stream(motorControllers).mapToDouble(MotorController::get).toArray(),
0.00005);
}
@ParameterizedTest
@MethodSource("motorControllerArguments")
void stopMotorTest(final MotorControllerGroup group, final MotorController[] motorControllers) {
group.set(1.0);
group.stopMotor();
assertArrayEquals(
DoubleStream.generate(() -> 0.0).limit(motorControllers.length).toArray(),
Arrays.stream(motorControllers).mapToDouble(MotorController::get).toArray(),
0.00005);
}
}

View File

@@ -8,9 +8,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.PWMVictorSPX;
import edu.wpi.first.wpilibj.RobotController;
import edu.wpi.first.wpilibj.controller.PIDController;
import edu.wpi.first.wpilibj.motorcontrol.PWMVictorSPX;
import edu.wpi.first.wpilibj.system.plant.DCMotor;
import edu.wpi.first.wpilibj.system.plant.LinearSystemId;
import edu.wpi.first.wpilibj.util.Units;