contract.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package safety
  2. import (
  3. "fmt"
  4. "math"
  5. "hth/internal/config"
  6. "hth/internal/ids"
  7. "hth/internal/profile"
  8. "hth/internal/protocol"
  9. )
  10. // EffectiveContract is the unique merged safety policy used by runtime
  11. // SET_ALL, Capabilities, Doctor, Mock, and Replay.
  12. type EffectiveContract struct {
  13. Frame protocol.SetAll
  14. Fan int
  15. PTC int
  16. Door int
  17. ByID map[string]float64
  18. }
  19. func BuildEffectiveSafetyContract(p *profile.Profile, ov config.Safety) (EffectiveContract, error) {
  20. var out EffectiveContract
  21. if p == nil {
  22. return out, fmt.Errorf("effective safety: nil profile")
  23. }
  24. fan := p.Safety.Fan
  25. ptc := p.Safety.PTC
  26. door := p.Safety.DoorClosePercent
  27. if ov.Fan != nil {
  28. fan = *ov.Fan
  29. }
  30. if ov.PTC != nil {
  31. ptc = *ov.PTC
  32. }
  33. if ov.DoorClosePercent != nil {
  34. door = *ov.DoorClosePercent
  35. }
  36. if err := checkSafetyInt("fan", fan); err != nil {
  37. return out, err
  38. }
  39. if err := checkSafetyInt("ptc", ptc); err != nil {
  40. return out, err
  41. }
  42. if err := checkSafetyInt("door_close_percent", door); err != nil {
  43. return out, err
  44. }
  45. out.Fan, out.PTC, out.Door = fan, ptc, door
  46. out.ByID = map[string]float64{}
  47. assign := func(id string, v float64) error {
  48. a := p.LookupActuator(id)
  49. if a == nil {
  50. return fmt.Errorf("effective safety: unknown actuator %s", id)
  51. }
  52. if !isFinite(v) {
  53. return fmt.Errorf("effective safety: %s not finite", id)
  54. }
  55. if a.Resolution <= 0 || !isFinite(a.Resolution) {
  56. return fmt.Errorf("effective safety: %s resolution must be > 0", id)
  57. }
  58. if v < a.CommandRange.Min || v > a.CommandRange.Max {
  59. return fmt.Errorf("effective safety: %s value %v out of [%v,%v]", id, v, a.CommandRange.Min, a.CommandRange.Max)
  60. }
  61. steps := v / a.Resolution
  62. if math.Abs(steps-math.Round(steps)) > 1e-9 {
  63. return fmt.Errorf("effective safety: %s value %v is not an integer number of resolution %v", id, v, a.Resolution)
  64. }
  65. if v < 0 || v > 255 || math.Floor(v) != v {
  66. return fmt.Errorf("effective safety: %s cannot encode into SET_ALL byte (%v)", id, v)
  67. }
  68. out.ByID[id] = v
  69. return nil
  70. }
  71. for _, id := range []string{ids.Fan1, ids.Fan2, ids.Fan3, ids.Fan4} {
  72. if err := assign(id, float64(fan)); err != nil {
  73. return out, err
  74. }
  75. }
  76. for _, id := range []string{ids.PTC1, ids.PTC2, ids.PTC3, ids.PTC4} {
  77. if err := assign(id, float64(ptc)); err != nil {
  78. return out, err
  79. }
  80. }
  81. for _, id := range []string{ids.DoorX, ids.DoorY, ids.DoorZ} {
  82. if err := assign(id, float64(door)); err != nil {
  83. return out, err
  84. }
  85. }
  86. frame := protocol.SetAll{}
  87. for i := range frame.Fan {
  88. frame.Fan[i] = uint8(fan)
  89. }
  90. for i := range frame.PTC {
  91. frame.PTC[i] = uint8(ptc)
  92. }
  93. frame.DoorX, frame.DoorY, frame.DoorZ = uint8(door), uint8(door), uint8(door)
  94. if _, err := frame.Payload(); err != nil {
  95. return out, fmt.Errorf("effective safety: SET_ALL not encodable: %w", err)
  96. }
  97. out.Frame = frame
  98. return out, nil
  99. }
  100. func checkSafetyInt(name string, v int) error {
  101. if v < 0 || v > 100 {
  102. return fmt.Errorf("effective safety: %s %d out of 0..100", name, v)
  103. }
  104. return nil
  105. }
  106. func isFinite(v float64) bool {
  107. return !math.IsNaN(v) && !math.IsInf(v, 0)
  108. }