How do I use system time as a trigger in codesys ladder? - raspberry-pi

Programming a raspberry pi with codesys, using mostly ladder, basically I need to write all data that is currently in a couple arrays to a csv file at midnight, so i'd like to be able to use a dt value as a trigger. I can't figure out how to use that value in ladder, however. I can display the local time on visualizer, but if i wanted something like "if localTime=#value" then coil 'Write' turns on, where is the actual variable for system time?

As far as I know, you need to read the clock from local system using function blocks, for example GetDateAndTime from CAA DTUtil Extern Library. Then you need to keep it up-to-date by using a function block, for example RTC from Standard libary
The following reads the system local time and then updates it with a RTC function block. Works at least on Windows, couldn't test with Raspberry. Please note that if the local time changes for some reason, this won't update it again. So you need to run the GetDateAndTime call every now and then, for example.
First, a program that updates and provides the local time:
PROGRAM PRG_UpdateSystemTime
VAR_OUTPUT
SystemDateTime : DT;
END_VAR
VAR
ReadLocalTime : DTU.GetDateAndTime; //Reads local time from system
RtcBlock : RTC; //Real-time clock - updates the previously received local time
END_VAR
//NOTE: Output is UTC time
//The block that reads local time. NOTE: Error handling is missing
ReadLocalTime(xExecute:= TRUE);
//Running real-time clock
RtcBlock(
EN := ReadLocalTime.xDone AND NOT ReadLocalTime.xError,
PDT := ReadLocalTime.dtDateAndTime,
CDT => SystemDateTime
);
And then one example for ladder. I think there are millions of ways. Note that the "DoSomething" will be TRUE for the whole second, so you should probably use rising edge detection.

Related

Rasberry Pi Data Collection (First Steps)

