Sampling frequency : Swift on MAC OSX

edited September 2017 in General
I am using Swift to read the sensor data, as follows:

    device.accelerometer?.sampleFrequency = 100;
    device.gyro?.sampleFrequency = 100;
    device.accelerometer?.dataReadyEvent.startNotificationsAsync(handler: { (obj, error) in
        if let obj = obj {
            let toWrite = "\(obj.x)," + "\(obj.y)," + "\(obj.z),";
    device.gyro?.dataReadyEvent.startNotificationsAsync(handler: { (obj, error) in
        if let obj = obj {
            let toWrite = "\(obj.x)," + "\(obj.y)," + "\(obj.z),";

The problem that I am facing is that never am I getting a sample rate of 100Hz on either medium. When logging both the gyro and the accelero, I get 5~Hz on each . When choosing one vs the other, I can get 17Hz. ( Seems that the sampling frequency I set has no effect on the outcome.


p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187; background-color: #ffffff}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff; min-height: 13.0px}
span.s1 {color: #000000}
span.s2 {color: #272ad8}
span.s3 {color: #4f8187}
span.s4 {color: #31595d}
span.s5 {color: #ba2da2}
span.s6 {color: #d12f1b}
span.s7 {color: #3e1e81}
span.s8 {color: #703daa}


  • edited September 2017
    Here is some output that I am getting that I consider weird: ( Note, I set the sampleFrequency to 30, loggin my accelerometer data ~ and gyro data at the same time. Only accelero is shown ))


    It makes absolutelly no sense to me why I am getting 9 samples, when I said 30... lol ( 9 samples is a happy path in this log, almost all other sample sets range to around 5 Hz ... )

    You will also want to adjust the connection parameters.  Not sure if OSX has any weird connection interval restrictions but setting both max and min connection intervals to 20ms should suffice.

  • edited October 2017
    Hi Eric. Setting the connection parameters fixes the issue. I am now able to capture a lot more packets.

    However, the catch here is that I cannot limit the number of samples I am getting. For example, I set the sampling frequency to 30. With the connection interval set to 20ms, I now get more than 30 samples. ( 38 - 45 ) Staying focused on 30 Hz, how can I ensure that I get 30 samples exactly? ( +-2 samples is not a big deal. += 15 is ) While this is a substantial improvement on my original issue, it is still a bit away from where I want to be. ( Note: setting the ms rate to 33 does not solve the problem )
  • You can't, 30Hz is not a native sampling frequency of the accelerometer.  Supported ODRs for the BMI160 are listed in the bmi160.h header file.

  • ah, you are right. Thanks Eric! That answers all of my questions.
This discussion has been closed.