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.
source/ServiceProvider.cpp
- Committer:
- ptpaterson
- Date:
- 2016-01-04
- Revision:
- 3:12b3c25bdeba
- Parent:
- 2:c724ff3a4e4d
- Child:
- 4:2034b04c86d2
File content as of revision 3:12b3c25bdeba:
/** ****************************************************************************** * @file * @author Paul Paterson * @version * @date 2015-12-22 * @brief CANOpen implementation library ****************************************************************************** * @attention * * <h2><center>© COPYRIGHT(c) 2015 Paul Paterson * * All rights reserved. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include "ServiceProvider.h" #include "Node.h" #include "canopen_api.h" #include <string.h> #include <stdio.h> namespace ppCANOpen { ServiceProvider::ServiceProvider (void) : nodeCount(0) { printf ("-------- SERVICE PROVIDER: CONSTRUCTOR\r\n"); //memset (nodes, 0, sizeof(Node) * SERVICE_MAX_NODES ); // TODO: fix incorrect <-- for (int i = 0; i < SERVICE_MAX_NODES; i++) { nodes[i] = 0; } printf ("-------- SERVICE PROVIDER: MEMSET GOOD\r\n"); hCanOpen = new CanOpenHandle; CanOpenApiInit(hCanOpen); } ServiceProvider::~ServiceProvider (void) { delete hCanOpen; } /*============================================================================= * main loop *============================================================================= */ void ServiceProvider::Run (void) { printf ("-------- SERVICE PROVIDER: RUN\r\n"); while (1) { /* Check for new messages */ CanOpenMessage msg; if (hCanOpen->can) { if (CanOpenApiRead (hCanOpen, &msg)) { /* if new message exists, give it to the nodes*/ /* update all of the nodes */ for (int i=0; i < nodeCount; i++) { nodes[i]->DispatchMessage(&msg); } //Node::DispatchMessage(&msg); //**ECHO** if (CanOpenApiWrite(hCanOpen, &msg)) { } //**END ECHO } } /* update all of the nodes */ for (int i=0; i < nodeCount; i++) { nodes[i]->Update(); } } } /*============================================================================= * Add Node to list *============================================================================= */ int ServiceProvider::AddNode (Node * node) { int result = 1; if (nodeCount < SERVICE_MAX_NODES) { nodes[nodeCount++] = node; } else { result = 0; } return result; } /* ======================================================================== * Methods to handle message requests and responses * ======================================================================== */ void ServiceProvider::RequestPdo (const int pdoNum) {} void ServiceProvider::ProducePdo (const int pdoNum, char * data) {} int ServiceProvider::SendNodeControl (NmtCommandSpecifier cs, unsigned int targetId) { // CanOpenMessage canOpenMsg; // // canOpenMsg.data[0] = (char)cs; // canOpenMsg.data[1] = (char)nodeId; // canOpenMsg.dataCount = 2; // canOpenMsg.type = CANOPEN_TYPE_DATA; // canOpenMsg.format = CANOPEN_FORMAT_STANDARD; // // return CanOpenApiWrite (&canOpenMsg); return 0; } int ServiceProvider::RequestErrorControl (NmtCommandSpecifier cs, unsigned int targetId) { //NMT_STATE_SET(Node::state.nmtState, NMT_STATE_UNKNOWN); return 0; } int ServiceProvider::RespondErrorControl (NmtCommandSpecifier cs, unsigned int targetId) { //NMT_STATE_SET(Node::state.nmtState, NMT_STATE_UNKNOWN); return 0; } } /* namspace ppCANOpen */