Skip to content

Commit

Permalink
HAL_SITL: allow --home to specify a location from locations.txt
Browse files Browse the repository at this point in the history
this will make it easy to use a named location in MissionPlanner SITL
  • Loading branch information
tridge committed Jan 1, 2021
1 parent eaf7e73 commit 418367c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
5 changes: 5 additions & 0 deletions libraries/AP_HAL_SITL/SITL_State.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ class HALSITL::SITL_State {
Location &loc,
float &yaw_degrees);

/* lookup a location in locations.txt */
static bool lookup_location(const char *home_str,
Location &loc,
float &yaw_degrees);

private:
void _parse_command_line(int argc, char * const argv[]);
void _set_param_default(const char *parm);
Expand Down
44 changes: 42 additions & 2 deletions libraries/AP_HAL_SITL/SITL_cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <SITL/SIM_Scrimmage.h>
#include <SITL/SIM_Webots.h>
#include <SITL/SIM_JSON.h>
#include <AP_Filesystem/AP_Filesystem.h>

#include <signal.h>
#include <stdio.h>
Expand Down Expand Up @@ -74,7 +75,7 @@ void SITL_State::_usage(void)
"\t--instance|-I N set instance of SITL (adds 10*instance to all port numbers)\n"
// "\t--param|-P NAME=VALUE set some param\n" CURRENTLY BROKEN!
"\t--synthetic-clock|-S set synthetic clock mode\n"
"\t--home|-O HOME set start location (lat,lng,alt,yaw)\n"
"\t--home|-O HOME set start location (lat,lng,alt,yaw) or location name\n"
"\t--model|-M MODEL set simulation model\n"
"\t--config string set additional simulation config string\n"
"\t--fg|-F ADDRESS set Flight Gear view address, defaults to 127.0.0.1\n"
Expand Down Expand Up @@ -435,7 +436,12 @@ void SITL_State::_parse_command_line(int argc, char * const argv[])
if (home_str != nullptr) {
Location home;
float home_yaw;
if (!parse_home(home_str, home, home_yaw)) {
if (strchr(home_str,',') == nullptr) {
if (!lookup_location(home_str, home, home_yaw)) {
::printf("Failed to find location (%s). Should be in locations.txt or LAT,LON,ALT,HDG e.g. 37.4003371,-122.0800351,0,353\n", home_str);
exit(1);
}
} else if (!parse_home(home_str, home, home_yaw)) {
::printf("Failed to parse home string (%s). Should be LAT,LON,ALT,HDG e.g. 37.4003371,-122.0800351,0,353\n", home_str);
exit(1);
}
Expand Down Expand Up @@ -545,4 +551,38 @@ bool SITL_State::parse_home(const char *home_str, Location &loc, float &yaw_degr
return true;
}

/*
lookup a location in locations.txt in ROMFS
*/
bool SITL_State::lookup_location(const char *home_str, Location &loc, float &yaw_degrees)
{
const char *locations = "@ROMFS/locations.txt";
FileData *fd = AP::FS().load_file(locations);
if (fd == nullptr) {
::printf("Missing %s\n", locations);
return false;
}
char *str = strndup((const char *)fd->data, fd->length);
if (!str) {
delete fd;
return false;
}
size_t len = strlen(home_str);
char *saveptr = nullptr;
for (char *s = strtok_r(str, "\r\n", &saveptr);
s;
s=strtok_r(nullptr, "\r\n", &saveptr)) {
if (strncasecmp(s, home_str, len) == 0 && s[len]=='=') {
bool ok = parse_home(&s[len+1], loc, yaw_degrees);
free(str);
delete fd;
return ok;
}
}
free(str);
delete fd;
::printf("Failed to find location '%s'\n", home_str);
return false;
}

#endif

0 comments on commit 418367c

Please sign in to comment.