+ All Categories
Home > Documents > The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT...

The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT...

Date post: 06-Feb-2018
Category:
Upload: vuongnhan
View: 216 times
Download: 1 times
Share this document with a friend
29
EasyC Programming Tips
Transcript
Page 1: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 

EasyC                               

Programming Tips 

Page 2: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 

PART 1: EASYC PROGRAMMING  

ENVIRONMENT    The EasyC package is an integrated development environment for creating C‐Programs and loading them to run on the Vex Control System. Its Opening Screen has five sections as shown in the following figure.                                 

1. The Tool Bar: This provides a quick access to commonly used commands. It has some of the standard Windows commands and several EasyC specific commands.   

2. The Programming Window: This is the place where the user writes the programs; it starts with the Start Page window, which provides quick access to several commonly‐used EasyC features. You can write several programs in the same window, each program will have its own tab.   

3. Function Block Panel:   4. The Project Explorer Panel:   5. The Output Panel:  

 You can customize the relative arrangement of these sections on screen to fit your taste. 

Page 3: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 A: COMPONENTS OF THE TOOL BAR    The tool bar provides a quick access to commonly used commands. It has some of the standard Windows commands and several EasyC specific commands as shown in the following figure.        Details of these commands are provided in the Tutorial section of the Start Page screen in the Programming window. A snapshot of these details is shown in the following figure. 

Page 4: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 B: COMPONENTS OF THE PROGRAMMING WINDOW  The programming window is the place where the user writes the programs; it starts with the Start Page window as shown in the following figure. The Start Page provides a quick access to several commonly‐used features of easyC.                                  There are four screens in the Start Page as follows:  

a. Projects Screen: A normal C‐Program is composed of several variables and subprograms (functions) which can be in several files. “Project” is a name given to the collection of all of the variables, subprograms, and files that together form a single program. The Projects screen provides an easy access to tools for creation and modification of projects; it has four tabs for:   

i. Listing Recently Opened Projects: This provides an easy access to all recently opened projects by listing them up to six projects.   

ii. Creation of Standalone Projects: These are projects that drive the robot in one of the two possible modes: teleoperation or autonomous – to be discussed later.   

iii. Creation of Competition Projects: These projects drive the robot using both the autonomous mode and the teleoperation mode in a timed competition. These projects will not be necessary in BEST robotics applications; only the standalone projects will be sufficient.   

iv. Opening an Existing Project from the hard disk: Use this tab to open and possibly modify an existing project  

Page 5: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 b. Tools Screen: This screen provides an access to tools for communicating with the Vex 

hardware; it has four utilities as shown in the following figure.             

Each of these tools activates the Intelitek loader, which must be configured properly in order to successfully complete the communication between the user and Vex hardware. Use the online Help to learn how to set up the Intelitek loader. A snapshot on the main functions of these tools is as follows: 

 1. The Download tool provides an easy means for downloading a new program 

into the Vex Controller. Use the Download window to download your program to the controller and view the status of the download process.  

                  

The progress bar displays the status of the download process. It moves to the left to indicate the loader is erasing any previous programs from the controller, and moves to the right to indicate the loader is downloading the new program to the controller. 

 2. The Terminal tool is used as a feedback mechanism showing a list of 

actions carried out by the Vex controller. A Typical Terminal window will be as illustrated in the following figure.  

Page 6: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

                    3. The Graphic Display is an extension of the Intelitek loader that displays the 

feedback data from the Vexnet hardware by printing on screen. It typical appearance of the Graphic Display Screen is as shown in the following figure.  

                              4. The On­Line tool is for manually testing and controlling a robot directly from 

the EasyC software. This tool is used only after the program has been uploaded into the Vex Controller. By using the computer cursor keys, the user can control the robot using this tool; a typical On‐line window is as shown in the following figure.  

Page 7: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

                          

5. The Competition Switch tool emulates the physical VEXnet competition switch, which allows two players to load new programs to the robot wirelessly and compete; it is enabled only when working with Competition Projects, otherwise it is disabled. It is expected that BEST robotics teams won’t have to use this facility.  

                    c. The Help Screen: The help screen has three sections as shown in the following figure. 

