Presentation is loading. Please wait.

Presentation is loading. Please wait.

ROBOTC for VEX Online Professional Development Jesse Flot.

Similar presentations


Presentation on theme: "ROBOTC for VEX Online Professional Development Jesse Flot."— Presentation transcript:

1 ROBOTC for VEX Online Professional Development Jesse Flot

2 Program Planning

3 Behavior Based Programming A behavior is anything your robot does: –turning on a single motor, moving forward, tracking a line, navigating a maze Three main types of behaviors: –basic behaviors – single commands to the robot (turn on a motor) –simple behaviors – simple task performed by the robot (move forward, track a line) –and complex behaviors – robot performs a complex task (solve the maze) Complex behaviors can always be broken down into simple behaviors, which are then broken down into basic behaviors

4 The Labyrinth Challenge

5 Programming Discussion The Challenge: Labyrinth The Solution: ??? The Programmer’s (your) Role –Plan out the robot’s path / actions - Pseudocoding –Understanding Program Flow and Behavior Based Programming –Translate the Pseudocode into real code The Robot’s role –To carry out your instructions! The Solution: Programmer and Robot working together, fulfilling their roles –True for all robotic challenges!

6 Pseudocode Pseudocode is a shorthand notation for programming which uses –informal programming structures (if touch1 is pressed…) –verbal descriptions of code (move forward, stop) Emphasis is placed on expressing the behavior or outcome of each portion of code rather than on correct syntax Your lines of Pseudocode should also be listed in the same order as they will appear in the ROBOTC Program

7 Pseudocode Sample Pseudocode:

8 Putting it all Together Effective Program Planning is essential to writing correct code. Carrying out that plan is equally important! Once you have your plan, don’t try to implement it all at once! Systematically add sections of code, testing at every step!

9 Code Commenting “Comments” allow the programmer to make notes in the program that the robot will ignore –Begin with “//” –You can also click on a line number to “comment out” a line of code. This is useful during troubleshooting.

10 Standard ROBOTC: Basic Movement

11 Platform Type 1.Choose ROBOTC for VEX Robotics 4.X (not graphical) 2.Choose VEX 2.0 Cortex 3.Deselect Natural Language if needed

12 Get Your Motors Running Motor Check –Right Motor is plugged into MOTORS 2 –Left Motor is plugged into MOTORS 3 Open Sample Program –File > Open Sample Program > Training Samples > Motor Port 3 Forward

13 Motor Port 3 Forward Defines the “main task” of the robot All commands belonging to task main must be in-between these curly braces

14 Motor Port 3 Forward Turns the port3 motor on at full power forward

15 Motor Port 3 Forward Robot stays here in the program for 3000 milliseconds End Result: robot spins one motor for 3 seconds Try it out!

16 Basic Movements Forward / Reverse

17 Basic Movements Point Turns

18 Basic Movements Swing Turns

19 Basic Movements Manual Adjustments

20 Movement Challenges VCVT Videos (recommended) –Watch videos in the Movement > Moving Forward Section –Watch videos in the Movement > Speed and Direction Section The Labyrinth –Remember to Pseudocode –Remember to Journal –The two are not mutually exclusive!

21 Troubleshooting

22 Troubleshooting Method 1.Identify the problem. Don't tell me how to fix the problem. Just tell me what it is. 2.What can we tell about the student because of the problem? What do they know? What don't they know? 3.What can we tell the student to fix their understanding AND the problem? Just giving the answer to the student teaches dependence! This method teaches!

23 Troubleshooting Student: I want my robot to move forward, then turn. I had it moving forward, and added the turn. Now it’s turning, then moving forward.

24 Troubleshooting Student: My code won’t compile.

25 Troubleshooting Student: I want my robot to move forward, then reverse. I had it moving forward, and added the reverse, but it never actually backs up.

26 Troubleshooting Student: My code compiles, but I get an error when I try to download it to the robot. Check: –Is the robot turned on and sufficiently powered? (blinking green lights) –Is the robot connected to the computer? –Is the correct platform type selected in ROBOTC? –Is the correct port selected in ROBOTC? Has the driver for the programming cable been installed? –Has the ROBOTC firmware been loaded on the VEX? –Does the Master Firmware need re-downloaded? –Is another ROBOTC window open, using the debugger windows?

