A PX4 based camera pointer

servo_test.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2017 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. package pipoint
  16. import (
  17. "math"
  18. "testing"
  19. "juju.net.nz/x/pipoint/param"
  20. "juju.net.nz/x/pipoint/util"
  21. "github.com/stretchr/testify/assert"
  22. )
  23. func TestServo(t *testing.T) {
  24. p := &param.Params{}
  25. s := NewServo("pan", p)
  26. util.OverrideNow(1)
  27. assert.InDelta(t, s.sp.GetFloat64(), 0, 0.001, "Starts at zero")
  28. s.Set(-math.Pi * 0.5)
  29. s.Tick()
  30. assert.InDelta(t, s.pv.GetFloat64(), 1.1, 0.01)
  31. s.Set(+math.Pi * 0.5)
  32. s.Tick()
  33. assert.InDelta(t, s.pv.GetFloat64(), 1.9, 0.01)
  34. // Stays within limits
  35. s.Set(-math.Pi * 0.7)
  36. s.Tick()
  37. assert.InDelta(t, s.pv.GetFloat64(), 1.0, 0.01)
  38. s.Set(+math.Pi * 0.7)
  39. s.Tick()
  40. assert.InDelta(t, s.pv.GetFloat64(), 2.0, 0.01)
  41. }