1 Star 2 Fork 0

王英伟/CANopenSocket

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

CANopenSocket

CANopenSocket is a collection of CANopen tools running on Linux with socketCAN interface.

It is based on CANopenNode, which is free and open source CANopen Stack and is included as a git submodule.

CANopen is the internationally standardized (EN 50325-4) (CiA301) CAN-based higher-layer protocol for embedded control system. For more information on CANopen see http://www.can-cia.org/

CANopenSocket may be used as a master or a slave device. However, CANopen itself is not a typical master/slave protocol. It is more like producer/consumer protocol. It is also possible to operate CANopen network without a master. Pre-configured process data (PDO) are transmitted from producers. Each PDO may be consumed by multiple nodes.

Master functionality of CANopenNode contains command line interface with SDO and NMT master commands. With SDO master (or SDO client) it is possible to read or write any variable on any device on the CANopen Network. NMT master can start, stop or reset nodes.

CANopenNode should run on any Linux machine. Examples below was tested on Debian based machines, including Ubuntu, Beaglebone Black and Raspberry PI. It is possible to run tests described below without real CAN interface, because Linux kernel already contains virtual CAN interface.

CANopenSocket consists of two applications: canopend, which runs in background, and canopencomm, command interface for SDO and NMT master.

canopend

canopend is an implementation of CANopen device with master functionality. It runs within three threads. Realtime thread processes CANopen SYNC and PDO objects. Mainline thread processes other non time critical objects. Both are nonblocking. Command interface thread is blocking. It accepts commands from socket connection from external application and executes master SDO and NMT tasks.

canopencomm

canopencomm is the other end of the Command interface. It accepts text commands form arguments or from standard input or from file. It sends commands to canopend via socket, line after line. Received result is printed to standard output. It is implementation of the CiA 309 standard.

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Getting started with CANopen Socket

canopend with command interface and "Getting started" is now availabe in base CANopenNode repo. See https://github.com/CANopenNode/CANopenNode/blob/master/doc/gettingStarted.md

Example below is outdated


We will run two instances of CANopend. First will be basic node with ID=4, second, with nodeID = 3, will have master functionality.

Get the project

Clone the project from git repository and get submodules:

$ git clone https://github.com/CANopenNode/CANopenSocket.git
$ cd CANopenSocket
$ git submodule init
$ git submodule update

If you already have the project and just want to update, use:

cd CANopenSocket
git pull # or: git fetch; inspect the changes (gitk); git merge
git submodule update

(If you want to work on submodule CANopenNode, you can cd CANopenNode, and apply git commands directly on it. Initially is in head detached state, so you have to git checkout master first. Then you can control submodule separately, for example git remote add {yourName} {url-of-your-git-repository}, and git pull {yourName} {yourbranch})

First terminal: CAN dump

Prepare CAN virtual (or real) device:

$ sudo modprobe vcan
$ sudo ip link add dev vcan0 type vcan
$ sudo ip link set up vcan0

Run candump from can-utils:

$ sudo apt-get install can-utils
$ candump vcan0

It will show all CAN traffic on vcan0.

Second terminal: canopend

Start second terminal, compile and start canopend.

$ cd CANopenSocket/canopend
$ make
$ app/canopend --help
$ app/canopend vcan0 -i 4 -s od4_storage -a od4_storage_auto

You should now see CAN messages on CAN dump terminal. Wait few seconds and press CTRL-C.

vcan0  704   [1]  00                        # Bootup message.
vcan0  084   [8]  00 50 01 2F F3 FF FF FF   # Emergency message.
vcan0  704   [1]  7F                        # Heartbeat messages
vcan0  704   [1]  7F                        # one per second.

Heartbeat messages shows pre-operational state (0x7F). If you follow byte 4 of the Emergency message into [CANopenNode/stack/CO_Emergency.h], CO_EM_errorStatusBits, you will see under 0x2F "CO_EM_NON_VOLATILE_MEMORY", which is generic, critical error with access to non volatile device memory. This byte is CANopenNode specific. You can observe also first two bytes, which shows standard error code (0x5000 - Device Hardware) or third byte, which shows error register. If error register is different than zero, then node is not able to enter operational and PDOs can not be exchanged with it.

