library for C++ CANOpen implementation. mbed independant, but is easy to attach into with mbed.
Dependents: ppCANOpen_Example DISCO-F746NG_rtos_test
Example:
Import programppCANOpen_Example
I am no longer actively working on the ppCANOpen library, however, I want to publish this project so that anyone who wants to pick up any of the pieces can have a good example. This is a a project I was working on using the ppCANOpen library. It has a pretty in deep use of the object dictionary structure. And a number of functions to control high voltage pinball drivers, if you're into that sort of thing.
Diff: include/Node.h
- Revision:
- 5:22a337cdc0e3
- Parent:
- 4:2034b04c86d2
diff -r 2034b04c86d2 -r 22a337cdc0e3 include/Node.h --- a/include/Node.h Sat Jan 09 17:15:29 2016 +0000 +++ b/include/Node.h Sat Feb 13 20:22:59 2016 +0000 @@ -29,12 +29,14 @@ #ifndef PPCAN_NODE_H #define PPCAN_NODE_H +#include "ObjectDictionary.h" /*========================================================================= * Forward declarations *========================================================================= */ struct CanOpenMessage; +//struct ObjectData; /* NMT Constants --------------------------------------------------------*/ #define NMT_STATE_TOGGLE_BIT 0x80 @@ -47,8 +49,6 @@ /* Avoid circular reference */ class ServiceProvider; -class ObjectDictionary; - /** Node Class to implement feature of a CANOpen NMT node @@ -57,6 +57,11 @@ { public: + /* ======================================================================== + * Internal structures and constants + * ======================================================================== + */ + /** Node network management state */ struct State { static const int INITIALIZED = 0x00; @@ -80,45 +85,60 @@ int bLifeGuardToggle; }; + /* ======================================================================== + * Construction + * ======================================================================== + */ + /** Maintain the multitude of states for a node */ - - Node (ServiceProvider * provider); + Node (int id, ServiceProvider * provider, int bLoop = 0); /* ======================================================================== - * Methods to handle operation of node device + * Public Methods * ======================================================================== */ - // TODO: pass elapsed time to Update method - virtual void Update (void); + /** Call from ServiceProvider on an interrupt */ + void FixedUpdate (uint32_t time); + + /** Call from ServiceProvider every loop of Run() */ + void Update (void); + /** Handle message given by the ServiceProvider*/ + int DispatchMessage(CanOpenMessage *canOpenMsg); + /* ======================================================================== - * Message indication - * Called by a ServiceProvider when a message is received. + * Public Member Variables * ======================================================================== */ - /** - * @note + /* Common Node properties -----------------------------------------------*/ + /** Network id for the node + * @note Ref is held in object dictionary and upon setting, may need to + * update some other objects. */ - int DispatchMessage(CanOpenMessage *canOpenMsg); + int nodeId; + /** Loopback Mode. Default off (0), if on (1) then will hear it's own + * messages + */ + int bLoopbackOn; protected: /* ======================================================================== - * Methods to handle various messages - * Called by a ServiceProvider when a message is received. + * Protected? Methods to handle various messages + * Used to deal with everything from inside of Dispatch Message * ======================================================================== */ /* PDO (7.2.2), MPDO (7.2.3) --------------------------------------------*/ - int ConsumePdo (int pdoNum, char * dataIn); - int HandlePdoReadRequest (int pdoNum); + int HandlePdo (CanOpenMessage *canOpenMsg); /* SDO (7.2.4) ----------------------------------------------------------*/ + int HandleSdo (CanOpenMessage *canOpenMsg); int HandleExpeditedDownload (int sdoNum, int index, int subIndex, int dataCount, char * data); int HandleInitiateDownloadRequest (int sdoNum, int index, int subIndex, int dataCount, char * data); @@ -129,12 +149,10 @@ // TODO: express and not express /* SYNC object (7.2.5) --------------------------------------------------*/ - - // TODO: SYNC consumer + int HandleSync (CanOpenMessage *canOpenMsg); /* Time Stamp object (7.2.6) --------------------------------------------*/ - - // TODO: time consumer + /* Handled all inside of the ServiceProvider */ /* Emergency object (7.2.7) ---------------------------------------------*/ @@ -143,7 +161,7 @@ /* Network Management (7.2.8) -------------------------------------------*/ /* ---- Node Control (7.2.8.2.1) ----------------------------------------*/ - int HandleNodeControl (int commandSpecifier); + int HandleNodeControl (CanOpenMessage *canOpenMsg); /* ---- Error Control (7.2.8.2.2) ---------------------------------------*/ @@ -151,17 +169,34 @@ int HandleNodeGuardRequest (int masterId); int ConsumeHeartbeat (int producerId); - +protected: + /* ======================================================================== - * Methods to implement node control in derived classes + * Protected Methods to implement node application in derived classes * ======================================================================== */ - /** Perform actions every cycle + /** Perform actions every cycle (when in operational mode) + * @note Override to implement user application + * @note The OnTick function should avoid modifying data in the object + * dictionary unless necessary, because there will almost certainly be + * a race condition. + */ + virtual void OnFixedUpdate(void) = 0; + + + + virtual void OnUpdate(void) = 0; + + + /* SYNC -----------------------------------------------------------------*/ + /** Perform actions when state changed to stop * @note Override to implement user application */ - virtual void OnUpdate(void) = 0; + virtual void OnSync (uint8_t counter) = 0; + + /* NMT Node Control -----------------------------------------------------*/ /** Perform actions when node reset * @note Override to implement user application */ @@ -182,36 +217,61 @@ */ virtual void OnStopped (void) = 0; + /* Object Dictionary Handling -------------------------------------------*/ + /** Abstract method to give access to the object entries of derived + * classes + */ + virtual ObjectData * ScanIndex(IndexSize index) = 0; + + /* ======================================================================== - * Protected Members + * Protected Methods to Post Messages * ======================================================================== */ + + int PostTPDO (int cobId); - /** object dictionary */ - ObjectDictionary * pMyDictionary; -private: + /* ======================================================================== + * Protected Member Variables + * ======================================================================== + */ + + /* Object Dictionary Handling -------------------------------------------*/ + /** Array of ObjectData to contain all of the object dictionary data */ + ObjectData *dictionary; + + /* Elapsed Time ---------------------------------------------------------*/ + /** Millisecond time stamp at this OnTick() */ + uint32_t timeCurrentTick; + + /** Millisecond difference between current OnTick() and the previous */ + uint32_t timeSinceLastTick; + +// should be private +// TODO: implement functions in Node class to post all types of messages so +// derived classes do not have to consider message structure. +protected: /* ======================================================================== * Private members * ======================================================================== */ - - /** Reference to Service Provider that this node is attached to. - * @note May need to be a pointer, because node id is held in object dictionary - */ + /* ServiceProvider ------------------------------------------------------*/ + /** Reference to Service Provider that this node is attached to */ ServiceProvider * pMyProvider; - - /** Network id for the node - * @note May need to be a pointer, because node id is held in object dictionary - */ - int nodeId; + +private: + /* Common Node Methods --------------------------------------------------*/ + /** Change state of the node */ + void ChangeState(int newState); - /** Network and communication state of the node - * @note - */ - State state; + /* Common Node properties -----------------------------------------------*/ + /** Network and communication state of the node */ + State state; + + }; } /* namespace ppCANOpen */