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

📄 cml_pdo.h

📁 美国COPLEY驱动器,程序开发工具之一.
💻 H
📖 第 1 页 / 共 2 页
字号:
/************************************************************/
/*                                                          */
/*  Copley Motion Libraries                                 */
/*                                                          */
/*  Author: Stephen Glow                                    */
/*                                                          */
/*  Copyright (c) 2002-2005 Copley Controls Corp.           */
/*                          http://www.copleycontrols.com   */
/*                                                          */
/************************************************************/

/** 
\file

This header file defines the classes used to communicate
to CANopen nodes using Process Data Objects (PDOs).

*/

#ifndef _DEF_INC_CO_PDO
#define _DEF_INC_CO_PDO

#include "CML_Settings.h"
#include "CML_CanOpen.h"
#include "CML_Utils.h"

CML_NAMESPACE_START()

/// Number of variables that may be added to a PDO's map.
#define PDO_MAP_LEN  8

/// Flag used by transmit PDOs to indicate that RTR requests are allowed
#define FLG_RTR_OK    0x01

/**
This class represents error conditions related to PDOs.
*/
class PDO_Error: public CanOpenError
{
public:
   /// The variable map associated with the PDO is already full
   static const PDO_Error MapFull;

   /// Adding the variable to the map would cause the map to
   /// be too long (more then 64 bits).
   static const PDO_Error BitOverflow;

   /// PDO Map variables of the passed bit size are 
   /// not presently supported
   static const PDO_Error BitSizeError;

protected:
   /// Standard protected constructor
   PDO_Error( uint16 id, const char *desc ): CanOpenError( id, desc ){}
};

/***************************************************************************/
/**
This class allows variables to be mapped into a PDO.  This class can be used
directly for transmit PDOs if the received data is not of interest (it will
simply be discarded by the Set() function.  Using this for receive PDOs is
not recommended since the Get() function doesn't add any data to the output
stream and therefore the data transmitted to the node will be undefined.
*/
/***************************************************************************/
class Pmap
{
public:
   /// Default constructor for a generic PDO mapping variable
   Pmap(){}

   /// Construct a generic PDO mapping variable and initialize
   /// it's size and object ID.
   /// @param index The index of the variable in the object dictionary
   /// @param sub The variable's sub-index in the object dictionary
   /// @param bits The size of the variable in bits
   Pmap( uint16 index, byte sub, byte bits )
   {
      Init( index, sub, bits );
   }

   /// Virtual destructor
   virtual ~Pmap(){}

   /// Initialize a generic PDO mapping variable
   /// @param index The index of the variable in the object dictionary
   /// @param sub The variable's sub-index in the object dictionary
   /// @param bits The size of the variable in bits
   /// @return An error object
   virtual const Error *Init( uint16 index, byte sub, byte bits )
   {
      this->index = index;
      this->sub   = ByteCast(sub);
      this->bits  = ByteCast(bits);
      return 0;
   }

   /// Called when a receive PDO is about to be transmitted.
   /// This virtual function does nothing and therefore objects of this
   /// generic type shouldn't be used when actually transmitting PDOs
   virtual void Get( byte *cptr ){};

   /// Called when a transmit PDO is received.  This virtual function
   /// doesn't do anything and therefore objects of this base class should
   /// only be used for variables that are not of interest and can therefore
   /// be ignored.
   virtual void Set( byte *cptr ){};

   /// Return the 32-bit code used to identify this variable in the
   /// CANopen node's PDO mapping block
   virtual uint32 GetMapCode()
   {
      return (uint32)index<<16 | (uint32)sub<<8 | bits;
   }

   /// Get the object index associated with this variable
   /// @return The 16-bit object index.
   uint16 GetIndex(){
      return index;
   }

   /// Get the object sub-index associated with this variable
   /// @return The 8-bit object sub-index.
   byte GetSub(){
      return sub;
   }

   /// Get the number of bits in this variable
   /// @return The number of bits.
   byte GetBits(){
      return bits;
   }

protected:
   /// The 16-bit index of the object in the object dictionary
   uint16 index;

   /// The 8-bit sub-index of the object in the object dictionary
   byte sub;

   /// The number of bits that this object takes up.
   byte bits;
};

/***************************************************************************/
/**
This is the most generic PDO variable mapping class.  It doesn't do any
special formatting, just holds up to 8 bytes of raw data.
*/
/***************************************************************************/
class PmapRaw: public Pmap
{
public:
   /// Default constructor 
   PmapRaw()
   {
      for( int i=0; i<8; i++ ) data[i] = 0;
   }

   /// Create a new mapping object
   /// @param index Object index associated with this variable
   /// @param sub Object sub-index
   /// @param bits The size of the variable in bits
   PmapRaw( uint16 index, byte sub, byte bits ):Pmap(index, sub, bits)
   {
      for( int i=0; i<8; i++ ) data[i] = 0;
   }

   /// Copy the current value of this variable into the passed
   /// character array.  This function is called when a receive
   /// PDO is about to be transmitted to a node.
   /// @param cptr A character pointer that references a char
   ///        array large enough to hold this objects data.
   virtual void Get( byte *cptr ){
      int bytes = bits>>3;
      m.Lock();
      for( int i=0; i<bytes; i++ ) *cptr++ = ByteCast(data[i]);
      m.Unlock();
   }

