def test_unicode(self): """Tests the unicode method executes.""" log = MissionClockEvent(user=self.user1, team_on_clock=True, team_on_timeout=False) log.save() self.assertIsNotNone(log.__unicode__())
def user_json(user): """Generate JSON-style dict for user.""" telemetry = UasTelemetry.last_for_user(user) return { 'name': user.username, 'id': user.pk, 'on_clock': MissionClockEvent.user_on_clock(user), 'on_timeout': MissionClockEvent.user_on_timeout(user), 'in_air': TakeoffOrLandingEvent.user_in_air(user), 'telemetry': telemetry.json() if telemetry else None }
def put(self, request, pk): """PUT allows updating status.""" try: user = User.objects.get(pk=int(pk)) except User.DoesNotExist: return HttpResponseBadRequest('Unknown team %s' % pk) try: data = json.loads(request.body) except ValueError: return HttpResponseBadRequest('Invalid JSON: %s' % request.body) # Potential events to update. takeoff_event = None clock_event = None # Update whether UAS is in air. if 'in_air' in data: in_air = data['in_air'] if not isinstance(in_air, bool): return HttpResponseBadRequest('in_air must be boolean') currently_in_air = TakeoffOrLandingEvent.user_in_air(user) # New event only necessary if changing status if currently_in_air != in_air: takeoff_event = TakeoffOrLandingEvent(user=user, uas_in_air=in_air) # Update whether UAS in on clock or timeout. if 'on_clock' in data or 'on_timeout' in data: currently_on_clock = MissionClockEvent.user_on_clock(user) currently_on_timeout = MissionClockEvent.user_on_timeout(user) on_clock = data.get('on_clock', currently_on_clock) on_timeout = data.get('on_timeout', currently_on_timeout) if (not isinstance(on_clock, bool) or not isinstance(on_timeout, bool)): return HttpResponseBadRequest( 'on_clock and on_timeout must be boolean.') if on_clock and on_timeout: return HttpResponseBadRequest( 'Cannot be on mission clock and on timeout.') # New event only necessary if changing status if (on_clock != currently_on_clock or on_timeout != currently_on_timeout): clock_event = MissionClockEvent(user=user, team_on_clock=on_clock, team_on_timeout=on_timeout) # Request was valid. Save updates. if takeoff_event: takeoff_event.save() if clock_event: clock_event.save() return HttpResponse( json.dumps(user_json(user)), content_type="application/json")
def user_json(user): """Generate JSON-style dict for user.""" telemetry = UasTelemetry.last_for_user(user) return { 'name': user.username, 'id': user.pk, 'on_clock': MissionClockEvent.user_on_clock(user), 'on_timeout': MissionClockEvent.user_on_timeout(user), 'in_air': TakeoffOrLandingEvent.user_in_air(user), 'telemetry': telemetry.json() if telemetry else None }
def test_user_on_timeout(self): """Tests the user_on_timeout method.""" log = MissionClockEvent(user=self.user1, team_on_clock=False, team_on_timeout=False) log.save() log = MissionClockEvent(user=self.user2, team_on_clock=False, team_on_timeout=True) log.save() self.assertFalse(MissionClockEvent.user_on_timeout(self.user1)) self.assertTrue(MissionClockEvent.user_on_timeout(self.user2))
def test_get_noneditable_without_thumbnail_odlcs(self): """Test GET when there are non-editable odlcs without thumbnail.""" MissionClockEvent(user=self.team, team_on_clock=True, team_on_timeout=False).save() MissionClockEvent(user=self.team, team_on_clock=False, team_on_timeout=False).save() odlc = Odlc(user=self.team, odlc_type=OdlcType.standard) odlc.save() response = self.client.get(odlcs_review_url) self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertEqual(0, len(data))
def get(self, request): """Gets all of the targets ready for review.""" targets = [] for user in User.objects.all(): # Targets still editable aren't ready for review. if (MissionClockEvent.user_on_clock(user) or MissionClockEvent.user_on_timeout(user)): continue # Get targets which have thumbnail. targets.extend([t for t in Target.objects.filter(user=user).all() if t.thumbnail]) # Sort targets by last edit time, convert to json. targets = [t.json(is_superuser=request.user.is_superuser) for t in sorted(targets, key=lambda t: t.last_modified_time)] return JsonResponse(targets, safe=False)
def get(self, request): """Gets all of the targets ready for review.""" targets = [] for user in User.objects.all(): # Targets still editable aren't ready for review. if (MissionClockEvent.user_on_clock(user) or MissionClockEvent.user_on_timeout(user)): continue # Get targets which have thumbnail. targets.extend([t for t in Target.objects.filter(user=user).all() if t.thumbnail]) # Sort targets by last edit time, convert to json. targets = [t.json(is_superuser=request.user.is_superuser) for t in sorted(targets, key=lambda t: t.last_modified_time)] return JsonResponse(targets, safe=False)
def test_get_editable_odlcs(self): """Test GET when there are odlcs but are still in editable window.""" MissionClockEvent(user=self.team, team_on_clock=True, team_on_timeout=False).save() Odlc(user=self.team, odlc_type=OdlcType.standard).save() response = self.client.get(odlcs_review_url) self.assertEqual(200, response.status_code) self.assertEqual([], json.loads(response.content))
def test_user_on_timeout(self): """Tests the user_on_timeout method.""" log = MissionClockEvent(user=self.user1, team_on_clock=False, team_on_timeout=False) log.save() log = MissionClockEvent(user=self.user2, team_on_clock=False, team_on_timeout=True) log.save() self.assertFalse(MissionClockEvent.user_on_timeout(self.user1)) self.assertTrue(MissionClockEvent.user_on_timeout(self.user2))
def test_get_noneditable_odlcs(self): """Test GET when there are non-editable odlcs.""" MissionClockEvent(user=self.team, team_on_clock=True, team_on_timeout=False).save() MissionClockEvent(user=self.team, team_on_clock=False, team_on_timeout=False).save() odlc = Odlc(user=self.team, odlc_type=OdlcType.standard) odlc.save() with open(test_image('A.jpg')) as f: odlc.thumbnail.save('%d.%s' % (odlc.pk, 'jpg'), ImageFile(f)) odlc.save() response = self.client.get(odlcs_review_url) self.assertEqual(200, response.status_code) data = json.loads(response.content) self.assertEqual(1, len(data)) self.assertIn('type', data[0]) self.assertEqual('standard', data[0]['type'])
def put(self, request, pk): """PUT allows updating status.""" try: user = User.objects.get(pk=int(pk)) except User.DoesNotExist: return HttpResponseBadRequest('Unknown team %s' % pk) try: data = json.loads(request.body) except ValueError: return HttpResponseBadRequest('Invalid JSON: %s' % request.body) # Potential events to update. takeoff_event = None clock_event = None # Update whether UAS is in air. if 'in_air' in data: in_air = data['in_air'] if not isinstance(in_air, bool): return HttpResponseBadRequest('in_air must be boolean') currently_in_air = TakeoffOrLandingEvent.user_in_air(user) # New event only necessary if changing status if currently_in_air != in_air: takeoff_event = TakeoffOrLandingEvent( user=user, uas_in_air=in_air) # Update whether UAS in on clock or timeout. if 'on_clock' in data or 'on_timeout' in data: currently_on_clock = MissionClockEvent.user_on_clock(user) currently_on_timeout = MissionClockEvent.user_on_timeout(user) on_clock = data.get('on_clock', currently_on_clock) on_timeout = data.get('on_timeout', currently_on_timeout) if (not isinstance(on_clock, bool) or not isinstance(on_timeout, bool)): return HttpResponseBadRequest( 'on_clock and on_timeout must be boolean.') if on_clock and on_timeout: return HttpResponseBadRequest( 'Cannot be on mission clock and on timeout.') # New event only necessary if changing status if (on_clock != currently_on_clock or on_timeout != currently_on_timeout): clock_event = MissionClockEvent( user=user, team_on_clock=on_clock, team_on_timeout=on_timeout) # Request was valid. Save updates. if takeoff_event: takeoff_event.save() if clock_event: clock_event.save() return HttpResponse( json.dumps(user_json(user)), content_type="application/json")
def generate_feedback(mission_config, user, team_eval): """Generates mission feedback for the given team and mission. Args: mission_config: The mission to evaluate the team against. user: The team user object for which to evaluate and provide feedback. team_eval: The team evaluation to fill. """ feedback = team_eval.feedback # Calculate the total mission clock time. missions = MissionClockEvent.missions(user) mission_clock_time = datetime.timedelta(seconds=0) for mission in missions: duration = mission.duration() if duration is None: team_eval.warnings.append('Infinite mission clock.') else: mission_clock_time += duration feedback.mission_clock_time_sec = mission_clock_time.total_seconds() # Calculate total time in air. flight_periods = TakeoffOrLandingEvent.flights(user) if flight_periods: flight_time = reduce(lambda x, y: x + y, [p.duration() for p in flight_periods]) feedback.flight_time_sec = flight_time.total_seconds() else: feedback.flight_time_sec = 0 # Find the user's flights. for period in flight_periods: if period.duration() is None: team_eval.warnings.append('Infinite flight period.') uas_period_logs = [ UasTelemetry.dedupe(logs) for logs in UasTelemetry.by_time_period(user, flight_periods) ] uas_logs = list(itertools.chain.from_iterable(uas_period_logs)) if not uas_logs: team_eval.warnings.append('No UAS telemetry logs.') # Determine interop telemetry rates. telem_max, telem_avg = UasTelemetry.rates(user, flight_periods, time_period_logs=uas_period_logs) if telem_max: feedback.uas_telemetry_time_max_sec = telem_max if telem_avg: feedback.uas_telemetry_time_avg_sec = telem_avg # Determine if the uas went out of bounds. This must be done for # each period individually so time between periods isn't counted as # out of bounds time. Note that this calculates reported time out # of bounds, not actual or possible time spent out of bounds. out_of_bounds = datetime.timedelta(seconds=0) feedback.boundary_violations = 0 for logs in uas_period_logs: bv, bt = FlyZone.out_of_bounds(mission_config.fly_zones.all(), logs) feedback.boundary_violations += bv out_of_bounds += bt feedback.out_of_bounds_time_sec = out_of_bounds.total_seconds() # Determine if the uas hit the waypoints. feedback.waypoints.extend( UasTelemetry.satisfied_waypoints( mission_config.home_pos, mission_config.mission_waypoints.order_by('order'), uas_logs)) # Evaluate the object detections. user_odlcs = Odlc.objects.filter(user=user).all() evaluator = OdlcEvaluator(user_odlcs, mission_config.odlcs.all()) feedback.odlc.CopyFrom(evaluator.evaluate()) # Determine collisions with stationary and moving obstacles. for obst in mission_config.stationary_obstacles.all(): obst_eval = feedback.stationary_obstacles.add() obst_eval.id = obst.pk obst_eval.hit = obst.evaluate_collision_with_uas(uas_logs) for obst in mission_config.moving_obstacles.all(): obst_eval = feedback.moving_obstacles.add() obst_eval.id = obst.pk obst_eval.hit = obst.evaluate_collision_with_uas(uas_logs) # Add judge feedback. try: judge_feedback = MissionJudgeFeedback.objects.get( mission=mission_config.pk, user=user.pk) feedback.judge.CopyFrom(judge_feedback.proto()) except MissionJudgeFeedback.DoesNotExist: team_eval.warnings.append('No MissionJudgeFeedback for team.') # Sanity check mission time. judge_mission_clock = (feedback.judge.flight_time_sec + feedback.judge.post_process_time_sec) if abs(feedback.mission_clock_time_sec - judge_mission_clock) > 30: team_eval.warnings.append( 'Mission clock differs between interop and judge.')
def setUp(self): """Setup the test case.""" super(TestTargetEvaluator, self).setUp() self.maxDiff = None self.user = User.objects.create_user('user', '*****@*****.**', 'pass') l1 = GpsPosition(latitude=38, longitude=-76) l1.save() l2 = GpsPosition(latitude=38.0003, longitude=-76) l2.save() l3 = GpsPosition(latitude=-38, longitude=76) l3.save() event = MissionClockEvent(user=self.user, team_on_clock=True, team_on_timeout=False) event.save() event = TakeoffOrLandingEvent(user=self.user, uas_in_air=True) event.save() # A target worth full points. self.submit1 = Target(user=self.user, target_type=TargetType.standard, location=l1, orientation=Orientation.s, shape=Shape.square, background_color=Color.white, alphanumeric='ABC', alphanumeric_color=Color.black, description='Submit test target 1', autonomous=True, thumbnail_approved=True) self.submit1.save() self.real1 = Target(user=self.user, target_type=TargetType.standard, location=l1, orientation=Orientation.s, shape=Shape.square, background_color=Color.white, alphanumeric='ABC', alphanumeric_color=Color.black, description='Real target 1') self.real1.save() event = MissionClockEvent(user=self.user, team_on_clock=False, team_on_timeout=False) event.save() # A target worth less than full points. self.submit2 = Target(user=self.user, target_type=TargetType.standard, location=l1, orientation=Orientation.n, shape=Shape.circle, background_color=Color.white, # alphanumeric set below alphanumeric_color=Color.black, description='Submit test target 2', autonomous=False, thumbnail_approved=True) self.submit2.save() self.real2 = Target(user=self.user, target_type=TargetType.standard, location=l2, orientation=Orientation.s, shape=Shape.triangle, background_color=Color.white, alphanumeric='ABC', alphanumeric_color=Color.black, description='Real test target 2') self.real2.save() # A target worth no points, so unmatched. self.submit3 = Target(user=self.user, target_type=TargetType.standard, location=l1, orientation=Orientation.nw, shape=Shape.pentagon, background_color=Color.gray, alphanumeric='XYZ', alphanumeric_color=Color.orange, description='Incorrect description', autonomous=False, thumbnail_approved=True) self.submit3.save() self.real3 = Target(user=self.user, target_type=TargetType.standard, orientation=Orientation.e, shape=Shape.semicircle, background_color=Color.yellow, alphanumeric='LMN', # alphanumeric_color set below location=l3, description='Test target 3') self.real3.save() # Targets without approved image may still match. self.submit4 = Target(user=self.user, target_type=TargetType.emergent, location=l1, description='Test target 4', autonomous=False, thumbnail_approved=False) self.submit4.save() self.real4 = Target(user=self.user, target_type=TargetType.emergent, location=l1, description='Test target 4') self.real4.save() # A target without location worth fewer points. self.submit5 = Target(user=self.user, target_type=TargetType.standard, orientation=Orientation.n, shape=Shape.trapezoid, background_color=Color.purple, alphanumeric='PQR', alphanumeric_color=Color.blue, description='Test target 5', autonomous=False, thumbnail_approved=True) self.submit5.save() self.real5 = Target(user=self.user, target_type=TargetType.standard, location=l1, orientation=Orientation.n, shape=Shape.trapezoid, background_color=Color.purple, alphanumeric='PQR', alphanumeric_color=Color.blue, description='Test target 5') self.real5.save() event = TakeoffOrLandingEvent(user=self.user, uas_in_air=False) event.save() # submit2 updated after landing. self.submit2.alphanumeric = 'ABC' self.submit2.save() self.submit3.alphanumeric_color = Color.yellow self.submit3.save() self.submitted_targets = [self.submit5, self.submit4, self.submit3, self.submit2, self.submit1] self.real_targets = [self.real1, self.real2, self.real3, self.real4, self.real5] self.real_matched_targets = [self.real1, self.real2, self.real4, self.real5]
def test_interop_submission(self): """Tests interop_submission correctly filters submissions.""" # t1 created and updated before mission time starts. t1 = Target(user=self.user, target_type=TargetType.standard) t1.save() t1.alphanumeric = 'A' t1.save() # t2 created before mission time starts and updated once it does. t2 = Target(user=self.user, target_type=TargetType.standard) t2.save() # Mission time starts. event = MissionClockEvent(user=self.user, team_on_clock=True, team_on_timeout=False) event.save() t2.alphanumeric = 'A' t2.save() # t3 created and updated during mission time. t3 = Target(user=self.user, target_type=TargetType.standard) t3.save() t3.alphanumeric = 'A' t3.save() # t4 created in in mission time and updated during timeout. t4 = Target(user=self.user, target_type=TargetType.standard) t4.save() # Team takes timeout. Mission time stops. event = MissionClockEvent(user=self.user, team_on_clock=False, team_on_timeout=True) event.save() t4.alphanumeric = 'A' t4.save() # t5 created and updated during timeout. t5 = Target(user=self.user, target_type=TargetType.standard) t5.save() t5.alphanumeric = 'A' t5.save() # t6 created and updated once mission time resumes. event = MissionClockEvent(user=self.user, team_on_clock=True, team_on_timeout=False) event.save() t6 = Target(user=self.user, target_type=TargetType.standard) t6.save() t6.alphanumeric = 'A' t6.save() event = MissionClockEvent(user=self.user, team_on_clock=False, team_on_timeout=False) event.save() self.assertFalse(t1.interop_submission()) self.assertFalse(t2.interop_submission()) self.assertTrue(t3.interop_submission()) self.assertFalse(t4.interop_submission()) self.assertFalse(t5.interop_submission()) self.assertTrue(t6.interop_submission())
def test_missions(self): """Tets the missions().""" on_clock = MissionClockEvent(user=self.user1, team_on_clock=True, team_on_timeout=False) on_clock.save() on_clock.timestamp = self.year2000 on_clock.save() on_timeout = MissionClockEvent(user=self.user1, team_on_clock=False, team_on_timeout=True) on_timeout.save() on_timeout.timestamp = self.year2001 on_timeout.save() off_timeout = MissionClockEvent(user=self.user1, team_on_clock=True, team_on_timeout=False) off_timeout.save() off_timeout.timestamp = self.year2002 off_timeout.save() off_clock = MissionClockEvent(user=self.user1, team_on_clock=False, team_on_timeout=False) off_clock.save() off_clock.timestamp = self.year2003 off_clock.save() random_event = MissionClockEvent(user=self.user2, team_on_clock=True, team_on_timeout=False) random_event.save() missions = MissionClockEvent.missions(self.user1) self.assertEqual( [TimePeriod(self.year2000, self.year2001), TimePeriod(self.year2002, self.year2003)], missions)
def test_missions(self): """Tets the missions().""" on_clock = MissionClockEvent(user=self.user1, team_on_clock=True, team_on_timeout=False) on_clock.save() on_clock.timestamp = self.year2000 on_clock.save() on_timeout = MissionClockEvent(user=self.user1, team_on_clock=False, team_on_timeout=True) on_timeout.save() on_timeout.timestamp = self.year2001 on_timeout.save() off_timeout = MissionClockEvent(user=self.user1, team_on_clock=True, team_on_timeout=False) off_timeout.save() off_timeout.timestamp = self.year2002 off_timeout.save() off_clock = MissionClockEvent(user=self.user1, team_on_clock=False, team_on_timeout=False) off_clock.save() off_clock.timestamp = self.year2003 off_clock.save() random_event = MissionClockEvent(user=self.user2, team_on_clock=True, team_on_timeout=False) random_event.save() missions = MissionClockEvent.missions(self.user1) self.assertEqual([ TimePeriod(self.year2000, self.year2001), TimePeriod(self.year2002, self.year2003) ], missions)
def setUp(self): """Setup the test case.""" super(TestOdlcEvaluator, self).setUp() self.maxDiff = None self.user = User.objects.create_user('user', '*****@*****.**', 'pass') l1 = GpsPosition(latitude=38, longitude=-76) l1.save() l2 = GpsPosition(latitude=38.0003, longitude=-76) l2.save() l3 = GpsPosition(latitude=-38, longitude=76) l3.save() l4 = GpsPosition(latitude=0, longitude=0) l4.save() event = MissionClockEvent(user=self.user, team_on_clock=True, team_on_timeout=False) event.save() event = TakeoffOrLandingEvent(user=self.user, uas_in_air=True) event.save() # A odlc worth full points. self.submit1 = Odlc(user=self.user, odlc_type=OdlcType.standard, location=l1, orientation=Orientation.s, shape=Shape.square, background_color=Color.white, alphanumeric='ABC', alphanumeric_color=Color.black, description='Submit test odlc 1', description_approved=True, autonomous=True, thumbnail_approved=True) self.submit1.save() self.real1 = Odlc(user=self.user, odlc_type=OdlcType.standard, location=l1, orientation=Orientation.s, shape=Shape.square, background_color=Color.white, alphanumeric='ABC', alphanumeric_color=Color.black, description='Real odlc 1') self.real1.save() event = MissionClockEvent(user=self.user, team_on_clock=False, team_on_timeout=False) event.save() # A odlc worth less than full points. self.submit2 = Odlc( user=self.user, odlc_type=OdlcType.standard, location=l1, orientation=Orientation.n, shape=Shape.circle, background_color=Color.white, # alphanumeric set below alphanumeric_color=Color.black, description='Submit test odlc 2', autonomous=False, thumbnail_approved=True) self.submit2.save() self.real2 = Odlc(user=self.user, odlc_type=OdlcType.standard, location=l2, orientation=Orientation.s, shape=Shape.triangle, background_color=Color.white, alphanumeric='ABC', alphanumeric_color=Color.black, description='Real test odlc 2') self.real2.save() # A odlc worth no points, so unmatched. self.submit3 = Odlc(user=self.user, odlc_type=OdlcType.standard, location=l4, orientation=Orientation.nw, shape=Shape.pentagon, background_color=Color.gray, alphanumeric='XYZ', alphanumeric_color=Color.orange, description='Incorrect description', autonomous=False, thumbnail_approved=True) self.submit3.save() self.real3 = Odlc( user=self.user, odlc_type=OdlcType.standard, orientation=Orientation.e, shape=Shape.semicircle, background_color=Color.yellow, alphanumeric='LMN', # alphanumeric_color set below location=l3, description='Test odlc 3') self.real3.save() # Odlcs without approved image has no match value. self.submit4 = Odlc(user=self.user, odlc_type=OdlcType.emergent, location=l1, description='Test odlc 4', autonomous=False, thumbnail_approved=False) self.submit4.save() self.real4 = Odlc(user=self.user, odlc_type=OdlcType.emergent, location=l1, description='Test odlc 4') self.real4.save() # A odlc without location worth fewer points. self.submit5 = Odlc(user=self.user, odlc_type=OdlcType.standard, orientation=Orientation.n, shape=Shape.trapezoid, background_color=Color.purple, alphanumeric='PQR', alphanumeric_color=Color.blue, description='Test odlc 5', autonomous=False, thumbnail_approved=True) self.submit5.save() self.real5 = Odlc(user=self.user, odlc_type=OdlcType.standard, location=l1, orientation=Orientation.n, shape=Shape.trapezoid, background_color=Color.purple, alphanumeric='PQR', alphanumeric_color=Color.blue, description='Test odlc 5') self.real5.save() # Emergent odlc with correct description. self.submit6 = Odlc(user=self.user, odlc_type=OdlcType.emergent, location=l1, description='Submit test odlc 6', description_approved=True, autonomous=True, thumbnail_approved=True) self.submit6.save() self.real6 = Odlc(user=self.user, odlc_type=OdlcType.emergent, location=l1, description='Real odlc 1', description_approved=True) self.real6.save() event = TakeoffOrLandingEvent(user=self.user, uas_in_air=False) event.save() # submit2 updated after landing. self.submit2.alphanumeric = 'ABC' self.submit2.update_last_modified() self.submit2.save() self.submit2.update_last_modified() self.submit3.alphanumeric_color = Color.yellow self.submit3.update_last_modified() self.submit3.save() # Unused but not unmatched odlc. self.submit7 = Odlc(user=self.user, odlc_type=OdlcType.standard, location=l4, alphanumeric_color=Color.black, description='Submit unused test odlc 1', autonomous=False, thumbnail_approved=True) self.submit7.save() self.submitted_odlcs = [ self.submit7, self.submit6, self.submit5, self.submit4, self.submit3, self.submit2, self.submit1 ] self.real_odlcs = [ self.real1, self.real2, self.real3, self.real4, self.real5, self.real6 ]
def create_data(self): """Create a basic sample dataset.""" self.user1 = User.objects.create_user('user1', '*****@*****.**', 'testpass') self.user1.save() self.user2 = User.objects.create_user('user2', '*****@*****.**', 'testpass') self.user2.save() # user1 is on mission event = MissionClockEvent( user=self.user1, team_on_clock=True, team_on_timeout=False) event.save() # user1 is flying event = TakeoffOrLandingEvent(user=self.user1, uas_in_air=True) event.save() # user2 has landed event = TakeoffOrLandingEvent(user=self.user2, uas_in_air=True) event.save() event = TakeoffOrLandingEvent(user=self.user2, uas_in_air=False) event.save() # user2 is active self.timestamp = timezone.now() gps = GpsPosition(latitude=38.6462, longitude=-76.2452) gps.save() pos = AerialPosition(gps_position=gps, altitude_msl=0) pos.save() self.telem = UasTelemetry( user=self.user2, uas_position=pos, uas_heading=90) self.telem.save() self.telem.timestamp = dateutil.parser.parse( u'2016-10-01T00:00:00.0+00:00') self.telem.save()
def generate_feedback(mission_config, user, team_eval): """Generates mission feedback for the given team and mission. Args: mission_config: The mission to evaluate the team against. user: The team user object for which to evaluate and provide feedback. team_eval: The team evaluation to fill. """ feedback = team_eval.feedback # Calculate the total mission clock time. missions = MissionClockEvent.missions(user) mission_clock_time = datetime.timedelta(seconds=0) for mission in missions: duration = mission.duration() if duration is None: team_eval.warnings.append('Infinite mission clock.') else: mission_clock_time += duration feedback.mission_clock_time_sec = mission_clock_time.total_seconds() # Calculate total time in air. flight_periods = TakeoffOrLandingEvent.flights(user) if flight_periods: flight_time = reduce(lambda x, y: x + y, [p.duration() for p in flight_periods]) feedback.flight_time_sec = flight_time.total_seconds() else: feedback.flight_time_sec = 0 # Find the user's flights. for period in flight_periods: if period.duration() is None: team_eval.warnings.append('Infinite flight period.') uas_period_logs = [ UasTelemetry.dedupe(logs) for logs in UasTelemetry.by_time_period(user, flight_periods) ] uas_logs = list(itertools.chain.from_iterable(uas_period_logs)) if not uas_logs: team_eval.warnings.append('No UAS telemetry logs.') # Determine interop telemetry rates. telem_max, telem_avg = UasTelemetry.rates(user, flight_periods, time_period_logs=uas_period_logs) if telem_max: feedback.uas_telemetry_time_max_sec = telem_max if telem_avg: feedback.uas_telemetry_time_avg_sec = telem_avg # Determine if the uas went out of bounds. This must be done for # each period individually so time between periods isn't counted as # out of bounds time. Note that this calculates reported time out # of bounds, not actual or possible time spent out of bounds. out_of_bounds = datetime.timedelta(seconds=0) feedback.boundary_violations = 0 for logs in uas_period_logs: bv, bt = FlyZone.out_of_bounds(mission_config.fly_zones.all(), logs) feedback.boundary_violations += bv out_of_bounds += bt feedback.out_of_bounds_time_sec = out_of_bounds.total_seconds() # Determine if the uas hit the waypoints. feedback.waypoints.extend(UasTelemetry.satisfied_waypoints( mission_config.home_pos, mission_config.mission_waypoints.order_by( 'order'), uas_logs)) # Evaluate the targets. user_targets = Target.objects.filter(user=user).all() evaluator = TargetEvaluator(user_targets, mission_config.targets.all()) feedback.target.CopyFrom(evaluator.evaluate()) # Determine collisions with stationary and moving obstacles. for obst in mission_config.stationary_obstacles.all(): obst_eval = feedback.stationary_obstacles.add() obst_eval.id = obst.pk obst_eval.hit = obst.evaluate_collision_with_uas(uas_logs) for obst in mission_config.moving_obstacles.all(): obst_eval = feedback.moving_obstacles.add() obst_eval.id = obst.pk obst_eval.hit = obst.evaluate_collision_with_uas(uas_logs) # Add judge feedback. try: judge_feedback = MissionJudgeFeedback.objects.get( mission=mission_config.pk, user=user.pk) feedback.judge.CopyFrom(judge_feedback.proto()) except MissionJudgeFeedback.DoesNotExist: team_eval.warnings.append('No MissionJudgeFeedback for team.') # Sanity check mission time. judge_mission_clock = ( feedback.judge.flight_time_sec + feedback.judge.post_process_time_sec) if abs(feedback.mission_clock_time_sec - judge_mission_clock) > 30: team_eval.warnings.append( 'Mission clock differs between interop and judge.')
def create_data(self): """Create a basic sample dataset.""" self.user1 = User.objects.create_user('user1', '*****@*****.**', 'testpass') self.user1.save() self.user2 = User.objects.create_user('user2', '*****@*****.**', 'testpass') self.user2.save() # user1 is on mission event = MissionClockEvent(user=self.user1, team_on_clock=True, team_on_timeout=False) event.save() # user1 is flying event = TakeoffOrLandingEvent(user=self.user1, uas_in_air=True) event.save() # user2 has landed event = TakeoffOrLandingEvent(user=self.user2, uas_in_air=True) event.save() event = TakeoffOrLandingEvent(user=self.user2, uas_in_air=False) event.save() # user2 is active self.timestamp = timezone.now() gps = GpsPosition(latitude=38.6462, longitude=-76.2452) gps.save() pos = AerialPosition(gps_position=gps, altitude_msl=0) pos.save() self.telem = UasTelemetry(user=self.user2, uas_position=pos, uas_heading=90) self.telem.save() self.telem.timestamp = dateutil.parser.parse( u'2016-10-01T00:00:00.0+00:00') self.telem.save()