2010-09-06 17:18:32 -03:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: t -*-
|
|
|
|
|
2012-06-08 03:40:59 -03:00
|
|
|
#include <FastSerial.h>
|
|
|
|
|
2012-06-15 02:53:14 -03:00
|
|
|
#define GPS_DEBUGGING 0
|
2012-06-08 03:40:59 -03:00
|
|
|
|
|
|
|
#if GPS_DEBUGGING
|
|
|
|
#include <FastSerial.h>
|
|
|
|
# define Debug(fmt, args...) do {Serial.printf("%s:%d: " fmt "\n", __FUNCTION__, __LINE__ , ##args); delay(0); } while(0)
|
|
|
|
#else
|
|
|
|
# define Debug(fmt, args...)
|
|
|
|
#endif
|
|
|
|
|
2012-03-23 02:23:23 -03:00
|
|
|
#include <AP_Common.h>
|
|
|
|
#include <AP_Math.h>
|
2010-09-06 17:18:32 -03:00
|
|
|
#include "GPS.h"
|
2012-01-27 23:25:47 -04:00
|
|
|
#if defined(ARDUINO) && ARDUINO >= 100
|
|
|
|
#include "Arduino.h"
|
|
|
|
#else
|
|
|
|
#include "WProgram.h"
|
|
|
|
#endif
|
2010-09-06 17:18:32 -03:00
|
|
|
|
2010-12-24 02:35:09 -04:00
|
|
|
void
|
|
|
|
GPS::update(void)
|
2010-10-17 17:13:53 -03:00
|
|
|
{
|
2011-10-28 15:52:50 -03:00
|
|
|
bool result;
|
2012-06-08 03:40:59 -03:00
|
|
|
uint32_t tnow;
|
2011-10-28 15:52:50 -03:00
|
|
|
|
|
|
|
// call the GPS driver to process incoming data
|
|
|
|
result = read();
|
|
|
|
|
2012-06-08 03:40:59 -03:00
|
|
|
tnow = millis();
|
|
|
|
|
2011-10-28 15:52:50 -03:00
|
|
|
// if we did not get a message, and the idle timer has expired, re-init
|
|
|
|
if (!result) {
|
2012-06-08 03:40:59 -03:00
|
|
|
if ((tnow - _idleTimer) > idleTimeout) {
|
|
|
|
Debug("gps read timeout %lu %lu", (unsigned long)tnow, (unsigned long)_idleTimer);
|
2011-10-28 15:52:50 -03:00
|
|
|
_status = NO_GPS;
|
|
|
|
|
2012-06-10 03:34:53 -03:00
|
|
|
init(_nav_setting);
|
2011-10-28 15:52:50 -03:00
|
|
|
// reset the idle timer
|
2012-06-08 03:40:59 -03:00
|
|
|
_idleTimer = tnow;
|
2011-10-28 15:52:50 -03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we got a message, update our status correspondingly
|
|
|
|
_status = fix ? GPS_OK : NO_FIX;
|
|
|
|
|
|
|
|
valid_read = true;
|
|
|
|
new_data = true;
|
|
|
|
|
|
|
|
// reset the idle timer
|
2012-06-08 03:40:59 -03:00
|
|
|
_idleTimer = tnow;
|
2012-03-04 05:33:12 -04:00
|
|
|
|
|
|
|
if (_status == GPS_OK) {
|
|
|
|
// update our acceleration
|
2012-03-07 00:15:13 -04:00
|
|
|
float deltat = 1.0e-3 * (_idleTimer - last_fix_time);
|
2012-08-10 22:59:47 -03:00
|
|
|
float deltav = 1.0e-2 * ((float)ground_speed - (float)_last_ground_speed_cm);
|
2012-03-23 02:23:23 -03:00
|
|
|
float gps_heading = ToRad(ground_course * 0.01);
|
|
|
|
float gps_speed = ground_speed * 0.01;
|
|
|
|
float sin_heading, cos_heading;
|
|
|
|
|
|
|
|
cos_heading = cos(gps_heading);
|
|
|
|
sin_heading = sin(gps_heading);
|
|
|
|
|
2012-03-07 00:15:13 -04:00
|
|
|
last_fix_time = _idleTimer;
|
2012-08-10 22:59:47 -03:00
|
|
|
_last_ground_speed_cm = ground_speed;
|
2012-03-04 05:33:12 -04:00
|
|
|
|
2012-03-23 02:23:23 -03:00
|
|
|
_velocity_north = gps_speed * cos_heading;
|
|
|
|
_velocity_east = gps_speed * sin_heading;
|
|
|
|
|
2012-03-04 05:33:12 -04:00
|
|
|
if (deltat > 2.0 || deltat == 0) {
|
|
|
|
// we didn't get a fix for 2 seconds - set
|
|
|
|
// acceleration to zero, as the estimate will be too
|
|
|
|
// far out
|
|
|
|
_acceleration = 0;
|
2012-03-23 02:23:23 -03:00
|
|
|
_acceleration_north = 0;
|
|
|
|
_acceleration_east = 0;
|
2012-03-04 05:33:12 -04:00
|
|
|
} else {
|
|
|
|
// calculate a mildly smoothed acceleration value
|
|
|
|
_acceleration = (0.7 * _acceleration) + (0.3 * (deltav/deltat));
|
2012-03-23 02:23:23 -03:00
|
|
|
|
|
|
|
// calculate the components, to save time in the AHRS code
|
|
|
|
_acceleration_north = _acceleration * cos_heading;
|
|
|
|
_acceleration_east = _acceleration * sin_heading;
|
2012-03-04 05:33:12 -04:00
|
|
|
}
|
|
|
|
}
|
2011-10-28 15:52:50 -03:00
|
|
|
}
|
2010-10-17 17:13:53 -03:00
|
|
|
}
|
2010-12-19 09:24:29 -04:00
|
|
|
|
2011-06-16 13:33:08 -03:00
|
|
|
void
|
2012-06-26 10:50:17 -03:00
|
|
|
GPS::setHIL(uint32_t _time, float _latitude, float _longitude, float _altitude,
|
2011-05-04 16:12:27 -03:00
|
|
|
float _ground_speed, float _ground_course, float _speed_3d, uint8_t _num_sats)
|
2011-02-19 14:33:42 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-12-24 02:35:09 -04:00
|
|
|
// XXX this is probably the wrong way to do it, too
|
2010-12-19 09:24:29 -04:00
|
|
|
void
|
2010-12-24 02:35:09 -04:00
|
|
|
GPS::_error(const char *msg)
|
2010-12-19 09:24:29 -04:00
|
|
|
{
|
2011-10-28 15:52:50 -03:00
|
|
|
Serial.println(msg);
|
2010-12-19 09:24:29 -04:00
|
|
|
}
|
2012-06-15 02:53:14 -03:00
|
|
|
|
|
|
|
///
|
|
|
|
/// write a block of configuration data to a GPS
|
|
|
|
///
|
|
|
|
void GPS::_write_progstr_block(Stream *_fs, const prog_char *pstr, uint8_t size)
|
|
|
|
{
|
|
|
|
while (size--) {
|
|
|
|
_fs->write(pgm_read_byte(pstr++));
|
|
|
|
}
|
|
|
|
}
|