Probably, the most important section is the Contents section, which allows the user to search information about any topic.  

Page 8: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

          

d. The Tutorials Screen: The EasyC package comes loaded with 14 tutorial sessions on how to navigate the EasyC programming environment, writing a Vex control program, debugging and loading it into the Vex controller hardware. Make use of these tutorials to equip yourself with the necessary skills on writing good and winning programs for your robots.  

                    C: THE FUNCTION BLOCKS AND THE PROJECT EXPLORER PANELS:  On starting any new program as will be shown later, the Function Blocks and the Project Explorer panels will also open. The Function Block panel shows all available EasyC built‐in functions. The user can click and drag any of these functions into the program followed by filling in with appropriate values for the function arguments. The Project Explorer controls the details of the project and is the access point for the text editor and other features; it displays the overall structure of the project. As the program builds up, and so does the project explorer expand. A normal user will probably do nothing in the project explorer panel. The following figure shows both the basic contents of the function blocks and the project explorer panels. 

Page 9: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

  D: THE OUTPUT PANEL:  The Output Panel displays the outputs of the compiler about the correctness of the program. It has three tabs: Build, Output and Find Results as shown in the figure below.  

1. The Build  tab  displays  the  output  of  the  compiler,  and  lets  the  user  know what files are being processed during long compilations.   

2. If the compiler finds errors in the program, a list of these errors will automatically be generated and displayed in the Output tab. You can highlight the line of the code in blue in the C programming window by selecting the error line from this list.   

3. The Find Results tab lists all results obtained after running a search using Find in Project Files command under the Edit pull‐down menu in the main menu bar.  

Page 10: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 PART 2: WHAT YOU NEED TO KNOW ON CREATING  

A VEX PROGRAM  A: VEX ROBOT PROJECTS  There are two modes of driving any robot; either by teleoperation using a human driver, or autonomously by the robot following a sequence of preprogrammed events. In line with these modes, there are two types of Vex projects: the Standalone project and the Competition project. A Standalone project allows only one mode of driving the robot, while the competition project allows both modes to compete on a single robot. All BEST robotics projects will use teleoperation mode, therefore they will be Standalone projects.  To create a new project for a teleoperated robot, use the Projects Screen and click New Standalone project…. A dialog window will open asking you to choose whether you want to create a Standalone project for Teleoperation (Joystick Project (wiFi)) or you want an Autonomous only standalone project as shown below. Choose Joystick Project            The Function blocks panel fills with all function needed for your project. The project explorer panel and the programming window will automatically be filled with the basic information that you will need to complete your program. In general you will see a screen similar to one shown below 

Page 11: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 Programming your robot will involve reading the joystick control signals and translating them to motor or servo drive signals. A clear understanding of the functions for reading the joystick commands and the functions for sending control signals to motors, and servo motors is neede. Let us start by examining the contents of the Function blocks panel. This panel has several standard functions needed by the robot project; we will focus on the joystick functions and the output functions only.    B.JOYSTICK FUNCTIONS  The joystick functions manage how the Vex controller gets control commands from the Joystick. There a total of 17 joystick functions as shown in the following figure, probably the important functions for our projects will be ten only as described below.                         

1. Arcade ­2 Motor: This command allows you to control two motors simultaneously using the x‐ and y‐ joystick movements of one joystick only; the motor speed is proportional to the joystick position from the center. Basically, the joystick sends same commands to two Motor channels, as specified in the command. When using the arcade 2 command, you must specify the followings:   

a. The intended joystick. VEX Controller can be controlled by using two joysticks numbered “1” and “2.” In BEST competitions, we will use only one joystick, number   “1.”   

b. The joystick channel assignment for forward‐reverse motions : Channels 1 or 3 for the x motion and Channel 2 or 4 for the y and x motion. You would be better off using channels 1 and 2 on one stick.   

c. The  Joystick channel assignment  for  left‐right  turns  (Channels 1 or 2  for  the x and y motion of left stick and channels 3 or 4 for the y and x motions for the right stick.)   

