43#include "N2kMessages.h"
48#include "androidUTIL.h"
52 auto w = wxWindow::FindWindowByName(kDataMonitorWindowName);
53 auto log =
dynamic_cast<NmeaLog *
>(w);
58static void SendRmc(NMEA0183 &nmea0183,
Routeman &routeman) {
60 nmea0183.Rmc.IsDataValid =
bGPSValid ? NTrue : NFalse;
62 nmea0183.Rmc.Position.Latitude.Set(std::fabs(
gLat),
gLat < 0 ?
"S" :
"N");
63 nmea0183.Rmc.Position.Longitude.Set(std::fabs(
gLon),
gLon < 0 ?
"W" :
"E");
65 nmea0183.Rmc.SpeedOverGroundKnots = std::isnan(
gSog) ? 0.0 :
gSog;
66 nmea0183.Rmc.TrackMadeGoodDegreesTrue = std::isnan(
gCog) ? 0.0 :
gCog;
68 if (!std::isnan(
gVar)) {
69 nmea0183.Rmc.MagneticVariation = std::fabs(
gVar);
70 nmea0183.Rmc.MagneticVariationDirection =
gVar < 0 ? West : East;
73 nmea0183.Rmc.MagneticVariation = 361.;
77 if (!gRmcTime.IsEmpty() && !gRmcDate.IsEmpty()) {
78 nmea0183.Rmc.UTCTime = gRmcTime;
79 nmea0183.Rmc.Date = gRmcDate;
81 wxDateTime now = wxDateTime::Now();
82 wxDateTime utc = now.ToUTC();
83 wxString time = utc.Format(
"%H%M%S");
84 nmea0183.Rmc.UTCTime = time;
85 wxString date = utc.Format(
"%d%m%y");
86 nmea0183.Rmc.Date = date;
89 nmea0183.Rmc.FAAModeIndicator =
"A";
90 if (!
bGPSValid) nmea0183.Rmc.FAAModeIndicator =
"N";
92 nmea0183.Rmc.Write(snt);
94 BroadcastNMEA0183Message(snt.Sentence, GetNmeaLog(),
95 routeman.GetMessageSentEventVar());
98static void SendDummyRmb(NMEA0183 &nmea0183,
Routeman &routeman) {
99 nmea0183.Rmb.IsDataValid = NTrue;
100 nmea0183.Rmb.CrossTrackError = 0;
101 nmea0183.Rmb.DirectionToSteer = Left;
102 nmea0183.Rmb.RangeToDestinationNauticalMiles = 0;
103 nmea0183.Rmb.BearingToDestinationDegreesTrue = 0;
104 nmea0183.Rmb.DestinationPosition.Latitude.Set(0,
"N");
105 nmea0183.Rmb.DestinationPosition.Longitude.Set(0,
"E");
106 nmea0183.Rmb.DestinationClosingVelocityKnots = 0;
107 nmea0183.Rmb.IsArrivalCircleEntered = NFalse;
108 nmea0183.Rmb.FAAModeIndicator =
"A";
109 nmea0183.Rmb.To =
"";
110 nmea0183.Rmb.From =
"";
113 nmea0183.Rmb.Write(snt);
114 BroadcastNMEA0183Message(snt.Sentence, GetNmeaLog(),
115 routeman.GetMessageSentEventVar());
118static void SendRmb(NMEA0183 &nmea0183,
Routeman &routeman) {
120 RoutePoint *pActivePoint = routeman.GetpActivePoint();
121 const int maxName = 6;
123 nmea0183.Rmb.IsDataValid =
bGPSValid ? NTrue : NFalse;
124 nmea0183.Rmb.CrossTrackError = routeman.GetCurrentXTEToActivePoint();
125 nmea0183.Rmb.DirectionToSteer = routeman.GetXTEDir() < 0 ? Left : Right;
126 nmea0183.Rmb.RangeToDestinationNauticalMiles =
127 routeman.GetCurrentRngToActivePoint();
128 nmea0183.Rmb.BearingToDestinationDegreesTrue =
129 routeman.GetCurrentBrgToActivePoint();
132 const char *ns = pActivePoint->m_lat < 0 ?
"S" :
"N";
133 nmea0183.Rmb.DestinationPosition.Latitude.Set(fabs(pActivePoint->m_lat), ns);
134 const char *ew = pActivePoint->m_lon < 0 ?
"W" :
"E";
135 nmea0183.Rmb.DestinationPosition.Longitude.Set(fabs(pActivePoint->m_lon), ew);
137 double sog = std::isnan(
gSog) ? 0.0 :
gSog;
138 double cog = std::isnan(
gCog) ? 0.0 :
gCog;
139 nmea0183.Rmb.DestinationClosingVelocityKnots =
140 sog * cos((cog - routeman.GetCurrentBrgToActivePoint()) * PI / 180.0);
141 nmea0183.Rmb.IsArrivalCircleEntered = routeman.GetArrival() ? NTrue : NFalse;
142 nmea0183.Rmb.FAAModeIndicator =
bGPSValid ?
"A" :
"N";
145 int wp_len = maxName;
147 nmea0183.Rmb.To = pActivePoint->GetName().Truncate(wp_len);
149 routeman.GetpActiveRouteSegmentBeginPoint()->GetName().Truncate(wp_len);
150 nmea0183.Rmb.Write(snt);
152 }
while (snt.Sentence.size() > 82 && wp_len > 0);
154 BroadcastNMEA0183Message(snt.Sentence, GetNmeaLog(),
155 routeman.GetMessageSentEventVar());
159 if (routeman.GetpActivePoint())
return false;
160 NMEA0183 nmea0183 = routeman.GetNMEA0183();
161 SendRmc(nmea0183, routeman);
162 SendDummyRmb(nmea0183, routeman);
166bool UpdateAutopilotN0183(
Routeman &routeman) {
167 NMEA0183 nmea0183 = routeman.GetNMEA0183();
168 RoutePoint *pActivePoint = routeman.GetpActivePoint();
172 if ((g_maxWPNameLength >= 3) && (g_maxWPNameLength <= 32))
173 maxName = g_maxWPNameLength;
175 SendRmb(nmea0183, routeman);
176 SendRmc(nmea0183, routeman);
182 nmea0183.Apb.IsLoranBlinkOK =
184 if (!
bGPSValid) nmea0183.Apb.IsLoranBlinkOK = NFalse;
186 nmea0183.Apb.IsLoranCCycleLockOK = NTrue;
187 if (!
bGPSValid) nmea0183.Apb.IsLoranCCycleLockOK = NFalse;
189 nmea0183.Apb.CrossTrackErrorMagnitude =
190 routeman.GetCurrentXTEToActivePoint();
192 if (routeman.GetXTEDir() < 0)
193 nmea0183.Apb.DirectionToSteer = Left;
195 nmea0183.Apb.DirectionToSteer = Right;
197 nmea0183.Apb.CrossTrackUnits =
"N";
199 if (routeman.GetArrival())
200 nmea0183.Apb.IsArrivalCircleEntered = NTrue;
202 nmea0183.Apb.IsArrivalCircleEntered = NFalse;
206 nmea0183.Apb.IsPerpendicular = NFalse;
208 nmea0183.Apb.To = pActivePoint->GetName().Truncate(maxName);
211 DistanceBearingMercator(pActivePoint->m_lat, pActivePoint->m_lon,
212 routeman.GetpActiveRouteSegmentBeginPoint()->m_lat,
213 routeman.GetpActiveRouteSegmentBeginPoint()->m_lon,
216 if (g_bMagneticAPB && !std::isnan(
gVar)) {
218 ((brg1 -
gVar) >= 0.) ? (brg1 -
gVar) : (brg1 -
gVar + 360.);
219 double bapm = ((routeman.GetCurrentBrgToActivePoint() -
gVar) >= 0.)
220 ? (routeman.GetCurrentBrgToActivePoint() -
gVar)
221 : (routeman.GetCurrentBrgToActivePoint() -
gVar + 360.);
223 nmea0183.Apb.BearingOriginToDestination = brg1m;
224 nmea0183.Apb.BearingOriginToDestinationUnits =
"M";
226 nmea0183.Apb.BearingPresentPositionToDestination = bapm;
227 nmea0183.Apb.BearingPresentPositionToDestinationUnits =
"M";
229 nmea0183.Apb.HeadingToSteer = bapm;
230 nmea0183.Apb.HeadingToSteerUnits =
"M";
232 nmea0183.Apb.BearingOriginToDestination = brg1;
233 nmea0183.Apb.BearingOriginToDestinationUnits =
"T";
235 nmea0183.Apb.BearingPresentPositionToDestination =
236 routeman.GetCurrentBrgToActivePoint();
237 nmea0183.Apb.BearingPresentPositionToDestinationUnits =
"T";
239 nmea0183.Apb.HeadingToSteer = routeman.GetCurrentBrgToActivePoint();
240 nmea0183.Apb.HeadingToSteerUnits =
"T";
243 nmea0183.Apb.Write(snt);
244 BroadcastNMEA0183Message(snt.Sentence, GetNmeaLog(),
245 routeman.GetMessageSentEventVar());
252 nmea0183.Xte.IsLoranBlinkOK =
254 if (!
bGPSValid) nmea0183.Xte.IsLoranBlinkOK = NFalse;
256 nmea0183.Xte.IsLoranCCycleLockOK = NTrue;
257 if (!
bGPSValid) nmea0183.Xte.IsLoranCCycleLockOK = NFalse;
259 nmea0183.Xte.CrossTrackErrorDistance =
260 routeman.GetCurrentXTEToActivePoint();
262 if (routeman.GetXTEDir() < 0)
263 nmea0183.Xte.DirectionToSteer = Left;
265 nmea0183.Xte.DirectionToSteer = Right;
267 nmea0183.Xte.CrossTrackUnits =
"N";
269 nmea0183.Xte.Write(snt);
270 BroadcastNMEA0183Message(snt.Sentence, GetNmeaLog(),
271 routeman.GetMessageSentEventVar());
277bool UpdateAutopilotN2K(
Routeman &routeman) {
278 bool fail_any =
false;
281 auto ®istry = CommDriverRegistry::GetInstance();
282 const std::vector<DriverPtr> &drivers = registry.GetDrivers();
285 for (
auto key : routeman.GetOutpuDriverArray()) {
286 for (
auto &d : drivers) {
287 if (d->Key() == key) {
288 std::unordered_map<std::string, std::string> attributes =
290 auto protocol_it = attributes.find(
"protocol");
291 if (protocol_it != attributes.end()) {
292 std::string protocol = protocol_it->second;
294 if (protocol ==
"nmea2000") {
301 if (!found)
return false;
306 drv_serial->AddTxPGN(129283);
307 drv_serial->AddTxPGN(129284);
308 drv_serial->AddTxPGN(129285);
310 if (routeman.IsAnyRouteActive()) {
311 fail_any |= !SendPGN129285(routeman, found);
312 fail_any |= !SendPGN129284(routeman, found);
313 fail_any |= !SendPGN129283(routeman, found);
316 return (fail_any == 0);
320 bool fail_any =
false;
324 if ((g_maxWPNameLength >= 3) && (g_maxWPNameLength <= 32))
325 maxName = g_maxWPNameLength;
330 char route_name[] =
"Route";
331 SetN2kPGN129285(msg129285, 0, 0, 0, N2kdir_forward, 0, route_name);
334 RoutePoint *pLegBeginPoint = routeman.GetpActiveRouteSegmentBeginPoint();
335 wxString start_point_name = pLegBeginPoint->GetName().Truncate(maxName);
336 std::string sname = start_point_name.ToStdString();
337 char *s = (
char *)sname.c_str();
339 fail_any |= !AppendN2kPGN129285(msg129285, 0, s, pLegBeginPoint->m_lat,
340 pLegBeginPoint->m_lon);
342 RoutePoint *pActivePoint = routeman.GetpActivePoint();
343 wxString destination_name = pActivePoint->GetName().Truncate(maxName);
344 std::string dname = destination_name.ToStdString();
345 char *d = (
char *)dname.c_str();
346 fail_any |= !AppendN2kPGN129285(msg129285, 1, d, pActivePoint->m_lat,
347 pActivePoint->m_lon);
349 if (fail_any)
return false;
351 auto dest_addr = std::make_shared<const NavAddr2000>(driver->
iface, 255);
352 std::vector<uint8_t> payload;
353 for (
int i = 0; i < msg129285.DataLen; i++)
354 payload.push_back(msg129285.Data[i]);
356 std::make_shared<const Nmea2000Msg>(129285, payload, dest_addr, 6);
357 fail_any |= !driver->SendMessage(PGN129285, dest_addr);
359 return (fail_any == 0);
363 bool fail_any =
false;
365 RoutePoint *pActivePoint = routeman.GetpActivePoint();
369 if (!std::isnan(
gCog) && !std::isnan(
gSog)) {
370 double brg = routeman.GetCurrentBrgToActivePoint();
371 vmg =
gSog * cos((brg -
gCog) * PI / 180.);
373 wxTimeSpan tttg_span;
374 wxDateTime arrival_time = wxDateTime::Now();
376 double tttg_sec = (routeman.GetCurrentRngToActivePoint() /
gSog) * 3600;
377 tttg_span = wxTimeSpan::Seconds((
long)tttg_sec);
378 arrival_time += tttg_span;
380 double time_days_1979 = arrival_time.GetTicks() / (3600. * 24.);
384 double eta_time_days;
385 double eta_time_seconds = modf(time_days_1979, &eta_time_days);
386 int16_t eta_time_days_16 =
static_cast<uint16_t
>(eta_time_days);
391 routeman.GetCurrentRngToActivePoint() * 1852.,
398 routeman.GetCurrentSegmentCourse() * PI /
400 routeman.GetCurrentBrgToActivePoint() * PI /
408 auto dest_addr = std::make_shared<const NavAddr2000>(driver->
iface, 255);
409 std::vector<uint8_t> payload;
410 for (
int i = 0; i < msg129284.DataLen; i++)
411 payload.push_back(msg129284.Data[i]);
413 std::make_shared<const Nmea2000Msg>(129284, payload, dest_addr, 6);
414 fail_any |= !driver->SendMessage(PGN129284, dest_addr);
416 return (fail_any == 0);
420 bool fail_any =
false;
422 RoutePoint *pActivePoint = routeman.GetpActivePoint();
424 double xte = routeman.GetCurrentXTEToActivePoint() * 1852.;
427 if (routeman.GetXTEDir() > 0) {
430 SetN2kPGN129283(msg129283, 0,
436 auto dest_addr = std::make_shared<const NavAddr2000>(driver->
iface, 255);
437 std::vector<uint8_t> payload;
438 for (
int i = 0; i < msg129283.DataLen; i++)
439 payload.push_back(msg129283.Data[i]);
441 std::make_shared<const Nmea2000Msg>(129283, payload, dest_addr, 6);
442 fail_any |= !driver->SendMessage(PGN129283, dest_addr);
444 return (fail_any == 0);
bool SendNoRouteRmbRmc(Routeman &routeman)
Send RMC + a faked RMB when there is no active route.
Autopilot output support.
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.
NMEA Data Multiplexer Object.
Variables maintained by comm stack, read-only access for others.
Global variables stored in configuration file.
Hooks into gui available in model.
Wrapper for creating an NmeaContext based on global vars.
Basic DataMonitor logging interface: LogLine (reflects a line in the log) and NmeaLog,...
Global variables Listen()/Notify() wrapper.
bool bGPSValid
Indicate whether the Global Navigation Satellite System (GNSS) has a valid position.
double gVar
Magnetic variation in degrees.
double gLat
Vessel's current latitude in decimal degrees.
double gCog
Course over ground in degrees (0-359.99).
double gSog
Speed over ground in knots.
double gLon
Vessel's current longitude in decimal degrees.
Position, course, speed, etc.
const std::unordered_map< std::string, std::string > GetAttributes(DriverHandle handle)
Query a specific driver for attributes.