[iOS] get pure linear acceleration

hi. I’ve been writing test application that get moved distance from iOS accelerometer.
after that, I can move around with this distance.

greate reference and code :

so I did :

  1. get linear accelerometer : remove gravity vector from accelerometer.
  2. if each x, y, z of linear accelerometer are smaller than some value (ex - 0.1) ignore it.
  3. get velocity : integrate acceleration.
  4. if step 2 happens enough of times (like over 20 times), make speed to 0.
  5. get distance : integrate velocity.

but the problem is that, iOS accelerometer is also influenced by rotation. maybe I can get rotate acceleration from gyroscope. but still I cannot find solution. I don’t have good mathatical knowledge.

with this test, I used ofxCoreMotion.

modified code is here

void ofApp::dododo(){

	ofVec3f accel = coreMotion.getAccelerometerData();
	ofVec3f gyro = coreMotion.getGyroscopeData();
	ofVec3f gravity = coreMotion.getGravity();
	
	
	// remove acceleration from gravity
	accel = accel - gravity;
//	accel.z -= (abs(gyro.y) - abs(lastGyro.y));
	
	// filtering
	if(abs(accel.x) < 0.1 ) accel.x = 0;
	if(abs(accel.y) < 0.1 ) accel.y = 0;
	if(abs(accel.z) < 0.1 ) accel.z = 0;
	
	accel.x == 0 ? countx++ : countx = 0;
	accel.y == 0 ? county++ : county = 0;
	accel.z == 0 ? countz++ : countz = 0;
	
	
	if(countx>=20)	{ speed.x = 0; lastSpeed.x = 0;}
	if(county>=20)	{ speed.y = 0; lastSpeed.y = 0;}
	if(countz>=20)	{ speed.z = 0; lastSpeed.z = 0;}
	
	
	// Newton - D'Lambery physics for no relativistic speeds dictates
	// speed = lastSpeed + (currentAcceleration - lastAcceleration)/2 * INTERVAL
	speed.x = lastSpeed.x + lastAccel.x + (accel.x - lastAccel.x)/2 * 1/100;
	speed.y = lastSpeed.y + lastAccel.y + (accel.y - lastAccel.y)/2 * 1/100;
	speed.z = lastSpeed.z + lastAccel.z + (accel.z - lastAccel.z)/2 * 1/100;
	
	
	// location = lastLocation + (currentSpeed - lastSpeed)/2 * INTERVAL
	distance.x = lastDistance.x + lastSpeed.x + (speed.x - lastSpeed.x)/2 * 1/100;
	distance.y = lastDistance.y + lastSpeed.y + (speed.y - lastSpeed.y)/2 * 1/100;
	distance.z = lastDistance.z + lastSpeed.z + (speed.z - lastSpeed.z)/2 * 1/100;

	lastAccel = accel;
	lastSpeed = speed;
	lastDistance = distance;
	lastGyro = gyro;
}

anyone help?