Presentation is loading. Please wait.

Presentation is loading. Please wait.

By Willem Scholten Learning Access Institute

Similar presentations


Presentation on theme: "By Willem Scholten Learning Access Institute"— Presentation transcript:

1 By Willem Scholten Learning Access Institute
RobotC for VEX By Willem Scholten Learning Access Institute

2 RobotC for VEX Section 1 - RobotC How to switch between VEX 2.0 Cortex and VEX IQ Section 2 - RobotC Firmware Updating Section 3 - Establishing VEXnet Link Section 4 - Paring Remote and Cortex Section 5 - RobotC Examples - basic telex robot Section 6 - RobotC and Basic Drive Bot Section 7 - VEX Robotics - RobotC Competition Template

3 RobotC How to switch between VEX 2.0 Cortex and VEX IQ
By Willem Scholten Learning Access Institute

4 Goto the Robot menu

5 Goto the Platform Type

6 Now select platform: — VEX 2.0 Cortex (For VEX) — VEX IQ

7 VEX IQ — Now select platform Controller Mode:
— TeleOp - when driven by remote control — Autonomous - when driving in autonomous mode

8 VEX — Select Communication mode:
— VEXnet or USB is typical good, so it will work with field control system as well as via USB cable tethered to Computer to get DEBUG stream

9 RobotC - Firmware Updating
By Willem Scholten Learning Access Institute

10 Firmware Updating Turn the Cortex OFF.
1 — VEX Cortex USB Port Plug the other end of the USB A-to-A cable into the USB port on the VEX Cortex. 2 — Plug the other end into your computer USB port

11 Firmware Updating Goto Robot -> Download Firmware ->
Automatically Update VEX Cortex

12 Firmware Updating IF all is OK, press the OK button not proceed.
IF update is required, the update will be applied, if the cortex is up-to-date you will informed as well

13 Firmware Updating Turn the VEXnet Remote Control OFF
1 — VEX Remote USB Port Plug the other end of the USB A-to-A cable into the USB port on the VEX Remote. 2 — Plug the other end into your computer USB port

14 Firmware Updating

15 Firmware Updating IF all is OK, press the OK button not proceed.
IF update is required, the update will be applied, if the remote is up-to-date you will informed as well

16 By Willem Scholten Learning Access Institute
Establish VEXnet Link By Willem Scholten Learning Access Institute

17 Establish VEXnet Link Install Batteries in the VEXnet Remote Control. Remove the battery cover plate on the remote control. Install 6 AAA batteries, and replace the battery cover plate. Do not power the remote control ON. Connect a Battery to the Cortex Connect a 7.2V robot battery to the Cortex, but do not power it ON.

18 Establish VEXnet Link 1 — Tether the USB port on the VEXnet Remote Control to the USB port on the Cortex using a USB A-to-A cable. 2 — VEXnet Remote Control USB Port Plug one end of the USB A-to-A cable into the USB port on the VEXnet Remote Control. 3 — VEX Cortex USB Port Plug the other end of the USB A-to-A cable into the USB port on the VEX Cortex.

19 Establish VEXnet Link Status LEDs — The ROBOT and VEXnet LEDs will blink green once the Cortex and VEXnet Remote Control have successfully paired. Power the Cortex ON. After a few seconds, ROBOT and VEXnet LEDs will blink green, indicating that the Cortex and VEXnet Remote Control have successfully paired.

20 Establish VEXnet Link Remove the USB A-to-A cable from the VEXnet Remote Control and Cortex. Turn the Cortex OFF.

21 Establish VEXnet Link VEXnet USB Keys —
Insert VEXnet USB Keys into the VEXnet Remote Control and Cortex. Note: It does not matter which VEXnet USB Key you insert into the Cortex versus the VEXnet Remote Control. Pairing the Cortex and VEXnet Remote Control establishes the link; the VEXnet USB Keys act as antennas for the link.

22 Establish VEXnet Link Power the Cortex and Remote Control ON. After roughly 15 seconds, the ROBOT and VEXnet LED’s will blink green, indicating that the VEXnet communication link has been established. Turn the Cortex ON. Turn the VEXnet Remote Control ON

23 Establish VEXnet Link Status LEDs —
After roughly 10 seconds, the ROBOT and VEXnet status LEDs will start blinking green. With the VEXnet link established, you should power OFF your Cortex and VEXnet Remote Control to preserve battery.

24 For VEX Cortex Using Remote Control in TeleOp
RobotC Examples For VEX Cortex Using Remote Control in TeleOp

25 Typical Program Layout
A basic RobotC program - either Telop or Autonomous consists of four key blocks: #pragma section - define the motors and sensors Program Description Comment Block Task Main() Functions