You can follow the reason of the problem inside the source code. However, there are missing non-default storage files. Add them and run it again.

$ echo - > od4_storage
$ echo - > od4_storage_auto
$ app/canopend vcan0 -i 4 -s od4_storage -a od4_storage_auto

vcan0  704   [1]  00
vcan0  184   [2]  00 00                     # PDO message
vcan0  704   [1]  05

Now there is operational state (0x05) and there shows one PDO on CAN address 0x184. To learn more about PDOs, how to configure communication and mapping parameters and how to use them see other sources of CANopen documentation (For example article of PDO re-mapping procedure in CAN newsletter magazine, June 2016 ).

Start also second instance of canopend (master on nodeID=3) in the same window (canopend terminal). Use default od_storage files and default socket for command interface.

$ # press CTRL-Z
$ bg
$ app/canopend vcan0 -i 3 -c ""

Third terminal: canopencomm

Start third terminal, compile and start canopencomm.

$ cd CANopenSocket/canopencomm
$ make
$ ./canopencomm --help

SDO master

Play with it and also observe CAN dump terminal. First Heartbeat at index 0x1017, subindex 0, 16-bit integer, on nodeID 4.

$ ./canopencomm [1] 4 read 0x1017 0 i16
$ ./canopencomm [1] 4 write 0x1017 0 i16 5000

In CAN dump you can see some SDO communication. You will notice, that Heartbeats from node 4 are coming in 5 second interval now. You can do the same also for node 3. Now store Object dictionary, so it will preserve variables on next start of the program.

$ ./canopencomm 4 w 0x1010 1 u32 0x65766173

You can read more about Object dictionary variables for this CANopenNode in [canopend/CANopenSocket.html].

NMT master

If node is operational (started), it can exchange all objects, including PDO, SDO, etc. In pre-operational, PDOs are disabled, SDOs works. In stopped only NMT messages are accepted.

$ ./canopencomm 4 preop
$ ./canopencomm 4 start
$ ./canopencomm 4 stop
$ ./canopencomm 4 r 0x1017 0 i16 		# time out
$ ./canopencomm 4 reset communication
$ ./canopencomm 4 reset node
$ ./canopencomm 3 reset node

In canopend terminal you see, that both devices finished. Further commands are not possible. If you set so, last command can also reset computer.

Combining NMT commands into a single file

Create a commands.txt file, and for its content enter your commands. Example:

[1] 3 start
[2] 4 start

Make canopencomm use that file:

$ ./canopencomm -f commands.txt
[1] OK
[2] OK

CANOpen network client

By default canopencomm and canopend opens a local unix socket to interact. That's fine for local usage, but note that TCP sockets are also supported. Hence we can interact with our devices from a remote client.

To interact with canopend over TCP, specify its TCP port using the -t argument. On our embedded linux device:

$ ./canopend can0 -i 1 -c "" -t 6000

On our linux pc we need to specify the target device using -t, and its port using -p:

$ ./canopencomm -t 192.168.0.2 -p 6000

Next steps

