Introduction to Programing the Cortex for BEST SA
Introduction to Programing the Cortex for BEST SA BEST Kickoff - 2019 1 www. sabest. org
Presentation Goals • Primary Goals – understand how to: • Get and install software. • Connect the Cortex for programming. • Compile, link, and download the default program to the Cortex. • Secondary Goals • Components of the default program. • Physical connections to Cortex. • Joystick interface. 2 www. sabest. org
BEST Software Options • Robot. C for Cortex from VEX Robotics • “Standard” C programming by writing text. • Used in a number of competitions/products, so teams may have familiarity with this. • Simulink from Math. Works • Visual block programming. • BEST specific online training available. • Easy. C from Intelitek • Menu driven C programming • Visual blocks with code – drag, drop, and configure. • All options provide simple interfaces to motors, servos, switches, etc that are included in the BEST return kit. 3 www. sabest. org
easy. C Software Installation • Either download from link provided in “Software Access Instructions” or use the file provided on the USB drive to install the software. (Administrative access is needed). • A license key is provided on the USB drive that will need to be used to activate the software when it is first started. The computer must have internet access to validate the license key. • The licenses key can be copied to the clipboard from the text file on the USB drive, and then pasted into the activation form using the paste button on the form. 4 www. sabest. org
Connect the Cortex to the Computer • The simplest way to connect the Cortex to the PC is directly with an A-A USB cable. • The USB port powers the Cortex, so no battery is needed. • Motors and servos cannot be used when connected this way. • Refer to the return Kit information for other ways to connect. When connected properly: Robot light: Red/flashing Vexnet light: Green 5 www. sabest. org
Programming Vocabulary • Compile – changes your C program into object code that the linker understands. • Link – combines your program’s object code with library functions to create code that is executable on the Cortex processor. • Download – transfers the machine code version of your program from the PC to the Cortex where it will execute (the IFI/Intelitek Loader will perform the transfer via the PC USB cable). 6 www. sabest. org
Programming Flow 1. Write It. 2. Save It. 3. Compile & Link. (aka Build) 4. Download It. 5. Test It. Block-based Drag & Drop (Menu Cfg) or User Created C Program Project Files Integrated Design Environment (IDE) easy. C V 6 for Cortex Target (Cortex) easy. C Project From Scratch Libraries (C Functions) 7 www. sabest. org
easy. C Start Page Easy. C supports other controllers so make sure it’s set to the Cortex To set the controller: Select: Tools-> controller 8 www. sabest. org
Open BEST Default Project 9 www. sabest. org
Build and Download 10 www. sabest. org
Compile and Link If build is successful, the option to download is provided. Shows status while compiling and linking. Errors will show up here. 11 www. sabest. org
Download the File to the Cortex Tip: It’s very common for teams to forget to download the code to the Cortex once they get code to compile without any errors. 12 www. sabest. org
VEX Cortex Connections USB Gnd +5 v Sig (Blk) (Red) (Wht) Analog in 1 8 1 Digital in/out Speaker Out Do not use 1 2 12 SP 9 10 3 -wire PWM servo/motor ctrl Do not use Sig +5 v Gnd (Wht) (Red) (Blk) Config Button Battery 13 On/Off Switch www. sabest. org
Joystick Controls and Values • Channels numbers are labeled on joystick. • Joysticks (1 -4) return values between -127 and +127 in proportion to the stick position. • Buttons (5 -8) U/D/L/R return 1 when pressed and 0 when not pressed. • Two channels of accelerometers provide values between -127 and +127 in proportion to the angle(s) at which the joystick is being held (tilted forward/back or left/right). 7 3 -4 1 -2 6 14 8 5 www. sabest. org
BEST Default Program Function Right Joystick Connected by program Cortex Channel 1 Physical Connection Motor 2 Motor 9 Right Drive Motor Left Drive Motor 5 “Claw” Servo Motor 6 “Arm” Motor 2 Left Joystick 3 4 15 www. sabest. org
BEST Default Code • Outside the body of the program • Declares global variables • Defines names for constants to help make code easier to read. 16 www. sabest. org
Config block detail 17 www. sabest. org
Globals Block Detail 18 www. sabest. org
BEST Default Code • “main” is the name of the function that is executed when the program is compiled. • The main function includes everything between the matched curly braces {} • The END label and indenting makes it easy to see the extent. 19 www. sabest. org
BEST Default Code • Sets up region around center position of joysticks where there is no output. • Eliminates the problem of having motors move when you really don’t want them to. • Statements prior to the while loop are executed once. 20 www. sabest. org
Joystick Analog Deadband Configuration Screen Joystick positions of ± 4 are output as zero. 21 www. sabest. org
BEST Default Code • while (1) creates a loop where everything between the {} is repeated as “forever. ” • There should be no statements inside the while loop that require an external action before the next statement is executed. (More about this later. ) 22 www. sabest. org
BEST Default Code • The text in green are comments to help document the program. • The “arcade 2” function maps the right joystick channels to the driver motors. 23 www. sabest. org
Arcade -2 motor Configuration Screen 24 www. sabest. org
BEST Default Code • “Joystick. To. Servo” and “Joystickto. Motor” do essentially the same thing in mapping a joystick movement to a motor/servo output channel. • The first statement includes an “invert” function that reverses the direction of the mapping. 25 www. sabest. org
Joystick to Servo Configuration Screen 26 www. sabest. org
Programming (step-by-step) 1. Create a new project. • • Select “New Standalone Project” from the File menu Select “Joystick Project (wifi)” from the popup window 2. Create your program; save your program. • Drag & drop programming blocks to create your program 3. Build (i. e. , compile and link) the code. • • Use the “Build and Download” option from the Build and Download Menu Errors will be reported in the “Outputs & Tasks” window 4. Download the program to the Cortex • • Automatic if using the “Build and Download” option IFI/Intelitek Loader window will popup showing progress 27 www. sabest. org
Drag and Drop Blocks Configure block parameters by double clicking on icon. Drag blocks from here into program. 28 www. sabest. org
Example of bad while function use • This section of code will prevent the statements below from being executed until the button is pressed. • This means that the joystick values are not being read, and the other motors are not being controlled. • Don’t make this mistake! 29 www. sabest. org
Do this instead … • This just reads the button and then performs a function based on the state of the button. • Without the “else” statement, the motor would turn on the first time the button is pressed and would never turn off. 30 www. sabest. org
Some other tips • Use the BEST default program/project as a starting place for your robot program. • Use the “Save As” function to save a copy of the project at different stages of development. This will make it easy to take a step backwards if you create a problem in the code. • Make sure you name the different versions in a way to keep them straight (version numbers are good). • Make sure to use the right version on game day! 31 www. sabest. org
… yeah, but that’s not enough. • There are tutorials and examples built into easy. C. • Forums on bestrobotics. org and vexforum. com and other forums. • Be aware that non-BEST-specific forums may discuss capabilities that we don’t have. • Try things: you can always go back to the default code or some other simplified version of the code if things stop working. 32 www. sabest. org
- Slides: 32