CML VC++ Guide STEP 1. See How to: Install CML

CML VC++ Guide This guide is intended as a tool for the beginning programmer. CML (Copley Motion C++ Libraries) will be used in the VC++ (Microsoft Vi...
Author: Letitia Nelson
20 downloads 1 Views 108KB Size
CML VC++ Guide This guide is intended as a tool for the beginning programmer. CML (Copley Motion C++ Libraries) will be used in the VC++ (Microsoft Visual C++ V6.0) programming environment on the Windows operating system. The example will make use of the Kvaser CAN card and the CML move example. It is assumed that VC++ is and CAN card is installed. Also, the Copley CANopen amplifier has been tuned and configured in the CANopen mode.

See How to: 1. Install CML 2. Create a new Project 3. Add Folders and Files 4. Exclude Files and Precompiled Headers 5. Set the Include Path 6. Link the CAN card Windows Drivers 7. Use the CML code 8. Compile Link and Run

STEP 1 Install CML Place the CML C++ libraries in the root directory. The CML libraries can be downloaded from the web after a username and password have been issued.

STEP 2 Create a new Project Open Microsoft Visual C++ and select File\New.

From the project tab select “Win32 Console Application” and give it the project name CML2. Click OK then select to create a simple application. A simple windows consol application will be created for you with auto generated CML2.cpp code.

The move.cpp file can be added and the auto-generated code can be excluded. See how next.

STEP 3 Add Folders and Files In the work space right click on the Source Files and select add files to folder. Navigate to the CML\examples\move folder and select the move example to add. The CML2.cpp file can now be deleted from the source files since, the main entry point is now in the move.cpp file. A tree structure that reflects the original CML folder and file structure can be crated. Add all the files from CML to the project.

The windows library file for the CAN card being used will need to be added to the project. For the Kvaser Card, the files are located on the web at: www.kvaser.com/download Install in the root directory. CANLIB SDK (Software Development Kit) V3.5 Link libraries, header files, sample programs.

STEP 4 Exclude Files Not all files will need to be compiled so, exclude the files that are not required for Windows and for the CAN card that is not being used. From Project\Settings select exclude file from build, for files that will not be complied. 1. From the can folder, only keep the file for the CAN card being used. Exclude the others. 2. From the threads folder, only keep Threads_w32.cpp for doing program threads with Windows. Exclude the other. 3. Exclude flash and twoaxis example for now. These can be used later after running the move example. 4. The CML libraries do not use StdAfx.cpp and StdAfx.h so, if no other libraries use them, they can be excluded.

Exclude Precompiled Headers Select not using precompiled headers option from the Projects\Settings\C\C++ category precompiled headers.

STEP 5 Set the Include Path The CML and CAN card header files will need to be included in the path. For CML that will be CML/inc, CML/inc/can, and CML/examples/inc. For the Kvaser CAN card that will be Kvaser\CANLIB\LIB\INC

Select Project\Settings\C/C++ tab category Preprocessor and add to the additional include directories: c:\CML\inc,c:\CML\inc\can,c:\CML\examples\inc,c:\Kvaser\CANLIB\inc Include the C-Runtime Library To make use of the C-Runtime call to “_beginthread” in the threads_w32.cpp file the multithread run time library will need to be used. Select Project\Settings\C++\Code Generation\Debug Multithread.

STEP 6 Link the CAN card Windows Drivers Add the CAN card Windows library by adding c:\kvaser\CANLIB\LIB\MS\canlib32.lib to the project to the Project\Settings\Link tab Object/library modules list.

STEP 7 Use the CML code 1. The can.h file (located in the CML\examples\inc\ folder) uncomment CAN card being used. Example uses the Kvaser card.

/** \file In order to make the example programs work on various environments, this file defines a generic ExampleCAN object which is just one of the CanInterface objects present in the library. */ // Uncomment one of these lines to select the CAN interface //#define USE_LINUX_CAN #define USE_KVASER_CAN //#define USE_VECTOR_CAN //#define USE_IXXAT_CAN /************************************************** * Kvaser CAN interface for use with windows **************************************************/ #ifdef USE_KVASER_CAN #include "can_kvaser.h" #define ExampleCAN

KvaserCAN

#endif 2. In the move.cpp file make sure the make sure the canNodeID agrees with the CAN address of the amplifier being used. /* local data */ int32 canBPS = 1000000; char *canDevice = "CAN0"; int16 canNodeID = 1;

// CAN network bit rate // Identifies the CAN device, if necessary // CANopen node ID

3. Also, in the move.cpp example, make sure the move velocities, acceleration, and jerk are in an acceptable range for the motor being used. Note: Velocity is in cnts/s, acceleration is in cnts/s/s, and jerk is in cnts/s/s/s // Home the motor. HomeConfig hcfg; hcfg.method = CHM_NDX_POS; hcfg.velFast = 100000; hcfg.velSlow = 50000; hcfg.accel = 90000; hcfg.offset = 0;

err = amp.GoHome( hcfg ); showerr( err, "Going home" ); printf( "Waiting for home to finish...\n" ); err = amp.WaitMoveDone( 20000 ); showerr( err, "waiting on home" ); // Setup the move speeds. For simplicity I'm just using the same // vel, acc & decel for all moves. printf( "Setting up moves...\n" ); ProfileConfigTrap trap; ProfileConfigScurve scurve; trap.vel = 800000; trap.acc = 50000; trap.dec = 50000; scurve.vel = 800000; scurve.acc = 50000; scurve.jrk = 8000; // Do a bunch of moves to random locaitons….

STEP 8 Compile Link and Run Compile: Select Build\Rebuild All to compile the code. If all links and code is present then no error will be produced.

Set a Breakpoint: Use F9 to set a breakpoint at “if( i & 1 );” in the code: // Do a bunch of moves to random locaitons. for( int i=0; i