27 The Problem with Wait States Motor Speed is affected by battery power –If the battery is fully charged, the robot moves quickly –If the battery is running low, the robot moves slowly –Consequently, the robot will not move consistently as the battery power drains –Makes completing the Labyrinth tricky Anyone experience these effects? Wouldn’t it be better if we could control the distance the robot moves, regardless of how long it took to complete?

28 Better Movement: Integrated Motor Encoders

29 Integrated Motor Encoders How they work –“I2C” Smart Sensor –Replaces the back plate of 269 and 393 Motors –As inner striped wheel spins, internal light sensor detects and counts Capabilities and Resolution –627 Counts Per Revolution in High Torque Configuration –392 Counts Per Revolution in High Speed Configuration –Up to 8 encoders “daisy chained” in a row –Counts Up and down –Allows you to control the distance a robot moves, by monitoring how much the wheels spin –Due to “closed-loop” configuration, “PID” control is possible

30 Motors and Sensors Setup Enabled and configured on the Motors tab Motor “Type” is important, as their resolution is not uniform

31 Testing your Encoder Setup You can try to follow the wires from the sensors to the I2C and Motor ports… …or use the Motor Debug Window!

32 Better Movement with Encoders Forward and Backward for Distance

33 Better Movement with Encoders Turning Right and Left

34 PID Control Proportional-Integral-Derivative With the integrated motor encoders, we have what is called a “closed-loop” system –The Cortex knows exactly how fast the motors should be spinning versus how fast they are actually spinning and can make rapid adjustments to compensate for the difference

35 Variables A variable is a space in your robots memory where data can be stored, including whole numbers, decimal numbers, and words Variable names follow the same rules as custom motor and sensor names: capitalization, spelling, availability Variables can improve the readability and expandability of your programs

36 Variable Types Data TypeDescriptionExampleCode IntegerPositive and negative whole numbers, as well as zero -35, -1, 0, 33, 100 int Floating Point Number Numeric values with decimal points (even if the decimal part is zero) -.123, 0.56, 3.0, 1000.07 float BooleanTrue or false – Useful for expressing the outcomes of comparisons true, false bool CharacterIndividual characters, placed in single quotes ‘L’, ‘f’, ‘8’ char StringStrings of characters, such as words and sentences placed in double quotes “Hello World!”, “asdf” string

37 Creating a Variable To create a variable you must give it a: 1.Type for type of data it will hold 2.Name by which variable can be referenced Variables can be set to different values throughout program Naming variables follows the same rules as naming your Motors and Sensors Giving a variable an initial value is called “initializing the variable”

38 Common Variable Uses Variables are useful for keeping track of loop iterations The following code lets the loop run 10x Note that the side on the right of the equal sign is evaluated first, then set to the variable on the left. This is always the case.

39 Common Variable Uses Variables are useful for keeping track of sensor or timer values at different parts of the program

40 Common Variable Uses Variables are useful for keeping track of results from complicated equations

41 Variable Exercises Create and initialize the following variables in your Labyrinth Program: –int motorSpeed –int encoderCount1 –int encoderCount2 –int encoderCount3 –int encoderCount4 Replace the corresponding values in your program with variables. –Download and run. What happens? –Try increasing and decreasing the speed of your robot only by adjusting the variable.

42 Variables Continued

43 More Data Types There are 8 different types of variables in ROBOTC –Integer (int) – Memory Usage: 16 bits / 2 bytes Integer Numbers Only Ranges in value from -32768 to +32767 –Long Integer (long) – Memory Usage: 32 bits / 4 bytes Integer Numbers Only Ranges in value from -2147483648 to +2147483647 –Floating Point (float) – Memory Usage: 32 bits / 4 bytes Integer or Decimal Numbers Variable precision, maximum of 4 digits after decimal

44 More Data Types There are 8 different types of variables in ROBOTC –Single Byte Integer (byte) – Memory Usage: 8 bits/1 byte Integer Numbers Only Ranges in value from -128 to +127 –Unsigned Single Byte Integer (ubyte) – Memory Usage: 8 bits / 1 byte Integer Numbers Only Ranges in value from 0 to +255 –Boolean Value (bool) – Memory Usage 4 bits /.5 bytes True (1) or False (0) values only.

