Presentation is loading. Please wait.

Presentation is loading. Please wait.

Robotics Programming Using Shaft Encoders

Similar presentations


Presentation on theme: "Robotics Programming Using Shaft Encoders"— Presentation transcript:

1 Robotics Programming Using Shaft Encoders

2 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 If..else

3 Movement using Shaft Encoders
Configure (Motors and Sensors Setup) We will look at the following in this section SensorValue[] while Conditions (<, >, <=, >=, !=, ==)

4 Getting Started/ Review
Open RobotC Start a new program Set up a Squarebot rightMotor: port2 leftMotor: port3 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.

5 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.

6 Configuring the Encoders
Configure the encoders on your warm-up code. Robot -> Motors and Sensors Setup

7 Name and Select 1) Select the VEX Cortex Digital Sensors 1-12 tab.
3) Select the Sensor Type. Quadrature Encoder in this case. 2) Name the encoder. (Start with a letter, no spaces, no punctuation, no reserved words, descriptive.) 4) Click ‘Apply’ and ‘OK’ Note: On the Cortex the quadrature encoder cables must be plugged in next to each other.

8 pragma Statements Created

9 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? Pseudocode While the robot has not travelled 1800 ticks, keep ongoing. 3.6 feet 12 inches 1 revolution 360 ticks = 1800 ticks 1 foot Pi * 2.75 inches Use the wheel diameter to match your robot.

10 Initializes both encoder values to 0.
From Math to Code SensorValue[rightEncoder] 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) { }

11 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.

12 1. The word “while” Every while loop begins with the keyword “while”.

13 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.

14 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.

15 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.

16 Boolean Logic Conditions: True or False
Conditions: Compare two items and return a true or a false value examples while (SensorValue[leftEncoder] < 2000) { } while (SensorValue[leftEncoder] > -2000) while (SensorValue[leftEncoder] <= 2000) RobotC Comparison Operations < Is less than > Is greater than <= Is less than or equal to >= Is greater than or equal to == Is equal to != Is not equal to

17 Back to the Encoder Example
SensorValue[rightEncoder] 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) { } What happens if you add a wait1Msec(12000); command inside the while loop? Test this code in the Virtual World.

18 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.

19 Watch the Values Run the program in the Virtual World
Watch the sensor values change Sensor Values

20 Online Time Motor Encoder Movement Challenges using only encoders, no timing. Basketball Drills Turning Investigation Labyrinth Challenge

21 Auto Straightening using VEX Shaft Encoders

22 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.

23 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

24 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!

25 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

26 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.

27 Code this and test it on the Huge Table.
Implementing if..else Code this and test it on the Huge Table.

28 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.

29 RobotC 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

30 Code After Using Variables for Speed Values
These variables are setup as local variables in task main(). Declaring the variables. type name; Initialize the variables. You can change the values of the variables. fullSpeed = fullSpeed + 5; Modify your going straight code to incorporate variables. Try modifying the fullSpeed and slowerSpeed values to see how if changes the robot behavior.

31 Summary motor[] SensorValue[] wait1Msec() while if if..else Conditions
Conjunctions Motors and Sensor Quadrature Encoder Strategies on going straight Timing vs. distance

32 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


Download ppt "Robotics Programming Using Shaft Encoders"

Similar presentations


Ads by Google