Dev: improve robustness of CTags.cmake
[marnav.git] / src / marnav / nmea / apb.cpp
blob8190050c3c87f35bcee9ffa6a78930cb683af990
1 #include <marnav/nmea/apb.hpp>
2 #include "checks.hpp"
3 #include <marnav/nmea/io.hpp>
4 #include <stdexcept>
6 namespace marnav
8 namespace nmea
10 constexpr sentence_id apb::ID;
11 constexpr const char * apb::TAG;
13 apb::apb()
14 : sentence(ID, TAG, talker::global_positioning_system)
18 apb::apb(talker talk, fields::const_iterator first, fields::const_iterator last)
19 : sentence(ID, TAG, talk)
21 const auto size = std::distance(first, last);
22 if ((size < 14) || (size > 15))
23 throw std::invalid_argument{"invalid number of fields in apb"};
25 read(*(first + 0), loran_c_blink_warning_);
26 read(*(first + 1), loran_c_cycle_lock_warning_);
27 read(*(first + 2), cross_track_error_magnitude_);
28 read(*(first + 3), direction_to_steer_);
29 read(*(first + 4), cross_track_unit_);
30 read(*(first + 5), status_arrival_);
31 read(*(first + 6), status_perpendicular_passing_);
32 read(*(first + 7), bearing_origin_to_destination_);
33 read(*(first + 8), bearing_origin_to_destination_ref_);
34 read(*(first + 9), waypoint_id_);
35 read(*(first + 10), bearing_pos_to_destination_);
36 read(*(first + 11), bearing_pos_to_destination_ref_);
37 read(*(first + 12), heading_to_steer_to_destination_);
38 read(*(first + 13), heading_to_steer_to_destination_ref_);
40 if (size > 14)
41 read(*(first + 14), mode_ind_);
43 check();
46 void apb::set_bearing_origin_to_destination(double t, reference ref)
48 check_value(bearing_origin_to_destination_ref_, {reference::TRUE, reference::MAGNETIC},
49 "bearing_origin_to_destination_ref");
50 bearing_origin_to_destination_ = t;
51 bearing_origin_to_destination_ref_ = ref;
54 void apb::set_bearing_pos_to_destination(double t, reference ref)
56 check_value(bearing_pos_to_destination_ref_, {reference::TRUE, reference::MAGNETIC},
57 "bearing_pos_to_destination_ref");
58 bearing_pos_to_destination_ = t;
59 bearing_pos_to_destination_ref_ = ref;
62 void apb::set_heading_to_steer_to_destination(double t, reference ref)
64 check_value(heading_to_steer_to_destination_ref_, {reference::TRUE, reference::MAGNETIC},
65 "heading_to_steer_to_destination_ref");
66 heading_to_steer_to_destination_ = t;
67 heading_to_steer_to_destination_ref_ = ref;
70 void apb::set_mode_indicator(mode_indicator t)
72 check_value(t,
73 {mode_indicator::invalid, mode_indicator::autonomous, mode_indicator::differential},
74 "mode_indicator");
75 mode_ind_ = t;
78 void apb::check() const
80 check_status(loran_c_blink_warning_, "loran_c_blink_warning");
81 check_status(loran_c_cycle_lock_warning_, "loran_c_cycle_lock_warning");
83 check_value(direction_to_steer_, {side::left, side::right}, "direction_to_steer");
84 check_value(cross_track_unit_, {unit::distance::nm, unit::distance::km}, "cross_talk_unit");
85 check_status(status_arrival_, "status_arrival");
86 check_status(status_perpendicular_passing_, "status_perpendicular_passing");
88 if (bearing_origin_to_destination_ && !bearing_origin_to_destination_ref_)
89 throw std::invalid_argument{"missing bearing_origin_to_destination_ref"};
90 check_value(bearing_origin_to_destination_ref_, {reference::TRUE, reference::MAGNETIC},
91 "bearing_origin_to_destination_ref");
93 if (bearing_pos_to_destination_ && !bearing_pos_to_destination_ref_)
94 throw std::invalid_argument{"missing bearing_pos_to_destination_ref"};
95 check_value(bearing_pos_to_destination_ref_, {reference::TRUE, reference::MAGNETIC},
96 "bearing_pos_to_destination_ref");
98 if (heading_to_steer_to_destination_ && !heading_to_steer_to_destination_ref_)
99 throw std::invalid_argument{"missing heading_to_steer_to_destination_ref"};
100 check_value(heading_to_steer_to_destination_ref_, {reference::TRUE, reference::MAGNETIC},
101 "heading_to_steer_to_destination_ref");
103 check_value(mode_ind_,
104 {mode_indicator::invalid, mode_indicator::autonomous, mode_indicator::differential},
105 "mode_indicator");
108 void apb::append_data_to(std::string & s) const
110 append(s, to_string(loran_c_blink_warning_));
111 append(s, to_string(loran_c_cycle_lock_warning_));
112 append(s, format(cross_track_error_magnitude_, 2));
113 append(s, to_string(direction_to_steer_));
114 append(s, to_string(cross_track_unit_));
115 append(s, to_string(status_arrival_));
116 append(s, to_string(status_perpendicular_passing_));
117 append(s, format(bearing_origin_to_destination_, 1));
118 append(s, to_string(bearing_origin_to_destination_ref_));
119 append(s, to_string(waypoint_id_));
120 append(s, format(bearing_pos_to_destination_, 1));
121 append(s, to_string(bearing_pos_to_destination_ref_));
122 append(s, format(heading_to_steer_to_destination_, 1));
123 append(s, to_string(heading_to_steer_to_destination_ref_));
124 append(s, to_string(mode_ind_));