0

I am working on an Ionic project that uses the accelerometer at 100hz. I need to save the accelerometer readings to a file, and I am using ionic native file plugin. My problem - the writing time to the file is longer than the accelerometer sample time and the data in the file is not written in the needed order (newer data is written before the previous one had the chance to be written.

This is the provider that handles the accelerometer reading and the writing to local file :`import {DeviceMotion, DeviceMotionAccelerationData} from

'@ionic-native/device-motion';
import {Subject} from "rxjs/Subject";
import {Observable} from "rxjs/Observable";
import {Subscription} from "rxjs/Subscription";

import {Http} from '@angular/http';
import {File} from '@ionic-native/file';

@Injectable()
export class DeviceMotionProvider {
    accelerometerata$: Subject<DeviceMotionAccelerationData> = new Subject();
    subscription: Subscription;

    constructor(private deviceMotion: DeviceMotion, private http: Http,
                private file: File) {


    }

    startAccelerometer() {
        // Watch device acceleration
        this.subscription = this.deviceMotion.watchAcceleration({frequency: 100})
            .subscribe((acceleration: DeviceMotionAccelerationData) => {
                this.accelerometerata$.next(acceleration);
                this.file.writeFile(this.file.dataDirectory, 'flightData.txt', acceleration.timestamp + ',' +Math.sqrt(acceleration.x*acceleration.x+acceleration.y*acceleration.y+acceleration.z*acceleration.z)/10 + '\n' ,{ append:true})
            });
    }

    getAccelerometerData(): Observable<DeviceMotionAccelerationData> {
        return this.accelerometerata$.asObservable();
    }

    stopAccelerometer() {
        this.subscription.unsubscribe();
    }


}`

I couldn't find the proper way to overcome this issue. Any help will be greatly appreciated.

Ziv
  • 538
  • 1
  • 4
  • 7

1 Answers1

0

I'm trying the same thing. However, (1) I'm trying it with 10hz and (2) I limit the time/#records. The way I do it is collecting all the data into an array or whichever buffer suites your needs and once collecting is done, iterating through the collection, formatting it, and writing the data into the file.

Hope this helped

user3027595
  • 111
  • 1
  • 1
  • 6