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/Node.cpp@3:12b3c25bdeba, 2016-01-04 (annotated)
- Committer:
- ptpaterson
- Date:
- Mon Jan 04 06:10:49 2016 +0000
- Revision:
- 3:12b3c25bdeba
- Parent:
- 2:c724ff3a4e4d
- Child:
- 4:2034b04c86d2
Echo communication verified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ptpaterson | 2:c724ff3a4e4d | 1 | /** |
ptpaterson | 2:c724ff3a4e4d | 2 | ****************************************************************************** |
ptpaterson | 2:c724ff3a4e4d | 3 | * @file |
ptpaterson | 2:c724ff3a4e4d | 4 | * @author Paul Paterson |
ptpaterson | 2:c724ff3a4e4d | 5 | * @version |
ptpaterson | 2:c724ff3a4e4d | 6 | * @date 2015-12-14 |
ptpaterson | 2:c724ff3a4e4d | 7 | * @brief CANOpen implementation library |
ptpaterson | 2:c724ff3a4e4d | 8 | ****************************************************************************** |
ptpaterson | 2:c724ff3a4e4d | 9 | * @attention |
ptpaterson | 2:c724ff3a4e4d | 10 | * |
ptpaterson | 2:c724ff3a4e4d | 11 | * <h2><center>© COPYRIGHT(c) 2015 Paul Paterson |
ptpaterson | 2:c724ff3a4e4d | 12 | * |
ptpaterson | 2:c724ff3a4e4d | 13 | * All rights reserved. |
ptpaterson | 2:c724ff3a4e4d | 14 | |
ptpaterson | 2:c724ff3a4e4d | 15 | This program is free software: you can redistribute it and/or modify |
ptpaterson | 2:c724ff3a4e4d | 16 | it under the terms of the GNU General Public License as published by |
ptpaterson | 2:c724ff3a4e4d | 17 | the Free Software Foundation, either version 3 of the License, or |
ptpaterson | 2:c724ff3a4e4d | 18 | (at your option) any later version. |
ptpaterson | 2:c724ff3a4e4d | 19 | |
ptpaterson | 2:c724ff3a4e4d | 20 | This program is distributed in the hope that it will be useful, |
ptpaterson | 2:c724ff3a4e4d | 21 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
ptpaterson | 2:c724ff3a4e4d | 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ptpaterson | 2:c724ff3a4e4d | 23 | GNU General Public License for more details. |
ptpaterson | 2:c724ff3a4e4d | 24 | |
ptpaterson | 2:c724ff3a4e4d | 25 | You should have received a copy of the GNU General Public License |
ptpaterson | 2:c724ff3a4e4d | 26 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
ptpaterson | 2:c724ff3a4e4d | 27 | */ |
ptpaterson | 2:c724ff3a4e4d | 28 | |
ptpaterson | 2:c724ff3a4e4d | 29 | #include "Node.h" |
ptpaterson | 3:12b3c25bdeba | 30 | #include "ServiceProvider.h" |
ptpaterson | 2:c724ff3a4e4d | 31 | #include "ObjectDictionary.h" |
ptpaterson | 2:c724ff3a4e4d | 32 | |
ptpaterson | 2:c724ff3a4e4d | 33 | namespace ppCANOpen |
ptpaterson | 2:c724ff3a4e4d | 34 | { |
ptpaterson | 2:c724ff3a4e4d | 35 | |
ptpaterson | 3:12b3c25bdeba | 36 | Node::Node (ServiceProvider * provider) |
ptpaterson | 3:12b3c25bdeba | 37 | { |
ptpaterson | 3:12b3c25bdeba | 38 | provider->AddNode(this); |
ptpaterson | 3:12b3c25bdeba | 39 | } |
ptpaterson | 3:12b3c25bdeba | 40 | |
ptpaterson | 3:12b3c25bdeba | 41 | /*============================================================================= |
ptpaterson | 3:12b3c25bdeba | 42 | * Methods to handle message indication and confirmation |
ptpaterson | 3:12b3c25bdeba | 43 | *============================================================================= |
ptpaterson | 3:12b3c25bdeba | 44 | */ |
ptpaterson | 3:12b3c25bdeba | 45 | |
ptpaterson | 3:12b3c25bdeba | 46 | int Node::DispatchMessage(CanOpenMessage *canOpenMsg) |
ptpaterson | 3:12b3c25bdeba | 47 | { |
ptpaterson | 3:12b3c25bdeba | 48 | int command = MESSAGE_GET_COMMAND(canOpenMsg->id); |
ptpaterson | 3:12b3c25bdeba | 49 | int nodeId = MESSAGE_GET_NODEID(canOpenMsg->id); |
ptpaterson | 3:12b3c25bdeba | 50 | |
ptpaterson | 3:12b3c25bdeba | 51 | switch (command) { |
ptpaterson | 3:12b3c25bdeba | 52 | case CANOPEN_FUNCTION_CODE_NMT: |
ptpaterson | 3:12b3c25bdeba | 53 | if (canOpenMsg->data[0] == nodeId) { |
ptpaterson | 3:12b3c25bdeba | 54 | HandleNodeControl ((int)canOpenMsg->data[1]); |
ptpaterson | 3:12b3c25bdeba | 55 | } |
ptpaterson | 3:12b3c25bdeba | 56 | break; |
ptpaterson | 3:12b3c25bdeba | 57 | default: |
ptpaterson | 3:12b3c25bdeba | 58 | break; |
ptpaterson | 3:12b3c25bdeba | 59 | } |
ptpaterson | 3:12b3c25bdeba | 60 | |
ptpaterson | 3:12b3c25bdeba | 61 | return 1; |
ptpaterson | 3:12b3c25bdeba | 62 | } |
ptpaterson | 2:c724ff3a4e4d | 63 | |
ptpaterson | 2:c724ff3a4e4d | 64 | /*============================================================================= |
ptpaterson | 2:c724ff3a4e4d | 65 | * Methods to handle message indication and confirmation |
ptpaterson | 2:c724ff3a4e4d | 66 | *============================================================================= |
ptpaterson | 2:c724ff3a4e4d | 67 | */ |
ptpaterson | 2:c724ff3a4e4d | 68 | |
ptpaterson | 2:c724ff3a4e4d | 69 | int Node::ConsumePdo (const int pdoNum, char *const data) |
ptpaterson | 2:c724ff3a4e4d | 70 | { |
ptpaterson | 2:c724ff3a4e4d | 71 | return 0; |
ptpaterson | 2:c724ff3a4e4d | 72 | } |
ptpaterson | 2:c724ff3a4e4d | 73 | |
ptpaterson | 2:c724ff3a4e4d | 74 | int Node::HandlePdoReadRequest (const int pdoNum) |
ptpaterson | 2:c724ff3a4e4d | 75 | { |
ptpaterson | 2:c724ff3a4e4d | 76 | return 0; |
ptpaterson | 2:c724ff3a4e4d | 77 | } |
ptpaterson | 2:c724ff3a4e4d | 78 | |
ptpaterson | 2:c724ff3a4e4d | 79 | int Node::ConsumeEmergency (void) |
ptpaterson | 2:c724ff3a4e4d | 80 | { |
ptpaterson | 2:c724ff3a4e4d | 81 | return 0; |
ptpaterson | 2:c724ff3a4e4d | 82 | } |
ptpaterson | 2:c724ff3a4e4d | 83 | |
ptpaterson | 3:12b3c25bdeba | 84 | int Node::HandleNodeControl (int commandSpecifier) |
ptpaterson | 2:c724ff3a4e4d | 85 | { |
ptpaterson | 3:12b3c25bdeba | 86 | int result = 0; |
ptpaterson | 3:12b3c25bdeba | 87 | |
ptpaterson | 3:12b3c25bdeba | 88 | switch (commandSpecifier) { |
ptpaterson | 3:12b3c25bdeba | 89 | case NMT_CS_START: |
ptpaterson | 3:12b3c25bdeba | 90 | if (state.nmtState != NMT_STATE_INITIALIZED) { |
ptpaterson | 3:12b3c25bdeba | 91 | state.nmtState = NMT_STATE_OPERATIONAL; |
ptpaterson | 3:12b3c25bdeba | 92 | state.bBoot = 0; |
ptpaterson | 3:12b3c25bdeba | 93 | state.bSDO = 1; |
ptpaterson | 3:12b3c25bdeba | 94 | state.bEmergency = 1; |
ptpaterson | 3:12b3c25bdeba | 95 | state.bSYNC = 1; |
ptpaterson | 3:12b3c25bdeba | 96 | state.bLifeGuard = 1; |
ptpaterson | 3:12b3c25bdeba | 97 | state.bPDO = 1; |
ptpaterson | 3:12b3c25bdeba | 98 | state.bLSS = 0; |
ptpaterson | 3:12b3c25bdeba | 99 | OnOperational(); |
ptpaterson | 3:12b3c25bdeba | 100 | result = 1; |
ptpaterson | 3:12b3c25bdeba | 101 | } |
ptpaterson | 3:12b3c25bdeba | 102 | break; |
ptpaterson | 3:12b3c25bdeba | 103 | |
ptpaterson | 3:12b3c25bdeba | 104 | case NMT_CS_STOP: |
ptpaterson | 3:12b3c25bdeba | 105 | if (state.nmtState != NMT_STATE_INITIALIZED) { |
ptpaterson | 3:12b3c25bdeba | 106 | state.nmtState = NMT_STATE_STOPPED; |
ptpaterson | 3:12b3c25bdeba | 107 | state.bBoot = 0; |
ptpaterson | 3:12b3c25bdeba | 108 | state.bSDO = 0; |
ptpaterson | 3:12b3c25bdeba | 109 | state.bEmergency = 0; |
ptpaterson | 3:12b3c25bdeba | 110 | state.bSYNC = 0; |
ptpaterson | 3:12b3c25bdeba | 111 | state.bLifeGuard = 1; |
ptpaterson | 3:12b3c25bdeba | 112 | state.bPDO = 0; |
ptpaterson | 3:12b3c25bdeba | 113 | state.bLSS = 1; |
ptpaterson | 3:12b3c25bdeba | 114 | } |
ptpaterson | 3:12b3c25bdeba | 115 | OnStopped(); |
ptpaterson | 3:12b3c25bdeba | 116 | result = 1; |
ptpaterson | 3:12b3c25bdeba | 117 | break; |
ptpaterson | 3:12b3c25bdeba | 118 | |
ptpaterson | 3:12b3c25bdeba | 119 | case NMT_CS_ENTER_PREOP: |
ptpaterson | 3:12b3c25bdeba | 120 | state.nmtState = NMT_STATE_PREOPERATIONAL; |
ptpaterson | 3:12b3c25bdeba | 121 | state.bBoot = 0; |
ptpaterson | 3:12b3c25bdeba | 122 | state.bSDO = 1; |
ptpaterson | 3:12b3c25bdeba | 123 | state.bEmergency = 1; |
ptpaterson | 3:12b3c25bdeba | 124 | state.bSYNC = 1; |
ptpaterson | 3:12b3c25bdeba | 125 | state.bLifeGuard = 1; |
ptpaterson | 3:12b3c25bdeba | 126 | state.bPDO = 0; |
ptpaterson | 3:12b3c25bdeba | 127 | state.bLSS = 1; |
ptpaterson | 3:12b3c25bdeba | 128 | OnPreoperational(); |
ptpaterson | 3:12b3c25bdeba | 129 | result = 1; |
ptpaterson | 3:12b3c25bdeba | 130 | break; |
ptpaterson | 3:12b3c25bdeba | 131 | |
ptpaterson | 3:12b3c25bdeba | 132 | case NMT_CS_RESET_NODE: |
ptpaterson | 3:12b3c25bdeba | 133 | case NMT_CS_RESET_COM: |
ptpaterson | 3:12b3c25bdeba | 134 | state.nmtState = NMT_STATE_INITIALIZED; |
ptpaterson | 3:12b3c25bdeba | 135 | state.bBoot = 1; |
ptpaterson | 3:12b3c25bdeba | 136 | state.bSDO = 0; |
ptpaterson | 3:12b3c25bdeba | 137 | state.bEmergency = 0; |
ptpaterson | 3:12b3c25bdeba | 138 | state.bSYNC = 0; |
ptpaterson | 3:12b3c25bdeba | 139 | state.bLifeGuard = 0; |
ptpaterson | 3:12b3c25bdeba | 140 | state.bPDO = 0; |
ptpaterson | 3:12b3c25bdeba | 141 | state.bLSS = 0; |
ptpaterson | 3:12b3c25bdeba | 142 | OnInitialize(); |
ptpaterson | 3:12b3c25bdeba | 143 | result = 1; |
ptpaterson | 3:12b3c25bdeba | 144 | break; |
ptpaterson | 3:12b3c25bdeba | 145 | |
ptpaterson | 3:12b3c25bdeba | 146 | default: |
ptpaterson | 3:12b3c25bdeba | 147 | break; |
ptpaterson | 3:12b3c25bdeba | 148 | } |
ptpaterson | 3:12b3c25bdeba | 149 | |
ptpaterson | 3:12b3c25bdeba | 150 | return result; |
ptpaterson | 2:c724ff3a4e4d | 151 | } |
ptpaterson | 2:c724ff3a4e4d | 152 | |
ptpaterson | 2:c724ff3a4e4d | 153 | int Node::HandleNodeGuardRequest (const int masterId) |
ptpaterson | 2:c724ff3a4e4d | 154 | { |
ptpaterson | 2:c724ff3a4e4d | 155 | return 0; |
ptpaterson | 2:c724ff3a4e4d | 156 | } |
ptpaterson | 2:c724ff3a4e4d | 157 | |
ptpaterson | 2:c724ff3a4e4d | 158 | int Node::ConsumeHeartbeat (const int producerId) |
ptpaterson | 2:c724ff3a4e4d | 159 | { |
ptpaterson | 2:c724ff3a4e4d | 160 | return 0; |
ptpaterson | 2:c724ff3a4e4d | 161 | } |
ptpaterson | 2:c724ff3a4e4d | 162 | |
ptpaterson | 2:c724ff3a4e4d | 163 | /*============================================================================= |
ptpaterson | 2:c724ff3a4e4d | 164 | * Methods to handle operation of node device |
ptpaterson | 2:c724ff3a4e4d | 165 | *============================================================================= |
ptpaterson | 2:c724ff3a4e4d | 166 | */ |
ptpaterson | 2:c724ff3a4e4d | 167 | |
ptpaterson | 2:c724ff3a4e4d | 168 | void Node::Update (void) |
ptpaterson | 2:c724ff3a4e4d | 169 | { |
ptpaterson | 3:12b3c25bdeba | 170 | if (NMT_STATE_OPERATIONAL == state.nmtState) { |
ptpaterson | 3:12b3c25bdeba | 171 | OnUpdate(); |
ptpaterson | 3:12b3c25bdeba | 172 | } |
ptpaterson | 2:c724ff3a4e4d | 173 | } |
ptpaterson | 2:c724ff3a4e4d | 174 | |
ptpaterson | 2:c724ff3a4e4d | 175 | /*============================================================================= |
ptpaterson | 2:c724ff3a4e4d | 176 | * Methods to implement node control in derived classes |
ptpaterson | 2:c724ff3a4e4d | 177 | *============================================================================= |
ptpaterson | 2:c724ff3a4e4d | 178 | */ |
ptpaterson | 2:c724ff3a4e4d | 179 | |
ptpaterson | 2:c724ff3a4e4d | 180 | /*============================================================================= |
ptpaterson | 2:c724ff3a4e4d | 181 | * Local functions |
ptpaterson | 2:c724ff3a4e4d | 182 | *============================================================================= |
ptpaterson | 2:c724ff3a4e4d | 183 | */ |
ptpaterson | 2:c724ff3a4e4d | 184 | |
ptpaterson | 2:c724ff3a4e4d | 185 | |
ptpaterson | 2:c724ff3a4e4d | 186 | |
ptpaterson | 2:c724ff3a4e4d | 187 | } /* namspace ppCANOpen */ |
ptpaterson | 2:c724ff3a4e4d | 188 | |
ptpaterson | 2:c724ff3a4e4d | 189 |