26 #pragma section - define the motors and sensors
#pragma config(Sensor, dgtl1, limitSwitchUp, sensorTouch) #pragma config(Sensor, dgtl2, limitSwitchDown, sensorTouch) #pragma config(Motor, port2, rightMotor, tmotorServoContinuousRotation, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorServoContinuousRotation, openLoop) #pragma config(Motor, port6, armMotor, tmotorServoContinuousRotation, openLoop) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// The motor and sensor definitions must be at the top of the program, and is created using the Motor and Sensor ‘painter’ in the RobotC programming toolbar.

27 Pragma Section Two limit switches are defined, plugged into the digital port #1 and #2 on the Cortex #pragma config(Sensor, dgtl1, limitSwitchUp, sensorTouch) #pragma config(Sensor, dgtl2, limitSwitchDown, sensorTouch)

28 Pragma Section #pragma config(Motor, port2, rightMotor, tmotorServoContinuousRotation, openLoop, reversed) #pragma config(Motor, port3, leftMotor, tmotorServoContinuousRotation, openLoop) #pragma config(Motor, port6, armMotor, tmotorServoContinuousRotation, openLoop) Three motors are defined - two are for the drive train, and notice one is set to ‘reverse’. A third motor is for the lifting arm movement.

29 Program Description /*——————————————————————————————————————————— *\ |* Dual Joystick Control with Arm - 2 Remotes *| |* ROBOTC on VEX 2.0 Cortex *| |* *| |* This program uses the Left and the Right joysticks on Remote Control 1 to run the robot using *| |* "tank control". The Group 6 buttons on the top-right of Remote Control 2 are used to raise and *| |* lower the arm *| |* *| |* ROBOT CONFIGURATION *| |* NOTES: *| |* 1) Ch1 is the X axis and Ch2 is the Y axis for the RIGHT joystick *| |* 2) Ch3 is the Y axis and Ch4 is the X axis for the LEFT joystick *| |* MOTORS & SENSORS: *| |* [I/O Port] [Name] [Type] [Description] *| |* Motor - Port rightMotor VEX Motor Right motor *| |* Motor - Port leftMotor VEX Motor Left motor *| |* Motor - Port armMotor VEX Motor Arm motor *| \*—————————————————————————————————————————— */ Put a clear description of what your program does in the top, including any major revisions you may have made, and who is the author of the code.

