⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 move.cpp

📁 美国COPLEY驱动器,程序开发工具之一.
💻 CPP
字号:
/** \file

Simple minded example of homing & moving a motor.
*/

#include <cstdio>
#include <cstdlib>

#include "CML.h"
#include "can/can_copley.h"

// If a namespace has been defined in CML_Settings.h, this
// macros starts using it. 
CML_NAMESPACE_USE();

/* local functions */
static void showerr( const Error *err, char *str );

/* local data */
int32 canBPS = 1000000;             // CAN network bit rate
char *canDevice = "CAN0";           // Identifies the CAN device, if necessary
int16 canNodeID = 1;                // CANopen node ID

/**************************************************
* Just home the motor and do a bunch of random
* moves.
**************************************************/
int main( void )
{
   // The libraries define one global object of type
   // CopleyMotionLibraries named cml.
   //
   // This object has a couple handy member functions
   // including this one which enables the generation of
   // a log file for debugging
   cml.SetDebugLevel( LOG_EVERYTHING );

   // Create an object used to access the low level CAN network.
   // This examples assumes that we're using the Copley PCI CAN card.
   CopleyCAN can( canDevice );
   can.SetBaud( canBPS );

   // Open the CANopen network object
   CanOpen canOpen;
   const Error *err = canOpen.Open( can );
   showerr( err, "Opening CANopen network" );

   // Initialize the amplifier using default settings
   Amp amp;
   printf( "Doing init\n" );
   err = amp.Init( canOpen, canNodeID );
   showerr( err, "Initting amp" );

   // 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.
   for( int i=0; i<50; i++ )
   {
      int x = rand(); x %= 100000;

      printf( "Moving to %d ", x );

      if( i & 1 )
      {
	 printf( "using trap profile\n" );
	 trap.pos = x;
	 err = amp.DoMove( trap );
      }
      else
      {
	 printf( "using s-curve profile\n" );
	 scurve.pos = x;
	 err = amp.DoMove( scurve );
      }

      showerr( err, "doing move" );

      err = amp.WaitMoveDone( 30000 ); 
      showerr( err, "waiting on move" );
   }
   return 0;
}

/**************************************************/

static void showerr( const Error *err, char *str )
{
   if( err )
   {
      printf( "Error %s: %s\n", str, err->toString() );
      exit(1);
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -