-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy path17. Core Motion and Camera.srt
7249 lines (5789 loc) · 150 KB
/
17. Core Motion and Camera.srt
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1
00:00:00,401 --> 00:00:04,603
[MUSIC]
2
00:00:04,671 --> 00:00:07,906
Stanford University.
>> Well,
3
00:00:07,975 --> 00:00:12,477
welcome to lecture number
17 of Stanford CS193P,
4
00:00:12,546 --> 00:00:16,815
this is a fall of 2017. Today
we're gonna talk about some
5
00:00:16,883 --> 00:00:20,085
hardware specific APIs,
specifically Core Motion,
6
00:00:20,153 --> 00:00:23,555
which is the position of the
device in space, basically,
7
00:00:23,623 --> 00:00:26,391
as it moves around.
And then the camera, so
8
00:00:26,460 --> 00:00:31,496
we can capture images with
the camera. Now Core Motion
9
00:00:31,565 --> 00:00:34,233
is really a whole set of
APIs that go together.
10
00:00:34,301 --> 00:00:37,569
The primary inputs to Core
Motion are the accelerometer,
11
00:00:37,638 --> 00:00:40,505
almost all devices have
an accelerometer. The gyro,
12
00:00:40,574 --> 00:00:44,142
most newer devices have
a gyro. And the magnetometer,
13
00:00:44,210 --> 00:00:47,846
kind of similar to the gyro,
newer devices have all that.
14
00:00:47,915 --> 00:00:50,515
And you're gonna see a part
of the API we're really gonna
15
00:00:50,584 --> 00:00:53,017
wanna check and make sure
that the device we're on has
16
00:00:53,086 --> 00:00:56,988
the hardware we want before we
do it. Now the primary API to
17
00:00:57,057 --> 00:00:59,992
get at all this is this class
called CMMotionManager.
18
00:01:00,060 --> 00:01:01,460
There's some other classes
we're gonna talk about,
19
00:01:01,528 --> 00:01:04,796
but this is the primary one.
Now, you can create multiple
20
00:01:04,865 --> 00:01:07,131
instances of this thing,
but in a sense,
21
00:01:07,200 --> 00:01:09,834
there's only set of hardware.
So usually in your app,
22
00:01:09,903 --> 00:01:13,805
you're gonna have one
shared CMMotionManager to
23
00:01:13,874 --> 00:01:16,574
collect all the information
from these devices.
24
00:01:16,643 --> 00:01:19,511
Now, the way the API works,
basically, is first you're
25
00:01:19,580 --> 00:01:21,680
gonna check to see what
hardware is available,
26
00:01:21,749 --> 00:01:23,515
all right,
that's a very important check.
27
00:01:23,584 --> 00:01:26,618
Then you can either start
sampling, okay, just
28
00:01:26,687 --> 00:01:29,754
asking the CMMotionManager
what's the current state of
29
00:01:29,823 --> 00:01:31,857
the accelerometer? What's
the current state of gyro?
30
00:01:31,925 --> 00:01:34,593
What's the current state
of magnetometer? Or
31
00:01:34,661 --> 00:01:38,863
you can register a closure,
and a certain refresh rate,
32
00:01:38,932 --> 00:01:41,566
and it will execute your
closure at that refresh rate,
33
00:01:41,635 --> 00:01:44,703
as best it can anyway, and
tell you what the state is.
34
00:01:44,772 --> 00:01:47,505
So it's kind of like you can
either pull the data out
35
00:01:47,574 --> 00:01:50,341
of it, or it can push the data
to you. Really either
36
00:01:50,410 --> 00:01:52,977
model, and it kinda depends on
how your app works as to which
37
00:01:53,046 --> 00:01:55,980
of those is appropriate. So
I'm gonna talk about the API's
38
00:01:56,049 --> 00:01:58,350
for both of those things.
So let's do number one here,
39
00:01:58,419 --> 00:02:00,218
which checking the
availability of the sensors.
40
00:02:00,287 --> 00:02:02,720
Really, where you're
simply are just gonna get
41
00:02:02,789 --> 00:02:05,824
your CMMotionManager and then
you gonna ask questions like,
42
00:02:05,893 --> 00:02:08,360
accelerometer available,
is gyro available,
43
00:02:08,428 --> 00:02:11,095
magnetometer available? And
this is gonna return yes or
44
00:02:11,164 --> 00:02:12,931
no whether these
things are available.
45
00:02:13,000 --> 00:02:16,001
Notice that there's something
there called device motion,
46
00:02:16,070 --> 00:02:18,570
you see accelerometer,
gyro, magnetometer, and
47
00:02:18,638 --> 00:02:21,272
deviceMotion. So we're gonna
talk all about deviceMotion.
48
00:02:21,341 --> 00:02:25,009
It's essentially a combination
of all the other devices, and
49
00:02:25,078 --> 00:02:27,912
it allows us to get much
more accurate readings,
50
00:02:27,981 --> 00:02:31,716
more interesting readings, by
combining the accelerometer,
51
00:02:31,785 --> 00:02:35,253
gyro, and magnetometer. So the
way where you're going to pull
52
00:02:35,322 --> 00:02:38,256
the information, okay, not
gonna register a closure but
53
00:02:38,325 --> 00:02:40,425
you're just gonna ask for
as you need it.
54
00:02:40,494 --> 00:02:43,594
You do that by calling start
accelerometer updates, or
55
00:02:43,663 --> 00:02:46,832
start gyro updates and that's
gonna tell the hardware,
56
00:02:46,900 --> 00:02:49,567
tell iOS, hey,
l wanna get this information.
57
00:02:49,636 --> 00:02:52,804
So, if that particular piece
of hardware has to be powered
58
00:02:52,873 --> 00:02:55,773
up or something like that,
then iOS is gonna do that so
59
00:02:55,842 --> 00:02:58,076
that you can start
asking the value of it.
60
00:02:58,145 --> 00:03:02,380
So that's all you have to do
to start pulling the data.
61
00:03:02,449 --> 00:03:05,250
You can also find out if
the hardware is on and
62
00:03:05,318 --> 00:03:07,919
collecting data with
accelerometer active or
63
00:03:07,988 --> 00:03:12,290
gyro active VAR in
the CMMotionManager.
64
00:03:12,358 --> 00:03:14,860
Now a really important thing
to do when you're accessing
65
00:03:14,928 --> 00:03:18,530
this data from these hardware
devices is to turn it off,
66
00:03:18,599 --> 00:03:20,898
okay? The reason you
wanna turn it off is,
67
00:03:20,967 --> 00:03:22,567
it requires battery, okay?
68
00:03:22,636 --> 00:03:26,104
Who knows how much, it depends
on the piece of hardware, to
69
00:03:26,173 --> 00:03:28,473
do this, and you don't wanna
waste the user's battery.
70
00:03:28,542 --> 00:03:31,109
All right, so we definitely
wanna turn if off anytime
71
00:03:31,178 --> 00:03:33,244
we're not using it. Not like
just when our app quits or
72
00:03:33,313 --> 00:03:35,813
something, but actually any
time we are not actually gonna
73
00:03:35,882 --> 00:03:38,383
use the information, let's
turn that thing off, right,
74
00:03:38,451 --> 00:03:41,052
save some battery. All right,
so now let's talk a little
75
00:03:41,121 --> 00:03:43,621
bit about that pull interface
where you're just gonna
76
00:03:43,690 --> 00:03:46,091
ask for the current state
of this thing all the time.
77
00:03:46,160 --> 00:03:47,893
And the API is
very similar for
78
00:03:47,962 --> 00:03:50,729
all the devices, gyro,
accelerometer, and
79
00:03:50,797 --> 00:03:55,333
magnetometer, and for this
combined device motion thing.
80
00:03:55,401 --> 00:03:56,334
So, here's what
it looks like for
81
00:03:56,402 --> 00:03:58,971
accelerometer, it's just a VAR
in CMMotionManager called
82
00:03:59,039 --> 00:04:01,272
accelerometer data.
Gives you back a struct,
83
00:04:01,341 --> 00:04:02,974
it looks like this, x, y, and
84
00:04:03,043 --> 00:04:07,779
z. That is the acceleration
of the device in x, y, and z.
85
00:04:07,848 --> 00:04:11,382
So x is the axis that goes
across the device, okay, so
86
00:04:11,451 --> 00:04:15,019
if you're looking at the
device and the home button at
87
00:04:15,088 --> 00:04:18,556
the bottom and you're looking
at it. Then across is x,
88
00:04:18,625 --> 00:04:21,492
y is up and down through
the home button essentially,
89
00:04:21,561 --> 00:04:22,995
or you have an iPhone 10,
90
00:04:23,063 --> 00:04:25,863
through where the home
button used to be. And
91
00:04:25,932 --> 00:04:28,666
then z is down through
back of the device device.
92
00:04:28,735 --> 00:04:31,636
Okay, so if you had a device
sitting on a table flat,
93
00:04:31,705 --> 00:04:34,672
then z would be 1.0 here,
because this is in g, and
94
00:04:34,741 --> 00:04:38,142
the only acceleration that the
device would be experiencing
95
00:04:38,211 --> 00:04:40,678
is the acceleration
due to gravity, okay?
96
00:04:40,747 --> 00:04:42,347
Don't forget about
that acceleration,
97
00:04:42,416 --> 00:04:44,849
right, 9.8 meters per second
squared towards the center of
98
00:04:44,918 --> 00:04:47,052
the earth,
that's always happening. So
99
00:04:47,120 --> 00:04:49,754
if you then pick the device
up and kinda set it down so
100
00:04:49,823 --> 00:04:52,323
that the home button is on
the bottom, right, then your
101
00:04:52,392 --> 00:04:56,494
y would be experiencing 1 g,
and z and x would be 0,
102
00:04:56,563 --> 00:04:58,229
okay, cuz nothing would
be moving it. Now,
103
00:04:58,298 --> 00:05:00,999
if you took the device and you
shake it all around like this,
104
00:05:01,068 --> 00:05:03,502
then x, y, and z are gonna be
all kinds of random numbers.
105
00:05:03,570 --> 00:05:06,171
Could be much greater than
1 if you really jerk it,
106
00:05:06,240 --> 00:05:09,274
much faster acceleration and
the acceleration due to
107
00:05:09,342 --> 00:05:11,176
gravity, you could get
all kinds of numbers.
108
00:05:11,244 --> 00:05:13,945
All right, so this'll give
me the raw acceleration
109
00:05:14,014 --> 00:05:16,014
of the device in space,
including gravity.
110
00:05:17,284 --> 00:05:20,418
All right, the gyro,
so the gyro, we're
111
00:05:20,487 --> 00:05:23,221
almost never gonna ask for the
gyro information directly like
112
00:05:23,290 --> 00:05:25,891
this as you're gonna see in
a motion. But if you did, it's
113
00:05:25,959 --> 00:05:28,559
essentially gonna tell you
the rotation rate around those
114
00:05:28,628 --> 00:05:32,430
three axes I talk about in
radians per second. Okay, so
115
00:05:32,499 --> 00:05:35,834
it's basically how fast it's
rotating in these devices.
116
00:05:35,902 --> 00:05:38,937
Now the problem is,
is the gyro on its own can get
117
00:05:39,006 --> 00:05:42,374
bias introduced to it. It can
kind of drift a little bit,
118
00:05:42,443 --> 00:05:45,243
so the gyro works a lot
better in concert with
119
00:05:45,311 --> 00:05:47,946
the accelerometer,
as you'll see in a moment,
120
00:05:48,015 --> 00:05:50,582
and similarly with
magnetometer. Yeah, you can
121
00:05:50,651 --> 00:05:54,252
find out the magnetic field
around you but, you'd like to
122
00:05:54,321 --> 00:05:56,988
know what, how much of that
is earth's magnetic field,
123
00:05:57,057 --> 00:05:59,524
so you can find out where the
north is, versus how much is
124
00:05:59,593 --> 00:06:02,393
just local interference
from devices. And so,
125
00:06:02,462 --> 00:06:05,330
we don't usually access the
magnetometer directly either.
126
00:06:05,399 --> 00:06:09,333
Instead, in both cases,
we access this pseudo device
127
00:06:09,402 --> 00:06:12,804
called deviceMotion. Okay, and
so, it's similar to the other
128
00:06:12,873 --> 00:06:15,173
ones, it has var, this one's
called deviceMotion, and
129
00:06:15,242 --> 00:06:18,176
it gives you back this
deviceMotion struct. Now,
130
00:06:18,245 --> 00:06:19,877
the deviceMotion
struct is giving you
131
00:06:19,946 --> 00:06:21,145
a lot of the same
information that you're
132
00:06:21,214 --> 00:06:23,214
getting from these
other devices, but
133
00:06:23,283 --> 00:06:26,885
it's combining the devices to
give you better information.
134
00:06:26,953 --> 00:06:29,354
For example, let's look
at acceleration. So,
135
00:06:29,422 --> 00:06:31,222
when you have a device and
you're looking at
136
00:06:31,290 --> 00:06:34,192
the accelerometer, you don't
really know how much of that
137
00:06:34,261 --> 00:06:36,194
is due to the acceleration
due to gravity, and
138
00:06:36,262 --> 00:06:38,963
how much is because the user
is moving the device around.
139
00:06:39,032 --> 00:06:42,700
Right, well, but if you have
the gyro, now you do know
140
00:06:42,769 --> 00:06:45,837
how much the user is moving
the device around, okay.
141
00:06:45,906 --> 00:06:48,306
So, if you combine the two,
you can factor
142
00:06:48,375 --> 00:06:50,108
out the acceleration
that's due to gravity and
143
00:06:50,177 --> 00:06:53,678
the acceleration that's the
actual user moving the device.
144
00:06:53,747 --> 00:06:56,314
So in CMMotion,
you have the gravity which
145
00:06:56,383 --> 00:06:59,251
is a CM acceleration, and you
also have user acceleration,
146
00:06:59,319 --> 00:07:01,319
and they're separated out.
Okay, so
147
00:07:01,388 --> 00:07:03,955
you see how it's used the gyro
to give you more information
148
00:07:04,024 --> 00:07:07,425
about acceleration, and
similarly for the gyro, okay?
149
00:07:07,494 --> 00:07:10,594
If you have this gyro, you can
remove the bias that happens
150
00:07:10,663 --> 00:07:13,298
with the gyro because you have
the accelerometer information
151
00:07:13,367 --> 00:07:16,034
that tells you where
this thing is being
152
00:07:16,103 --> 00:07:19,971
moved around as it's being
rotated. So, there you can
153
00:07:20,039 --> 00:07:22,807
get rotation rate in the same
way as you got before, but
154
00:07:22,876 --> 00:07:25,276
you can also get much more
interesting in things like
155
00:07:25,345 --> 00:07:27,912
roll, pitch, and yaw, okay?
So if you know anything about
156
00:07:27,981 --> 00:07:31,315
flying, if you have an
airplane that's flying along,
157
00:07:31,384 --> 00:07:35,586
the roll is when the airplane
tilts it's wings side to side,
158
00:07:35,655 --> 00:07:38,289
right, that's the roll. Pitch
is when the pitches is nose
159
00:07:38,357 --> 00:07:41,292
up or down, right, that's
the pitch. And the yaw is when
160
00:07:41,361 --> 00:07:44,929
it kinda turns into the wind
in the same plane parallel
161
00:07:44,998 --> 00:07:48,466
to the ground, right. So you
can get that same roll, pitch,
162
00:07:48,535 --> 00:07:52,036
and yaw in your device, which
is really kind of a cool way
163
00:07:52,105 --> 00:07:53,938
to think about your devices,
164
00:07:54,007 --> 00:07:57,876
what your device is doing. You
can also get heading, okay,
165
00:07:57,944 --> 00:08:00,712
now it needs the magnetometer
for that, obviously,
166
00:08:00,781 --> 00:08:02,780
to do the heading. And this is
gonna give you the heading.
167
00:08:02,849 --> 00:08:04,548
And heading is sometimes
interesting to
168
00:08:04,617 --> 00:08:09,220
have if you have, for example,
an, augmented reality app, or
169
00:08:09,289 --> 00:08:12,223
something like that, where you
wanna know the gyro position
170
00:08:12,291 --> 00:08:13,591
as you're moving
this thing around.
171
00:08:13,660 --> 00:08:15,126
But you also know where
you are in the world.
172
00:08:15,194 --> 00:08:18,730
Are you facing north?
Where are you facing?
173
00:08:18,799 --> 00:08:20,431
So, we can give you that
information, as well.
174
00:08:20,500 --> 00:08:24,235
So, CMDeviceMotion is probably
the primary way we get
175
00:08:24,303 --> 00:08:27,472
information out of core
motion, when we want gyro,
176
00:08:27,541 --> 00:08:29,374
accelerometer, magnetometer,
177
00:08:29,442 --> 00:08:32,743
because it combines them
very intelligently.
178
00:08:32,812 --> 00:08:35,813
Okay, now when you're getting
device motion you can actually
179
00:08:35,882 --> 00:08:40,251
control, which of the devices,
accelerometer, gyro and
180
00:08:40,320 --> 00:08:43,154
magnetometer are used,
specifically the magnetometer,
181
00:08:43,222 --> 00:08:46,157
okay? There's a couple of
reference frames and reference
182
00:08:46,226 --> 00:08:49,627
frame is just an argument when
you start getting the updates,
183
00:08:49,695 --> 00:08:53,030
from the device motion. So,
there's two reference frames
184
00:08:53,099 --> 00:08:56,467
called xArbitraryZVertical and
xArbitraryCorrected ZVertical
185
00:08:56,536 --> 00:08:58,770
which either don't use
the magnetometer, or
186
00:08:58,839 --> 00:09:01,339
can work without the,
magnetometer,
187
00:09:01,408 --> 00:09:04,108
I believe they can do.
And so, they're not,
188
00:09:04,177 --> 00:09:06,744
they're really not used for
a reference frame when,
189
00:09:06,813 --> 00:09:09,413
when you wanna know where
your device is in the world,
190
00:09:09,482 --> 00:09:13,852
okay? So, there's two other
ones. xMagnetic/TrueNorth and
191
00:09:13,920 --> 00:09:17,622
xMagneticTrue NorthZVertical.
And
192
00:09:17,691 --> 00:09:20,124
those use the magnetometer.
Now, those actually will
193
00:09:20,193 --> 00:09:22,560
require the magnetometer
to be calibrated,
194
00:09:22,629 --> 00:09:25,329
and, and ready to use, so,
you know, you don't wanna
195
00:09:25,398 --> 00:09:28,032
use these reference frames
lightly, but if you need to
196
00:09:28,101 --> 00:09:30,902
know where you are then you
need this. Obviously, for
197
00:09:30,970 --> 00:09:33,371
true north it not only needs
to know all the information
198
00:09:33,440 --> 00:09:35,940
that we talked about so far,
but it also needs to know your
199
00:09:36,009 --> 00:09:37,775
location, where you
are on the planet.
200
00:09:37,844 --> 00:09:40,077
So, it can tell the difference
between magnetic north,
201
00:09:40,146 --> 00:09:42,647
which is the only thing it can
measure, and true north, okay?
202