d. The motor channels controlled by the joystick ( any motor channel from 1 to 10 is possible); you have to specify two motor channels  

Page 12: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

                            2. Arcade ­4 Motor: This function is similar to the Arcade ­2 Motor, but it can simultaneously 

control a total of four motor channels instead only two. Other parameters are same but the motor channel specification requires four motors to be specified as shown in the following figure instead of only two as seen before  

Page 13: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 3. Tank ­2 Motor: This function allows one analog x‐y stick on the joystick to control two 

motors for the forward and reverse motions only, it does not allow left and right turns; the motor speed is proportional to the joystick position from the center. To use this function, the user must specify:   

a. The intended joystick (“1” for one joystick mode)   b. The motor motion assignments. Each stick motion will control one motor on one 

side of the robot (left or right); therefore, if the x‐motion of the tick is controlling the left motor, then the y‐motion of the same stick will control the right motor. The stick channels range from 1 to 4 with channels 1 and 2 attached to the x and y motions of the right stick, while channels 3 and 4 are attached to the x and y motions of the left stick.   

c. The motor channels controlled by this joystick; any channel between 1 and 10 is possible; you have to specify two motor channels.  

                            4. Tank ­4 Motor: This is an extension of the Tank ­2 Motor function with the ability to 

control up to 4 motors: two on the left hand side of the robot and two on the right hand side. It has the same features as the Tank ­2 Motor function as shown in the next figure.  

Page 14: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

                            5. Joystick to Motor: The Joystick to Motor function uses one channel of the analog x‐y joystick 

to control one motor only; the motor speed is proportional to the joystick position from the center. Therefore, each joystick can be assigned to two motors using two Joystick to Motor functions. In setting up the joystick to motor function, you must specify:  

a. The intended joystick:1   b. The intended channel ( 1 to 4),   c. The controlled motor channel (1 to 10).  

Page 15: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 6. Joystick to Motor and Limit Switch: This function is similar to the Joystick to Motor 

function, except that it also allows limit switches to be used as emergency stops for the motor. One limit switch stops the motor from the forward direction and another switch stops the motor from the reverse direction. Limit switches will be connected to any of the digital input channels 1 to 12. If limit switches are disabled by selecting Channel 0, then the function behaves just like the Joystick to Motor function.  

                          7. Joystick to Servo: The Joystick to Servo function uses the analog x‐y joysticks to control 

the servos. It is similar to the Joystick to Motor function, except that the motor is replaced by a servo.  

 8. Joystick Digital to Motor: This is the digital equivalent of the Joystick to Motor. It behaves 

just like the Joystick to Motor but the motor has only two speeds, zero and the maximum set speed in either direction. The digital joystick is labeled 7 and 8 at the top of the analog x‐y joysticks.  

Page 16: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 To use these digital joysticks, you must specify the joystick channel, joystick displacement direction, the channel of the controlled motor and the digital value corresponding to the maximum motor speed, which ranges from ‐127 corresponding to full reverse motor speed to +127 corresponding to the maximum forward full motor speed. Half forward speed will be 64, and quarter will be 32 etc. Motors will be connected to the PWM Motor terminal block (numbered 1‐10 not 1‐12 as shown in its dialog window). 

                        

9. Joystick Digital to Motor and Limit Switch: This is the digital equivalent of the Joystick to Motor and Limit Switch.   

10. Joystick Digital to Servo: This is the digital equivalent of the Joystick to Servo.      

D. OUTPUT FUNCTIONS  Output functions control how the Vex controller sends control signals to the output devices. There are three output functions as shown in the following figure and discussed next. 

               1. The Motor Module: This function block sends control signals to the motors connected at the 

Motor PWM terminal block at analog channels 1‐10. The user must specify the motor  

Page 17: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 channel, and the motor speed. The motor full speed is specified by a digital value 127 in one direction and ‐127 in the opposite direction. A digital value of 64 corresponds to the half speed, and a value of 32 corresponds to the quarter speed and so on. 

                     2. Servo Module: The Servo Module controls a specified servo connected at the PWM motor 