45 More Data Types There are 8 different types of variables in ROBOTC –Single Character (char) - Memory Usage: 8 bits / 1 byte Single ASCII Character only Declared with apostrophe – ‘A’ –String of Character (string) - Memory Usage: 160 bits / 20 bytes Multiple ASCII Characters Declared with quotations – “ROBOTC” 19 characters maximum per string (NXT Screen limit)

46 Constants Some additional notes –Adding “const” in front of a variable will make that variable a constant. This will prevent the variable from being changed by the program –Constants do not take up any memory on the VEX IQ –The VEX IQ has room for 7500 bytes of variables

47 The M in STEM

48 Current method for moving the robot: –Guess-and-check - true for wait states and encoder rotations Do it smarter! –We can measure the distance for the robot to travel –We can measure the circumference of the wheel –We know 627 encoder counts = 1 wheel rotation –Spend 10 - 15 minutes working out a solution Use your idea to move forward and reverse –Discuss how well your ideas worked Now Consider Turning –Is having the robot turn 90 degrees the same as having the encoder turn 90 counts? Why or why not?

49 The M in STEM How did you solve the math problem? Some common methods: –Scale Factor (“Scaling” multiples of a known quantity) –Rate: Unit Ratio (# of X in a single Y, times the number of Y’s) “Rate” relationship Find #degrees/1in, then multiply that rate by the total What about #degrees per floor-line? –Rate: Raw Ratio (# of X per # of Y, times the number of Y’s) Find 360degrees/8.6in, then multiply that rate by the total –Direct Proportion (Traditional “ratio” equation) 8.6 in = 24 in 627 deg X deg Solved mechanically using cross-multiplication Solved algebraically as a Linear Equation of One Variable

50 Everyone is a Math Teacher How did you solve the math problem? Many STEM teachers will not be teaching in Math class, yet Math is clearly what they are teaching –Make the math explicit; don’t waste the opportunity! –Embrace multiple methods of solving the same problem; students need more tools at their disposal, not conflicting information about which ones are “better” than others (none are, use what works!)

51 Exercise: Precise Turning Now that you know how to calculate the encoder counts for any forward movement, can you apply that to point turns and swing turns? Figure it out! Keep in mind what “distance” the wheels are traversing to accomplish the turn. Be prepared to share your findings!

52 Programming Discussion The behaviors we are using work, but imagine using them to solve the Labyrinth. –Moving Forward or Turning with Encoders = ~8 lines –7 movements needed for the Labyrinth –7 x ~8 = ~56 Lines of code! Notice that the behaviors are mostly comprised of the same exact code, over and over. –What if there were a way to reuse the same set of code? –There is: Functions!

53 Functions

54 Functions are used to group together several lines of code, which can then be referenced many times in task main, or even other functions. Convenient uses in the Labyrinth –Moving Forward –Turning 90 Degrees Left –Turning 90 Degrees Right Creating Functions –Example: Moving Forward –Function Header (Part1 – The name of the function) –Function Definition (Part 2 – The code that goes with the function) Using Functions –Function Call (Part 3 – Where you want the function code to run)

55 Function Definition

56 Function Call

57 Function Practice Create Functions to: –Point Turn 90 Degrees Right –Point Turn 90 Degrees Left –Reverse for 2 Rotations Review VCVT Videos – Note: The code is different, but the process is the same

58 Advanced Functions What about the very similar lines of code that go with the automatically moving straight code? Is there a way to include those lines in the function? –What’s different each time we use the code? The number of degrees for the distance of the straight movement! What programming tool do we have that lets us store numbers in our code? Variables! Functions allow use for a special kind of variable, called a Parameter. Parameters allow you to pass values into functions to account for small differences, like the number of degrees to move forward.

59 Advanced Function Definition

60 Advanced Function Call

61 Advanced Function Practice Expand your previous functions : –Point Turn x Degrees Right –Point Turn x Degrees Left –Reverse for x Rotations Review VCVT Videos

62 Homework! Homework assignments can always be found and turned-in on CS2NLearn You’ll also be expected to watch a series of videos for review Optional practice quizzes will prepare you for the certification exam at the end


Download ppt "ROBOTC for VEX Online Professional Development Jesse Flot."

Similar presentations


Ads by Google