dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
geofence.c
Go to the documentation of this file.
1 
16 /*
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful, but
23  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25  * for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, see <http://www.gnu.org/licenses/>
29  */
30 
31 
32 #include "openpilot.h"
33 #include <eventdispatcher.h>
34 #include "misc_math.h"
35 #include "physical_constants.h"
36 
37 #include "geofencesettings.h"
38 #include "positionactual.h"
39 #include "modulesettings.h"
40 
41 
42 //
43 // Configuration
44 //
45 #define SAMPLE_PERIOD_MS 250
46 
47 // Private types
48 
49 // Private variables
50 
51 // Private functions
52 static void settingsUpdated(const UAVObjEvent *ev,
53  void *ctx, void *obj, int len);
54 static void checkPosition(const UAVObjEvent *ev,
55  void *ctx, void *obj, int len);
56 
57 // Private variables
58 static GeoFenceSettingsData *geofenceSettings;
59 
64 int32_t GeofenceInitialize(void)
65 {
66  bool module_enabled = false;
67 
68 #ifdef MODULE_Geofence_BUILTIN
69  module_enabled = true;
70 #else
71  uint8_t module_state[MODULESETTINGS_ADMINSTATE_NUMELEM];
72  ModuleSettingsAdminStateGet(module_state);
73  if (module_state[MODULESETTINGS_ADMINSTATE_GEOFENCE] == MODULESETTINGS_ADMINSTATE_ENABLED) {
74  module_enabled = true;
75  } else {
76  module_enabled = false;
77  }
78 #endif
79 
80  if (GeoFenceSettingsInitialize() == -1) {
81  module_enabled = false;
82  return -1;
83  }
84 
85  if (module_enabled) {
86  // allocate and initialize the static data storage only if module is enabled
87  geofenceSettings = (GeoFenceSettingsData *) PIOS_malloc(sizeof(GeoFenceSettingsData));
88  if (geofenceSettings == NULL) {
89  module_enabled = false;
90  return -1;
91  }
92 
93  GeoFenceSettingsConnectCallback(settingsUpdated);
94  settingsUpdated(NULL, NULL, NULL, 0);
95 
96  return 0;
97  }
98 
99  return -1;
100 }
101 
102 /* stub: module has no module thread */
103 int32_t GeofenceStart(void)
104 {
105  if (geofenceSettings == NULL) {
106  return -1;
107  }
108 
109  // Schedule periodic task to check position
110  UAVObjEvent ev = {
111  .obj = PositionActualHandle(),
112  .instId = 0,
113  .event = 0,
114  };
116 
117  return 0;
118 }
119 
121 
126 static void checkPosition(const UAVObjEvent *ev,
127  void *ctx, void *obj, int len)
128 {
129  (void) ev; (void) ctx; (void) obj; (void) len;
130  if (PositionActualHandle()) {
131  PositionActualData positionActual;
132  PositionActualGet(&positionActual);
133 
134  const float distance2 = powf(positionActual.North, 2) + powf(positionActual.East, 2);
135 
136  // ErrorRadius is squared when it is fetched, so this is correct
137  if (distance2 > geofenceSettings->ErrorRadius) {
138  AlarmsSet(SYSTEMALARMS_ALARM_GEOFENCE, SYSTEMALARMS_ALARM_ERROR);
139  } else if (distance2 > geofenceSettings->WarningRadius) {
140  AlarmsSet(SYSTEMALARMS_ALARM_GEOFENCE, SYSTEMALARMS_ALARM_WARNING);
141  } else {
142  AlarmsClear(SYSTEMALARMS_ALARM_GEOFENCE);
143  }
144  }
145 }
146 
150 static void settingsUpdated(const UAVObjEvent *ev,
151  void *ctx, void *obj, int len)
152 {
153  (void) ev; (void) ctx; (void) obj; (void) len;
154  GeoFenceSettingsGet(geofenceSettings);
155 
156  // Cache squared distances to save computations
157  geofenceSettings->WarningRadius = powf(geofenceSettings->WarningRadius, 2);
158  geofenceSettings->ErrorRadius = powf(geofenceSettings->ErrorRadius, 2);
159 }
160 
int32_t GeofenceInitialize(void)
Definition: geofence.c:64
void * PIOS_malloc(size_t size)
Definition: pios_heap.c:125
int32_t AlarmsSet(SystemAlarmsAlarmElem alarm, SystemAlarmsAlarmOptions severity)
Definition: alarms.c:97
bool module_enabled
int32_t GeofenceStart(void)
Definition: geofence.c:103
UAVObjHandle obj
MODULE_INITCALL(GeofenceInitialize, GeofenceStart)
int32_t EventPeriodicCallbackCreate(UAVObjEvent *ev, UAVObjEventCallback cb, uint16_t periodMs)
Definition: systemmod.c:884
Event dispatcher, distributes object events as callbacks. Alternative to using tasks and queues...
static void checkPosition(const UAVObjEvent *ev, void *ctx, void *obj, int len)
Definition: geofence.c:126
static void settingsUpdated(const UAVObjEvent *ev, void *ctx, void *obj, int len)
Definition: geofence.c:150
Includes PiOS and core architecture components.
int32_t AlarmsClear(SystemAlarmsAlarmElem alarm)
Definition: alarms.c:171
#define SAMPLE_PERIOD_MS
Definition: geofence.c:45
static GeoFenceSettingsData * geofenceSettings
Definition: geofence.c:58