Simulink Equivalent to 'now'?

I have been declaring 'now' an extrinsic function in an embedded MATLAB function in Simulink. The trouble is, you can't use extrinsic functions in real time applications using Real Time Windows Target. There doesn't seem to be a Simulink block to access the system time.
I feel the need to write a C/C++ S-Function to try and give the same result. I've never written anything in C/C++ and I don't really think I'm going to be able to figure it out soon enough.
Can anybody help me out here? I kind of figured there would be something in the File Exchange, but searches like 'simulink system time' return huge applications that seem to have nothing to do with what I want.
Thanks in advance,
Zack

Answers (1)

Walter Roberson
Walter Roberson on 30 Nov 2011

0 votes

I would not really expect Simulink to have a block for that, unless it was a target-specific block, as there are a lot of embedded systems that do not have any time-of-day device.
Time-of-day devices need to have a battery backup, as they must keep counting while the system is otherwise turned off. Those batteries run out, making time-of-day devices unsuitable for situations where there is no interface for the user to input the time.
There is not just one way to interface time-of-day devices: some are port-mapped and some are memory-mapped and some involve running special instruction sequences.

4 Comments

This application will be running on a laptop. We are running a Hardware in the Loop Simulation.
Nonetheless, that is a good point and definitely something I didn't think about.
http://www.beaglesoft.com/mainfaqclock.htm#clocks%20in%20pc has some useful information. The software clock it mentions is a function of the operating system; I do not know if it would exist on RTWT. The hardware clock it mentions is standard PC hardware, but as indicated on that site, the readable time resolution is limited to 1 second.
Putting together some information I found, it appears that the standard Real Time Clock (RTC) on MS Windows is accessed via OUT instructions to port 70 and IN instructions from port 71, with the CMOS index being OUT and the response being IN'd. The list of offsets needed is shown at http://www.codepedia.com/1/CMOS_C which makes the very good point that interrupts should be disabled during the reading, as otherwise an interrupt might gain control and OUT something else before you can do the IN, so you would get the wrong value back from the IN.
The 70 and 71 are hex port numbers -- i.e., 112 and 113 decimal.
Thank you for the work you did in finding all that information. I am constantly humbled at what real programmers do. I wouldn't even know where to begin in making a C function to deal with all that.
I ended up just evaluating 'now' in the script that places the needed variables in the workspace, loading that through a Constant block, and adding the simulation time/86400 (seconds in a day) to that value during each simulation step.

Sign in to comment.

Categories

Asked:

on 30 Nov 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!