30 task Main() task main() { while(1 == 1) //Driving Motor Control with Remote Control 1 motor[leftMotor] = vexRT[Ch3] / 2; motor[rightMotor] = vexRT[Ch2] / 2; //Arm Control with Remote Control 2 - allow up movement // as long as the upper limit switch is not pressed if(vexRT[Btn6UXmtr2] == 1 && SensorValue(limitSwitchUp) == 0) motor[armMotor] = 40; } // Allow down movement as long as bottom limit switch is not pressed else if(vexRT[Btn6DXmtr2] == 1 && SensorValue(limitSwitchDown) == 0) motor[armMotor] = -40; else motor[armMotor] = 0; task Main() is the main control task which is excited. Any robot control events you want to occur must be within this task statement.

31 Btn7U Btn7L Btn7R Btn8U Btn7D Btn8L Btn8R Btn8D Ch4 Ch1 Ch3 Ch2 Btn6U
(X-axis) Ch1 (X-axis) Ch3 (Y-axis) Ch2 (Y-axis) Btn6U Btn5U Btn6D Btn5D

32 task Main() task main() { while(1 == 1) //Driving Motor Control with Remote Control 1 motor[leftMotor] = vexRT[Ch3] / 2; motor[rightMotor] = vexRT[Ch2] / 2; //Arm Control with Remote Control 2 - allow up movement // as long as the upper limit switch is not pressed if(vexRT[Btn6UXmtr2] == 1 && SensorValue(limitSwitchUp) == 0) motor[armMotor] = 40; } // Allow down movement as long as bottom limit switch is not pressed else if(vexRT[Btn6DXmtr2] == 1 && SensorValue(limitSwitchDown) == 0) motor[armMotor] = -40; else motor[armMotor] = 0; while( 1 == 1) This is an infinite execute loop. It is needed to continuously readout the Button actions on the Remote Control. If you don’t have this, the buttons would be read once and that is the end of it….

33 task Main() First in the loop, we control the drive motors. leftMotor is controlled by JoyStick Ch3 (Left JoyStick Y-axis), the rightMotor is controlled by JoyStick Ch2 (Left JoyStick X-axis). The position of the JoyStick which is from 127 — -127 is halted, does effectively driving the robot at half speed. task main() { while(1 == 1) //Driving Motor Control with Remote Control 1 motor[leftMotor] = vexRT[Ch3] / 2; motor[rightMotor] = vexRT[Ch2] / 2; //Arm Control with Remote Control 2 - allow up movement // as long as the upper limit switch is not pressed if(vexRT[Btn6UXmtr2] == 1 && SensorValue(limitSwitchUp) == 0) motor[armMotor] = 40; } // Allow down movement as long as bottom limit switch is not pressed else if(vexRT[Btn6DXmtr2] == 1 && SensorValue(limitSwitchDown) == 0) motor[armMotor] = -40; else motor[armMotor] = 0;

34 task Main() task main() { while(1 == 1) //Driving Motor Control with Remote Control 1 motor[leftMotor] = vexRT[Ch3] / 2; motor[rightMotor] = vexRT[Ch2] / 2; //Arm Control with Remote Control 2 - allow up movement // as long as the upper limit switch is not pressed if(vexRT[Btn6UXmtr2] == 1 && SensorValue(limitSwitchUp) == 0) motor[armMotor] = 40; } // Allow down movement as long as bottom limit switch is not pressed else if(vexRT[Btn6DXmtr2] == 1 && SensorValue(limitSwitchDown) == 0) motor[armMotor] = -40; else motor[armMotor] = 0; Second - Now we read the action on Button Group 6 and control the armMotor. Notice -we swapped to Remote Control #2 - the partner remote. But notice we are not just reading the RemoteControl state we are also adding in the state of two limitSwitches.

35 task Main() Get the State of the limitSwitch Address button
if(vexRT[Btn6UXmtr2] == 1 && SensorValue(limitSwitchUp) == 0) Get the State of the limitSwitch 0 = not pressed 1 = pressed Address button on Remote 2 Append Xmtr2 to any button name and you are on the second (partner) remote. && - logic AND In this scenario, when Btn6U on Remote 2, is pressed and as long as the limitSwitch is not depressed you can move the arm up. As soon as the limitSwitch is pressed, you can press Btn6U all you want, but there will be no longer a motor movement upwards.

36 RobotC and Basic Drive Bot
By Willem Scholten Learning Access Institute

37 Basic Drive Bot Setup

38 Basic Drive Bot Setup Things to note:
Two mother used for driving LEFT DRIVE MOTOR and RIGHT DRIVE MOTOR Two motors used for actuators - CLAW MOTOR and ARM MOTOR

39 Basic Drive Bot Setup

40 Basic Drive Bot Setup NOTE: the LEFT and RIGHT DRIVE MOTORS are directly connected to either port #1 or port #10. These ports have build in speed control. Ports are 3 wire ports and any MOTOR plugged in to those require the use of a PWM motor controller 29

41 Basic Drive Bot Setup Motor Controller Module — The Motor Controller 29 regulates the speed of a VEX motor based on a signal it receives from a VEX Microcontroller. This allows for control of any VEX motor that doesn’t have a built- in motor controller, such as the VEX 2-wire motors.

42 Basic Drive Bot Setup

43 Basic Drive Bot Setup

44 Basic Drive Bot Setup RobotC setup and configuration for the Basic Drive Bot Assuming motors are wired as shown in previous slides (Remember that can be any robot of your design as long as the motors are functioning and performing the “tasks” as shown) You have RobotC installed on your computer, and configured for VEX Cortex as shown earlier.

45 Basic Drive Bot Setup Steps we need to undertake in RobotC:
1 - define all the motors and connections 2 - create basic drive code using a while loop which continuously reads the remote control, and send actions to the motors based on the state of buttons and joysticks on the controller.

46 Basic Drive Bot Setup

47 Basic Drive Bot Setup Start a NEW PROGRAM

48 Define your CORTEX setup
Basic Drive Bot Setup Define your CORTEX setup

49 Define which ports the motors are plugged into
Basic Drive Bot Setup Define which ports the motors are plugged into When done, apply / and press OK This will create a special section in your code called #pragma

50 #PRAGMA section which is auto generated for you
Basic Drive Bot Setup #PRAGMA section which is auto generated for you

51 Basic Drive Bot Setup Robot Definition Pragma Section
Define your Robot configuration - motors, sensors etc Robot Definition Pragma Section while 1 == 1 Main control loop - run forever and scan the remote for any button changes, and act upon them This program does not end until you turn off the power Read continuously the state of the buttons, and take actions

52 Basic Drive Bot Setup Define your Robot configuration - motors, sensors etc while 1 == 1 LEFT and RIGHT motor get input for speed directly from Joystick F In the main loop there are IF THEN ELSE statements for each button you want to define and an action associated with it. IF vexRT[Btn5U] == 1 T Take Action

53 #pragma config(Motor, port1, leftMotor, tmotorVex393_HBridge, openLoop, reversed)
#pragma config(Motor, port6, clawMotor, tmotorVex393_MC29, openLoop, reversed) #pragma config(Motor, port7, armMotor, tmotorVex393_MC29, openLoop, reversed) #pragma config(Motor, port10, rightMotor, tmotorVex393_HBridge, openLoop) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// task main () { while(1 == 1) //Driving Motor Control motor[leftMotor] = vexRT[Ch3] / 2; motor[rightMotor] = vexRT[Ch2] / 2; // Raise, lower or do not move arm if(vexRT[Btn5U] == 1) //If button 5U is pressed... motor[armMotor] = 127; //...raise the arm. } else if(vexRT[Btn5D] == 1) //Else, if button 5D is pressed... motor[armMotor] = -127; //...lower the arm. else //Else (neither button is pressed)... motor[armMotor] = 0; //...stop the arm. // Open, close or do not more claw if(vexRT[Btn6U] == 1) //If Button 6U is pressed... motor[clawMotor] = 127; //...close the gripper. else if(vexRT[Btn6D] == 1) //Else, if button 6D is pressed... motor[clawMotor] = -127; //...open the gripper. motor[clawMotor] = 0; //...stop the gripper.

54 VEX Robotics - RobotC Competition Template
By Willem Scholten Learning Access Institute

55 VEXnet Competition Code
Things to remember: You must name your Robot - this must be your team number You must use the supplied Competition Template for your code If you are doing the Skill Challenge, you must also use the Skill Challenge Template for that code

56 VEXnet Competition Code
Set your Robot name - Robot -> Advanced Tools -> Rename Robot (make sure your PC is communicating with the Cortex when doing this) Set the new name - it should be your team number!

57 VEXnet Competition Code
Writing Competition Code: Open the the competition_template from the Sample file menu Once opened - first do a Save As and re-name the template to something else and stored on your computer in a folder you can remember.

58 Competition Code filename: VEXcompetitionV01.c

59 VEXnet Competition Code
Define your Robot configuration - motors, sensors etc Robot Definition Pragma Section In this section you should not change ANYTHING if you do your robot will not connect or interface with competition control system Competition System Pragma Section Code that runs one at power up to pre-position a claw for example Pre-Autonomous Section Code that runs during the autonomous portion of the competition Autonomous Task Section Code that runs during the Tele-op portion of the competition - make sure you ahem a while(true) loop reading constantly the remote control buttons! User Control Task Section

60 VEXnet Competition Code
Robot definitions created using the the Motor and Sensor Setup Wizard -> should match exactly what you have configured on your robot and plugged into the Cortex Controller #pragma config(Motor, port1, leftMotor, tmotorVex393_HBridge, openLoop) #pragma config(Motor, port10, rightMotor, tmotorVex393_HBridge, openLoop, reversed) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

61 VEXnet Competition Code
In this section you should not change ANYTHING if you do your robot will not connect or interface with competition control system //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// #pragma platform(VEX) //Competition Control and Duration Settings #pragma competitionControl(Competition) #pragma autonomousDuration(20) #pragma userControlDuration(120) #include "Vex_Competition_Includes.c" //Main competition background code...do not modify! /////////////////////////////////////////////////////////////////////////////////////////

62 VEXnet Competition Code
Code that runs one at power up - in this case we move the robot forward for 3 seconds at half speed - in reality this should be for pre-positioning say a servo to have claw grab the cube. ///////////////////////////////////////////////////////////////////////////////////////// // // Pre-Autonomous Functions // You may want to perform some actions before the competition starts. Do them in the // following function. void pre_auton() { // Set bStopTasksBetweenModes to false if you want to keep user created tasks running between // Autonomous and Tele-Op modes. You will need to manage all user created tasks if set to false. bStopTasksBetweenModes = true; // All activities that occur before the competition starts // Example: clearing encoders, setting servo positions, ... // Willem: in this case we just have the robot move forward a bit to demonstrate // how the pre_autorun works. wait1Msec(2000); // Robot waits for 2000 milliseconds before executing program // Move forward at half power for 3 seconds motor[rightMotor] = 60; // Motor on port2 is run at full (127) power forward motor[leftMotor] = 60; // Motor on port3 is run at full (127) power forward wait1Msec(3000); // Robot runs previous code for 3000 milliseconds }

63 VEXnet Competition Code
Read this parameter carefully - and do NOT remove it. If you wanted to run the Pre-Autnomous function again between the control system switching from Autonomous to User Controlled, then set this to ‘false’ but be careful, and test test test before doing so! // Set bStopTasksBetweenModes to false if you want to keep user created tasks running between // Autonomous and Tele-Op modes. You will need to manage all user created tasks if set to false. bStopTasksBetweenModes = true;

64 VEXnet Competition Code
///////////////////////////////////////////////////////////////////////////////////////// // // Autonomous Task // This task is used to control your robot during the autonomous phase of a VEX Competition. // You must modify the code to add your own robot specific commands here. task autonomous() { // // Insert user code here. //AutonomousCodePlaceholderForTesting(); // Remove this function call once you have "real" code. // Willem: We are going to a pivot turn to the right the not the left // Turn Right at full power for 0.75 seconds motor[rightMotor] = -127; // Motor on port2 is run at full (-127) power reverse motor[leftMotor] = 127; // Motor on port3 is run at full (127) power forward wait1Msec(750); // Robot runs previous code for 750 milliseconds before moving on // Turn Left at full power for 0.75 seconds motor[rightMotor] = 127; // Motor on port2 is run at full (127) power forward motor[leftMotor] = -127; // Motor on port3 is run at full (-127) power reverse motor[rightMotor] = 0; motor[leftMotor] = 0; }

65 VEXnet Competition Code
By default this function call is not commented out, however as soon as you have your own code block in the Autonomous Task() section (blue code in previous slide) you MUST comment it out. You will get a warning message when compiling, but you can safely ignore this! (The message will be: WARNING: Unreferenced function AutonomousCodePlaceholderForTesting) //AutonomousCodePlaceholderForTesting(); // Remove this function call once you have "real" code.

66 VEXnet Competition Code
Sample Autonomous Code - make a pivot turn to the right, then a pivot turn to the left. NOTICE: the motors are deliberately stopped when we want to, if we do not do this the motors will keep on running until the timer expires the autonomous period of the game! // Willem: We are going to a pivot turn to the right the not the left // Turn Right at full power for 0.75 seconds motor[rightMotor] = -127; // Motor on port2 is run at full (-127) power reverse motor[leftMotor] = 127; // Motor on port3 is run at full (127) power forward wait1Msec(750); // Robot runs previous code for 750 milliseconds before moving on // Turn Left at full power for 0.75 seconds motor[rightMotor] = 127; // Motor on port2 is run at full (127) power forward motor[leftMotor] = -127; // Motor on port3 is run at full (-127) power reverse motor[rightMotor] = 0; motor[leftMotor] = 0;

67 VEXnet Competition Code
///////////////////////////////////////////////////////////////////////////////////////// // // User Control Task // This task is used to control your robot during the user control phase of a VEX Competition. // You must modify the code to add your own robot specific commands here. task usercontrol() { // User control code here, inside the loop while (true) // This is the main execution loop for the user control program. Each time through the loop // your program should update motor + servo values based on feedback from the joysticks. // // Insert user code here. This is where you use the joystick values to update your motors, etc. //UserControlCodePlaceholderForTesting(); // Remove this function call once you have "real" code. // Willem: We are just inserting a basic tank control remote program here to demonstrate how it works //Driving Motor Control motor[leftMotor] = vexRT[Ch3] / 2; motor[rightMotor] = vexRT[Ch2] / 2; }

68 VEXnet Competition Code
By default this function call is not commented out, however as soon as you have your own code block in the Usercontrol Task() section (green code in previous slide) you MUST comment it out. You will get a warning message when compiling, but you can safely ignore this! (The message will be: WARNING: Unreferenced function UserControlcodePlaceholderForTesting) //UserControlCodePlaceholderForTesting(); // Remove this function call once you have "real" code.

69 VEXnet Competition Code
while (true) { // This is the main execution loop for the user control program. Each time through the loop // your program should update motor + servo values based on feedback from the joysticks. // // Insert user code here. This is where you use the joystick values to update your motors, etc. //UserControlCodePlaceholderForTesting(); // Remove this function call once you have "real" code. // Willem: We are just inserting a basic tank control remote program here to demonstrate how it works //Driving Motor Control motor[leftMotor] = vexRT[Ch3] / 2; motor[rightMotor] = vexRT[Ch2] / 2; } Main While loop reading constantly the Remote Control - all you code acting upon the various buttons of your remotes MUST be within this loop. Simple code driving the robot using the two joysticks on the remote control. Every button you want to use must be defined here with actions.


Download ppt "By Willem Scholten Learning Access Institute"

Similar presentations


Ads by Google