Skip to content

Commit

Permalink
added fence point loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Tridgell committed Dec 16, 2011
1 parent d6562d2 commit 95b086b
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions mavwp.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,63 @@ def save(self, filename):
w.param1, w.param2, w.param3, w.param4,
w.x, w.y, w.z, w.autocontinue))
f.close()


class MAVFenceError(Exception):
'''MAVLink fence error class'''
def __init__(self, msg):
Exception.__init__(self, msg)
self.message = msg

class MAVFenceLoader(object):
'''MAVLink geo-fence loader'''
def __init__(self, target_system=0, target_component=0):
self.points = []
self.target_system = target_system
self.target_component = target_component

def count(self):
'''return number of points'''
return len(self.points)

def point(self, i):
'''return a point'''
return self.points[i]

def add(self, p):
'''add a point'''
self.points.append(p)

def clear(self):
'''clear point list'''
self.points = []

def load(self, filename):
'''load points from a file.
returns number of points loaded'''
f = open(filename, mode='r')
self.clear()
for line in f:
if line.startswith('#'):
continue
line = line.strip()
if not line:
continue
a = line.split()
if len(a) != 2:
raise MAVFenceError("invalid fence point line: %s" % line)
p = mavlink.MAVLink_fence_point_message(self.target_system, self.target_component,
self.count(), 0, float(a[0]), float(a[1]))
self.add(p)
f.close()
for i in range(self.count()):
self.points[i].count = self.count()
return len(self.points)


def save(self, filename):
'''save fence points to a file'''
f = open(filename, mode='w')
for p in self.points:
f.write("%f\t%f\n" % (p.lat, p.lng))
f.close()

0 comments on commit 95b086b

Please sign in to comment.