Skip to content

Commit abec11c

Browse files
committed
Merge pull request #371 from ThomasJClark/nt
Add publish operations for number, point, and size
2 parents 8d1c950 + 3a60434 commit abec11c

5 files changed

Lines changed: 91 additions & 6 deletions

File tree

core/src/main/java/edu/wpi/grip/core/operations/Operations.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
import com.google.common.eventbus.EventBus;
44
import edu.wpi.grip.core.events.OperationAddedEvent;
55
import edu.wpi.grip.core.operations.composite.*;
6+
import edu.wpi.grip.core.operations.networktables.NTNumber;
67
import edu.wpi.grip.core.operations.networktables.NTPublishOperation;
8+
import edu.wpi.grip.core.operations.networktables.NTVector2D;
79
import edu.wpi.grip.core.operations.opencv.MatFieldAccessor;
810
import edu.wpi.grip.core.operations.opencv.MinMaxLoc;
911
import edu.wpi.grip.core.operations.opencv.NewPointOperation;
1012
import edu.wpi.grip.core.operations.opencv.NewSizeOperation;
1113

14+
import static org.bytedeco.javacpp.opencv_core.*;
15+
1216
public final class Operations {
1317

1418
private Operations() { /* no op */}
@@ -31,6 +35,9 @@ public static void addOperations(EventBus eventBus) {
3135
eventBus.post(new OperationAddedEvent(new NewPointOperation()));
3236
eventBus.post(new OperationAddedEvent(new NewSizeOperation()));
3337
eventBus.post(new OperationAddedEvent(new MatFieldAccessor()));
38+
eventBus.post(new OperationAddedEvent(new NTPublishOperation<>(Number.class, NTNumber.class, NTNumber::new)));
39+
eventBus.post(new OperationAddedEvent(new NTPublishOperation<>(Point.class, NTVector2D.class, NTVector2D::new)));
40+
eventBus.post(new OperationAddedEvent(new NTPublishOperation<>(Size.class, NTVector2D.class, NTVector2D::new)));
3441
eventBus.post(new OperationAddedEvent(new NTPublishOperation<>(ContoursReport.class)));
3542
eventBus.post(new OperationAddedEvent(new NTPublishOperation<>(BlobsReport.class)));
3643
eventBus.post(new OperationAddedEvent(new NTPublishOperation<>(LinesReport.class)));
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package edu.wpi.grip.core.operations.networktables;
2+
3+
import java.util.function.Function;
4+
5+
/**
6+
* An adapter to allow numbers to be published from GRIP sockets into NetworkTables
7+
*
8+
* @see NTPublishOperation#NTPublishOperation(Class, Class, Function)
9+
*/
10+
public class NTNumber implements NTPublishable {
11+
12+
private final double number;
13+
14+
public NTNumber(Number number) {
15+
this.number = number.doubleValue();
16+
}
17+
18+
@NTValue
19+
public double getValue() {
20+
return number;
21+
}
22+
}

core/src/main/java/edu/wpi/grip/core/operations/networktables/NTPublishOperation.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.ArrayList;
1313
import java.util.List;
1414
import java.util.Optional;
15+
import java.util.function.Function;
1516

1617
import static com.google.common.base.Preconditions.checkNotNull;
1718

@@ -21,16 +22,35 @@
2122
* To be publishable, a type should have one or more accessor methods annotated with {@link NTValue}. This is done
2223
* with annotations instead of methods
2324
*/
24-
public class NTPublishOperation<T extends NTPublishable> implements Operation {
25+
public class NTPublishOperation<S, T extends NTPublishable> implements Operation {
2526

26-
private final Class<T> type;
27+
private final Class<S> type;
28+
private final Function<S, T> converter;
2729
private final List<Method> ntValueMethods = new ArrayList<>();
2830

31+
/**
32+
* Create a new publish operation for a socket type that implements {@link NTPublishable} directly
33+
*/
34+
@SuppressWarnings("unchecked")
2935
public NTPublishOperation(Class<T> type) {
30-
this.type = checkNotNull(type, "Type was null");
36+
this((Class<S>) type, type, value -> (T) value);
37+
}
38+
39+
/**
40+
* Create a new publish operation where the socket type and NTPublishable type are different. This is useful for
41+
* classes that we don't create, such as JavaCV's {@link org.bytedeco.javacpp.opencv_core.Size} class, since we
42+
* can't make them implement additional interfaces.
43+
*
44+
* @param socketType The type of socket that can be connected to this step
45+
* @param reportType A class implementing {@link NTPublishable} that determines what data is sent to NetworkTables
46+
* @param converter A function to convert socket values into publishable values
47+
*/
48+
public NTPublishOperation(Class<S> socketType, Class<T> reportType, Function<S, T> converter) {
49+
this.type = checkNotNull(socketType, "Type was null");
50+
this.converter = checkNotNull(converter, "Converter was null");
3151

3252
// Any accessor method with an @NTValue annotation can be published to NetworkTables.
33-
for (Method method : type.getDeclaredMethods()) {
53+
for (Method method : reportType.getDeclaredMethods()) {
3454
if (method.getAnnotation(NTValue.class) != null) {
3555
if (method.getParameters().length > 0) {
3656
throw new IllegalArgumentException("@NTValue method must have 0 parameters: " + method);
@@ -88,7 +108,7 @@ public OutputSocket<?>[] createOutputSockets(EventBus eventBus) {
88108
public void perform(InputSocket<?>[] inputs, OutputSocket<?>[] outputs) {
89109
int i = 0;
90110

91-
final NTPublishable value = (NTPublishable) inputs[i++].getValue().get();
111+
final NTPublishable value = converter.apply((S) inputs[i++].getValue().get());
92112
final String subtableName = (String) inputs[i++].getValue().get();
93113

94114
if (subtableName.isEmpty()) {

core/src/main/java/edu/wpi/grip/core/operations/networktables/NTValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
@Retention(RetentionPolicy.RUNTIME)
2222
@Target(ElementType.METHOD)
2323
public @interface NTValue {
24-
String key();
24+
String key() default "";
2525
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package edu.wpi.grip.core.operations.networktables;
2+
3+
import java.util.function.Function;
4+
5+
import static org.bytedeco.javacpp.opencv_core.*;
6+
7+
/**
8+
* A type publishable to NetworkTables that consists of two numbers. JavaCV {@link Point}s and {@link Size}s are
9+
* converted into this.
10+
*
11+
* @see NTPublishOperation#NTPublishOperation(Class, Class, Function)
12+
*/
13+
public class NTVector2D implements NTPublishable {
14+
15+
private final double x, y;
16+
17+
public NTVector2D(Point point) {
18+
this.x = point.x();
19+
this.y = point.y();
20+
}
21+
22+
public NTVector2D(Size size) {
23+
this.x = size.width();
24+
this.y = size.height();
25+
}
26+
27+
@NTValue(key = "x")
28+
public double getX() {
29+
return x;
30+
}
31+
32+
@NTValue(key = "y")
33+
public double getY() {
34+
return y;
35+
}
36+
}

0 commit comments

Comments
 (0)