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!