mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-16 01:21:20 -04:00
auto sensor restart
This commit is contained in:
Vendored
BIN
Binary file not shown.
Vendored
BIN
Binary file not shown.
@@ -92,6 +92,10 @@ func (s *SerialAdapter) SendRaw(raw []byte) error {
|
|||||||
return s.write(raw)
|
return s.write(raw)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SerialAdapter) StartOI() error {
|
||||||
|
return s.write([]byte{128})
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SerialAdapter) SeekDock() error {
|
func (s *SerialAdapter) SeekDock() error {
|
||||||
return s.write([]byte{143})
|
return s.write([]byte{143})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,6 +52,11 @@ func (s *SerialAdapter) SendRaw(raw []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SerialAdapter) StartOI() error {
|
||||||
|
s.log.Printf("[dummy] start OI")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SerialAdapter) SeekDock() error {
|
func (s *SerialAdapter) SeekDock() error {
|
||||||
s.log.Printf("[dummy] seek dock")
|
s.log.Printf("[dummy] seek dock")
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
+70
-2
@@ -15,14 +15,14 @@ type WSClient struct {
|
|||||||
cfg *Config
|
cfg *Config
|
||||||
adapter *SerialAdapter
|
adapter *SerialAdapter
|
||||||
sensorFrames <-chan []byte
|
sensorFrames <-chan []byte
|
||||||
events <-chan RoverEvent
|
events chan RoverEvent
|
||||||
media *MediaSupervisor
|
media *MediaSupervisor
|
||||||
servo *CameraServo
|
servo *CameraServo
|
||||||
nightVision *NightVisionLight
|
nightVision *NightVisionLight
|
||||||
log *log.Logger
|
log *log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWSClient(cfg *Config, adapter *SerialAdapter, frames <-chan []byte, events <-chan RoverEvent, media *MediaSupervisor, servo *CameraServo, nightVision *NightVisionLight, logger *log.Logger) *WSClient {
|
func NewWSClient(cfg *Config, adapter *SerialAdapter, frames <-chan []byte, events chan RoverEvent, media *MediaSupervisor, servo *CameraServo, nightVision *NightVisionLight, logger *log.Logger) *WSClient {
|
||||||
return &WSClient{
|
return &WSClient{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
adapter: adapter,
|
adapter: adapter,
|
||||||
@@ -186,11 +186,64 @@ func (c *WSClient) handleServoCommand(payload *servoPayload) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *WSClient) forwardSensors(ctx context.Context, conn *websocket.Conn) {
|
func (c *WSClient) forwardSensors(ctx context.Context, conn *websocket.Conn) {
|
||||||
|
const (
|
||||||
|
sensorSilenceTimeout = 5 * time.Second
|
||||||
|
sensorRecoveryCooldown = 3 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
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 {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
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.emitEvent("sensorWatchdog.restart", map[string]any{
|
||||||
|
"idleMs": idleFor.Milliseconds(),
|
||||||
|
})
|
||||||
|
if err := c.adapter.StartOI(); err != nil {
|
||||||
|
c.log.Printf("start OI failed: %v", err)
|
||||||
|
c.emitEvent("sensorWatchdog.startOI.error", map[string]any{"error": err.Error()})
|
||||||
|
}
|
||||||
|
if err := c.ensureSensorStream(); err != nil {
|
||||||
|
c.log.Printf("sensor stream restart failed: %v", err)
|
||||||
|
c.emitEvent("sensorWatchdog.streamRestart.error", map[string]any{"error": err.Error()})
|
||||||
|
} else {
|
||||||
|
c.emitEvent("sensorWatchdog.streamRestart.ok", map[string]any{
|
||||||
|
"idleMs": idleFor.Milliseconds(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
lastRecovery = now
|
||||||
|
resetTimer()
|
||||||
case frame := <-c.sensorFrames:
|
case frame := <-c.sensorFrames:
|
||||||
|
resetTimer()
|
||||||
|
lastFrame = time.Now()
|
||||||
msg := sensorMessage{
|
msg := sensorMessage{
|
||||||
Type: "sensor",
|
Type: "sensor",
|
||||||
Timestamp: time.Now().UnixMilli(),
|
Timestamp: time.Now().UnixMilli(),
|
||||||
@@ -224,6 +277,21 @@ func (c *WSClient) forwardEvents(ctx context.Context, conn *websocket.Conn) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *WSClient) emitEvent(event string, data map[string]any) {
|
||||||
|
if c.events == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case c.events <- RoverEvent{
|
||||||
|
Type: "event",
|
||||||
|
Event: event,
|
||||||
|
Ts: time.Now().UnixMilli(),
|
||||||
|
Data: data,
|
||||||
|
}:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func writeJSON(ctx context.Context, conn *websocket.Conn, v any) error {
|
func writeJSON(ctx context.Context, conn *websocket.Conn, v any) error {
|
||||||
data, err := json.Marshal(v)
|
data, err := json.Marshal(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
# general idea
|
||||||
|
- users can choose from preset, and create UI themes
|
||||||
|
- the default theme is the current in place theme
|
||||||
|
- themes will NOT change margins, borders, padding, spacing, etc.
|
||||||
|
- should be as simple as changing the global css stuff
|
||||||
|
- themes can change colors, fonts, border radius, borders, etc
|
||||||
|
|
||||||
|
## technical stuff
|
||||||
|
- save theme info in
|
||||||
|
|
||||||
|
## UI for themeing
|
||||||
|
- allow people to tune every theme element that there is to tune
|
||||||
|
- allow people to export themes as json files, and import them
|
||||||
|
-
|
||||||
Reference in New Issue
Block a user