terminal block at channels 1‐10. It is similar to the Motor Module although servos have a limited range of motion (about 120 degrees). The command value specifies a position within that range of motion for the servo motor to move to and hold. A value of 127 will result in a full turn while a value of 64 will result in a half turn etc.  

                     3. Digital Output: The Digital Output function block is for controlling the state of a Digital 

Output. You can use the Digital Output to turn ON or OFF a device such as an LED or a solenoid.  

Page 18: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

               

To set up a digital output, you must specify:  a. The digital  output  channel #  that  corresponds with  the port where  the device  is 

plugged into on the controller. The output digital channels range from 1 to 12.   b. Set a predefined output value in the "Set" field; the valid values are 1 for ON and 0 

for OFF. By default, all the digital outputs are set to 0 (or low) by default.    

C: PROGRAM FLOW CONTROL  As  in  any  computer  programming  project,  there  must  be  a  logical  flow  of  instructions  in order  to  achieve  the  intended  objective.  There  are  several  program  flow  control  function blocks as shown in the following figure.                    In most of BEST robotics projects, the most important instruction will be the While Loop; this allows the program to repeat the specified code sequence for as long as certain conditions hold, stopping only when those condition cease to be valid. By using the While Loop, the code for reading the joysticks placing control signals to output devices, i.e. motors and servomotors, the robot runs continuously until the power is turned off or the robot is reset. To use the While Loop you must specify the condition that will remain true for as long as the robot is running; the following figure illustrates one way to make a valid WHILE loop, it is valid as 1 equals to 1: 

Page 19: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and
Page 20: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 PART 3: CREATING A ROBOT PROGRAM 

 The following steps outline a systematic procedure for creating a simple but sufficient robot program. Before writing any robot program, you have to clearly understand the controlled devices that are connected to your controller. Recall that the Vex Control System has two power banks, each distributed to five motor ports: the first power unit is distributed to port 1 through 5, and the second power unit is distributed to port 6 through 10. Additionally, ports 1 and 10 have internal motor controllers while the rest need an external controller. This year we will use four external controllers only, so port 1 and 10 will not be used. Balance your load between the two power units by proper assignment of ports.  Hardware Assumptions:  In this example, we will assume that: 

 • We will assume further that all drive motors and servos are connected at the 

motor terminal block, not at the digital block. . The motors and servos will not be protected by limit switches. 

• There are four motors and four servos consistent with the BEST robotics return kit. Two motors are connected to motor ports 2 and 9 for driving the robot, another two motors are connected to ports 3 and 8 are for actuating the robot arm:  

• The servos connected to ports 4 through 7;  two for are activating the robot fingers, and two for activating the robot arm.  

Program Plan: The control program will be implemented as follows:  • There will be only one joystick, i.e. joystick #1; and the motor channels 1 and 10 will not 

be used.  • The two drive motors on ports 2 and 9 will be driven simultaneously by the using 

channels 1 and 2 of the joystick in Arcade mode.  • The other two motors on ports 3 and 8 will be controlled independently using channels 3 

and 4 respectively.  • The servos on ports 4 through 7 will be controlled by using digital channels 5 through 8.   Program Development:  1. Start a Standalone project for teleoperation mode using one of the following approaches:   

a. Main Menu: File ­> New Standalone Project   b. The Start Page: Projects ­>  New Standalone Project   

After the following dialog window opens, choose Joystick Project (WiFi) 

Page 21: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 The programming window will open with the Main block diagram as shown in the following figure: 

           2.   Configure the hardware connections by clicking the Config block.            

This will open the hardware configuration window as shown in the following figure:                           

Assign the motor ports in the motor description space as illustrated in the following figure, and press OK to close the configuration window.   

Page 22: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

  

  

 3. In the Function Blocks panel, open the Program Flow group, and select the While Loop 

block. Click and drag the While loop block to the line between the Variables block and the End block in the Main block diagram as shown below.  

Page 23: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 The While Loop dialog window will open asking you to state the logical condition under the loop will be valid; any integer number greater 0 as indicated in the following figure will set the loop to be valid for ever, as long as the robot is running.                   Press OK, close the While Loop dialog window, a loop will be created in the Main Block diagram as illustrated below.                     5. In the Function Blocks panel, open the Joystick group and select the Arcade ­2 motor block. 

