Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming VEX Cortex Robotics with ROBOTC

Similar presentations


Presentation on theme: "Programming VEX Cortex Robotics with ROBOTC"— Presentation transcript:

1 Programming VEX Cortex Robotics with ROBOTC
Penn State Abington Spring 2013 Version 1.6.1 Programming VEX Cortex Robotics with ROBOTC Topics Sensors Motors ROBOTC programming examples Sample VEX robot projects Spring 2013 Instructor: R. Avanzato

2 Robots 3 categories of robots Teleoperated (remote control)
Human operated No sensors needed on robot May or may not need on-board computer VEX is capable of remote control operation with a transmitter Example: surgical robots (at least for now) Semi-Autonomous Combination of human operation and computer control Computer can stop human from making bad control decision VEX is capable of semi-autonomous Some sensors required Microprocessor on robot required; software is required Autonomous Computer controlled; no human or operator control VEX is capable of autonomous control Sensors required Software generally more sophisticated; may require AI predator Davinci surgery Mars rover Roomba vacuum robot

3 VEX Sensors Sensors can be places into 2 general categories:
Digital (2 values: either “on” or “off”; 1 or 0) Analog (continuous range of values ex: light intensity, temperature) Pushbutton  digital (2 states: On or OFF; 1 or 0) Lever (limit) switch  digital Light sensor  analog (0 to 1024) Line -following (light) sensors  analog (0 to 1024) Ultrasonic/sonar sensor  “analog”; needs special hookup (0 to 255 in) What are applications of each? Understand difference between “digital sensor” (on/off only) and “analog sensor” (continuous range of values). How does it affect software? Each type of sensor is plugged into special ports on VEX

4 VEX Robot & Sensors (side view)
servomotor Potentiometer (pot) Top push button Light sensor Cortex controller Sonar (Ultrasound) Wheel encoder Front bump (push button) sensor PSU Abington VEX Squarebot with Sensors

5 VEX Robot & Sensors (back view)
potentiometer Servo LCD screen Optical Wheel (shaft) Encoder battery On/off power switch (slide up-down) PSU Abington VEX Squarebot with Sensors

6 VEX Robot & Sensors (bottom view)
3 floor light sensors Optical Wheel (shaft) Encoders (2) Drive motors (2) Wheels (4) PSU Abington VEX Squarebot with Sensors

7 VEX Robot & Sensors (front view)
Top bump sensor Light sensor Sonar (ultrasonic) Front bump sensor PSU Abington VEX Squarebot with Sensors

8 Warning!!! Program execution begins immediately after program is downloaded onto robot (unless you add a delay such as a 2 second delay) This can present a hazard to you and the robot. Do not let robot fall off table. Be prepared to stop robot every rime you download program. Robot will start moving wheels or moving arm immediately Place a delay or start button logic to allow you to place robot on floror before execution Wear safety glasses if you have a robot with a moving arm

9 ROBOTC Guide Connect robot to PC; turn on robot; last program stored will execute!!! Open ROBOTC  Always set Platform Type = VEX Cortex (default) In ROBOTC, select “Robot”  VEX Cortex Comm. Mode  USB only Use debugger windows (shows all motor, sensor and variable values) ROBOTC has automatic formatting feature (sets proper indentation) …click on the magic wand..use this feature Start with Sample Code or use this PPT; build on examples Configure all sensors in ROBOTC Select Robot  Motors and Sensors Setup Save all copies of programs; to other team members; share on Angel, Google, etc. First lines of every program must be “#pragma config” statements

10 VEX Guide Examples (ROBOTC)
Moving Forward with Time Moving Forward and Reverse with Time Bump Sensor Sonar (Ultrasonic) Sensor (ver 1 & ver 2) FOR loop Encoder (ver 1 & ver 2) Light sensor Pushbutton to start robot (ver 1 & ver 2) VEX Speaker (sounds and music) LCD (character display) Multi-robot communication (Xbee) Multi-robot communication (Xbee) with message ID

