Get Moving with Core Motion

27
Get Moving With Core Mo/on! h1p://www.slideshare.net/jblocksom/coremo/onpresenta/on Jonathan Blocksom Tuesday Sept 28 2010 2:00 – 3:15 PM

description

Slides for "Get Your iOS 4 Apps Moving with Core Motion" talk, September 2010 at iPhoneDevCon. Thanks everyone who came to the talk!

Transcript of Get Moving with Core Motion

Page 1: Get Moving with Core Motion

Get  Moving  With  Core  Mo/on!  

h1p://www.slideshare.net/jblocksom/core-­‐mo/on-­‐presenta/on  

Jonathan  Blocksom  Tuesday  Sept  28  2010  2:00  –  3:15  PM  

Page 2: Get Moving with Core Motion

Agenda  

•  Intro  •  SDK  •  Experiments  

•  Q  &  A  

•  Intro  level,  but  lots  of  code  

Page 3: Get Moving with Core Motion

About  Me  

Jonathan  Blocksom  [email protected]  augmentedjonathan.tumblr.com  @jblocksom  

•  Computer  Vision  So[ware  Engineer,  SET  Corp  

•  Computer  Graphics  background  •  Several  years  working  with  Remote  

Sensing  

Page 4: Get Moving with Core Motion

Meet  Core  Mo/on!  

•  Core  Mo/on  Framework:  High  level  interface  to  the    orienta/on  and  movement    data  from  the  device  

•  Based  on  Accelerometer  and  Gyroscopes  

•  Accelera/on,  Gravity,  Rota/onal  Accelera/on  •  High  level  filtering  

Page 5: Get Moving with Core Motion

Core  Mo/on  isn’t…  

•  For  GPS  data  – That’s  Core  Loca/on  (Loca/onManager)  

•  For  Compass  data  – Also  Core  Loca/on  

•  High  Level  Mo/on  Events  – Orienta/on  change  – Shake  events  

•  Perfect  

Page 6: Get Moving with Core Motion

Quick  Demo  

•  CoreMo/onTeapot  (WWDC  2010  sample  code)  

Page 7: Get Moving with Core Motion

Accelerometer  

•  Available  since  iPhone  1  •  Consider  it  a  “Gravity  Detector”  •  Noisy  

•  iPad,  iPhone  4:  STMicro  STM33DH  3-­‐axis  accelerometer  

Photo  by  David  Hodson  h1p://www.ifixit.com/Guide/Image/meta/IP1qEpYFQSSqwSbg  

Page 8: Get Moving with Core Motion

Gyroscopes  

•  New  in  iPhone  4,  iPod  Touch  4  

•  Detects  rota/on  rate  along  three  axis  

•  Chip  datasheet:  h1p://www.st.com/stonline/products/literature/ds/17116.pdf  

•  200/500/2500  dps  (degrees  per  second)  

•  0.03  dps  error  

Page 9: Get Moving with Core Motion

Gyro  Teardown  

h1p://www.ifixit.com/Teardown/iPhone-­‐4-­‐Gyroscope-­‐Teardown/3156/1  

Andrew  Bookholt:  

h1p://www.ifixit.com/Guide/Image/meta/UurGsklhtJRW2uuB  (le[)  h1p://www.ifixit.com/Guide/Image/meta/MeunHawo54qPRY5S  (right  1)  

Miroslav  Djuric  h1p://www.ifixit.com/Guide/Image/meta/RUQLo2PP1oqVtVsD  (right  2)  

Page 10: Get Moving with Core Motion

Accelerometer,  Gyro  &  Device  Mo/on  

Accelerometer   Gyro  

Device  Mo/on  

x,  y,  z   yaw,  pitch,  roll  Gravity  User  accelera/on  Aptude  Rota/on  Rate  

Page 11: Get Moving with Core Motion

Supported  Devices  

iPhone  1   iPhone  3G  

iPhone  3GS  

iPhone  4   iPod  Touch    

iPod  Touch  4  

iPad  

Accelerometer  

Y   Y   Y   Y   Y   Y   Y  

Gyros   Y  

iOS  4   Y   Y   Y   Y  

If  we  must…  Yay!   No.  

iPhone  4  iPod  Touch  4  

iPhone  3GS  iPod  Touch  iPad  w/  iOS  4  

iPhone  1  iPad  iOS  3.x  

Page 12: Get Moving with Core Motion

Classes  Class   Descrip8on  

CMMo/onManager   The  Gateway  

CMAccelerometerData   Accelera/on  data  X,  Y,  and  Z  rela/ve  to  device  In  G’s  

CMAptude   Orienta/on  Roll,  Pitch,  Yaw  Rota/on  Matrix  Quaternion  

CMDeviceMo/on   Roll  up  of  sensors  Differen/ates  gravity  from  user  accelera/on  

CMGyroData   Rota/onal  Velocity  

CMLogItem   Timestamped  Measurement  

Parent  Class  for  measurements  

Page 13: Get Moving with Core Motion

CMMo/onManager  

•  Tells  you  what’s  available  –  is<Acclerometer|DeviceMo/on|Gyro>Available  

•  Sets  the  update  intervals  –  set<…>UpdateInterval  

•  Starts  and  stops  updates  –  start<…>Updates  –  start<…>UpdatesToQueue:withHandler:  –  stop<…>Updates  

•  Lets  you  poll  for  updates  –  accelerometerData  –  gyroData  –  deviceMo/on  

Page 14: Get Moving with Core Motion

CMMo/onManager  

•  CMMo/onManager  *mgr  =  [[CMMo/onManager  alloc]  init];  

•  Ini/alizes  reference  frame  at  construc/on  

•  Only  make  one  of  these  for  your  app  

Page 15: Get Moving with Core Motion

Core  Mo/on  Coordinate  System  Quick  Reference  

yaw  

pitch  

roll  

x  

z  

y  

Accelerometer   Gyroscopes  

Page 16: Get Moving with Core Motion

CMAccelerometerData  

•  accelera/on  CMAccelera/on  struct:  typedef struct { double x; double y; double z; } CMAcceleration;

•  Measured  in  G’s

Page 17: Get Moving with Core Motion

CMGyroData  

•  YAGNI  

•  Gyro’s  have  bias;  DeviceMo/on  fusion  algorithms  remove  this  

•  Get  orienta/on  from  DeviceMo/on  instead  

Page 18: Get Moving with Core Motion

CMDeviceMo/on  

•  CMAptude  aptude  •  CMRota/onRate  rota/onRate  

•  CMAccelera/on  gravity  

•  CMAccelera/on  userAccelera/on  

Page 19: Get Moving with Core Motion

DEMO  

•  MacCMViewer  •  CMIPViewer  

•  Source  code  available:  h1p://bitbucket.org/jblocksom/coremo/onviewer  

Page 20: Get Moving with Core Motion

MacCMView:  What’s  Going  On?  

•  Mac  opens  socket;  iPhone  connects  •  iPhone  sends  CM  updates  to  Mac  

•  Mac  uses  rota/on  matrix  for  display  

From  CMAptude  

Page 21: Get Moving with Core Motion

CMAptude  

•  Rota/on  as  – Yaw,  Pitch,  Roll  – Quaternion  – Rota/on  Matrix  

•  mul/plyByInverseOfAptude:    

Page 22: Get Moving with Core Motion

Why  Quaternions?  

•  Great  technique  for  represen/ng  rota/ons  •  Mu/plying  q1  and  q2  is  composing  rota/ons  •  Can  easily  pull  out  axis  /  angle  •  Avoids  “Gimbal  Lock”  

•  h1p://en.wikipedia.org/wiki/Quaternions_and_spa/al_rota/on  is  a  pre1y  good  treatment  

•  Graphics  Gems  textbooks  

Page 23: Get Moving with Core Motion

mul/plyByInverseOfAptude:  

•  Teapot  Demo  revisited  

Page 24: Get Moving with Core Motion

Core  Mo/on  in  Simulator  

•  Accelerometer:  Not  available  •  Gyros:  Not  available  •  Device  Mo/on:  Not  available  

•  Workaround  thoughts…  

Page 25: Get Moving with Core Motion

Final  Steps:  App  Requirements  

•  Add  UIRequiredDeviceCapabili/es  for    accelerometer    gyro  

•  Not  needed  for  orienta/on  /  shake  events  

•  See  Event  Handling  Guide,  “Sepng  Required  Hardware  Capabili/es  for  Accelerometer  and  Gyroscope  Events”  

Page 26: Get Moving with Core Motion

Experiments  

•  iPhone  3GS  vs  iPhone  4  •  Inter/al  Navigator  •  Salad  Spinner  

Page 27: Get Moving with Core Motion

Further  Reading  

•  Event  Handling  Guide  for  iPhone  OS  – Look  under  “Mo/on  Events”  

– Sec/on  /tled  “Core  Mo/on”  

•  Core  Mo/on  framework  reference  

•  Sample  Code  – WWDC  ’10:  CoreMo/onTeapot  

•  iPhone  Sensor  Group?