Matlab simulink interface?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Matlab simulink interface?

Jump to solution
4,724 Views
mathieugranzott
Contributor II

I'm trying to tune some PID but since the only data I see from kinetis is through the debug interface, so it's being quite sloppy.

Is there any way to make a Simulink connection (this way I can use matlab to project my controllers) ?

If this is not possible, how can I dump data back to the PC in a better way?

Thanks

1 Solution
3,228 Views
LuisCasado
NXP Employee
NXP Employee

Hello,

In the FreeMaster comm drivers, you have an example of using  Kinetis as USB-CDC on target and Freemaster comm.

C:\Program Files\Freescale\FreeMASTER Serial Communication V1.6\examples\Kxx

You need to change in freemaster_cfg.h 

 

#define FMSTR_USE_USB_CDC 1 /* To select USB CDC communication interface */

If the device is different to the Kinetis examples, you can use the implementation from USB stack, the file structure is the same.

http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MEDICALUSB

I hope, this helps.

Luis

View solution in original post

0 Kudos
Reply
18 Replies
3,228 Views
JimDon
Senior Contributor III

What board are you using?

USB will add much more over head and effort on your part that just using the serial port.

0 Kudos
Reply
3,228 Views
mathieugranzott
Contributor II

MK40DX256

Yeah, looking around USB seems that would consume too much time.

Ill get my board back and see if the FreeMaster programs works for me (Seems to be close to what I need)

0 Kudos
Reply
3,228 Views
JimDon
Senior Contributor III

printf on a Serial Port? This could work with Simulink.

0 Kudos
Reply
3,228 Views
mathieugranzott
Contributor II

printf (at least when im on eclipse) is extremely slow and cripples the behavior of my program, is it slow because of something on eclipse?

0 Kudos
Reply
3,228 Views
JimDon
Senior Contributor III

Are you printing to a serial port? If so what is the baud rate? Is it interrupt driven? How big are your buffers?

Are you using floating point? 

Eclipse should really have nothing to do with printf, unless you are printing to the debug console.

0 Kudos
Reply
3,228 Views
mathieugranzott
Contributor II

It's probably the debug buffer since im just using "printf" (and I have  not specified the output buffer), maybe this buffer is extremely slow.

Now to find how to make a serial port and print over there. (Is this possible over USB? )

0 Kudos
Reply
3,228 Views
LuisCasado
NXP Employee
NXP Employee

Hi,

I would suggest you to use FreeMaster. FreeMaster is a powerful tool, it is not only an interface to watch/change/scope real time variables, it is an Active X component able to access to MCU variables through serial port/debugger tools, etc.  So, one of the reason of the existence of the tool is your use case, without stopping the application you can watch and change variables on target.

If you have a look to FreeMaster manual: C:\Program Files\Freescale\FreeMASTER 1.3\pcm_um.pdf

You will find the Matlab script to access to the target:

 

6.6.4Matlab m-script

 

Test this code at Matlab console

 

% create FreeMASTER ActiveX object

fmstr = actxserver ('MCB.PCM'); % write 0x10 value into "var16" variable defined in FreeMASTER project bSucc = fmstr.WriteVariable('var16inc', 16); var16 = 0; % read the "var16" variable as defined in FreeMASTER project % "!" - immediate performs the command bSucc = fmstr.ReadVariable('var16'); if bSucc var16 = fmstr.LastVariable_vValue; end % show the value var16 % configure matlab to accept safe array as single dimension feature('COM_SafeArraySingleDim', 1) ; % write 4 bytes to memory 0x20. WriteMemory function expects SafeArray as input data bSucc = fmstr.WriteMemory(32, 4, {11;22;33;44}) % reads 4 bytes from memory at address of var32inc variable bSucc = fmstr.ReadMemory('var32inc', 4);

if bSucc % reads data of last call the ReadMemory()function. readMemResult = fmstr.LastMemory_data end % write to memory at pointer. The value of "var8" represents the pointer, 0x02 is offset. % this is only demonstration !!Beware that wrong value of var8 may cause memory corruption!! array = [11;22;33;44]; bSucc = fmstr.WriteUIntArray('valueof(var8) + 0x02', 4, 1, {array});

Page 75 of the manual.

Regards,

Luis

3,228 Views
mathieugranzott
Contributor II

