/* SITL handling This simulates a GPS on a serial port Andrew Tridgell November 2011 */ #include #include #include #include #include #include #include #include #include #include #include "desktop.h" #include "util.h" extern SITL sitl; #define MAX_GPS_DELAY 100 struct gps_data { double latitude; double longitude; float altitude; double speedN; double speedE; bool have_lock; } gps_data[MAX_GPS_DELAY]; static uint8_t next_gps_index; static uint8_t gps_delay; // state of GPS emulation static struct { /* pipe emulating UBLOX GPS serial stream */ int gps_fd; uint32_t last_update; // milliseconds } gps_state; /* hook for reading from the GPS pipe */ ssize_t sitl_gps_read(int fd, void *buf, size_t count) { return read(fd, buf, count); } /* setup GPS input pipe */ int sitl_gps_pipe(void) { int fd[2]; pipe(fd); gps_state.gps_fd = fd[1]; gps_state.last_update = millis(); set_nonblocking(gps_state.gps_fd); set_nonblocking(fd[0]); return fd[0]; } /* send a UBLOX GPS message */ static void gps_send(uint8_t msgid, uint8_t *buf, uint16_t size) { const uint8_t PREAMBLE1 = 0xb5; const uint8_t PREAMBLE2 = 0x62; const uint8_t CLASS_NAV = 0x1; uint8_t hdr[6], chk[2]; hdr[0] = PREAMBLE1; hdr[1] = PREAMBLE2; hdr[2] = CLASS_NAV; hdr[3] = msgid; hdr[4] = size & 0xFF; hdr[5] = size >> 8; chk[0] = chk[1] = hdr[2]; chk[1] += (chk[0] += hdr[3]); chk[1] += (chk[0] += hdr[4]); chk[1] += (chk[0] += hdr[5]); for (uint8_t i=0; i= gps_delay) { next_gps_index = 0; } d = gps_data[next_gps_index]; if (sitl.gps_delay != gps_delay) { // cope with updates to the delay control gps_delay = sitl.gps_delay; for (uint8_t i=0; i