├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── CONTRIBUTORS.txt ├── COPYRIGHT.txt ├── LICENSE.txt ├── README.md ├── ant ├── .project └── strongback │ ├── build.properties │ └── build.xml ├── build-common.xml ├── build.properties ├── build.xml ├── dependencies.xml ├── ide-config ├── strongback-java-cleanup.xml ├── strongback-java-formatter.xml └── strongback-java-templates.xml ├── libs ├── .project ├── ant │ └── ant-contrib-1.0b3.jar └── test │ ├── fest-assert-1.4-sources.jar │ ├── fest-assert-1.4.jar │ ├── fest-util-1.1.6-sources.jar │ ├── fest-util-1.1.6.jar │ ├── hamcrest-core-1.3-sources.jar │ ├── hamcrest-core-1.3.jar │ ├── junit-4.11-sources.jar │ ├── junit-4.11.jar │ ├── metrics-core-3.1.0-sources.jar │ └── metrics-core-3.1.0.jar ├── strongback-examples ├── .classpath ├── .project ├── build.xml └── src │ └── org │ └── strongback │ └── example │ └── simple │ ├── SimpleAutoTankDriveRobot.java │ └── SimpleTankDriveRobot.java ├── strongback-testing ├── .classpath ├── .project ├── build.xml └── src │ └── org │ └── strongback │ ├── ExecutableTimer.java │ ├── command │ ├── CommandTester.java │ └── WatchedCommand.java │ └── mock │ ├── Mock.java │ ├── MockAccelerometer.java │ ├── MockAngleSensor.java │ ├── MockClock.java │ ├── MockCompass.java │ ├── MockController.java │ ├── MockCurrentSensor.java │ ├── MockDistanceSensor.java │ ├── MockGyroscope.java │ ├── MockMotor.java │ ├── MockPIDController.java │ ├── MockPneumaticsModule.java │ ├── MockPowerPanel.java │ ├── MockRelay.java │ ├── MockSolenoid.java │ ├── MockSwitch.java │ ├── MockTalonSRX.java │ ├── MockTemperatureSensor.java │ ├── MockThreeAxisAccelerometer.java │ ├── MockTwoAxisAccelerometer.java │ ├── MockVoltageSensor.java │ └── MockZeroable.java ├── strongback-tests ├── .classpath ├── .project ├── build.xml └── src │ └── org │ └── strongback │ ├── AccumulatingEventWriter.java │ ├── AsyncEventRecorderTest.java │ ├── ExecutableTimerTest.java │ ├── StrongbackTest.java │ ├── command │ ├── CommandGroupTest.java │ ├── CommandRunnerTest.java │ └── sample │ │ └── Pause.java │ ├── component │ ├── AbstractDoubleValueTest.java │ ├── AngleSensorTest.java │ ├── CompassTest.java │ ├── CounterTest.java │ ├── DistanceSensorTest.java │ ├── FuseTest.java │ ├── GyroscopeTest.java │ ├── LimitedMotorTest.java │ └── SwitchTest.java │ ├── control │ ├── SoftwarePIDControllerTest.java │ └── TestableRobotState.java │ ├── hardware │ ├── HardwareTalonSRX_AnalogInputSensorTest.java │ └── HardwareTalonSRX_EncoderInputSensorTest.java │ ├── mock │ ├── AbstractDoubleMockTest.java │ ├── MockAccelerometerTest.java │ ├── MockAngleSensorTest.java │ ├── MockCompassTest.java │ └── MockPneumaticsModuleTest.java │ └── util │ └── ValuesTest.java ├── strongback-tools ├── .classpath ├── .project ├── build.xml ├── scripts │ ├── strongback.bat │ └── strongback.sh ├── src │ ├── org │ │ └── strongback │ │ │ └── tools │ │ │ ├── logdecoder │ │ │ └── LogDecoder.java │ │ │ ├── newproject │ │ │ └── NewProject.java │ │ │ └── utils │ │ │ ├── FileUtils.java │ │ │ ├── Parser.java │ │ │ ├── Printer.java │ │ │ ├── PropertiesUtils.java │ │ │ └── Version.java │ └── strongback.properties └── test │ └── org │ └── strongback │ └── tools │ ├── newproject │ └── NewProjectTest.java │ └── utils │ ├── FileUtilsTest.java │ └── VersionTest.java ├── strongback.properties ├── strongback ├── .classpath ├── .project ├── build.xml └── src │ └── org │ └── strongback │ ├── AsyncEventRecorder.java │ ├── AsyncSwitchReactor.java │ ├── DataRecordable.java │ ├── DataRecorder.java │ ├── DataRecorderChannel.java │ ├── DataRecorderChannels.java │ ├── DataRecorderDriver.java │ ├── DataWriter.java │ ├── EventRecorder.java │ ├── Executable.java │ ├── Executables.java │ ├── Executor.java │ ├── ExecutorDriver.java │ ├── FileDataWriter.java │ ├── FileEventWriter.java │ ├── Logger.java │ ├── MappedFileDataWriter.java │ ├── NoOpLogger.java │ ├── Strongback.java │ ├── StrongbackRequirementException.java │ ├── SwitchReactor.java │ ├── SystemLogger.java │ ├── annotation │ ├── Experimental.java │ ├── Immutable.java │ ├── NotImplemented.java │ ├── NotThreadSafe.java │ └── ThreadSafe.java │ ├── command │ ├── Command.java │ ├── CommandGroup.java │ ├── CommandRunner.java │ ├── CommandState.java │ ├── Commands.java │ ├── ControllerCommand.java │ ├── Requirable.java │ ├── Scheduler.java │ └── UnmanagedControllerCommand.java │ ├── components │ ├── Accelerometer.java │ ├── AngleSensor.java │ ├── Clock.java │ ├── Compass.java │ ├── Counter.java │ ├── CurrentSensor.java │ ├── DistanceSensor.java │ ├── Fuse.java │ ├── Gyroscope.java │ ├── LimitedMotor.java │ ├── Motor.java │ ├── PneumaticsModule.java │ ├── PowerPanel.java │ ├── Relay.java │ ├── Solenoid.java │ ├── SolenoidWithPosition.java │ ├── SpeedController.java │ ├── SpeedSensor.java │ ├── Stoppable.java │ ├── Switch.java │ ├── TalonSRX.java │ ├── TemperatureSensor.java │ ├── ThreeAxisAcceleration.java │ ├── ThreeAxisAccelerometer.java │ ├── TwoAxisAcceleration.java │ ├── TwoAxisAccelerometer.java │ ├── VoltageSensor.java │ ├── Zeroable.java │ ├── package-info.java │ └── ui │ │ ├── ContinuousRange.java │ │ ├── DirectionalAxis.java │ │ ├── FlightStick.java │ │ ├── Gamepad.java │ │ └── InputDevice.java │ ├── control │ ├── Controller.java │ ├── PIDController.java │ ├── SoftwarePIDController.java │ └── TalonController.java │ ├── drive │ ├── MecanumDrive.java │ └── TankDrive.java │ ├── function │ ├── DoubleBiFunction.java │ ├── DoubleToDoubleFunction.java │ ├── IntBiFunction.java │ ├── IntToBooleanFunction.java │ └── IntToIntFunction.java │ ├── hardware │ ├── Hardware.java │ ├── HardwareDoubleSolenoid.java │ ├── HardwareMotor.java │ ├── HardwarePneumaticsModule.java │ ├── HardwareRelay.java │ ├── HardwareSpark.java │ ├── HardwareTalonController.java │ └── HardwareTalonSRX.java │ └── util │ ├── Collections.java │ ├── Iterators.java │ ├── Metronome.java │ └── Values.java └── templates ├── .project ├── Robot.java.template ├── Strongback.userlibraries.import.template ├── Strongback.userlibraries.template ├── TestRobot.java.template ├── build.properties.template ├── build.xml.template ├── classpath.template └── project.template /.gitignore: -------------------------------------------------------------------------------- 1 | # Eclipse top-level resource files are excluded ... 2 | .metadata/ 3 | 4 | # Some Eclipse project resource files should never be checked in ... 5 | .recommenders/ 6 | .settings/ 7 | 8 | # These are names of directories typically used for generated content, so are ignored ... 9 | bin/ 10 | build/ 11 | deploy/ 12 | target/ 13 | *.class 14 | 15 | # The 3rd party dependencies should never be committed 16 | libs/wpilib 17 | libs/ctre 18 | libs/navx-mxp 19 | downloads/ 20 | 21 | .DS_Store/ 22 | 23 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: java 3 | before_install: 4 | - ant -version 5 | - wget http://www-us.apache.org/dist//ant/binaries/apache-ant-1.10.1-bin.tar.gz 6 | - tar -xf apache-ant-1.10.1-bin.tar.gz 7 | - export ANT_HOME=$PWD/apache-ant-1.10.1 8 | - export PATH=$ANT_HOME/bin:$PATH 9 | - ant -version 10 | 11 | install: ant deps 12 | script: ant test 13 | jdk: 14 | - oraclejdk8 15 | -------------------------------------------------------------------------------- /CONTRIBUTORS.txt: -------------------------------------------------------------------------------- 1 | Matthew Alonso 2 | Zach Anderson 3 | Nathan Brown 4 | Adam Gausmann 5 | Colin Hauch 6 | Randall Hauch 7 | Filip Kernan 8 | Rothanak So 9 | Braden Steffaniak 10 | Adam Gausmann 11 | Parth Oza 12 | Raa'Shaun H 13 | Rothanak So 14 | Dennis Fuglsang 15 | -------------------------------------------------------------------------------- /COPYRIGHT.txt: -------------------------------------------------------------------------------- 1 | Copyright 2015-2017, Strongback and individual contributors identified by the @authors tags and in the CONTRIBUTORS file. 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Strongback 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /ant/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | ant 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /ant/strongback/build.properties: -------------------------------------------------------------------------------- 1 | strongback.home=${user.home}/strongback 2 | strongback.ant.dir=${strongback.home}/java/ant/ 3 | strongback=${strongback.home}/java/lib/strongback.jar 4 | 5 | strongback.classpath=${classpath}:${strongback} 6 | -------------------------------------------------------------------------------- /build.properties: -------------------------------------------------------------------------------- 1 | strongback.version=2017.2.0 2 | # 3 | # The build will download a specific version of the WPILib given by the following URL 4 | # and install it into the 'libs/wpilib' folder. To use a different version of WPILib, 5 | # replace the value with the URL of the WPILib Eclipse Update Site 6 | # 7 | wpilib.updatesite.url=http://first.wpi.edu/FRC/roborio/release/eclipse/ 8 | 9 | # 10 | # Starting in 2017, WPILib will no longer have built-in support for the TalonSRX 11 | # from Cross the Road Electronics (CTRE). Instead, the `CANTalon` class and associated 12 | # functionality is available directly from CTRE as a separate JAR. 13 | # 14 | ctre.download.url=http://www.ctr-electronics.com/downloads/lib/CTRE_FRCLibs_NON-WINDOWS.zip 15 | 16 | # 17 | # The navX-MXP library is available in a ZIP file that has only the Java and C++ libraries. 18 | # 19 | navx.download.url=http://www.kauailabs.com/public_files/navx-mxp/navx-mxp-libs.zip -------------------------------------------------------------------------------- /dependencies.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 18 | 19 | 20 | 21 | 24 | 25 | 26 | 27 | 30 | 31 | 32 | 33 | 36 | 37 | -------------------------------------------------------------------------------- /ide-config/strongback-java-templates.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /libs/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | libs 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /libs/ant/ant-contrib-1.0b3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strongback/strongback-java/dc1914a3f91db91d5cb0c8376237e662481d4989/libs/ant/ant-contrib-1.0b3.jar -------------------------------------------------------------------------------- /libs/test/fest-assert-1.4-sources.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strongback/strongback-java/dc1914a3f91db91d5cb0c8376237e662481d4989/libs/test/fest-assert-1.4-sources.jar -------------------------------------------------------------------------------- /libs/test/fest-assert-1.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strongback/strongback-java/dc1914a3f91db91d5cb0c8376237e662481d4989/libs/test/fest-assert-1.4.jar -------------------------------------------------------------------------------- /libs/test/fest-util-1.1.6-sources.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strongback/strongback-java/dc1914a3f91db91d5cb0c8376237e662481d4989/libs/test/fest-util-1.1.6-sources.jar -------------------------------------------------------------------------------- /libs/test/fest-util-1.1.6.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strongback/strongback-java/dc1914a3f91db91d5cb0c8376237e662481d4989/libs/test/fest-util-1.1.6.jar -------------------------------------------------------------------------------- /libs/test/hamcrest-core-1.3-sources.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strongback/strongback-java/dc1914a3f91db91d5cb0c8376237e662481d4989/libs/test/hamcrest-core-1.3-sources.jar -------------------------------------------------------------------------------- /libs/test/hamcrest-core-1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strongback/strongback-java/dc1914a3f91db91d5cb0c8376237e662481d4989/libs/test/hamcrest-core-1.3.jar -------------------------------------------------------------------------------- /libs/test/junit-4.11-sources.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strongback/strongback-java/dc1914a3f91db91d5cb0c8376237e662481d4989/libs/test/junit-4.11-sources.jar -------------------------------------------------------------------------------- /libs/test/junit-4.11.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strongback/strongback-java/dc1914a3f91db91d5cb0c8376237e662481d4989/libs/test/junit-4.11.jar -------------------------------------------------------------------------------- /libs/test/metrics-core-3.1.0-sources.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strongback/strongback-java/dc1914a3f91db91d5cb0c8376237e662481d4989/libs/test/metrics-core-3.1.0-sources.jar -------------------------------------------------------------------------------- /libs/test/metrics-core-3.1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/strongback/strongback-java/dc1914a3f91db91d5cb0c8376237e662481d4989/libs/test/metrics-core-3.1.0.jar -------------------------------------------------------------------------------- /strongback-examples/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /strongback-examples/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | strongback-examples 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | edu.wpi.first.wpilib.plugins.core.nature.FRCProjectNature 17 | 18 | 19 | -------------------------------------------------------------------------------- /strongback-examples/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /strongback-testing/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /strongback-testing/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | strongback-testing 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | edu.wpi.first.wpilib.plugins.core.nature.FRCProjectNature 17 | 18 | 19 | -------------------------------------------------------------------------------- /strongback-testing/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/command/CommandTester.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.command; 18 | 19 | 20 | /** 21 | * A Strongback testing utility that can be used in unit tests 22 | */ 23 | public class CommandTester { 24 | private final CommandRunner runner; 25 | 26 | public CommandTester(Command command) { 27 | runner = new CommandRunner(command); 28 | } 29 | 30 | /** 31 | * Steps through all of the state logic for its {@link Command}. 32 | * 33 | * @param timeInMillis the current system time in milliseconds 34 | * @return {@code true} if this {@link CommandRunner} is ready to be terminated; {@code false} otherwise 35 | */ 36 | public boolean step(long timeInMillis) { 37 | return runner.step(timeInMillis); 38 | } 39 | 40 | /** 41 | * Schedules its {@link Command} to be canceled next iteration. 42 | */ 43 | public void cancel() { 44 | runner.cancel(); 45 | } 46 | 47 | public boolean isCancelled() { 48 | return runner.isCancelled(); 49 | } 50 | 51 | @Override 52 | public String toString() { 53 | return runner.toString(); 54 | } 55 | } -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockAccelerometer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | import org.strongback.components.Accelerometer; 21 | 22 | /** 23 | * An {@link Accelerometer} implementation useful for testing, where the acceleration can be set in the test case so that it is 24 | * read by the component that uses an {@link Accelerometer}. 25 | * 26 | * @author Randall Hauch 27 | */ 28 | @ThreadSafe 29 | public class MockAccelerometer implements Accelerometer { 30 | 31 | private volatile double accel; 32 | 33 | @Override 34 | public double getAcceleration() { 35 | return accel; 36 | } 37 | 38 | /** 39 | * Set the acceleration value {@link #getAcceleration() returned} by this object. 40 | * 41 | * @param accel the acceleration value 42 | * @return this instance to enable chaining methods; never null 43 | */ 44 | public MockAccelerometer setAcceleration(double accel) { 45 | this.accel = accel; 46 | return this; 47 | } 48 | 49 | @Override 50 | public String toString() { 51 | return Double.toString(getAcceleration()) + " g/s\u00B2"; 52 | } 53 | } -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockAngleSensor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | import org.strongback.components.AngleSensor; 21 | 22 | /** 23 | * An {@link AngleSensor} implementation useful for testing, where the angle can be explicitly set in the test case so that the 24 | * known value is read by the component that uses an {@link AngleSensor}. 25 | * 26 | * @author Randall Hauch 27 | */ 28 | @ThreadSafe 29 | public class MockAngleSensor extends MockZeroable implements AngleSensor { 30 | 31 | @Override 32 | public MockAngleSensor zero() { 33 | super.zero(); 34 | return this; 35 | } 36 | 37 | @Override 38 | public double getAngle() { 39 | return super.getValue(); 40 | } 41 | 42 | /** 43 | * Set the angle value {@link #getAngle() returned} by this object. 44 | * 45 | * @param angle the angle value 46 | * @return this instance to enable chaining methods; never null 47 | */ 48 | public MockAngleSensor setAngle(double angle) { 49 | super.setValue(angle); 50 | return this; 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | return Double.toString(getAngle()) + "\u00B0"; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockClock.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import java.util.concurrent.atomic.AtomicLong; 20 | 21 | import org.strongback.components.Clock; 22 | 23 | /** 24 | * A very simple mock {@link Clock} implementation that advances only when manually {@link #incrementBySeconds incremented}. 25 | * 26 | * @author Randall Hauch 27 | */ 28 | public class MockClock implements Clock { 29 | 30 | private final AtomicLong ticker = new AtomicLong(1000 * 1000 * 20); // 2 seconds 31 | 32 | public MockClock() { 33 | } 34 | 35 | /** 36 | * Increment the clock by the specified number of seconds. 37 | * 38 | * @param seconds the number of seconds; must be positive 39 | * @return this instance to enable chaining methods; never null 40 | */ 41 | public MockClock incrementBySeconds(long seconds) { 42 | return incrementByMicroseconds(1000 * 1000 * seconds); 43 | } 44 | 45 | /** 46 | * Increment the clock by the specified number of milliseconds. 47 | * 48 | * @param miliseconds the number of milliseconds; must be positive 49 | * @return this instance to enable chaining methods; never null 50 | */ 51 | public MockClock incrementByMilliseconds(long miliseconds) { 52 | return incrementByMicroseconds(1000 * miliseconds); 53 | } 54 | 55 | /** 56 | * Increment the clock by the specified number of microseconds. 57 | * 58 | * @param incrementInMicros the number of microseconds to add to the clock; must be positive 59 | * @return this instance to enable chaining methods; never null 60 | */ 61 | public MockClock incrementByMicroseconds(long incrementInMicros) { 62 | if (incrementInMicros < 1) throw new IllegalArgumentException("The clock increment must be positive"); 63 | ticker.accumulateAndGet(incrementInMicros, (a, b) -> a + b); 64 | return this; 65 | } 66 | 67 | @Override 68 | public long currentTimeInMicros() { 69 | return ticker.get(); 70 | } 71 | 72 | @Override 73 | public String toString() { 74 | return Long.toString(currentTimeInMillis()) + " ms"; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockCompass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | import org.strongback.components.Compass; 21 | 22 | /** 23 | * A {@link Compass} implementation useful for testing, where the direction can be explicitly set in the test case so that the 24 | * known value is read by the component that uses an {@link Compass}. 25 | * 26 | * @author Randall Hauch 27 | */ 28 | @ThreadSafe 29 | public class MockCompass extends MockZeroable implements Compass { 30 | 31 | @Override 32 | public MockCompass zero() { 33 | super.zero(); 34 | return this; 35 | } 36 | 37 | @Override 38 | public double getAngle() { 39 | return super.getValue(); 40 | } 41 | 42 | /** 43 | * Set the angle value {@link #getAngle() returned} by this object. 44 | * 45 | * @param angle the angle value 46 | * @return this instance to enable chaining methods; never null 47 | */ 48 | public MockCompass setAngle(double angle) { 49 | super.setValue(angle); 50 | return this; 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | return Double.toString(getHeading()) + "\u00B0 heading (" + getAngle() + "\u00B0)"; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.strongback.Executable; 20 | import org.strongback.control.Controller; 21 | 22 | /** 23 | * A {@link Controller} implementation that tests can use to manually {@link #setValue(double) set the value}. 24 | */ 25 | public class MockController implements Controller { 26 | 27 | private boolean enabled = true; 28 | private double setpoint = 0.0d; 29 | private double tolerance = 0.0d; 30 | private double value = 0.0d; 31 | private final Executable executable = (time)->computeOutput(); 32 | 33 | @Override 34 | public boolean isEnabled() { 35 | return enabled; 36 | } 37 | 38 | @Override 39 | public Controller enable() { 40 | enabled = true; 41 | return this; 42 | } 43 | 44 | @Override 45 | public Controller disable() { 46 | enabled = false; 47 | return this; 48 | } 49 | 50 | @Override 51 | public double getTarget() { 52 | return setpoint; 53 | } 54 | 55 | @Override 56 | public MockController withTarget(double target) { 57 | this.setpoint = target; 58 | return this; 59 | } 60 | 61 | @Override 62 | public double getTolerance() { 63 | return tolerance; 64 | } 65 | 66 | @Override 67 | public MockController withTolerance(double tolerance) { 68 | this.tolerance = tolerance; 69 | return this; 70 | } 71 | 72 | @Override 73 | public boolean computeOutput() { 74 | return isWithinTolerance(); 75 | } 76 | 77 | @Override 78 | public MockController reset() { 79 | return this; 80 | } 81 | 82 | @Override 83 | public boolean hasExecutable() { 84 | return true; 85 | } 86 | 87 | @Override 88 | public Executable executable() { 89 | return executable; 90 | } 91 | 92 | @Override 93 | public double getValue() { 94 | return value; 95 | } 96 | 97 | public MockController setValue( double value ) { 98 | this.value = value; 99 | return this; 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockCurrentSensor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | import org.strongback.components.CurrentSensor; 21 | 22 | /** 23 | * A {@link CurrentSensor} implementation useful for testing, where the current (in amps) can be explicitly set in the test 24 | * case so that the known value is read by the component that uses an {@link CurrentSensor}. 25 | * 26 | * @author Randall Hauch 27 | */ 28 | @ThreadSafe 29 | public class MockCurrentSensor implements CurrentSensor { 30 | 31 | private volatile double currentInAmps = 0; 32 | 33 | @Override 34 | public double getCurrent() { 35 | return currentInAmps; 36 | } 37 | 38 | /** 39 | * Set the current (in amps) {@link #getCurrent() returned} by this object. 40 | * 41 | * @param currentInAmps the new current reading 42 | * @return this instance to enable chaining methods; never null 43 | */ 44 | public MockCurrentSensor setCurrent(double currentInAmps) { 45 | this.currentInAmps = currentInAmps; 46 | return this; 47 | } 48 | 49 | @Override 50 | public String toString() { 51 | return Double.toString(getCurrent()) + "A"; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockDistanceSensor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | import org.strongback.components.DistanceSensor; 21 | 22 | /** 23 | * A {@link DistanceSensor} implementation useful for testing, where the distance can be explicitly set in the test 24 | * case so that the known value is read by the component that uses an {@link DistanceSensor}. 25 | * 26 | * @author Randall Hauch 27 | */ 28 | @ThreadSafe 29 | public class MockDistanceSensor extends MockZeroable implements DistanceSensor { 30 | 31 | @Override 32 | public MockDistanceSensor zero() { 33 | super.zero(); 34 | return this; 35 | } 36 | 37 | @Override 38 | public double getDistanceInInches() { 39 | return super.getValue(); 40 | } 41 | 42 | /** 43 | * Set the distance in inches {@link #getDistanceInInches() returned} by this object. 44 | * 45 | * @param distance the new distance in inches 46 | * @return this instance to enable chaining methods; never null 47 | * @see #setDistanceInFeet(double) 48 | */ 49 | public MockDistanceSensor setDistanceInInches(double distance) { 50 | super.setValue(distance); 51 | return this; 52 | } 53 | 54 | /** 55 | * Set the distance in feet {@link #getDistanceInInches() returned} by this object. 56 | * 57 | * @param distance the new distance in feet 58 | * @return this instance to enable chaining methods; never null 59 | * @see #setDistanceInInches(double) 60 | */ 61 | public MockDistanceSensor setDistanceInFeet(double distance) { 62 | return setDistanceInInches(distance * 12.0); 63 | } 64 | 65 | @Override 66 | public String toString() { 67 | return Double.toString(getDistanceInInches()) + " inches"; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockGyroscope.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | import org.strongback.components.AngleSensor; 21 | import org.strongback.components.Gyroscope; 22 | 23 | /** 24 | * A {@link Gyroscope} implementation useful for testing, where the angle and angular rate can be explicitly set in the test 25 | * case so that the known values can be read by the component that uses an {@link AngleSensor}. 26 | * 27 | * @author Randall Hauch 28 | */ 29 | @ThreadSafe 30 | public class MockGyroscope extends MockZeroable implements Gyroscope { 31 | 32 | private volatile double rate = 0; 33 | 34 | @Override 35 | public MockGyroscope zero() { 36 | super.zero(); 37 | return this; 38 | } 39 | 40 | @Override 41 | public double getAngle() { 42 | return super.getValue(); 43 | } 44 | 45 | @Override 46 | public double getRate() { 47 | return rate; 48 | } 49 | 50 | /** 51 | * Set the angle value {@link #getAngle() returned} by this object. 52 | * 53 | * @param angle the angle value 54 | * @return this instance to enable chaining methods; never null 55 | */ 56 | public MockGyroscope setAngle(double angle) { 57 | super.setValue(angle); 58 | return this; 59 | } 60 | 61 | /** 62 | * Set the angular velocity {@link #getRate() returned} by this object. 63 | * 64 | * @param angularVelocity the angular velocity (or rate of turn) in degrees per second 65 | * @return this instance to enable chaining methods; never null 66 | */ 67 | public MockGyroscope setRate(double angularVelocity) { 68 | this.rate = rate; 69 | return this; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockMotor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.strongback.components.Motor; 20 | 21 | /** 22 | * A {@link Motor} implementation useful for testing. This motor does nothing but maintain a record of the current speed. 23 | * 24 | * @author Randall Hauch 25 | * 26 | */ 27 | public class MockMotor implements Motor { 28 | 29 | private volatile double speed = 0; 30 | 31 | MockMotor(double speed) { 32 | this.speed = speed; 33 | } 34 | 35 | @Override 36 | public double getSpeed() { 37 | return speed; 38 | } 39 | 40 | @Override 41 | public MockMotor setSpeed(double speed) { 42 | this.speed = speed; 43 | return this; 44 | } 45 | 46 | @Override 47 | public MockMotor invert() { 48 | return new MockMotor(speed) { 49 | @Override 50 | public MockMotor setSpeed(double speed) { 51 | super.setSpeed(-1 * speed); 52 | return this; 53 | } 54 | 55 | @Override 56 | public double getSpeed() { 57 | return -1 * super.getSpeed(); 58 | } 59 | }; 60 | } 61 | 62 | 63 | @Override 64 | public String toString() { 65 | return Double.toString(getSpeed()); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockPowerPanel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | import org.strongback.components.PowerPanel; 23 | 24 | /** 25 | * A mock implementation of the {@link PowerPanel}, which has 16 channels 26 | * @author Randall Hauch 27 | */ 28 | public class MockPowerPanel implements PowerPanel { 29 | 30 | private final List channels; 31 | private final MockTemperatureSensor temperature = new MockTemperatureSensor().setTemperature(72.0); 32 | private final MockCurrentSensor totalCurrent = new MockCurrentSensor().setCurrent(10.0); 33 | private final MockVoltageSensor voltage = new MockVoltageSensor().setVoltage(12.0); 34 | 35 | /** 36 | * Create a mock power panel with the specified number of channels. 37 | * @param numChannels the number of channels; must be positive 38 | */ 39 | public MockPowerPanel( int numChannels ) { 40 | channels = new ArrayList<>(numChannels); 41 | for ( int i= 0; i!= numChannels; ++i ) { 42 | channels.add(new MockCurrentSensor()); 43 | } 44 | } 45 | 46 | @Override 47 | public MockCurrentSensor getCurrentSensor(int channel) { 48 | return channels.get(channel); 49 | } 50 | 51 | @Override 52 | public MockTemperatureSensor getTemperatureSensor() { 53 | return temperature; 54 | } 55 | 56 | @Override 57 | public MockCurrentSensor getTotalCurrentSensor() { 58 | return totalCurrent; 59 | } 60 | 61 | @Override 62 | public MockVoltageSensor getVoltageSensor() { 63 | return voltage; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockRelay.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | import org.strongback.components.Compass; 21 | import org.strongback.components.Relay; 22 | 23 | /** 24 | * A {@link Compass} implementation useful for testing, where the direction can be explicitly set in the test case so that the 25 | * known value is read by the component that uses an {@link Compass}. 26 | * 27 | * @author Randall Hauch 28 | */ 29 | @ThreadSafe 30 | public class MockRelay implements Relay { 31 | 32 | private State state; 33 | 34 | @Override 35 | public MockRelay off() { 36 | state = State.OFF; 37 | return this; 38 | } 39 | 40 | @Override 41 | public MockRelay on() { 42 | state = State.ON; 43 | return this; 44 | } 45 | 46 | /** 47 | * Set the state of this relay to {@link org.strongback.components.Relay.State#SWITCHING_OFF}. 48 | * @return this instance to enable chaining methods; never null 49 | */ 50 | public MockRelay switchingOff() { 51 | state = State.SWITCHING_OFF; 52 | return this; 53 | } 54 | 55 | /** 56 | * Set the state of this relay to {@link org.strongback.components.Relay.State#SWITCHING_ON}. 57 | * @return this instance to enable chaining methods; never null 58 | */ 59 | public MockRelay switchingOn() { 60 | state = State.SWITCHING_ON; 61 | return this; 62 | } 63 | 64 | @Override 65 | public State state() { 66 | return state; 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | return state().name(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockSolenoid.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | import org.strongback.components.CurrentSensor; 21 | import org.strongback.components.Solenoid; 22 | 23 | /** 24 | * A {@link Solenoid} implementation useful for testing, where the current (in amps) can be explicitly set in the test 25 | * case so that the known value is read by the component that uses an {@link CurrentSensor}. 26 | * 27 | * @author Randall Hauch 28 | */ 29 | @ThreadSafe 30 | public class MockSolenoid implements Solenoid { 31 | 32 | private volatile Direction direction = Direction.STOPPED; 33 | private final boolean completeImmediately; 34 | 35 | protected MockSolenoid( boolean completeImmediately ) { 36 | this.completeImmediately = completeImmediately; 37 | } 38 | 39 | @Override 40 | public MockSolenoid extend() { 41 | direction = Direction.EXTENDING; 42 | if ( completeImmediately ) direction = Direction.STOPPED; 43 | return this; 44 | } 45 | 46 | @Override 47 | public MockSolenoid retract() { 48 | direction = Direction.RETRACTING; 49 | if ( completeImmediately ) direction = Direction.STOPPED; 50 | return this; 51 | } 52 | 53 | @Override 54 | public Direction getDirection() { 55 | return direction; 56 | } 57 | 58 | /** 59 | * Stop any movement of this solenoid. 60 | * @return this object so that methods can be chained together; never null 61 | */ 62 | public MockSolenoid stop() { 63 | direction = Direction.STOPPED; 64 | return this; 65 | } 66 | 67 | @Override 68 | public String toString() { 69 | return getDirection().name(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockSwitch.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.strongback.components.Switch; 20 | 21 | /** 22 | * A {@link Switch} implementation useful for testing, where the triggered state can be explicitly set in the test 23 | * case so that component using the Switch can determine if it is triggered. 24 | * 25 | */ 26 | public class MockSwitch implements Switch { 27 | 28 | private boolean triggered = false; 29 | 30 | @Override 31 | public boolean isTriggered() { 32 | return triggered; 33 | } 34 | 35 | /** 36 | * Set whether this switch is to be triggered. 37 | * @param triggered true if the switch is to be triggered, or false otherwise 38 | * @return this object to allow chaining of methods; never null 39 | */ 40 | public MockSwitch setTriggered( boolean triggered ) { 41 | this.triggered = triggered; 42 | return this; 43 | } 44 | 45 | /** 46 | * Set this switch as being triggered. 47 | * @return this object to allow chaining of methods; never null 48 | */ 49 | public MockSwitch setTriggered() { 50 | setTriggered(true); 51 | return this; 52 | } 53 | 54 | /** 55 | * Set this switch as being not triggered. 56 | * @return this object to allow chaining of methods; never null 57 | */ 58 | public MockSwitch setNotTriggered() { 59 | setTriggered(false); 60 | return this; 61 | } 62 | 63 | @Override 64 | public String toString() { 65 | return triggered ? "closed" : "open"; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockTemperatureSensor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | import org.strongback.components.TemperatureSensor; 21 | 22 | /** 23 | * A {@link TemperatureSensor} implementation useful for testing, where the temperature can be explicitly set in the test case 24 | * so that the known value is read by the component that uses an {@link TemperatureSensor}. 25 | * 26 | * @author Randall Hauch 27 | */ 28 | @ThreadSafe 29 | public class MockTemperatureSensor implements TemperatureSensor { 30 | 31 | private volatile double temp = 0; 32 | 33 | @Override 34 | public double getTemperatureInCelsius() { 35 | return temp; 36 | } 37 | 38 | /** 39 | * Set the temperature value {@link #getTemperatureInCelsius() returned} by this object. 40 | * 41 | * @param temp the temperature 42 | * @return this object to allow chaining of methods; never null 43 | */ 44 | public MockTemperatureSensor setTemperature(double temp) { 45 | this.temp = temp; 46 | return this; 47 | } 48 | 49 | @Override 50 | public String toString() { 51 | return Double.toString(getTemperatureInFahrenheit()) + "\u00B0F"; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockThreeAxisAccelerometer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.strongback.annotation.Immutable; 20 | import org.strongback.components.ThreeAxisAccelerometer; 21 | import org.strongback.components.TwoAxisAccelerometer; 22 | 23 | /** 24 | * A {@link TwoAxisAccelerometer} implementation useful for testing, where the two accelerometers are mocks themselves and 25 | * can be explicitly set in the test case 26 | * so that the known acceleration values are read by the component that uses an {@link TwoAxisAccelerometer}. 27 | * 28 | * @author Randall Hauch 29 | */ 30 | @Immutable 31 | public class MockThreeAxisAccelerometer extends MockTwoAxisAccelerometer implements ThreeAxisAccelerometer { 32 | 33 | private final MockAccelerometer z = new MockAccelerometer(); 34 | 35 | @Override 36 | public MockAccelerometer getZDirection() { 37 | return z; 38 | } 39 | 40 | @Override 41 | public String toString() { 42 | return "" + getXDirection().getAcceleration() + ", " + getYDirection().getAcceleration() + ", " + getZDirection().getAcceleration() + " g/s\u00B2"; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockTwoAxisAccelerometer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.strongback.annotation.Immutable; 20 | import org.strongback.components.TwoAxisAccelerometer; 21 | 22 | /** 23 | * A {@link TwoAxisAccelerometer} implementation useful for testing, where the two accelerometers are mocks themselves and 24 | * can be explicitly set in the test case 25 | * so that the known acceleration values are read by the component that uses an {@link TwoAxisAccelerometer}. 26 | * 27 | * @author Randall Hauch 28 | */ 29 | @Immutable 30 | public class MockTwoAxisAccelerometer implements TwoAxisAccelerometer { 31 | 32 | private final MockAccelerometer x = new MockAccelerometer(); 33 | private final MockAccelerometer y = new MockAccelerometer(); 34 | 35 | @Override 36 | public MockAccelerometer getXDirection() { 37 | return x; 38 | } 39 | 40 | @Override 41 | public MockAccelerometer getYDirection() { 42 | return y; 43 | } 44 | 45 | @Override 46 | public String toString() { 47 | return "" + getXDirection().getAcceleration() + ", " + getYDirection().getAcceleration() + " g/s\u00B2"; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockVoltageSensor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | import org.strongback.components.VoltageSensor; 21 | 22 | /** 23 | * A {@link VoltageSensor} implementation useful for testing, where the {@link VoltageSensor} can be explicitly set in the test 24 | * case so that the known value is read by the component that uses an {@link VoltageSensor}. 25 | * 26 | * @author Randall Hauch 27 | */ 28 | @ThreadSafe 29 | public class MockVoltageSensor implements VoltageSensor { 30 | 31 | private volatile double voltage = 0; 32 | 33 | @Override 34 | public double getVoltage() { 35 | return voltage; 36 | } 37 | 38 | /** 39 | * Set the voltage value {@link #getVoltage() returned} by this object. 40 | * 41 | * @param voltage the voltage 42 | * @return this object to allow chaining of methods; never null 43 | */ 44 | public MockVoltageSensor setVoltage(double voltage) { 45 | this.voltage = voltage; 46 | return this; 47 | } 48 | 49 | @Override 50 | public String toString() { 51 | return Double.toString(getVoltage()) + " V"; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /strongback-testing/src/org/strongback/mock/MockZeroable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.strongback.components.Zeroable; 20 | 21 | /** 22 | * A base class for other mock components that implement {@link Zeroable}. 23 | * 24 | * @author Randall Hauch 25 | */ 26 | abstract class MockZeroable implements Zeroable { 27 | 28 | private volatile double zero = 0; 29 | private volatile double value; 30 | 31 | protected double getValue() { 32 | return value - zero; 33 | } 34 | 35 | protected void setValue( double value) { 36 | this.value = value; 37 | } 38 | 39 | @Override 40 | public MockZeroable zero() { 41 | zero = value; 42 | return this; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /strongback-tests/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /strongback-tests/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | strongback-tests 4 | JUnit test cases for the 'strongback' library and 'strongback-testing' library 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | edu.wpi.first.wpilib.plugins.core.nature.FRCProjectNature 17 | 18 | 19 | -------------------------------------------------------------------------------- /strongback-tests/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | [run-tests] Running all unit tests... 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /strongback-tests/src/org/strongback/AsyncEventRecorderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback; 18 | 19 | import org.junit.Before; 20 | import org.junit.Test; 21 | import org.strongback.mock.Mock; 22 | import org.strongback.mock.MockClock; 23 | 24 | public class AsyncEventRecorderTest { 25 | 26 | protected static final String EVENT_TYPE_1 = "event_type_1"; 27 | protected static final String EVENT_TYPE_2 = "event_type_2"; 28 | protected static final String EVENT_TYPE_3 = "event_type_3"; 29 | 30 | private AsyncEventRecorder recorder; 31 | private AccumulatingEventWriter writer; 32 | private MockClock clock; 33 | 34 | @Before 35 | public void beforeEach() { 36 | clock = Mock.clock(); 37 | writer = new AccumulatingEventWriter(); 38 | recorder = new AsyncEventRecorder(writer, clock); 39 | } 40 | 41 | @Test 42 | public void shouldProperlyPassEventsThroughQueuedWriter() { 43 | recorder.record(EVENT_TYPE_1,true); 44 | recorder.record(EVENT_TYPE_1,false); 45 | recorder.record(EVENT_TYPE_2,false); 46 | recorder.record(EVENT_TYPE_2,true); 47 | recorder.record(EVENT_TYPE_2,false); 48 | // Verify we haven't written anything yet 49 | writer.assertEmpty(); 50 | // Now execute the recorder (which will write out everything its recorder so far) ... 51 | recorder.execute(clock.currentTimeInMillis()); 52 | // Verify that events have been written ... 53 | writer.assertMatch(clock.currentTimeInMillis(), EVENT_TYPE_1, true); 54 | writer.assertMatch(clock.currentTimeInMillis(), EVENT_TYPE_1, false); 55 | writer.assertMatch(clock.currentTimeInMillis(), EVENT_TYPE_2, false); 56 | writer.assertMatch(clock.currentTimeInMillis(), EVENT_TYPE_2, true); 57 | writer.assertMatch(clock.currentTimeInMillis(), EVENT_TYPE_2, false); 58 | writer.assertEmpty(); 59 | // Increment the clock and execute the recorder again (which has nothing to write) ... 60 | clock.incrementBySeconds(1); 61 | recorder.execute(clock.currentTimeInMillis()); 62 | writer.assertEmpty(); 63 | // Write a few more events and execute the recorder ... 64 | recorder.record(EVENT_TYPE_3,10); 65 | recorder.execute(clock.currentTimeInMillis()); 66 | // Verify that the record was written ... 67 | writer.assertMatch(clock.currentTimeInMillis(), EVENT_TYPE_3, 10); 68 | writer.assertEmpty(); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /strongback-tests/src/org/strongback/StrongbackTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback; 18 | 19 | import static org.fest.assertions.Assertions.assertThat; 20 | 21 | import org.junit.After; 22 | import org.junit.Before; 23 | import org.junit.Test; 24 | import org.strongback.Logger.Level; 25 | import org.strongback.components.Clock; 26 | 27 | /** 28 | * Tests that check the functionality of the {@link Strongback.Engine}. 29 | */ 30 | public class StrongbackTest { 31 | 32 | private static final SystemLogger LOGGER = new SystemLogger(); 33 | 34 | private Clock clock; 35 | private Strongback.Engine engine; 36 | 37 | @Before 38 | public void beforeEach() { 39 | LOGGER.enable(Level.INFO); 40 | clock = Clock.system(); 41 | engine = new Strongback.Engine(clock, LOGGER); 42 | } 43 | 44 | @After 45 | public void afterEach() { 46 | try { 47 | if (engine != null && engine.isRunning()) { 48 | engine.stop(); 49 | } 50 | } finally { 51 | engine = null; 52 | } 53 | } 54 | 55 | @Test 56 | public void shouldNotBeRunningWhenCreated() { 57 | assertThat(engine.isRunning()).isFalse(); 58 | } 59 | 60 | @Test 61 | public void shouldStartWithDefaultConfiguration() { 62 | engine.logConfiguration(); 63 | assertThat(engine.isRunning()).isFalse(); 64 | assertThat(engine.start()).isTrue(); 65 | assertThat(engine.isRunning()).isTrue(); 66 | } 67 | 68 | @Test 69 | public void shouldAllowChangingExecutionPeriodWhenNotRunning() { 70 | assertThat(engine.isRunning()).isFalse(); 71 | assertThat(engine.getExecutionPeriod()).isEqualTo(20); 72 | engine.setExecutionPeriod(5); 73 | assertThat(engine.getExecutionPeriod()).isEqualTo(5); 74 | assertThat(engine.start()).isTrue(); 75 | engine.logConfiguration(); 76 | } 77 | 78 | @Test 79 | public void shouldNotAllowChangingExecutionPeriodWhenRunning() { 80 | assertThat(engine.isRunning()).isFalse(); 81 | assertThat(engine.start()).isTrue(); 82 | assertThat(engine.getExecutionPeriod()).isEqualTo(20); 83 | LOGGER.enable(Level.OFF); 84 | assertThat(engine.setExecutionPeriod(5)).isFalse(); 85 | LOGGER.enable(Level.INFO); 86 | assertThat(engine.getExecutionPeriod()).isEqualTo(20); 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /strongback-tests/src/org/strongback/command/sample/Pause.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.command.sample; 18 | 19 | import org.strongback.command.Command; 20 | 21 | /** 22 | * A sample command that pauses for a specified number of seconds. 23 | * @author Randall Hauch 24 | */ 25 | public class Pause extends Command { 26 | 27 | public Pause(double timeInSeconds) { 28 | super(timeInSeconds); 29 | } 30 | 31 | @Override 32 | public boolean execute() { 33 | return false; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /strongback-tests/src/org/strongback/component/AbstractDoubleValueTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.component; 18 | 19 | import static org.fest.assertions.Assertions.assertThat; 20 | 21 | import java.util.function.DoubleSupplier; 22 | 23 | import org.fest.assertions.Delta; 24 | import org.junit.Before; 25 | 26 | /** 27 | * @author Randall Hauch 28 | * 29 | */ 30 | abstract class AbstractDoubleValueTest { 31 | 32 | private static double value; 33 | 34 | protected static double getValue() { 35 | return value; 36 | } 37 | 38 | protected static void setValue( double value) { 39 | AbstractDoubleValueTest.value = value; 40 | } 41 | 42 | protected static void assertValue(double value, DoubleSupplier getter, double result ) { 43 | setValue(value); 44 | assertThat(getter.getAsDouble()).isEqualTo(result, Delta.delta(0.0001)); 45 | } 46 | 47 | @Before 48 | public void beforeEach() { 49 | value = 0; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /strongback-tests/src/org/strongback/component/AngleSensorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.component; 18 | 19 | import static org.fest.assertions.Assertions.assertThat; 20 | 21 | import org.junit.Before; 22 | import org.junit.Test; 23 | import org.strongback.components.AngleSensor; 24 | 25 | /** 26 | * @author Randall Hauch 27 | * 28 | */ 29 | public class AngleSensorTest extends AbstractDoubleValueTest { 30 | 31 | protected static void assertAngle(double newAngle) { 32 | assertValue(newAngle, sensor::getAngle, newAngle); 33 | } 34 | 35 | protected static void assertAngle(double newAngle, double result) { 36 | assertValue(newAngle, sensor::getAngle, result); 37 | } 38 | 39 | private static AngleSensor sensor = AngleSensor.create(AngleSensorTest::getValue); 40 | 41 | @Override 42 | @Before 43 | public void beforeEach() { 44 | setValue(0); 45 | sensor.zero(); 46 | } 47 | 48 | @Test 49 | public void shouldReturnPositiveAngle() { 50 | assertAngle(0.0); 51 | assertAngle(10.0); 52 | assertAngle(100.0); 53 | assertAngle(0.001); 54 | } 55 | 56 | @Test 57 | public void shouldReturnNegativeAngle() { 58 | assertAngle(-0.0); 59 | assertAngle(-10.0); 60 | assertAngle(-100.0); 61 | assertAngle(-0.001); 62 | } 63 | 64 | @Test 65 | public void shouldRemoveZeroFromValue() { 66 | setValue(45); 67 | sensor.zero(); 68 | assertAngle(45, 0); 69 | assertAngle(90, 45); 70 | assertAngle(0, -45); 71 | } 72 | 73 | @Test 74 | public void shouldInvertAngle() { 75 | AngleSensor inverted = AngleSensor.invert(sensor); 76 | assertAngle(0.0); 77 | assertThat(inverted.getAngle()).isEqualTo(0); 78 | assertAngle(100.0); 79 | assertThat(inverted.getAngle()).isEqualTo(-100); 80 | assertAngle(-100.0); 81 | assertThat(inverted.getAngle()).isEqualTo(100); 82 | 83 | setValue(45); 84 | sensor.zero(); 85 | assertAngle(45, 0); 86 | assertThat(inverted.getAngle()).isEqualTo(0); 87 | assertAngle(90, 45); 88 | assertThat(inverted.getAngle()).isEqualTo(-45); 89 | assertAngle(0, -45); 90 | assertThat(inverted.getAngle()).isEqualTo(45); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /strongback-tests/src/org/strongback/component/CounterTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.component; 18 | 19 | import static org.fest.assertions.Assertions.assertThat; 20 | 21 | import org.junit.Test; 22 | import org.strongback.components.Counter; 23 | 24 | /** 25 | * @author Randall Hauch 26 | * 27 | */ 28 | public class CounterTest { 29 | 30 | @Test(expected = IllegalArgumentException.class) 31 | public void shouldNotAllowMaximumThatEqualsInitial() { 32 | Counter.circular(100, 100, 100); 33 | } 34 | 35 | @Test 36 | public void shouldCirculateAfterReachingMaximum() { 37 | Counter counter = Counter.circular(0, 100, 200); 38 | assertThat(counter.get()).isEqualTo(0); 39 | counter.increment(); 40 | assertThat(counter.get()).isEqualTo(100); 41 | counter.increment(); 42 | assertThat(counter.get()).isEqualTo(200); 43 | counter.increment(); 44 | assertThat(counter.get()).isEqualTo(0); 45 | } 46 | 47 | @Test(expected = IllegalArgumentException.class) 48 | public void shouldNotAllowNegativeIncrement() { 49 | Counter.circular(100, -100, 100); 50 | } 51 | 52 | @Test(expected = IllegalArgumentException.class) 53 | public void shouldNotAllowNegativeInitialValue() { 54 | Counter.circular(0,-1,100); 55 | } 56 | 57 | @Test 58 | public void shouldCirculateAfterReachingMaximumWithDefaultIncrement() { 59 | Counter counter = Counter.circular(2); 60 | assertThat(counter.get()).isEqualTo(0); 61 | counter.increment(); 62 | assertThat(counter.get()).isEqualTo(1); 63 | counter.increment(); 64 | assertThat(counter.get()).isEqualTo(2); 65 | counter.increment(); 66 | assertThat(counter.get()).isEqualTo(0); 67 | } 68 | 69 | @Test 70 | public void shouldZeroValue() { 71 | Counter counter = Counter.circular(200); 72 | assertThat(counter.get()).isEqualTo(0); 73 | counter.increment(); 74 | assertThat(counter.get()).isEqualTo(1); 75 | counter.zero(); 76 | assertThat(counter.get()).isEqualTo(0); 77 | counter.increment(); 78 | assertThat(counter.get()).isEqualTo(1); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /strongback-tests/src/org/strongback/component/DistanceSensorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.component; 18 | 19 | import org.junit.Before; 20 | import org.junit.Test; 21 | import org.strongback.components.DistanceSensor; 22 | 23 | /** 24 | * @author Randall Hauch 25 | * 26 | */ 27 | public class DistanceSensorTest extends AbstractDoubleValueTest { 28 | 29 | protected static void assertDistance(double distance) { 30 | assertValue(distance, sensor::getDistanceInInches, distance); 31 | } 32 | 33 | protected static void assertDistance(double distance, double result) { 34 | assertValue(distance, sensor::getDistanceInInches, result); 35 | } 36 | 37 | protected static void assertDistanceInFeet(double distance) { 38 | assertValue(distance, sensor::getDistanceInFeet, distance/12.0); 39 | } 40 | 41 | private static DistanceSensor sensor = DistanceSensor.create(DistanceSensorTest::getValue); 42 | 43 | @Override 44 | @Before 45 | public void beforeEach() { 46 | setValue(0); 47 | sensor.zero(); 48 | } 49 | 50 | @Test 51 | public void shouldReturnPositiveDistance() { 52 | assertDistance(0.0); 53 | assertDistance(10.0); 54 | assertDistance(100.0); 55 | assertDistance(0.001); 56 | } 57 | 58 | @Test 59 | public void shouldReturnPositiveDistanceInFeet() { 60 | assertDistanceInFeet(0.0); 61 | assertDistanceInFeet(12.0); 62 | assertDistanceInFeet(144.0); 63 | assertDistanceInFeet(1); 64 | } 65 | 66 | @Test 67 | public void shouldReturnNegativeDistance() { 68 | assertDistance(-0.0); 69 | assertDistance(-10.0); 70 | assertDistance(-100.0); 71 | assertDistance(-0.001); 72 | } 73 | 74 | @Test 75 | public void shouldRemoveZeroFromValue() { 76 | setValue(45); 77 | sensor.zero(); 78 | assertDistance(45, 0); 79 | assertDistance(90, 45); 80 | assertDistance(0, -45); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /strongback-tests/src/org/strongback/component/FuseTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.component; 18 | 19 | import static org.fest.assertions.Assertions.assertThat; 20 | 21 | import java.util.concurrent.TimeUnit; 22 | 23 | import org.junit.Test; 24 | import org.strongback.components.Fuse; 25 | import org.strongback.mock.MockClock; 26 | 27 | /** 28 | * @author Randall Hauch 29 | * 30 | */ 31 | public class FuseTest { 32 | 33 | protected void assertTriggered(Fuse f) { 34 | assertThat(f.isTriggered()).isEqualTo(true); 35 | } 36 | 37 | protected void assertNotTriggered(Fuse f) { 38 | assertThat(f.isTriggered()).isEqualTo(false); 39 | } 40 | 41 | @Test 42 | public void shouldTriggerAndReset() { 43 | Fuse f = Fuse.create(); 44 | assertNotTriggered(f); 45 | for (int i = 0; i != 10; ++i) { 46 | f.trigger(); 47 | assertTriggered(f); 48 | f.trigger(); 49 | assertTriggered(f); 50 | f.reset(); 51 | assertNotTriggered(f); 52 | } 53 | } 54 | 55 | @Test 56 | public void shouldAutoResetWhenMoreTimeHasPastThanDelay() { 57 | MockClock clock = new MockClock(); 58 | Fuse f = Fuse.autoResetting(10, TimeUnit.SECONDS, clock); 59 | assertNotTriggered(f); 60 | // advance time by more than delay, and should still not be triggered ... 61 | clock.incrementBySeconds(100); 62 | assertNotTriggered(f); 63 | // Trigger ... 64 | f.trigger(); 65 | assertTriggered(f); 66 | // advance time by exactly the delay, and should still be triggered ... 67 | clock.incrementBySeconds(10); 68 | assertTriggered(f); 69 | // advance time beyond the delay, and it should not be triggered ... 70 | clock.incrementBySeconds(1); 71 | assertNotTriggered(f); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /strongback-tests/src/org/strongback/component/SwitchTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.component; 18 | 19 | import static org.fest.assertions.Assertions.assertThat; 20 | 21 | import org.junit.Test; 22 | import org.strongback.components.Switch; 23 | 24 | /** 25 | * @author Randall Hauch 26 | * 27 | */ 28 | public class SwitchTest { 29 | 30 | @Test 31 | public void shouldAlwaysBeTriggered() { 32 | Switch s = Switch.alwaysTriggered(); 33 | for ( int i=0; i!=100; ++i) { 34 | assertThat(s.isTriggered()).isEqualTo(true); 35 | } 36 | } 37 | 38 | @Test 39 | public void shouldNeverBeTriggered() { 40 | Switch s = Switch.neverTriggered(); 41 | for ( int i=0; i!=100; ++i) { 42 | assertThat(s.isTriggered()).isEqualTo(false); 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /strongback-tests/src/org/strongback/mock/MockAccelerometerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.junit.Before; 20 | 21 | /** 22 | * @author Randall Hauch 23 | */ 24 | public class MockAccelerometerTest extends AbstractDoubleMockTest { 25 | 26 | private MockAccelerometer mock; 27 | 28 | @Before 29 | public void beforeEach() { 30 | mock = new MockAccelerometer(); 31 | initialize(mock::setAcceleration, mock::getAcceleration); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /strongback-tests/src/org/strongback/mock/MockAngleSensorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import org.junit.Before; 20 | 21 | /** 22 | * @author Randall Hauch 23 | */ 24 | public class MockAngleSensorTest extends AbstractDoubleMockTest { 25 | 26 | private MockAngleSensor mock; 27 | 28 | @Before 29 | public void beforeEach() { 30 | mock = new MockAngleSensor(); 31 | initialize(mock::setAngle, mock::getAngle); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /strongback-tests/src/org/strongback/mock/MockCompassTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.mock; 18 | 19 | import static org.fest.assertions.Assertions.assertThat; 20 | 21 | import org.junit.Before; 22 | import org.junit.Test; 23 | 24 | /** 25 | * @author Randall Hauch 26 | */ 27 | public class MockCompassTest extends AbstractDoubleMockTest { 28 | 29 | private static final double[][] POSITIVE_ANGLES = { { 0.0, 0.0 }, { 180.0, 180.0 }, { 359.0, 359.0 }, { 360.0, 0.0 }, 30 | { 360.001, 0.001 }, { 361.0, 1.0 }, { 360.0 * 2, 0.0 }, { 360.0 * 3, 0.0 }, { 360.0 * 4, 0.0 } }; 31 | 32 | private static final double[][] NEGATIVE_ANGLES = { { -1.0, 359 }, { -90.0, 270 }, { -180, 180 }, { -270, 90 }, 33 | { -360, 0 }, { -361, 359 }, { -360 * 2, 0 } }; 34 | 35 | private MockCompass mock; 36 | 37 | @Before 38 | public void beforeEach() { 39 | mock = new MockCompass(); 40 | initialize(mock::setAngle, mock::getAngle, mock); 41 | } 42 | 43 | @Test 44 | public void shouldProperlyComputeHeadingForPositiveAngles() { 45 | for (double[] pair : POSITIVE_ANGLES) { 46 | assertProperHeading(pair[0], pair[1]); 47 | } 48 | } 49 | 50 | @Test 51 | public void shouldProperlyComputeHeadingForNegativeAngles() { 52 | for (double[] pair : NEGATIVE_ANGLES) { 53 | assertProperHeading(pair[0], pair[1]); 54 | } 55 | } 56 | 57 | protected void assertProperHeading(double angle, double expectedHeading) { 58 | mock.setAngle(angle); 59 | assertThat(mock.getAngle()).isEqualTo(angle, NOMINAL_TOLERANCE); 60 | assertThat(mock.getHeading()).isEqualTo(expectedHeading, NOMINAL_TOLERANCE); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /strongback-tools/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /strongback-tools/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | strongback-tools 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /strongback-tools/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /strongback-tools/scripts/strongback.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | if "%1"=="" goto help 4 | 5 | set strongback_home=%~dp0\..\.. 6 | set cmd=%1 7 | shift 8 | 9 | GOTO %cmd% 10 | :new-project 11 | java -cp %strongback_home%\java\lib-tools\strongback-tools.jar org.strongback.tools.newproject.NewProject %* 12 | GOTO END 13 | :log-decoder 14 | java -cp %strongback_home%\java\lib-tools\strongback-tools.jar org.strongback.tools.logdecoder.LogDecoder %* 15 | GOTO END 16 | :version 17 | FINDSTR /r ".*version.*" %strongback_home%\strongback.properties 18 | GOTO END 19 | :help 20 | echo usage: strongback ^ 21 | echo. 22 | echo Commands 23 | echo new-project 24 | echo Creates a new project configured to use strongback 25 | echo. 26 | echo log-decoder 27 | echo Converts a Strongback Binary Log to a readable CSV 28 | echo. 29 | echo version 30 | echo Prints the version of Strongback that is installed 31 | GOTO END 32 | :END 33 | -------------------------------------------------------------------------------- /strongback-tools/scripts/strongback.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | strongback_home=$( cd "$(dirname "${BASH_SOURCE}")/../.." ; pwd -P ) 4 | cmd=$1 5 | shift 6 | 7 | case $cmd in 8 | "new-project") 9 | java -cp $strongback_home/java/lib-tools/strongback-tools.jar org.strongback.tools.newproject.NewProject "$@" 10 | ;; 11 | "log-decoder") 12 | java -cp $strongback_home/java/lib-tools/strongback-tools.jar org.strongback.tools.logdecoder.LogDecoder "$@" 13 | ;; 14 | "version") 15 | grep version $strongback_home/strongback.properties | sed -n -e 's/^.*=//p' 16 | ;; 17 | *) 18 | echo "usage: strongback " 19 | echo 20 | echo "Commands" 21 | echo " new-project" 22 | echo " Creates a new project configured to use strongback" 23 | echo 24 | echo " log-decoder" 25 | echo " Converts a Strongback Binary Log to a readable CSV" 26 | echo 27 | echo " version" 28 | echo " Prints the version of Strongback that is installed" 29 | ;; 30 | esac 31 | 32 | -------------------------------------------------------------------------------- /strongback-tools/src/org/strongback/tools/utils/FileUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.tools.utils; 18 | 19 | import java.io.File; 20 | import java.util.function.Supplier; 21 | 22 | /** 23 | * Utility methods for working with files 24 | * 25 | * @author Zach Anderson 26 | * 27 | */ 28 | public class FileUtils { 29 | /** 30 | * Convenience method that replaces the {@code ~} character at the beginning of the supplied string with the user's home 31 | * directory. 32 | * 33 | * @param path the path to resolve 34 | * @return the path with the {@code ~} replaced with the user's home directory 35 | */ 36 | public static final String resolveHome(String path) { 37 | return resolveHome(path, () -> System.getProperty("user.home")); 38 | } 39 | 40 | /** 41 | * Convenience method that resolves a file path if it starts with {@code ~}. 42 | * 43 | * @param path the path to resolve 44 | * @param userHomeSupplier the value to be used for the home directory path; may not be null 45 | * @return the path with the {@code ~} replaced with the user's home directory 46 | */ 47 | public static final String resolveHome(String path, Supplier userHomeSupplier) { 48 | if (path.length() == 0) return path; 49 | if (path.charAt(0) == '~') { 50 | path = userHomeSupplier.get() + path.substring(1); 51 | } 52 | return path; 53 | } 54 | 55 | /** 56 | * Convenience method that resolves a file path if it starts with {@code ~}. 57 | * 58 | * @param path the path to resolve 59 | * @return an absolute {@link File} representing that path 60 | */ 61 | public static final File resolvePath(String path) { 62 | return resolvePath(path,() -> System.getProperty("user.home")); 63 | } 64 | 65 | /** 66 | * Convenience method that resolves a file path if it starts with {@code ~}. 67 | * 68 | * @param path the path to resolve 69 | * @param userHomeSupplier the value to be used for the home directory path; may not be null 70 | * @return an absolute {@link File} representing that path 71 | */ 72 | public static final File resolvePath(String path, Supplier userHomeSupplier) { 73 | path = resolveHome(path, userHomeSupplier); 74 | return new File(path).getAbsoluteFile(); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /strongback-tools/src/org/strongback/tools/utils/Printer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.tools.utils; 18 | 19 | /** 20 | * Utility to manage printing to stdout and sterr. Provides methods to easily change the verbosity of output. 21 | * @author Zach Anderson 22 | * 23 | */ 24 | public class Printer { 25 | private boolean quiet = false; 26 | private boolean verbose = false; 27 | 28 | /** 29 | * Sets the verbosity of this {@link Printer}. If {@code q} is set, no messages will be displayed. 30 | * If {@code v} is set all messages will be displayed. If {@code v} is not set, only messages with a 31 | * verbosity of {@link Verbosity#ALWAYS} will be displayed. 32 | * @param q silence output 33 | * @param v verbose output 34 | */ 35 | public void setVerbosity(boolean q, boolean v) { 36 | quiet = q; 37 | verbose = v; 38 | } 39 | 40 | /** 41 | * Print the specified {@link String} to stdout if the current verbosity allows it and this {@link Printer} is not silenced. 42 | * @param s the {@link String} to print 43 | * @param verbosity The {@link Verbosity} level to print the message at 44 | */ 45 | public void print(String s, Verbosity verbosity) { 46 | if(!quiet) { 47 | switch(verbosity) { 48 | case ALWAYS: 49 | System.out.println(s); 50 | break; 51 | case VERBOSE: 52 | if(verbose) System.out.println(s); 53 | break; 54 | } 55 | } 56 | } 57 | 58 | /** 59 | * Print the specified {@link String} to stderr if this {@link Printer} is not silenced.. 60 | * @param s the {@link String} to print 61 | */ 62 | public void error(String s) { 63 | if(!quiet) System.err.println(s); 64 | } 65 | 66 | public static enum Verbosity { ALWAYS, VERBOSE } 67 | } 68 | -------------------------------------------------------------------------------- /strongback-tools/src/org/strongback/tools/utils/Version.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.tools.utils; 18 | 19 | import java.io.IOException; 20 | import java.io.InputStream; 21 | import java.util.Properties; 22 | 23 | public class Version { 24 | 25 | private static final Properties PROPS; 26 | 27 | static { 28 | PROPS = new Properties(); 29 | try (InputStream stream = Version.class.getClassLoader().getResourceAsStream("strongback.properties")) { 30 | if ( stream != null ) PROPS.load(stream); 31 | else System.err.println("Unable to find the strongback.properties file"); 32 | } catch (IOException e) { 33 | System.err.println("Unable to read the strongback.properties file"); 34 | e.printStackTrace(System.err); 35 | } 36 | } 37 | 38 | public static String versionNumber() { 39 | return PROPS.getProperty("strongback.version"); 40 | } 41 | 42 | public static String buildDate() { 43 | return PROPS.getProperty("build.date"); 44 | } 45 | 46 | private Version() { 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /strongback-tools/src/strongback.properties: -------------------------------------------------------------------------------- 1 | strongback.version=1.0.0 2 | build.date=2015-10-16 -------------------------------------------------------------------------------- /strongback-tools/test/org/strongback/tools/newproject/NewProjectTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.tools.newproject; 18 | 19 | import static org.fest.assertions.Assertions.assertThat; 20 | 21 | import java.io.ByteArrayOutputStream; 22 | import java.io.File; 23 | import java.io.IOException; 24 | import java.nio.charset.StandardCharsets; 25 | import java.nio.file.Files; 26 | import java.util.List; 27 | import java.util.Properties; 28 | import java.util.stream.Collectors; 29 | 30 | import org.junit.Test; 31 | 32 | /** 33 | * @author Randall Hauch 34 | * 35 | */ 36 | public class NewProjectTest { 37 | 38 | @Test 39 | public void shouldEscapeUserlibTemplateCorrectly() throws IOException { 40 | boolean print = false; 41 | File template = new File("../templates/Strongback.userlibraries.template"); 42 | if (!template.exists()) { 43 | template = new File("templates/Strongback.userlibraries.template"); 44 | } 45 | assertThat(template.exists()).isTrue(); 46 | 47 | List lines = Files.readAllLines(template.toPath(), StandardCharsets.UTF_8); 48 | List updated = lines.stream().collect(Collectors.toList()); 49 | if (print) updated.forEach(System.out::println); 50 | String content = NewProject.combineAndEscape(updated,"/Users/jsmith/strongback"); 51 | if (print) System.out.println(content); 52 | assertThat(content.indexOf("STRONGBACK")).isEqualTo(-1); 53 | // assertThat(content.indexOf(" ")).isEqualTo(-1); 54 | 55 | Properties props = new Properties(); 56 | props.setProperty("template", content); 57 | ByteArrayOutputStream ostream = new ByteArrayOutputStream(); 58 | props.store(ostream, ""); 59 | String propFileContents = new String(ostream.toByteArray()); 60 | assertThat(propFileContents.contains(">\\n\\t\\t "C:/Users/AnnieSmith"); 33 | assertThat(actual).isEqualTo("C:/Users/AnnieSmith/strongback"); 34 | } 35 | 36 | @Test 37 | public void shouldResolveHomeWithLinuxPath() { 38 | String actual = FileUtils.resolveHome("~/strongback", () -> "/Users/AnnieSmith"); 39 | assertThat(actual).isEqualTo("/Users/AnnieSmith/strongback"); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /strongback-tools/test/org/strongback/tools/utils/VersionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.tools.utils; 18 | 19 | import static org.fest.assertions.Assertions.assertThat; 20 | 21 | import org.junit.Test; 22 | 23 | public class VersionTest { 24 | 25 | @Test 26 | public void shouldReadVersionInfo() { 27 | assertThat(Version.versionNumber()).isNotEmpty(); 28 | assertThat(Version.buildDate()).isNotEmpty(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /strongback.properties: -------------------------------------------------------------------------------- 1 | strongback.home=${user.home}/strongback 2 | strongback.ant.dir=${strongback.home}/java/ant/ 3 | strongback.templates.dir=${strongback.home}/java/templates 4 | 5 | wpilib.home=${user.home}/wpilib 6 | wpilib.props=${wpilib.home}/wpilib.properties 7 | -------------------------------------------------------------------------------- /strongback/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /strongback/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | strongback 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | edu.wpi.first.wpilib.plugins.core.nature.FRCProjectNature 17 | 18 | 19 | -------------------------------------------------------------------------------- /strongback/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/DataRecordable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback; 18 | 19 | /** 20 | * An interface that classes can implement when they are to be recordable but they involve multiple channels. 21 | */ 22 | @FunctionalInterface 23 | public interface DataRecordable { 24 | 25 | /** 26 | * Register this object with the specified data recorder. 27 | * 28 | * @param recorder the data recorder; never null 29 | * @param name the parent name for the channels; never null 30 | */ 31 | void registerWith(DataRecorder recorder, String name); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/DataRecorderChannel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback; 18 | 19 | import java.util.function.IntSupplier; 20 | 21 | import org.strongback.annotation.Immutable; 22 | 23 | @Immutable 24 | public final class DataRecorderChannel { 25 | private final String name; 26 | private final IntSupplier supplier; 27 | 28 | public DataRecorderChannel(String name, IntSupplier supplier) { 29 | assert name != null; 30 | assert supplier != null; 31 | this.name = name; 32 | this.supplier = supplier; 33 | } 34 | 35 | public String getName() { 36 | return name; 37 | } 38 | 39 | public IntSupplier getSupplier() { 40 | return supplier; 41 | } 42 | 43 | @Override 44 | public int hashCode() { 45 | return name.hashCode(); 46 | } 47 | 48 | @Override 49 | public boolean equals(Object obj) { 50 | if (obj == this) return true; 51 | if (obj instanceof DataRecorderChannel) { 52 | DataRecorderChannel that = (DataRecorderChannel) obj; 53 | return this.name.equals(that.name); 54 | } 55 | return false; 56 | } 57 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/DataRecorderChannels.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback; 18 | 19 | import java.util.Iterator; 20 | import java.util.concurrent.CopyOnWriteArrayList; 21 | import java.util.function.IntSupplier; 22 | 23 | import org.strongback.annotation.ThreadSafe; 24 | import org.strongback.components.SpeedSensor; 25 | import org.strongback.components.Switch; 26 | import org.strongback.util.Iterators; 27 | 28 | /** 29 | * A threadsafe {@link DataRecorder} that allows for switches, motors and other functions to be registered, and then to 30 | * {@link #start() start} recording the values. 31 | * 32 | * @author Randall Hauch 33 | */ 34 | @ThreadSafe 35 | final class DataRecorderChannels implements DataRecorder, Iterable { 36 | 37 | private final CopyOnWriteArrayList channels = new CopyOnWriteArrayList<>(); 38 | 39 | DataRecorderChannels() { 40 | } 41 | 42 | @Override 43 | public DataRecorder register(String name, IntSupplier supplier) { 44 | if (name == null) throw new IllegalArgumentException("The name may not be null"); 45 | if (supplier == null) throw new IllegalArgumentException("The supplier may not be null"); 46 | channels.addIfAbsent(new DataRecorderChannel(name, supplier)); 47 | return this; 48 | } 49 | 50 | @Override 51 | public DataRecorder register(String name, Switch swtch) { 52 | if (name == null) throw new IllegalArgumentException("The name may not be null"); 53 | if (swtch == null) throw new IllegalArgumentException("The switch may not be null"); 54 | channels.addIfAbsent(new DataRecorderChannel(name, () -> swtch.isTriggered() ? 1 : 0)); 55 | return this; 56 | } 57 | 58 | @Override 59 | public DataRecorder register(String name, SpeedSensor sensor) { 60 | if (name == null) throw new IllegalArgumentException("The name may not be null"); 61 | if (sensor == null) throw new IllegalArgumentException("The motor may not be null"); 62 | channels.addIfAbsent(new DataRecorderChannel(name + " speed", () -> (int) (sensor.getSpeed() * 1000))); 63 | return this; 64 | } 65 | 66 | @Override 67 | public DataRecorder register(String name, DataRecordable recordable) { 68 | if (name == null) throw new IllegalArgumentException("The name may not be null"); 69 | if (recordable == null) throw new IllegalArgumentException("The recordable may not be null"); 70 | recordable.registerWith(this, name); 71 | return this; 72 | } 73 | 74 | @Override 75 | public Iterator iterator() { 76 | return Iterators.immutable(channels); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/DataRecorderDriver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback; 18 | 19 | import java.util.concurrent.atomic.AtomicReference; 20 | import java.util.function.Function; 21 | 22 | import org.strongback.annotation.ThreadSafe; 23 | 24 | /** 25 | * A threadsafe {@link DataRecorder} that allows for switches, motors and other functions to be registered, and then to 26 | * {@link #start() start} recording the values. 27 | * 28 | * @author Randall Hauch 29 | */ 30 | @ThreadSafe 31 | final class DataRecorderDriver implements Executable { 32 | 33 | private final DataRecorderChannels channels; 34 | private final Function, DataWriter> writerFactory; 35 | private final AtomicReference writer = new AtomicReference<>(NULL_WRITER); 36 | 37 | DataRecorderDriver(DataRecorderChannels channels, Function, DataWriter> writerFactory) { 38 | this.channels = channels; 39 | this.writerFactory = writerFactory != null ? writerFactory : (channelIter) -> NULL_WRITER; 40 | } 41 | 42 | protected boolean isRunning() { 43 | return writer.get() != NULL_WRITER; 44 | } 45 | 46 | public synchronized void start() { 47 | writer.getAndUpdate((existing) -> existing == NULL_WRITER ? writerFactory.apply(channels) : existing); 48 | } 49 | 50 | public synchronized void flush() { 51 | writer.get().close(); // reopens if necessary 52 | } 53 | 54 | public synchronized void stop() { 55 | // We will always replace the existing data writer with NULL_WRITER, but only after we do this do we close the 56 | // writer. These steps are done in a very strict and ordered manner to ensure the non-NULL_WRITER is always closed. 57 | AtomicReference unclosed = new AtomicReference<>(); 58 | try { 59 | writer.getAndUpdate((existing) -> { 60 | if (existing != NULL_WRITER) unclosed.set(existing); 61 | return NULL_WRITER; 62 | }); 63 | } finally { 64 | if (unclosed.get() != null) { 65 | unclosed.get().close(); 66 | } 67 | } 68 | } 69 | 70 | @Override 71 | public void execute(long timeInMillis) { 72 | writer.get().write(timeInMillis); 73 | } 74 | 75 | private static final DataWriter NULL_WRITER = new DataWriter() { 76 | @Override 77 | public void write(long time) { 78 | } 79 | 80 | @Override 81 | public void close() { 82 | } 83 | }; 84 | } 85 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/DataWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback; 18 | 19 | /** 20 | * A writer used to periodically write data. 21 | * 22 | * @author Randall Hauch 23 | * @see Strongback.Configurator#recordDataTo(java.util.function.Function) 24 | */ 25 | public interface DataWriter extends AutoCloseable { 26 | 27 | /** 28 | * Writes the current status of the data channels. 29 | * 30 | * @param time the current time in milliseconds 31 | */ 32 | public void write(long time); 33 | 34 | /** 35 | * Frees the resources used by this {@link DataWriter}. 36 | */ 37 | @Override 38 | public void close(); 39 | } 40 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/Executable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback; 18 | 19 | /** 20 | * Something that can be executed by an {@link Executor}. 21 | * 22 | * @author Randall Hauch 23 | * @see Executor 24 | */ 25 | @FunctionalInterface 26 | public interface Executable { 27 | /** 28 | * Perform an execution at a given moment within the robot match. 29 | * 30 | * @param timeInMillis the time in the match (in milliseconds) when this execution is being called 31 | */ 32 | public void execute(long timeInMillis); 33 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/Executor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | 21 | /** 22 | * An executor that invokes registered {@link Executable}s on a fixed period. 23 | */ 24 | @ThreadSafe 25 | public interface Executor { 26 | 27 | public static enum Priority { 28 | HIGH, MEDIUM, LOW; 29 | } 30 | 31 | /** 32 | * Register a high priority {@link Executable} task so that it is called repeatedly on Strongback's executor thread. 33 | * 34 | * @param task the executable task 35 | * @return {@code true} if the executable task was registered for the first time as a high priority, or {@code false} if 36 | * {@code task} was null or was already registered with this executor at high priority 37 | * @deprecated Use {@link #register(Executable, Priority)} instead 38 | */ 39 | @Deprecated 40 | default boolean register(Executable task) { 41 | return register(task, Priority.HIGH); 42 | } 43 | 44 | /** 45 | * Register an {@link Executable} task with the given priority so that it is called repeatedly on Strongback's executor 46 | * thread. If the given task is already registered with a different priority, this method reassigns it to the desired 47 | * priority; if the given task is already registered with the desired priority, this method does nothing. 48 | *

