HOWTO: Use FFI to call user C modules on EV3 #1492
jaguilar
started this conversation in
Show and tell
Replies: 1 comment
-
I replaced the PID module in my gyroboy implementation with a C FFI version, and it made a material difference in the steadiness of the robot. I noticed the average loop time went down from 7-8ms to 5ms. Which probably means the variance went down a ton too since I have the loop set to run at a maximum frequency of 200hz. You can see the video of the after and before on the README file. I found it to be a lot of fun to program in a mix of C and Python. If you're doing anything CPU-intensive in Micropython, I really recommend this approach! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi there! In #1451, it was suggested that I make an example project to show how to use the micropython ffi module on ev3dev.
This repository contains the results of that effort. It shows how to build C libraries, copy them to the brick, load them with ffi, and call them.
I found that C is (big surprise) quite a bit faster than Python. According to very, very unscientific tests, it looks like it offers around a 32x speedup on this toy problem. If Viper is 4x as fast as plain micropython, that means C can give you an additional 8x speedup on top of Viper. So it might be something to reach for if you want to program CPU-intensive stuff on EV3 but not give up Python for the glue code. I reckon that similar results can be obtained using the technique in pybricks/pybricks-micropython#149 on more recent hubs.
One sad thing: it appears to no longer be possible to build the docker container used to cross-compile pybricks-micropython from ev3dev. It looks like the Debian stretch repositories have gone away, so the container can no longer be built. So let's not lose that docker image okay!?
Beta Was this translation helpful? Give feedback.
All reactions