2023-11-28 06:24:44 -04:00
|
|
|
#include "SIM_config.h"
|
|
|
|
|
|
|
|
#if AP_SIM_GPS_NMEA_ENABLED
|
|
|
|
|
|
|
|
#include "SIM_GPS_NMEA.h"
|
|
|
|
|
|
|
|
#include <SITL/SITL.h>
|
|
|
|
#include <AP_Common/NMEA.h>
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
|
2023-11-30 19:17:39 -04:00
|
|
|
#include <time.h>
|
2024-08-12 19:25:51 -03:00
|
|
|
#include <sys/time.h>
|
2023-11-30 19:17:39 -04:00
|
|
|
|
2023-11-28 06:24:44 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
using namespace SITL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
formatted print of NMEA message, with checksum appended
|
|
|
|
*/
|
|
|
|
void GPS_NMEA::nmea_printf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
char *s = nmea_vaprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (s != nullptr) {
|
|
|
|
write_to_autopilot((const char*)s, strlen(s));
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
send a new GPS NMEA packet
|
|
|
|
*/
|
|
|
|
void GPS_NMEA::publish(const GPS_Data *d)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
struct tm *tm;
|
|
|
|
char tstring[20];
|
|
|
|
char dstring[20];
|
|
|
|
char lat_string[20];
|
|
|
|
char lng_string[20];
|
|
|
|
|
|
|
|
simulation_timeval(&tv);
|
|
|
|
|
2024-02-06 21:22:53 -04:00
|
|
|
struct tm tvd {};
|
|
|
|
tm = gmtime_r(&tv.tv_sec, &tvd);
|
2023-11-28 06:24:44 -04:00
|
|
|
|
|
|
|
// format time string
|
|
|
|
hal.util->snprintf(tstring, sizeof(tstring), "%02u%02u%06.3f", tm->tm_hour, tm->tm_min, tm->tm_sec + tv.tv_usec*1.0e-6);
|
|
|
|
|
|
|
|
// format date string
|
|
|
|
hal.util->snprintf(dstring, sizeof(dstring), "%02u%02u%02u", tm->tm_mday, tm->tm_mon+1, tm->tm_year % 100);
|
|
|
|
|
|
|
|
// format latitude
|
|
|
|
double deg = fabs(d->latitude);
|
|
|
|
hal.util->snprintf(lat_string, sizeof(lat_string), "%02u%08.5f,%c",
|
|
|
|
(unsigned)deg,
|
|
|
|
(deg - int(deg))*60,
|
|
|
|
d->latitude<0?'S':'N');
|
|
|
|
|
|
|
|
// format longitude
|
|
|
|
deg = fabs(d->longitude);
|
|
|
|
hal.util->snprintf(lng_string, sizeof(lng_string), "%03u%08.5f,%c",
|
|
|
|
(unsigned)deg,
|
|
|
|
(deg - int(deg))*60,
|
|
|
|
d->longitude<0?'W':'E');
|
|
|
|
|
|
|
|
nmea_printf("$GPGGA,%s,%s,%s,%01d,%02d,%04.1f,%07.2f,M,0.0,M,,",
|
|
|
|
tstring,
|
|
|
|
lat_string,
|
|
|
|
lng_string,
|
|
|
|
d->have_lock?1:0,
|
2024-08-24 03:35:01 -03:00
|
|
|
d->have_lock?d->num_sats:3,
|
2023-11-28 06:24:44 -04:00
|
|
|
1.2,
|
|
|
|
d->altitude);
|
2023-11-28 16:21:48 -04:00
|
|
|
|
2023-11-28 06:24:44 -04:00
|
|
|
const float speed_mps = d->speed_2d();
|
|
|
|
const float speed_knots = speed_mps * M_PER_SEC_TO_KNOTS;
|
2024-05-12 00:02:44 -03:00
|
|
|
const auto ground_track_deg = degrees(d->ground_track_rad());
|
2023-11-28 06:24:44 -04:00
|
|
|
|
|
|
|
//$GPVTG,133.18,T,120.79,M,0.11,N,0.20,K,A*24
|
|
|
|
nmea_printf("$GPVTG,%.2f,T,%.2f,M,%.2f,N,%.2f,K,A",
|
|
|
|
tstring,
|
2024-05-12 00:02:44 -03:00
|
|
|
ground_track_deg,
|
|
|
|
ground_track_deg,
|
2023-11-28 06:24:44 -04:00
|
|
|
speed_knots,
|
|
|
|
speed_knots * KNOTS_TO_METERS_PER_SECOND * 3.6);
|
|
|
|
|
|
|
|
nmea_printf("$GPRMC,%s,%c,%s,%s,%.2f,%.2f,%s,,",
|
|
|
|
tstring,
|
|
|
|
d->have_lock?'A':'V',
|
|
|
|
lat_string,
|
|
|
|
lng_string,
|
|
|
|
speed_knots,
|
2024-05-12 00:02:44 -03:00
|
|
|
ground_track_deg,
|
2023-11-28 06:24:44 -04:00
|
|
|
dstring);
|
|
|
|
|
2024-11-11 17:49:13 -04:00
|
|
|
if (_sitl->gps[instance].hdg_enabled == SITL::SIM::GPS_HEADING_HDT) {
|
2023-11-28 06:24:44 -04:00
|
|
|
nmea_printf("$GPHDT,%.2f,T", d->yaw_deg);
|
|
|
|
}
|
2024-11-11 17:49:13 -04:00
|
|
|
else if (_sitl->gps[instance].hdg_enabled == SITL::SIM::GPS_HEADING_THS) {
|
2023-11-28 06:24:44 -04:00
|
|
|
nmea_printf("$GPTHS,%.2f,%c,T", d->yaw_deg, d->have_lock ? 'A' : 'V');
|
2024-11-11 17:49:13 -04:00
|
|
|
} else if (_sitl->gps[instance].hdg_enabled == SITL::SIM::GPS_HEADING_KSXT) {
|
2023-11-28 06:24:44 -04:00
|
|
|
// Unicore support
|
|
|
|
// $KSXT,20211016083433.00,116.31296102,39.95817066,49.4911,223.57,-11.32,330.19,0.024,,1,3,28,27,,,,-0.012,0.021,0.020,,*2D
|
|
|
|
nmea_printf("$KSXT,%04u%02u%02u%02u%02u%02u.%02u,%.8f,%.8f,%.4f,%.2f,%.2f,%.2f,%.2f,%.3f,%u,%u,%u,%u,,,,%.3f,%.3f,%.3f,,",
|
|
|
|
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, unsigned(tv.tv_usec*1.e-4),
|
|
|
|
d->longitude, d->latitude,
|
|
|
|
d->altitude,
|
|
|
|
wrap_360(d->yaw_deg),
|
|
|
|
d->pitch_deg,
|
2024-05-12 00:02:44 -03:00
|
|
|
ground_track_deg,
|
2023-11-28 06:24:44 -04:00
|
|
|
speed_mps,
|
|
|
|
d->roll_deg,
|
|
|
|
d->have_lock?1:0, // 2=rtkfloat 3=rtkfixed,
|
|
|
|
3, // fixed rtk yaw solution,
|
2024-08-24 03:35:01 -03:00
|
|
|
d->have_lock?d->num_sats:3,
|
|
|
|
d->have_lock?d->num_sats:3,
|
2023-11-28 06:24:44 -04:00
|
|
|
d->speedE * 3.6,
|
|
|
|
d->speedN * 3.6,
|
|
|
|
-d->speedD * 3.6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // AP_SIM_GPS_NMEA_ENABLED
|