diff --git a/gui/public/i18n/en/translation.ftl b/gui/public/i18n/en/translation.ftl index eb41704bc..dd200d8e4 100644 --- a/gui/public/i18n/en/translation.ftl +++ b/gui/public/i18n/en/translation.ftl @@ -293,6 +293,7 @@ widget-imu_visualizer-rotation_raw = Raw rotation widget-imu_visualizer-rotation_preview = Preview rotation widget-imu_visualizer-acceleration = Acceleration widget-imu_visualizer-position = Position +widget-imu_visualizer-stay_aligned = Stay Aligned ## Widget: Skeleton Visualizer widget-skeleton_visualizer-preview = Skeleton preview @@ -317,6 +318,7 @@ tracker-table-column-temperature = Temp. °C tracker-table-column-linear-acceleration = Accel. X/Y/Z tracker-table-column-rotation = Rotation X/Y/Z tracker-table-column-position = Position X/Y/Z +tracker-table-column-stay_aligned = Stay Aligned tracker-table-column-url = URL ## Tracker rotation @@ -447,6 +449,7 @@ mounting_selection_menu-close = Close settings-sidebar-title = Settings settings-sidebar-general = General settings-sidebar-tracker_mechanics = Tracker mechanics +settings-sidebar-stay_aligned = Stay Aligned settings-sidebar-fk_settings = Tracking settings settings-sidebar-gesture_control = Gesture control settings-sidebar-interface = Interface @@ -539,6 +542,27 @@ settings-general-tracker_mechanics-use_mag_on_all_trackers-description = Can be disabled per tracker in the tracker's settings. Please don't shutdown any of the trackers while toggling this! settings-general-tracker_mechanics-use_mag_on_all_trackers-label = Use magnetometer on trackers +settings-stay_aligned = Stay Aligned +settings-stay_aligned-description = Stay Aligned reduces drift by gradually adjusting your trackers to match your most common poses. +settings-stay_aligned-warnings-drift_compensation = ⚠ Please turn off Drift Compensation! Drift Compensation will conflict with Stay Aligned. +settings-stay_aligned-enabled-label = Enabled +settings-stay_aligned-extra_yaw_correction-label = Extra correction for low quality IMUs (e.g. BMI160, MPU60XX) +settings-stay_aligned-hide_yaw_correction-label = Hide correction (for comparison) +settings-stay_aligned-general-label = General +settings-stay_aligned-relaxed_poses-label = Relaxed Poses +settings-stay_aligned-relaxed_poses-standing = Standing +settings-stay_aligned-relaxed_poses-sitting = Sitting in chair +settings-stay_aligned-relaxed_poses-flat = Sitting on floor +settings-stay_aligned-relaxed_poses-current_angles = Current angles +settings-stay_aligned-relaxed_poses-upper_leg_angle = Thigh +settings-stay_aligned-relaxed_poses-lower_leg_angle = Ankle +settings-stay_aligned-relaxed_poses-foot_angle = Foot +settings-stay_aligned-relaxed_poses-detect_pose = Detect pose +settings-stay_aligned-relaxed_poses-reset_pose = Reset pose +settings-stay_aligned-relaxed_poses-outwards = {$angle} outwards +settings-stay_aligned-relaxed_poses-inwards = {$angle} inwards +settings-stay_aligned-relaxed_poses-disabled = Not enabled + ## FK/Tracking settings settings-general-fk_settings = Tracking settings diff --git a/gui/src/components/commons/NumberSelector.tsx b/gui/src/components/commons/NumberSelector.tsx index a21d35634..5e2eb022d 100644 --- a/gui/src/components/commons/NumberSelector.tsx +++ b/gui/src/components/commons/NumberSelector.tsx @@ -83,7 +83,7 @@ export function NumberSelector({ - -
+
{valueLabelFormat ? valueLabelFormat(value) : value}
diff --git a/gui/src/components/settings/SettingsSidebar.tsx b/gui/src/components/settings/SettingsSidebar.tsx index 97325b062..ffb09640a 100644 --- a/gui/src/components/settings/SettingsSidebar.tsx +++ b/gui/src/components/settings/SettingsSidebar.tsx @@ -57,6 +57,9 @@ export function SettingsSidebar() { SteamVR + + {l10n.getString('settings-sidebar-stay_aligned')} + {l10n.getString('settings-sidebar-tracker_mechanics')} diff --git a/gui/src/components/settings/pages/GeneralSettings.tsx b/gui/src/components/settings/pages/GeneralSettings.tsx index 5b0bb6e32..267b9c7da 100644 --- a/gui/src/components/settings/pages/GeneralSettings.tsx +++ b/gui/src/components/settings/pages/GeneralSettings.tsx @@ -33,8 +33,15 @@ import { import { HandsWarningModal } from '@/components/settings/HandsWarningModal'; import { MagnetometerToggleSetting } from './MagnetometerToggleSetting'; import { DriftCompensationModal } from '@/components/settings/DriftCompensationModal'; +import { + defaultStayAlignedSettings, + StayAlignedSettings, + StayAlignedSettingsForm, + serializeStayAlignedSettings, + deserializeStayAlignedSettings, +} from './components/StayAlignedSettings'; -interface SettingsForm { +export type SettingsForm = { trackers: { waist: boolean; chest: boolean; @@ -103,7 +110,8 @@ interface SettingsForm { saveMountingReset: boolean; resetHmdPitch: boolean; }; -} + stayAligned: StayAlignedSettingsForm; +}; const defaultValues: SettingsForm = { trackers: { @@ -169,6 +177,7 @@ const defaultValues: SettingsForm = { saveMountingReset: false, resetHmdPitch: false, }, + stayAligned: defaultStayAlignedSettings, }; export function GeneralSettings() { @@ -297,6 +306,8 @@ export function GeneralSettings() { driftCompensation.maxResets = values.driftCompensation.maxResets; settings.driftCompensation = driftCompensation; + settings.stayAligned = serializeStayAlignedSettings(values.stayAligned); + if (values.resetsSettings) { const resetsSettings = new ResetsSettingsT(); resetsSettings.resetMountingFeet = @@ -420,6 +431,12 @@ export function GeneralSettings() { formData.resetsSettings = settings.resetsSettings; } + if (settings.stayAligned) { + formData.stayAligned = deserializeStayAlignedSettings( + settings.stayAligned + ); + } + reset({ ...getValues(), ...formData }); }); @@ -612,6 +629,7 @@ export function GeneralSettings() {
+ } id="mechanics"> <> diff --git a/gui/src/components/settings/pages/components/StayAlignedSettings.tsx b/gui/src/components/settings/pages/components/StayAlignedSettings.tsx new file mode 100644 index 000000000..516671c54 --- /dev/null +++ b/gui/src/components/settings/pages/components/StayAlignedSettings.tsx @@ -0,0 +1,153 @@ +import { Control } from 'react-hook-form'; +import { StayAlignedSettingsT } from 'solarxr-protocol'; +import { SettingsForm } from '@/components/settings/pages/GeneralSettings'; +import { Button } from '@/components/commons/Button'; +import { CheckBox } from '@/components/commons/Checkbox'; +import { WrenchIcon } from '@/components/commons/icon/WrenchIcons'; +import { Typography } from '@/components/commons/Typography'; +import { SettingsPagePaneLayout } from '@/components/settings/SettingsPageLayout'; +import { useLocalization } from '@fluent/react'; +import { useConfig } from '@/hooks/config'; +import { + RelaxedPosesSettings, + RelaxedPosesSummary, +} from '@/components/stay-aligned/RelaxedPose'; + +export type StayAlignedSettingsForm = { + enabled: boolean; + extraYawCorrection: boolean; + hideYawCorrection: boolean; + standingEnabled: boolean; + standingUpperLegAngle: number; + standingLowerLegAngle: number; + standingFootAngle: number; + sittingEnabled: boolean; + sittingUpperLegAngle: number; + sittingLowerLegAngle: number; + sittingFootAngle: number; + flatEnabled: boolean; + flatUpperLegAngle: number; + flatLowerLegAngle: number; + flatFootAngle: number; +}; + +export const defaultStayAlignedSettings: StayAlignedSettingsForm = { + enabled: false, + extraYawCorrection: false, + hideYawCorrection: false, + standingEnabled: false, + standingUpperLegAngle: 0.0, + standingLowerLegAngle: 0.0, + standingFootAngle: 0.0, + sittingEnabled: false, + sittingUpperLegAngle: 0.0, + sittingLowerLegAngle: 0.0, + sittingFootAngle: 0.0, + flatEnabled: false, + flatUpperLegAngle: 0.0, + flatLowerLegAngle: 0.0, + flatFootAngle: 0.0, +}; + +export function serializeStayAlignedSettings( + settings: StayAlignedSettingsForm +): StayAlignedSettingsT { + const serialized = new StayAlignedSettingsT(); + serialized.enabled = settings.enabled; + serialized.extraYawCorrection = settings.extraYawCorrection; + serialized.hideYawCorrection = settings.hideYawCorrection; + serialized.standingEnabled = settings.standingEnabled; + serialized.standingUpperLegAngle = settings.standingUpperLegAngle; + serialized.standingLowerLegAngle = settings.standingLowerLegAngle; + serialized.standingFootAngle = settings.standingFootAngle; + serialized.sittingEnabled = settings.sittingEnabled; + serialized.sittingUpperLegAngle = settings.sittingUpperLegAngle; + serialized.sittingLowerLegAngle = settings.sittingLowerLegAngle; + serialized.sittingFootAngle = settings.sittingFootAngle; + serialized.flatEnabled = settings.flatEnabled; + serialized.flatUpperLegAngle = settings.flatUpperLegAngle; + serialized.flatLowerLegAngle = settings.flatLowerLegAngle; + serialized.flatFootAngle = settings.flatFootAngle; + return serialized; +} + +export function deserializeStayAlignedSettings( + serialized: StayAlignedSettingsT +): StayAlignedSettingsForm { + return serialized; +} + +export function StayAlignedSettings({ + values, + control, +}: { + values: SettingsForm; + control: Control; +}) { + const { l10n } = useLocalization(); + const { config } = useConfig(); + + return ( + } id="stayaligned"> + + {l10n.getString('settings-stay_aligned')} + +
+ + {l10n.getString('settings-stay_aligned-description')} + +
+
+ + {l10n.getString('settings-stay_aligned-general-label')} + + {values.stayAligned.enabled && values.driftCompensation.enabled && ( +
+ {l10n.getString( + 'settings-stay_aligned-warnings-drift_compensation' + )} +
+ )} +
+ + + +
+
+
+ + {l10n.getString('settings-stay_aligned-relaxed_poses-label')} + +
+ {config?.debug ? ( + + ) : ( + + )} +
+
+
+ ); +} diff --git a/gui/src/components/stay-aligned/RelaxedPose.tsx b/gui/src/components/stay-aligned/RelaxedPose.tsx new file mode 100644 index 000000000..71750934d --- /dev/null +++ b/gui/src/components/stay-aligned/RelaxedPose.tsx @@ -0,0 +1,439 @@ +import { useLocaleConfig } from '@/i18n/config'; +import { ReactLocalization, useLocalization } from '@fluent/react'; +import { + DetectStayAlignedRelaxedPoseRequestT, + RpcMessage, + StayAlignedPoseT, + StayAlignedRelaxedPose, +} from 'solarxr-protocol'; +import { Typography } from '@/components/commons/Typography'; +import { SettingsForm } from '@/components/settings/pages/GeneralSettings'; +import { Control } from 'react-hook-form'; +import { NumberSelector } from '@/components/commons/NumberSelector'; +import { useWebsocketAPI } from '@/hooks/websocket-api'; +import { MouseEventHandler } from 'react'; +import { Button } from '@/components/commons/Button'; +import { CheckBox } from '@/components/commons/Checkbox'; +import { useAtomValue } from 'jotai'; +import { stayAlignedPoseAtom } from '@/store/app-store'; + +/** + * Creates a pose angle formatter that formats an positive angle as "$angle + * outwards" and a negative angle as "$angle inwards". Useful for describing + * leg angles in relaxed poses. + */ +function PoseAngleFormat(l10n: ReactLocalization, currentLocales: string[]) { + const degreeFormat = new Intl.NumberFormat(currentLocales, { + style: 'unit', + unit: 'degree', + maximumFractionDigits: 0, + }); + + return { + format: (angle: number) => { + angle = Math.round(angle); + const angleStr = degreeFormat.format(Math.abs(angle)); + if (angle >= 1) { + return l10n.getString('settings-stay_aligned-relaxed_poses-outwards', { + angle: angleStr, + }); + } else if (angle <= -1) { + return l10n.getString('settings-stay_aligned-relaxed_poses-inwards', { + angle: angleStr, + }); + } else { + return angleStr; + } + }, + }; +} + +function relaxedPoseKey(pose: StayAlignedRelaxedPose) { + switch (pose) { + case StayAlignedRelaxedPose.STANDING: + return 'settings-stay_aligned-relaxed_poses-standing'; + case StayAlignedRelaxedPose.SITTING: + return 'settings-stay_aligned-relaxed_poses-sitting'; + case StayAlignedRelaxedPose.FLAT: + return 'settings-stay_aligned-relaxed_poses-flat'; + } +} + +/** + * Read-only view of a relaxed pose. + */ +export function RelaxedPose({ + pose, + enabled, + upperLegAngleInDeg, + lowerLegAngleInDeg, + footAngleInDeg, +}: { + pose: StayAlignedRelaxedPose; + enabled: boolean; + upperLegAngleInDeg: number; + lowerLegAngleInDeg: number; + footAngleInDeg: number; +}) { + const { l10n } = useLocalization(); + const { currentLocales } = useLocaleConfig(); + const angleFormat = PoseAngleFormat(l10n, currentLocales); + + if (enabled) { + return ( +
+ + {l10n.getString(relaxedPoseKey(pose))} + + + {l10n.getString( + 'settings-stay_aligned-relaxed_poses-upper_leg_angle' + )} + : {angleFormat.format(upperLegAngleInDeg)} + + + {l10n.getString( + 'settings-stay_aligned-relaxed_poses-lower_leg_angle' + )} + : {angleFormat.format(lowerLegAngleInDeg)} + + + {l10n.getString('settings-stay_aligned-relaxed_poses-foot_angle')}:{' '} + {angleFormat.format(footAngleInDeg)} + +
+ ); + } else { + return ( +
+ + {l10n.getString(relaxedPoseKey(pose))} + + + {l10n.getString('settings-stay_aligned-relaxed_poses-disabled')} + +
+ ); + } +} + +/** + * Read-only view of the current pose's relaxed angles. + */ +export function CurrentRelaxedPose() { + const { l10n } = useLocalization(); + const { currentLocales } = useLocaleConfig(); + const angleFormat = PoseAngleFormat(l10n, currentLocales); + + const stayAlignedPose = + useAtomValue(stayAlignedPoseAtom) || new StayAlignedPoseT(0.0, 0.0, 0.0); + + return ( +
+ + {l10n.getString('settings-stay_aligned-relaxed_poses-current_angles')} + + + {l10n.getString('settings-stay_aligned-relaxed_poses-upper_leg_angle')}:{' '} + {angleFormat.format(stayAlignedPose.upperLegAngleInDeg)} + + + {l10n.getString('settings-stay_aligned-relaxed_poses-lower_leg_angle')}:{' '} + {angleFormat.format(stayAlignedPose.lowerLegAngleInDeg)} + + + {l10n.getString('settings-stay_aligned-relaxed_poses-foot_angle')}:{' '} + {angleFormat.format(stayAlignedPose.footAngleInDeg)} + +
+ ); +} + +/** + * Read-only view of all the relaxed poses, and the current pose's angles. + */ +export function RelaxedPosesSummary({ values }: { values: SettingsForm }) { + return ( +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ ); +} + +/** + * Tells the server to set a relaxed pose to the current pose's angles. + */ +export function DetectRelaxedPoseButton({ + pose, + onClick, +}: { + pose: StayAlignedRelaxedPose; + onClick?: MouseEventHandler; +}) { + const { sendRPCPacket } = useWebsocketAPI(); + const { l10n } = useLocalization(); + + return ( + + ); +} + +/** + * Tells the server to reset the angles in a relaxed pose. + */ +export function ResetRelaxedPoseButton({ + pose, + onClick, + children, +}: { + pose: StayAlignedRelaxedPose; + onClick?: MouseEventHandler; +} & React.PropsWithChildren) { + const { sendRPCPacket } = useWebsocketAPI(); + const { l10n } = useLocalization(); + + return ( + + ); +} + +/** + * Control to edit the angles of a pose. + */ +function RelaxedPoseControl({ + pose, + enabledKey, + upperLegSettingsKey, + lowerLegSettingsKey, + footSettingsKey, + control, +}: { + pose: StayAlignedRelaxedPose; + enabledKey: string; + upperLegSettingsKey: string; + lowerLegSettingsKey: string; + footSettingsKey: string; + control: Control; +}) { + const { l10n } = useLocalization(); + const { currentLocales } = useLocaleConfig(); + const angleFormat = PoseAngleFormat(l10n, currentLocales); + + return ( +
+ + {l10n.getString(relaxedPoseKey(pose))} + + + `${l10n.getString( + 'settings-stay_aligned-relaxed_poses-upper_leg_angle' + )}: ${angleFormat.format(value)}` + } + min={-90.0} + max={90.0} + step={1.0} + /> + + `${l10n.getString( + 'settings-stay_aligned-relaxed_poses-lower_leg_angle' + )}: ${angleFormat.format(value)}` + } + min={-90.0} + max={90.0} + step={1.0} + /> + + `${l10n.getString( + 'settings-stay_aligned-relaxed_poses-foot_angle' + )}: ${angleFormat.format(value)}` + } + min={-90.0} + max={90.0} + step={1.0} + /> + + + +
+ ); +} + +/** + * Control that displays the current pose's relaxed angles, in a similar layout + * to . + */ +function CurrentRelaxedPoseControl({ + control, +}: { + control: Control; +}) { + const { l10n } = useLocalization(); + const { currentLocales } = useLocaleConfig(); + const angleFormat = PoseAngleFormat(l10n, currentLocales); + + const stayAlignedPose = + useAtomValue(stayAlignedPoseAtom) || new StayAlignedPoseT(0.0, 0.0, 0.0); + + return ( +
+ + {l10n.getString('settings-stay_aligned-relaxed_poses-current_angles')} + + + `${l10n.getString( + 'settings-stay_aligned-relaxed_poses-upper_leg_angle' + )}: ${angleFormat.format(stayAlignedPose.upperLegAngleInDeg || 0.0)}` + } + min={-90.0} + max={90.0} + step={1.0} + /> + + `${l10n.getString( + 'settings-stay_aligned-relaxed_poses-lower_leg_angle' + )}: ${angleFormat.format(stayAlignedPose.lowerLegAngleInDeg || 0.0)}` + } + min={-90.0} + max={90.0} + step={1.0} + /> + + `${l10n.getString( + 'settings-stay_aligned-relaxed_poses-foot_angle' + )}: ${angleFormat.format(stayAlignedPose.footAngleInDeg || 0.0)}` + } + min={-90.0} + max={90.0} + step={1.0} + /> +
+ ); +} + +/** + * Control to edit the angles of all the relaxed poses. + */ +export function RelaxedPosesSettings({ + control, +}: { + control: Control; +}) { + return ( +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ ); +} diff --git a/gui/src/components/stay-aligned/StayAlignedInfo.tsx b/gui/src/components/stay-aligned/StayAlignedInfo.tsx new file mode 100644 index 000000000..447fe1f39 --- /dev/null +++ b/gui/src/components/stay-aligned/StayAlignedInfo.tsx @@ -0,0 +1,53 @@ +import { Typography } from '@/components/commons/Typography'; +import { useLocaleConfig } from '@/i18n/config'; +import { angleIsNearZero } from '@/maths/angle'; +import { TrackerDataT } from 'solarxr-protocol'; + +export function StayAlignedInfo({ + color, + tracker, +}: { + color: 'primary' | 'secondary'; + tracker: TrackerDataT; +}) { + const { currentLocales } = useLocaleConfig(); + const degreeFormat = new Intl.NumberFormat(currentLocales, { + style: 'unit', + unit: 'degree', + minimumFractionDigits: 1, + maximumFractionDigits: 1, + }); + const errorFormat = new Intl.NumberFormat(currentLocales, { + minimumFractionDigits: 1, + maximumFractionDigits: 1, + }); + + const stayAligned = tracker.stayAligned; + if (!stayAligned) { + return <>; + } + + const locked = stayAligned.locked ? '🔒' : ''; + + const delta = `Δ=${degreeFormat.format(stayAligned.yawCorrectionInDeg)}`; + + const errors = []; + const maxErrorToShow = 0.1; + if (!angleIsNearZero(stayAligned.lockedErrorInDeg, maxErrorToShow)) { + errors.push(`L=${errorFormat.format(stayAligned.lockedErrorInDeg)}`); + } + if (!angleIsNearZero(stayAligned.centerErrorInDeg, maxErrorToShow)) { + errors.push(`C=${errorFormat.format(stayAligned.centerErrorInDeg)}`); + } + if (!angleIsNearZero(stayAligned.neighborErrorInDeg, maxErrorToShow)) { + errors.push(`N=${errorFormat.format(stayAligned.neighborErrorInDeg)}`); + } + + const error = errors.length > 0 ? `(${errors.join(', ')})` : ''; + + return ( + + {locked} {delta} {error} + + ); +} diff --git a/gui/src/components/tracker/TrackersTable.tsx b/gui/src/components/tracker/TrackersTable.tsx index 8ac6c382a..78064204b 100644 --- a/gui/src/components/tracker/TrackersTable.tsx +++ b/gui/src/components/tracker/TrackersTable.tsx @@ -17,6 +17,7 @@ import { TrackerStatus } from './TrackerStatus'; import { TrackerWifi } from './TrackerWifi'; import { trackerStatusRelated, useStatusContext } from '@/hooks/status-system'; import { FlatDeviceTracker } from '@/store/app-store'; +import { StayAlignedInfo } from '@/components/stay-aligned/StayAlignedInfo'; enum DisplayColumn { NAME, @@ -28,6 +29,7 @@ enum DisplayColumn { TEMPERATURE, LINEAR_ACCELERATION, POSITION, + STAY_ALIGNED, URL, } @@ -41,6 +43,7 @@ const displayColumns: { [k: string]: boolean } = { [DisplayColumn.TEMPERATURE]: true, [DisplayColumn.LINEAR_ACCELERATION]: true, [DisplayColumn.POSITION]: true, + [DisplayColumn.STAY_ALIGNED]: true, [DisplayColumn.URL]: true, }; @@ -196,6 +199,7 @@ export function TrackersTable({ displayColumns[DisplayColumn.TEMPERATURE] = hasTemperature || false; displayColumns[DisplayColumn.POSITION] = moreInfo || false; displayColumns[DisplayColumn.LINEAR_ACCELERATION] = moreInfo || false; + displayColumns[DisplayColumn.STAY_ALIGNED] = moreInfo || false; displayColumns[DisplayColumn.URL] = moreInfo || false; const displayColumnsKeys = Object.keys(displayColumns).filter( (k) => displayColumns[k] @@ -362,6 +366,15 @@ export function TrackersTable({ ), })} + {column({ + id: DisplayColumn.STAY_ALIGNED, + label: l10n.getString('tracker-table-column-stay_aligned'), + labelClassName: 'w-36', + row: ({ tracker }) => ( + + ), + })} + {column({ id: DisplayColumn.URL, label: l10n.getString('tracker-table-column-url'), diff --git a/gui/src/components/widgets/IMUVisualizerWidget.tsx b/gui/src/components/widgets/IMUVisualizerWidget.tsx index fc55815b6..b753da8f6 100644 --- a/gui/src/components/widgets/IMUVisualizerWidget.tsx +++ b/gui/src/components/widgets/IMUVisualizerWidget.tsx @@ -12,6 +12,7 @@ import { useLocalization } from '@fluent/react'; import { Vector3Object, Vector3FromVec3fT } from '@/maths/vector3'; import { Gltf } from '@react-three/drei'; import { ErrorBoundary } from 'react-error-boundary'; +import { StayAlignedInfo } from '@/components/stay-aligned/StayAlignedInfo'; const groundColor = '#4444aa'; @@ -168,6 +169,15 @@ export function IMUVisualizerWidget({ tracker }: { tracker: TrackerDataT }) {
)} + {!!tracker.stayAligned && ( +
+ + {l10n.getString('widget-imu_visualizer-stay_aligned')} + + +
+ )} + {!enabled && (