I am creating an inertial sensors application where the values from three axis of gyroscope will be displayed on a line chart. I am using this https://github.com/danielgindi/Charts library for the chart implementation.
I have managed to show the values on the chart but the chart stops behaving normally say after 4,5 seconds and does not show the values correctly. Sometimes just the flat line even when i am moving the device and sometimes spikes in the data even when the device is stationary.
Could anyone please let me know where i am making a mistake. Thanks
Here is the code i am using.
import UIKit
import Charts
import CoreMotion
class ViewController: UIViewController ,ChartViewDelegate{
		@IBOutlet var lineChartView: LineChartView!
		
		 var motion = CMMotionManager()
		var xAxisArray : [String]?
		var yAxisArray : [Double]?
		var date : NSDate?
		var dateFormatter : DateFormatter?
		
		
		override func viewDidLoad() {
				super.viewDidLoad()
				myGyroscope()
				//charts
				self.lineChartView.delegate = self
				let set_a: LineChartDataSet = LineChartDataSet(entries:[ChartDataEntry(x: Double(0), y: Double(0))], label: "x")
				set_a.drawCirclesEnabled = false
				set_a.setColor(UIColor.blue)
				let set_b: LineChartDataSet = LineChartDataSet(entries: [ChartDataEntry(x: Double(0), y: Double(0))], label: "y")
				set_b.drawCirclesEnabled = false
				set_b.setColor(UIColor.green)
				
				let set_c: LineChartDataSet = LineChartDataSet(entries: [ChartDataEntry(x: Double(0), y: Double(0))], label: "z")
				set_c.drawCirclesEnabled = false
				set_c.setColor(UIColor.red)
				self.lineChartView.data = LineChartData(dataSets: [set_a,set_b,set_c])
				
				
				let leftAxis = lineChartView.leftAxis
				leftAxis.labelTextColor = UIColor(red: 51/255, green: 181/255, blue: 229/255, alpha: 1)
				leftAxis.axisMaximum = 10
				leftAxis.axisMinimum = -10
				leftAxis.drawGridLinesEnabled = true
				leftAxis.granularityEnabled = true
				
				lineChartView.animate(xAxisDuration: -1)
				
		}
		
		// add point
		var i = 0
		@objc func updateCounter(x:Double,y:Double,z:Double) {
				
				self.lineChartView.data?.addEntry(ChartDataEntry(x: Double(i), y:x), dataSetIndex: 0)
				 self.lineChartView.data?.addEntry(ChartDataEntry(x: Double(i), y:y ), dataSetIndex: 1)
				 self.lineChartView.data?.addEntry(ChartDataEntry(x: Double(i), y:z), dataSetIndex: 2)
				self.lineChartView.setVisibleXRange(minXRange: Double(1), maxXRange: Double(50))
				self.lineChartView.notifyDataSetChanged()
				self.lineChartView.moveViewToX(Double(i))
		}
		
		func myGyroscope() {
				motion.startGyroUpdates(to: OperationQueue.current!) { (data, error) in
						
						if let trueData = data {
								self.view.reloadInputViews()
		
								let x = trueData.rotationRate.x
								let y = trueData.rotationRate.y
								let z = trueData.rotationRate.z
								self.i += 1
								self.updateCounter(x: x, y: y, z: z)
								
								
							
						}
				}
		}
		
}
Post
Replies
Boosts
Views
Activity
Hi everyone,
I am trying to make an application which will gather the inertial sensors data from iphone devices.
Could anyone please tell me if we can set the update interval of every sensor independently to each other or they should be the same for every sensor at one time. ?
Thanks