MicroPython Tutorial XVII

Ok, lets back to edges. One of the tasks you’ll surely need your robot to do is square its self up along an edge. A task that with a single colour sensor needs some engineering. What I have done here is mount a colour sensor on cog, that is connected to the medium motor, so that I can move it left and right.

Image for post
Image for post

With the accompanying script to drive it shown here.

How does it work. It defines the motors and the sensors and starts to move forward. Note we reset the angle of the motor managing the position of the sensor to zero.

The bot moves forward until it sees a black line, it then moves the sensor right to left. If it sees the black line again, it came from the right, if it sees nothing it came from the left. Depending on the direction it has figured out moves one of the main motors to align the bot to the line.

Finally it used the run_target command to move the colour scanner back to its default position.

Does it work well. Ok. There is a lot of room for improvement. On the engineering side it is difficult to get the sensor truly aligned and the script needs more tunning. Can you build it? Can you replicate it? Can you do better?

Image for post
Image for post

Here is another version with different mounting for the color sensor. It has less travel than before, but is more stable. And here is the beginnings of the code to use it.

I didn’t finish the code because I honestly came to the conclusion that using LEGO engineering to move the sensors if precision is critical isn’t going to work well enough, and well its easier with two sensors.

You may recall if you read microPython musings that I suggested if you have money to spare, you should invest in more colour sensors. Yes, I am sorry to say this is the best answer. More colour sensors.

Image for post
Image for post

Two to be precise, mounted just in front of wheels. Now on to the code to do that line squaring.

Three main blocks, the first very short one simply moves the robot forward until it meets a blackline on sensor S2 or sensor S3.

The second block is executed if the colour sensor on the right in our case, plugged into port S3 hits the black line first. It than stops the left motor, and continues with power to the right until it detects the blackline on the other sensor. It then moves the left again to make sure it is straight. The macro move. Finally if either S2 or S3 are not giving the same reading it makes a move to correct itself again. A micro move.

The third block does exactly the same thing, only it is swinging on the sensor on the left this time.

Assuming you got two colour sensors, copy and paste the code and have a go. You’re more than welcome to improve the code, what about trying to create some subroutines to try to simplify it.

You could also port a version to the other builds to see if you can get the same results ultimately, and prove me wrong :)

Written by

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store