Skip to content

Commit aaff88f

Browse files
committed
Update Mechanism2d
1 parent 9ae6854 commit aaff88f

File tree

4 files changed

+38
-188
lines changed

4 files changed

+38
-188
lines changed

wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/Mechanism2d.java

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44

55
package edu.wpi.first.wpilibj.smartdashboard;
66

7-
import edu.wpi.first.networktables.DoubleArrayPublisher;
8-
import edu.wpi.first.networktables.NTSendable;
9-
import edu.wpi.first.networktables.NTSendableBuilder;
10-
import edu.wpi.first.networktables.NetworkTable;
11-
import edu.wpi.first.networktables.StringPublisher;
7+
import edu.wpi.first.telemetry.TelemetryLoggable;
8+
import edu.wpi.first.telemetry.TelemetryTable;
129
import edu.wpi.first.wpilibj.util.Color8Bit;
1310
import java.util.HashMap;
1411
import java.util.Map;
@@ -25,13 +22,10 @@
2522
* @see MechanismLigament2d
2623
* @see MechanismRoot2d
2724
*/
28-
public final class Mechanism2d implements NTSendable, AutoCloseable {
29-
private NetworkTable m_table;
25+
public final class Mechanism2d implements TelemetryLoggable, AutoCloseable {
3026
private final Map<String, MechanismRoot2d> m_roots;
3127
private final double[] m_dims = new double[2];
3228
private String m_color;
33-
private DoubleArrayPublisher m_dimsPub;
34-
private StringPublisher m_colorPub;
3529

3630
/**
3731
* Create a new Mechanism2d with the given dimensions and default color (dark blue).
@@ -63,12 +57,6 @@ public Mechanism2d(double width, double height, Color8Bit backgroundColor) {
6357

6458
@Override
6559
public void close() {
66-
if (m_dimsPub != null) {
67-
m_dimsPub.close();
68-
}
69-
if (m_colorPub != null) {
70-
m_colorPub.close();
71-
}
7260
for (MechanismRoot2d root : m_roots.values()) {
7361
root.close();
7462
}
@@ -92,9 +80,6 @@ public synchronized MechanismRoot2d getRoot(String name, double x, double y) {
9280

9381
var root = new MechanismRoot2d(name, x, y);
9482
m_roots.put(name, root);
95-
if (m_table != null) {
96-
root.update(m_table.getSubTable(name));
97-
}
9883
return root;
9984
}
10085

@@ -105,32 +90,18 @@ public synchronized MechanismRoot2d getRoot(String name, double x, double y) {
10590
*/
10691
public synchronized void setBackgroundColor(Color8Bit color) {
10792
m_color = color.toHexString();
108-
if (m_colorPub != null) {
109-
m_colorPub.set(m_color);
110-
}
11193
}
11294

11395
@Override
114-
public void initSendable(NTSendableBuilder builder) {
115-
builder.setSmartDashboardType("Mechanism2d");
96+
public void toTelemetry(TelemetryTable table, boolean first) {
97+
if (first) {
98+
table.log(".type", "Mechanism2d");
99+
}
116100
synchronized (this) {
117-
m_table = builder.getTable();
118-
if (m_dimsPub != null) {
119-
m_dimsPub.close();
120-
}
121-
m_dimsPub = m_table.getDoubleArrayTopic("dims").publish();
122-
m_dimsPub.set(m_dims);
123-
if (m_colorPub != null) {
124-
m_colorPub.close();
125-
}
126-
m_colorPub = m_table.getStringTopic("backgroundColor").publish();
127-
m_colorPub.set(m_color);
101+
table.log("dims", m_dims);
102+
table.log("backgroundColor", m_color);
128103
for (Entry<String, MechanismRoot2d> entry : m_roots.entrySet()) {
129-
String name = entry.getKey();
130-
MechanismRoot2d root = entry.getValue();
131-
synchronized (root) {
132-
root.update(m_table.getSubTable(name));
133-
}
104+
table.log(entry.getKey(), entry.getValue());
134105
}
135106
}
136107
}

wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/MechanismLigament2d.java

Lines changed: 12 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
package edu.wpi.first.wpilibj.smartdashboard;
66

77
import edu.wpi.first.math.geometry.Rotation2d;
8-
import edu.wpi.first.networktables.DoubleEntry;
9-
import edu.wpi.first.networktables.NetworkTable;
10-
import edu.wpi.first.networktables.StringEntry;
11-
import edu.wpi.first.networktables.StringPublisher;
12-
import edu.wpi.first.networktables.StringTopic;
8+
import edu.wpi.first.telemetry.TelemetryTable;
139
import edu.wpi.first.wpilibj.util.Color8Bit;
1410

1511
/**
@@ -19,17 +15,10 @@
1915
* @see Mechanism2d
2016
*/
2117
public class MechanismLigament2d extends MechanismObject2d {
22-
private StringPublisher m_typePub;
23-
private double m_angle;
24-
private DoubleEntry m_angleEntry;
18+
private Rotation2d m_angle;
2519
private String m_color;
26-
private StringEntry m_colorEntry;
2720
private double m_length;
28-
private DoubleEntry m_lengthEntry;
2921
private double m_weight;
30-
private DoubleEntry m_weightEntry;
31-
32-
private static String kSmartDashboardType = "line";
3322

3423
/**
3524
* Create a new ligament.
@@ -60,36 +49,13 @@ public MechanismLigament2d(String name, double length, double angle) {
6049
this(name, length, angle, 10, new Color8Bit(235, 137, 52));
6150
}
6251

63-
@Override
64-
public void close() {
65-
super.close();
66-
if (m_typePub != null) {
67-
m_typePub.close();
68-
}
69-
if (m_angleEntry != null) {
70-
m_angleEntry.close();
71-
}
72-
if (m_colorEntry != null) {
73-
m_colorEntry.close();
74-
}
75-
if (m_lengthEntry != null) {
76-
m_lengthEntry.close();
77-
}
78-
if (m_weightEntry != null) {
79-
m_weightEntry.close();
80-
}
81-
}
82-
8352
/**
8453
* Set the ligament's angle relative to its parent.
8554
*
8655
* @param degrees the angle in degrees
8756
*/
8857
public final synchronized void setAngle(double degrees) {
89-
m_angle = degrees;
90-
if (m_angleEntry != null) {
91-
m_angleEntry.set(degrees);
92-
}
58+
m_angle = Rotation2d.fromDegrees(degrees);
9359
}
9460

9561
/**
@@ -106,10 +72,7 @@ public synchronized void setAngle(Rotation2d angle) {
10672
*
10773
* @return the angle in degrees
10874
*/
109-
public synchronized double getAngle() {
110-
if (m_angleEntry != null) {
111-
m_angle = m_angleEntry.get();
112-
}
75+
public synchronized Rotation2d getAngle() {
11376
return m_angle;
11477
}
11578

@@ -120,9 +83,6 @@ public synchronized double getAngle() {
12083
*/
12184
public final synchronized void setLength(double length) {
12285
m_length = length;
123-
if (m_lengthEntry != null) {
124-
m_lengthEntry.set(length);
125-
}
12686
}
12787

12888
/**
@@ -131,9 +91,6 @@ public final synchronized void setLength(double length) {
13191
* @return the line length
13292
*/
13393
public synchronized double getLength() {
134-
if (m_lengthEntry != null) {
135-
m_length = m_lengthEntry.get();
136-
}
13794
return m_length;
13895
}
13996

@@ -144,9 +101,6 @@ public synchronized double getLength() {
144101
*/
145102
public final synchronized void setColor(Color8Bit color) {
146103
m_color = String.format("#%02X%02X%02X", color.red, color.green, color.blue);
147-
if (m_colorEntry != null) {
148-
m_colorEntry.set(m_color);
149-
}
150104
}
151105

152106
/**
@@ -155,9 +109,6 @@ public final synchronized void setColor(Color8Bit color) {
155109
* @return the color of the line
156110
*/
157111
public synchronized Color8Bit getColor() {
158-
if (m_colorEntry != null) {
159-
m_color = m_colorEntry.get();
160-
}
161112
int r = 0;
162113
int g = 0;
163114
int b = 0;
@@ -182,9 +133,6 @@ public synchronized Color8Bit getColor() {
182133
*/
183134
public final synchronized void setLineWeight(double weight) {
184135
m_weight = weight;
185-
if (m_weightEntry != null) {
186-
m_weightEntry.set(weight);
187-
}
188136
}
189137

190138
/**
@@ -193,46 +141,17 @@ public final synchronized void setLineWeight(double weight) {
193141
* @return the line thickness
194142
*/
195143
public synchronized double getLineWeight() {
196-
if (m_weightEntry != null) {
197-
m_weight = m_weightEntry.get();
198-
}
199144
return m_weight;
200145
}
201146

202147
@Override
203-
protected void updateEntries(NetworkTable table) {
204-
if (m_typePub != null) {
205-
m_typePub.close();
206-
}
207-
m_typePub =
208-
table
209-
.getStringTopic(".type")
210-
.publishEx(
211-
StringTopic.kTypeString, "{\"SmartDashboard\":\"" + kSmartDashboardType + "\"}");
212-
m_typePub.set(kSmartDashboardType);
213-
214-
if (m_angleEntry != null) {
215-
m_angleEntry.close();
216-
}
217-
m_angleEntry = table.getDoubleTopic("angle").getEntry(0.0);
218-
m_angleEntry.set(m_angle);
219-
220-
if (m_lengthEntry != null) {
221-
m_lengthEntry.close();
222-
}
223-
m_lengthEntry = table.getDoubleTopic("length").getEntry(0.0);
224-
m_lengthEntry.set(m_length);
225-
226-
if (m_colorEntry != null) {
227-
m_colorEntry.close();
228-
}
229-
m_colorEntry = table.getStringTopic("color").getEntry("");
230-
m_colorEntry.set(m_color);
231-
232-
if (m_weightEntry != null) {
233-
m_weightEntry.close();
234-
}
235-
m_weightEntry = table.getDoubleTopic("weight").getEntry(0.0);
236-
m_weightEntry.set(m_weight);
148+
public void toTelemetry(TelemetryTable table, boolean first) {
149+
if (first) {
150+
table.log(".type", "line");
151+
}
152+
table.log("angle", m_angle);
153+
table.log("length", m_length);
154+
table.log("color", m_color);
155+
table.log("weight", m_weight);
237156
}
238157
}

wpilibj/src/main/java/edu/wpi/first/wpilibj/smartdashboard/MechanismObject2d.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
package edu.wpi.first.wpilibj.smartdashboard;
66

7-
import edu.wpi.first.networktables.NetworkTable;
7+
import edu.wpi.first.telemetry.TelemetryLoggable;
8+
import edu.wpi.first.telemetry.TelemetryTable;
89
import java.util.HashMap;
910
import java.util.Map;
11+
import java.util.Map.Entry;
1012

1113
/**
1214
* Common base class for all Mechanism2d node types.
@@ -16,11 +18,10 @@
1618
*
1719
* @see Mechanism2d
1820
*/
19-
public abstract class MechanismObject2d implements AutoCloseable {
21+
public abstract class MechanismObject2d implements TelemetryLoggable, AutoCloseable {
2022
/** Relative to parent. */
2123
private final String m_name;
2224

23-
private NetworkTable m_table;
2425
private final Map<String, MechanismObject2d> m_objects = new HashMap<>(1);
2526

2627
/**
@@ -53,27 +54,9 @@ public final synchronized <T extends MechanismObject2d> T append(T object) {
5354
throw new UnsupportedOperationException("Mechanism object names must be unique!");
5455
}
5556
m_objects.put(object.getName(), object);
56-
if (m_table != null) {
57-
object.update(m_table.getSubTable(object.getName()));
58-
}
5957
return object;
6058
}
6159

62-
final synchronized void update(NetworkTable table) {
63-
m_table = table;
64-
updateEntries(m_table);
65-
for (MechanismObject2d obj : m_objects.values()) {
66-
obj.update(m_table.getSubTable(obj.m_name));
67-
}
68-
}
69-
70-
/**
71-
* Update this object's entries with new ones from a new table.
72-
*
73-
* @param table the new table.
74-
*/
75-
protected abstract void updateEntries(NetworkTable table);
76-
7760
/**
7861
* Retrieve the object's name.
7962
*
@@ -82,4 +65,11 @@ final synchronized void update(NetworkTable table) {
8265
public final String getName() {
8366
return m_name;
8467
}
68+
69+
@Override
70+
public synchronized void toTelemetry(TelemetryTable table, boolean first) {
71+
for (Entry<String, MechanismObject2d> entry : m_objects.entrySet()) {
72+
table.log(entry.getKey(), entry.getValue());
73+
}
74+
}
8575
}

0 commit comments

Comments
 (0)