Presentation is loading. Please wait.

Presentation is loading. Please wait.

Movement using Shaft Encoders

Similar presentations


Presentation on theme: "Movement using Shaft Encoders"— Presentation transcript:

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

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

3 Configuring the Encoders
Robot -> Motors and Sensors Setup

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

5 pragma Statements Created

6 Initializing and Reading the Encoder
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) { }

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

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

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

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

11 Boolean Logic Decisions robots make must always based on questions which have only two possible answers: yes or no, true or false, right or wrong. Statements that can be only true or false are called Boolean statements, and their true-or-false value is called a truth value.

12 Boolean Logic Conditions: True or False
Conditions: Compare two items and return a true or a false value 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 no equal to

13 Boolean Logic

14 Conjunctions Conditions can be combined using conjunctions && (AND)
while ((SensorValue[rightEncoder]<1800) && (SensorValue[leftEncoder] <1800)) { //while both encoders are less than 1800 it will repeat the code in the {} block } || (OR) while ((SensorValue[rightEncoder]<1800) || (SensorValue[leftEncoder] <1800)) { // While either the leftEncoder<1800 or the rightEncoder is <1800 it will //repeat the code inside the {} block ! (NOT) Turns true to false and false to true while !(SensorValue(leftEncoder)>1800)

15 Boolean Check: Fill in the Chart (T/F)
x y x <= y y > 5 ( x <= y) && ( y > 5 ) ! (( x <= y) && ( y > 5 )) 10 11 4 100 90 3

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

17 Debugging: Watching the Sensor Values
Open Debugger window for Sensors Robot->Degugger Windows -> Sensors

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

19 Online Time Complete the Labyrinth Challenge using encoders rather than timers

20 Using Encoders to go Straight
Limit drifting when trying to go straight In this section we will look at if

21 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

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

23 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

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

25 Chaining if..else if ..else

26 Implementing if..else

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

28 RobotC Variables int float string char bool
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

29 Code After Using Variables for Speed Values
Declaring the variables. type name Initialize the variables. Try modifying the fullSpeed and slowerSpeed values to see how if changes the robot behavior.

30 Summary: What do you recall about…?
motor[] SensorValue[] wait1Msec() while if If..else Conditions Conjunctions Motors and Sensor Quadrature Encoder Strategies on going straight Timing vs. distance Variables

31 Online Time: Movement Challenge using the Shaft Encoders
Basketball Drills Driving Straight I Driving Straight II Labyrinth Challenge Power Level Inv. Power Level Inv. (Encoders) Robot Acceleration Sentry Simulation I Sumo Bot Turning Inv. Encoders Turning Inv. Wait State Inv. Each of the movement challenge activities can be done using the encoder to improve consistency. For you… use the encoders to solve the Labyrinth Challenge, then you can do others as time permits.


Download ppt "Movement using Shaft Encoders"

Similar presentations


Ads by Google