• AVR Freaks

Helpful ReplyHot!ECAN/CANopen Integration Using CANopenNode and C-Function Call

Author
aboehler
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2014/07/23 19:17:15
  • Location: Tempe, AZ
  • Status: offline
2015/07/03 12:01:56 (permalink)
0

ECAN/CANopen Integration Using CANopenNode and C-Function Call

Hi all,
 
As most of you probably know the current version of the blockset does not yet include a driver for the CAN module. However, I figured out a way to still get the ECAN module to work using the C-Function Call block and an open source CANopen stack from Janez Paternoster called "CANopenNode". Here is the basic idea:
 
  • CANopenNode is written in an object oriented way and provides a full CANopen library along with ECAN drivers for the following Microchips: dsPIC30F, PIC24H, dsPIC33F, PIC32. It also includes a CANopen Object Dictionary Editor.
  • I then wrote my own CANopen_wrapper library to provide C-Functions to be called in MATLAB/Simulink via the C-Function Call block to provide higher level functionality, e.g. initialization of the CAN module and processing CANopen data objects (PDOs/SDOs).
Attached to this post at the bottom is a zip-file with a basic MATLAB/Simulink modelfile, CANopen_wrapper library and slightly modified CANopenNode stack (Janez was blinking an LED on a certain port of the uC, which interfered with my own use of that port). It should show you the basic way of how to go about binding in CANopenNode into your Simulink file. However, it does not contain any application code, obviously you would need to write and integrate that with CANopenNode yourself (using additional C-function calls) for your specific application. For instance, I am using CANopenNode with Lubin's blockset to communicate with one or more motor controllers from Maxon Motors or also to communicate sensor information between two dsPICs.
 
More information on CANopenNode along with the latest versions, documentation, forum, etc. can be found here:
 

 
I'm hoping that this example code may be helpful to others in implementing ECAN/CANopen functionality using Lubin's blockset. If you have any questions or comments please feel free to respond in this thread.
 
Alex
 
post edited by aboehler - 2015/07/03 12:56:54
#1
patyoung
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2015/08/26 23:18:14
  • Location: 0
  • Status: offline
Re: ECAN/CANopen Integration Using CANopenNode and C-Function Call 2015/08/27 18:49:10 (permalink)
0
Hi,Alex
 
The dsPIC used is dsPIC30f6010A.Can you upload CANopenNode with Lubin's blockset?
 
Thank you
#2
aboehler
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2014/07/23 19:17:15
  • Location: Tempe, AZ
  • Status: offline
Re: ECAN/CANopen Integration Using CANopenNode and C-Function Call 2015/09/10 13:37:49 (permalink)
0
Hi patyoung,
 
The latest CANopenNode is available on source forge - just google canopennode and you will find it, for some reason it wont let me post the link in this forum. You can also get started with the base code I provided, see attachment above.
 
Alex
#3
Sobottech
Super Member
  • Total Posts : 254
  • Reward points : 0
  • Joined: 2015/12/02 03:32:17
  • Location: 0
  • Status: offline
Re: ECAN/CANopen Integration Using CANopenNode and C-Function Call 2015/12/10 05:20:39 (permalink)
0
Hm, using Matlab R2012a, the Simulink version is too old. Current version is 7.9 and the model used version is 8.5.
Not knowing when the Matlab version will be updatet, this is a problem, because CAN is essential working with a dsPIC33FJ256MC710A platform.
Is there a work around using CAN with Matlab R2012a?
A fast and easy solution is needed, thats the cause Matlab is used and not C.
Now it's the time for trying an other platform than the STM32F4DISCOVERY and the Waijung Blockset, but without CAN that's not possible.
Here are the warnings Matlab generates:
Warning: Loading model 'ecan_canopen_example' generated with a newer version (8.5) of Simulink 
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 88: block_diagram does not have a parameter named 'ShowMarkup'
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 97: block_diagram does not have a parameter named 'PauseTimes'
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 98: block_diagram does not have a parameter named 'NumberOfSteps'
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 99: block_diagram does not have a parameter named 'SnapshotBufferSize'
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 100: block_diagram does not have a parameter named 'SnapshotInterval'
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 101: block_diagram does not have a parameter named 'NumberOfLastSnapshots'
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 136: block_diagram does not have a parameter named 'CovEnableCumulative'
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 144: block_diagram does not have a parameter named 'CovSFcnEnable'
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 145: block_diagram does not have a parameter named 'CovBoundaryAbsTol'
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 146: block_diagram does not have a parameter named 'CovBoundaryRelTol'
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 147: block_diagram does not have a parameter named 'CovUseTimeInterval'
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 148: block_diagram does not have a parameter named 'CovStartTime'
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 149: block_diagram does not have a parameter named 'CovStopTime'
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 345: Parameter must be a valid enumeration choice.
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 951: This model contains objects of class "Simulink.GlobalDataTransfer" that can not be
instantiated. If you save this model, the information associated with these objects will be lost
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 1001: block_diagram does not have a parameter named 'NeverSave' in group
'MaskParameterDefaults'
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 1002: block_diagram does not have a parameter named 'Internal' in group
'MaskParameterDefaults'
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
Warning: ecan_canopen_example.mdl, line 1003: block_diagram does not have a parameter named 'ReadOnly' in group
'MaskParameterDefaults'
  In general\private\openmdl at 13
  In open at 159
  In uiopen at 196
 
 
 