11 VEX Cortex Robot Ports Right Motor: Motor 2 Left Motor: Motor 3 Top Servo: Motor 4 Front Servo: Motor 5 Light sensor (front): Analog 1 Right line sensor: Analog 2 Center line sensor: Analog 3 Left line sensor: Analog 4 Potentiometer: Analog 5 Front Touch Sensor: Digital 1 Top Touch sensor: Digital 2 Sonar Input: Digital 3 Sonar Output: Digital 4 Right Encoder Front: Digital 5 Right Encoder Back: Digital 6 Left Encoder Front: Digital 7 Left Encoder Back: Digital 8 LED [Color may vary]: Digital 12 Xbee UART1 LCD: UART2 LCD RX: Yellow from Y cable LCD TX: White from Y cable

12 VEX Cortex Robot Ports #pragma config(Motor, port2, rightMotor, tmotorServoContinuousRotation, openLoop, reversed #pragma config(Motor, port3, leftMotor, tmotorServoContinuousRotation, openLoop) #pragma config(Motor, port4, topServo, tmotorServoStandard, openLoop) #pragma config(Motor, port5, frontServo, tmotorServoStandard, openLoop) #pragma config(Sensor, in1, light1, sensorReflection) #pragma config(Sensor, in2, lineRight, sensorLineFollower) #pragma config(Sensor, in3, lineCenter, sensorLineFollower) #pragma config(Sensor, in4, lineLeft, sensorLineFollower) #pragma config(Sensor, in5, pot, sensorPotentiometer) #pragma config(Sensor, dgtl1, frontTouch, sensorTouch) #pragma config(Sensor, dgtl2, topTouch, sensorTouch) #pragma config(Sensor, dgtl3, sonar, sensorSONAR_inch) #pragma config(Sensor, dgtl5, encoderRight1, sensorQuadEncoder) #pragma config(Sensor, dgtl7, encoderLeft1, sensorQuadEncoder) #pragma config(Sensor, dgtl12, led, sensorLEDtoVCC) //*!!Code automatically generated by 'ROBOTC' configuration wizard

13 RobotC Program General Format
#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop) #pragma config …… define sensors #pragma config …… define sensors // comments …documentation task main() { Instructions; // comments } //Program ends, and the robot stops NOTE: pragma config statements must be at top of program (before any comments statements)

14 VEX Example #1: Move Forward with Time
#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop) task main() { wait1Msec(2000); //Robot waits for 2000 milliseconds (2 seconds) //Move forward at full power for 3 seconds, then stop motor[rightMotor] = 127; // run at full (127) power forward motor[leftMotor] = 127; // run at full (127) power forward wait1Msec(3000); //Wait 3000ms (3 sec) -- motors stay on!! } //Program ends, and the robot stops // Note: be careful with parentheses and square brackets Questions: Why is motor on port2 reversed? What does that mean? What would happen if you omitted this? What if no motor is plugged into port 2? If right and left motors reversed? Motor speed ranges from 127 (max fwd) to 0 (stop) to -127 (max reverse) How do you make a robot turn? Does it depend on chassis construction? What is command to wait 1.5 seconds? seconds? seconds?

15 VEX Example #2 FWD and REV with Time
#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop) task main() { wait1Msec(2000); //Robot waits for 2000 milliseconds (2 seconds) //Move forward for 3 seconds (half power) motor[rightMotor] = 63; motor[leftMotor] = 63; wait1Msec(3000); //Move reverse for 2 seconds motor[rightMotor] = -63; motor[leftMotor] = - 63; wait1Msec(2000); } //Program ends, and the robot stops // How do you turn (pivot) robot?

16 VEX Program Example #3 Bump Sensor
#pragma config(Sensor, dgtl1, frontTouch, sensorTouch) #pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// // Move forward until robot hits front bump sensor, then stop // Assumes front bump sensor is connected to digital input port 1 // bump sensor value = 0 when not pressed; value = 1 when pressed (digital sensor) task main() { while (SensorValue[ frontTouch ] == 0) // loop until button is pressed motor[rightMotor] = 63; motor[leftMotor] = 63; } } // stop motors when program ends NOTE: In ROBOTC, configure sensors by selecting Robot  Motors and Sensors Setup

