aboutsummaryrefslogtreecommitdiff
path: root/src/modules/navigator
diff options
context:
space:
mode:
authorThomas Gubler <thomasgubler@gmail.com>2014-01-04 14:28:05 +0100
committerThomas Gubler <thomasgubler@gmail.com>2014-01-04 14:28:05 +0100
commita65de1e0b9412708c862fbe87d459250a7a3d5fd (patch)
tree0c1ca8efbf81839a6a771150c4ae4c1291d1e397 /src/modules/navigator
parent220011914c01ef4050ca487059b0d317e6b53fb7 (diff)
parenta48264d5d44018412b9443b245e6974d3f54b20d (diff)
downloadpx4-firmware-a65de1e0b9412708c862fbe87d459250a7a3d5fd.tar.gz
px4-firmware-a65de1e0b9412708c862fbe87d459250a7a3d5fd.tar.bz2
px4-firmware-a65de1e0b9412708c862fbe87d459250a7a3d5fd.zip
Merge branch 'navigator_new_fw' into navigator_new
Diffstat (limited to 'src/modules/navigator')
-rw-r--r--src/modules/navigator/geofence.cpp263
-rw-r--r--src/modules/navigator/geofence.h89
-rw-r--r--src/modules/navigator/mission_feasibility_checker.cpp21
-rw-r--r--src/modules/navigator/mission_feasibility_checker.h11
-rw-r--r--src/modules/navigator/module.mk3
-rw-r--r--src/modules/navigator/navigator_main.cpp150
6 files changed, 412 insertions, 125 deletions
diff --git a/src/modules/navigator/geofence.cpp b/src/modules/navigator/geofence.cpp
new file mode 100644
index 000000000..199ccb41b
--- /dev/null
+++ b/src/modules/navigator/geofence.cpp
@@ -0,0 +1,263 @@
+/****************************************************************************
+ *
+ * Copyright (c) 2013 PX4 Development Team. All rights reserved.
+ * Author: @author Jean Cyr <jean.m.cyr@gmail.com>
+ * @author Thomas Gubler <thomasgubler@gmail.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name PX4 nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+/**
+ * @file geofence.cpp
+ * Provides functions for handling the geofence
+ */
+#include "geofence.h"
+
+#include <uORB/topics/vehicle_global_position.h>
+#include <string.h>
+#include <dataman/dataman.h>
+#include <systemlib/err.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <nuttx/config.h>
+#include <unistd.h>
+
+
+/* Oddly, ERROR is not defined for C++ */
+#ifdef ERROR
+# undef ERROR
+#endif
+static const int ERROR = -1;
+
+Geofence::Geofence() : _fence_pub(-1),
+ _altitude_min(0),
+ _altitude_max(0)
+{
+ memset(&_fence, 0, sizeof(_fence));
+}
+
+Geofence::~Geofence()
+{
+
+}
+
+
+bool Geofence::inside(const struct vehicle_global_position_s *vehicle)
+{
+
+ /* Adaptation of algorithm originally presented as
+ * PNPOLY - Point Inclusion in Polygon Test
+ * W. Randolph Franklin (WRF) */
+
+ unsigned int i, j, vertices = _fence.count;
+ bool c = false;
+ double lat = vehicle->lat / 1e7d;
+ double lon = vehicle->lon / 1e7d;
+
+ // skip vertex 0 (return point)
+ for (i = 0, j = vertices - 1; i < vertices; j = i++)
+ if (((_fence.vertices[i].lon) >= lon != (_fence.vertices[j].lon >= lon)) &&
+ (lat <= (_fence.vertices[j].lat - _fence.vertices[i].lat) * (lon - _fence.vertices[i].lon) /
+ (_fence.vertices[j].lon - _fence.vertices[i].lon) + _fence.vertices[i].lat))
+ c = !c;
+ return c;
+}
+
+bool
+Geofence::loadFromDm(unsigned vertices)
+{
+ struct fence_s temp_fence;
+
+ unsigned i;
+ for (i = 0; i < vertices; i++) {
+ if (dm_read(DM_KEY_FENCE_POINTS, i, temp_fence.vertices + i, sizeof(struct fence_vertex_s)) != sizeof(struct fence_vertex_s)) {
+ break;
+ }
+ }
+
+ temp_fence.count = i;
+
+ if (valid())
+ memcpy(&_fence, &temp_fence, sizeof(_fence));
+ else
+ warnx("Invalid fence file, ignored!");
+
+ return _fence.count != 0;
+}
+
+bool
+Geofence::valid()
+{
+ // NULL fence is valid
+ if (_fence.count == 0) {
+ return true;
+ }
+
+ // Otherwise
+ if ((_fence.count < 4) || (_fence.count > GEOFENCE_MAX_VERTICES)) {
+ warnx("Fence must have at least 3 sides and not more than %d", GEOFENCE_MAX_VERTICES - 1);
+ return false;
+ }
+
+ return true;
+}
+
+void
+Geofence::addPoint(int argc, char *argv[])
+{
+ int ix, last;
+ double lon, lat;
+ struct fence_vertex_s vertex;
+ char *end;
+
+ if ((argc == 1) && (strcmp("-clear", argv[0]) == 0)) {
+ dm_clear(DM_KEY_FENCE_POINTS);
+ publishFence(0);
+ return;
+ }
+
+ if (argc < 3)
+ errx(1, "Specify: -clear | sequence latitude longitude [-publish]");
+
+ ix = atoi(argv[0]);
+ if (ix >= DM_KEY_FENCE_POINTS_MAX)
+ errx(1, "Sequence must be less than %d", DM_KEY_FENCE_POINTS_MAX);
+
+ lat = strtod(argv[1], &end);
+ lon = strtod(argv[2], &end);
+
+ last = 0;
+ if ((argc > 3) && (strcmp(argv[3], "-publish") == 0))
+ last = 1;
+
+ vertex.lat = (float)lat;
+ vertex.lon = (float)lon;
+
+ if (dm_write(DM_KEY_FENCE_POINTS, ix, DM_PERSIST_POWER_ON_RESET, &vertex, sizeof(vertex)) == sizeof(vertex)) {
+ if (last)
+ publishFence((unsigned)ix + 1);
+ return;
+ }
+
+ errx(1, "can't store fence point");
+}
+
+void
+Geofence::publishFence(unsigned vertices)
+{
+ if (_fence_pub == -1)
+ _fence_pub = orb_advertise(ORB_ID(fence), &vertices);
+ else
+ orb_publish(ORB_ID(fence), _fence_pub, &vertices);
+}
+
+int
+Geofence::loadFromFile(const char *filename)
+{
+ FILE *fp;
+ char line[120];
+ int pointCounter = 0;
+ bool gotVertical = false;
+ const char commentChar = '#';
+
+ /* Make sure no data is left in the datamanager */
+ clearDm();
+
+ /* open the mixer definition file */
+ fp = fopen(GEOFENCE_FILENAME, "r");
+ if (fp == NULL) {
+ return ERROR;
+ }
+
+ /* create geofence points from valid lines and store in DM */
+ for (;;) {
+
+ /* get a line, bail on error/EOF */
+ if (fgets(line, sizeof(line), fp) == NULL)
+ break;
+
+ /* Trim leading whitespace */
+ size_t textStart = 0;
+ while((textStart < sizeof(line)/sizeof(char)) && isspace(line[textStart])) textStart++;
+
+ /* if the line starts with #, skip */
+ if (line[textStart] == commentChar)
+ continue;
+
+ if (gotVertical) {
+ /* Parse the line as a geofence point */
+ struct fence_vertex_s vertex;
+
+ if (sscanf(line, "%f %f", &(vertex.lat), &(vertex.lon)) != 2)
+ return ERROR;
+
+
+ if (dm_write(DM_KEY_FENCE_POINTS, pointCounter, DM_PERSIST_POWER_ON_RESET, &vertex, sizeof(vertex)) != sizeof(vertex))
+ return ERROR;
+
+ warnx("Geofence: point: %d, lat %.5f: lon: %.5f", pointCounter, (double)vertex.lat, (double)vertex.lon);
+
+ pointCounter++;
+ } else {
+ /* Parse the line as the vertical limits */
+ if (sscanf(line, "%f %f", &_altitude_min, &_altitude_max) != 2)
+ return ERROR;
+
+
+ warnx("Geofence: alt min: %.4f, alt_max: %.4f", (double)_altitude_min, (double)_altitude_max);
+ gotVertical = true;
+ }
+
+
+ }
+
+ fclose(fp);
+
+ /* Re-Load imported geofence from DM */
+ if(gotVertical && pointCounter > 0)
+ {
+ bool fence_valid = loadFromDm(GEOFENCE_MAX_VERTICES);
+ if (fence_valid) {
+ warnx("Geofence: imported and loaded successfully");
+ return OK;
+ } else {
+ warnx("Geofence: datamanager read error");
+ return ERROR;
+ }
+ } else {
+ warnx("Geofence: import error");
+ }
+
+ return ERROR;
+}
+
+int Geofence::clearDm()
+{
+ dm_clear(DM_KEY_FENCE_POINTS);
+}
diff --git a/src/modules/navigator/geofence.h b/src/modules/navigator/geofence.h
new file mode 100644
index 000000000..8a1d06e71
--- /dev/null
+++ b/src/modules/navigator/geofence.h
@@ -0,0 +1,89 @@
+/****************************************************************************
+ *
+ * Copyright (c) 2013 PX4 Development Team. All rights reserved.
+ * Author: @author Jean Cyr <jean.m.cyr@gmail.com>
+ * @author Thomas Gubler <thomasgubler@gmail.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name PX4 nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+/**
+ * @file geofence.h
+ * Provides functions for handling the geofence
+ */
+
+#ifndef GEOFENCE_H_
+#define GEOFENCE_H_
+
+#include <uORB/topics/fence.h>
+
+#define GEOFENCE_FILENAME "/fs/microsd/etc/geofence.txt"
+
+class Geofence {
+private:
+ struct fence_s _fence;
+ orb_advert_t _fence_pub; /**< publish fence topic */
+
+ float _altitude_min;
+ float _altitude_max;
+public:
+ Geofence();
+ ~Geofence();
+
+ /**
+ * Return whether craft is inside geofence.
+ *
+ * Calculate whether point is inside arbitrary polygon
+ * @param craft pointer craft coordinates
+ * @param fence pointer to array of coordinates, one per vertex. First and last vertex are assumed connected
+ * @return true: craft is inside fence, false:craft is outside fence
+ */
+ bool inside(const struct vehicle_global_position_s *craft);
+
+
+ /**
+ * Load fence parameters.
+ */
+ bool loadFromDm(unsigned vertices);
+
+ int clearDm();
+
+ bool valid();
+
+ /**
+ * Specify fence vertex position.
+ */
+ void addPoint(int argc, char *argv[]);
+
+ void publishFence(unsigned vertices);
+
+ int loadFromFile(const char *filename);
+};
+
+
+#endif /* GEOFENCE_H_ */
diff --git a/src/modules/navigator/mission_feasibility_checker.cpp b/src/modules/navigator/mission_feasibility_checker.cpp
index 25b2636bb..aba2dffff 100644
--- a/src/modules/navigator/mission_feasibility_checker.cpp
+++ b/src/modules/navigator/mission_feasibility_checker.cpp
@@ -48,6 +48,7 @@
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
+#include <uORB/topics/fence.h>
/* oddly, ERROR is not defined for c++ */
#ifdef ERROR
@@ -61,7 +62,7 @@ MissionFeasibilityChecker::MissionFeasibilityChecker() : _mavlink_fd(-1), _capab
}
-bool MissionFeasibilityChecker::checkMissionFeasible(bool isRotarywing, dm_item_t dm_current, size_t nItems)
+bool MissionFeasibilityChecker::checkMissionFeasible(bool isRotarywing, dm_item_t dm_current, size_t nMissionItems, Geofence &geofence)
{
/* Init if not done yet */
init();
@@ -74,39 +75,39 @@ bool MissionFeasibilityChecker::checkMissionFeasible(bool isRotarywing, dm_item_
if (isRotarywing)
- return checkMissionFeasibleRotarywing(dm_current, nItems);
+ return checkMissionFeasibleRotarywing(dm_current, nMissionItems, geofence);
else
- return checkMissionFeasibleFixedwing(dm_current, nItems);
+ return checkMissionFeasibleFixedwing(dm_current, nMissionItems, geofence);
}
-bool MissionFeasibilityChecker::checkMissionFeasibleRotarywing(dm_item_t dm_current, size_t nItems)
+bool MissionFeasibilityChecker::checkMissionFeasibleRotarywing(dm_item_t dm_current, size_t nMissionItems, Geofence &geofence)
{
- return checkGeofence(dm_current, nItems);
+ return checkGeofence(dm_current, nMissionItems, geofence);
}
-bool MissionFeasibilityChecker::checkMissionFeasibleFixedwing(dm_item_t dm_current, size_t nItems)
+bool MissionFeasibilityChecker::checkMissionFeasibleFixedwing(dm_item_t dm_current, size_t nMissionItems, Geofence &geofence)
{
/* Update fixed wing navigation capabilites */
updateNavigationCapabilities();
// warnx("_nav_caps.landing_slope_angle_rad %.4f, _nav_caps.landing_horizontal_slope_displacement %.4f", _nav_caps.landing_slope_angle_rad, _nav_caps.landing_horizontal_slope_displacement);
- return (checkFixedWingLanding(dm_current, nItems) && checkGeofence(dm_current, nItems));
+ return (checkFixedWingLanding(dm_current, nMissionItems) && checkGeofence(dm_current, nMissionItems, geofence));
}
-bool MissionFeasibilityChecker::checkGeofence(dm_item_t dm_current, size_t nItems)
+bool MissionFeasibilityChecker::checkGeofence(dm_item_t dm_current, size_t nMissionItems, Geofence &geofence)
{
//xxx: check geofence
return true;
}
-bool MissionFeasibilityChecker::checkFixedWingLanding(dm_item_t dm_current, size_t nItems)
+bool MissionFeasibilityChecker::checkFixedWingLanding(dm_item_t dm_current, size_t nMissionItems)
{
/* Go through all mission items and search for a landing waypoint
* if landing waypoint is found: the previous waypoint is checked to be at a feasible distance and altitude given the landing slope */
- for (size_t i = 0; i < nItems; i++) {
+ for (size_t i = 0; i < nMissionItems; i++) {
static struct mission_item_s missionitem;
const ssize_t len = sizeof(struct mission_item_s);
if (dm_read(dm_current, i, &missionitem, len) != len) {
diff --git a/src/modules/navigator/mission_feasibility_checker.h b/src/modules/navigator/mission_feasibility_checker.h
index 7d1cc2f8a..7a0b2a296 100644
--- a/src/modules/navigator/mission_feasibility_checker.h
+++ b/src/modules/navigator/mission_feasibility_checker.h
@@ -43,6 +43,7 @@
#include <uORB/topics/mission.h>
#include <uORB/topics/navigation_capabilities.h>
#include <dataman/dataman.h>
+#include "geofence.h"
class MissionFeasibilityChecker
@@ -57,15 +58,15 @@ private:
void init();
/* Checks for all airframes */
- bool checkGeofence(dm_item_t dm_current, size_t nItems);
+ bool checkGeofence(dm_item_t dm_current, size_t nMissionItems, Geofence &geofence);
/* Checks specific to fixedwing airframes */
- bool checkMissionFeasibleFixedwing(dm_item_t dm_current, size_t nItems);
- bool checkFixedWingLanding(dm_item_t dm_current, size_t nItems);
+ bool checkMissionFeasibleFixedwing(dm_item_t dm_current, size_t nMissionItems, Geofence &geofence);
+ bool checkFixedWingLanding(dm_item_t dm_current, size_t nMissionItems);
void updateNavigationCapabilities();
/* Checks specific to rotarywing airframes */
- bool checkMissionFeasibleRotarywing(dm_item_t dm_current, size_t nItems);
+ bool checkMissionFeasibleRotarywing(dm_item_t dm_current, size_t nMissionItems, Geofence &geofence);
public:
MissionFeasibilityChecker();
@@ -74,7 +75,7 @@ public:
/*
* Returns true if mission is feasible and false otherwise
*/
- bool checkMissionFeasible(bool isRotarywing, dm_item_t dm_current, size_t nItems);
+ bool checkMissionFeasible(bool isRotarywing, dm_item_t dm_current, size_t nMissionItems, Geofence &geofence);
};
diff --git a/src/modules/navigator/module.mk b/src/modules/navigator/module.mk
index 6be4e87a0..b7900e84e 100644
--- a/src/modules/navigator/module.mk
+++ b/src/modules/navigator/module.mk
@@ -40,6 +40,7 @@ MODULE_COMMAND = navigator
SRCS = navigator_main.cpp \
navigator_params.c \
navigator_mission.cpp \
- mission_feasibility_checker.cpp
+ mission_feasibility_checker.cpp \
+ geofence.cpp
INCLUDE_DIRS += $(MAVLINK_SRC)/include/mavlink
diff --git a/src/modules/navigator/navigator_main.cpp b/src/modules/navigator/navigator_main.cpp
index 61de91dce..e52ef16fa 100644
--- a/src/modules/navigator/navigator_main.cpp
+++ b/src/modules/navigator/navigator_main.cpp
@@ -75,9 +75,12 @@
#include <mathlib/mathlib.h>
#include <dataman/dataman.h>
#include <mavlink/mavlink_log.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#include "navigator_mission.h"
#include "mission_feasibility_checker.h"
+#include "geofence.h"
/* oddly, ERROR is not defined for c++ */
@@ -119,14 +122,14 @@ public:
void status();
/**
- * Load fence parameters.
+ * Add point to geofence
*/
- bool load_fence(unsigned vertices);
+ void add_fence_point(int argc, char *argv[]);
/**
- * Specify fence vertex position.
+ * Load fence from file
*/
- void fence_point(int argc, char *argv[]);
+ void load_fence_from_file(const char *filename);
private:
@@ -144,7 +147,6 @@ private:
int _capabilities_sub; /**< notification of vehicle capabilities updates */
orb_advert_t _triplet_pub; /**< publish position setpoint triplet */
- orb_advert_t _fence_pub; /**< publish fence topic */
orb_advert_t _mission_result_pub; /**< publish mission result topic */
orb_advert_t _control_mode_pub; /**< publish vehicle control mode topic */
@@ -157,7 +159,7 @@ private:
perf_counter_t _loop_perf; /**< loop performance counter */
- struct fence_s _fence; /**< storage for fence vertices */
+ Geofence _geofence;
bool _fence_valid; /**< flag if fence is valid */
bool _inside_fence; /**< vehicle is inside fence */
@@ -268,12 +270,8 @@ private:
*/
void task_main() __attribute__((noreturn));
- void publish_fence(unsigned vertices);
-
void publish_safepoints(unsigned points);
- bool fence_valid(const struct fence_s &fence);
-
/**
* Functions that are triggered when a new state is entered.
*/
@@ -378,7 +376,6 @@ Navigator::Navigator() :
/* publications */
_triplet_pub(-1),
- _fence_pub(-1),
_mission_result_pub(-1),
_control_mode_pub(-1),
@@ -398,8 +395,6 @@ Navigator::Navigator() :
_need_takeoff(true),
_do_takeoff(false)
{
- memset(&_fence, 0, sizeof(_fence));
-
_parameter_handles.min_altitude = param_find("NAV_MIN_ALT");
_parameter_handles.acceptance_radius = param_find("NAV_ACCEPT_RAD");
_parameter_handles.loiter_radius = param_find("NAV_LOITER_RAD");
@@ -499,7 +494,7 @@ Navigator::offboard_mission_update(bool isrotaryWing)
} else {
dm_current = DM_KEY_WAYPOINTS_OFFBOARD_1;
}
- missionFeasiblityChecker.checkMissionFeasible(isrotaryWing, dm_current, (size_t)offboard_mission.count);
+ missionFeasiblityChecker.checkMissionFeasible(isrotaryWing, dm_current, (size_t)offboard_mission.count, _geofence);
_mission.set_offboard_dataman_id(offboard_mission.dataman_id);
_mission.set_current_offboard_mission_index(offboard_mission.current_index);
@@ -552,7 +547,22 @@ Navigator::task_main()
mavlink_log_info(_mavlink_fd, "[navigator] started");
- _fence_valid = load_fence(GEOFENCE_MAX_VERTICES);
+ _fence_valid = _geofence.loadFromDm(GEOFENCE_MAX_VERTICES);
+
+ /* Try to load the geofence:
+ * if /fs/microsd/etc/geofence.txt load from this file
+ * else clear geofence data in datamanager
+ */
+ struct stat buffer;
+ if( stat (GEOFENCE_FILENAME, &buffer) == 0 ) {
+ warnx("Try to load geofence.txt");
+ _geofence.loadFromFile(GEOFENCE_FILENAME);
+ } else {
+ if (_geofence.clearDm() > 0 )
+ warnx("Geofence cleared");
+ else
+ warnx("Could not clear geofence");
+ }
/*
* do subscriptions
@@ -814,9 +824,9 @@ Navigator::status()
}
if (_fence_valid) {
warnx("Geofence is valid");
- warnx("Vertex longitude latitude");
- for (unsigned i = 0; i < _fence.count; i++)
- warnx("%6u %9.5f %8.5f", i, (double)_fence.vertices[i].lon, (double)_fence.vertices[i].lat);
+// warnx("Vertex longitude latitude");
+// for (unsigned i = 0; i < _fence.count; i++)
+// warnx("%6u %9.5f %8.5f", i, (double)_fence.vertices[i].lon, (double)_fence.vertices[i].lat);
} else {
warnx("Geofence not set");
}
@@ -840,96 +850,6 @@ Navigator::status()
}
}
-void
-Navigator::publish_fence(unsigned vertices)
-{
- if (_fence_pub == -1)
- _fence_pub = orb_advertise(ORB_ID(fence), &vertices);
- else
- orb_publish(ORB_ID(fence), _fence_pub, &vertices);
-}
-
-bool
-Navigator::fence_valid(const struct fence_s &fence)
-{
- // NULL fence is valid
- if (fence.count == 0) {
- return true;
- }
-
- // Otherwise
- if ((fence.count < 4) || (fence.count > GEOFENCE_MAX_VERTICES)) {
- warnx("Fence must have at least 3 sides and not more than %d", GEOFENCE_MAX_VERTICES - 1);
- return false;
- }
-
- return true;
-}
-
-bool
-Navigator::load_fence(unsigned vertices)
-{
- struct fence_s temp_fence;
-
- unsigned i;
- for (i = 0; i < vertices; i++) {
- if (dm_read(DM_KEY_FENCE_POINTS, i, temp_fence.vertices + i, sizeof(struct fence_vertex_s)) != sizeof(struct fence_vertex_s)) {
- break;
- }
- }
-
- temp_fence.count = i;
-
- if (fence_valid(temp_fence))
- memcpy(&_fence, &temp_fence, sizeof(_fence));
- else
- warnx("Invalid fence file, ignored!");
-
- return _fence.count != 0;
-}
-
-void
-Navigator::fence_point(int argc, char *argv[])
-{
- int ix, last;
- double lon, lat;
- struct fence_vertex_s vertex;
- char *end;
-
- if ((argc == 1) && (strcmp("-clear", argv[0]) == 0)) {
- dm_clear(DM_KEY_FENCE_POINTS);
- publish_fence(0);
- return;
- }
-
- if (argc < 3)
- errx(1, "Specify: -clear | sequence latitude longitude [-publish]");
-
- ix = atoi(argv[0]);
- if (ix >= DM_KEY_FENCE_POINTS_MAX)
- errx(1, "Sequence must be less than %d", DM_KEY_FENCE_POINTS_MAX);
-
- lat = strtod(argv[1], &end);
- lon = strtod(argv[2], &end);
-
- last = 0;
- if ((argc > 3) && (strcmp(argv[3], "-publish") == 0))
- last = 1;
-
- vertex.lat = (float)lat;
- vertex.lon = (float)lon;
-
- if (dm_write(DM_KEY_FENCE_POINTS, ix, DM_PERSIST_POWER_ON_RESET, &vertex, sizeof(vertex)) == sizeof(vertex)) {
- if (last)
- publish_fence((unsigned)ix + 1);
- return;
- }
-
- errx(1, "can't store fence point");
-}
-
-
-
StateTable::Tran const Navigator::myTable[NAV_STATE_MAX][MAX_EVENT] = {
{
/* STATE_NONE */
@@ -1582,6 +1502,16 @@ bool Navigator::cmp_mission_item_equivalent(const struct mission_item_s a, const
}
}
+void Navigator::add_fence_point(int argc, char *argv[])
+{
+ _geofence.addPoint(argc, argv);
+}
+
+void Navigator::load_fence_from_file(const char *filename)
+{
+ _geofence.loadFromFile(filename);
+}
+
static void usage()
{
@@ -1626,7 +1556,9 @@ int navigator_main(int argc, char *argv[])
navigator::g_navigator->status();
} else if (!strcmp(argv[1], "fence")) {
- navigator::g_navigator->fence_point(argc - 2, argv + 2);
+ navigator::g_navigator->add_fence_point(argc - 2, argv + 2);
+ } else if (!strcmp(argv[1], "fencefile")) {
+ navigator::g_navigator->load_fence_from_file(GEOFENCE_FILENAME);
} else {
usage();