Skip to content

Commit

Permalink
Code for the following youtube video tutorial
Browse files Browse the repository at this point in the history
https://www.youtube.com/watch?v=O68O7yUK_9c

All credit to the original youtube author, I’m just providing a source
for the code.
  • Loading branch information
loldi authored and loldi committed Nov 29, 2015
0 parents commit 2501566
Showing 1 changed file with 154 additions and 0 deletions.
154 changes: 154 additions & 0 deletions Acc-RotData/ViewController.swift
@@ -0,0 +1,154 @@
//
// ViewController.swift
// Stumble
//
// Created by Andrew Genualdi on 11/22/15.
// Copyright © 2015 Andrew Genualdi. All rights reserved.
//

import UIKit
import MapKit
import CoreLocation
import CoreMotion

class ViewController: UIViewController{

//Instance Variables

var currentMaxAccelX: Double = 0.0
var currentMaxAccelY: Double = 0.0
var currentMaxAccelZ: Double = 0.0

var currentMaxRotX: Double = 0.0
var currentMaxRotY: Double = 0.0
var currentMaxRotZ: Double = 0.0

var movementManager = CMMotionManager()

//Outlets

@IBOutlet var accX: UILabel!
@IBOutlet var accY: UILabel!
@IBOutlet var accZ: UILabel!
@IBOutlet var maxAccX: UILabel!
@IBOutlet var maxAccY: UILabel!
@IBOutlet var maxAccZ: UILabel!

@IBOutlet var rotX: UILabel!
@IBOutlet var rotY: UILabel!
@IBOutlet var rotZ: UILabel!
@IBOutlet var maxRotX: UILabel!
@IBOutlet var maxRotY: UILabel!
@IBOutlet var maxRotZ: UILabel!


@IBAction func resetMaxValues(sender: AnyObject) {


currentMaxAccelX = 0
currentMaxAccelY = 0
currentMaxAccelZ = 0

currentMaxRotX = 0
currentMaxRotY = 0
currentMaxRotZ = 0
}

override func viewDidLoad() {

currentMaxAccelX = 0
currentMaxAccelY = 0
currentMaxAccelZ = 0

currentMaxRotX = 0
currentMaxRotY = 0
currentMaxRotZ = 0

movementManager.gyroUpdateInterval = 0.2
movementManager.accelerometerUpdateInterval = 0.2

//Start Recording Data

movementManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue()!) { (accelerometerData: CMAccelerometerData?, NSError) -> Void in

self.outputAccData(accelerometerData!.acceleration)
if(NSError != nil) {
print("\(NSError)")
}
}

movementManager.startGyroUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler: { (gyroData: CMGyroData?, NSError) -> Void in
self.outputRotData(gyroData!.rotationRate)
if (NSError != nil){
print("\(NSError)")
}


})


This comment has been minimized.

Copy link
@nemr

nemr Nov 4, 2016

The following supports swift 3

motionManager.startAccelerometerUpdates(to: OperationQueue.current!) { (accelerometerData: CMAccelerometerData?, NSError) -> Void in
            
            self.outputAccData(acceleration: accelerometerData!.acceleration)
            if(NSError != nil) {
                print("\(NSError)")
            }
        }
        
        motionManager.startGyroUpdates(to: OperationQueue.current!, withHandler: { (gyroData: CMGyroData?, NSError) -> Void in
            self.outputRotData(rotation: gyroData!.rotationRate)
            if (NSError != nil){
                print("\(NSError)")
            }


}

func outputAccData(acceleration: CMAcceleration){

accX?.text = "\(acceleration.x).2fg"
if fabs(acceleration.x) > fabs(currentMaxAccelX)
{
currentMaxAccelX = acceleration.x
}

accY?.text = "\(acceleration.y).2fg"
if fabs(acceleration.y) > fabs(currentMaxAccelY)
{
currentMaxAccelY = acceleration.y
}

accZ?.text = "\(acceleration.z).2fg"
if fabs(acceleration.z) > fabs(currentMaxAccelZ)
{
currentMaxAccelZ = acceleration.z
}


maxAccX?.text = "\(currentMaxAccelX).2f"
maxAccY?.text = "\(currentMaxAccelY).2f"
maxAccZ?.text = "\(currentMaxAccelZ).2f"


}

func outputRotData(rotation: CMRotationRate){


rotX?.text = "\(rotation.x).2fr/s"
if fabs(rotation.x) > fabs(currentMaxRotX)
{
currentMaxRotX = rotation.x
}

rotY?.text = "\(rotation.y).2fr/s"
if fabs(rotation.y) > fabs(currentMaxRotY)
{
currentMaxRotY = rotation.y
}

rotZ?.text = "\(rotation.z).2fr/s"
if fabs(rotation.z) > fabs(currentMaxRotZ)
{
currentMaxRotZ = rotation.z
}




maxRotX?.text = "\(currentMaxRotX).2f"
maxRotY?.text = "\(currentMaxRotY).2f"
maxRotZ?.text = "\(currentMaxRotZ).2f"



}

}

0 comments on commit 2501566

Please sign in to comment.