17 VEX Program Example #4 Sonar (v1)
#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop) #pragma config(Sensor, dgtl3, sonar, sensorSONAR_inch) // Assume SquareBot robot with sonar sensor on front of robot // Move robot forward for until ultrasonic (sonar) sensor detects object with 24 inches, // then, robot stops; Range of sonar 0  255 inches (sonar returns -1 when out of range) task main() { wait1Msec(2000); //Robot waits for 2 sec before executing program while(SensorValue[sonar] > 24) //Loop while further than 24 inches away motor[rightMotor] = 63; // You can change the speed motor[leftMotor] = 63; } } // Robot stops when program ends // Question: How do you deal with out-of-range (-1) condition?

18 VEX Program Example #4 Sonar (v2)
#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop) #pragma config(Sensor, dgtl3, sonar, sensorSONAR_inch) // Same operation as version 1, but turn off motors explicitly at end of program for clarity // This is unnecessary because motors are always turned off when program ends task main() { wait1Msec(2000); //Robot waits for 2 sec before executing program while(SensorValue[sonar] > 24) //Loop while further than 24 inches away motor[rightMotor] = 63; motor[leftMotor] = 63; } motor[rightMotor] = 0; // turn off motors explicitly (optional in this case) motor[leftMotor] = 0 ; // Note: you can explicitly turn off motors (as in above) for clarity, or all motors will be turned off automatically when program ends.

19 VEX Program Example #5 For Loop
#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop) // Move robot forward for 2 seconds, the reverse robot for 2 seconds , then repeat total of 5 times, // then, robot stops //NOTE: use a “for” loop when you want to repeat a fixed number of times task main() { wait1Msec(1000); //Robot waits for 1000 milliseconds (1 second) before starting for ( int k = 0; k < 5; k = k + 1) // loop (repeat 5 times; while k is less than 5) // k is a loop counter variable which is initially set at 0, and // increments by 1 for each loop; when k reaches 5, then the loop stops motor[rightMotor] = 63; //Move forward for 2 seconds (half power) motor[leftMotor] = 63; wait1Msec(2000); motor[rightMotor] = -63; //Move reverse for 2 seconds motor[leftMotor] = -63; } } // in the above example, if ‘k < 5’ was changed to ‘k < 10’ then the loop would repeat 10 times

20 VEX Example #6 Encoder (v1)
#pragma config(Sensor, dgtl5, encoderRight1, sensorRotation) #pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop) // Assume SquareBot robot with at least one encoder connected of robot // An encoder (also called shaft encoder, optical encoder) counts the number of times an axle or shaft or wheel (depends on gears) rotates // For the VEX kit, the encoder counts 100 ticks every time an axle makes one complete revolution // Encoders can be used to move more precisely (why?) // Encoders can be connected to one motor shaft or both motor shafts. // GOAL: Move forward until encoder reaches 100 counts (you can change this to any value), then stops task main() { wait1Msec(2000); // waits for 2000 milliseconds before executing program SensorValue[encoderRight1] = 0; // reset encoder value to 0 while (SensorValue[encoderRight1] < 100) // move forward until encoder count >= 100 motor[rightMotor] = 63; motor[leftMotor] = 63; } } // Robot stops when program ends // Note: there is some overshoot when motor power is set to off. How to fix?