Click and drag the Arcade ­2 motor block to the line between the { block and the } block in the While loop in Block diagram as shown below. This is for reading joystick commands and controlling the drive motors on ports 1 and 10 on the Vex hardware.  

Page 24: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

A dialog window will open for configuring the Arcade 2‐ motor command. Set the parameters as shown in the following figure.  

a. Joystick # 1 defines the only one  joystick used by this system.  It  is possible to use two joysticks.  

b. The transmitter channels indicate the functions of the joystick movements: The forward and reverse motions of the robot will be controlled by joystick channel 2, which is the y‐axis motion of the joystick, and the left‐right turns of the robot will be controlled by channel 1, which is the x‐axis motion of the joystick.   

c. The  left  and  right  drive motors  of  the  robot will  be  from ports  2  and  9 respectively as indicated below.  

 Press OK to close the Arcade – 2 motor dialog window. 

6. Now you want to control the two motors for the robot arm. Select the Joystick to Motor block from the Function Blocks panel, and place it in the While Loop of the Main block diagram under the Arcade ‐2 motor block as shown in the following figure.  

                    

In the dialog window that opens, specify the motor channel and the joystick channel that will be involved. For example, the following dialog window sets channel 3 (the y­axis) of 

Page 25: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 the left joystick to control the robot arm motor connected on Channel 3 of the Vex hardware;  the Vex motor port and joystick channel do not have to be of the same number. 

                   

Click OK, to close the dialog window.  

Repeat the above process for the robot arm motor in port 8 to be controlled by Channel 4 (the x‐axis) of the left joystick. 

 7. Having controlled all four motors, now it is a time to set up the controls for the three 

servos. Since all analog joystick channels have been assigned to motors, the servos will be controlled using the digital joystick channels, which start from channel 5 through 8. In the Function Blocks panel, click and drag the Joystick Digital to Servo block into the While Loop as illustrate in the following figure.  

 In the dialog window that opens, set the channel 5 of the digital Joystick # 1 to control the hand servo connected in port 4 of the Vex hardware. Button 1 (D) of the joystick will 

Page 26: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 provide a forward motion of the servo at a full range of 127, and button 2 (U) will reverse the motion at a full range of ‐127. You can change the range by descresing these values.  

  

Click OK to close the Joystick Digital to Servo dialog window.  

Repeat the process above two more times for the servos in ports 6 and 7. Port 6 will be controlled by channel 7 of the second joystick, and port 8 will be controlled by channel 8. 

  Unfortunately, there is no function to hold the servo in one position. If required, you can program it using combinations of If then commands. Those who will need this feature can directly contact Dr. Selekwa for help at [email protected],        8. The  block  diagram  appearance  of  your  final  program  will  appear  as  shown  in  the 

following figure.  

Page 27: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 You can view the generated C program either by opening the C‐ programming window from the main menu: Window­ > C­Programming or by opening the main.c file from the project explorer panel. The C‐program for this project will be as shown in the following figure. 

  

 Unless one is good at C‐programming, most BEST robotics users will find it not important to access the C program.   

9. Save your program at any convenient location on the hard disk, using any name of your choice. 

 10. After creating the program, the next task is to compile and upload it into the Vex 

system ready for running your robot. There are two options, either to compile vthe program without uploading or compile and upload. The tools for performing these operations are shown in the following figure.  

Page 28: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

             To compile the program without uploading it, use Build and Download ­> Compile Project from the main menu, and to compile and upload, use Build and Download ­> Build and Download as shown below.               On compiling the project, you can see the compilation results in the Output Panel as shown in the  following  figure.  Programming  errors  also  will  be  listed  here,  and  by  highlighting  and clicking the error, the C‐file opens with the cursor placed at the line that contains the error.                     

Page 29: The EasyC Training new - North Dakota State · PDF filePART 1: EASYC PROGRAMMING ENVIRONMENT The EasyC package is an integrated development environment for creating C‐Programs and

 


Recommended