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
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 | *
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.
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 | *
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
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 | *
19 | *
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 | *
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 | *
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 | *
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 | * 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