musicboy
Posts: 15
Joined: Fri Feb 14, 2014 6:52 pm

Python loop @ 80Hz

Wed Mar 01, 2017 8:18 am

Hi all,

I need to run a control loop for a robot at 80Hz in Python.
Obviously there are various ways to do this using time/datetime etc, starting each loop iteration once dt has elapsed.
I have two questions:
1) I have heard that when using time/datetime, the values may not always be accurate at the milliseconds level. Thoughts?
2) Clearly the program will be running on top of Raspbian, and so it is not guaranteed that the program will make 80Hz every time. Is there anything I can strip down to make this more likely?

Any help much appreciated.
Ta

User avatar
rpdom
Posts: 14998
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Python loop @ 80Hz

Wed Mar 01, 2017 8:41 am

80Hz is not very fast. What is the frequency tolerance of your robot? It may be that the Pi's timing will be sufficient.

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

Re: Python loop @ 80Hz

Wed Mar 01, 2017 9:54 am

For what purpose do you need 80Hz? If it is just for a trigger or sync signal, you could use frequency output on GPIO4.
If you want to perform some actions in python, the things to execute must fit inside 12.5ms time frame...
Regards,
Gerhard

musicboy
Posts: 15
Joined: Fri Feb 14, 2014 6:52 pm

Re: Python loop @ 80Hz

Wed Mar 01, 2017 1:31 pm

80Hz is a ballpark figure for the frequency at which I need to step the motors on the bot.
The PID loop will be running at ~30Hz since that is the maximum sampling frequency of my accelerometer

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

Re: Python loop @ 80Hz

Thu Mar 02, 2017 9:11 pm

Hello,
was curious to see how accurate an 80Hz loop in python is on a Pi.
See http://heppg.de/ikg/wordpress/?p=914 for the results.
Accuracy can be achieved close to 0.1ms.
Regards,
Gerhard

musicboy
Posts: 15
Joined: Fri Feb 14, 2014 6:52 pm

Re: Python loop @ 80Hz

Fri Mar 03, 2017 1:35 pm

Thanks very much for looking into it in such detail!
Much appreciated.

Return to “Python”