21 VEX Example #6 Encoder (v2)
#pragma config(Sensor, dgtl5, encoderRight1, sensorRotation) #pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop) // Assume SquareBot robot with at least one encoder connected of robot /// GOAL: Move forward until encoder reaches 100 counts (you can change this to any value), then rotate robot for 50 counts, then stop robot task main() { wait1Msec(2000); // waits for 2000 milliseconds before executing program SensorValue[encoderRight1] = 0; // reset encoder value to 0 while (SensorValue[encoderRight1] < 100) // move forward until encoder count >= 100 motor[rightMotor] = 63; motor[leftMotor] = 63; } SensorValue[encoderRight1] = 0; // reset encoder value to 0 ****** important step ********* while (SensorValue[encoderRight1] < 50) // move forward until encoder count >= 100 motor[leftMotor] = -63; } // Robot stops when program ends // Note: there is some overshoot when motor power is set to off. How to fix?

22 VEX Example #7 Light Sensor
#pragma config(Sensor, in1, light1, sensorReflection) #pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop) // A light sensor is plugged into any of the analog ports of the VEX controller // There are 2 sets of light sensors on the VEX robots; 1 ) fwd facing light sensor (in1) and 2) a set of 3 downward line following light sensors (in2, in3, in4) // Note: light sensor values (0 = bright light; 1024 = dark) task main() { wait1Msec(1000); //Robot waits for 1s before executing program while (SensorValue[ light1 ] < 500) // while bright is light, go forward motor[rightMotor] = 63; motor[leftMotor] = 63; } } // when light gets dark, loop ends, and program ends and motors stop

23 VEX Example #8A : Pushbutton To Start Code (v1)
#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop) #pragma config(Sensor, dgtl2, topTouch, sensorTouch) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// // Use pushbutton at top of robot to start the program instead of using time delay // Assume pushbutton at top of robot is plugged into digital input port 2 task main() { while (SensorValue [topTouch] == 0) // loop while button is not pressed { } // do nothing (just wait) wait1Msec(1000); motor[rightMotor] = 63; // go forward after the button is pressed by user motor[leftMotor] = 63; wait1Msec(1000); // go for 1 second , then stop when program ends }

24 VEX Example #8B : Pushbutton To Start Code (v2)
#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop) #pragma config(Sensor, dgtl2, topTouch, sensorTouch) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// // Use pushbutton at top of robot to start the program instead of using time delay // Assume pushbutton at top of robot is plugged into digital input port 2 task main() { while ( 1 ) // loop forever if (SensorValue [topTouch] == 1) break; } wait1Msec(1000); motor[rightMotor] = 63; // go forward after the button is pressed by user motor[leftMotor] = 63; wait1Msec(1000); // go for 1 second , then stop when program ends

