| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package safety
- import (
- "fmt"
- "math"
- "hth/internal/config"
- "hth/internal/ids"
- "hth/internal/profile"
- "hth/internal/protocol"
- )
- // EffectiveContract is the unique merged safety policy used by runtime
- // SET_ALL, Capabilities, Doctor, Mock, and Replay.
- type EffectiveContract struct {
- Frame protocol.SetAll
- Fan int
- PTC int
- Door int
- ByID map[string]float64
- }
- func BuildEffectiveSafetyContract(p *profile.Profile, ov config.Safety) (EffectiveContract, error) {
- var out EffectiveContract
- if p == nil {
- return out, fmt.Errorf("effective safety: nil profile")
- }
- fan := p.Safety.Fan
- ptc := p.Safety.PTC
- door := p.Safety.DoorClosePercent
- if ov.Fan != nil {
- fan = *ov.Fan
- }
- if ov.PTC != nil {
- ptc = *ov.PTC
- }
- if ov.DoorClosePercent != nil {
- door = *ov.DoorClosePercent
- }
- if err := checkSafetyInt("fan", fan); err != nil {
- return out, err
- }
- if err := checkSafetyInt("ptc", ptc); err != nil {
- return out, err
- }
- if err := checkSafetyInt("door_close_percent", door); err != nil {
- return out, err
- }
- out.Fan, out.PTC, out.Door = fan, ptc, door
- out.ByID = map[string]float64{}
- assign := func(id string, v float64) error {
- a := p.LookupActuator(id)
- if a == nil {
- return fmt.Errorf("effective safety: unknown actuator %s", id)
- }
- if !isFinite(v) {
- return fmt.Errorf("effective safety: %s not finite", id)
- }
- if a.Resolution <= 0 || !isFinite(a.Resolution) {
- return fmt.Errorf("effective safety: %s resolution must be > 0", id)
- }
- if v < a.CommandRange.Min || v > a.CommandRange.Max {
- return fmt.Errorf("effective safety: %s value %v out of [%v,%v]", id, v, a.CommandRange.Min, a.CommandRange.Max)
- }
- steps := v / a.Resolution
- if math.Abs(steps-math.Round(steps)) > 1e-9 {
- return fmt.Errorf("effective safety: %s value %v is not an integer number of resolution %v", id, v, a.Resolution)
- }
- if v < 0 || v > 255 || math.Floor(v) != v {
- return fmt.Errorf("effective safety: %s cannot encode into SET_ALL byte (%v)", id, v)
- }
- out.ByID[id] = v
- return nil
- }
- for _, id := range []string{ids.Fan1, ids.Fan2, ids.Fan3, ids.Fan4} {
- if err := assign(id, float64(fan)); err != nil {
- return out, err
- }
- }
- for _, id := range []string{ids.PTC1, ids.PTC2, ids.PTC3, ids.PTC4} {
- if err := assign(id, float64(ptc)); err != nil {
- return out, err
- }
- }
- for _, id := range []string{ids.DoorX, ids.DoorY, ids.DoorZ} {
- if err := assign(id, float64(door)); err != nil {
- return out, err
- }
- }
- frame := protocol.SetAll{}
- for i := range frame.Fan {
- frame.Fan[i] = uint8(fan)
- }
- for i := range frame.PTC {
- frame.PTC[i] = uint8(ptc)
- }
- frame.DoorX, frame.DoorY, frame.DoorZ = uint8(door), uint8(door), uint8(door)
- if _, err := frame.Payload(); err != nil {
- return out, fmt.Errorf("effective safety: SET_ALL not encodable: %w", err)
- }
- out.Frame = frame
- return out, nil
- }
- func checkSafetyInt(name string, v int) error {
- if v < 0 || v > 100 {
- return fmt.Errorf("effective safety: %s %d out of 0..100", name, v)
- }
- return nil
- }
- func isFinite(v float64) bool {
- return !math.IsNaN(v) && !math.IsInf(v, 0)
- }
|