<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Cornelis van der Bent on Medium]]></title>
        <description><![CDATA[Stories by Cornelis van der Bent on Medium]]></description>
        <link>https://medium.com/@cornelis_51044?source=rss-8061a8b44810------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jCQGSKZ9je4cHcpkrA1QGw.jpeg</url>
            <title>Stories by Cornelis van der Bent on Medium</title>
            <link>https://medium.com/@cornelis_51044?source=rss-8061a8b44810------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 18:41:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cornelis_51044/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Measuring iPhone Rotation Speed Using Quaternions]]></title>
            <link>https://medium.com/@cornelis_51044/measuring-iphone-rotation-using-quaternions-19c29b4396a6?source=rss-8061a8b44810------2</link>
            <guid isPermaLink="false">https://medium.com/p/19c29b4396a6</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[quaternion]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swiftui]]></category>
            <dc:creator><![CDATA[Cornelis van der Bent]]></dc:creator>
            <pubDate>Wed, 28 Aug 2024 11:30:08 GMT</pubDate>
            <atom:updated>2024-08-29T09:18:11.276Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/719/1*GG2lMwxgPqgIKQgUA0OcGA.png" /></figure><p>Wanted to make an app that showed average rotation of an iOS device.</p><p>I’ve done averaging over time earlier: Just have a fixed sample rate and run the measurements through a low-pass (LP) filter.</p><p>But how to get the total device rotation? Of course had to use gyroscope output: device attitude.</p><p>iOS’ <a href="https://developer.apple.com/documentation/coremotion/cmattitude">CMAttitude</a> provides three representations of attitude: a rotation matrix, a quaternion, and Euler angles (roll, pitch, and yaw values).</p><p>Decided to give the ‘scary quaternions’ a go and started reading on Wikipedia. The <a href="https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Definition">norm of a quaternion</a> gave me the idea I was looking for.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/527/1*8bEgrcJUJbiufzDYLfRlYg.png" /></figure><p>A quaternion can be seen as a 4D ‘vector’. When the device is rotated, this ‘vector’ changes direction. The distance between two subsequent ‘vector’ endpoints seemed a good enough measure of rotation. Dividing this distance by the time between the two samples gives rotation speed.</p><p>When sampling at fixed interval gives a stream of raw rotation speed values which can then be filtered to show a smooth UI effect.</p><p>I tried it out. Turned out in addition to a LP filter, I also needed a <a href="https://en.wikipedia.org/wiki/Moving_average#Simple_moving_average">Simple Moving Average (SMA)</a> filter for smooth visual results.</p><pre>let sampleRate = 45.0<br>motionManager.deviceMotionUpdateInterval = 1 / sampleRate<br>motionManager.startDeviceMotionUpdates()<br><br>lpFilter = LowPassFilter(sampleRate: sampleRate, cutOffFrequency: 0.3)<br>smaFilter = SimpleMovingAverageFilter(sampleRate: sampleRate, period: 4)<br>Timer.scheduledTimer(withTimeInterval: 1 / sampleRate, repeats: true)<br>{ timer in<br>    guard let attitude = self.motionManager.deviceMotion?.attitude else { return }<br>    if self.previousQuaternion == nil<br>    {<br>        self.previousQuaternion = attitude.quaternion<br>    }<br><br>    // Dynamic algorithm which calculates travelled distance.<br>    var currentQuaternion: CMQuaternion<br>    var distance: Double<br><br>    currentQuaternion = attitude.quaternion<br><br>    distance = sqrt(pow(currentQuaternion.x - self.previousQuaternion.x, 2) +<br>                    pow(currentQuaternion.y - self.previousQuaternion.y, 2) +<br>                    pow(currentQuaternion.z - self.previousQuaternion.z, 2) +<br>                    pow(currentQuaternion.w - self.previousQuaternion.w, 2));<br><br>    self.previousQuaternion = currentQuaternion;<br><br>    let filteredDistance = self.lpFilter.addSample(sample: self.smaFilter.addSample(sample: distance))<br>    self.subject.send(filteredDistance * 10)<br>}</pre><p>I showed the output on screen using one of my first SwiftUI experiments of which the oldish <a href="https://github.com/meaning-matters/Rotation">code is here.</a></p><p>If you liked this article let others and me know; thanks!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19c29b4396a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>