It seems there is the Lubin Kerhuel's Blockset also for Microchip dsPIC / PIC24 / PIC32 Microcontrollers and with CAN Bus ().
Because the Newest version is from 10/09/2012, it should work with Matlab R2012a.
The Problem is, it seems not supporting dsPIC33FJ256MC710A (Target ressource) or dsPIC33FJ256GP710 (current Explorer 16 test ressource).
 
I would be happy if there is a quick work arround getting the can bus running, or even better, there will be new MPLAB 16-Bit Device Blocks for Simulink with integreated CAN bus.
It would be also fine getting some Beta stuff, for just not quitting migrating to the PIC controller.
#4
aboehler
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2014/07/23 19:17:15
  • Location: Tempe, AZ
  • Status: offline
Re: ECAN/CANopen Integration Using CANopenNode and C-Function Call 2015/12/10 13:40:57 (permalink) ☄ Helpfulby mkchrum 2018/05/18 05:19:17
0
Hi Sobottech,
 
I have attached a new zip file with the model converted to R2012a.
 
Hope this helps,
 
Alex
#5
Sobottech
Super Member
  • Total Posts : 254
  • Reward points : 0
  • Joined: 2015/12/02 03:32:17
  • Location: 0
  • Status: offline
Re: ECAN/CANopen Integration Using CANopenNode and C-Function Call 2015/12/11 05:48:01 (permalink)
0
Thank you very much for the File.
There are no more errors on opening.
I downloaded the CANopenNode (sourceforge.net/p/canopennode/code/HEAD/tree/; download snapshot)
Now I got to download the Lubin's blockset, install it, check which files from CANopenNode are needet (when I understand it right, the CANopenNode is integrated in the File), how to link everything in Simulink and how to set the parameters.
Is there no walktrough what to do, step by step?
When will Microchip integrate CAN in their Blockset?
 
Well, I am going to ask for help...
Next I'm looking at Lubin's blockset...
#6
Lubin
Moderator
  • Total Posts : 372
  • Reward points : 5
  • Joined: 2007/03/31 07:38:15
  • Location: Bayonne, France
  • Status: offline
Re: ECAN/CANopen Integration Using CANopenNode and C-Function Call 2015/12/11 06:08:16 (permalink)
0
Hi Sobottech,
 
This package works with the Microchip Blockset.
The old blockset you mentioned is not maintained nor distributed anymore. 
Please note also that it supports the CAN peripheral available on the older 30f family, but not the ECAN peripheral available with newer chips which is a much more advanced peripheral.
 
Supporting an ECAN block is a complex task regarding all the capability ECAN peripheral propose. Result might not provide the flexibility needed by the end user and might not use fully the peripheral capabilities. Thus the preferred solution for now would be to implement ECAN through a custom C file added to the project.
 
It seems that Alex worked on this topic and is kind enough to share his solution. Please give it a try and let us know how it goes with Matlab R2012a.
 
Lubin
#7
Sobottech
Super Member
  • Total Posts : 254
  • Reward points : 0
  • Joined: 2015/12/02 03:32:17
  • Location: 0
  • Status: offline
Re: ECAN/CANopen Integration Using CANopenNode and C-Function Call 2015/12/11 07:08:26 (permalink)
0
Dear Lubin,
 
thank you for your hint.
 
Just using the postet example, it seams like there is no way for receiving or transmitting CANMessages.
So it seams like there is need to install the Lubin's blockset, for getting transmit, receive and config blocks, because the MPLAB Device Blocks for Simulink support Analog IO, BUS I2C, BUS SPI, BUS UART, Digital IO, PWM IO, Profiling, Pulse Input/Output, QEI, System Configuration, System Functions, System Info, Timers and User Functions, without any CAN BUS Blocks.
 
Well, ok, I implicate, the Lubin's Blockset is needed, because aboehler postet: "For instance, I am using CANopenNode with Lubin's blockset..."
 
But Lubin's Blockset is not working with the actual XC16 compiler.
Using Can Receive and Transmit Blocks require the CAN Configuration block.
The CAN Configuration block requires the dsPIC Master block, which generates following file not found messages:
 
new GMAKE path : C:\PROGRA~1\MATLAB\R2012a\bin\win64\gmake
file not found : C:\PROGRA~2\MICROC~1\xc16\v1.25\lib\dsPIC30F\libpchoose_dsPIC_Target-coff.a
Model configured...
file not found : C:\PROGRA~2\MICROC~1\xc16\v1.25\lib\dsPIC30F\libpchoose_dsPIC_Target-coff.a
file not found : C:\PROGRA~2\MICROC~1\xc16\v1.25\lib\dsPIC30F\libpchoose_dsPIC_Target-coff.a
 
Well, the question is how to configure CAN. Setting bitrate, identifier...
How receive and transmit CAN...
 
 

