This commit is contained in:
legop3
2025-12-17 01:13:38 -05:00
parent 67f4f014c4
commit 7b29d741b0
9 changed files with 23 additions and 96 deletions
Vendored
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
-4
View File
@@ -92,10 +92,6 @@ func (s *SerialAdapter) SendRaw(raw []byte) error {
return s.write(raw)
}
func (s *SerialAdapter) StartOI() error {
return s.write([]byte{128})
}
func (s *SerialAdapter) SeekDock() error {
return s.write([]byte{143})
}
-5
View File
@@ -52,11 +52,6 @@ func (s *SerialAdapter) SendRaw(raw []byte) error {
return nil
}
func (s *SerialAdapter) StartOI() error {
s.log.Printf("[dummy] start OI")
return nil
}
func (s *SerialAdapter) SeekDock() error {
s.log.Printf("[dummy] seek dock")
return nil
+13 -73
View File
@@ -22,12 +22,6 @@ type WSClient struct {
log *log.Logger
}
const (
sensorSilenceTimeout = 5 * time.Second
sensorRecoveryCooldown = 5 * time.Second
sensorCommandPause = 1 * time.Millisecond
)
func NewWSClient(cfg *Config, adapter *SerialAdapter, frames <-chan []byte, events chan RoverEvent, media *MediaSupervisor, servo *CameraServo, nightVision *NightVisionLight, logger *log.Logger) *WSClient {
return &WSClient{
cfg: cfg,
@@ -132,15 +126,24 @@ func (c *WSClient) dispatch(ctx context.Context, msg *inboundMessage) error {
return c.adapter.MotorPWM(main, side, vac)
case msg.SensorStream != nil:
if msg.SensorStream.Enable {
return c.kickstartSensorStream(sensorCommandPause)
if err := c.adapter.StartSensorStream(defaultStreamPackets); err != nil {
return err
}
return c.adapter.PauseSensorStream(false)
}
return nil
return c.adapter.PauseSensorStream(true)
case msg.Raw != "" && len(msg.Raw) > 0:
buf, err := base64.StdEncoding.DecodeString(msg.Raw)
if err != nil {
return fmt.Errorf("raw decode: %w", err)
}
return c.adapter.SendRaw(buf)
if err := c.adapter.SendRaw(buf); err != nil {
return err
}
if len(buf) > 0 && isModeOpcode(buf[0]) {
return c.ensureSensorStream()
}
return nil
case msg.Media != nil:
if c.media == nil {
return fmt.Errorf("media supervisor disabled")
@@ -183,45 +186,11 @@ func (c *WSClient) handleServoCommand(payload *servoPayload) error {
}
func (c *WSClient) forwardSensors(ctx context.Context, conn *websocket.Conn) {
timer := time.NewTimer(sensorSilenceTimeout)
defer timer.Stop()
resetTimer := func() {
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
timer.Reset(sensorSilenceTimeout)
}
lastRecovery := time.Time{}
lastFrame := time.Now()
for {
select {
case <-ctx.Done():
return
case <-timer.C:
now := time.Now()
if !lastRecovery.IsZero() && now.Sub(lastRecovery) < sensorRecoveryCooldown {
resetTimer()
continue
}
idleFor := now.Sub(lastFrame)
if idleFor < 0 {
idleFor = sensorSilenceTimeout
}
c.log.Printf("no sensor frames for %v; restarting OI and sensor stream", idleFor)
c.recoverSensorStream(idleFor, sensorCommandPause)
lastRecovery = now
resetTimer()
case frame := <-c.sensorFrames:
resetTimer()
lastFrame = time.Now()
msg := sensorMessage{
Type: "sensor",
Timestamp: time.Now().UnixMilli(),
@@ -289,39 +258,10 @@ func clamp(value, min, max int) int {
}
func (c *WSClient) ensureSensorStream() error {
return c.kickstartSensorStream(sensorCommandPause)
}
func (c *WSClient) recoverSensorStream(idleFor time.Duration, cmdPause time.Duration) {
c.emitEvent("sensorWatchdog.restart", map[string]any{
"idleMs": idleFor.Milliseconds(),
})
if err := c.kickstartSensorStream(cmdPause); err != nil {
c.log.Printf("sensor stream restart failed: %v", err)
c.emitEvent("sensorWatchdog.streamRestart.error", map[string]any{"error": err.Error()})
return
}
c.emitEvent("sensorWatchdog.streamRestart.ok", map[string]any{
"idleMs": idleFor.Milliseconds(),
})
}
func (c *WSClient) kickstartSensorStream(cmdPause time.Duration) error {
if err := c.adapter.StartOI(); err != nil {
return err
}
if cmdPause > 0 {
time.Sleep(cmdPause)
}
if err := c.adapter.StartSensorStream(defaultStreamPackets); err != nil {
return err
}
if cmdPause > 0 {
time.Sleep(cmdPause)
}
return nil
return c.adapter.PauseSensorStream(false)
}
func isModeOpcode(op byte) bool {
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -11,7 +11,7 @@
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="apple-mobile-web-app-title" content="Multi Roomba Rover" />
<title>Multi Roomba Rover</title>
<script type="module" crossorigin src="/assets/index-CwvJUtN6.js"></script>
<script type="module" crossorigin src="/assets/index-BI_1Ts67.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-CsRcYKTY.css">
</head>
<body>
+3 -5
View File
@@ -105,9 +105,10 @@ export function useCommandPipeline() {
type: 'raw',
data: { raw: bytesToBase64(bytes) },
});
enableSensorStream();
return true;
},
[emitCommand, roverId],
[emitCommand, enableSensorStream, roverId],
);
const runMacroSteps = useCallback(
@@ -119,9 +120,6 @@ export function useCommandPipeline() {
case 'oi':
sendOiCommand(step.command);
break;
case 'sensorStream':
enableSensorStream();
break;
case 'drive':
sendDriveDirect(step.speeds ?? { left: 0, right: 0 });
break;
@@ -145,7 +143,7 @@ export function useCommandPipeline() {
}
}
},
[roverId, sendOiCommand, sendDriveDirect, sendAuxMotors, sendServoAngle, enableSensorStream],
[roverId, sendOiCommand, sendDriveDirect, sendAuxMotors, sendServoAngle],
);
const sendNightVision = useCallback(
+1 -3
View File
@@ -53,12 +53,10 @@ export const DEFAULT_MACROS = [
{
id: 'drive-sequence',
label: 'Drive',
description: 'Start OI, request sensors, dock poke, then full mode for driving.',
description: 'Start, dock, and full command sequence used by the drive button.',
steps: [
{ type: 'oi', command: 'start' },
{ type: 'pause', duration: COMMAND_DELAY_MS },
{ type: 'sensorStream', enable: true },
{ type: 'pause', duration: COMMAND_DELAY_MS },
{ type: 'oi', command: 'dock' },
{ type: 'pause', duration: COMMAND_DELAY_MS },
{ type: 'oi', command: 'full' },