   /// Update the value of this variable based on the data passed
   /// in a character array.  This function is called when a transmit
   /// PDO that this variable is mapped to is received.
   /// @param cptr A pointer to the received data.
   virtual void Set( byte *cptr ){
      int bytes = bits>>3;
      m.Lock();
      for( int i=0; i<bytes; i++ ) data[i] = ByteCast(cptr[i]);
      m.Unlock();
   }

private:
   /// A mutex used to protect access to this variable
   Mutex m;

   /// The data associated with this mapping.
   byte data[8];
};

/***************************************************************************/
/**
This is a PDO variable mapping class that extends the virtual Pmap class to
handle 32-bit integers.
*/
/***************************************************************************/
class Pmap32: public Pmap
{
public:
   /// Default constructor for a 32-bit mapping object
   Pmap32(): data(0) {}

   /// Create a new 32-bit mapping object
   /// @param index Object index associated with this variable
   /// @param sub Object sub-index (defaults to 0)
   Pmap32( uint16 index, byte sub=0 ):Pmap(index,sub,32), data(0){}

   /// Initialize a 32-bit mapping object
   /// @param index Object index associated with this variable
   /// @param sub Object sub-index (defaults to 0)
   /// @return An error object
   const Error *Init( uint16 index, byte sub=0 ){
      return Pmap::Init( index, sub, 32 );
   }

   /// Copy the current value of this variable into the passed
   /// character array.  This function is called when a receive
   /// PDO is about to be transmitted to a node.
   /// @param cptr A character pointer that references a char
   ///        array of at least 4 bytes.  The current value
   ///        of this variable will be copied there.
   virtual void Get( byte *cptr ){
      m.Lock();
      int32 v = data;
      m.Unlock();
      cptr[0] = ByteCast(v);
      cptr[1] = ByteCast(v>>8);
      cptr[2] = ByteCast(v>>16);
      cptr[3] = ByteCast(v>>24);
   }

   /// Update the value of this variable based on the data passed
   /// in a character array.  This function is called when a transmit
   /// PDO that this variable is mapped to is received.
   /// @param cptr A character pointer that references a char
   ///        array of at least 4 bytes.  The value of this variable 
   ///        will be updated with the data passed in this array.
   virtual void Set( byte *cptr ){
      int32 v = bytes_to_int32( cptr );
      m.Lock();
      data = v;
      m.Unlock();
   }

   /// Read the current value of this variable
   /// @return The current value of this variable.
   virtual int32 Read( void ){
      m.Lock();
      int32 d = data;
      m.Unlock();
      return d;
   }

   /// Write a new value to this variable.
   /// @param d The new value to write.
   virtual void Write( int32 d ){
      m.Lock();
      data = d;
      m.Unlock();
   }

private:
   /// A mutex used to protect access to this variable
   Mutex m;

   /// The 32-bit integer associated with this mapping.
   int32 data;
};

/***************************************************************************/
/**
This is a PDO variable mapping class that extends the virtual Pmap class to
handle 24-bit integers.
*/
/***************************************************************************/
class Pmap24: public Pmap
{
public:
   /// Default constructor for a 24-bit mapping object
   Pmap24():data(0){}

   /// Create a new 24-bit mapping object
   /// @param index Object index associated with this variable
   /// @param sub Object sub-index (defaults to 0)
   Pmap24( uint16 index, byte sub=0 ):Pmap(index,sub,24), data(0){}

   /// Initialize a 24-bit mapping object
   /// @param index Object index associated with this variable
   /// @param sub Object sub-index (defaults to 0)
   /// @return An error object
   const Error *Init( uint16 index, byte sub=0 ){
      return Pmap::Init( index, sub, 24 );
   }

   /// Copy the current value of this variable into the passed
   /// character array.  This function is called when a receive
   /// PDO is about to be transmitted to a node.
   /// @param cptr A character pointer that references a char
   ///        array of at least 4 bytes.  The current value
   ///        of this variable will be copied there.
   virtual void Get( byte *cptr ){
      m.Lock();
      int32 v = data;
      m.Unlock();
      cptr[0] = ByteCast(v);
      cptr[1] = ByteCast(v>>8);
      cptr[2] = ByteCast(v>>16);
   }

   /// Update the value of this variable based on the data passed
   /// in a character array.  This function is called when a transmit
   /// PDO that this variable is mapped to is received.
   /// @param cptr A character pointer that references a char
   ///        array of at least 4 bytes.  The value of this variable 
   ///        will be updated with the data passed in this array.
   virtual void Set( byte *cptr ){
      int32 v = bytes_to_int32(cptr);
      v = (v<<8) >> 8;
      m.Lock();
      data = v;
      m.Unlock();
   }

   /// Read the current value of this variable
   /// @return The current value of this variable.
   virtual int32 Read( void ){
      m.Lock();
      int32 d = data;
      m.Unlock();
      return d;
   }

   /// Write a new value to this variable.
   /// @param d The new value to write.
   virtual void Write( int32 d ){
      m.Lock();
      data = d;
      m.Unlock();
   }

⌨️ 快捷键说明

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