Attached Image(s)

#8
Lubin
Moderator
  • Total Posts : 372
  • Reward points : 5
  • Joined: 2007/03/31 07:38:15
  • Location: Bayonne, France
  • Status: offline
Re: ECAN/CANopen Integration Using CANopenNode and C-Function Call 2015/12/11 08:14:22 (permalink)
0
Hi Sobottech,
 
There is some confusion about blockset names.
The blockset to use today is the Microchip blockset. All model posted in this thread are based on the Microchip blockset.
 
I tested the version uploaded for Matlab R2012a with Matlab R2012a.
I needed to change one parameters (Simulink often do this error when saving as previous version):
  • Configuration Parameters (Ctrl-E)
    • Code Generation
      • Interface
        • Code replacement library ==> modify from 'NONE' to C89/C90 (AINSI)
Then the model generates code and compile without issues.
 
If you have issues with that model, you should check your blockset configuration.
Make sure you install latest patch available at http://www.microchip.com/forums/FindPost/878432
Then try to compile the examples. (go to a new folder, type picInfo('examples') at the matlab prompt and follow instructions to compile all examples)
#9
Sobottech
Super Member
  • Total Posts : 254
  • Reward points : 0
  • Joined: 2015/12/02 03:32:17
  • Location: 0
  • Status: offline
Re: ECAN/CANopen Integration Using CANopenNode and C-Function Call 2015/12/11 08:58:26 (permalink)
0
Dear Lubin,
 
that sounds great.
Could you please share your Simulink model for receiving, transmitting and configurating everything for CAN messages?
I like to connect a ISO1050 with the dsPIC33FJ256GP710 so I could send and transmit CAN messages with a CAN Analyser.
I like to send messages for testing with the CAN analyser to the pic and send them back thru RS232 to see Data on the PC in a terminal program...
 
 
It's great there is no need in the Lubin's Blockset, because it's causing warnings because there is something missing.
#10
Alphaceem
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2008/11/28 08:35:49
  • Location: 0
  • Status: offline
Re: ECAN/CANopen Integration Using CANopenNode and C-Function Call 2015/12/21 03:59:21 (permalink)
0
Hello Lubin,
 
I study with DSpic33EP512MU810 . I want to communicate with 3 different motordriver over CAN bus. CanOpenNode is compatible with 33EP. And also in CAnOpenNode folder I cant find pin assignment. For EP512MU810 RP96(RF0) and RP97 (RF1). İs pin assignment done automatic?
 
And is there any example code for transmit and receive example code.
 
I wait your reply
 
Your sincerely
 
Alpaslan Yiğit
 
 
#11
aboehler
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2014/07/23 19:17:15
  • Location: Tempe, AZ
  • Status: offline
Re: ECAN/CANopen Integration Using CANopenNode and C-Function Call 2015/12/21 12:41:53 (permalink)
0
Hi Alpaslan,
 
I'm not too familiar with this MCU but know that as long as the CAN peripheral is not remappable CANopenNode should know which pin to use (given that you got the correct CO_driver.h/.c file for your chip).
 
I currently do not have any example code to send and receive as this is usually part of the application specific code. What I can tell you though is that CANopenNode is handling the actual send/receive of PDOs. For client SDO transfer you can use the functions in my wrapper file to create and process client SDOs.
 
To send PDOs you must do three things:
(1) Setup the OD correctly using OD editor. In many cases when something is not working I have found the problem to be in the incorrect setup of the OD.
(2) You then simply write the value to be sent to the correct entry in the OD or read the received value from the correct entry in the OD.
(3) Ensure that the necessary cyclic functions in my wrapper library are getting called and then CANopenNode will handle everything else automatically.
 
To transfer client SDOs you must do two things:
(1) Create a new transfer using the function in my wrapper file
(2) Ensure that the necessary cyclic functions in my wrapper library are getting called.
 
Hope this helps!
 
Alex
#12
Lubin
Moderator
  • Total Posts : 372
  • Reward points : 5
  • Joined: 2007/03/31 07:38:15
  • Location: Bayonne, France
  • Status: offline
Re: ECAN/CANopen Integration Using CANopenNode and C-Function Call 2015/12/21 14:32:04 (permalink)
0
Hi Alpaslan,
 
Quick input: The ECAN peripehral offers remappable pin capabilities on the DSpic 33EP512MU810.
Thus you need to add the code to map the pin accordingly to your design.
I guess there is an initialization function, just place your mapping code there.  
 
From the datasheet DS70616G, you will find mapping information from
  • TABLE 11-1: SELECTABLE INPUT SOURCES (MAPS INPUT TO FUNCTION) and
  • TABLE 11-3: OUTPUT SELECTION FOR REMAPPABLE PINS (RPn)
Lubin
 
#13
dillydilly
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2018/10/24 14:17:38
  • Location: 0
  • Status: offline
Re: ECAN/CANopen Integration Using CANopenNode and C-Function Call 2019/08/09 09:11:55 (permalink)
0
Alex, are you still around to talk with?
#14
Jump to:
© 2019 APG vNext Commercial Version 4.5