📄 ampstruct.cpp
字号:
/***************************************************************************/
/**
Configure the amplifier's PWM input pins. Note that these settings are only
used when the amplifier is controlled by it's PWM (or pulse/direction) input
pins, i.e. not in CANopen mode.
@param cfg A structure holding the configuration to download
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *Amp::SetPwmInConfig( PwmInConfig &cfg )
{
const Error *err = sdo.Dnld16( OBJID_PWMIN_CFG, 0, cfg.cfg );
if( !err ) err = sdo.Dnld32( OBJID_PWMIN_SCALE, 0, cfg.scale );
// PWM input frequency was added to CANopen interface in version 4.56
if( !err && (SwVersionNum >= VERSION(4,56) ) )
err = sdo.Dnld16( OBJID_PWMIN_FREQ, 0, cfg.freq );
return err;
}
/***************************************************************************/
/**
Upload the amplifier's PWM input pin configuration.
@param cfg A structure where the configuration parameters will be returned.
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *Amp::GetPwmInConfig( PwmInConfig &cfg )
{
const Error *err = sdo.Upld16( OBJID_PWMIN_CFG, 0, cfg.cfg );
if( !err ) err = sdo.Upld32( OBJID_PWMIN_SCALE, 0, cfg.scale );
if( !err && (SwVersionNum >= VERSION(4,56) ) )
err = sdo.Upld16( OBJID_PWMIN_FREQ, 0, cfg.freq );
return err;
}
/***************************************************************************/
/**
Set the CANopen node ID and bit rate configuration. Note that the amplifier
only uses this parameter at startup or after a reset.
@param cfg Structure holding the configuration to set.
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *Amp::SetCanNetworkConfig( CanNetworkConfig &cfg )
{
uint16 value = cfg.ToAmpFormat();
return sdo.Dnld16( OBJID_CANID_CFG, 0, value );
}
/***************************************************************************/
/**
Get the current CANopen network configuration programmed into the amplifier.
@param cfg A structure where the configuration parameters will be returned.
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *Amp::GetCanNetworkConfig( CanNetworkConfig &cfg )
{
uint16 value;
const Error *err = sdo.Upld16( OBJID_CANID_CFG, 0, value );
if( err ) return err;
cfg.FromAmpFormat( value );
return 0;
}
/***************************************************************************/
/**
Load the structure from a 16-bit word.
@param a A 16-bit value encoding the network configuration. See the amplifier
documentation for details on the format.
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
void CanNetworkConfig::FromAmpFormat( uint16 a )
{
offset = a & 0x7F;
numInPins = (a>>8) & 7;
useSwitch = ((a & 0x0800) == 0x0800);
bitRate = (CAN_BIT_RATE)(a & 0xF000);
}
/***************************************************************************/
/**
Encode the contents of the structure into a 16-bit word in the format used
by the amplifier. See the amplifier documentation for details on this format.
@return A 16-bit word representing the contents of this structure.
*/
/***************************************************************************/
uint16 CanNetworkConfig::ToAmpFormat( void )
{
uint16 config = offset & 0x7F;
config |= (uint16)(numInPins & 7) << 8;
if( useSwitch ) config |= 0x0800;
config |= (uint16)bitRate & 0xF000;
return config;
}
/***************************************************************************/
/**
Set new coefficients for the velocity loop output filter.
@param f A structure holding the filter coefficients
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *Amp::SetVloopOutputFilter( Filter &f )
{
byte data[18];
f.toBytes( data );
return sdo.Download( OBJID_VEL_OUTFILT, 0, 18, data );
}
/***************************************************************************/
/**
Get the coefficients used in the velocity loop output filter.
@param f A structure where the filter coefficients will be returned
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *Amp::GetVloopOutputFilter( Filter &f )
{
byte data[18];
int32 ct = 18;
const Error *err = sdo.Upload( OBJID_VEL_OUTFILT, 0, ct, data );
if( !err )
f.fromBytes(data);
return err;
}
/***************************************************************************/
/**
Set new coefficients for the velocity loop command filter.
@param f A structure holding the filter coefficients
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *Amp::SetVloopCommandFilter( Filter &f )
{
byte data[18];
f.toBytes( data );
return sdo.Download( OBJID_VEL_CMDFILT, 0, 18, data );
}
/***************************************************************************/
/**
Get the coefficients used in the velocity loop command filter.
@param f A structure where the filter coefficients will be returned
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *Amp::GetVloopCommandFilter( Filter &f )
{
byte data[18];
int32 ct = 18;
const Error *err = sdo.Upload( OBJID_VEL_CMDFILT, 0, ct, data );
if( !err )
f.fromBytes(data);
return err;
}
/***************************************************************************/
/**
Read the complete amplifier configuration from the amplifier and return it
in the passed structure. This structure holds every amplifier parameter that
can be stored to the amplifier's internal flash memory. The contents of the
structure represent the complete amplifier configuration.
@param cfg The structure which will hold the uploaded configuration.
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *Amp::GetAmpConfig( AmpConfig &cfg )
{
int32 l;
const Error *err = sdo.UpldString( OBJID_AMP_NAME, 0, l=COPLEY_MAX_STRING, cfg.name );
if( !err ) err = sdo.Upload( OBJID_CME2_CONFIG, 0, l=COPLEY_MAX_STRING, cfg.CME_Config );
if( !err ) err = sdo.Upld32( OBJID_MISC_OPTIONS, 0, cfg.options );
if( !err ) err = sdo.Upld16( OBJID_CAP_CTRL, 0, cfg.capCtrl );
if( !err ) err = sdo.Upld16( OBJID_ENCOUT_CONFIG, 0, cfg.encoderOutCfg );
if( !err ) err = sdo.Upld32( OBJID_CANMASK_LIMIT, 0, l );
cfg.limitBitMask = (EVENT_STATUS)l;
if( !err ) err = GetVelocityProgrammed( cfg.progVel );
if( !err ) err = GetCurrentProgrammed( cfg.progCrnt );
if( !err ) err = GetFaultMask( cfg.faultMask );
if( !err ) err = GetAmpMode( cfg.controlMode );
if( !err ) err = GetPwmMode( cfg.pwmMode );
if( !err ) err = GetPhaseMode( cfg.phaseMode );
if( !err ) err = GetMicrostepRate( cfg.stepRate );
if( !err ) err = GetCanNetworkConfig( cfg.can );
if( !err ) err = GetPosLoopConfig( cfg.pLoop );
if( !err ) err = GetVelLoopConfig( cfg.vLoop );
if( !err ) err = GetCrntLoopConfig( cfg.cLoop );
if( !err ) err = GetMtrInfo( cfg.motor );
if( !err ) err = GetTrackingWindows( cfg.window );
if( !err ) err = GetSoftLimits( cfg.limit );
if( !err ) err = GetIoConfig( cfg.io );
if( !err ) err = GetHomeConfig( cfg.home );
if( !err ) err = GetProfileConfig( cfg.profile );
if( !err ) err = GetAnalogRefConfig( cfg.ref );
if( !err ) err = GetPwmInConfig( cfg.pwmIn );
if( !err ) err = GetFuncGenConfig( cfg.fgen );
if( !err ) err = GetRegenConfig( cfg.regen );
if( !err ) err = GetVloopOutputFilter( cfg.vloopOutFltr );
if( !err && (SwVersionNum >= VERSION(4,56) ) )
err = GetVloopCommandFilter( cfg.vloopCmdFltr );
return err;
}
/***************************************************************************/
/**
Update an amplifier's configuration from the passed structure.
The AmpConfig structure holds all amplifier parameters that may be stored
in the amplifier's non-volatile flash memory. This function may be used
to update all of these parameters in a single call.
Note that this function updates the copies of these variables in working
RAM, not directly in the amplifier flash memory. To copy these parameters
to non-volatile memory, call Amp::SaveAmpConfig after updating them.
@param cfg The structure which holds the new configuration.
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *Amp::SetAmpConfig( AmpConfig &cfg )
{
const Error *err = sdo.DnldString( OBJID_AMP_NAME, 0, cfg.name );
if( !err ) err = sdo.Download( OBJID_CME2_CONFIG, 0, COPLEY_MAX_STRING-1, cfg.CME_Config );
if( !err ) err = sdo.Dnld32( OBJID_MISC_OPTIONS, 0, cfg.options );
if( !err ) err = sdo.Dnld16( OBJID_CAP_CTRL, 0, cfg.capCtrl );
if( !err ) err = sdo.Dnld32( OBJID_CANMASK_LIMIT, 0, (uint32)cfg.limitBitMask );
if( !err ) err = sdo.Dnld16( OBJID_ENCOUT_CONFIG, 0, cfg.encoderOutCfg );
if( !err ) err = SetVelocityProgrammed( cfg.progVel );
if( !err ) err = SetCurrentProgrammed( cfg.progCrnt );
if( !err ) err = SetFaultMask( cfg.faultMask );
if( !err ) err = SetAmpMode( cfg.controlMode );
if( !err ) err = SetPwmMode( cfg.pwmMode );
if( !err ) err = SetPhaseMode( cfg.phaseMode );
if( !err ) err = SetMicrostepRate( cfg.stepRate );
if( !err ) err = SetCanNetworkConfig( cfg.can );
if( !err ) err = SetPosLoopConfig( cfg.pLoop );
if( !err ) err = SetVelLoopConfig( cfg.vLoop );
if( !err ) err = SetCrntLoopConfig( cfg.cLoop );
if( !err ) err = SetMtrInfo( cfg.motor );
if( !err ) err = SetTrackingWindows( cfg.window );
if( !err ) err = SetSoftLimits( cfg.limit );
if( !err ) err = SetIoConfig( cfg.io );
if( !err ) err = SetHomeConfig( cfg.home );
if( !err ) err = SetProfileConfig( cfg.profile );
if( !err ) err = SetAnalogRefConfig( cfg.ref );
if( !err ) err = SetPwmInConfig( cfg.pwmIn );
if( !err ) err = SetFuncGenConfig( cfg.fgen );
if( !err ) err = SetRegenConfig( cfg.regen );
if( !err ) err = SetVloopOutputFilter( cfg.vloopOutFltr );
if( !err && (SwVersionNum >= VERSION(4,56) ) )
err = SetVloopCommandFilter( cfg.vloopCmdFltr );
return err;
}
/***************************************************************************/
/**
Save all amplifier parameters to internal flash memory. Flash memory is a
type of non-volatile RAM which allows amplifier parameters to be saved
between power cycles. When this function is called, any amplifier parameters
that may be stored to flash will be copied from their working (RAM) locations
to the stored (flash) locations.
For a list of those amplifier parameters which may be saved to flash memory,
see the AmpConfig structure. Every member of that structure represents an
amplifier parameter that may be saved to flash.
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *Amp::SaveAmpConfig( void )
{
return sdo.Download( 0x1010, 1, 4, "save" );
}
/***************************************************************************/
/**
Upload the passed amplifier configuration to the amplifier's workign memory,
and then copy that working memory to flash.
@param cfg The structure which holds the new configuration.
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *Amp::SaveAmpConfig( AmpConfig &cfg )
{
const Error *err = SetAmpConfig( cfg );
if( !err ) err = SaveAmpConfig();
return err;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -