def projectBearing(self, bearing, startPos, searchArea): '''Projects bearing from startPos until it reaches the edge(s) of searchArea (list of lat/long tuples. Returns the First/Last position(s)''' coPoints = [] #for each line in the search are border, get any overlaps with startPos on bearing(+-180) for point in searchArea: dist = cuav_util.gps_distance(point[0], point[1], searchArea[searchArea.index(point)-1][0], searchArea[searchArea.index(point)-1][1]) theta2 = cuav_util.gps_bearing(point[0], point[1], searchArea[searchArea.index(point)-1][0], searchArea[searchArea.index(point)-1][1]) posn = self.Intersection(startPos, bearing, point, theta2) if posn != 0 and cuav_util.gps_distance(posn[0], posn[1], point[0], point[1]) < dist: coPoints.append(posn) posn = self.Intersection(startPos, (bearing + 180) % 360, point, theta2) if posn != 0 and cuav_util.gps_distance(posn[0], posn[1], point[0], point[1]) < dist: coPoints.append(posn) #if there's more than two points in coPoints, return the furthest away points if len(coPoints) < 2: #print str(len(coPoints)) + " point i-sect" return 0 elif len(coPoints) == 2: return coPoints else: dist = 0 for point in coPoints: for pt in coPoints: if cuav_util.gps_distance(pt[0], pt[1], point[0], point[1]) > dist: dist = cuav_util.gps_distance(pt[0], pt[1], point[0], point[1]) newcoPoints = [point, pt] return newcoPoints
def report_points(wp, optspeed): '''show points agl''' home = wp.wp(0) home_ground = EleModel.GetElevation(home.x, home.y) total_distance = 0 max_climb_rate = 0 for i in range(1, wp.count()): w0 = wp.wp(i-1) w = wp.wp(i) ground2 = EleModel.GetElevation(w.x, w.y) if ground2 == None: ground2 = home_ground agl = (home_ground + w.z) - ground2 print("wp[%u] agl=%u" % (i, agl)) wp_dist = cuav_util.gps_distance(w0.x, w0.y, w.x, w.y) wp_time = wp_dist / optspeed climb = (w.z - w0.z) / wp_time if climb > max_climb_rate and i > 1: max_climb_rate = climb print("Climb %.1f at wp %u" % (climb, i)) total_distance += wp_dist t = total_distance / optspeed print("Total_distance: %.2f km flight time %u:%u max_climb=%.1f" % ( total_distance*0.001, int(t/60), t % 60, max_climb_rate))
def report_points(wp): """show points agl""" home = wp.wp(0) home_ground = get_ground_alt(home.x, home.y) total_distance = 0 max_climb_rate = 0 for i in range(1, wp.count()): w0 = wp.wp(i - 1) w = wp.wp(i) ground2 = get_ground_alt(w.x, w.y) agl = (home_ground + w.z) - ground2 print("wp[%u] agl=%u" % (i, agl)) wp_dist = cuav_util.gps_distance(w0.x, w0.y, w.x, w.y) wp_time = wp_dist / opts.speed climb = (w.z - w0.z) / wp_time if climb > max_climb_rate and i > 1: max_climb_rate = climb print("Climb %.1f at wp %u" % (climb, i)) total_distance += wp_dist t = total_distance / opts.speed print( "Total_distance: %.2f km flight time %u:%u max_climb=%.1f" % (total_distance * 0.001, int(t / 60), t % 60, max_climb_rate) )
def report_points(wp): '''show points agl''' home = wp.wp(0) home_ground = get_ground_alt(home.x, home.y) total_distance = 0 max_climb_rate = 0 for i in range(1, wp.count()): w0 = wp.wp(i - 1) w = wp.wp(i) ground2 = get_ground_alt(w.x, w.y) agl = (home_ground + w.z) - ground2 print("wp[%u] agl=%u" % (i, agl)) wp_dist = cuav_util.gps_distance(w0.x, w0.y, w.x, w.y) wp_time = wp_dist / opts.speed climb = (w.z - w0.z) / wp_time if climb > max_climb_rate and i > 1: max_climb_rate = climb print("Climb %.1f at wp %u" % (climb, i)) total_distance += wp_dist t = total_distance / opts.speed print("Total_distance: %.2f km flight time %u:%u max_climb=%.1f" % (total_distance * 0.001, int(t / 60), t % 60, max_climb_rate))
def position(self, t, max_deltat=0,roll=None, pitch=None, maxroll=0, maxpitch=0, pitch_offset=0, roll_offset=0): '''return a MavPosition estimate given a time''' self.advance_log(t) # extrapolate our latitude/longitude gpst = t + self.gps_lag gps_raw = self._find_msg('GLOBAL_POSITION_INT', gpst) gps_timestamp = gps_raw._timestamp velocity = math.sqrt((gps_raw.vx*0.01)**2 + (gps_raw.vy*0.01)**2) deltat = gpst - gps_timestamp (lat, lon) = cuav_util.gps_newpos(gps_raw.lat/1.0e7, gps_raw.lon/1.0e7, gps_raw.hdg*0.01, velocity * (gpst - gps_timestamp)) scaled_pressure = self._find_msg('SCALED_PRESSURE', t) terrain_report = None if len(self.terrain_report) > 0: terrain_report = self._find_msg('TERRAIN_REPORT', t) if terrain_report is not None: (tlat, tlon) = (terrain_report.lat/1.0e7, terrain_report.lon/1.0e7) # don't use it if its too far away if (cuav_util.gps_distance(lat, lon, tlat, tlon) > 150 or abs(terrain_report._timestamp - t) > 5): terrain_report = None # get altitude altitude = self._altitude(scaled_pressure, terrain_report) # and attitude if roll is None: roll = math.degrees(self.interpolate_angle('ATTITUDE', 'roll', t, max_deltat)) if abs(roll) < maxroll: # camera stabilisation system can take care of it roll = 0 elif roll >= maxroll: # adjust for roll stabilisation system can't handle roll = roll - maxroll else: # adjust for roll stabilisation system can't handle roll = roll + maxroll if pitch is None: pitch = math.degrees(self.interpolate_angle('ATTITUDE', 'pitch', t, max_deltat)) if abs(pitch) < maxpitch: pitch = 0 elif pitch >= maxpitch: pitch = pitch - maxpitch else: pitch = pitch + maxpitch # add pitch and roll offset pitch += pitch_offset roll += roll_offset yaw = math.degrees(self.interpolate_angle('ATTITUDE', 'yaw', t, max_deltat)) return MavPosition(lat, lon, altitude, roll, pitch, yaw, t)
def filter_radius(regions, latlon, radius): '''filter a list of regions using a search boundary''' ret = [] for r in regions: if r.latlon is None or cuav_util.gps_distance( latlon[0], latlon[1], r.latlon[0], r.latlon[1]) > radius: r.score = 0 ret.append(r) return ret
def getPolygonLength(self): '''Returns the search pattern path length (metres)''' distance = 0 totPoint = self.entryPoints + self.SearchPattern + self.exitPoints for point in totPoint: if point != totPoint[-1]: distance = distance + cuav_util.gps_distance(point[0], point[1], totPoint[totPoint.index(point)-1][0], totPoint[totPoint.index(point)-1][1]) return int(distance)
def getPolygonLength(self): '''Returns the search pattern path length (metres)''' distance = 0 totPoint = self.entryPoints + self.SearchPattern + self.exitPoints for point in totPoint: if point != totPoint[-1]: distance = distance + cuav_util.gps_distance( point[0], point[1], totPoint[totPoint.index(point) - 1][0], totPoint[totPoint.index(point) - 1][1]) return int(distance)
def position(self, t, max_deltat=0, roll=None, maxroll=45): '''return a MavPosition estimate given a time''' self.advance_log(t) try: # extrapolate our latitude/longitude gpst = t + self.gps_lag gps_raw = self._find_msg('GLOBAL_POSITION_INT', gpst) gps_timestamp = gps_raw._timestamp velocity = math.sqrt((gps_raw.vx * 0.01)**2 + (gps_raw.vy * 0.01)**2) deltat = gpst - gps_timestamp (lat, lon) = cuav_util.gps_newpos(gps_raw.lat / 1.0e7, gps_raw.lon / 1.0e7, gps_raw.hdg * 0.01, velocity * (gpst - gps_timestamp)) scaled_pressure = self._find_msg('SCALED_PRESSURE', t) terrain_report = None if len(self.terrain_report) > 0: terrain_report = self._find_msg('TERRAIN_REPORT', t) if terrain_report is not None: (tlat, tlon) = (terrain_report.lat / 1.0e7, terrain_report.lon / 1.0e7) # don't use it if its too far away if (cuav_util.gps_distance(lat, lon, tlat, tlon) > 150 or abs(terrain_report._timestamp - t) > 5): terrain_report = None # get altitude altitude = self._altitude(scaled_pressure, terrain_report) # and attitude mavroll = math.degrees( self.interpolate_angle('ATTITUDE', 'roll', t, max_deltat)) if roll is None: roll = mavroll elif abs(mavroll) < maxroll: roll = 0 elif mavroll >= maxroll: roll = mavroll - maxroll else: roll = mavroll + maxroll pitch = math.degrees( self.interpolate_angle('ATTITUDE', 'pitch', t, max_deltat)) yaw = math.degrees( self.interpolate_angle('ATTITUDE', 'yaw', t, max_deltat)) return MavPosition(lat, lon, altitude, roll, pitch, yaw, t) except Exception as exception: return MavPosition(0.0, 0.0, 0, 0, 0, 0, t)
def projectBearing(self, bearing, startPos, searchArea): '''Projects bearing from startPos until it reaches the edge(s) of searchArea (list of lat/long tuples. Returns the First/Last position(s)''' coPoints = [] #for each line in the search are border, get any overlaps with startPos on bearing(+-180) for point in searchArea: dist = cuav_util.gps_distance( point[0], point[1], searchArea[searchArea.index(point) - 1][0], searchArea[searchArea.index(point) - 1][1]) theta2 = cuav_util.gps_bearing( point[0], point[1], searchArea[searchArea.index(point) - 1][0], searchArea[searchArea.index(point) - 1][1]) posn = self.Intersection(startPos, bearing, point, theta2) if posn != 0 and cuav_util.gps_distance(posn[0], posn[1], point[0], point[1]) < dist: coPoints.append(posn) posn = self.Intersection(startPos, (bearing + 180) % 360, point, theta2) if posn != 0 and cuav_util.gps_distance(posn[0], posn[1], point[0], point[1]) < dist: coPoints.append(posn) #if there's more than two points in coPoints, return the furthest away points if len(coPoints) < 2: #print str(len(coPoints)) + " point i-sect" return 0 elif len(coPoints) == 2: return coPoints else: dist = 0 for point in coPoints: for pt in coPoints: if cuav_util.gps_distance(pt[0], pt[1], point[0], point[1]) > dist: dist = cuav_util.gps_distance(pt[0], pt[1], point[0], point[1]) newcoPoints = [point, pt] return newcoPoints
def position(self, t, max_deltat=0,roll=None, pitch=None, maxroll=0, maxpitch=0, pitch_offset=0, roll_offset=0): '''return a MavPosition estimate given a time''' self.advance_log(t) # interpolate our latitude/longitude lat = self.interpolate('GLOBAL_POSITION_INT', 'lat', t, max_deltat)*1.0e-7 lon = self.interpolate('GLOBAL_POSITION_INT', 'lon', t, max_deltat)*1.0e-7 terrain_report = None if len(self.terrain_report) > 0: terrain_report = self._find_msg('TERRAIN_REPORT', t) if terrain_report is not None: (tlat, tlon) = (terrain_report.lat/1.0e7, terrain_report.lon/1.0e7) # don't use it if its too far away if (cuav_util.gps_distance(lat, lon, tlat, tlon) > 150 or abs(terrain_report._timestamp - t) > 5): terrain_report = None # get altitude gpst = t + self.gps_lag gps_pos = self._find_msg('GLOBAL_POSITION_INT', gpst) altitude = self._altitude(gps_pos, terrain_report) # and attitude if roll is None: roll = math.degrees(self.interpolate_angle('ATTITUDE', 'roll', t, max_deltat)) if abs(roll) < maxroll: # camera stabilisation system can take care of it roll = 0 elif roll >= maxroll: # adjust for roll stabilisation system can't handle roll = roll - maxroll else: # adjust for roll stabilisation system can't handle roll = roll + maxroll if pitch is None: pitch = math.degrees(self.interpolate_angle('ATTITUDE', 'pitch', t, max_deltat)) if abs(pitch) < maxpitch: pitch = 0 elif pitch >= maxpitch: pitch = pitch - maxpitch else: pitch = pitch + maxpitch # add pitch and roll offset pitch += pitch_offset roll += roll_offset yaw = math.degrees(self.interpolate_angle('ATTITUDE', 'yaw', t, max_deltat)) return MavPosition(lat, lon, altitude, roll, pitch, yaw, t)
def find_xy_in_image(lat, lon, pos, Xres, Yres, C_params): '''find (x,y) coordinates in image that match a position. Very simple method''' # find best X bestX = -1 bestDist = -1 for x in range(0, Xres, 10): latlon = cuav_util.gps_position_from_xy(x, Yres / 2, pos, C_params, pos.altitude) if latlon is None: return None (lat1, lon1) = latlon dist = cuav_util.gps_distance(lat, lon, lat1, lon1) if bestDist < 0 or dist < bestDist: bestX = x bestDist = dist if bestX <= 20 or bestX >= Xres - 20: return None bestY = -1 bestDist = -1 for y in range(0, Yres, 10): latlon = cuav_util.gps_position_from_xy(bestX, y, pos, C_params, pos.altitude) if latlon is None: return None (lat1, lon1) = latlon dist = cuav_util.gps_distance(lat, lon, lat1, lon1) if bestDist < 0 or dist < bestDist: bestY = y bestDist = dist if bestY <= 20 or bestY >= Yres - 20: return None latlon = cuav_util.gps_position_from_xy(bestX, bestY, pos, C_params, pos.altitude) (lat1, lon1) = latlon dist = cuav_util.gps_distance(lat, lon, lat1, lon1) if dist > 15: print("Bad interpolation %.1f" % dist) return None return (bestX, bestY)
def find_xy_in_image(lat, lon, pos, Xres, Yres, C_params): '''find (x,y) coordinates in image that match a position. Very simple method''' # find best X bestX = -1 bestDist = -1 for x in range(0, Xres, 10): latlon = cuav_util.gps_position_from_xy(x, Yres/2, pos, C_params, pos.altitude) if latlon is None: return None (lat1,lon1) = latlon dist = cuav_util.gps_distance(lat, lon, lat1, lon1) if bestDist < 0 or dist < bestDist: bestX = x bestDist = dist if bestX <= 20 or bestX >= Xres-20: return None bestY = -1 bestDist = -1 for y in range(0, Yres, 10): latlon = cuav_util.gps_position_from_xy(bestX, y, pos, C_params, pos.altitude) if latlon is None: return None (lat1,lon1) = latlon dist = cuav_util.gps_distance(lat, lon, lat1, lon1) if bestDist < 0 or dist < bestDist: bestY = y bestDist = dist if bestY <= 20 or bestY >= Yres-20: return None latlon = cuav_util.gps_position_from_xy(bestX, bestY, pos, C_params, pos.altitude) (lat1,lon1) = latlon dist = cuav_util.gps_distance(lat, lon, lat1, lon1) if dist > 15: print("Bad interpolation %.1f" % dist) return None return (bestX, bestY)
def add_points(wp, argagl, argstep, argmaxdelta, argrtlalt): '''add more points for terrain following''' wplist = [] wplist2 = [] for i in range(0, wp.count()): wplist.append(wp.wp(i)) wplist[0].z = argagl # add in RTL wplist.append(wplist[0]) wplist2.append(wplist[0]) home = wp.wp(0) home_ground = EleModel.GetElevation(home.x, home.y) for i in range(1, len(wplist)): prev = (wplist2[-1].x, wplist2[-1].y, wplist2[-1].z) dist = cuav_util.gps_distance(wplist2[-1].x, wplist2[-1].y, wplist[i].x, wplist[i].y) bearing = cuav_util.gps_bearing(wplist2[-1].x, wplist2[-1].y, wplist[i].x, wplist[i].y) print("dist=%u bearing=%u" % (dist, bearing)) while dist > argstep: newpos = cuav_util.gps_newpos(prev[0], prev[1], bearing, argstep) ground1 = EleModel.GetElevation(prev[0], prev[1]) ground2 = EleModel.GetElevation(newpos[0], newpos[1]) if ground2 == None: ground2 = home_ground agl = (home_ground + prev[2]) - ground2 if abs(agl - argagl) > argmaxdelta: newwp = copy.copy(wplist2[-1]) newwp.x = newpos[0] newwp.y = newpos[1] newwp.z = (ground2 + argagl) - home_ground wplist2.append(newwp) print("Inserting at %u" % newwp.z) prev = (newpos[0], newpos[1], newwp.z) else: prev = (newpos[0], newpos[1], wplist2[-1].z) dist -= argstep wplist2.append(wplist[i]) wplist2[-1].z = argrtlalt wp2 = mavwp.MAVWPLoader() for w in wplist2: wp2.add(w) wp2.save("newwp.txt") return wp2
def position(self, t, max_deltat=0,roll=None, maxroll=45): '''return a MavPosition estimate given a time''' self.advance_log(t) try: # extrapolate our latitude/longitude gpst = t + self.gps_lag gps_raw = self._find_msg('GLOBAL_POSITION_INT', gpst) gps_timestamp = gps_raw._timestamp velocity = math.sqrt((gps_raw.vx*0.01)**2 + (gps_raw.vy*0.01)**2) deltat = gpst - gps_timestamp (lat, lon) = cuav_util.gps_newpos(gps_raw.lat/1.0e7, gps_raw.lon/1.0e7, gps_raw.hdg*0.01, velocity * (gpst - gps_timestamp)) scaled_pressure = self._find_msg('SCALED_PRESSURE', t) terrain_report = None if len(self.terrain_report) > 0: terrain_report = self._find_msg('TERRAIN_REPORT', t) if terrain_report is not None: (tlat, tlon) = (terrain_report.lat/1.0e7, terrain_report.lon/1.0e7) # don't use it if its too far away if (cuav_util.gps_distance(lat, lon, tlat, tlon) > 150 or abs(terrain_report._timestamp - t) > 5): terrain_report = None # get altitude altitude = self._altitude(scaled_pressure, terrain_report) # and attitude mavroll = math.degrees(self.interpolate_angle('ATTITUDE', 'roll', t, max_deltat)) if roll is None: roll = mavroll elif abs(mavroll) < maxroll: roll = 0 elif mavroll >= maxroll: roll = mavroll - maxroll else: roll = mavroll + maxroll pitch = math.degrees(self.interpolate_angle('ATTITUDE', 'pitch', t, max_deltat)) yaw = math.degrees(self.interpolate_angle('ATTITUDE', 'yaw', t, max_deltat)) return MavPosition(lat, lon, altitude, roll, pitch, yaw, t) except Exception as exception: return MavPosition(0.0, 0.0, 0, 0, 0, 0,t)
def add_points(wp): '''add more points for terrain following''' wplist = [] wplist2 = [] for i in range(0, wp.count()): wplist.append(wp.wp(i)) wplist[0].z = opts.agl # add in RTL wplist.append(wplist[0]) wplist2.append(wplist[0]) home = wp.wp(0) home_ground = get_ground_alt(home.x, home.y) for i in range(1, len(wplist)): prev = (wplist2[-1].x, wplist2[-1].y, wplist2[-1].z) dist = cuav_util.gps_distance(wplist2[-1].x, wplist2[-1].y, wplist[i].x, wplist[i].y) bearing = cuav_util.gps_bearing(wplist2[-1].x, wplist2[-1].y, wplist[i].x, wplist[i].y) print("dist=%u bearing=%u" % (dist, bearing)) while dist > opts.step: newpos = cuav_util.gps_newpos(prev[0], prev[1], bearing, opts.step) ground1 = get_ground_alt(prev[0], prev[1]) ground2 = get_ground_alt(newpos[0], newpos[1]) agl = (home_ground + prev[2]) - ground2 if abs(agl - opts.agl) > opts.maxdelta: newwp = copy.copy(wplist2[-1]) newwp.x = newpos[0] newwp.y = newpos[1] newwp.z = (ground2 + opts.agl) - home_ground wplist2.append(newwp) print("Inserting at %u" % newwp.z) prev = (newpos[0], newpos[1], newwp.z) else: prev = (newpos[0], newpos[1], wplist2[-1].z) dist -= opts.step wplist2.append(wplist[i]) wplist2[-1].z = opts.rtlalt wp2 = mavwp.MAVWPLoader() for w in wplist2: wp2.add(w) wp2.save("newwp.txt") return wp2
def mouse_event_view(self, event): '''called on mouse events in View window''' x = event.X y = event.Y if self.current_view >= len(self.images): return image = self.images[self.current_view] latlon = cuav_util.gps_position_from_xy(x, y, image.pos, C=self.c_params, shape=image.shape, altitude=image.pos.altitude) if self.last_view_latlon is None or latlon is None: dist = '' else: distance = cuav_util.gps_distance(self.last_view_latlon[0], self.last_view_latlon[1], latlon[0], latlon[1]) bearing = cuav_util.gps_bearing(self.last_view_latlon[0], self.last_view_latlon[1], latlon[0], latlon[1]) dist = "dist %.1f bearing %.1f alt=%.1f shape=%s" % (distance, bearing, image.pos.altitude, image.shape) print("-> %s %s %s" % (latlon, image.filename, dist)) self.last_view_latlon = latlon
def add_points(wp): """add more points for terrain following""" wplist = [] wplist2 = [] for i in range(0, wp.count()): wplist.append(wp.wp(i)) wplist[0].z = opts.agl # add in RTL wplist.append(wplist[0]) wplist2.append(wplist[0]) home = wp.wp(0) home_ground = get_ground_alt(home.x, home.y) for i in range(1, len(wplist)): prev = (wplist2[-1].x, wplist2[-1].y, wplist2[-1].z) dist = cuav_util.gps_distance(wplist2[-1].x, wplist2[-1].y, wplist[i].x, wplist[i].y) bearing = cuav_util.gps_bearing(wplist2[-1].x, wplist2[-1].y, wplist[i].x, wplist[i].y) print("dist=%u bearing=%u" % (dist, bearing)) while dist > opts.step: newpos = cuav_util.gps_newpos(prev[0], prev[1], bearing, opts.step) ground1 = get_ground_alt(prev[0], prev[1]) ground2 = get_ground_alt(newpos[0], newpos[1]) agl = (home_ground + prev[2]) - ground2 if abs(agl - opts.agl) > opts.maxdelta: newwp = copy.copy(wplist2[-1]) newwp.x = newpos[0] newwp.y = newpos[1] newwp.z = (ground2 + opts.agl) - home_ground wplist2.append(newwp) print("Inserting at %u" % newwp.z) prev = (newpos[0], newpos[1], newwp.z) else: prev = (newpos[0], newpos[1], wplist2[-1].z) dist -= opts.step wplist2.append(wplist[i]) wplist2[-1].z = opts.rtlalt wp2 = mavwp.MAVWPLoader() for w in wplist2: wp2.add(w) wp2.save("newwp.txt") return wp2
def fix_climb(wp): '''fix waypoints for max climb rate''' while True: adjusted = False for i in range(1, wp.count()): w0 = wp.wp(i - 1) w = wp.wp(i) w.frame = 3 wp_dist = cuav_util.gps_distance(w0.x, w0.y, w.x, w.y) wp_time = wp_dist / opts.speed climb = (w.z - w0.z) / wp_time if climb > opts.maxclimb + 0.1 and i > 1: z = w.z - (wp_time * opts.maxclimb) adjusted = True print("fix climb %.1f i=%u by %.1fm" % (climb, i, z - w0.z)) w0.z = z if not adjusted: return wp return wp
def fix_climb(wp): """fix waypoints for max climb rate""" while True: adjusted = False for i in range(1, wp.count()): w0 = wp.wp(i - 1) w = wp.wp(i) w.frame = 3 wp_dist = cuav_util.gps_distance(w0.x, w0.y, w.x, w.y) wp_time = wp_dist / opts.speed climb = (w.z - w0.z) / wp_time if climb > opts.maxclimb + 0.1 and i > 1: z = w.z - (wp_time * opts.maxclimb) adjusted = True print("fix climb %.1f i=%u by %.1fm" % (climb, i, z - w0.z)) w0.z = z if not adjusted: return wp return wp
def show_closest(self, latlon, selected): '''show closest camera image''' # first try to show the exact image selected if len(selected) != 0 and self.show_selected(selected[0]): return (lat, lon) = latlon closest = -1 closest_distance = -1 for idx in range(len(self.images)): pos = self.images[idx].pos if pos is not None: distance = cuav_util.gps_distance(lat, lon, pos.lat, pos.lon) if closest == -1 or distance < closest_distance: closest_distance = distance closest = idx if closest == -1: return self.current_view = closest self.last_view_latlon = None image = self.images[closest] self.view_imagefile(image.filename, focus_region=selected)
def best_image(self, pos): '''return the best image for a position''' latint = int(pos[0] * 1000) lonint = int(pos[1] * 1000) if not latint in self.latset or not lonint in self.lonset: return None s = self.latset[latint].intersection(self.lonset[lonint]) if len(s) == 0: return None bestdist = None bestidx = None for idx in s: ipos = self.images[idx].pos if ipos is None: continue dist = cuav_util.gps_distance(pos[0], pos[1], ipos[0], ipos[1]) if bestdist is None or dist < bestdist: bestdist = dist bestidx = idx if bestdist is None: return None print(bestdist) return self.images[bestidx]
def best_image(self, pos): '''return the best image for a position''' latint = int(pos[0]*1000) lonint = int(pos[1]*1000) if not latint in self.latset or not lonint in self.lonset: return None s = self.latset[latint].intersection(self.lonset[lonint]) if len(s) == 0: return None bestdist = None bestidx = None for idx in s: ipos = self.images[idx].pos if ipos is None: continue dist = cuav_util.gps_distance(pos[0], pos[1], ipos[0], ipos[1]) if bestdist is None or dist < bestdist: bestdist = dist bestidx = idx if bestdist is None: return None print(bestdist) return self.images[bestidx]
def position(self, t, max_deltat=0, roll=None, pitch=None, maxroll=0, maxpitch=0, pitch_offset=0, roll_offset=0): '''return a MavPosition estimate given a time''' self.advance_log(t) # interpolate our latitude/longitude lat = self.interpolate('GLOBAL_POSITION_INT', 'lat', t, max_deltat) * 1.0e-7 lon = self.interpolate('GLOBAL_POSITION_INT', 'lon', t, max_deltat) * 1.0e-7 terrain_report = None if len(self.terrain_report) > 0: terrain_report = self._find_msg('TERRAIN_REPORT', t) if terrain_report is not None: (tlat, tlon) = (terrain_report.lat / 1.0e7, terrain_report.lon / 1.0e7) # don't use it if its too far away if (cuav_util.gps_distance(lat, lon, tlat, tlon) > 150 or abs(terrain_report._timestamp - t) > 5): terrain_report = None # get altitude gpst = t + self.gps_lag gps_pos = self._find_msg('GLOBAL_POSITION_INT', gpst) altitude = self._altitude(gps_pos, terrain_report) # and attitude if roll is None: roll = math.degrees( self.interpolate_angle('ATTITUDE', 'roll', t, max_deltat)) if abs(roll) < maxroll: # camera stabilisation system can take care of it roll = 0 elif roll >= maxroll: # adjust for roll stabilisation system can't handle roll = roll - maxroll else: # adjust for roll stabilisation system can't handle roll = roll + maxroll if pitch is None: pitch = math.degrees( self.interpolate_angle('ATTITUDE', 'pitch', t, max_deltat)) if abs(pitch) < maxpitch: pitch = 0 elif pitch >= maxpitch: pitch = pitch - maxpitch else: pitch = pitch + maxpitch # add pitch and roll offset pitch += pitch_offset roll += roll_offset yaw = math.degrees( self.interpolate_angle('ATTITUDE', 'yaw', t, max_deltat)) return MavPosition(lat, lon, altitude, roll, pitch, yaw, t)
def distance_from(self, region, center): '''return distance of a region from a center point''' (lat, lon) = region.latlon (clat, clon) = center return cuav_util.gps_distance(lat, lon, clat, clon)
def altitudeCompensation(self, heightAGL, numMaxPoints=100, threshold=5): '''Creates height points (ASL) for each point in searchArea, entry and exit points such that the plane stays a constant altitude above the ground, constrained by a max number of waypoints''' maxDeltaAlt = 0 maxDeltaAltPoints = [] maxDeltapercentAlong = 0 EleModel = mp_elevation.ElevationModel() #make sure the SRTM tiles are downloaded EleModel.GetElevation(self.SearchPattern[0][0], self.SearchPattern[0][1]) #get the ASL height of the airfield home, entry and exit points and initial search pattern # and add the heightAGL to them self.airportHeight = EleModel.GetElevation(self.airfieldHome[0], self.airfieldHome[1]) if abs(opts.basealt - self.airportHeight) > 30: print("BAD BASE ALTITUDE %u - airfieldhome %u" % (opts.basealt, self.airportHeight)) sys.exit(1) self.airportHeight = opts.basealt self.airfieldHome = (self.airfieldHome[0], self.airfieldHome[1], heightAGL+opts.basealt) for point in self.entryPoints: self.entryPoints[self.entryPoints.index(point)] = (point[0], point[1], heightAGL+10+EleModel.GetElevation(point[0], point[1])) for point in self.exitPoints: self.exitPoints[self.exitPoints.index(point)] = (point[0], point[1], heightAGL+10+EleModel.GetElevation(point[0], point[1])) for point in self.SearchPattern: self.SearchPattern[self.SearchPattern.index(point)] = (point[0], point[1], heightAGL+EleModel.GetElevation(point[0], point[1])) #keep looping through the search area waypoints and add new waypoints where needed #to maintain const height above terrain print "---Starting terrain tracking optimisation---" while True: maxDeltaAlt = 0 maxDeltaAltPoints = [] maxDeltaPointIndex = 0 for point in self.SearchPattern: if point != self.SearchPattern[-1]: dist = cuav_util.gps_distance(point[0], point[1], self.SearchPattern[self.SearchPattern.index(point)+1][0], self.SearchPattern[self.SearchPattern.index(point)+1][1]) theta = cuav_util.gps_bearing(point[0], point[1], self.SearchPattern[self.SearchPattern.index(point)+1][0], self.SearchPattern[self.SearchPattern.index(point)+1][1]) AltDiff = float(self.SearchPattern[self.SearchPattern.index(point)+1][2] - point[2]) if numpy.around(theta) == numpy.around(self.searchBearing) or numpy.around(theta) == numpy.around((self.searchBearing+180) % 360): #increment 10% along waypoint-to-waypoint and get max height difference for i in range(1, 9): partPoint = cuav_util.gps_newpos(point[0], point[1], theta, (i*dist/10)) partAlt = EleModel.GetElevation(partPoint[0], partPoint[1]) + heightAGL #print "Part = " + str(partAlt) + ", Orig = " + str(point[2]) if numpy.abs(point[2] + ((AltDiff*i)/10) - partAlt) > maxDeltaAlt: maxDeltaAlt = numpy.abs(point[2] + ((AltDiff*i)/10) - partAlt) maxDeltaAltPoint = (partPoint[0], partPoint[1], partAlt) maxDeltaPointIndex = self.SearchPattern.index(point)+1 #print "New max alt diff: " + str(maxDeltaAlt) + ", " + str(partAlt) #now put a extra waypoint in between the two maxDeltaAltPoints if maxDeltaAltPoint is not None: self.SearchPattern.insert(maxDeltaPointIndex, maxDeltaAltPoint) #print "There are " + str(len(self.SearchPattern)) + " points in the search pattern. Max Alt error is " + str(maxDeltaAlt) if len(self.SearchPattern) >= numMaxPoints or threshold > maxDeltaAlt: break print "---Done terrain tracking optimisation---"
def altitudeCompensation(self, heightAGL, numMaxPoints=100, threshold=5): '''Creates height points (ASL) for each point in searchArea, entry and exit points such that the plane stays a constant altitude above the ground, constrained by a max number of waypoints''' maxDeltaAlt = 0 maxDeltaAltPoints = [] maxDeltapercentAlong = 0 EleModel = mp_elevation.ElevationModel() #make sure the SRTM tiles are downloaded EleModel.GetElevation(self.SearchPattern[0][0], self.SearchPattern[0][1]) #get the ASL height of the airfield home, entry and exit points and initial search pattern # and add the heightAGL to them self.airportHeight = EleModel.GetElevation(self.airfieldHome[0], self.airfieldHome[1]) if abs(opts.basealt - self.airportHeight) > 30: print("BAD BASE ALTITUDE %u - airfieldhome %u" % (opts.basealt, self.airportHeight)) sys.exit(1) self.airportHeight = opts.basealt self.airfieldHome = (self.airfieldHome[0], self.airfieldHome[1], heightAGL + opts.basealt) for point in self.entryPoints: self.entryPoints[self.entryPoints.index(point)] = ( point[0], point[1], heightAGL + 10 + EleModel.GetElevation(point[0], point[1])) for point in self.exitPoints: self.exitPoints[self.exitPoints.index(point)] = ( point[0], point[1], heightAGL + 10 + EleModel.GetElevation(point[0], point[1])) for point in self.SearchPattern: self.SearchPattern[self.SearchPattern.index(point)] = ( point[0], point[1], heightAGL + EleModel.GetElevation(point[0], point[1])) #keep looping through the search area waypoints and add new waypoints where needed #to maintain const height above terrain print "---Starting terrain tracking optimisation---" while True: maxDeltaAlt = 0 maxDeltaAltPoints = [] maxDeltaPointIndex = 0 for point in self.SearchPattern: if point != self.SearchPattern[-1]: dist = cuav_util.gps_distance( point[0], point[1], self.SearchPattern[self.SearchPattern.index(point) + 1][0], self.SearchPattern[self.SearchPattern.index(point) + 1][1]) theta = cuav_util.gps_bearing( point[0], point[1], self.SearchPattern[self.SearchPattern.index(point) + 1][0], self.SearchPattern[self.SearchPattern.index(point) + 1][1]) AltDiff = float(self.SearchPattern[ self.SearchPattern.index(point) + 1][2] - point[2]) if numpy.around(theta) == numpy.around( self.searchBearing) or numpy.around( theta) == numpy.around( (self.searchBearing + 180) % 360): #increment 10% along waypoint-to-waypoint and get max height difference for i in range(1, 9): partPoint = cuav_util.gps_newpos( point[0], point[1], theta, (i * dist / 10)) partAlt = EleModel.GetElevation( partPoint[0], partPoint[1]) + heightAGL #print "Part = " + str(partAlt) + ", Orig = " + str(point[2]) if numpy.abs(point[2] + ( (AltDiff * i) / 10) - partAlt) > maxDeltaAlt: maxDeltaAlt = numpy.abs(point[2] + ((AltDiff * i) / 10) - partAlt) maxDeltaAltPoint = (partPoint[0], partPoint[1], partAlt) maxDeltaPointIndex = self.SearchPattern.index( point) + 1 #print "New max alt diff: " + str(maxDeltaAlt) + ", " + str(partAlt) #now put a extra waypoint in between the two maxDeltaAltPoints if maxDeltaAltPoint is not None: self.SearchPattern.insert(maxDeltaPointIndex, maxDeltaAltPoint) #print "There are " + str(len(self.SearchPattern)) + " points in the search pattern. Max Alt error is " + str(maxDeltaAlt) if len(self.SearchPattern ) >= numMaxPoints or threshold > maxDeltaAlt: break print "---Done terrain tracking optimisation---"
def CreateSearchPattern(self, width=50.0, overlap=10.0, offset=10, wobble=10, alt=100): '''Generate the waypoints for the search pattern, using alternating strips width is the width (m) of each strip, overlap is the % overlap between strips, alt is the altitude (relative to ground) of the points''' self.SearchPattern = [] #find the nearest point to Airfield Home - use this as a starting point (if entry lanes are not used) if len(self.entryPoints) == 0: nearestdist = cuav_util.gps_distance(self.airfieldHome[0], self.airfieldHome[1], self.searchArea[0][0], self.searchArea[0][1]) nearest = self.searchArea[0] for point in self.searchArea: newdist = cuav_util.gps_distance(self.airfieldHome[0], self.airfieldHome[1], point[0], point[1]) if newdist < nearestdist: nearest = point nearestdist = newdist else: nearestdist = cuav_util.gps_distance(self.entryPoints[0][0], self.entryPoints[0][1], self.searchArea[0][0], self.searchArea[0][1]) nearest = self.searchArea[0] for point in self.searchArea: newdist = cuav_util.gps_distance(self.entryPoints[0][0], self.entryPoints[0][1], point[0], point[1]) #print "dist = " + str(newdist) if newdist < nearestdist: nearest = point nearestdist = newdist #print "Start = " + str(nearest) + ", dist = " + str(nearestdist) #the search pattern will run between the longest side from nearest bearing1 = cuav_util.gps_bearing(nearest[0], nearest[1], self.searchArea[self.searchArea.index(nearest)-1][0], self.searchArea[self.searchArea.index(nearest)-1][1]) bearing2 = cuav_util.gps_bearing(nearest[0], nearest[1], self.searchArea[self.searchArea.index(nearest)+1][0], self.searchArea[self.searchArea.index(nearest)+1][1]) dist1 = cuav_util.gps_distance(nearest[0], nearest[1], self.searchArea[self.searchArea.index(nearest)-1][0], self.searchArea[self.searchArea.index(nearest)-1][1]) dist2 = cuav_util.gps_distance(nearest[0], nearest[1], self.searchArea[self.searchArea.index(nearest)+1][0], self.searchArea[self.searchArea.index(nearest)+1][1]) if dist1 > dist2: self.searchBearing = bearing1 else: self.searchBearing = bearing2 #the search pattern will then run parallel between the two furthest points in the list #searchLine = (0, 0) #for point in self.searchArea: # newdist = cuav_util.gps_distance(point[0], point[1], self.searchArea[self.searchArea.index(point)-1][0], self.searchArea[self.searchArea.index(point)-1][1]) # if newdist > searchLine[0]: # searchLine = (newdist, cuav_util.gps_bearing(point[0], point[1], self.searchArea[self.searchArea.index(point)-1][0], self.searchArea[self.searchArea.index(point)-1][1])) #self.searchBearing = searchLine[1] #need to find the 90 degree bearing to searchBearing that is inside the search area. This #will be the bearing we increment the search rows by #need to get the right signs for the bearings, depending which quadrant the search area is in wrt nearest if not cuav_util.polygon_outside(cuav_util.gps_newpos(nearest[0], nearest[1], (self.searchBearing + 45) % 360, 10), self.searchArea): self.crossBearing = (self.searchBearing + 90) % 360 elif not cuav_util.polygon_outside(cuav_util.gps_newpos(nearest[0], nearest[1], (self.searchBearing + 135) % 360, 10), self.searchArea): self.crossBearing = (self.searchBearing + 90) % 360 self.searchBearing = (self.searchBearing + 180) % 360 elif not cuav_util.polygon_outside(cuav_util.gps_newpos(nearest[0], nearest[1], (self.searchBearing - 45) % 360, 10), self.searchArea): self.crossBearing = (self.searchBearing - 90) % 360 else: self.crossBearing = (self.searchBearing - 90) % 360 self.searchBearing = (self.searchBearing - 180) % 360 print "Search bearing is " + str(self.searchBearing) + "/" + str((self.searchBearing + 180) % 360) print "Cross bearing is: " + str(self.crossBearing) #the distance between runs is this: self.deltaRowDist = width - width*(float(overlap)/100) if self.deltaRowDist <= 0: print "Error, overlap % is too high" return print "Delta row = " + str(self.deltaRowDist) #expand the search area to 1/2 deltaRowDist to ensure full coverage #we are starting at the "nearest" and mowing the lawn parallel to "self.searchBearing" #first waypoint is right near the Search Area boundary (without being on it) (10% of deltaRowDist #on an opposite bearing (so behind the search area) nextWaypoint = cuav_util.gps_newpos(nearest[0], nearest[1], self.crossBearing, self.deltaRowDist/10) print "First = " + str(nextWaypoint) #self.SearchPattern.append(firstWaypoint) #mow the lawn, every 2nd row: while True: pts = self.projectBearing(self.searchBearing, nextWaypoint, self.searchArea) #print "Projecting " + str(nextWaypoint) + " along " + str(self.searchBearing) #check if we're outside the search area if pts == 0: break (nextW, nextnextW) = (pts[0], pts[1]) if cuav_util.gps_distance(nextWaypoint[0], nextWaypoint[1], nextW[0], nextW[1]) < cuav_util.gps_distance(nextWaypoint[0], nextWaypoint[1], nextnextW[0], nextnextW[1]): self.SearchPattern.append(cuav_util.gps_newpos(nextW[0], nextW[1], (self.searchBearing + 180) % 360, (offset+wobble))) self.SearchPattern[-1] =(self.SearchPattern[-1][0], self.SearchPattern[-1][1], alt) self.SearchPattern.append(cuav_util.gps_newpos(nextnextW[0], nextnextW[1], self.searchBearing, (offset+wobble))) self.SearchPattern[-1] =(self.SearchPattern[-1][0], self.SearchPattern[-1][1], alt) #now turn 90degrees from bearing and width distance nextWaypoint = cuav_util.gps_newpos(nextnextW[0], nextnextW[1], self.crossBearing, self.deltaRowDist*2) self.searchBearing = (self.searchBearing + 180) % 360 else: self.SearchPattern.append(cuav_util.gps_newpos(nextnextW[0], nextnextW[1], (self.searchBearing + 180) % 360, offset+wobble)) self.SearchPattern[-1] =(self.SearchPattern[-1][0], self.SearchPattern[-1][1], alt) self.SearchPattern.append(cuav_util.gps_newpos(nextW[0], nextW[1], self.searchBearing, (offset+wobble))) self.SearchPattern[-1] =(self.SearchPattern[-1][0], self.SearchPattern[-1][1], alt) #now turn 90degrees from bearing and width distance nextWaypoint = cuav_util.gps_newpos(nextW[0], nextW[1], self.crossBearing, self.deltaRowDist*2) self.searchBearing = (self.searchBearing + 180) % 360 print "Next = " + str(nextWaypoint) #go back and do the rows we missed. There still might be one more row to do in # the crossbearing direction, so check for that first nextWaypoint = cuav_util.gps_newpos(nextWaypoint[0], nextWaypoint[1], self.crossBearing, -self.deltaRowDist) pts = self.projectBearing(self.searchBearing, nextWaypoint, self.searchArea) if pts == 0: nextWaypoint = cuav_util.gps_newpos(nextWaypoint[0], nextWaypoint[1], self.crossBearing, -2*self.deltaRowDist) self.crossBearing = (self.crossBearing + 180) % 360 else: self.crossBearing = (self.crossBearing + 180) % 360 while True: pts = self.projectBearing(self.searchBearing, nextWaypoint, self.searchArea) #print "Projecting " + str(nextWaypoint) + " along " + str(self.searchBearing) #check if we're outside the search area if pts == 0: break (nextW, nextnextW) = (pts[0], pts[1]) if cuav_util.gps_distance(nextWaypoint[0], nextWaypoint[1], nextW[0], nextW[1]) < cuav_util.gps_distance(nextWaypoint[0], nextWaypoint[1], nextnextW[0], nextnextW[1]): self.SearchPattern.append(cuav_util.gps_newpos(nextW[0], nextW[1], (self.searchBearing + 180) % 360, offset)) self.SearchPattern[-1] =(self.SearchPattern[-1][0], self.SearchPattern[-1][1], alt) self.SearchPattern.append(cuav_util.gps_newpos(nextnextW[0], nextnextW[1], self.searchBearing, offset)) self.SearchPattern[-1] =(self.SearchPattern[-1][0], self.SearchPattern[-1][1], alt) #now turn 90degrees from bearing and width distance nextWaypoint = cuav_util.gps_newpos(nextnextW[0], nextnextW[1], self.crossBearing, self.deltaRowDist*2) self.searchBearing = (self.searchBearing + 180) % 360 else: self.SearchPattern.append(cuav_util.gps_newpos(nextnextW[0], nextnextW[1], (self.searchBearing + 180) % 360, offset)) self.SearchPattern[-1] =(self.SearchPattern[-1][0], self.SearchPattern[-1][1], alt) self.SearchPattern.append(cuav_util.gps_newpos(nextW[0], nextW[1], self.searchBearing, offset)) self.SearchPattern[-1] =(self.SearchPattern[-1][0], self.SearchPattern[-1][1], alt) #now turn 90degrees from bearing and width distance nextWaypoint = cuav_util.gps_newpos(nextW[0], nextW[1], self.crossBearing, self.deltaRowDist*2) self.searchBearing = (self.searchBearing + 180) % 360 print "Next(alt) = " + str(nextWaypoint) #add in the altitude points (relative to airfield home) for point in self.SearchPattern: self.SearchPattern[self.SearchPattern.index(point)] = (point[0], point[1], alt)
def update_mission(self, m): '''update mission status''' if not self.cuav_settings.auto_mission: return wpmod = self.module('wp') wploader = wpmod.wploader if wploader.count() < 2 and self.last_attitude_ms - self.last_wp_list_ms > 5000: self.last_wp_list_ms = self.last_attitude_ms wpmod.cmd_wp(["list"]) wp_start = self.find_user_wp(wploader, self.cuav_settings.wp_start) wp_center = self.find_user_wp(wploader, self.cuav_settings.wp_center) wp_end = self.find_user_wp(wploader, self.cuav_settings.wp_end) if (wp_center is None or wp_start is None or wp_end is None): # not configured return if m.seq >= wp_start and m.seq <= wp_end: lookahead = self.cuav_settings.lookahead_search margin_exc = self.cuav_settings.margin_exc_search else: lookahead = self.cuav_settings.lookahead_default margin_exc = self.cuav_settings.margin_exc_default v = self.mav_param.get('AVD_LOOKAHEAD', None) if v is not None and abs(v - lookahead) > 1: self.send_message("Set AVD_LOOKAHEAD %u" % lookahead) self.master.param_set_send('AVD_LOOKAHEAD', lookahead) v = self.mav_param.get('AVD_MARGIN_EXCL', None) if v is not None and abs(v - margin_exc) > 1: self.send_message("Set AVD_MARGIN_EXCL %u" % margin_exc) self.master.param_set_send('AVD_MARGIN_EXCL', margin_exc) # run every 5 seconds if self.last_attitude_ms - self.last_mission_check_ms < 5000: return self.last_mission_check_ms = self.last_attitude_ms if self.updated_waypoints: cam = self.module('camera_air') if wpmod.loading_waypoints: self.send_message("listing waypoints") wpmod.cmd_wp(["list"]) else: self.send_message("sending waypoints") self.updated_waypoints = False wploader.save("newwp.txt") cam.send_file("newwp.txt") if self.started_landing: # no more to do return if self.last_attitude_ms - self.last_wp_move_ms < 2*60*1000: # only move waypoints every 2 minutes return try: cam = self.module('camera_air') lz = cam.lz target_latitude = cam.camera_settings.target_latitude target_longitude = cam.camera_settings.target_longitude target_radius = cam.camera_settings.target_radius except Exception: self.send_message("target not set") return lzresult = lz.calclandingzone() if lzresult is None: return self.send_message("lzresult nr:%u avgscore:%u" % (lzresult.numregions, lzresult.avgscore)) if lzresult.numregions < 5 and lzresult.avgscore < 20000: # only accept short lists if they have high scores return (lat, lon) = lzresult.latlon # check it is within the target radius if target_radius > 0: dist = cuav_util.gps_distance(lat, lon, target_latitude, target_longitude) self.send_message("dist %u" % dist) if dist > target_radius: return # don't move more than 70m from the center of the search, this keeps us # over more of the search area, and further from the fence max_move = target_radius if self.wp_move_count == 0: # don't move more than 50m from center on first move max_move = 35 if self.wp_move_count == 1: # don't move more than 80m from center on 2nd move max_move = 80 if dist > max_move: bearing = cuav_util.gps_bearing(target_latitude, target_longitude, lat, lon) (lat, lon) = cuav_util.gps_newpos(target_latitude, target_longitude, bearing, max_move) # we may need to fetch the wp list if self.last_attitude_ms - self.last_wp_list_ms > 120000 or wpmod.loading_waypoints: self.last_wp_list_ms = self.last_attitude_ms self.send_message("fetching waypoints") wpmod.cmd_wp(["list"]) return self.last_wp_move_ms = self.last_attitude_ms self.wp_move_count += 1 self.send_message("Moving search to: (%f,%f) %u" % (lat, lon, self.wp_move_count)) wpmod.cmd_wp_movemulti([wp_center, wp_start, wp_end], (lat,lon)) wp_land = self.find_user_wp(wploader, self.cuav_settings.wp_land) if (wp_land is not None and self.wp_move_count >= 3 and lzresult.numregions > 10 and self.master.flightmode == "AUTO"): self.send_message("Starting landing") self.master.waypoint_set_current_send(wp_land) self.started_landing = True self.updated_waypoints = True
def update_mission(self): '''update mission status''' if not self.cuav_settings.auto_mission: return wpmod = self.module('wp') wploader = wpmod.wploader if wploader.count( ) < 2 and self.last_attitude_ms - self.last_wp_list_ms > 5000: self.last_wp_list_ms = self.last_attitude_ms wpmod.cmd_wp(["list"]) wp_start = self.find_user_wp(wploader, self.cuav_settings.wp_start) wp_center = self.find_user_wp(wploader, self.cuav_settings.wp_center) wp_end = self.find_user_wp(wploader, self.cuav_settings.wp_end) if (wp_center is None or wp_start is None or wp_end is None): # not configured return # run every 5 seconds if self.last_attitude_ms - self.last_mission_check_ms < 5000: return self.last_mission_check_ms = self.last_attitude_ms if self.updated_waypoints: cam = self.module('camera_air') if wpmod.loading_waypoints: self.send_message("listing waypoints") wpmod.cmd_wp(["list"]) else: self.send_message("sending waypoints") self.updated_waypoints = False wploader.save("newwp.txt") cam.send_file("newwp.txt") if self.started_landing: # no more to do return if self.last_attitude_ms - self.last_wp_move_ms < 2 * 60 * 1000: # only move waypoints every 2 minutes return try: cam = self.module('camera_air') lz = cam.lz target_latitude = cam.camera_settings.target_latitude target_longitude = cam.camera_settings.target_longitude target_radius = cam.camera_settings.target_radius except Exception: self.send_message("target not set") return lzresult = lz.calclandingzone() if lzresult is None: return self.send_message("lzresult nr:%u avgscore:%u" % (lzresult.numregions, lzresult.avgscore)) if lzresult.numregions < 5 and lzresult.avgscore < 20000: # only accept short lists if they have high scores return (lat, lon) = lzresult.latlon # check it is within the target radius if target_radius > 0: dist = cuav_util.gps_distance(lat, lon, target_latitude, target_longitude) self.send_message("dist %u" % dist) if dist > target_radius: return # don't move more than 70m from the center of the search, this keeps us # over more of the search area, and further from the fence max_move = target_radius if self.wp_move_count == 0: # don't move more than 50m from center on first move max_move = 35 if self.wp_move_count == 1: # don't move more than 80m from center on 2nd move max_move = 80 if dist > max_move: bearing = cuav_util.gps_bearing(target_latitude, target_longitude, lat, lon) (lat, lon) = cuav_util.gps_newpos(target_latitude, target_longitude, bearing, max_move) # we may need to fetch the wp list if self.last_attitude_ms - self.last_wp_list_ms > 120000 or wpmod.loading_waypoints: self.last_wp_list_ms = self.last_attitude_ms self.send_message("fetching waypoints") wpmod.cmd_wp(["list"]) return self.last_wp_move_ms = self.last_attitude_ms self.wp_move_count += 1 self.send_message("Moving search to: (%f,%f) %u" % (lat, lon, self.wp_move_count)) wpmod.cmd_wp_movemulti([wp_center, wp_start, wp_end], (lat, lon)) wp_land = self.find_user_wp(wploader, self.cuav_settings.wp_land) if (wp_land is not None and self.wp_move_count >= 3 and lzresult.numregions > 10 and self.master.flightmode == "AUTO"): self.send_message("Starting landing") self.master.waypoint_set_current_send(wp_land) self.started_landing = True self.updated_waypoints = True
def CreateSearchPattern(self, width=50.0, overlap=10.0, offset=10, wobble=10, alt=100): '''Generate the waypoints for the search pattern, using alternating strips width is the width (m) of each strip, overlap is the % overlap between strips, alt is the altitude (relative to ground) of the points''' self.SearchPattern = [] #find the nearest point to Airfield Home - use this as a starting point (if entry lanes are not used) if len(self.entryPoints) == 0: nearestdist = cuav_util.gps_distance(self.airfieldHome[0], self.airfieldHome[1], self.searchArea[0][0], self.searchArea[0][1]) nearest = self.searchArea[0] for point in self.searchArea: newdist = cuav_util.gps_distance(self.airfieldHome[0], self.airfieldHome[1], point[0], point[1]) if newdist < nearestdist: nearest = point nearestdist = newdist else: nearestdist = cuav_util.gps_distance(self.entryPoints[0][0], self.entryPoints[0][1], self.searchArea[0][0], self.searchArea[0][1]) nearest = self.searchArea[0] for point in self.searchArea: newdist = cuav_util.gps_distance(self.entryPoints[0][0], self.entryPoints[0][1], point[0], point[1]) #print "dist = " + str(newdist) if newdist < nearestdist: nearest = point nearestdist = newdist #print "Start = " + str(nearest) + ", dist = " + str(nearestdist) #the search pattern will run between the longest side from nearest bearing1 = cuav_util.gps_bearing( nearest[0], nearest[1], self.searchArea[self.searchArea.index(nearest) - 1][0], self.searchArea[self.searchArea.index(nearest) - 1][1]) bearing2 = cuav_util.gps_bearing( nearest[0], nearest[1], self.searchArea[self.searchArea.index(nearest) + 1][0], self.searchArea[self.searchArea.index(nearest) + 1][1]) dist1 = cuav_util.gps_distance( nearest[0], nearest[1], self.searchArea[self.searchArea.index(nearest) - 1][0], self.searchArea[self.searchArea.index(nearest) - 1][1]) dist2 = cuav_util.gps_distance( nearest[0], nearest[1], self.searchArea[self.searchArea.index(nearest) + 1][0], self.searchArea[self.searchArea.index(nearest) + 1][1]) if dist1 > dist2: self.searchBearing = bearing1 else: self.searchBearing = bearing2 #the search pattern will then run parallel between the two furthest points in the list #searchLine = (0, 0) #for point in self.searchArea: # newdist = cuav_util.gps_distance(point[0], point[1], self.searchArea[self.searchArea.index(point)-1][0], self.searchArea[self.searchArea.index(point)-1][1]) # if newdist > searchLine[0]: # searchLine = (newdist, cuav_util.gps_bearing(point[0], point[1], self.searchArea[self.searchArea.index(point)-1][0], self.searchArea[self.searchArea.index(point)-1][1])) #self.searchBearing = searchLine[1] #need to find the 90 degree bearing to searchBearing that is inside the search area. This #will be the bearing we increment the search rows by #need to get the right signs for the bearings, depending which quadrant the search area is in wrt nearest if not cuav_util.polygon_outside( cuav_util.gps_newpos(nearest[0], nearest[1], (self.searchBearing + 45) % 360, 10), self.searchArea): self.crossBearing = (self.searchBearing + 90) % 360 elif not cuav_util.polygon_outside( cuav_util.gps_newpos(nearest[0], nearest[1], (self.searchBearing + 135) % 360, 10), self.searchArea): self.crossBearing = (self.searchBearing + 90) % 360 self.searchBearing = (self.searchBearing + 180) % 360 elif not cuav_util.polygon_outside( cuav_util.gps_newpos(nearest[0], nearest[1], (self.searchBearing - 45) % 360, 10), self.searchArea): self.crossBearing = (self.searchBearing - 90) % 360 else: self.crossBearing = (self.searchBearing - 90) % 360 self.searchBearing = (self.searchBearing - 180) % 360 print "Search bearing is " + str(self.searchBearing) + "/" + str( (self.searchBearing + 180) % 360) print "Cross bearing is: " + str(self.crossBearing) #the distance between runs is this: self.deltaRowDist = width - width * (float(overlap) / 100) if self.deltaRowDist <= 0: print "Error, overlap % is too high" return print "Delta row = " + str(self.deltaRowDist) #expand the search area to 1/2 deltaRowDist to ensure full coverage #we are starting at the "nearest" and mowing the lawn parallel to "self.searchBearing" #first waypoint is right near the Search Area boundary (without being on it) (10% of deltaRowDist #on an opposite bearing (so behind the search area) nextWaypoint = cuav_util.gps_newpos(nearest[0], nearest[1], self.crossBearing, self.deltaRowDist / 10) print "First = " + str(nextWaypoint) #self.SearchPattern.append(firstWaypoint) #mow the lawn, every 2nd row: while True: pts = self.projectBearing(self.searchBearing, nextWaypoint, self.searchArea) #print "Projecting " + str(nextWaypoint) + " along " + str(self.searchBearing) #check if we're outside the search area if pts == 0: break (nextW, nextnextW) = (pts[0], pts[1]) if cuav_util.gps_distance( nextWaypoint[0], nextWaypoint[1], nextW[0], nextW[1]) < cuav_util.gps_distance( nextWaypoint[0], nextWaypoint[1], nextnextW[0], nextnextW[1]): self.SearchPattern.append( cuav_util.gps_newpos(nextW[0], nextW[1], (self.searchBearing + 180) % 360, (offset + wobble))) self.SearchPattern[-1] = (self.SearchPattern[-1][0], self.SearchPattern[-1][1], alt) self.SearchPattern.append( cuav_util.gps_newpos(nextnextW[0], nextnextW[1], self.searchBearing, (offset + wobble))) self.SearchPattern[-1] = (self.SearchPattern[-1][0], self.SearchPattern[-1][1], alt) #now turn 90degrees from bearing and width distance nextWaypoint = cuav_util.gps_newpos(nextnextW[0], nextnextW[1], self.crossBearing, self.deltaRowDist * 2) self.searchBearing = (self.searchBearing + 180) % 360 else: self.SearchPattern.append( cuav_util.gps_newpos(nextnextW[0], nextnextW[1], (self.searchBearing + 180) % 360, offset + wobble)) self.SearchPattern[-1] = (self.SearchPattern[-1][0], self.SearchPattern[-1][1], alt) self.SearchPattern.append( cuav_util.gps_newpos(nextW[0], nextW[1], self.searchBearing, (offset + wobble))) self.SearchPattern[-1] = (self.SearchPattern[-1][0], self.SearchPattern[-1][1], alt) #now turn 90degrees from bearing and width distance nextWaypoint = cuav_util.gps_newpos(nextW[0], nextW[1], self.crossBearing, self.deltaRowDist * 2) self.searchBearing = (self.searchBearing + 180) % 360 print "Next = " + str(nextWaypoint) #go back and do the rows we missed. There still might be one more row to do in # the crossbearing direction, so check for that first nextWaypoint = cuav_util.gps_newpos(nextWaypoint[0], nextWaypoint[1], self.crossBearing, -self.deltaRowDist) pts = self.projectBearing(self.searchBearing, nextWaypoint, self.searchArea) if pts == 0: nextWaypoint = cuav_util.gps_newpos(nextWaypoint[0], nextWaypoint[1], self.crossBearing, -2 * self.deltaRowDist) self.crossBearing = (self.crossBearing + 180) % 360 else: self.crossBearing = (self.crossBearing + 180) % 360 while True: pts = self.projectBearing(self.searchBearing, nextWaypoint, self.searchArea) #print "Projecting " + str(nextWaypoint) + " along " + str(self.searchBearing) #check if we're outside the search area if pts == 0: break (nextW, nextnextW) = (pts[0], pts[1]) if cuav_util.gps_distance( nextWaypoint[0], nextWaypoint[1], nextW[0], nextW[1]) < cuav_util.gps_distance( nextWaypoint[0], nextWaypoint[1], nextnextW[0], nextnextW[1]): self.SearchPattern.append( cuav_util.gps_newpos(nextW[0], nextW[1], (self.searchBearing + 180) % 360, offset)) self.SearchPattern[-1] = (self.SearchPattern[-1][0], self.SearchPattern[-1][1], alt) self.SearchPattern.append( cuav_util.gps_newpos(nextnextW[0], nextnextW[1], self.searchBearing, offset)) self.SearchPattern[-1] = (self.SearchPattern[-1][0], self.SearchPattern[-1][1], alt) #now turn 90degrees from bearing and width distance nextWaypoint = cuav_util.gps_newpos(nextnextW[0], nextnextW[1], self.crossBearing, self.deltaRowDist * 2) self.searchBearing = (self.searchBearing + 180) % 360 else: self.SearchPattern.append( cuav_util.gps_newpos(nextnextW[0], nextnextW[1], (self.searchBearing + 180) % 360, offset)) self.SearchPattern[-1] = (self.SearchPattern[-1][0], self.SearchPattern[-1][1], alt) self.SearchPattern.append( cuav_util.gps_newpos(nextW[0], nextW[1], self.searchBearing, offset)) self.SearchPattern[-1] = (self.SearchPattern[-1][0], self.SearchPattern[-1][1], alt) #now turn 90degrees from bearing and width distance nextWaypoint = cuav_util.gps_newpos(nextW[0], nextW[1], self.crossBearing, self.deltaRowDist * 2) self.searchBearing = (self.searchBearing + 180) % 360 print "Next(alt) = " + str(nextWaypoint) #add in the altitude points (relative to airfield home) for point in self.SearchPattern: self.SearchPattern[self.SearchPattern.index(point)] = (point[0], point[1], alt)