31#include "model/autopilot_output.h"
32#include "model/comm_drv_n2k_serial.h"
34#include "model/comm_n0183_output.h"
35#include "model/comm_vars.h"
36#include "model/config_vars.h"
37#include "model/georef.h"
39#include "model/nmea_ctx_factory.h"
41#include "model/own_ship.h"
42#include "model/routeman.h"
44#include "N2kMessages.h"
49#include "androidUTIL.h"
53 auto w = wxWindow::FindWindowByName(kDataMonitorWindowName);
54 auto log =
dynamic_cast<NmeaLog *
>(w);
59static void SendRmc(NMEA0183 &nmea0183,
Routeman &routeman) {
61 nmea0183.Rmc.IsDataValid = bGPSValid ? NTrue : NFalse;
63 nmea0183.Rmc.Position.Latitude.Set(std::fabs(gLat), gLat < 0 ?
"S" :
"N");
64 nmea0183.Rmc.Position.Longitude.Set(std::fabs(gLon), gLon < 0 ?
"W" :
"E");
66 nmea0183.Rmc.SpeedOverGroundKnots = std::isnan(gSog) ? 0.0 : gSog;
67 nmea0183.Rmc.TrackMadeGoodDegreesTrue = std::isnan(gCog) ? 0.0 : gCog;
69 if (!std::isnan(gVar)) {
70 nmea0183.Rmc.MagneticVariation = std::fabs(gVar);
71 nmea0183.Rmc.MagneticVariationDirection = gVar < 0 ? West : East;
74 nmea0183.Rmc.MagneticVariation = 361.;
78 if (!gRmcTime.IsEmpty() && !gRmcDate.IsEmpty()) {
79 nmea0183.Rmc.UTCTime = gRmcTime;
80 nmea0183.Rmc.Date = gRmcDate;
82 wxDateTime now = wxDateTime::Now();
83 wxDateTime utc = now.ToUTC();
84 wxString time = utc.Format(_T(
"%H%M%S"));
85 nmea0183.Rmc.UTCTime = time;
86 wxString date = utc.Format(_T(
"%d%m%y"));
87 nmea0183.Rmc.Date = date;
90 nmea0183.Rmc.FAAModeIndicator =
"A";
91 if (!bGPSValid) nmea0183.Rmc.FAAModeIndicator =
"N";
93 nmea0183.Rmc.Write(snt);
95 BroadcastNMEA0183Message(snt.Sentence, GetNmeaLog(),
96 routeman.GetMessageSentEventVar());
99static void SendDummyRmb(NMEA0183 &nmea0183,
Routeman &routeman) {
100 nmea0183.Rmb.IsDataValid = NTrue;
101 nmea0183.Rmb.CrossTrackError = 0;
102 nmea0183.Rmb.DirectionToSteer = Left;
103 nmea0183.Rmb.RangeToDestinationNauticalMiles = 0;
104 nmea0183.Rmb.BearingToDestinationDegreesTrue = 0;
105 nmea0183.Rmb.DestinationPosition.Latitude.Set(0,
"N");
106 nmea0183.Rmb.DestinationPosition.Longitude.Set(0,
"E");
107 nmea0183.Rmb.DestinationClosingVelocityKnots = 0;
108 nmea0183.Rmb.IsArrivalCircleEntered = NFalse;
109 nmea0183.Rmb.FAAModeIndicator =
"A";
110 nmea0183.Rmb.To =
"";
111 nmea0183.Rmb.From =
"";
114 nmea0183.Rmb.Write(snt);
115 BroadcastNMEA0183Message(snt.Sentence, GetNmeaLog(),
116 routeman.GetMessageSentEventVar());
119static void SendRmb(NMEA0183 &nmea0183,
Routeman &routeman) {
121 RoutePoint *pActivePoint = routeman.GetpActivePoint();
122 const int maxName = 6;
124 nmea0183.Rmb.IsDataValid = bGPSValid ? NTrue : NFalse;
125 nmea0183.Rmb.CrossTrackError = routeman.GetCurrentXTEToActivePoint();
126 nmea0183.Rmb.DirectionToSteer = routeman.GetXTEDir() < 0 ? Left : Right;
127 nmea0183.Rmb.RangeToDestinationNauticalMiles =
128 routeman.GetCurrentRngToActivePoint();
129 nmea0183.Rmb.BearingToDestinationDegreesTrue =
130 routeman.GetCurrentBrgToActivePoint();
133 const char *ns = pActivePoint->m_lat < 0 ?
"S" :
"N";
134 nmea0183.Rmb.DestinationPosition.Latitude.Set(fabs(pActivePoint->m_lat), ns);
135 const char *ew = pActivePoint->m_lon < 0 ?
"W" :
"E";
136 nmea0183.Rmb.DestinationPosition.Longitude.Set(fabs(pActivePoint->m_lon), ew);
138 double sog = std::isnan(gSog) ? 0.0 : gSog;
139 double cog = std::isnan(gCog) ? 0.0 : gCog;
140 nmea0183.Rmb.DestinationClosingVelocityKnots =
141 sog * cos((cog - routeman.GetCurrentBrgToActivePoint()) * PI / 180.0);
142 nmea0183.Rmb.IsArrivalCircleEntered = routeman.GetArrival() ? NTrue : NFalse;
143 nmea0183.Rmb.FAAModeIndicator = bGPSValid ?
"A" :
"N";
146 int wp_len = maxName;
148 nmea0183.Rmb.To = pActivePoint->GetName().Truncate(wp_len);
150 routeman.GetpActiveRouteSegmentBeginPoint()->GetName().Truncate(wp_len);
151 nmea0183.Rmb.Write(snt);
153 }
while (snt.Sentence.size() > 82 && wp_len > 0);
155 BroadcastNMEA0183Message(snt.Sentence, GetNmeaLog(),
156 routeman.GetMessageSentEventVar());
159bool SendNoRouteRmbRmc(
Routeman &routeman) {
160 if (routeman.GetpActivePoint())
return false;
161 NMEA0183 nmea0183 = routeman.GetNMEA0183();
162 SendRmc(nmea0183, routeman);
163 SendDummyRmb(nmea0183, routeman);
167bool UpdateAutopilotN0183(
Routeman &routeman) {
168 NMEA0183 nmea0183 = routeman.GetNMEA0183();
169 RoutePoint *pActivePoint = routeman.GetpActivePoint();
173 if ((g_maxWPNameLength >= 3) && (g_maxWPNameLength <= 32))
174 maxName = g_maxWPNameLength;
176 SendRmb(nmea0183, routeman);
177 SendRmc(nmea0183, routeman);
183 nmea0183.Apb.IsLoranBlinkOK =
185 if (!bGPSValid) nmea0183.Apb.IsLoranBlinkOK = NFalse;
187 nmea0183.Apb.IsLoranCCycleLockOK = NTrue;
188 if (!bGPSValid) nmea0183.Apb.IsLoranCCycleLockOK = NFalse;
190 nmea0183.Apb.CrossTrackErrorMagnitude =
191 routeman.GetCurrentXTEToActivePoint();
193 if (routeman.GetXTEDir() < 0)
194 nmea0183.Apb.DirectionToSteer = Left;
196 nmea0183.Apb.DirectionToSteer = Right;
198 nmea0183.Apb.CrossTrackUnits = _T(
"N");
200 if (routeman.GetArrival())
201 nmea0183.Apb.IsArrivalCircleEntered = NTrue;
203 nmea0183.Apb.IsArrivalCircleEntered = NFalse;
207 nmea0183.Apb.IsPerpendicular = NFalse;
209 nmea0183.Apb.To = pActivePoint->GetName().Truncate(maxName);
212 DistanceBearingMercator(pActivePoint->m_lat, pActivePoint->m_lon,
213 routeman.GetpActiveRouteSegmentBeginPoint()->m_lat,
214 routeman.GetpActiveRouteSegmentBeginPoint()->m_lon,
217 if (g_bMagneticAPB && !std::isnan(gVar)) {
219 ((brg1 - gVar) >= 0.) ? (brg1 - gVar) : (brg1 - gVar + 360.);
220 double bapm = ((routeman.GetCurrentBrgToActivePoint() - gVar) >= 0.)
221 ? (routeman.GetCurrentBrgToActivePoint() - gVar)
222 : (routeman.GetCurrentBrgToActivePoint() - gVar + 360.);
224 nmea0183.Apb.BearingOriginToDestination = brg1m;
225 nmea0183.Apb.BearingOriginToDestinationUnits = _T(
"M");
227 nmea0183.Apb.BearingPresentPositionToDestination = bapm;
228 nmea0183.Apb.BearingPresentPositionToDestinationUnits = _T(
"M");
230 nmea0183.Apb.HeadingToSteer = bapm;
231 nmea0183.Apb.HeadingToSteerUnits = _T(
"M");
233 nmea0183.Apb.BearingOriginToDestination = brg1;
234 nmea0183.Apb.BearingOriginToDestinationUnits = _T(
"T");
236 nmea0183.Apb.BearingPresentPositionToDestination =
237 routeman.GetCurrentBrgToActivePoint();
238 nmea0183.Apb.BearingPresentPositionToDestinationUnits = _T(
"T");
240 nmea0183.Apb.HeadingToSteer = routeman.GetCurrentBrgToActivePoint();
241 nmea0183.Apb.HeadingToSteerUnits = _T(
"T");
244 nmea0183.Apb.Write(snt);
245 BroadcastNMEA0183Message(snt.Sentence, GetNmeaLog(),
246 routeman.GetMessageSentEventVar());
253 nmea0183.Xte.IsLoranBlinkOK =
255 if (!bGPSValid) nmea0183.Xte.IsLoranBlinkOK = NFalse;
257 nmea0183.Xte.IsLoranCCycleLockOK = NTrue;
258 if (!bGPSValid) nmea0183.Xte.IsLoranCCycleLockOK = NFalse;
260 nmea0183.Xte.CrossTrackErrorDistance =
261 routeman.GetCurrentXTEToActivePoint();
263 if (routeman.GetXTEDir() < 0)
264 nmea0183.Xte.DirectionToSteer = Left;
266 nmea0183.Xte.DirectionToSteer = Right;
268 nmea0183.Xte.CrossTrackUnits = _T(
"N");
270 nmea0183.Xte.Write(snt);
271 BroadcastNMEA0183Message(snt.Sentence, GetNmeaLog(),
272 routeman.GetMessageSentEventVar());
278bool UpdateAutopilotN2K(
Routeman &routeman) {
279 bool fail_any =
false;
282 auto ®istry = CommDriverRegistry::GetInstance();
283 const std::vector<DriverPtr> &drivers = registry.GetDrivers();
286 for (
auto key : routeman.GetOutpuDriverArray()) {
287 for (
auto &d : drivers) {
288 if (d->Key() == key) {
289 std::unordered_map<std::string, std::string> attributes =
291 auto protocol_it = attributes.find(
"protocol");
292 if (protocol_it != attributes.end()) {
293 std::string protocol = protocol_it->second;
295 if (protocol ==
"nmea2000") {
302 if (!found)
return false;
307 drv_serial->AddTxPGN(129283);
308 drv_serial->AddTxPGN(129284);
309 drv_serial->AddTxPGN(129285);
311 if (routeman.IsAnyRouteActive()) {
312 fail_any |= !SendPGN129285(routeman, found);
313 fail_any |= !SendPGN129284(routeman, found);
314 fail_any |= !SendPGN129283(routeman, found);
317 return (fail_any == 0);
321 bool fail_any =
false;
325 if ((g_maxWPNameLength >= 3) && (g_maxWPNameLength <= 32))
326 maxName = g_maxWPNameLength;
331 char route_name[] =
"Route";
332 SetN2kPGN129285(msg129285, 0, 0, 0, N2kdir_forward, 0, route_name);
335 RoutePoint *pLegBeginPoint = routeman.GetpActiveRouteSegmentBeginPoint();
336 wxString start_point_name = pLegBeginPoint->GetName().Truncate(maxName);
337 std::string sname = start_point_name.ToStdString();
338 char *s = (
char *)sname.c_str();
340 fail_any |= !AppendN2kPGN129285(msg129285, 0, s, pLegBeginPoint->m_lat,
341 pLegBeginPoint->m_lon);
343 RoutePoint *pActivePoint = routeman.GetpActivePoint();
344 wxString destination_name = pActivePoint->GetName().Truncate(maxName);
345 std::string dname = destination_name.ToStdString();
346 char *d = (
char *)dname.c_str();
347 fail_any |= !AppendN2kPGN129285(msg129285, 1, d, pActivePoint->m_lat,
348 pActivePoint->m_lon);
350 if (fail_any)
return false;
352 auto dest_addr = std::make_shared<const NavAddr2000>(driver->
iface, 255);
353 std::vector<uint8_t> payload;
354 for (
int i = 0; i < msg129285.DataLen; i++)
355 payload.push_back(msg129285.Data[i]);
357 std::make_shared<const Nmea2000Msg>(129285, payload, dest_addr, 6);
358 fail_any |= !driver->SendMessage(PGN129285, dest_addr);
360 return (fail_any == 0);
364 bool fail_any =
false;
366 RoutePoint *pActivePoint = routeman.GetpActivePoint();
370 if (!std::isnan(gCog) && !std::isnan(gSog)) {
371 double brg = routeman.GetCurrentBrgToActivePoint();
372 vmg = gSog * cos((brg - gCog) * PI / 180.);
374 wxTimeSpan tttg_span;
375 wxDateTime arrival_time = wxDateTime::Now();
377 double tttg_sec = (routeman.GetCurrentRngToActivePoint() / gSog) * 3600;
378 tttg_span = wxTimeSpan::Seconds((
long)tttg_sec);
379 arrival_time += tttg_span;
381 double time_days_1979 = arrival_time.GetTicks() / (3600. * 24.);
385 double eta_time_days;
386 double eta_time_seconds = modf(time_days_1979, &eta_time_days);
387 int16_t eta_time_days_16 =
static_cast<uint16_t
>(eta_time_days);
392 routeman.GetCurrentRngToActivePoint() * 1852.,
399 routeman.GetCurrentSegmentCourse() * PI /
401 routeman.GetCurrentBrgToActivePoint() * PI /
409 auto dest_addr = std::make_shared<const NavAddr2000>(driver->
iface, 255);
410 std::vector<uint8_t> payload;
411 for (
int i = 0; i < msg129284.DataLen; i++)
412 payload.push_back(msg129284.Data[i]);
414 std::make_shared<const Nmea2000Msg>(129284, payload, dest_addr, 6);
415 fail_any |= !driver->SendMessage(PGN129284, dest_addr);
417 return (fail_any == 0);
421 bool fail_any =
false;
423 RoutePoint *pActivePoint = routeman.GetpActivePoint();
425 double xte = routeman.GetCurrentXTEToActivePoint() * 1852.;
428 if (routeman.GetXTEDir() > 0) {
431 SetN2kPGN129283(msg129283, 0,
437 auto dest_addr = std::make_shared<const NavAddr2000>(driver->
iface, 255);
438 std::vector<uint8_t> payload;
439 for (
int i = 0; i < msg129283.DataLen; i++)
440 payload.push_back(msg129283.Data[i]);
442 std::make_shared<const Nmea2000Msg>(129283, payload, dest_addr, 6);
443 fail_any |= !driver->SendMessage(PGN129283, dest_addr);
445 return (fail_any == 0);
Common interface for all drivers.
const std::string iface
Physical device for 0183, else a unique string.
Represents a waypoint or mark within the navigation system.
Driver registration container, a singleton.
Hooks into gui available in model.
Basic DataMonitor logging interface: LogLine (reflects a line in the log) and NmeaLog,...
Global variables Listen()/Notify() wrapper.
const std::unordered_map< std::string, std::string > GetAttributes(DriverHandle handle)
Query a specific driver for attributes.