49 | * This executor runs high priority tasks every cycle, medium priority tasks slightly every other cycles, and low priority 50 | * tasks every 4 cycles. All {@link Executable} tasks are called on the first cycle. 51 | * 52 | * @param task the executable task 53 | * @param priority the priority of the executable; may not be null 54 | * @return {@code true} if the executable task was registered for the first time at the given priority, or {@code false} if 55 | * {@code task} was null or was already registered with this executor at the given priority 56 | */ 57 | public boolean register(Executable task, Priority priority); 58 | 59 | /** 60 | * Unregister an {@link Executable} task to no longer be called. 61 | * 62 | * @param task the executable task 63 | * @return true if the executable task was unregistered, or false if it was null or not registered with this executor 64 | */ 65 | public boolean unregister(Executable task); 66 | 67 | /** 68 | * Unregister all {@link Executable} tasks. 69 | */ 70 | public void unregisterAll(); 71 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/FileEventWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback; 18 | 19 | import java.io.IOException; 20 | import java.util.function.Supplier; 21 | 22 | import org.strongback.AsyncEventRecorder.EventType; 23 | import org.strongback.AsyncEventRecorder.EventWriter; 24 | import org.strongback.annotation.ThreadSafe; 25 | 26 | /** 27 | * @author Randall Hauch 28 | * 29 | */ 30 | @ThreadSafe 31 | final class FileEventWriter implements EventWriter { 32 | 33 | private static final byte STRING_TYPE = 0x1; 34 | private static final byte INT_TYPE = 0x2; 35 | 36 | private final Supplier filenameGenerator; 37 | private MappedFileDataWriter writer; 38 | private final long fileSize; 39 | 40 | public FileEventWriter(Supplier filenameGenerator, long fileSize) { 41 | this.filenameGenerator = filenameGenerator; 42 | this.fileSize = fileSize; 43 | } 44 | 45 | protected void openIfNeeded() { 46 | if (writer == null) { 47 | try { 48 | writer = new MappedFileDataWriter(filenameGenerator.get(), fileSize); 49 | } catch (IOException e) { 50 | throw new RuntimeException(e); 51 | } 52 | } else if (writer.remaining() < 100) { 53 | System.err.println("Insuffient space to write next all of next record, closing file"); 54 | close(); 55 | openIfNeeded(); 56 | } 57 | } 58 | 59 | @Override 60 | public void recordEventType(long time, EventType newType) { 61 | openIfNeeded(); 62 | writer.write(time); 63 | writer.write(newType.typeName()); 64 | writer.write(newType.typeNumber()); 65 | } 66 | 67 | @Override 68 | public void recordEvent(long time, int eventType, String value) { 69 | openIfNeeded(); 70 | writer.write(time); 71 | writer.write(eventType); 72 | writer.write(STRING_TYPE); 73 | writer.write(value); 74 | } 75 | 76 | @Override 77 | public void recordEvent(long time, int eventType, int value) { 78 | openIfNeeded(); 79 | writer.write(time); 80 | writer.write(eventType); 81 | writer.write(INT_TYPE); 82 | writer.write(value); 83 | } 84 | 85 | @Override 86 | public void close() { 87 | try { 88 | writer.close(); 89 | } finally { 90 | writer = null; 91 | } 92 | } 93 | 94 | } 95 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/Logger.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | 21 | /** 22 | * A simple logging framework. 23 | * 24 | * @author Randall Hauch 25 | */ 26 | @ThreadSafe 27 | public interface Logger { 28 | 29 | public static enum Level { 30 | ERROR, WARN, INFO, DEBUG, TRACE, OFF; 31 | } 32 | 33 | /** 34 | * Log an exception at the error level. The exception and message are logged only if error-level (or higher) logging is 35 | * enabled. 36 | * 37 | * @param t the exception 38 | */ 39 | public void error(Throwable t); 40 | 41 | /** 42 | * Log an exception and a custom error message at the error level. The exception and message are logged only if error-level 43 | * (or higher) logging is enabled. 44 | * 45 | * @param t the exception 46 | * @param message the error message 47 | */ 48 | public void error(Throwable t, String message); 49 | 50 | /** 51 | * Log a message at the error level. The message is logged only if error-level (or higher) logging is enabled. 52 | * 53 | * @param message the error message 54 | */ 55 | public void error(String message); 56 | 57 | /** 58 | * Log a message at the warning level. The message is logged only if warning-level (or higher) logging is enabled. 59 | * 60 | * @param message the warning message 61 | */ 62 | public void warn(String message); 63 | 64 | /** 65 | * Log a message at the information level. The message is logged only if information-level (or higher) logging is enabled. 66 | * 67 | * @param message the message 68 | */ 69 | public void info(String message); 70 | 71 | /** 72 | * Log a message at the debug level. The message is logged only if debug-level (or higher) logging is enabled. 73 | * 74 | * @param message the message 75 | */ 76 | public void debug(String message); 77 | 78 | /** 79 | * Log a message at the trace level. The message is logged only if trace-level (or higher) logging is enabled. 80 | * 81 | * @param message the message 82 | */ 83 | public void trace(String message); 84 | 85 | /** 86 | * Get a {@link Logger} implementation that does nothing with log messages. 87 | * @return the no-operation logger; never null 88 | */ 89 | public static Logger noOp() { 90 | return NoOpLogger.INSTANCE; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/MappedFileDataWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback; 18 | 19 | import java.io.File; 20 | import java.io.IOException; 21 | import java.io.RandomAccessFile; 22 | import java.nio.MappedByteBuffer; 23 | import java.nio.channels.FileChannel; 24 | import java.nio.channels.FileChannel.MapMode; 25 | import java.nio.charset.Charset; 26 | import java.nio.charset.StandardCharsets; 27 | 28 | import org.strongback.annotation.ThreadSafe; 29 | 30 | /** 31 | * @author Randall Hauch 32 | * 33 | */ 34 | @ThreadSafe 35 | final class MappedFileDataWriter implements DataWriter { 36 | 37 | private final Charset UTF8 = StandardCharsets.UTF_8; 38 | 39 | private final File outFile; 40 | private final FileChannel channel; 41 | private final MappedByteBuffer buffer; 42 | 43 | @SuppressWarnings("resource") 44 | protected MappedFileDataWriter( String filename, long size ) throws IOException { 45 | outFile = new File(filename); 46 | channel = new RandomAccessFile(outFile, "rw").getChannel(); 47 | buffer = channel.map(MapMode.READ_WRITE, 0, size); 48 | } 49 | 50 | public void write( String str ) { 51 | buffer.putInt(str.length()); 52 | buffer.put(str.getBytes(UTF8)); 53 | } 54 | 55 | public void write( int number ) { 56 | buffer.putInt(number); 57 | } 58 | 59 | @Override 60 | public void write( long number ) { 61 | buffer.putLong(number); 62 | } 63 | 64 | public void write( short number ) { 65 | buffer.putShort(number); 66 | } 67 | 68 | public void write( float number ) { 69 | buffer.putFloat(number); 70 | } 71 | 72 | public void write( double number ) { 73 | buffer.putDouble(number); 74 | } 75 | 76 | public int remaining() { 77 | return buffer.remaining(); 78 | } 79 | 80 | @Override 81 | public void close() { 82 | try { 83 | // Write terminator 84 | buffer.putInt(0xFFFFFFFF); 85 | } finally { 86 | try { 87 | // And always force the buffer ... 88 | buffer.force(); 89 | } finally { 90 | try{ 91 | // And always close the channel ... 92 | channel.close(); 93 | } catch (IOException e) { 94 | throw new RuntimeException("Failed to close channel",e); 95 | } 96 | } 97 | } 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/NoOpLogger.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback; 18 | 19 | import org.strongback.annotation.Immutable; 20 | 21 | /** 22 | * @author Randall Hauch 23 | * 24 | */ 25 | @Immutable 26 | final class NoOpLogger implements Logger { 27 | 28 | public static final Logger INSTANCE = new NoOpLogger(); 29 | 30 | private NoOpLogger() { 31 | } 32 | 33 | @Override 34 | public void error(Throwable t) { 35 | } 36 | 37 | @Override 38 | public void error(Throwable t, String message) { 39 | } 40 | 41 | @Override 42 | public void error(String message) { 43 | } 44 | 45 | @Override 46 | public void warn(String message) { 47 | } 48 | 49 | @Override 50 | public void info(String message) { 51 | } 52 | 53 | @Override 54 | public void debug(String message) { 55 | } 56 | 57 | @Override 58 | public void trace(String message) { 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/StrongbackRequirementException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback; 18 | 19 | /** 20 | * An exception that signals that the (typically hardware) requirements of the code could not be satisified. 21 | * 22 | * @author Randall Hauch 23 | */ 24 | public class StrongbackRequirementException extends RuntimeException { 25 | 26 | private static final long serialVersionUID = 1L; 27 | 28 | /** 29 | * 30 | */ 31 | public StrongbackRequirementException() { 32 | } 33 | 34 | /** 35 | * @param message the message 36 | */ 37 | public StrongbackRequirementException(String message) { 38 | super(message); 39 | } 40 | 41 | /** 42 | * @param cause the cause 43 | */ 44 | public StrongbackRequirementException(Throwable cause) { 45 | super(cause); 46 | } 47 | 48 | /** 49 | * @param message the message 50 | * @param cause the cause 51 | */ 52 | public StrongbackRequirementException(String message, Throwable cause) { 53 | super(message, cause); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/annotation/Experimental.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.annotation; 18 | 19 | import java.lang.annotation.Documented; 20 | import java.lang.annotation.ElementType; 21 | import java.lang.annotation.Retention; 22 | import java.lang.annotation.RetentionPolicy; 23 | import java.lang.annotation.Target; 24 | 25 | /** 26 | * This annotation documents that annotated method or class is experimental and need additional work and/or testing on actual 27 | * hardware. An experimental class implies that all methods within the class are experimental. 28 | */ 29 | @Documented 30 | @Target({ ElementType.TYPE, ElementType.METHOD }) 31 | @Retention(RetentionPolicy.CLASS) 32 | public @interface Experimental { 33 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/annotation/Immutable.java: -------------------------------------------------------------------------------- 1 | package org.strongback.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * Copyright (c) 2005 Brian Goetz and Tim Peierls.
11 | * Released under the Creative Commons Attribution License
12 | * (http://creativecommons.org/licenses/by/2.5)
13 | * Official home: http://www.jcip.net
14 | * Adopted from Java Concurrency in Practice. 15 | *

16 | * This annotation documents that instances of the annotated class are immutable. This means that its state is seen to others as 17 | * never being changed, even though the actual private internal state may indeed change. Therefore, in an immutable class: 18 | *

    19 | *
  • all public fields are final; and
  • 20 | *
  • all public final reference fields refer to other immutable objects; and
  • 21 | *
  • constructors and methods do not publish references to any internal state which is potentially mutable by the 22 | * implementation.
  • 23 | *
24 | */ 25 | @Documented 26 | @Target( ElementType.TYPE ) 27 | @Retention( RetentionPolicy.RUNTIME ) 28 | public @interface Immutable { 29 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/annotation/NotImplemented.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.annotation; 18 | 19 | import java.lang.annotation.Documented; 20 | import java.lang.annotation.ElementType; 21 | import java.lang.annotation.Retention; 22 | import java.lang.annotation.RetentionPolicy; 23 | import java.lang.annotation.Target; 24 | 25 | /** 26 | * This annotation documents that annotated method or class has not yet been implemented. 27 | */ 28 | @Documented 29 | @Target({ ElementType.TYPE, ElementType.METHOD }) 30 | @Retention(RetentionPolicy.CLASS) 31 | public @interface NotImplemented { 32 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/annotation/NotThreadSafe.java: -------------------------------------------------------------------------------- 1 | package org.strongback.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * Copyright (c) 2005 Brian Goetz and Tim Peierls.
11 | * Released under the Creative Commons Attribution License
12 | * (http://creativecommons.org/licenses/by/2.5)
13 | * Official home: http://www.jcip.net
14 | * Adopted from Java Concurrency in Practice. 15 | *

16 | * This annotation documents the class as not being thread-safe, meaning the caller is expected to properly handle and 17 | * guard all concurrent operations on an instance. 18 | * 19 | * @see ThreadSafe 20 | */ 21 | @Documented 22 | @Target( ElementType.TYPE ) 23 | @Retention( RetentionPolicy.RUNTIME ) 24 | public @interface NotThreadSafe { 25 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/annotation/ThreadSafe.java: -------------------------------------------------------------------------------- 1 | package org.strongback.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | import java.lang.annotation.Target; 8 | 9 | /** 10 | * Copyright (c) 2005 Brian Goetz and Tim Peierls.
11 | * Released under the Creative Commons Attribution License
12 | * (http://creativecommons.org/licenses/by/2.5)
13 | * Official home: http://www.jcip.net
14 | * Adopted from Java Concurrency in Practice. 15 | *

16 | * This annotation documents the class as being thread-safe. This means that no sequences of accesses (reads and writes to public 17 | * fields, calls to public methods) may put the object into an invalid state, regardless of the interleaving of those actions by 18 | * the runtime, and without requiring any additional synchronization or coordination on the part of the caller. 19 | * 20 | * @see NotThreadSafe 21 | */ 22 | @Documented 23 | @Target( ElementType.TYPE ) 24 | @Retention( RetentionPolicy.RUNTIME ) 25 | public @interface ThreadSafe { 26 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/command/CommandState.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.command; 18 | 19 | /** 20 | * Defines the current state of a {@link Command}. The state transition is as follows: 21 | * 22 | *

23 |  *   UNINITIALIZED ----> RUNNING ----> FINISHED ----> FINALIZED
24 |  *        |                 |             |
25 |  *        |                 |             |
26 |  *        +-----------------+-------------+----> INTERRUPTED
27 |  * 
28 | * 29 | * However, a command in any state state may transition to INTERRUPTED. 30 | * 31 | *
    32 | *
  1. {@link #UNINITIALIZED} - The {@link Command} has not been initialized or executed yet.
  2. 33 | *
  3. {@link #RUNNING} - The {@link Command} has been initialized and executed at least once.
  4. 34 | *
  5. {@link #INTERUPTED} - The {@link Command} has been interrupted, but it has not been processed.
  6. 35 | *
  7. {@link #FINISHED} - The {@link Command} has finished but has not been finalized.
  8. 36 | *
  9. {@link #FINALIZED} - The {@link Command} has finished and been cleaned up.
  10. 37 | *
38 | */ 39 | public enum CommandState { 40 | /** 41 | * The {@link Command} is ready to be {@link Command#initialize() initialized}. 42 | */ 43 | UNINITIALIZED, 44 | /** 45 | * The {@link Command} has been {@link Command#initialize() initialized} and is ready to {@link Command#execute() executed}. 46 | */ 47 | RUNNING, 48 | /** 49 | * The {@link Command} has completed {@link #RUNNING running} (because {@link Command#execute()} returned {@code true}). 50 | */ 51 | FINISHED, 52 | /** 53 | * The {@link Command} has completed running and is ready to be {@link Command#end() finished}. 54 | */ 55 | FINALIZED, 56 | /** 57 | * The {@link Command} has been interrupted before completion, but the interrupt has not been processed. 58 | */ 59 | INTERUPTED, 60 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/command/Requirable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.command; 18 | 19 | /** 20 | * A marker interface that denotes something required by commands. 21 | * 22 | * @author Zach Anderson 23 | * @see Command 24 | */ 25 | public interface Requirable { 26 | 27 | } 28 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/Accelerometer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | 21 | /** 22 | * A single-axis accelerometer capable of sensing acceleration in one direction. 23 | * 24 | * @author Zach Anderson 25 | * @see TwoAxisAccelerometer 26 | * @see ThreeAxisAccelerometer 27 | */ 28 | @ThreadSafe 29 | @FunctionalInterface 30 | public interface Accelerometer { 31 | 32 | /** 33 | * Get the acceleration in g's. 34 | * @return the acceleration 35 | */ 36 | public double getAcceleration(); 37 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/Compass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components; 18 | 19 | import java.util.function.DoubleSupplier; 20 | 21 | import org.strongback.annotation.ThreadSafe; 22 | 23 | /** 24 | * A sensor that determines the positional heading, or angular displacement, in degrees. A compass is an {@link AngleSensor} 25 | * with an additional method to determine {@link #getHeading() heading}. It also can be {@link #zero() zeroed} to return angles 26 | * and heading relative to another. Negative values are assumed to be counter-clockwise and positive values are clockwise. 27 | * 28 | * @author Randall Hauch 29 | */ 30 | @ThreadSafe 31 | public interface Compass extends AngleSensor { 32 | 33 | /** 34 | * Gets the angular displacement of in degrees in the range [0, 360). 35 | * 36 | * @return the heading of this {@link Compass} 37 | */ 38 | public default double getHeading() { 39 | double positiveOrNegative = getAngle() % 360; 40 | return positiveOrNegative >= 0 ? positiveOrNegative : 360 + positiveOrNegative; 41 | } 42 | 43 | /** 44 | * Compute the change in heading between the {@link #getHeading() current heading} and the target heading, using the given 45 | * tolerance for the difference. The result is the angle that this sensor must rotate to reach the target heading, which may 46 | * be positive or negative. 47 | * 48 | * @param targetHeading the target heading 49 | * @param tolerance the allowed tolerance in degrees between the two headings 50 | * @return the angular displacement required for this sensor to reach the target heading, or 0.0d if the two headings are 51 | * already within the specified {@code tolerance} 52 | */ 53 | default public double computeHeadingChangeTo(double targetHeading, double tolerance) { 54 | double diff = targetHeading - this.getHeading(); 55 | return Math.abs(diff) <= Math.abs(tolerance) ? 0.0 : diff; 56 | } 57 | 58 | /** 59 | * Create a angle sensor for the given function that returns the angle. 60 | * 61 | * @param angleSupplier the function that returns the angle; may not be null 62 | * @return the angle sensor 63 | */ 64 | public static Compass create(DoubleSupplier angleSupplier) { 65 | return new Compass() { 66 | private volatile double zero = 0; 67 | 68 | @Override 69 | public double getAngle() { 70 | return angleSupplier.getAsDouble() - zero; 71 | } 72 | 73 | @Override 74 | public Compass zero() { 75 | zero = angleSupplier.getAsDouble(); 76 | return this; 77 | } 78 | }; 79 | } 80 | 81 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/CurrentSensor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | 21 | /** 22 | * A sensor the reports the electrical current. 23 | */ 24 | @ThreadSafe 25 | @FunctionalInterface 26 | public interface CurrentSensor { 27 | /** 28 | * Gets the current of this sensor in amperes. 29 | * @return the current of this sensor in amps 30 | */ 31 | public double getCurrent(); 32 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/DistanceSensor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components; 18 | 19 | import java.util.function.DoubleSupplier; 20 | 21 | import org.strongback.annotation.ThreadSafe; 22 | 23 | /** 24 | * A distance sensor is a sensor capable of sensing distance. The unit is assumed to be inches. 25 | * 26 | * @author Zach Anderson 27 | */ 28 | @ThreadSafe 29 | @FunctionalInterface 30 | public interface DistanceSensor extends Zeroable { 31 | /** 32 | * Gets the current value of this {@link DistanceSensor} in inches. 33 | * 34 | * @return the value of this {@link DistanceSensor} 35 | * @see #getDistanceInFeet() 36 | */ 37 | public double getDistanceInInches(); 38 | 39 | /** 40 | * Gets the current value of this {@link DistanceSensor} in feet. 41 | * 42 | * @return the value of this {@link DistanceSensor} 43 | * @see #getDistanceInInches() 44 | */ 45 | default public double getDistanceInFeet() { 46 | return getDistanceInInches() / 12.0; 47 | } 48 | 49 | @Override 50 | default public DistanceSensor zero() { 51 | return this; 52 | } 53 | 54 | /** 55 | * Create a distance sensor for the given function that returns the distance. 56 | * 57 | * @param distanceSupplier the function that returns the distance; may not be null 58 | * @return the angle sensor 59 | */ 60 | public static DistanceSensor create(DoubleSupplier distanceSupplier) { 61 | return new DistanceSensor() { 62 | private double zero = 0; 63 | 64 | @Override 65 | public double getDistanceInInches() { 66 | return distanceSupplier.getAsDouble() - zero; 67 | } 68 | 69 | @Override 70 | public DistanceSensor zero() { 71 | zero = distanceSupplier.getAsDouble(); 72 | return this; 73 | } 74 | }; 75 | } 76 | 77 | /** 78 | * Inverts the specified {@link DistanceSensor} so that negative distances become positive distances. 79 | * 80 | * @param sensor the {@link DistanceSensor} to invert 81 | * @return an {@link DistanceSensor} that reads the negated distance of the original sensor 82 | */ 83 | public static DistanceSensor invert(DistanceSensor sensor) { 84 | return new DistanceSensor() { 85 | @Override 86 | public double getDistanceInInches() { 87 | double dist = sensor.getDistanceInInches(); 88 | return dist == 0.0 ? 0.0 : -dist; 89 | } 90 | @Override 91 | public DistanceSensor zero() { 92 | return sensor.zero(); 93 | } 94 | }; 95 | } 96 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/Gyroscope.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components; 18 | 19 | import java.util.function.DoubleSupplier; 20 | 21 | import org.strongback.annotation.ThreadSafe; 22 | 23 | /** 24 | * A gyroscope is a device that measures angular velocity (in degrees per second) about a single axis. A gyroscope can 25 | * indirectly determine angular displacement by integrating velocity with respect to time, which is why it extends 26 | * {@link Compass}. Negative values are assumed to be counter-clockwise and positive values are clockwise. 27 | * 28 | * @author Zach Anderson 29 | * 30 | */ 31 | @ThreadSafe 32 | public interface Gyroscope extends Compass { 33 | /** 34 | * Gets the rate of change in {@link #getAngle()} of this {@link Gyroscope} in degrees per second. 35 | * 36 | * @return the angular velocity of this {@link Gyroscope} 37 | */ 38 | public double getRate(); 39 | 40 | @Override 41 | default public Gyroscope zero() { 42 | return this; 43 | } 44 | 45 | /** 46 | * Create a gyroscope for the given functions that returns the angular displacement and velocity. 47 | * 48 | * @param angleSupplier the function that returns the angle; may not be null 49 | * @param rateSupplier the function that returns the angular acceleration; may not be null 50 | * @return the angle sensor 51 | */ 52 | public static Gyroscope create(DoubleSupplier angleSupplier, DoubleSupplier rateSupplier) { 53 | return new Gyroscope() { 54 | private volatile double zero = 0; 55 | 56 | @Override 57 | public double getAngle() { 58 | return angleSupplier.getAsDouble() - zero; 59 | } 60 | 61 | @Override 62 | public Gyroscope zero() { 63 | zero = angleSupplier.getAsDouble(); 64 | return this; 65 | } 66 | 67 | @Override 68 | public double getRate() { 69 | return rateSupplier.getAsDouble(); 70 | } 71 | }; 72 | } 73 | 74 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/Solenoid.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | import org.strongback.command.Requirable; 21 | 22 | /** 23 | * A solenoid is a device that can be extended and retracted. 24 | * 25 | * @author Zach Anderson 26 | */ 27 | @ThreadSafe 28 | public interface Solenoid extends Requirable { 29 | 30 | /** 31 | * The direction of the solenoid. 32 | */ 33 | static enum Direction { 34 | /** The solenoid is extending. */ 35 | EXTENDING, 36 | /** The solenoid is retracting. */ 37 | RETRACTING, 38 | /** The solenoid is stopped. */ 39 | STOPPED; 40 | } 41 | 42 | /** 43 | * Get the current direction of this solenoid. 44 | * 45 | * @return the current direction; never null 46 | */ 47 | Direction getDirection(); 48 | 49 | /** 50 | * Extends this solenoid. 51 | * @return this object to allow chaining of methods; never null 52 | */ 53 | Solenoid extend(); 54 | 55 | /** 56 | * Retracts this solenoid. 57 | * @return this object to allow chaining of methods; never null 58 | */ 59 | Solenoid retract(); 60 | 61 | /** 62 | * Determine if this solenoid is or was extending. 63 | * 64 | * @return {@code true} if this solenoid is in the process of extending but not yet fully extended, or {@code false} 65 | * otherwise 66 | */ 67 | default boolean isExtending() { 68 | return getDirection() == Direction.EXTENDING; 69 | } 70 | 71 | /** 72 | * Determine if this solenoid is or was retracting. 73 | * 74 | * @return {@code true} if this solenoid is in the process of retracting but not yet fully retracted, or {@code false} 75 | * otherwise 76 | */ 77 | default boolean isRetracting() { 78 | return getDirection() == Direction.RETRACTING; 79 | } 80 | 81 | /** 82 | * Determine if this solenoid is stopped. 83 | * 84 | * @return {@code true} if this solenoid is not retracting or extending, or false otherwise 85 | */ 86 | default boolean isStopped() { 87 | return getDirection() == Direction.STOPPED; 88 | } 89 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/SpeedController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | 21 | /** 22 | * A component that can report and control the speed. 23 | * 24 | * @author Randall Hauch 25 | */ 26 | @ThreadSafe 27 | @FunctionalInterface 28 | public interface SpeedController { 29 | 30 | /** 31 | * Sets the speed. 32 | * 33 | * @param speed the new speed as a double 34 | * @return this object to allow chaining of methods; never null 35 | */ 36 | public SpeedController setSpeed(double speed); 37 | 38 | } 39 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/SpeedSensor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | 21 | /** 22 | * A simple sensor that reports the speed. 23 | * @author Randall Hauch 24 | */ 25 | @ThreadSafe 26 | @FunctionalInterface 27 | public interface SpeedSensor { 28 | 29 | /** 30 | * Gets the current speed. 31 | * 32 | * @return the speed 33 | */ 34 | public double getSpeed(); 35 | } 36 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/Stoppable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components; 18 | 19 | /** 20 | * @author Randall Hauch 21 | */ 22 | @FunctionalInterface 23 | public interface Stoppable { 24 | 25 | /** 26 | * Stops this component. 27 | */ 28 | public void stop(); 29 | } 30 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/Switch.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components; 18 | 19 | import java.util.Objects; 20 | 21 | import org.strongback.annotation.ThreadSafe; 22 | 23 | 24 | /** 25 | * A switch is any readable device that has an active state when it is triggered and an inactive state when it isn't. 26 | * 27 | * @author Zach Anderson 28 | */ 29 | @ThreadSafe 30 | @FunctionalInterface 31 | public interface Switch { 32 | /** 33 | * Checks if this switch is triggered. 34 | * 35 | * @return {@code true} if this switch was triggered, or {@code false} otherwise 36 | */ 37 | public boolean isTriggered(); 38 | 39 | /** 40 | * Create a switch that is always triggered. 41 | * @return the always-triggered switch; never null 42 | */ 43 | public static Switch alwaysTriggered() { 44 | return ()->true; 45 | } 46 | 47 | /** 48 | * Create a switch that is never triggered. 49 | * @return the never-triggered switch; never null 50 | */ 51 | public static Switch neverTriggered() { 52 | return ()->false; 53 | } 54 | 55 | /** 56 | * Return a new switch that is only triggered when both switches are triggered. 57 | * @param switch1 the first switch; may not be null 58 | * @param switch2 the second switch; may not be null 59 | * @return the logical AND of the two switches; never null 60 | */ 61 | public static Switch and( Switch switch1, Switch switch2 ) { 62 | Objects.requireNonNull(switch1,"The first switch may not be null"); 63 | Objects.requireNonNull(switch2,"The second switch may not be null"); 64 | if ( switch1 == switch2 ) return switch1; 65 | return ()->switch1.isTriggered() && switch2.isTriggered(); 66 | } 67 | 68 | /** 69 | * Return a new switch that is only triggered when either switch is triggered. 70 | * @param switch1 the first switch; may not be null 71 | * @param switch2 the second switch; may not be null 72 | * @return the logical OR of the two switches; never null 73 | */ 74 | public static Switch or( Switch switch1, Switch switch2 ) { 75 | Objects.requireNonNull(switch1,"The first switch may not be null"); 76 | Objects.requireNonNull(switch2,"The second switch may not be null"); 77 | if ( switch1 == switch2 ) return switch1; 78 | return ()->switch1.isTriggered() || switch2.isTriggered(); 79 | } 80 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/TemperatureSensor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | 21 | /** 22 | * A sensor the reports the temperature. 23 | */ 24 | @ThreadSafe 25 | @FunctionalInterface 26 | public interface TemperatureSensor { 27 | /** 28 | * Gets the current temperature in degrees Celsius. 29 | * 30 | * @return the current temperature in Celsius 31 | */ 32 | public double getTemperatureInCelsius(); 33 | /** 34 | * Gets the current temperature in degrees Fahrenheit. 35 | * 36 | * @return the current temperature in Fahrenheit 37 | */ 38 | default public double getTemperatureInFahrenheit() { 39 | return getTemperatureInCelsius() * 9.0/5.0 + 32.0; 40 | } 41 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/ThreeAxisAcceleration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components; 18 | 19 | import org.strongback.annotation.Immutable; 20 | 21 | /** 22 | * A set of three immutable acceleration values, one for each axis. 23 | */ 24 | @Immutable 25 | public final class ThreeAxisAcceleration extends TwoAxisAcceleration { 26 | private final double z; 27 | 28 | protected ThreeAxisAcceleration(double x, double y, double z) { 29 | super(x,y); 30 | this.z = z; 31 | } 32 | public double getZ() { 33 | return z; 34 | } 35 | @Override 36 | public String toString() { 37 | return "[" + getX() + ',' + getY() + ',' + getZ() + "]"; 38 | } 39 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/TwoAxisAcceleration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components; 18 | 19 | import org.strongback.annotation.Immutable; 20 | 21 | /** 22 | * A pair of immutable acceleration values, one for each axis. 23 | */ 24 | @Immutable 25 | public class TwoAxisAcceleration { 26 | 27 | private final double x; 28 | private final double y; 29 | 30 | protected TwoAxisAcceleration(double x, double y) { 31 | this.x = x; 32 | this.y = y; 33 | } 34 | 35 | public double getX() { 36 | return x; 37 | } 38 | 39 | public double getY() { 40 | return y; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "[" + x + ',' + y + "]"; 46 | } 47 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/TwoAxisAccelerometer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components; 18 | 19 | /** 20 | * An accelerometer is a device capable of sensing acceleration. By performing two integrations, an accelerometer can also find 21 | * velocity and displacement. 22 | * 23 | * @author Zach Anderson 24 | * @see ThreeAxisAccelerometer 25 | * @see Accelerometer 26 | */ 27 | public interface TwoAxisAccelerometer { 28 | 29 | /** 30 | * Get the X-axis accelerometer. 31 | * 32 | * @return the accelerometer for the X-axis; never null 33 | */ 34 | public Accelerometer getXDirection(); 35 | 36 | /** 37 | * Get the Y-axis accelerometer. 38 | * 39 | * @return the accelerometer for the Y-axis; never null 40 | */ 41 | public Accelerometer getYDirection(); 42 | 43 | /** 44 | * Get the accelerometer for the axis with the given index, where 0 is the X-axis and 1 is the Y-axis. 45 | * 46 | * @param axis the axis direction; must be either 0 or 1 47 | * @return the accelerometer; never null 48 | * @throws IllegalArgumentException if {@code axis} is invalid 49 | */ 50 | default public Accelerometer getDirection(int axis) { 51 | if (axis == 0) return getXDirection(); 52 | if (axis == 1) return getYDirection(); 53 | throw new IllegalArgumentException("The axis was '" + axis + "', but only '0' or '1' is accepted"); 54 | } 55 | 56 | /** 57 | * Get the instantaneous multidimensional acceleration values for all 2 axes. 58 | * 59 | * @return the acceleration values for 2 axes; never null 60 | */ 61 | default public TwoAxisAcceleration getAcceleration() { 62 | return new TwoAxisAcceleration(getXDirection().getAcceleration(), getYDirection().getAcceleration()); 63 | } 64 | 65 | /** 66 | * Create a 2-axis accelerometer from the two individual accelerometers. 67 | * 68 | * @param xAxis the accelerometer for the X-axis; may not be null 69 | * @param yAxis the accelerometer for the Y-axis; may not be null 70 | * @return the 2-axis accelerometer; never null 71 | */ 72 | public static TwoAxisAccelerometer create(Accelerometer xAxis, Accelerometer yAxis) { 73 | return new TwoAxisAccelerometer() { 74 | 75 | @Override 76 | public Accelerometer getXDirection() { 77 | return xAxis; 78 | } 79 | 80 | @Override 81 | public Accelerometer getYDirection() { 82 | return yAxis; 83 | } 84 | }; 85 | } 86 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/VoltageSensor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components; 18 | 19 | import org.strongback.annotation.ThreadSafe; 20 | 21 | /** 22 | * A sensor the reports the voltage. 23 | */ 24 | @ThreadSafe 25 | @FunctionalInterface 26 | public interface VoltageSensor { 27 | /** 28 | * Gets the current voltage in Volts. 29 | * 30 | * @return the current voltage 31 | */ 32 | public double getVoltage(); 33 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/Zeroable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components; 18 | 19 | /** 20 | * A component that can be zeroed. 21 | * @author Randall Hauch 22 | */ 23 | @FunctionalInterface 24 | public interface Zeroable { 25 | 26 | /** 27 | * Change the output so that the current value is considered to be 0 28 | * @return this object to allow chaining of methods; never null 29 | */ 30 | public Zeroable zero(); 31 | } 32 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/ui/ContinuousRange.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components.ui; 18 | 19 | import java.util.function.DoubleSupplier; 20 | import java.util.function.IntSupplier; 21 | 22 | import org.strongback.function.DoubleToDoubleFunction; 23 | 24 | /** 25 | * Defines a range of values between [-1.0, 1.0] inclusive. 26 | * 27 | * @author Zach Anderson 28 | */ 29 | @FunctionalInterface 30 | public interface ContinuousRange { 31 | /** 32 | * Read the current value. 33 | * 34 | * @return the value in the range [-1.0, 1.0] inclusive. 35 | */ 36 | public double read(); 37 | 38 | /** 39 | * Create a new range that inverts the values of this instance. 40 | * 41 | * @return the new inverted range; never null 42 | */ 43 | default public ContinuousRange invert() { 44 | return () -> this.read() * -1.0; 45 | } 46 | 47 | /** 48 | * Create a new range that scales the values of this instance. 49 | * 50 | * @param scale the scaling factor 51 | * @return the new scaled range; never null 52 | */ 53 | default public ContinuousRange scale(double scale) { 54 | return () -> this.read() * scale; 55 | } 56 | 57 | /** 58 | * Create a new range that scales the values of this instance. 59 | * 60 | * @param scale the function that determines the scaling factor 61 | * @return the new scaled range; never null 62 | */ 63 | default public ContinuousRange scale(DoubleSupplier scale) { 64 | return () -> this.read() * scale.getAsDouble(); 65 | } 66 | 67 | /** 68 | * Create a new range that maps the values of this instance using the supplied function. 69 | * 70 | * @param mapFunction the function that maps the current value to another 71 | * @return the new mapped range; never null 72 | */ 73 | default public ContinuousRange map(DoubleToDoubleFunction mapFunction) { 74 | return () -> mapFunction.applyAsDouble(this.read()); 75 | } 76 | 77 | /** 78 | * Create a new {@link IntSupplier} that scales the values of this instance and rounds to an integer. 79 | * 80 | * @param scale the scaling factor 81 | * @return the new scaled IntSupplier; never null 82 | */ 83 | default public IntSupplier scaleAsInt(double scale) { 84 | return () -> (int)(this.read() * scale); 85 | } 86 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/ui/DirectionalAxis.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components.ui; 18 | 19 | /** 20 | * Defines an axis that points in a direction. 21 | */ 22 | @FunctionalInterface 23 | public interface DirectionalAxis { 24 | /** 25 | * Get the direction that the axis is pointing in. 26 | * @return the direction 27 | */ 28 | public int getDirection(); 29 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/ui/FlightStick.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components.ui; 18 | 19 | import java.util.function.IntToDoubleFunction; 20 | 21 | import org.strongback.components.Switch; 22 | import org.strongback.function.IntToBooleanFunction; 23 | import org.strongback.function.IntToIntFunction; 24 | 25 | /** 26 | * A type of input device consisting of a joystick with twist and throttle and multiple buttons. 27 | */ 28 | public interface FlightStick extends InputDevice { 29 | public ContinuousRange getPitch(); 30 | 31 | public ContinuousRange getYaw(); 32 | 33 | public ContinuousRange getRoll(); 34 | 35 | public ContinuousRange getThrottle(); 36 | 37 | public Switch getTrigger(); 38 | 39 | public Switch getThumb(); 40 | 41 | public static FlightStick create(IntToDoubleFunction axisToValue, IntToBooleanFunction buttonNumberToSwitch, 42 | IntToIntFunction padToValue, ContinuousRange pitch, ContinuousRange yaw, ContinuousRange roll, 43 | ContinuousRange throttle, Switch trigger, Switch thumb) { 44 | return new FlightStick() { 45 | @Override 46 | public ContinuousRange getAxis(int axis) { 47 | return () -> axisToValue.applyAsDouble(axis); 48 | } 49 | 50 | @Override 51 | public Switch getButton(int button) { 52 | return () -> buttonNumberToSwitch.applyAsBoolean(button); 53 | } 54 | 55 | @Override 56 | public DirectionalAxis getDPad(int pad) { 57 | return () -> padToValue.applyAsInt(pad); 58 | } 59 | 60 | @Override 61 | public ContinuousRange getPitch() { 62 | return pitch; 63 | } 64 | 65 | @Override 66 | public ContinuousRange getYaw() { 67 | return yaw; 68 | } 69 | 70 | @Override 71 | public ContinuousRange getRoll() { 72 | return roll; 73 | } 74 | 75 | @Override 76 | public ContinuousRange getThrottle() { 77 | return throttle; 78 | } 79 | 80 | @Override 81 | public Switch getTrigger() { 82 | return trigger; 83 | } 84 | 85 | @Override 86 | public Switch getThumb() { 87 | return thumb; 88 | } 89 | }; 90 | } 91 | 92 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/components/ui/InputDevice.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.components.ui; 18 | 19 | import java.util.function.IntToDoubleFunction; 20 | 21 | import org.strongback.components.Switch; 22 | import org.strongback.function.IntToBooleanFunction; 23 | import org.strongback.function.IntToIntFunction; 24 | 25 | /** 26 | * A simple collection of axes and buttons. 27 | */ 28 | public interface InputDevice { 29 | /** 30 | * Get the analog axis for the given number. 31 | * @param axis the axis number 32 | * @return the analog axis, or null if there is no such axis 33 | */ 34 | public ContinuousRange getAxis(int axis); 35 | /** 36 | * Get the button for the given number. 37 | * @param button the button number 38 | * @return the button, or null if there is no such button 39 | */ 40 | public Switch getButton(int button); 41 | /** 42 | * Get the directional axis for the given D-pad number. 43 | * @param pad the pad number 44 | * @return the directional axis, or null if there is no such axis for the given D-pad number 45 | */ 46 | public DirectionalAxis getDPad(int pad); 47 | 48 | /** 49 | * Create an input device from the supplied mapping functions. 50 | * @param axisToValue the function that maps an integer to a double value for the axis 51 | * @param buttonNumberToSwitch the function that maps an integer to whether the button is pressed 52 | * @param padToValue the function that maps an integer to the directional axis output 53 | * @return the resulting input device; never null 54 | */ 55 | public static InputDevice create( IntToDoubleFunction axisToValue, IntToBooleanFunction buttonNumberToSwitch, IntToIntFunction padToValue ) { 56 | return new InputDevice() { 57 | @Override 58 | public ContinuousRange getAxis(int axis) { 59 | return ()->axisToValue.applyAsDouble(axis); 60 | } 61 | @Override 62 | public Switch getButton(int button) { 63 | return ()->buttonNumberToSwitch.applyAsBoolean(button); 64 | } 65 | @Override 66 | public DirectionalAxis getDPad(int pad) { 67 | return ()->padToValue.applyAsInt(pad); 68 | } 69 | }; 70 | } 71 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/function/DoubleBiFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.function; 18 | 19 | import java.util.function.Function; 20 | 21 | /** 22 | * Represents a function that accepts two a double-valued arguments and produces a double-valued result. This is the 23 | * {@code double} primitive specialization for {@link Function BiFunction}. 24 | *

25 | * This is a functional interface whose functional method is {@link #applyAsDouble(double,double)}. 26 | * 27 | * @see Function 28 | * @author Randall Hauch 29 | */ 30 | @FunctionalInterface 31 | public interface DoubleBiFunction { 32 | 33 | /** 34 | * Applies this function to the given arguments. 35 | * 36 | * @param t the first function argument 37 | * @param u the second function argument 38 | * @return the function result 39 | */ 40 | public double applyAsDouble(double t, double u); 41 | } 42 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/function/DoubleToDoubleFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.function; 18 | 19 | import java.util.function.Function; 20 | 21 | /** 22 | * Represents a function that accepts a double-valued argument and produces a 23 | * double-valued result. This is the {@code double}-to-{@code double} primitive 24 | * specialization for {@link Function}. 25 | *

This is a functional interface whose functional method is {@link #applyAsDouble(double)}. 26 | * 27 | * @see Function 28 | * @author Randall Hauch 29 | */ 30 | @FunctionalInterface 31 | public interface DoubleToDoubleFunction { 32 | 33 | /** 34 | * Applies this function to the given argument. 35 | * 36 | * @param value the function argument 37 | * @return the function result 38 | */ 39 | public double applyAsDouble(double value); 40 | } 41 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/function/IntBiFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.function; 18 | 19 | import java.util.function.Function; 20 | 21 | /** 22 | * Represents a function that accepts two a integer-valued arguments and produces a integer-valued result. This is the 23 | * {@code integer} primitive specialization for {@link Function BiFunction}. 24 | *

25 | * This is a functional interface whose functional method is {@link #applyAsInt(int,int)}. 26 | * 27 | * @see Function 28 | * @author Randall Hauch 29 | */ 30 | @FunctionalInterface 31 | public interface IntBiFunction { 32 | 33 | /** 34 | * Applies this function to the given arguments. 35 | * 36 | * @param t the first function argument 37 | * @param u the second function argument 38 | * @return the function result 39 | */ 40 | public int applyAsInt(int t, int u); 41 | } 42 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/function/IntToBooleanFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.function; 18 | 19 | import java.util.function.Function; 20 | 21 | /** 22 | * Represents a function that accepts an integer-valued argument and produces a 23 | * boolean-valued result. This is the {@code int}-to-{@code boolean} primitive 24 | * specialization for {@link Function}. 25 | *

This is a functional interface whose functional method is {@link #applyAsBoolean(int)}. 26 | * 27 | * @see Function 28 | * @author Randall Hauch 29 | */ 30 | @FunctionalInterface 31 | public interface IntToBooleanFunction { 32 | 33 | /** 34 | * Applies this function to the given argument. 35 | * 36 | * @param value the function argument 37 | * @return the function result 38 | */ 39 | public boolean applyAsBoolean(int value); 40 | } 41 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/function/IntToIntFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.function; 18 | 19 | import java.util.function.Function; 20 | 21 | /** 22 | * Represents a function that accepts an integer-valued argument and produces an 23 | * integer-valued result. This is the {@code int}-to-{@code int} primitive 24 | * specialization for {@link Function}. 25 | *

This is a functional interface whose functional method is {@link #applyAsInt(int)}. 26 | * 27 | * @see Function 28 | * @author Randall Hauch 29 | */ 30 | @FunctionalInterface 31 | public interface IntToIntFunction { 32 | 33 | /** 34 | * Applies this function to the given argument. 35 | * 36 | * @param value the function argument 37 | * @return the function result 38 | */ 39 | public int applyAsInt(int value); 40 | } 41 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/hardware/HardwareDoubleSolenoid.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.hardware; 18 | 19 | import org.strongback.components.Solenoid; 20 | 21 | import edu.wpi.first.wpilibj.DoubleSolenoid; 22 | import edu.wpi.first.wpilibj.DoubleSolenoid.Value; 23 | 24 | /** 25 | * Wrapper for WPILib {@link DoubleSolenoid}. 26 | * 27 | * @author Zach Anderson 28 | * @see Solenoid 29 | * @see Hardware 30 | * @see edu.wpi.first.wpilibj.DoubleSolenoid 31 | */ 32 | final class HardwareDoubleSolenoid implements Solenoid { 33 | private final DoubleSolenoid solenoid; 34 | 35 | private Direction direction; 36 | 37 | HardwareDoubleSolenoid(DoubleSolenoid solenoid, Direction initialDirection ) { 38 | assert solenoid != null; 39 | assert initialDirection != null; 40 | this.solenoid = solenoid; 41 | this.direction = initialDirection; 42 | checkState(); 43 | } 44 | 45 | protected void checkState() { 46 | if ( solenoid.get() == Value.kForward ) { 47 | direction = Direction.EXTENDING; 48 | } else if ( solenoid.get() == Value.kReverse ) { 49 | direction = Direction.RETRACTING; 50 | } else { 51 | direction = Direction.STOPPED; 52 | } 53 | } 54 | 55 | @Override 56 | public Direction getDirection() { 57 | checkState(); 58 | return direction; 59 | } 60 | 61 | @Override 62 | public HardwareDoubleSolenoid extend() { 63 | solenoid.set(Value.kForward); 64 | direction = Direction.EXTENDING; 65 | checkState(); 66 | return this; 67 | } 68 | 69 | @Override 70 | public HardwareDoubleSolenoid retract() { 71 | solenoid.set(Value.kReverse); 72 | direction = Direction.RETRACTING; 73 | checkState(); 74 | return this; 75 | } 76 | 77 | @Override 78 | public String toString() { 79 | return "direction = " + direction; 80 | } 81 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/hardware/HardwareMotor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.hardware; 18 | 19 | import org.strongback.components.Motor; 20 | import org.strongback.function.DoubleToDoubleFunction; 21 | 22 | import edu.wpi.first.wpilibj.PWM; 23 | import edu.wpi.first.wpilibj.SpeedController; 24 | 25 | /** 26 | * Wrapper for WPILib {@link SpeedController}. 27 | * 28 | * @author Zach Anderson 29 | * @see Motor 30 | * @see Hardware 31 | * @see edu.wpi.first.wpilibj.SpeedController 32 | */ 33 | class HardwareMotor implements Motor { 34 | 35 | private final SpeedController controller; 36 | private final DoubleToDoubleFunction speedValidator; 37 | 38 | HardwareMotor(SpeedController controller, DoubleToDoubleFunction speedValidator ) { 39 | this.controller = controller; 40 | this.speedValidator = speedValidator; 41 | } 42 | 43 | @Override 44 | public HardwareMotor setSpeed(double speed) { 45 | controller.set(speedValidator.applyAsDouble(speed)); 46 | return this; 47 | } 48 | 49 | @Override 50 | public double getSpeed() { 51 | return controller.get(); 52 | } 53 | 54 | public short getSpeedAsShort() { 55 | return (short) ((PWM) (controller)).getRaw(); 56 | } 57 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/hardware/HardwareRelay.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.hardware; 18 | 19 | import org.strongback.components.Relay; 20 | 21 | import edu.wpi.first.wpilibj.Relay.Value; 22 | 23 | /** 24 | * Wrapper for the WPILib Relay, and which has no delay and thus is only 25 | * {@link org.CommandState.robot.component.Relay.State#ON} or {@link org.CommandState.robot.component.Relay.State#OFF}. 26 | * This class cannot be constructed directly, use HardwareFactory to get instances of it. 27 | * 28 | * @author Zach Anderson 29 | * @see Relay 30 | * @see Hardware 31 | * @see edu.wpi.first.wpilibj.Relay 32 | */ 33 | final class HardwareRelay implements Relay { 34 | 35 | private final edu.wpi.first.wpilibj.Relay relay; 36 | 37 | HardwareRelay(int channel) { 38 | this.relay = new edu.wpi.first.wpilibj.Relay(channel); 39 | } 40 | 41 | @Override 42 | public HardwareRelay on() { 43 | relay.set(Value.kForward); 44 | return this; 45 | } 46 | 47 | @Override 48 | public HardwareRelay off() { 49 | relay.set(Value.kOff); 50 | return this; 51 | } 52 | 53 | @Override 54 | public State state() { 55 | Value value = relay.get(); 56 | if (value == Value.kForward || value == Value.kOn) return State.ON; 57 | if (value == Value.kReverse || value == Value.kOff) return State.OFF; 58 | return State.UNKOWN; 59 | } 60 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/hardware/HardwareSpark.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2017, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.hardware; 18 | 19 | import org.strongback.annotation.Immutable; 20 | import org.strongback.function.DoubleToDoubleFunction; 21 | 22 | import edu.wpi.first.wpilibj.SpeedController; 23 | 24 | /** 25 | * RevRobotics Spark motor controller. 26 | * 27 | * @author Randall Hauch 28 | */ 29 | @Immutable 30 | class HardwareSpark extends HardwareMotor { 31 | 32 | HardwareSpark(SpeedController controller, DoubleToDoubleFunction speedValidator ) { 33 | super(controller,speedValidator); 34 | } 35 | } -------------------------------------------------------------------------------- /strongback/src/org/strongback/util/Collections.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.util; 18 | 19 | import java.util.Collection; 20 | import java.util.LinkedHashSet; 21 | import java.util.Set; 22 | 23 | /** 24 | * Utility methods for constructing various kinds of collections. 25 | * 26 | * @author Randall Hauch 27 | */ 28 | public final class Collections { 29 | 30 | private Collections() { 31 | } 32 | 33 | /** 34 | * Create an immutable set from the supplied items. 35 | * 36 | * @param elements the elements to put into the new immutable set 37 | * @return the new immutable set; never null but possibly empty if {@code elements} is null or empty 38 | * @param the type of elements in the set 39 | */ 40 | public static Set immutableSet(Collection elements) { 41 | if (elements == null || elements.isEmpty()) return emptySet(); 42 | return java.util.Collections.unmodifiableSet(new LinkedHashSet<>(elements)); 43 | } 44 | 45 | /** 46 | * Create an immutable set from the supplied items. 47 | * 48 | * @param elements the elements to put into the new immutable set 49 | * @return the new immutable set; never null but possibly empty if {@code elements} is null or empty 50 | * @param the type of elements in the set 51 | */ 52 | public static Set immutableSet(@SuppressWarnings("unchecked") T... elements) { 53 | if (elements == null || elements.length == 0) return emptySet(); 54 | Set result = new LinkedHashSet<>(elements.length); 55 | for (T element : elements) { 56 | if (element != null) result.add(element); 57 | } 58 | return java.util.Collections.unmodifiableSet(result); 59 | } 60 | 61 | /** 62 | * Create an empty immutable set. 63 | * 64 | * @return the new immutable set; never null 65 | * @param the type of elements in the set 66 | */ 67 | public static Set emptySet() { 68 | return java.util.Collections.emptySet(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /strongback/src/org/strongback/util/Iterators.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Strongback 3 | * Copyright 2015, Strongback and individual contributors by the @authors tag. 4 | * See the COPYRIGHT.txt in the distribution for a full listing of individual 5 | * contributors. 6 | * 7 | * Licensed under the MIT License; you may not use this file except in 8 | * compliance with the License. You may obtain a copy of the License at 9 | * http://opensource.org/licenses/MIT 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.strongback.util; 18 | 19 | import java.util.Iterator; 20 | import java.util.NoSuchElementException; 21 | 22 | /** 23 | * Utility class for constructing custom iterators. 24 | * 25 | * @author Randall Hauch 26 | */ 27 | public final class Iterators { 28 | 29 | /** 30 | * Create an immutable iterator around the supplied {@link Iterable} object. 31 | * 32 | * @param iterable the iterable object 33 | * @return an immutable iterator, or an {@link #empty() empty} iterator if {@code iterable} is null 34 | * @param the type of element to iterate over 35 | */ 36 | public static Iterator immutable(Iterable iterable) { 37 | return iterable != null ? immutable(iterable.iterator()) : empty(); 38 | } 39 | 40 | /** 41 | * Create an immutable iterator around the supplied {@link Iterator}. 42 | * 43 | * @param iterator the existing iterator 44 | * @return an immutable iterator, or an {@link #empty() empty} iterator if {@code iterable} is null 45 | * @param the type of element to iterate over 46 | */ 47 | public static Iterator immutable(Iterator iterator) { 48 | return iterator == null ? empty() : new Iterator() { 49 | @Override 50 | public boolean hasNext() { 51 | return iterator.hasNext(); 52 | } 53 | 54 | @Override 55 | public T next() { 56 | return iterator.next(); 57 | } 58 | }; 59 | } 60 | 61 | /** 62 | * Create an empty iterator. 63 | * 64 | * @return the empty iterator; never null 65 | * @param the type of element to iterate over 66 | */ 67 | public static Iterator empty() { 68 | return new Iterator() { 69 | @Override 70 | public boolean hasNext() { 71 | return false; 72 | } 73 | 74 | @Override 75 | public T next() { 76 | throw new NoSuchElementException(); 77 | } 78 | }; 79 | } 80 | 81 | private Iterators() { 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /templates/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | templates 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /templates/Robot.java.template: -------------------------------------------------------------------------------- 1 | /* Created DATE */ 2 | package PACKAGE; 3 | 4 | import org.strongback.Strongback; 5 | 6 | import edu.wpi.first.wpilibj.IterativeRobot; 7 | 8 | public class Robot extends IterativeRobot { 9 | 10 | @Override 11 | public void robotInit() { 12 | } 13 | 14 | @Override 15 | public void teleopInit() { 16 | // Start Strongback functions ... 17 | Strongback.start(); 18 | } 19 | 20 | @Override 21 | public void teleopPeriodic() { 22 | } 23 | 24 | @Override 25 | public void disabledInit() { 26 | // Tell Strongback that the robot is disabled so it can flush and kill commands. 27 | Strongback.disable(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /templates/Strongback.userlibraries.import.template: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /templates/Strongback.userlibraries.template: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /templates/TestRobot.java.template: -------------------------------------------------------------------------------- 1 | /* Created DATE */ 2 | package PACKAGE; 3 | 4 | import org.junit.Test; 5 | 6 | public class TestRobot { 7 | 8 | @Test 9 | public void test() { 10 | // do something here 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /templates/build.properties.template: -------------------------------------------------------------------------------- 1 | # Project specific information 2 | # Created DATE 3 | package=PACKAGE 4 | robot.class=${package}.Robot 5 | simulation.world.file=/usr/share/frcsim/worlds/GearsBotDemo.world 6 | -------------------------------------------------------------------------------- /templates/build.xml.template: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /templates/classpath.template: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /templates/project.template: -------------------------------------------------------------------------------- 1 | 2 | 3 | PROJECT_NAME 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | edu.wpi.first.wpilib.plugins.core.nature.FRCProjectNature 17 | 18 | 19 | --------------------------------------------------------------------------------