Skip to content

Commit 4dd1e52

Browse files
AP_Periph: add support for rover side logging
1 parent 07c0f74 commit 4dd1e52

File tree

8 files changed

+115
-17
lines changed

8 files changed

+115
-17
lines changed

AP_Periph/AP_Periph.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ void AP_Periph_FW::update()
257257

258258
#ifdef ENABLE_BASE_MODE
259259
gps_base.update();
260+
gps_rover.update();
260261
#endif
261262

262263
SRV_Channels::enable_aux_servos();

AP_Periph/AP_Periph.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <AP_HAL/AP_HAL.h>
3434
#include "MAVLink.h"
3535
#include "GPS_Base.h"
36+
#include "GPS_Rover.h"
3637

3738
#if defined(HAL_PERIPH_ENABLE_BATTERY_MPPT_PACKETDIGITAL) && HAL_MAX_CAN_PROTOCOL_DRIVERS < 2
3839
#error "Battery MPPT PacketDigital driver requires at least two CAN Ports"
@@ -230,6 +231,7 @@ class AP_Periph_FW {
230231

231232
#ifdef ENABLE_BASE_MODE
232233
GPS_Base gps_base;
234+
GPS_Rover gps_rover;
233235
#endif
234236

235237
#ifdef GPIO_UBX_SAFEBOOT

AP_Periph/GPS_Base.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ GPS_Base::GPS_Base() {
122122
}
123123

124124
// convert week number and time of week to date/time
125-
void GPS_Base::gps_week_time(const uint16_t week, const uint32_t tow)
125+
void GPS_Base::gps_week_time(struct date_time &dt, const uint16_t week, const uint32_t tow)
126126
{
127127
// days since 1st epoch (6th Jan 1980)
128128
uint32_t days = (week * 7) + (tow / 86400000) + 5;
@@ -170,15 +170,15 @@ void GPS_Base::parse_runtime_ubx(uint8_t byte) {
170170
// handle RAWX message
171171
if (_class == CLASS_RXM && _msg_id == MSG_RXM_RAWX) {
172172
if (_buffer.raw_rawx.week > 0 && _buffer.raw_rawx.rcvTow >= 0) {
173-
gps_week_time((uint16_t)_buffer.raw_rawx.week, (uint32_t)(_buffer.raw_rawx.rcvTow * 1000));
173+
gps_week_time(dt, (uint16_t)_buffer.raw_rawx.week, (uint32_t)(_buffer.raw_rawx.rcvTow * 1000));
174174
can_printf("GPS: %d-%02d-%02d %02d:%02d:%02d\n", dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second);
175175
}
176176
} else if (_class == CLASS_NAV && _msg_id == MSG_NAV_SVIN) {
177177
can_printf("GPS: Survey in status: %s Active:%d Acc:%fm\n", _buffer.nav_svin.valid ? "Valid":"Invalid", _buffer.nav_svin.active, _buffer.nav_svin.meanAcc/10000.0);
178178
if (_buffer.nav_svin.valid) {
179179
can_printf("GPS: Survey in complete\n");
180180
}
181-
if (_start_mean_acc == 0.0 && _buffer.nav_svin.active && (_buffer.nav_svin.meanAcc < (50*10000))) {
181+
if (_start_mean_acc == 0 && _buffer.nav_svin.active && (_buffer.nav_svin.meanAcc < (50*10000))) {
182182
_start_mean_acc = _buffer.nav_svin.meanAcc;
183183
}
184184
memcpy(&curr_svin, &_buffer.nav_svin, sizeof(curr_svin));
@@ -526,7 +526,7 @@ void GPS_Base::do_configurations()
526526
if ((AP_HAL::millis() - _last_surveyin_config_ms) > 1000) {
527527
can_printf("GPS_Base: Setting survey in config");
528528
ubx_cfg_tmode3 surveyin_cfg {};
529-
if (_s_in_lat == 0.0 && _s_in_lon == 0.0 && _s_in_alt == 0.0) {
529+
if (int(_s_in_lat*1000) == 0 && int(_s_in_lon*1000) == 0 && int(_s_in_alt*1000) == 0) {
530530
surveyin_cfg.flags = 1;
531531
surveyin_cfg.fixedPosAcc = 0;
532532
surveyin_cfg.svinAccLimit = _s_in_acc*10000.0;

AP_Periph/GPS_Base.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
#include <AP_GPS/RTCM3_Parser.h>
44

55
#ifdef ENABLE_BASE_MODE
6-
6+
struct date_time {
7+
uint16_t year;
8+
uint8_t month;
9+
uint8_t day;
10+
uint8_t hour;
11+
uint8_t minute;
12+
uint8_t second;
13+
uint32_t utc_sec;
14+
};
715
class GPS_Base {
816
public:
917
GPS_Base();
@@ -14,9 +22,8 @@ class GPS_Base {
1422
bool enabled() const { return _enabled; }
1523

1624
static const struct AP_Param::GroupInfo var_info[];
17-
25+
static void gps_week_time(struct date_time &dt, const uint16_t week, const uint32_t tow);
1826
private:
19-
void gps_week_time(const uint16_t week, const uint32_t tow);
2027
void parse_runtime_ubx(uint8_t byte);
2128
void _update_checksum(uint8_t *data, uint16_t len, uint8_t &ck_a, uint8_t &ck_b);
2229
bool _send_message(uint8_t msg_class, uint8_t msg_id, const void *msg, uint16_t size);
@@ -195,16 +202,7 @@ class GPS_Base {
195202

196203
int ubx_file = -1;
197204

198-
struct date_time {
199-
uint16_t year;
200-
uint8_t month;
201-
uint8_t day;
202-
uint8_t hour;
203-
uint8_t minute;
204-
uint8_t second;
205-
uint32_t utc_sec;
206-
} dt;
207-
205+
struct date_time dt;
208206
bool gps_received_preamble;
209207
bool gcs_received_preamble;
210208
uint8_t gps_length_counter;

AP_Periph/GPS_Rover.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "AP_Periph.h"
2+
#include <AP_Filesystem/AP_Filesystem.h>
3+
#include <stdio.h>
4+
5+
#ifdef ENABLE_BASE_MODE
6+
// table of user settable parameters
7+
const AP_Param::GroupInfo GPS_Rover::var_info[] = {
8+
// @Param: ENABLE
9+
// @DisplayName: Enable GPS Rover Mode
10+
// @Description: Enable GPS Rover Mode
11+
// @User: Standard
12+
AP_GROUPINFO_FLAGS("_ENABLE", 1, GPS_Rover, _enabled, 0, AP_PARAM_FLAG_ENABLE),
13+
14+
AP_GROUPEND
15+
};
16+
17+
GPS_Rover::GPS_Rover() {
18+
// setup parameters
19+
AP_Param::setup_object_defaults(this, var_info);
20+
}
21+
22+
void GPS_Rover::init() {
23+
if (!_enabled) {
24+
return;
25+
}
26+
// set gps raw data if not set
27+
float value;
28+
AP_Param::get("GPS_RAW_DATA", value);
29+
if (value < 1.0) {
30+
AP_Param::set_and_save_by_name("GPS_RAW_DATA", 1);
31+
}
32+
// set callback for GPS RAW data
33+
AP::gps().set_gps_raw_cb(FUNCTOR_BIND_MEMBER(&GPS_Rover::ubx_rawdata_cb, void, const uint8_t*, uint32_t));
34+
}
35+
36+
void GPS_Rover::update() {
37+
// do nothing if not enabled
38+
if (!_initialized && _enabled && (AP::gps().status() >= AP_GPS::GPS_OK_FIX_3D)) {
39+
init();
40+
_initialized = true;
41+
}
42+
}
43+
44+
void GPS_Rover::ubx_rawdata_cb(const uint8_t *data, uint32_t length) {
45+
if (ubx_log_fd == -1) {
46+
struct stat st;
47+
int ret = AP::FS().stat("rover", &st);
48+
if (ret == -1) {
49+
ret = AP::FS().mkdir("rover");
50+
}
51+
date_time dt;
52+
GPS_Base::gps_week_time(dt, AP::gps().time_week(), AP::gps().time_week_ms());
53+
snprintf(_ubx_log_filename, sizeof(_ubx_log_filename), "/rover/UTC_%04d_%02d_%02d_%02d_%02d_%02d.ubx", dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second);
54+
ubx_log_fd = AP::FS().open(_ubx_log_filename, O_CREAT | O_WRONLY | O_TRUNC);
55+
if (ubx_log_fd < 0) {
56+
can_printf("Failed to create rover log file %d\n", errno);
57+
} else {
58+
can_printf("Opened rover log file %s\n", _ubx_log_filename);
59+
}
60+
if (!AP::FS().set_mtime(_ubx_log_filename, dt.utc_sec)) {
61+
can_printf("Failed to set file time %s\n", strerror(errno));
62+
}
63+
}
64+
65+
if (ubx_log_fd != -1) {
66+
AP::FS().write(ubx_log_fd, data, length);
67+
AP::FS().fsync(ubx_log_fd);
68+
}
69+
}
70+
71+
#endif // ENABLE_BASE_MODE

AP_Periph/GPS_Rover.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
#include <AP_Param/AP_Param.h>
3+
4+
#ifdef ENABLE_BASE_MODE
5+
class GPS_Rover {
6+
public:
7+
GPS_Rover();
8+
void update();
9+
static const struct AP_Param::GroupInfo var_info[];
10+
private:
11+
void init();
12+
void ubx_rawdata_cb(const uint8_t *data, uint32_t length);
13+
bool _initialized;
14+
AP_Int8 _enabled;
15+
int ubx_log_fd = -1;
16+
char _ubx_log_filename[48];
17+
};
18+
19+
#endif

AP_Periph/Parameters.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ const AP_Param::Info AP_Periph_FW::var_info[] = {
206206
GSCALAR(gps_safeboot, "GPS_SAFEBOOT", 0),
207207
#endif
208208

209+
#ifdef ENABLE_BASE_MODE
210+
// @Param: ROVER
211+
// @Path: GPS_Rover.cpp
212+
GOBJECT(gps_rover, "ROVER", GPS_Rover),
213+
#endif
214+
209215
AP_VAREND
210216
};
211217

AP_Periph/Parameters.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class Parameters {
5858
k_param_imu_sample_rate,
5959
k_param_imu,
6060
k_param_gps_safeboot,
61+
k_param_gps_rover,
6162
};
6263

6364
AP_Int16 format_version;

0 commit comments

Comments
 (0)