library for C++ CANOpen implementation. mbed independant, but is easy to attach into with mbed.

Dependents:   ppCANOpen_Example DISCO-F746NG_rtos_test

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Node.h Source File

Node.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file
00004  * @author  Paul Paterson
00005  * @version
00006  * @date    2015-12-14
00007  * @brief   CANOpen implementation library
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 Paul Paterson
00012  *
00013  * All rights reserved.
00014 
00015  This program is free software: you can redistribute it and/or modify
00016  it under the terms of the GNU General Public License as published by
00017  the Free Software Foundation, either version 3 of the License, or
00018  (at your option) any later version.
00019 
00020  This program is distributed in the hope that it will be useful,
00021  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  GNU General Public License for more details.
00024 
00025  You should have received a copy of the GNU General Public License
00026  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00027  */
00028 
00029 #ifndef PPCAN_NODE_H
00030 #define PPCAN_NODE_H
00031 
00032 #include "ObjectDictionary.h"
00033 
00034 /*=========================================================================
00035  * Forward declarations
00036  *=========================================================================
00037  */
00038 struct CanOpenMessage;
00039 //struct ObjectData;
00040 
00041 /* NMT Constants --------------------------------------------------------*/
00042 #define NMT_STATE_TOGGLE_BIT      0x80
00043 
00044 /* NMT Macros -----------------------------------------------------------*/
00045 #define NMT_SET_STATE(curState, newState) (curState) = (((curState) & NMT_STATE_TOGGLE_BIT) | ((newState) & (~NMT_STATE_TOGGLE_BIT))))
00046 
00047 namespace ppCANOpen
00048 {
00049 
00050 /* Avoid circular reference */
00051 class ServiceProvider;
00052 
00053 
00054 /** Node Class to implement feature of a CANOpen NMT node
00055  */
00056 class Node
00057 {
00058 public:
00059 
00060     /* ========================================================================
00061      * Internal structures and constants
00062      * ========================================================================
00063      */
00064      
00065     /** Node network management state */
00066     struct State {
00067         static const int INITIALIZED     = 0x00;
00068         static const int DISCONNECTED    = 0x01;
00069         static const int CONNECTING      = 0x02;
00070         static const int PREPARING       = 0x02;
00071         static const int STOPPED         = 0x04;
00072         static const int OPERATIONAL     = 0x05;
00073         static const int PREOPERATIONAL  = 0x7F;
00074         static const int UNKNOWN         = 0x0F;
00075         
00076         int nmtState;
00077         int bBoot;
00078         int bSDO;
00079         int bEmergency;
00080         int bSYNC;
00081         int bLifeGuard;
00082         int bPDO;
00083         int bLSS;
00084         
00085         int bLifeGuardToggle;
00086     };
00087 
00088     /* ========================================================================
00089      * Construction
00090      * ========================================================================
00091      */
00092      
00093     /** Maintain the multitude of states for a node */
00094     Node (int id, ServiceProvider * provider, int bLoop = 0);    
00095 
00096     /* ========================================================================
00097      * Public Methods
00098      * ========================================================================
00099      */
00100     
00101     /** Call from ServiceProvider on an interrupt */
00102     void FixedUpdate (uint32_t time);
00103     
00104     /** Call from ServiceProvider every loop of Run() */
00105     void Update (void);
00106     
00107     /** Handle message given by the ServiceProvider*/
00108      int DispatchMessage(CanOpenMessage *canOpenMsg);
00109      
00110     /* ========================================================================
00111      * Public Member Variables
00112      * ========================================================================
00113      */
00114      
00115     /* Common Node properties -----------------------------------------------*/
00116     /** Network id for the node
00117       * @note Ref is held in object dictionary and upon setting, may need to
00118       * update some other objects.
00119       */
00120     int nodeId;
00121 
00122     /** Loopback Mode.  Default off (0), if on (1) then will hear it's own 
00123       * messages
00124       */
00125     int bLoopbackOn;
00126 
00127 protected:
00128 
00129     /* ========================================================================
00130      * Protected? Methods to handle various messages
00131      * Used to deal with everything from inside of Dispatch Message
00132      * ========================================================================
00133      */
00134     
00135     /* PDO (7.2.2), MPDO (7.2.3) --------------------------------------------*/
00136     
00137     int HandlePdo (CanOpenMessage *canOpenMsg);
00138     
00139     
00140     /* SDO (7.2.4) ----------------------------------------------------------*/
00141     int HandleSdo (CanOpenMessage *canOpenMsg);
00142     
00143     int HandleExpeditedDownload (int sdoNum, int index, int subIndex, int dataCount, char * data);
00144     int HandleInitiateDownloadRequest (int sdoNum, int index, int subIndex, int dataCount, char * data);
00145     
00146     int HandleExpeditedUpload (int sdoNum, int index, int subIndex);
00147     
00148     
00149     // TODO: express and not express
00150     
00151     /* SYNC object (7.2.5) --------------------------------------------------*/
00152     int HandleSync (CanOpenMessage *canOpenMsg);
00153     
00154     /* Time Stamp object (7.2.6) --------------------------------------------*/
00155     /* Handled all inside of the ServiceProvider */
00156     
00157     /* Emergency object (7.2.7) ---------------------------------------------*/
00158     
00159     int ConsumeEmergency (void);// TODO: fix params
00160     
00161     
00162     /* Network Management (7.2.8) -------------------------------------------*/
00163     /* ---- Node Control (7.2.8.2.1) ----------------------------------------*/
00164     int HandleNodeControl (CanOpenMessage *canOpenMsg);
00165 
00166 
00167     /* ---- Error Control (7.2.8.2.2) ---------------------------------------*/
00168     
00169     int HandleNodeGuardRequest (int masterId);
00170     int ConsumeHeartbeat (int producerId);
00171     
00172 protected:
00173 
00174     /* ========================================================================
00175      * Protected Methods to implement node application in derived classes
00176      * ========================================================================
00177      */
00178      
00179      /** Perform actions every cycle (when in operational mode)
00180        * @note Override to implement user application
00181        * @note The OnTick function should avoid modifying data in the object
00182        * dictionary unless necessary, because there will almost certainly be 
00183        * a race condition.
00184        */
00185      virtual void OnFixedUpdate(void) = 0;
00186      
00187      
00188      
00189      virtual void OnUpdate(void) = 0;
00190     
00191     
00192     /* SYNC -----------------------------------------------------------------*/
00193     /** Perform actions when state changed to stop
00194       * @note Override to implement user application
00195       */
00196     virtual void OnSync (uint8_t counter) = 0;
00197     
00198     
00199     /* NMT Node Control -----------------------------------------------------*/
00200     /** Perform actions when node reset
00201       * @note Override to implement user application
00202       */
00203     virtual void OnInitialize (void) = 0;
00204     
00205     /** Perform actions when state changed to pre-operational
00206       * @note Override to implement user application
00207       */
00208     virtual void OnPreoperational (void) = 0;
00209     
00210     /** Perform actions when state changed to operational
00211       * @note Override to implement user application
00212       */
00213     virtual void OnOperational (void) = 0;
00214     
00215     /** Perform actions when state changed to stop
00216       * @note Override to implement user application
00217       */
00218     virtual void OnStopped (void) = 0;
00219     
00220     /* Object Dictionary Handling -------------------------------------------*/
00221     /** Abstract method to give access to the object entries of derived
00222       * classes
00223       */
00224     virtual ObjectData * ScanIndex(IndexSize index) = 0;
00225 
00226 
00227     /* ========================================================================
00228      * Protected Methods to Post Messages
00229      * ========================================================================
00230      */
00231      
00232     int PostTPDO (int cobId);
00233 
00234 
00235     /* ========================================================================
00236      * Protected Member Variables
00237      * ========================================================================
00238      */
00239      
00240     /* Object Dictionary Handling -------------------------------------------*/
00241     /** Array of ObjectData to contain all of the object dictionary data */
00242     ObjectData *dictionary;
00243     
00244     /* Elapsed Time ---------------------------------------------------------*/
00245     /** Millisecond time stamp at this OnTick() */
00246     uint32_t timeCurrentTick;
00247     
00248     /** Millisecond difference between current OnTick() and the previous */
00249     uint32_t timeSinceLastTick;
00250 
00251 // should be private
00252 // TODO: implement functions in Node class to post all types of messages so
00253 //       derived classes do not have to consider message structure.
00254 protected:
00255 
00256     /* ========================================================================
00257      * Private members
00258      * ========================================================================
00259      */
00260      
00261     /* ServiceProvider ------------------------------------------------------*/
00262     /** Reference to Service Provider that this node is attached to */
00263     ServiceProvider * pMyProvider;
00264 
00265 private:  
00266     /* Common Node Methods --------------------------------------------------*/
00267     /** Change state of the node */
00268     void ChangeState(int newState);
00269     
00270     /* Common Node properties -----------------------------------------------*/    
00271     /** Network and communication state of the node */
00272     State state;
00273     
00274     
00275 };
00276 
00277 } /* namespace ppCANOpen */
00278 
00279 #endif // PPCAN_NODE_H
00280