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

📄 cml_pdo.h

📁 美国COPLEY驱动器,程序开发工具之一.
💻 H
📖 第 1 页 / 共 2 页
字号:

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 16-bit integers.
*/
/***************************************************************************/
class Pmap16: public Pmap
{
public:
   /// Default constructor for a 16-bit mapping object
   Pmap16():data(0){}

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

   /// Initialize a 16-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, 16 );
   }

   /// 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 2 bytes.  The current value
   ///        of this variable will be copied there.
   virtual void Get( byte *cptr ){
      m.Lock();
      int16 v = data;
      m.Unlock();
      cptr[0] = ByteCast(v);
      cptr[1] = ByteCast(v>>8);
   }

   /// 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 2 bytes.  The value of this variable 
   ///        will be updated with the data passed in this array.
   virtual void Set( byte *cptr ){
      int16 v = bytes_to_int16(cptr);
      m.Lock();
      data = v;
      m.Unlock();
   }

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

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

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

   /// The 16-bit integer associated with this mapping.
   int16 data;
};

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

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

   /// Initialize a 8-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, 8 );
   }

   /// 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 1 bytes.  The current value
   ///        of this variable will be copied there.
   virtual void Get( byte *cptr ){
      cptr[0] = ByteCast(data);
   }

   /// 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 1 bytes.  The value of this variable 
   ///        will be updated with the data passed in this array.
   virtual void Set( byte *cptr ){
      data = ByteCast(cptr[0]);
   }

   /// Read the current value of this variable
   /// @return The current value of this variable.
   virtual byte Read( void ){
      return ByteCast(data);
   }

   /// Write a new value to this variable.
   /// @param d The new value to write.
   virtual void Write( byte d ){
      data = ByteCast(d);
   }

private:
   /// The 8-bit integer associated with this mapping.
   byte data;
};

/***************************************************************************/
/**
PDO (Process Data Object) base class.  
*/
/***************************************************************************/
class PDO
{
public:
   /// Default constructor.  Simply initializes some variables.
   PDO( void ){
      flags = FLG_RTR_OK;
      mapCt = 0;
      bitCt = 0;
      type = 0;
   }

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

   /// Initialize the PDO.
   /// @return A pointer to an error object, or NULL on success
   virtual const Error *Init( void )
   {
      flags = FLG_RTR_OK;
      mapCt = 0;
      bitCt = 0;
      type = 0;
      return 0;
   }

   /// Return the base index in the object dictionary
   /// for this type of PDOs communication parameters
   virtual uint16 BaseIndex( void ) = 0;

   /// Get the COB ID associated with the PDO
   /// @return The COB-ID of the PDO
   virtual uint32 GetID( void ) = 0;

   /// Set the PDO transmission type code.
   /// @param t Transmission type code
   /// @return An error object.
   virtual const Error *SetType( byte t ){
      type= ByteCast(t);
      return 0;
   }

   /// Return the PDO transmission type associated with this PDO.
   /// @return The 8-bit type code
   virtual byte GetType( void ){
      return type;
   }

   /// Clear the variable map associated with this PDO.
   /// @return An error code
   virtual const Error *ClearMap( void ){
      mapCt = 0;
      return 0;
   }

   virtual const Error *AddVar( Pmap &var );

   /// Return non-zero if RTR requests are OK for this PDO.
   /// Note that this only really makes sense for transmit PDOs
   /// @return zero if RTR not allowed, non-zero if allowed.
   virtual int GetRtrOk( void ){
      return flags & FLG_RTR_OK;
   }

   /// Fill the array of 32-bit ints with the PDO mapping codes
   /// used by this PDO.  
   /// @param codes An array of at least PDO_MAP_LEN 32-bit ints.
   ///        The mapping codes will be stored here
   /// @return The number of elements mapped into this PDO.
   virtual int GetMapCodes( uint32 codes[] )
   {
      int i;
      for( i=0; i<mapCt; i++ )
	 codes[i] = map[i]->GetMapCode();
      return i;
   }

protected:
   /// Misc flags associated with the PDO
   byte flags;

   /// Transmission type code
   byte type;

   /// Number of elements in the variable map
   int mapCt;

   /// Number of bits mapped so far
   int bitCt;

   /// Array of pointers to Pmap objects that describe 
   /// the variables transmitted by this PDO.
   Pmap *map[ PDO_MAP_LEN ];
};

/***************************************************************************/
/**
Transmit PDO (transmitted by node, received by this software).
*/
/***************************************************************************/
class TPDO: public Receiver, public PDO
{
public:
   /// Default constructor.
   TPDO(){}

   /// Calls Init() at construction time.
   TPDO( CanOpen &co, uint32 cobID ){
      Init( co, cobID );
   }

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

   /// Initialize the PDO
   virtual const Error *Init( CanOpen &co, uint32 cobID ){
      const Error *err = PDO::Init();
      if( !err ) err = Receiver::Init( co, cobID );
      return err;
   }

   /// Return the base index in the object dictionary
   /// for this type of PDOs communication parameters
   virtual uint16 BaseIndex( void ){ return 0x1800; }

   /// Enable or disable RTR requests for this PDO
   /// @param ok zero for no RTR, non-zero for RTR allowed
   virtual void SetRtrOk( int ok ){
      if( ok ) flags |= FLG_RTR_OK;
      else flags &= ~FLG_RTR_OK;
   }

   /// Get the COB ID associated with the PDO
   /// @return The COB-ID of the PDO
   virtual uint32 GetID( void ){
      return getRecvID();
   }

   /// This function is called by the CANopen read thread
   /// when this PDO has been received.
   virtual void Received(){}

   virtual const Error *Request( void );
   virtual int NewFrame( CanFrame &frame );
};

/***************************************************************************/
/**
Receive PDO (received by node, transmitted by this software).
*/
/***************************************************************************/
class RPDO: public PDO
{
public:
   /// Default constructor. 
   RPDO(){ id = 0; }

   /// Construct the PDO object and initialize it.
   /// @param cobID The CAN message ID associated with this PDO
   RPDO( uint32 cobID ){
      Init( cobID );
   }

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

   /// Initialize the PDO object.
   /// @param cobID The CAN message ID associated with this PDO
   /// @return A pointer to an error object, or NULL on success
   virtual const Error *Init( uint32 cobID ){
      id = cobID;
      return PDO::Init();
   }

   virtual const Error *Transmit( CanOpen &co );

   /// Return the base index in the object dictionary
   /// for this type of PDOs communication parameters
   virtual uint16 BaseIndex( void ){ return 0x1400; }

   /// Get the COB ID associated with the PDO
   /// @return The COB-ID of the PDO
   virtual uint32 GetID( void ){
      return id;
   }

private:
   uint32 id;
};

CML_NAMESPACE_END()

#endif

⌨️ 快捷键说明

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