25 VEX Example #9 Speaker (sounds)
task main() { //Basic "Play Sound" commands PlaySound(soundBeepBeep); wait1Msec(200); //Play a tone: //First Parameter: Frequence in Hz //Second Parameter: Length to play in 1/100th of a seconds (50 = .5 seconds) PlayTone(440, 50); wait1Msec(500); //Play a Sound File (need to use the File Management to Upload First) PlaySoundFile("1.wav"); wait1Msec(1000); } Note: see ROBOTC documentation for more examples and songs for speaker

26 VEX Example #10 LCD Display
#pragma config(Sensor, in1, light1, sensorReflection) // A light sensor is plugged into any of the analog ports of the VEX controller // This program display the value of the light sensor on the VEX robot LCD display // Note: light sensor values (0 = bright light; 1024 = dark) task main() { wait1Msec(1000); //Robot waits for 1s before executing program displayLCDCenteredString(0, "LightSensor"); // display message on line 0 while(true) displayLCDNumber(1,0,SensorValue(light1)); //display value for the light sensor wait1Msec(200); clearLCDLine(1); // clear screen and repeat (clear line 1) } Note: You can display the values of any sensor or display any message to the LCD screen. It is not necessary to put the display command in a loop – it depends on the application. There are 2 lines on the display (designated 0 and 1) and there is a maximum of only 16 characters (letters or numbers) which can be displayed on each line.

27 VEX Example #11A Multi-Robot (XBee) Robot A
#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop) #include "XbeeTools.h " // need this line for Multi-robot task main() { InitRS232(uartOne,baudRate9600); // need this line for Multi-robot wait1Msec(1000); string message = “RobotB Go"; SendString(message); // send message to Robot B to move motor[rightMotor] = 63; motor[leftMotor] = 63; wait1Msec(1000); //Move forward for 1 second } //Program ends, and the robot stops

28 VEX Example #11B Multi-Robot (XBee) Robot B
#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop) #include "XbeeTools.h " // need this line for Multi-robot task main() { InitRS232(uartOne,baudRate9600); // need this line for Multi-robot wait1Msec(500); string message; ReceiveString(message); // wait here for any message from Robot A, then go motor[rightMotor] = 63; motor[leftMotor] = 63; wait1Msec(1000); //Move forward for 1 second } //Program ends, and the robot stops

29 VEX Example #12 Multi-Robot (XBee) Robot B with ID
#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop) #include "XbeeTools.h " // need this line for Multi-robot task main() // this version of Robot B waits for a message with unique ID { InitRS232(uartOne,baudRate9600); // need this line for Multi-robot wait1Msec(500); string message; // declare a variable which can store a string (text) while(true) // keep repeating forever (or until you receive correct message) ReceiveString(message); // wait for a message from RobotA; store in message if (StringFind(message, "RobotB") >= 0) // if message contains “RobotB” then move; { // otherwise, wait for another message motor[rightMotor] = 63; motor[leftMotor] = 63; wait1Msec(1000); //Move forward for 1 second break; // break out of while loop and stop robot } } //Program ends, and the robot stops

30 VEX Example #12 MR String Commands
Xbee wireless commands always send and receive strings (not numbers). Remember, when you transmit a command, every robot within 300 feet will receive it – be careful. (Try to use unique robot names and put names in commands) Some numbers must be converted to string before transmitting, and strings need to be converted back to numbers after receiving them. It is sometimes useful to send compound commands, such as “FWD500” to command another robot to move forward for 500 milliseconds, etc. Here is another example of a compound command “JOE FWD500”  this tells robot named JOE to go forward for 500 milliseconds A string is an array of characters. The first character is at index = 0 Reference: Here are the important string functions for MR communication string message; // declare variable message to be a string SendString(message); // transmits string and continue to next line ReceiveString (message); // waits here until a string is received StringFormat( message, “FWD%d”, time); //inserts integer into string StringFind( message, “JOE”); // returns index of where string is found. This function returns a -1 if string is not found; if != -1, then found int t = atoi( myString ); // atoi() converts string into integer number StringDelete(inputString, startPos, size); // deletes portion of a string;

31 VEX Example #12 MR String Commands
string message; int count = 350; StringFormat(message, “%d”, count); // would produce string  “350” (stored in message) Example #2 int count = 350; StringFormat(message, “FWD%d”, count); // would produce string  “FWD350” Example #3 if (StringFind (message, “FWD”) != -1) //must have been found { goForward(); } Example #4 if (StringFind (message, “FWD”) != -1) // “FWD” must have been found in message { DeleteString(message, 0, 3); // delete the “FWD” part of the string (start at 0, delete 3 chars) int count = atoi(message); // convert string number into an integer }

32 Sample Programming Problems
Robot moves forward for 5 seconds, then stops Robot pivots for 3 seconds, then stops Robot moves forward until robot hits bump sensor, then stops Robot pivots until sonar senses object within 2 feet Robot moves forward for 2 seconds, stops for 1 second, then repeats these two actions 5 times, then stops Robot moves forward for 2 seconds, then reverse for 2 seconds….repeats these 2 tasks forever (robot never stops) Robot follows a white line on a black background Robot moves to a white line, then stops Robot moves forward and counts white lines on surface Robot moves a fixed distance using encoders Be able to write C code for these examples – and extend It is not necessary to have any background in C programming to do these exercises; ask instructor for help if needed. Reference: ROBOTC website


Download ppt "Programming VEX Cortex Robotics with ROBOTC"

Similar presentations


Ads by Google