Transcription of RobotC for VEX-IQ - Storming Robots
1 RobotC for VEX-IQ Computational Thinking and Engineering For Kids 1 | P a g e Date: August of 2014 Properties of Storming Robots - An introduction to programming in RobotC using VEX-IQ hardware RobotC for VEX-IQ Computational Thinking and Engineering For Kids 2 | P a g e Before You Start 4 SCOPE 4 BACKGROUND NEEDED: 4 DOWNLOAD FIRMWARE 4 Chatper 1 - Using the Configuration Tool 5 Chapter 2 - Debugging 7 USING THE LCD DISPLAY 7 USING SOUND 7 USING THE DEBUGGER 7 WATCH IT STEP BY STEP 8 WARM UP - CHALLENGE 10 Chapter 3 - Motors 13 ) KNOW YOUR DEVICE 13 ) MOST COMMONLY USED FUNCTIONS 13 ) CODE SAMPLE SEGMENT 14 Chapter 4 - Bumper Sensor 15 ) KNOW YOUR SENSOR 15 ) MOST COMMONLY USED FUNCTIONS 15 ) CODE EXAMPLE SEGMENT15 Chapter 5 - Touch LED Sensor 16 Chapter 6 - Color Sensor 17 ) KNOW THE SENSOR 17 TO CONFIGURE TO GRAYSCALE - SENSORVEXIQ_COLORGRAYSCALE 17 TO CONFIGURE TO COLOR HUE - SENSORVEXIQ_COLORHUE 18 TO CONFIGURE TO COLOR HUE - SENSORVEXIQ_COLOR12 COLOR 18 ) MOST COMMONLY USED FUNCTIONS 19 ) CODE SAMPLE SEGMENT 20 Chapter 7 - Distance Sensor 21 )
2 KNOW THE SENSOR 21 ) MOST COMMONLY USED FUNCTIONS 21 ) CODE SAMPLE SEGMENT 23 ) OTHER APIS 23 Chapter 8 - Gyro Sensor 24 ) KNOW YOUR SENSOR 24 ) MOST COMMONLY USED FUNCTIONS 25 ) GYRO SENSOR CODE SAMPLE 26 Chapter 9 Motor Encoder 27 ) KNOW YOUR DEVICE 27 ) MOST COMMONLY USED FUNCTIONS 27 RobotC for VEX-IQ Computational Thinking and Engineering For Kids 3 | P a g e ) CODE SEGMENT SAMPLE 30 Chapter 10 Feedback Control (PID) 31 Fundamentals of PID Controller 32 Using PID Algorithm. 32 TARGET POWER (TP) 33 TARGET VALUE (TVAL) 34 PROPORTIONAL FACTOR (KP) 34 INTEGRAL FACTOR (KI) 34 DERIVATIVE FACTOR (KD) 36 Steps to tune 37 STEP 1) TUNE THE KP FIRST. 37 STEP 2) TUNE THE KI. 37 STEP 3) TUNE THE KD. 38 THINGS WILL CHANGE YOUR TUNING FACTORS 38 RobotC for VEX-IQ Computational Thinking and Engineering For Kids 4 | P a g e There are over hundreds of APIs.
3 You should look into the Functions Explorers and their help windows to explore them. Most of them are listed in the TMotorTypes or In this tutorial, only the most commonly used APIs and basic tools are covered in this tutorial. You should always consult the Functions Explorers. You often can right-click on a particular function or system variables to find out what other APIs, RobotC / VEX-IQ data types, etc. available. 1. You should have already known some basics in programming in C/C++: o Primitive data types: char, int, float, long, bool, o Some pre-processor directives: #define , #undef, #include o Basics in control structure statements: o if .. else if .. Else o switch o while .. do { } while o Single array 2. Basics in working with functions: o void func() o void func(int.)
4 O int func(..) o bool func(..), etc. 3. More advanced ones will be good too: o typedef o Simple pointers o Simple structure o typedef enum In order to be able to begin programming your VEX-IQ robot, you must first download the most up to date firmware from the VEX website. Go to , and follow the instructions to get started. (You must also download RobotC firmware before programming. Simply open RobotC and select Firmware Download from the top of the screen.) This tutorial was written based on Version , and VEX IQ Firmware Update Utility for Windows dated July of 2014. You must follow their installation process of this firmware because it may actually need to update the firmware of the sensors and motor devices as well.
5 Scope Background needed: Download Firmware RobotC for VEX-IQ Computational Thinking and Engineering For Kids 5 | P a g e The following preprocessor directives will be inserted at the top of your program: #pragma config(Motor, motor9, LM,tmotorVexIQ, PIDC ontrol, encoder) #pragma config(Motor, motor10,RM,tmotorVexIQ, PIDC ontrol, encoder) #pragma config(Motor, motor11,ARM,tmotorVexIQ, PIDC ontrol, encoder) You may also do these yourself: #define LM motor9 #define RM motor10 .. etc. setMotorEncoderUnits(encoderCounts); SensorType[LM] = tmotoVexIQ; setMotorBrakeMode(LM, motorBrake); ..etc. RobotC for VEX-IQ Computational Thinking and Engineering For Kids 6 | P a g e The following preprocessor directives will be inserted at the top of your program: #pragma config(Sensor, port1, Leye, sensorVexIQ_ColorGrayscale) #pragma config(Sensor, port2, Reye, sensorVexIQ_ColorGrayscale) #pragma config(Sensor, port3, Lping, sensorVexIQ_Distance) #pragma config(Sensor, port4, Rping, sensorVexIQ_Distance) #pragma config(Sensor, port5, Gyro, sensorVexIQ_Gyro) #pragma config(Sensor, port6, DiscoLight, sensorVexIQ_Color12 Color) #pragma config(Sensor, port7, FrontBumper, sensorVexIQ_LED) #pragma config(Sensor, port8, BackBumper, sensorVexIQ_Touch) You may also do these yourself.
6 #define Reye port1 #define Leye port2 .. etc. SensorType[Reye] = sensorVexIQ_ColorGrayscale; SensorType[Lping] = sensorVexIQ_Distance; SensorType[Rping] = sensorVexIQ_Distance; SensorType[Gyro] = sensorVexIQ_Gyro; SensorType[DiscoLight] = sensorVexIQ_Color12 Color; SensorType[FrontBumper] = sensorVexIQ_LED; SensorType[BackBumper] = sensorVexIQ_Touch; RobotC for VEX-IQ Computational Thinking and Engineering For Kids 7 | P a g e The LCD measures 46 x 128 pixels ( 0<= x <= 127, 0<= y <=45 ) You may display data during execution: displayTextLine(1, "Hello "); displayTextLine(3, %d: %s , 10, Storming Robots ); You should look up all the display functions from the Function explorers. Play a sound to signal a certain stage of execution can also be very informative too if you use wisely.
7 PlaySound(TSounds); where Tsounds can be in many values such as soundTada, soundSiren2, Right click on the function in the IDE, the IDE will open where you will find the sounds available. You can play with playSoundfile(..) and playNote(..), but these usually are not very useful in real time debugging. RobotC comes with probably most powerful debugger available for microcontroller. You program must be compiled with no errors and downloaded to the controller in order to watch the screen debugger: Using the LCD Display Using Sound Using the Debugger RobotC for VEX-IQ Computational Thinking and Engineering For Kids 8 | P a g e Set breakpoint Step into each expressions. Watch the change of the variable here.
8 Watch it step by step RobotC for VEX-IQ Computational Thinking and Engineering For Kids 9 | P a g e RobotC for VEX-IQ Computational Thinking and Engineering For Kids 10 | P a g e Challenge 1: Write a single program to do the following: - Display your name. - Play a sound. Wait for one second. - Display your school name on the next line. - Play another sound, Wait for one second. - Display your favorite event, followed by another sound. Wait for another one second. - End the program. Challenge 2: Write a program to create a variable called ct . Create a loop so that your screen will display the following: Challenge 3: Draw a small rectangle with coordinates with top left point at (10, 20) and bottom right point at (25, 5), . drawRect(10,20, 25, 5).
9 Make it expand to the max size of screen, then shrink back with dimension of 1 pixel width and height. Challenge 4: Given the radius. Generate Circumference and Area of a circle. Only 2 decimal places. Do it in one s increment four times. Warm Up - challenge 1 2 3 4 5 6 7 8 R Cir Area ================ RobotC for VEX-IQ Computational Thinking and Engineering For Kids 11 | P a g e Challenge 5: Given the width and length of a rectangle. Generate the area of the biggest circle to fit inside this rectangle. Challenge 6: Draw the rectangle, as well as the painted complete circle fit inside the rectangle. Challenge 7: Given the measurement of base and height of a right angle triangle, find out the measurement of the hypotenuse of the triangle.
10 (Pythagorean Theorem) W L CirA 2 W L CirA 50 60 B H Hyp 50 60 RobotC for VEX-IQ Computational Thinking and Engineering For Kids 12 | P a g e Challenge 8 : (for the advanced) 1. Write a function that outputs a right-side-up triangle of height n and width 2n-1; the output for n = 6 would be: 2. Write a program to calculate a gcd with two numbers using Euclid Algorithm. This is how it works: N M R= N % M 300 64 44 = 300 % 64 64 44 20 = 64 % 44 44 20 4 = 44 % 20 20 4 0 = 20 % 4 3. Write a program to calculate a square root of a number using Newton s method. This is how it works: user wishes to find out square root of 3: N N/M M Average of N/M and M 3 3 1 2 2 Your get your answer when: | M (current) M (previous) | <= M * 110000 * ** ** ** ** ** RobotC for VEX-IQ Computational Thinking and Engineering For Kids 13 | P a g e Type : sensorVexIQ_Motor You may set the ports to many other type of devices.