position_vector.cpp 979 B

123456789101112131415161718192021222324252627282930
  1. #include "Sub.h"
  2. // position_vector.pde related utility functions
  3. // position vectors are Vector3f
  4. // .x = latitude from home in cm
  5. // .y = longitude from home in cm
  6. // .z = altitude above home in cm
  7. // pv_location_to_vector - convert lat/lon coordinates to a position vector
  8. Vector3f Sub::pv_location_to_vector(const Location& loc)
  9. {
  10. Location origin;
  11. if (!ahrs.get_origin(origin)) {
  12. origin.zero();
  13. }
  14. float alt_above_origin = pv_alt_above_origin(loc.alt); // convert alt-relative-to-home to alt-relative-to-origin
  15. return Vector3f((loc.lat-origin.lat) * LATLON_TO_CM, (loc.lng-origin.lng) * LATLON_TO_CM * scaleLongDown, alt_above_origin);
  16. }
  17. // pv_alt_above_origin - convert altitude above home to altitude above EKF origin
  18. float Sub::pv_alt_above_origin(float alt_above_home_cm)
  19. {
  20. Location origin;
  21. if (!ahrs.get_origin(origin)) {
  22. origin.zero();
  23. }
  24. return alt_above_home_cm + (ahrs.get_home().alt - origin.alt);
  25. }