Skip to content

Commit be16b80

Browse files
committed
ftc codesim hardware improvements
1 parent 9e332dd commit be16b80

4 files changed

Lines changed: 110 additions & 1 deletion

File tree

simulation/SyntheSimFTC/src/main/java/com/autodesk/synthesis/ftc/FTCWsBridge.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public interface ConnectionListener {
2727
private final Gamepad gamepad1 = new Gamepad();
2828
private final Gamepad gamepad2 = new Gamepad();
2929
private final Map<String, SynthesisDcMotor> dcMotors = new ConcurrentHashMap<>();
30+
private final Map<String, SynthesisServo> servos = new ConcurrentHashMap<>();
3031
private volatile ConnectionListener listener;
3132

3233
private volatile boolean dsEnabled;
@@ -70,6 +71,19 @@ public void sendMotorPower(String deviceName, double power) {
7071
send("CANMotor", deviceName, data);
7172
}
7273

74+
public void registerServo(String deviceName, SynthesisServo servo) {
75+
servos.put(deviceName, servo);
76+
Map<String, Object> init = new HashMap<>();
77+
init.put("<init", true);
78+
send("PWM", deviceName, init);
79+
}
80+
81+
public void sendServoPosition(String deviceName, double position) {
82+
Map<String, Object> data = new HashMap<>();
83+
data.put("<position", position);
84+
send("PWM", deviceName, data);
85+
}
86+
7387
//Announces the driver station deice
7488
private void registerDriverStation() {
7589
Map<String, Object> init = new HashMap<>();
@@ -107,6 +121,7 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) {
107121
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
108122
System.out.println("[FTCWsBridge] Fission disconnected: " + reason);
109123
dcMotors.clear();
124+
servos.clear();
110125
ConnectionListener l = listener;
111126
if (l != null) {
112127
l.onFissionDisconnected();

simulation/SyntheSimFTC/src/main/java/com/autodesk/synthesis/ftc/OpModeRunner.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,12 @@ private void stopCurrent() {
146146
}
147147

148148
private com.qualcomm.robotcore.hardware.HardwareDevice createDevice(Class<?> requestedType, String deviceName) {
149-
// Widen this as the shim grows past DcMotorSimple (Servo, CRServo, IMU, ...).
150149
if (requestedType.isAssignableFrom(SynthesisDcMotor.class)) {
151150
return new SynthesisDcMotor(deviceName, bridge);
152151
}
152+
if (requestedType.isAssignableFrom(SynthesisServo.class)) {
153+
return new SynthesisServo(deviceName, bridge);
154+
}
153155

154156
return null;
155157
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.autodesk.synthesis.ftc;
2+
3+
import com.qualcomm.robotcore.hardware.HardwareDevice;
4+
import com.qualcomm.robotcore.hardware.Servo;
5+
6+
/**
7+
* Minimal synthetic FTC servo backed by the PWM contract used by Fission.
8+
*/
9+
public class SynthesisServo implements Servo {
10+
private final String deviceName;
11+
private final FTCWsBridge bridge;
12+
13+
private volatile Direction direction = Direction.FORWARD;
14+
private volatile double position = 0.0;
15+
16+
public SynthesisServo(String deviceName, FTCWsBridge bridge) {
17+
this.deviceName = deviceName;
18+
this.bridge = bridge;
19+
bridge.registerServo(deviceName, this);
20+
}
21+
22+
@Override
23+
public void setDirection(Direction direction) {
24+
this.direction = direction;
25+
}
26+
27+
@Override
28+
public Direction getDirection() {
29+
return direction;
30+
}
31+
32+
@Override
33+
public void setPosition(double position) {
34+
this.position = Math.max(0.0, Math.min(1.0, position));
35+
double effectivePosition = direction == Direction.REVERSE ? 1.0 - this.position : this.position;
36+
bridge.sendServoPosition(deviceName, effectivePosition);
37+
}
38+
39+
@Override
40+
public double getPosition() {
41+
return position;
42+
}
43+
44+
@Override
45+
public HardwareDevice.Manufacturer getManufacturer() {
46+
return HardwareDevice.Manufacturer.Synthesis;
47+
}
48+
49+
@Override
50+
public String getDeviceName() {
51+
return "Synthesis Servo";
52+
}
53+
54+
@Override
55+
public String getConnectionInfo() {
56+
return "Synthesis simulation: " + deviceName;
57+
}
58+
59+
@Override
60+
public int getVersion() {
61+
return 1;
62+
}
63+
64+
@Override
65+
public void resetDeviceConfigurationForOpMode() {
66+
direction = Direction.FORWARD;
67+
position = 0.0;
68+
}
69+
70+
@Override
71+
public void close() {
72+
}
73+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.qualcomm.robotcore.hardware;
2+
3+
/**
4+
* Clean-room shim of the real FTC SDK interface.
5+
*/
6+
public interface Servo extends HardwareDevice {
7+
enum Direction {
8+
FORWARD,
9+
REVERSE,
10+
}
11+
12+
void setDirection(Direction direction);
13+
14+
Direction getDirection();
15+
16+
void setPosition(double position);
17+
18+
double getPosition();
19+
}

0 commit comments

Comments
 (0)