I work in industrial automation and the functions of automation processors and software are locked down. I'm trying to sample and collect an analog signal at as fast of a rate as I can, <=10ms.
I have tried VB into excel, using a DDERequest and incrementing a delayed loop.
Application.Wait is too slow (1s)
"Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)," had the most promise, but too slow (100ms). It can be pushed faster, but this is on my computer, and then grabbing the float from the automation processor over ethernet... 100ms is the fastest without distorting the "real-time sample."
I have tried a Python module that pulls the float from the IP traffic. (Still over ethernet and too slow)
#x parameters
sample = .001
iterations = 1000
#Collection
for i in range(iterations):
# read the GPIO
float1 = SomeGPIOCommand(pin#)
float2 = SomeGPIOCommand(pin#)
# add the result to our ypoints list
ypoints1.append(float(float1.Value))
ypoints2.append(float(float2.Value))
#x
t = i*sample
xpoints.append(float(t))
# pause
time.sleep(sample)
#Plot
plt.plot(xpoints, ypoints1, 'c-', label='target' ) plt.plot(xpoints, ypoints2 ,'r--', label='actual')
OR is this fast of a sample rate going to require code under an IDE? The key here is matching the time stamp, in ms, exactly with the measured value.
I'd like to get there without an IDE, I just have no clue where to start, especially with the pi.
I have yet to see any example with this performance level.
Appreciate any help!
OR is this fast of a sample rate going to require code under an IDE?
No. A fast sample rate doesn't require coding under an IDE. Whether or not the code is developed under an IDE will have no bearing on sample rate.

How to set baudrate on TwinCAT3 PLC for EL6002 or EL6001 comport?

I cannot find how to set baudrate for the Beckhoff EL6002. I got a hint that I should use CoeWrite block for that but as I am bit new to TwinCAT I cannot find the correct function block. Could someone send a code example (on structured text) how to do that?
An alternative to programming it would be to configure it directly via the IO configuration. If you add a Startup value, it will be set every time the IO changes from a specified state to another. In the pic below, PS means when going from Pre-Op to Safety. So it will work, even if you replace the IO.
Another solution is to change it under the IO configuration and COE-online tab. When you update it there, it will always remember the value.
In code, you can update it through CoE (Can over EtherCAT) too. You can find the index number of the setting variable from documentation. For channel 1, it seems to be 8000:11 so index = 8000 and subindex = 11.
Then by using mailbox writer block (FB_EcCoESdoWriteEx) from Tc2_EtherCAT library it is possible to write a value to that parameter. So when your PLC program starts, first run the code that updates the variable to desired baudrate.
For example, something like this:
TargetValue := 1; //WORD, Check documentation for correct value
//MailBoxWriter = Instance of FB_EcCoESdoWriteEx
MailBoxWriter(
sNetId:= **AmsNetId of the EtherCAT master**,
nSlaveAddr:= **Serial interface terminal port**,
nSubIndex:= 11,
nIndex:= 8000,
pSrcBuf:= ADR(TargetValue),
cbBufLen:= SIZEOF(TargetValue),
bExecute:= TRUE,
tTimeout:= T#500MS,
bCompleteAccess:= FALSE,
bBusy=> ,
bError=> ,
nErrId=>
);
The sNetIdis AmsNetId of the EtherCAT bus master. It can be linked from IO configuration, see Master->Infodata->AmsNetId.
The nSlaveAddr is terminal port from EL6002 and it can be linked from IO configuration, see Terminal->InfoData->AdsAddr->port.

How can I save output from Simulink?

I'm a student learning to use MATLAB. For an assignment, I have to create a simple state machine and collect some results. I'm used to using Verilog/Modelsim, and I'd like to collect data only when the state machine's output changes, which is not necessarily every time/sample period.
Right now I have a model that looks like this:
RequestChart ----> ResponseChart ----> Unit Delay Block --> (Back to RequestChart)
| |
------------------------> Mux --> "To Workspace" Sink Block
I've tried setting the sink block to save as "Array" format, but it only saves 51 values. I've tried setting it to "Timeseries", but it saves tons of zero values.
Can someone give me some suggestions? Like I said, MATLAB is new to me, please let me know if I need to clarify my question or provide more information.
Edit: Here's a screen capture of my model:
Generally Simulink will output a sample at every integration step. If you want to only output data when a particular event occurs -- in this case only when some data changes -- then do the following,
run the output of the state machine into a Detect Change block (from the Logic and Bit Operations library)
run that signal into the trigger port of a Triggered Subsystem.
run the output of the state machine into the data port of the Triggered Subsystem.
inside the triggered subsystem, run the data signal into a To Workspace block.
Data will only be saved at time point that the trigger occurs, i.e. when your data changes.
In your Simulink window, make sure the Relative Tolerance is small so that you can generate many more points in between your start and ending time. Click on the Simulation option at the top of the window, then click on Model Configuration Parameters.
From there, change the Relative Tolerance to something small... like 1e-10. After that, try running your simulation again. You should have a lot more points in your output array that you can now save.

How can I make a 'trial version' of a matlab GUI

My aim is to make a GUI, then by using deploytool to make an exe file from it.
Since I don't want the user to be able to use it for ever I want to make it as a trial version meaning that it will work only for a certain time.
I thought maybe by somehow connecting to the user's computer clock and date, and using the code for a time limit, but I found some problems it this logic.
Any ideas, how it can be done?
Using the computer's clock seems a reasonable way to go. Sure, the user than thwart that by changing the clock, but this will most likely create sufficient inconvenience that they rather pay the reasonable price of the software.
Simply put the following inside the OpeningFcn of your GUI
expiryDate = '2012-12-31';
if now > datenum(expiryDate)
h = errordlg('please upgrade to a full license');
uiwait(h)
return %# or throw an error
end

Is it possible to get the atomic clock timestamp from the iphone GPS?

I'm looking for a reliable way to get the time. It can't be tampered with and it needs to work offline. So no internet time , no user time setup in settings and no BSD uptime time since last reboot. I was wondering since GPS works using atomic clock, whether I could access that information.
Thank you
This works to get the GPS time:
#import <CoreLocation/CoreLocation.h>
CLLocation* gps = [[CLLocation alloc]
initWithLatitude:(CLLocationDegrees) 0.0
longitude:(CLLocationDegrees) 0.0];
NSDate* now = gps.timestamp;
It doesn't seem to be tamper-proof though.
I tried this code on an iPhone 4 in airplane mode (iOS 6.1), and even then it gives a time all right. But unfortunately this time seems to change with the system clock. Ugh.
Funny thing that I found (still in airplane mode) is that if you tamper with the system clock (after turning to off Time & Date's Set Automatically), and then turn Set Automatically back to on, the machine restores the real (original) time without a hitch. this works even after cycling the phone's power. So it seems that there is something like a tamper-proof time the device maintains internally. But how to access this?
P.S. A discussion of this from 2010. The author of the penultimate comment tried this in a fallout shelter: so it's clear the phone is not getting the pristine time from any external source.
Addendum, July 2013
Found a few more posts (here, here and here) about another kind of time measure: system kernel boot time. It's accessed through a call something like this: sysctlbyname("kern.boottime", &boottime, &size, NULL, 0);. Unfortunately it too changes with the user-adjusted data and time, even without reboot. Another function gettimeofday() is similarly dependent on user-defined time.
NSDate and it's CF counterpart are all based on the user controllable time, and thereby aren't tamper proof.
As far as I know, there is no open API for either GPS time or carrier time directly. However, you can check the mach_absolute_time to get untampered time since last boot up, and perhaps use it to at least be aware of how much time has passed since the app has been awoken (without having the potential for that time to be tampered with while the app is running).
mach_absolute_time depends on the processor of the device. It returns ticks since the device was last rebooted (otherwise known as uptime). In order to get it in a human readable form, you have to modify it by the result from mach_timebase_info (a ratio), which will return billionth of seconds (or nanoseconds). To make this more usable I use a function like the one below:
#include <mach/mach_time.h>
int getUptimeInMilliseconds()
{
static const int64_t kOneMillion = 1000 * 1000;
static mach_timebase_info_data_t s_timebase_info;
if (s_timebase_info.denom == 0) {
(void) mach_timebase_info(&s_timebase_info);
}
// mach_absolute_time() returns billionth of seconds,
// so divide by one million to get milliseconds
return (int)((mach_absolute_time() * s_timebase_info.numer) / (kOneMillion * s_timebase_info.denom));
}
Even if you can get hold of the time from GPS you should be aware that GPS time is not quite the same as UTC. The GPS receiver in the iPhone might take care of that for you though.
This gets you the current date and time:
NSDate *now = [NSDate date];
This will be as reliable as you can get. The internal clock on the iPhone will be updated when it can get access to an NTP server. If the phone uses GPS as a time sync source it'll also be used to update the same system-wide clock which is accessible via the above method.
The CoreFoundation equivalent is something like:
CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
Which returns the CoreFoundation equivalent of the normal UNIX seconds-since-epoch timestamp.
The gold standard of timekeeping are the various government time observatories in the U.S. and worldwide. They provide Atomic time. That is used world wide. Apple should be using that. If the want to sync w/ the cell towers, there should be an Alternate internal time. If the tower or GPS system malfunctions all are left with incorrect time.