Now you can learn more skills on CANopen from some other sources: books, data sheet of some CANopen device, standard CiA 301(it's free), etc. Then you can enter the big world of CANopen devices.

Accessing real CANopen devices is the same as described above for virtual CAN interface. Some tested USB to CAN interfaces, which are natively integrated into Linux are:

  • Simple serial USBtin - Start with: sudo slcand -f -o -c -s8 /dev/ttyACM0 can0; sudo ip link set up can0
  • EMS CPC-USB - Start with: sudo ip link set up can0 type can bitrate 250000
  • PCAN-USB FD - Needs newer Linux kernel, supports CAN flexible data rate.
  • You can get the idea of other supported CAN interfaces in Linux kernel source.
  • Beaglebone or Paspberry PI or similar has CAN capes available. On RPI worked also the above USB interfaces, but it was necessary to compile the kernel.

With CANopenNode you can also design your own device. There are many very useful and high quality specifications for different device profiles, some of them are public and free to download.

Here we played with virtual CAN interface and result shows as pixels on screen. If you connect real CAN interface to your computer, things may become dangerous. Keep control and safety on your machines!

LSS extension

LSS (Layer settings service) is a extension to CANopen described in CiA DSP 305. The interface is described in CiA DS 309 2.1.0 (ASCII mapping). LSS allows the user to change node ID and bitrate, as well as setting the node ID on an unconfigured node.

LSS uses the the OD Identity register as an unique value to select a node. Therefore the LSS address always consists of four 32 bit values. This also means that LSS relies on this register to actually be unique.

To use LSS, a compatible node is needed. Note that canopend only includes LSS master functionality.

The following example show some typical use cases for LSS:

  • Changing the node ID for a known slave, store the new node ID to eeprom, apply new node ID. The node currently has the node ID 22.

     $ ./canopencomm lss_switch_sel 0x00000428 0x00000431 0x00000002 0x5C17EEBC
     $ ./canopencomm lss_set_node 4
     $ ./canopencomm lss_store
     $ ./canopencomm lss_switch_glob 0
     $ ./canopencomm 22 reset communication
    

    Note that the node ID change is not done until reset communication/node

  • Changing the node ID for a known slave, store the new node ID to eeprom, apply new node ID. The node currently has an invalid node ID.

     $ ./canopencomm lss_switch_sel 0x00000428 0x00000431 0x00000002 0x5C17EEBC
     $ ./canopencomm lss_set_node 4
     $ ./canopencomm lss_store
     $ ./canopencomm lss_switch_glob 0
    

    Note that the node ID is automatically applied.

  • Search for a node via LSS fastscan, store the new node ID to eeprom, apply new node ID

     $ ./canopencomm [1] _lss_fastscan
    
     [1] 0x00000428 0x00000432 0x00000002 0x6C81413C
    
     $ ./canopencomm lss_set_node 4
     $ ./canopencomm lss_store
     $ ./canopencomm lss_switch_glob 0
    

    To increase scanning speed, you can use

     $ ./canopencomm [1] _lss_fastscan 25
    

    where 25 is the scan step delay in ms. Be aware that the scan will become unreliable when the delay is set to low.

  • Auto enumerate all nodes via LSS fastscan. Enumeration automatically begins at node ID 2 and node ID is automatically stored to eeprom. Like with _lss_fastscan, an optional parameter can be used to change default delay time.

     $ ./canopencomm lss_allnodes
    
     [1] OK, found 3 nodes starting at node ID 2.
    
  • To get further control over the fastscan process, the lss_allnodes command supports an extended parameter set. If you want to use this set, all parameters are mandatory. Auto enumerate all nodes via LSS fastscan. Set delay time to 25ms, set enumeration start to node ID 7, do not store LSS address in eeprom, enumerate only devices with vendor ID "0x428", ignore product code and software revision, scan for serial number

     $ ./canopencomm lss_allnodes 25 7 0 2 0x428 1 0 1 0 0 0
    
     [1] OK, found 2 nodes starting at node ID 7.
    

    The parameters are as following:

    • 25 scan step delay time in ms
    • 7 enumeration start
    • 0 store node IDs to eeprom; 0 = no, 1 = yes
    • 2 vendor ID scan selector; 0 = fastscan, 2 = match value in next parameter
    • 0x428 vendor ID to match
    • 1 product code scan selector; 0 = fastscan, 1 = ignore, 2 = match value in next parameter
    • 0 product code to match (ignored in this example)
    • 1 software version scan selector; 0 = fastscan, 1 = ignore, 2 = match value in next parameter
    • 0 software version to match (ignored in this example)
    • 0 serial number scan selector; 0 = fastscan, 1 = ignore, 2 = match value in next parameter
    • 0 serial number to match (not used in this example)

Note that only unconfigured nodes (those without a valid node ID) will take part in fastscan!

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

CANopen implementation for Linux SocketCAN with master command interface. 展开 收起
README
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Dr_tree/CANopenSocket.git
git@gitee.com:Dr_tree/CANopenSocket.git
Dr_tree
CANopenSocket
CANopenSocket
master

搜索帮助