daelk
Posts: 1
Joined: Tue Apr 13, 2021 12:10 pm

Sampling Adafruit BNO055 at a given frequency

Tue Apr 13, 2021 12:34 pm

Hi,

I have Raspberry Pi 3 Model B V1.2, and i need to sample 2 Adafruit BNO055 sensors via I2C.
My desiered sampling rate is quite low ( Fs >= 40 Hz), and in order to achive best real-time preformance I opend new process only for sampling.

The problem is that I cant reliably sample the sensors - the sampling rate drop from 40Hz to ~25Hz....
(Sample the sensors = get IMU data, quaternions, etc.).

This is the current code, and i checked the average sampling time in each buffer:
(I'm expecting to get freq - epsilon)

Code: Select all

def sample_buffer(freq,num_samples,sensors,mv_ind_button=None):
    initialtime = time.perf_counter()
    lastTime = initialtime;
    firstTime_ns = time.time_ns()
    rows = []
    curr_numSample = 0
    #print(f"curr_samples {curr_numSample}")
    #print(f"num_samples {num_samples}")
    debug_sum_freqs = 0
    while curr_numSample < num_samples:

        currentTime = time.perf_counter()
        if(currentTime - lastTime >= 1/freq):
            debug_sum_freqs = debug_sum_freqs + 1/(currentTime - lastTime)
            #print(f"debug: curr freq: {1/(currentTime - lastTime)}")
            curr_numSample +=1
            lastTime = currentTime
            timeC = time.time_ns() - firstTime_ns

           
             if mv_ind_button is None:
                 data = [timeC] + bno_pkg.sampleSensor(sensors[0]) + bno_pkg.sampleSensor(sensors[1])
             else:
                 data = [timeC] + bno_pkg.sampleSensor(sensors[0])+ bno_pkg.sampleSensor(sensors[1]) +[int(not mv_ind_button.is_pressed)]

            rows.append(data)
    print(f"debug: curr freq: {debug_sum_freqs/num_samples}")
    return rows
    
def sampling_worker(q,freq,num_samples,sensors,mv_ind_button=None):
	while(True):
        	q.put(sample_buffer(freq,num_samples,sensors,mv_ind_button))
    
I have couple of question regarding the issue:
1. What is the best way to sample sensors into buffers (in order to preform real-time analysis)?
2. How can set python to run as high priority as default?
3. Does packages that already take care of this kind of sampling exist?

ghp
Posts: 1912
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Sampling Adafruit BNO055 at a given frequency

Tue Apr 13, 2021 8:26 pm

Could not find too much things slowing down this loop.
One interesting thing is the 'busy-loop' you use to create the delay. Could heat up the cpu and could cause throttle. Other processes running in parallel could affect timing too.
Unfortunately the code is specific to python3 and can not be run with pypy3.

Proposal is to measure expired time in the sensor calls, could be this is the bottleneck.

Return to “Python”