Presentation on theme: "Autonomy using Encoders Intro to Robotics. Goal Our new task is to navigate a labyrinth. But this time we will NOT use motor commands in conjunction with."— Presentation transcript:
Goal Our new task is to navigate a labyrinth. But this time we will NOT use motor commands in conjunction with timers. We use our new sensor, the ENCODER.
Autonomy/Encoders Forward for Distance In this unit, you will learn to use the encoders to control the distance the robot moves, and to self-correct its steering to counteract the random drift that occurs during movement. This will eventually serve two purposes.
Autonomy/Encoders Forward for Distance First, it will provide you with a new form of Operator Assist that uses the sensors on the robot to drive straighter than a human operator might be capable of doing. Second, it will begin building a base of behaviors that the robot can use on its own during the Autonomous Period that is part of the revised challenge. For 20 seconds at the start of the match, your robot will have a chance to run and score points completely on its own.
The Encoder The Encoder is a sensor that measures rotation. On the Squarebot 3.0, encoders are attached to the wheels of the robot. The encoders measure how far the wheels have turned, and therefore, how far the robot has moved. The distance the robot covers moving forward is equal to the length of the tire tread that spins by.
The Encoder We will be able to make reasonably accurate predictions about exactly how far the robot will move after every full turn of the wheels. The encoder measures in 4-degree increments, and so will register 90 counts for a full 360-degree rotation. For this lesson, the important part is simply that the number of encoder counts is directly linked to the amount that the wheels turn, and therefore, to the distance that the robot travels. Being able to directly measure the turning of the wheels allows us to dictate the motion of the robot in terms of distances rather than simply defining a period of time that the robot should move. This is a much more reliable method, because it is unaffected by factors like speed or battery power. Using the encoder-based distance to determine movement, therefore, will result in much better consistency of movement. This is exactly what your robot needs to do its job well, with or without human supervision!
Open a sample program Save in the UCF folder as Encoder Test
Motors & Sensors Setup Notice that the encoders are a DIGITAL sensor and that we named them “rightEncoder” and “leftEncoder”. Each encoder is plugged into TWO ports on the Cortex.
Let’s inspect our code The “SensorValue” command is used for ALL sensors and can be used to clear any digital value stored in the sensor. Each command must specify which sensor it is referring to by its NAME. In this case, we are setting each encoder to ZERO. This clears out the encoder so that it can measure out distance. This MUST be done each time you travel a distance or make a turn.
Let’s inspect our code This is a special type of CONTROL STRUCTURE, called a WHILE LOOP.
while ( ) When the program reaches most commands, it runs them and then moves on. When the program reaches a while ( ) loop, however, it steps INSIDE the loop, and stays there as long as the while ( ) loops decides that it should. The commands repeat over and over.
Let’s inspect our code The CONDITION inside the parenthesis states that the encoder values need to be LESS than 1800. As long as the counts are less than 1800 the COMMANDS(simple statements) inside the curly braces will loop over and over. As soon as the count goes OVER 1800, the loop will END and the next line of code will begin.
Modifying our Labyrinth Code Checkpoint Your robot should begin executing the first behavior two seconds after it is turned on. The robot will start moving — and keep moving! If you pick it up and let it run for a while, you will notice that it eventually stops, but nowhere near the right distance. The guess we used for the number of encoder counts was off. 1800 is far too many counts for the distance that the robot needs to move.
Encoder count off? Change this value until you reach the turn in the labyrinth. Compile and download each time until the robot reaches the correct position.
Making turns with the Encoder You must ZERO the left encoder before using it for a different leg of the labyrinth. We use a NEW COUNT for the right turn and also make sure we reverse the right motor to make the turn correctly. All of this is done inside a second WHILE loop.
The “If” statement The “IF” statement is similar to a while loop in that it is a CONTROL STRUCTURE. But it is very different in that it only runs the commands ONCE instead of looping. Just like the while loop, the “IF” statement has a CONDITION that robotC checks to see if it is true. If the condition is true, it runs the commands inside the curly braces. If it is false, it exits and runs the next line of code. Save your code from before as LABYRINTH AUTO STRAIGHT.
Using the “RIGHT” Encoder Since BOTH encoders will be used to monitor whether the robot moves straight it is important to make sure you ZERO out each encoder before each turn.
Using the “IF” statements Locate your FIRST while loop and inside set up the framework for the THREE “IF” statements.
If the LEFT is greater than the RIGHT This says, if the counts on the left encoder are greater than the counts on the right encoder then lessen the power on the left motor and increase the power on the right
If the RIGHT is equal to the LEFT The WHILE loop still makes sure the distance is set at the correct # of counts while the “IF” statements make sure the robot moves straight over this distance.
Using /* comments */ to test program Note: Make sure the last */ - comment tag is BEFORE your last “task main” punctuation pair. “Commenting out your code” is a great way to go back and test out individual parts of the code as you work on changes. Comment out your MAZE code and test just the first leg of the STRAIGHT program.