/* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #import "DragLatencyController.h" #import #import #import #import #import "UIAlertView+Extensions.h" #import "WALTAppDelegate.h" #import "WALTClient.h" #import "WALTLogger.h" #import "WALTTouch.h" static const NSTimeInterval kGoalpostFrequency = 0.55; // TODO(pquinn): User-configurable settings. static const NSUInteger kMinTouchEvents = 100; static const NSUInteger kMinLaserEvents = 8; static const char kWALTLaserTag = 'L'; @interface WALTLaserEvent : NSObject @property (assign) NSTimeInterval t; @property (assign) int value; @end @implementation WALTLaserEvent @end /** Linear interpolation between x0 and x1 at alpha. */ template static T Lerp(const T& x0, const T& x1, double alpha) { NSCAssert(alpha >= 0 && alpha <= 1, @"alpha must be between 0 and 1 (%f)", alpha); return ((1 - alpha) * x0) + (alpha * x1); } /** Linear interpolation of (xp, yp) at x. */ template static std::vector Interpolate(const std::vector& x, const std::vector& xp, const std::vector& yp) { NSCAssert(xp.size(), @"xp must contain at least one value."); NSCAssert(xp.size() == yp.size(), @"xp and yp must have matching lengths."); std::vector y; y.reserve(x.size()); size_t i = 0; // Index into x. for (; i < x.size() && x[i] < xp.front(); ++i) { y.push_back(yp.front()); // Pad out y with yp.front() for x values before xp.front(). } size_t ip = 0; // Index into xp/yp. for (; ip < xp.size() && i < x.size(); ++i) { while (ip < xp.size() && xp[ip] <= x[i]) { // Find an xp[ip] greater than x[i]. ++ip; } if (ip >= xp.size()) { break; // Ran out of values. } const double alpha = (x[i] - xp[ip - 1]) / static_cast(xp[ip] - xp[ip - 1]); y.push_back(Lerp(yp[ip - 1], yp[ip], alpha)); } for (; i < x.size(); ++i) { y.push_back(yp.back()); // Pad out y with yp.back() for values after xp.back(). } return y; } /** Extracts the values of y where the corresponding value in x is equal to value. */ template static std::vector Extract(const std::vector& x, const std::vector& y, const T& value) { NSCAssert(x.size() == y.size(), @"x and y must have matching lengths."); std::vector extracted; for (size_t i = 0; i < x.size(); ++i) { if (x[i] == value) { extracted.push_back(y[i]); } } return extracted; } /** Returns the standard deviation of the values in x. */ template static T StandardDeviation(const std::vector& x) { NSCAssert(x.size() > 0, @"x must have at least one value."); const T sum = std::accumulate(x.begin(), x.end(), T{}); const T mean = sum / x.size(); const T ss = std::accumulate(x.begin(), x.end(), T{}, ^(T accum, T value){ return accum + ((value - mean) * (value - mean)); }); return sqrt(ss / (x.size() - 1)); } /** Returns the index of the smallest value in x. */ template static size_t ArgMin(const std::vector& x) { NSCAssert(x.size() > 0, @"x must have at least one value."); size_t imin = 0; for (size_t i = 1; i < x.size(); ++i) { if (x[i] < x[imin]) { imin = i; } } return imin; } /** * Finds a positive time value that shifting laserTs by will minimise the standard deviation of * interpolated touchYs. */ static NSTimeInterval FindBestShift(const std::vector& laserTs, const std::vector& touchTs, const std::vector& touchYs) { NSCAssert(laserTs.size() > 0, @"laserTs must have at least one value."); NSCAssert(touchTs.size() == touchYs.size(), @"touchTs and touchYs must have matching lengths."); const NSTimeInterval kSearchCoverage = 0.15; const int kSteps = 1500; const NSTimeInterval kShiftStep = kSearchCoverage / kSteps; std::vector deviations; deviations.reserve(kSteps); std::vector ts(laserTs.size()); for (int i = 0; i < kSteps; ++i) { for (size_t j = 0; j < laserTs.size(); ++j) { ts[j] = laserTs[j] + (kShiftStep * i); } std::vector laserYs = Interpolate(ts, touchTs, touchYs); deviations.push_back(StandardDeviation(laserYs)); } return ArgMin(deviations) * kShiftStep; } @interface DragLatencyController () - (void)updateCountDisplay; - (void)processEvent:(UIEvent *)event; - (void)receiveTriggers:(id)context; - (void)stopReceiver; @end @implementation DragLatencyController { WALTClient *_client; WALTLogger *_logger; NSMutableArray *_touchEvents; NSMutableArray *_laserEvents; NSThread *_triggerReceiver; dispatch_semaphore_t _receiverComplete; } - (void)dealloc { [self stopReceiver]; } - (void)viewDidLoad { [super viewDidLoad]; _client = ((WALTAppDelegate *)[UIApplication sharedApplication].delegate).client; _logger = [WALTLogger sessionLogger]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self updateCountDisplay]; [_logger appendString:@"DRAGLATENCY\n"]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self processEvent:event]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [self processEvent:event]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self processEvent:event]; } - (void)processEvent:(UIEvent *)event { // TODO(pquinn): Pull out coalesced touches. WALTTouch *touch = [[WALTTouch alloc] initWithEvent:event]; [_touchEvents addObject:touch]; [_logger appendFormat:@"TOUCH\t%.3f\t%.2f\t%.2f\n", touch.kernelTime, touch.location.x, touch.location.y]; [self updateCountDisplay]; } - (void)updateCountDisplay { NSString *counts = [NSString stringWithFormat:@"N ✛ %lu ⇄ %lu", (unsigned long)_laserEvents.count, (unsigned long)_touchEvents.count]; self.countLabel.text = counts; } - (IBAction)start:(id)sender { [self reset:sender]; self.goalpostView.hidden = NO; self.statusLabel.text = @""; [UIView beginAnimations:@"Goalpost" context:NULL]; [UIView setAnimationDuration:kGoalpostFrequency]; [UIView setAnimationBeginsFromCurrentState:NO]; [UIView setAnimationRepeatCount:FLT_MAX]; [UIView setAnimationRepeatAutoreverses:YES]; self.goalpostView.transform = CGAffineTransformMakeTranslation(0.0, -CGRectGetHeight(self.view.frame) + 300); [UIView commitAnimations]; _receiverComplete = dispatch_semaphore_create(0); _triggerReceiver = [[NSThread alloc] initWithTarget:self selector:@selector(receiveTriggers:) object:nil]; [_triggerReceiver start]; } - (IBAction)reset:(id)sender { [self stopReceiver]; self.goalpostView.transform = CGAffineTransformMakeTranslation(0.0, 0.0); self.goalpostView.hidden = YES; _touchEvents = [[NSMutableArray alloc] init]; _laserEvents = [[NSMutableArray alloc] init]; [self updateCountDisplay]; NSError *error = nil; if (![_client syncClocksWithError:&error]) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"WALT Connection Error" error:error]; [alert show]; } [_logger appendString:@"RESET\n"]; } - (void)receiveTriggers:(id)context { // Turn on laser change notifications. NSError *error = nil; if (![_client sendCommand:WALTLaserOnCommand error:&error]) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"WALT Connection Error" error:error]; [alert show]; dispatch_semaphore_signal(_receiverComplete); return; } NSData *response = [_client readResponseWithTimeout:kWALTReadTimeout]; if (![_client checkResponse:response forCommand:WALTLaserOnCommand]) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"WALT Response Error" message:@"Failed to start laser probe." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; [alert show]; dispatch_semaphore_signal(_receiverComplete); return; } while (!NSThread.currentThread.isCancelled) { WALTTrigger response = [_client readTriggerWithTimeout:kWALTReadTimeout]; if (response.tag == kWALTLaserTag) { WALTLaserEvent *event = [[WALTLaserEvent alloc] init]; event.t = response.t; event.value = response.value; [_laserEvents addObject:event]; [_logger appendFormat:@"LASER\t%.3f\t%d\n", event.t, event.value]; } else if (response.tag != '\0') { // Don't fail for timeout errors. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"WALT Response Error" message:@"Failed to read laser probe." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; [alert show]; } } // Turn off laser change notifications. [_client sendCommand:WALTLaserOffCommand error:nil]; [_client readResponseWithTimeout:kWALTReadTimeout]; dispatch_semaphore_signal(_receiverComplete); } - (void)stopReceiver { // TODO(pquinn): This will deadlock if called in rapid succession -- there is a small delay // between dispatch_semaphore_signal() and -[NSThread isExecuting] changing. // Unfortunately, NSThread is not joinable... if (_triggerReceiver.isExecuting) { [_triggerReceiver cancel]; dispatch_semaphore_wait(_receiverComplete, DISPATCH_TIME_FOREVER); } } - (IBAction)computeStatistics:(id)sender { if (_touchEvents.count < kMinTouchEvents) { self.statusLabel.text = [NSString stringWithFormat:@"Too few touch events (%lu/%lu).", (unsigned long)_touchEvents.count, (unsigned long)kMinTouchEvents]; [self reset:sender]; return; } // Timestamps are reset to be relative to t0 to make the output easier to read. const NSTimeInterval t0 = _touchEvents.firstObject.kernelTime; const NSTimeInterval tF = _touchEvents.lastObject.kernelTime; std::vector ft(_touchEvents.count); std::vector fy(_touchEvents.count); for (NSUInteger i = 0; i < _touchEvents.count; ++i) { ft[i] = _touchEvents[i].kernelTime - t0; fy[i] = _touchEvents[i].location.y; } // Remove laser events that have a timestamp outside [t0, tF]. [_laserEvents filterUsingPredicate:[NSPredicate predicateWithBlock: ^BOOL(WALTLaserEvent *evaluatedObject, NSDictionary *bindings) { return evaluatedObject.t >= t0 && evaluatedObject.t <= tF; }]]; if (_laserEvents.count < kMinLaserEvents) { self.statusLabel.text = [NSString stringWithFormat:@"Too few laser events (%lu/%lu).", (unsigned long)_laserEvents.count, (unsigned long)kMinLaserEvents]; [self reset:sender]; return; } if (_laserEvents.firstObject.value != 0) { self.statusLabel.text = @"First laser crossing was not into the beam."; [self reset:sender]; return; } std::vector lt(_laserEvents.count); std::vector lv(_laserEvents.count); for (NSUInteger i = 0; i < _laserEvents.count; ++i) { lt[i] = _laserEvents[i].t - t0; lv[i] = _laserEvents[i].value; } // Calculate interpolated touch y positions at each laser event. std::vector ly = Interpolate(lt, ft, fy); // Labels for each laser event to denote those above/below the beam. // The actual side is irrelevant, but events on the same side should have the same label. The // vector will look like [0, 1, 1, 0, 0, 1, 1, 0, 0, ...]. std::vector sideLabels(lt.size()); for (size_t i = 0; i < lt.size(); ++i) { sideLabels[i] = ((i + 1) / 2) % 2; } NSTimeInterval averageBestShift = 0; for (int side = 0; side < 2; ++side) { std::vector lts = Extract(sideLabels, lt, side); NSTimeInterval bestShift = FindBestShift(lts, ft, fy); averageBestShift += bestShift / 2; } self.statusLabel.text = [NSString stringWithFormat:@"%.3f s", averageBestShift]; [self reset:sender]; } @end