Skip to content

Commit

Permalink
Show battery voltage (fixes #125)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbrott committed Nov 5, 2023
1 parent a1fc0b6 commit dc7f2ab
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ public enum OpModeStatus {
private OpModeStatus activeOpModeStatus;
private String warningMessage;
private String errorMessage;
private double batteryVoltage;

/**
* Creates a status object with the default values.
*/
public RobotStatus(boolean enabled, boolean available, String activeOpMode,
OpModeStatus activeOpModeStatus, String warningMessage,
String errorMessage) {
String errorMessage, double batteryVoltage) {
this.enabled = enabled;
this.available = available;
this.activeOpMode = activeOpMode;
this.activeOpModeStatus = activeOpModeStatus;
this.warningMessage = warningMessage;
this.errorMessage = errorMessage;
this.batteryVoltage = batteryVoltage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected void onMessage(NanoWSD.WebSocketFrame message) {
}

send(new ReceiveRobotStatus(
new RobotStatus(core.enabled, true, opModeName, opModeStatus, "", "")
new RobotStatus(core.enabled, true, opModeName, opModeStatus, "", "", 12.0)
));
break;
}
Expand Down
6 changes: 5 additions & 1 deletion FtcDashboard/dash/src/components/Dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export default function Dashboard() {
(state: RootState) => state.settings.layoutPreset,
);
const enabled = useSelector((state: RootState) => state.status.enabled);
const batteryVoltage = useSelector(
(state: RootState) => state.status.batteryVoltage,
);
const dispatch = useDispatch();

const [isSettingsModalOpen, setIsSettingsModalOpen] = useState(false);
Expand Down Expand Up @@ -58,11 +61,12 @@ export default function Dashboard() {
<p
className="mx-2"
style={{
width: '60px',
width: batteryVoltage > 0 ? '120px' : '60px',
textAlign: 'right',
}}
>
{socket.pingTime}ms
{batteryVoltage > 0 ? ` / ${batteryVoltage.toFixed(2)}V` : ''}
</p>
)}
{socket.isConnected ? (
Expand Down
1 change: 1 addition & 0 deletions FtcDashboard/dash/src/store/reducers/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const initialState: StatusState = {
errorMessage: '',
opModeList: [],
gamepadsSupported: true,
batteryVoltage: -1.0,
};

const statusReducer = (
Expand Down
2 changes: 2 additions & 0 deletions FtcDashboard/dash/src/store/types/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type RobotStatus = {
activeOpModeStatus: Values<typeof OpModeStatus>;
warningMessage: string;
errorMessage: string;
batteryVoltage: number;
};

export type StatusState = {
Expand All @@ -22,6 +23,7 @@ export type StatusState = {
activeOpModeStatus: Values<typeof OpModeStatus>;
warningMessage: string;
errorMessage: string;
batteryVoltage: number;
opModeList: string[];
gamepadsSupported: boolean;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.acmerobotics.dashboard.message.redux.ReceiveRobotStatus;
import com.acmerobotics.dashboard.telemetry.TelemetryPacket;
import com.qualcomm.ftccommon.FtcEventLoop;
import com.qualcomm.hardware.lynx.LynxModule;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
Expand Down Expand Up @@ -59,6 +60,7 @@
import org.firstinspires.ftc.robotcore.external.Telemetry;
import org.firstinspires.ftc.robotcore.external.function.Consumer;
import org.firstinspires.ftc.robotcore.external.function.Continuation;
import org.firstinspires.ftc.robotcore.external.navigation.VoltageUnit;
import org.firstinspires.ftc.robotcore.external.stream.CameraStreamSource;
import org.firstinspires.ftc.robotcore.internal.opmode.OpModeMeta;
import org.firstinspires.ftc.robotcore.internal.opmode.RegisteredOpModes;
Expand Down Expand Up @@ -1185,14 +1187,23 @@ private void updateGamepads(ReceiveGamepadState.Gamepad gamepad1,
private RobotStatus getRobotStatus() {
if (opModeManager == null) {
return new RobotStatus(core.enabled, false, "", RobotStatus.OpModeStatus.STOPPED, "",
"");
"", -1.0);
} else {
return activeOpMode.with(o -> {
double batteryVoltage = -1.0;
if (o.opMode.hardwareMap != null) {
for (LynxModule m : o.opMode.hardwareMap.getAll(LynxModule.class)) {
batteryVoltage =
Math.max(batteryVoltage, m.getInputVoltage(VoltageUnit.VOLTS));
}
}

return new RobotStatus(
core.enabled, true, opModeManager.getActiveOpModeName(),
// status is an enum so it's okay to return a copy here.
o.status,
RobotLog.getGlobalWarningMessage().message, RobotLog.getGlobalErrorMsg()
RobotLog.getGlobalWarningMessage().message, RobotLog.getGlobalErrorMsg(),
batteryVoltage
);
});
}
Expand Down

0 comments on commit dc7f2ab

Please sign in to comment.