aboutsummaryrefslogtreecommitdiff
path: root/ios/WALT/DragLatencyController.mm
blob: 5b6b9b4a811bd82152b700145aa3983a6942b4ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * 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 <dispatch/dispatch.h>
#import <math.h>
#import <numeric>
#import <vector>

#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 <typename T>
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 <typename S, typename T>
static std::vector<T> Interpolate(const std::vector<S>& x,
                                  const std::vector<S>& xp,
                                  const std::vector<T>& 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<T> 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<double>(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 <typename S, typename T>
static std::vector<S> Extract(const std::vector<T>& x, const std::vector<S>& y, const T& value) {
  NSCAssert(x.size() == y.size(), @"x and y must have matching lengths.");
  std::vector<S> 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 <typename T>
static T StandardDeviation(const std::vector<T>& 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 <typename T>
static size_t ArgMin(const std::vector<T>& 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<NSTimeInterval>& laserTs,
                                    const std::vector<NSTimeInterval>& touchTs,
                                    const std::vector<CGFloat>& 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<NSTimeInterval> deviations;
  deviations.reserve(kSteps);
  
  std::vector<NSTimeInterval> 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<CGFloat> 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<WALTTouch *> *_touchEvents;
  NSMutableArray<WALTLaserEvent *> *_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<UITouch *> *)touches withEvent:(UIEvent *)event {
  [self processEvent:event];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  [self processEvent:event];
}

- (void)touchesEnded:(NSSet<UITouch *> *)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<WALTTouch *> alloc] init];
  _laserEvents = [[NSMutableArray<WALTLaserEvent *> 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<NSTimeInterval> ft(_touchEvents.count);
  std::vector<CGFloat> 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<NSString *, id> *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<NSTimeInterval> lt(_laserEvents.count);
  std::vector<int> 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<CGFloat> 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<int> 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<NSTimeInterval> 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