A PX4 based camera pointer

run.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. "fmt"
  18. "log"
  19. "math"
  20. "juju.net.nz/x/pipoint/param"
  21. "juju.net.nz/x/pipoint/util"
  22. )
  23. // RunState executes when the camera is tracking the rover.
  24. type RunState struct {
  25. pi *PiPoint
  26. }
  27. func (s *RunState) Name() string {
  28. return "Run"
  29. }
  30. // Update is called when a param is updated.
  31. func (s *RunState) Update(param *param.Param) {
  32. switch param {
  33. case s.pi.neu:
  34. s.pi.rover.Set(param.Get())
  35. case s.pi.seconds:
  36. if (param.GetInt()%5) == 0 && s.pi.vel.Ok() {
  37. kph := s.pi.vel.GetFloat64() * 3.6
  38. if kph >= 2 {
  39. s.pi.audio.Say(fmt.Sprintf("%.0f kph", kph))
  40. }
  41. }
  42. case s.pi.mark:
  43. s.pi.state.Inc()
  44. }
  45. if param != s.pi.rover {
  46. return
  47. }
  48. if !s.pi.rover.Ok() || !s.pi.base.Ok() {
  49. return
  50. }
  51. rover := s.pi.rover.Get().(*NEUPosition)
  52. base := s.pi.base.Get().(*NEUPosition)
  53. baseOffset := s.pi.baseOffset.Get().(*NEUPosition)
  54. att, err := point(rover, base, baseOffset)
  55. if err != nil {
  56. log.Printf("point: %v\n", err)
  57. return
  58. }
  59. offset := s.pi.offset.Get().(*Attitude)
  60. s.pi.pan.Set(util.WrapAngle(att.Yaw + offset.Yaw))
  61. s.pi.tilt.Set(util.WrapAngle(att.Pitch + offset.Pitch))
  62. }
  63. func point(rover, base, offset *NEUPosition) (*Attitude, error) {
  64. delta := rover.Sub(base.Add(offset))
  65. if math.Abs(delta.North) > 10e3 || math.Abs(delta.East) > 10e3 {
  66. return nil, fmt.Errorf("Rover is too far away")
  67. }
  68. hdist := math.Sqrt(delta.North*delta.North + delta.East*delta.East)
  69. pitch := math.Atan2(delta.Up, hdist)
  70. yaw := math.Atan2(delta.East, delta.North)
  71. return &Attitude{
  72. Pitch: pitch,
  73. Yaw: yaw,
  74. }, nil
  75. }