MicroPython, micro:bit, and RVR position control

I used a Grove ultrasonic sensor and a micro:bit running MicroPython with an RVR to keep a consistent distance from the wall. This was to help a student in my Automation class who wanted to use a micro:bit and an RVR for a control algorithm project.

You can see the video here:
[https://youtu.be/zOWmA9jUjRM](Position control with Micro:bit and MicroPython)

To get the microbit working with the ultrasonic sensor, I needed to adapt the Grove Ultrasonic Ranger to use the micro:bit hardware in MicroPython. The code is based on the Grove Ultrasonic ranger wiki. (Since I’m a new user, the forum isn’t letting me paste a link, so just Google Grove Ultrasonic Ranger. )

Once that was working, I was able to put together a proportional control loop that worked pretty well to maintain the location. I did need to make an edit to the sphero.py file from the SDK.

When you send a negative velocity to RVRdrive, the default code spins the RVR 180 degrees and drives in the opposite direction. Since I had my ultrasonic sensor pointed at the wall, I needed the robot instead to move backwards.

Line 34 in the sphero_EMW.py file has the heading lines commented out so that this doesn’t happen.

You can see the full code at this link:

I hope you can learn from my effort to figure this out. Have fun!

3 Likes

Well done. I was doing this with a raspberry pi instead of a microbit. I wasn’t sure how well it would work with the microbit simply because of how fast it could process the data of the ultrasonic sensor…but it seems like it was fast! Very cool. Thanks for sharing!

2 Likes

I’ve seen better responsiveness in the micro:bit with MicroPython than with Makecode, so it has been worth putting in the time. As long as you aren’t displaying images on the LED matrix, the micro:bit can be pretty fast! There is some really impressive engineering in that thing.

2 Likes