This program seems to be the right solution but it expects a serial connection, and Im connecting via usb/jtag

Is there a plugin I should install for that?

0 Kudos
Reply
3,228 Views
LuisCasado
NXP Employee
NXP Employee

Hi,

For using serial port, you need to run the FreeMaster serial driver in your target. You can download communication drivers with examples:

https://www.freescale.com/webapp/Download?colCode=FMASTERSCIDRV&appType=license&location=null&fpsp=1... Drivers&WT_VENDOR=FREESCALE&WT_FILE_FORMAT=exe&WT_ASSET=Downloads&sr=3&Parent_nodeId=from search&Parent_pageType=from search

Luis

0 Kudos
Reply
3,228 Views
mathieugranzott
Contributor II

I do not have a serial connection, but the documentation shows it should be possible to do it via usb.FreeMASTER Run-Time Debugging Tool Product Summary Page

But I'm having a hard time figuring out what is the right combination of software to use.

Ohh I had to reinstall freemaster so it would detect the freemaster driver, but still, the configuration tab only shows P&E usb and I'm on segger J-link.

Guess the USB support is only for P&E...


(this is my board btw http://www.newark.com/jsp/search/productdetail.jsp?SKU=27T6527&CMP=KNC-GPLA&mckv=|pcrid|20115736341|... )

Seems like I'll have to go the write my own interface approach...

0 Kudos
Reply
3,228 Views
JimDon
Senior Contributor III

All you will need to do is get a serial port working.

I am pretty sure  I have seem some kwik stick sample code for that. Check the dvd that came with it..

I think it will plug into a tower backplane and you could use the tower serial board to get the level conversion and db-9 connector.

0 Kudos
Reply
3,228 Views
mathieugranzott
Contributor II


That still a hassle... I don't own a tower and I don't have serial connection on my pc, and I have no code guarantee (IMO usb tutorials/code sample should be around, and yet it's hard to find)

Isn't there something simple as arduino-print for freescale?

edit: Making clear, I know arduino is on a DSL so it's impossible to be as simple, but that follows the same construct.

0 Kudos
Reply
3,229 Views
LuisCasado
NXP Employee
NXP Employee

Hello,

In the FreeMaster comm drivers, you have an example of using  Kinetis as USB-CDC on target and Freemaster comm.

C:\Program Files\Freescale\FreeMASTER Serial Communication V1.6\examples\Kxx

You need to change in freemaster_cfg.h 

 

#define FMSTR_USE_USB_CDC 1 /* To select USB CDC communication interface */

If the device is different to the Kinetis examples, you can use the implementation from USB stack, the file structure is the same.

http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MEDICALUSB

I hope, this helps.

Luis

0 Kudos
Reply
3,228 Views
mathieugranzott
Contributor II

Success!

After installing the driver provided by the USB code stack, compiled the example program in freemaster example + the demo project, I got it to work via USB.

Now going to hack this into my project!

0 Kudos
Reply
3,228 Views
LuisCasado
NXP Employee
NXP Employee

Excellent :smileyhappy:

Thank you for the feedback!

Luis

0 Kudos
Reply
3,228 Views
mathieugranzott
Contributor II

Nice getting closer to get it done!

yeah the device is right, the code compiles (after fixing some read only stuff and making sure the folder hierarchy is the same), but still has some steps to it.

After running the code and having a new USB device detected, I got to install the right driver.


(The device is detected at JM CDC Demo, but has a driver error)

The only driver on the  support\...\\cdc folder is fsl_fmstr_wxp, that doesn't work (and it's windows xp)

but the one on the USB code stack is detected as valid, but since it has no digital signature it requires me to change some settings on w8 to install, Ill see if it's working fine tomorrow.

0 Kudos
Reply
3,228 Views
JimDon
Senior Contributor III

>I don't have serial connection on my pc,

Well, a serial to USB cable is what I mean.

If you are using  arduino print then you have decided to use a framework. There are frameworks available, like MQX, but they are not as simple to use.

0 Kudos
Reply
3,228 Views
LuisCasado
NXP Employee
NXP Employee

Hello,

USB connection in this case means to implement USB-CDC in the target MCU. So, you have to use serial connection in FreeMaster tool.

Best Regards,

Luis

0 Kudos
Reply