Time to introduce some sensors into the mix. We want you to add a gyro sensor to the basic rover model, add it to the underside between the two motors on the axel.

Be-warned don’t plug your gyro into a running brick, turn it off. After connecting the gyro, don’t touch the brick after you turn it on until it is fully up. Do so and there is chance you’ll mess up the initialisation of gyro sensor and may not be able to get any meaningful readings from it.

Image for post
Image for post

Now add the code you need to bring the gyro online, you need to mention it in the imports and define which port it is connected to.

Now incorporate it into the basic template we’ve been working on in these tutorials. Your code should end up looking like this.

Let’s take a closer look. Firstly we’re going to use some constants at the beginning. We do so for speed because we want to change it dynamically, and simply because it is good practice not have magic numbers as they’re know in your code.

We will use the UP and DOWN buttons to increase and decrease the speed of the turns and we will use the LEFT and RIGHT buttons to actually make them.

The motor command we use here turns the wheel by the degrees mentioned, so in this case 360 degrees. This will turn the wheel itself in a complete circle, it does not refer to the number of degrees the bot itself will turn.

The number of degrees the bot will turn is reported by the gyro sensor we added. Both the angle and the speed are reported after one of the four buttons is pressed.

How many degrees the bot turns will depend on the speed of the turn, the number of degrees you turn the wheels [360 in this case] and the stop parameter for the motor. It has three values that you can play with.

Finally the boolean value mentioned at the end of the motor.run command tells the script if it should continue to the next command before said command completes. Since we want it to turn both wheels at the same time mind, but not report the angle until it has finished doing so we use False & True here.

Copy and Paste the program into a script on your robot and give it a try. Try changing the speed with the buttons, try changing the angle the wheels turn, try changing the STOP command. They will all affect each other. If you can, even try changing the physical size of wheels of the bot itself, the results will get with the same parameters will of course be different.

Welcome to the world of robotics, a fascinating area where as you can now start to appreciate there many different factors that can affect how things work out.

Finally you might want to try to add some more key combinations to make changes some of the other constants we mentioned here, to make it easier to change them on the fly. I leave it as an exercise for the reader.

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