Here is our formula for easy, clean autonomous programming!

If you have ever coded an autonomous run for your robot, you do know how many lines that code can become and how hard it can be to find the point you’re looking for or troubleshoot little problems and so on. To deal with this, our team uses “my blocks” to turn lots of lines into a single line. Quick and easy to write and to troubleshoot. For an explanation of the use of “my blocks” you can read this post. Another thing this allows us to do is to take care of timeouts, the solution to the main cause of death to most autonomous programs. Read about it here. So let’s begin.

DRIVE BLOCK

We start with writing one block for driving and one block for turning. In this block we are using 2 variables. First is speed, second is the distance we want to cover.

So let’s see what’s going on here. We’re not using drivetrain and we are using individual motors for driving which is always ideal for autonomous runs. First thing we do is line up the robot on a black line on the field and figure out how many turns it takes to go from one line to the next.

This is because we want each square to be 100 units. We find that to be very helpful in guessing the distances to cover while using our Drive Blocks. So if you want your robot to go forward 2.5 squares with 50% velocity. You would write:

So once you figure out how many turns it takes for your robot to cover one square then we add this value to our formula so that if we type 100 to the drive distance it will convert it to the number of turns it takes to cover one square. So let’s say our robot took 1.2 turns on each motor to cover a square then we need to do our simple proportion math calculation.

If I want 100 units to be equal to 1.2 turns, how many units is 1 turn? This will give us our robotSquare number.

So if you define this variable in your Drive Block then from here on for example “250” distance will give you 2.5 squares. And a very important point -> Make sure to check your robot’s square distance every now and then because as you modify your robot, the square distance number might change and you can just update the “robotSquare” variable and main autonomous code will not change. How convenient is that!!! So this formula, distance/robotSquare will give us the number of turns it’s needed to cover the distance we want.

Let’s test it. 100 units takes us 1.25 turns right? So if we want to go 2.5 squares, distance will be 250 and that should take 1.25 turns multiplied by 2.5, which is 3.125. Let’s check our formula now, distance / robotSquare would be 250 / 80 which gives us 3.125. Voila! It worked! We are geniuses aren’t we?

How about this part of the code, what is it?

This is your safety. If by any chance your robot hits an object and can not cover the distance you were shooting for, this part of the code will allow your block to terminate and continue on to the next line instead of getting stuck and suffering a miserable death of an unfulfilled life. OK, maybe overdoing it a bit here but read this post if you want to understand better. As for the formula used here, it came out of lots of testing with our own particular robot. We are gonna test and improve this formula some more later on to apply to all robots and I will update this post. You just have to make sure this time out is more than the longest task in your autonomous code otherwise it will terminate that line half way.

So to sum up, our Drive Block first sets the motors to the velocity that you defined. Then it evaluates whether its a positive or a negative distance to so that it goes forward if its positive and backwards if its negative. Then it will spin your motor forwards or backwards while converting distance value to motor turns. BUT before doing this it sets the timeout value for those motors. Timeout always has to be applied BEFORE the instruction to run the motor. If its a negative distance then it does the same things except run the motors in reverse and we have to multiply the distance by -1 because spin motor instruction only runs with positive numbers, so setting it to reverse is already taking care of going back.

And lastly we add a wait at the end of the block so that the momentum the robot has while accomplishing this task dies down and does not affect the next task. You can play around with this number to get the ideal wait time for your robot.

TURN BLOCK

How about the turn? Here we go. First we need to find our robot’s turn variable. How many motor turns (one forward, one reverse) does it take for the robot to turn 90 degrees? I’ll call this variable turn90. So if it takes 2 motor turns for example to cover 90 units then we have our formula to figure out our turn90.

So let’s plug this into our turn block.

Then in our turn block we add velocity and we need to define whether its a right turn or a left turn and for that we are gonna add a “Boolean” to the block variables. Boolean is a variable that takes only 2 values and since we are talking about left or right, that works perfectly for us. So I’ll set right turn to be “true”, because you know, “right” turn, “correct” turn, “true” turn 🙂 or if you want, just be a rebel and make the right turn the false turn. Just make sure to remember what you set it to! I’m gonna assign true to right turn and false to left turn. AND we need to add our bodyguard, the timeout block to stop this code from killing us in case we hit a wall or something while turning. And how did we come up with the formula in the timeout? By trial and error. We will be improving this formula in an upcoming post. So this is how our final turn block looks like.

Let’s code a sample run with this. Let’s make our robot do this path:

For this out code using these blocks would look like this:

Nice, clean and easy, isn’t it? If you want to build up on this some more. Read my next article with some improvements on these blocks. Happy programming!

Yas Programmer