Robotics Programming Using Shaft Encoders Learning Objectives Be
Robotics Programming Using Shaft Encoders
Learning Objectives • Be able to use Shaft Encoders to control the robot driving for a distance • Motors and Sensors setup • Using the while loop • Using the debugger window to watch the sensor values change • Online time • Be able to use Shaft Encoders to help the robot drive straight • If. . else • Online time
Movement using Shaft Encoders • Configure (Motors and Sensors Setup) • We will look at the following in this section – Sensor. Value[] – while – Conditions (<, >, <=, >=, !=, ==)
Getting Started/ Review • Open Robot. C • Start a new program • Set up a Squarebot – right. Motor: port 2 – left. Motor: port 3 – Add code to make it go forward for 1 second and test in the Labyrinth Challenge Virtual World. – Make sure you select ‘VEX Squarebot’ from the ‘ROBOTS’ menu in the virtual world. – Make sure the robot moves forward and not just spins.
Quadrature/Shaft/Rotation Encoder • 360 Ticks per revolution • Counts up going forward, down going backwards • Takes two digital input ports on the Cortex • If the wires are plugged in in reverse order, then the counter will count backwards.
Configure the encoders on your warm-up code. Configuring the Encoders • Robot -> Motors and Sensors Setup
Name and Select 1) Select the VEX Cortex Digital Sensors 1 -12 tab. 2) Name the encoder. (Start with a letter, no spaces, no punctuation, no reserved words, descriptive. ) Note: On the Cortex the quadrature encoder cables must be plugged in next to each other. 3) Select the Sensor Type. Quadrature Encoder in this case. 4) Click ‘Apply’ and ‘OK’
pragma Statements Created
Now that it is set up, we want to use the encoder to control the distance a robot travels • The sensor will return 360 ticks per revolution or 1 tick per degree. • The squarebot has 2. 75” diameter wheels. • How many ticks for the robot to travel 3. 6 feet? 3. 6 feet 12 inches 1 revolution 1 foot Pi * 2. 75 inches 1 revolution Use the wheel diameter to match your robot. 360 ticks = 1800 ticks Pseudocode While the robot has not travelled 1800 ticks, keep ongoing.
From Math to Code Sensor. Value[right. Encoder] is used to initialize (set to 0 in this case) and read encoder values. While the value of the left encoder is less than 1800 it will go through the loop again. while (condition) { } Initializes both encoder values to 0.
Breaking Down While Loops • A while loop is a structure within ROBOTC which allows a section of code to be repeated as long as a certain condition remains true. • There are three main parts to every while loop.
1. The word “while” • Every while loop begins with the keyword “while”.
2. The Condition • The condition controls how long or how many times a while loop repeats. While the condition is true, the while loop repeats; when the condition is false, the while loop ends and the robot moves on in the program. • The condition is checked every time the loop repeats, before the commands between the curly braces are run.
3. Commands to be Repeated • Commands placed between the curly braces will repeat while the (condition) is true when the program checks at the beginning of each pass through the loop.
Boolean Logic Decisions robots make must always based on questions which have only two possible answers: yes or no, true or false. Statements that can be only true or false are called Boolean statements, and their true-or-false value is called a truth value.
Boolean Logic Conditions: True or False Conditions: Compare two items and return a true or a false value • Robot. C Comparison Operations < > <= >= == != Is less than Is greater than Is less than or equal to Is greater than or equal to Is not equal to examples while (Sensor. Value[left. Encoder] < 2000) { } while (Sensor. Value[left. Encoder] > -2000) { } while (Sensor. Value[left. Encoder] <= 2000) { }
Back to the Encoder Example Sensor. Value[right. Encoder] is used to initialize (set to 0 in this case) and read encoder values. Initializes both encoder values to 0. While the value of the left encoder is less than 1800 it will go through the loop again. while (condition) { } Test this code in the Virtual World. What happens if you add a wait 1 Msec(12000); command inside the while loop?
Debugging: Watching the Sensor Values • Open Debugger window for Sensors • Robot->Degugger Windows > Sensors Note: If the ‘Debugger Window’ option is not available, then compile and download the program to your robot and try again.
Watch the Values • Run the program in the Virtual World • Watch the sensor values change Sensor Values
Online Time • Motor Encoder Movement Challenges using only encoders, no timing. – Basketball Drills – Turning Investigation – Labyrinth Challenge
Auto Straightening using VEX Shaft Encoders
Learning Objectives • Understand how the strategy for using shaft encoders to help the robot drive straight. • Be able to program the robot to go straight using shaft encoders • Be able to use the while loop, if and variables.
Pseudo Code (What we are trying to get the robot to do) • While the robot has not reached the destination • If the left motor has gone farther, slow the left motor and have right motor go at speed • If the right motor has gone farther, slow the right motor and have the left motor go at speed • If the motors are the same, have both motors go at speed
If Statements • When your robot reaches an if Statement in the program, it evaluates the condition contained between the parenthesis. • If the condition is true, any commands between the braces are run. • If the condition is false, those same commands are ignored. • Very similar to how a while loop works, but does not repeat the code!
Going Straight: Translating to Code Pseudo Code While the robot has not reached the destination If the left motor has gone farther, slow the left motor and have right motor go at speed If the right motor has gone farther, slow the right motor and have the left motor go at speed If the motors are the same, have both motors go at speed
If-else statements • The if-else Statement is an expansion of the basic if Statement. • The “if” section still checks the condition and runs the appropriate commands when it evaluates to true • Using the “else” allows for specific code to be run only when the condition is false. • Either the “if” or the “else” branch is always run; no more, no less.
Implementing if. . else Code this and test it on the Huge Table.
But… • If the motors are not close in their Power to Speed then the difference between 50 and 63 might not be sufficient. • So it will take some tweaking. • Rather than changing the 63 to another value when modifying the ‘at speed’ needed, you can use variables.
Robot. C Variables Review • int • Stores integer values: 5, 0, -100 • float • Stores floating point (real) values: 3. 01, -5. 21, 0 • string • Stores words: “Hello World” • char • Stores single characters: “A”, “@” • bool • Stores Boolean (true, false) values
Code After Using Variables for Speed Values These variables are setup as local variables in task main(). You can change the values of the variables. full. Speed = full. Speed + 5; Declaring the variables. type name; Initialize the variables. Modify your going straight code to incorporate variables. Try modifying the full. Speed and slower. Speed values to see how if changes the robot behavior.
Summary • motor[] • Sensor. Value[] • wait 1 Msec() • while • if. . else • Conditions • Conjunctions • Motors and Sensor • Quadrature Encoder • Strategies on going straight • Timing vs. distance
Driving Straight Activity • Driving Straight II using encoders • Bull in the Ring: Using a while or an if • Robo-Slalom I: Using a while or an if
- Slides: 32