Author |
Topic: Serial port pin driving an interrupt (Read 6278 times) |
|
manxman
Developer
member is offline


Gender: 
Posts: 21
|
 |
Serial port pin driving an interrupt
« Thread started on: Dec 29th, 2012, 6:53pm » |
|
I would very much appreciate advice on how to incorporate an interrupt in my program whose purpose is to timestamp seismic data arriving via an RS232 port.
The 1 pulse per second (PPS) signal from a GPS receiver is connected to pin 6 (DSR) on the same serial port. Each time the rising edge of the PPS is detected, the value of the Windows millisecond timer is logged as Then%. When each seismic data packet arrives the millisecond time value Now% is logged, giving me the interval (Now%-Then%) which is added to the number of PPS accumulated (Tick%) to obtain the time since program start, with a theoretical accuracy of a millisecond. At the moment my code comprises a continuous loop which detects when the PPS signal or the data packet arrives and then does timestamping of the latter.
In a reply to another question, Richard suggested that I use an interrupt as a possible solution. From my limited understanding this would seem a better solution here too, since an interrupt that set Then% immediately the PPS signal was detected, would prevent pulses from being missed. The listing shows the essence of what I would like to achieve. Having no experience of how to configure interrupts is the reason for my humble query!
Regards,
Manxman
Code: SYS "timeBeginPeriod", 1 GPS = OPENUP "COM4: baud=9600 parity=N data=8 stop=1 xon=off" PinHigh=FALSE Tick%=0 ----------------- INTERRUPT FUNCTION ----------------- SYS "GetCommModemStatus", @hfile%(GPS), ^modemstatus% IF modemstatus%=32 AND PinHigh=FALSE THEN PinHigh=TRUE Tick%+=1 SYS "timeGetTime" TO Then% ENDIF IF modemstatus%<>32 AND PinHigh=TRUE THEN PinHigh=FALSE ENDIF ----------------- INTERRUPT FUNCTION ----------------- REM Remainder of program ......... REM When data is received from the port do: SYS "timeGetTime" TO Now% REM Then use Tick%+(Now%-Then%)/1000 to do timestamping of the data REM etc .......
|
« Last Edit: Dec 29th, 2012, 6:59pm by manxman » |
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Serial port pin driving an interrupt
« Reply #1 on: Dec 29th, 2012, 10:00pm » |
|
on Dec 29th, 2012, 6:53pm, manxman wrote:Having no experience of how to configure interrupts is the reason for my humble query! |
|
The first thing to note is that, as far as I know, there are no hardware serial port interrupts that you can trap in user code. Therefore you will need to create your own software interrupt.
The essence of what you would do is as follows. Start a new thread which most of the time sits in the WaitCommEvent API awaiting an EV_DSR event. On seeing that event, generate a software interrupt to your BASIC program.
The only way to achieve this in pure BBC BASIC code is to run two separate programs in two separate processes. The first waits for the EV_DSR event and sends a message to the second, which can be trapped using ON SYS.
However this is rather overkill - and the timing is uncertain because it depends on context switches between processes etc. Therefore my preferred method would be to do the waiting for the EV_DSR event in assembler code, which is faster and neater.
Unless you are experienced at writing BB4W assembler code you will probably need some help with this.
Richard.
|
|
Logged
|
|
|
|
manxman
Developer
member is offline


Gender: 
Posts: 21
|
 |
Re: Serial port pin driving an interrupt
« Reply #2 on: Dec 30th, 2012, 8:11pm » |
|
Richard,
Thanks for your advice. Indeed, I have no experience whatsoever of using assembler and would very much appreciate your help with the coding of this problem. From the various tests I have been carrying out, the combination of 1PPS GPS triggering and PC interval timing has the potential for a resolution and jitter of only a few milliseconds in UTC time.
Regards,
Manxman
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Serial port pin driving an interrupt
« Reply #3 on: Dec 30th, 2012, 9:22pm » |
|
on Dec 30th, 2012, 8:11pm, manxman wrote:From the various tests I have been carrying out, the combination of 1PPS GPS triggering and PC interval timing has the potential for a resolution and jitter of only a few milliseconds in UTC time. |
|
I have previously explained, in the thread Setting Windows Tick, that this is not the case. Windows is not a Real Time Operating System and can never achieve a guaranteed resolution of only a few milliseconds. If that is what you need there is only one way to achieve it - time-stamp your data before it even reaches the PC (e.g. using a hardware unit designed for the purpose).
In the other thread I stated "If a resolution in the order of 20 milliseconds is acceptable then you may be able to get away with a purely software solution (on a statistical basis - it cannot be guaranteed)". I don't think I've seen a comment from you on whether that is or isn't an acceptable level of performance.
Until it has been established that a purely software solution can achieve the performance you need there's no point in either of us wasting time writing code!
Richard.
|
|
Logged
|
|
|
|
manxman
Developer
member is offline


Gender: 
Posts: 21
|
 |
Re: Serial port pin driving an interrupt
« Reply #4 on: Dec 31st, 2012, 11:04am » |
|
Richard,
For my application a resolution of 20 milliseconds is acceptable (this would locate a seismic wavefront to within about 60m). The main reason for trying to implement a pin-triggered interrupt, of the type you outlined, is to reduce the probability that pulses on the pin may be missed, which can corrupt the timing of data.
Of course I do realise that an external GPS timestamping unit is the ideal solution but it would be good to avoid making more hardware if a software timing solution can be achieved with acceptable resolution.
This software project is key to my creating an array of detectors for which the cost of additional hardware would be prohibitive.
Regards,
Manxman
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Serial port pin driving an interrupt
« Reply #5 on: Dec 31st, 2012, 5:10pm » |
|
on Dec 31st, 2012, 11:04am, manxman wrote:For my application a resolution of 20 milliseconds is acceptable |
|
OK, just so long as you understand that there can be no guarantee of performance, and that occasionally there could be much larger errors.
Richard.
|
|
Logged
|
|
|
|
manxman
Developer
member is offline


Gender: 
Posts: 21
|
 |
Re: Serial port pin driving an interrupt
« Reply #6 on: Jan 1st, 2013, 10:37am » |
|
Richard,
I do appreciate your involvement.
You might be interested to know the context of this problem. I operate a continuous seismic recording station on the Isle of Man using my own design of instrument which, to date has detected 432 earthquakes (most along the Pacific rim). My plan now is to create an array of seismometers on the Island, the data from which can be correlated to determine the direction of arrival of seismic waves and thus pinpoint an event's location. For this it is essential that the data are timestamped to a relative precision of about 20-30 milliseconds. I have considered various options: radio atomic clocks, mains frequency clocks, stabilised crystal oscillators and GPS. The 1Hz pulse from a GPS module is generally stable to better than a microsecond and linked exactly to UTC (=GMT). This option has therefore been selected as the most cost-effective and suitable, provided that the 1 Hz pulse intervals can be captured and subdivided with sufficient precision. It would be great to show that this can be done using BBC4W, rather than in hardware.
Best wishes for the New Year
Manxman
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Serial port pin driving an interrupt
« Reply #7 on: Jan 1st, 2013, 4:15pm » |
|
on Jan 1st, 2013, 10:37am, manxman wrote:For this it is essential that the data are timestamped to a relative precision of about 20-30 milliseconds. |
|
You use the term "essential", and yet you also say "about". All one can say, with confidence, is that a Windows software-based timing system is likely to exhibit a classic bell-shaped statistical distribution, where the larger the timing error the less frequently it will occur.
So the question you have to ask yourself is: what would be the consequences of an occasional timing error much larger than the 20-30 milliseconds range? Would it cause an obviously anomalous reading (which you can therefore ignore) or could it give rise to a false reading that might be indistinguishable from a genuine event?
Edit: Here are a couple of 'scholarly articles', which completely contradict each other! My advice would be to assume that the more pessimistic of the two (Myors) is nearer the mark:
http://link.springer.com/content/pdf/10.3758%2FBF03207727 http://psych.cf.ac.uk/home2/chambers/Chambers_2003_BRMIC.pdf
Richard.
|
« Last Edit: Jan 1st, 2013, 4:38pm by admin » |
Logged
|
|
|
|
manxman
Developer
member is offline


Gender: 
Posts: 21
|
 |
Re: Serial port pin driving an interrupt
« Reply #8 on: Jan 2nd, 2013, 10:29am » |
|
Ground motion data at each seismometer is being logged at a rate of approximately 20Hz, and each data set will be interpolated, using the GPS-derived timestamps, to a UTC-disciplined synchronous rate of 20Hz. The interpolation process will look for and reject any data items with anomalous spikes in timing. The phase relationship between the instruments' data will then be obtained by cross-correlating timeslices of typically 10 minutes (1200 readings): the result being a set of phase differences within the array from which the bearing of an arriving seismic wavefront can be derived.
Apart from earthquake detection, my primary research interest is investigating the character and mechanism of microseismic noise, which is thought to be generated by weather systems passing over the ocean.
Suppose we have a pair of instruments spaced 10km apart East-West, logging microseismic surface waves from the North Atlantic, travelling with a velocity of 3km/s. Then a 100ms difference in their data phase corresponds to a wave arriving with a bearing of 1.7 degrees from north. Thus, even if the timing error is around 100ms, the bearing resolution towards northern sources in this configuration is acceptable for my purpose. Obviously, the resolution is zero with an E-W array for waves travelling E-W, but this would be the case no matter how good the timing. For this reason I will be deploying at least 5 instruments in a broad geographic pattern to ensure sufficient bearing resolution over the angular swathe of interest.
Regards,
Manxman
|
|
Logged
|
|
|
|
manxman
Developer
member is offline


Gender: 
Posts: 21
|
 |
Re: Serial port pin driving an interrupt
« Reply #9 on: Jan 4th, 2013, 4:51pm » |
|
I thought that I would reply to myself with the results of some timing tests I am carrying out!
The 20Hz, RS232 data stream from a single seismometer is being split into two feeds that go to a pair of PCs, each of which has an identical GPS module. The 1 Pulse Per Second (PPS) output from each module is converted to an RS232 voltage level and connected to Pin 6 (DSR) of the port that receives the seismometer data. A second (USB virtual) serial port on each PC receives NMEA sentences from the GPS module.
My program begins by capturing the UTC (=GMT) start time from an NMEA sentence, then adds a second each time the PPS is detected. This PPS event also starts a Windows millisecond counter, so that the interval between PPS signals is divided, yielding UTC time to a (theoretical) resolution of one millisecond. This process is essentially what I have described in earlier posts.
My program logs pairs of data: UTC time as seconds since midnight (e.g. 45678.123) together with the seismic velocity (e.g. -435). Obviously, each PC's log will contain an identical list of velocities, providing an exact basis for comparing corresponding UTC times.
In my experiments, data have been recorded for several minutes, first with a pair of identical Asus PCs, then with an Asus PC and an HP Brio, both running Windows XP.
Comparing the data sets from the pair of Asus machines the UTC timestamps are all identical to the millisecond. In the second experiment I find that about 80% of the times are identical to the millisecond, with the maximum deviation in the remainder being 3 milliseconds.
So far, so good in terms of progress towards my objective!
Regards,
Manxman
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Serial port pin driving an interrupt
« Reply #10 on: Jan 5th, 2013, 1:23pm » |
|
on Jan 4th, 2013, 4:51pm, manxman wrote:Comparing the data sets from the pair of Asus machines the UTC timestamps are all identical to the millisecond. |
|
Have you attempted to provoke the kind of event which is likely to upset the timing? For example, have you arranged for context-switches to take place? Have you performed disk accesses? Have you tried to access a network?
According to this Microsoft article a 'quantum', i.e. the time-slice for which a thread will run until a context switch occurs, is either 3.3 ms (single CPU) or 5 ms (multiple CPU):
http://support.microsoft.com/kb/259025
That would imply that timestamps "identical to the millisecond" would be expected only if no context switches take place at all, which is obviously unrealistic. Even BBC BASIC, running on its own with no other applications loaded, has two or three threads!
There's also a relevant discussion at the link below, where somebody states: "Thus knowing how accurate I could get a time-stamp became important. Knowing that Windows would eat 10 or 20 mSec here and there became my limit of accuracy":
http://discuss.joelonsoftware.com/default.asp?joel.3.66319.13
And another relevant comment at the same place: "As for windows being used in control applications, it may not have any true RT facilities, but with a fast enough processor and a low enough workload, you can get some soft RT performance along the lines of 99% of events happen within their time limit.... It also helps if you put in some effort to trap those 1% of errors well and handle them properly".
Richard.
|
|
Logged
|
|
|
|
manxman
Developer
member is offline


Gender: 
Posts: 21
|
 |
Re: Serial port pin driving an interrupt
« Reply #11 on: Jan 5th, 2013, 4:13pm » |
|
Q: Have you attempted to provoke the kind of event which is likely to upset the timing?
A: Yes. I found that when my program's MODE 7 window is dragged about the screen, the timing can miss a PPS count, leading to a 1s step in timing. Hence, I need to find a way to fix the window position and size. With regard to disk access, the program is continually writing pairs of seismic velocity and timestamp data to disk. None of the array PCs will be on a network and no other programs, such as virus checking, will be running, deliberately to reduce the OS's work and minimise the chance of timing glitches.
Q: That would imply that timestamps "identical to the millisecond" would be expected only if no context switches take place at all, which is obviously unrealistic. Even BBC BASIC, running on its own with no other applications loaded, has two or three threads!
A: All I can report is what I have observed by experiment! So far the timing discrepancies are well within the requirements of my project.
Incidentally, when it comes to a real field deployment there are some other interesting effects to consider. The most important factor is that the local geology and topography at each array station can cause a timing offset that may depend on the arrival direction of the seismic wave. This phenomenon is well known and must be taken into consideration during array processing.
Regards,
Manxman
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Serial port pin driving an interrupt
« Reply #12 on: Jan 5th, 2013, 9:45pm » |
|
on Jan 5th, 2013, 4:13pm, manxman wrote:I found that when my program's MODE 7 window is dragged about the screen, the timing can miss a PPS count, leading to a 1s step in timing. Hence, I need to find a way to fix the window position and size. |
|
I really think you're looking at this from the wrong angle! If you have observed that dragging a window can lead to an unacceptable timing error, there are probably many other circumstances which can too. Trying to prevent the one specific situation you have noticed (e.g. by fixing the window) whilst blissfully ignoring the possibility (nay, likelihood) that there are others you haven't yet noticed, is in my opinion reckless.
If there is to be any hope that Windows can reliably meet your timing requirements (and I remain sceptical) you need to find a way to make your program tolerant of dragging the window, not prevent the window being dragged! I would have expected that using an interrupt-based method, as previously discussed, ought to mean that missing (rather than delaying) a Pulse Per Second count is extremely unlikely.
Richard.
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Serial port pin driving an interrupt
« Reply #13 on: Jan 6th, 2013, 10:05am » |
|
on Jan 5th, 2013, 9:45pm, Richard Russell wrote:I would have expected that using an interrupt-based method, as previously discussed, ought to mean that missing (rather than delaying) a Pulse Per Second count is extremely unlikely. |
|
Further, how do you currently detect the PPS input? If you poll the DSR pin (using for example SYS "GetCommModemStatus") then the likelihood of missing a pulse entirely is quite high, whereas if you monitor the EV_DSR event (using for example SYS "WaitCommEvent") then the likelihood of missing a pulse is comparatively lower. The code complexity is not significantly different (except that the event method doesn't tell you directly in which direction the DSR input changed!).
In either case, what method do you use for preventing excessive use of CPU time? Normally one would use WAIT or SYS "Sleep" to suspend the CPU, but of course they are both deliberately triggering a task switch, which may be exactly what you don't want! But equally sitting in a tight loop using 100% CPU time isn't acceptable either, so I wonder what you are currently doing.
Richard.
|
|
Logged
|
|
|
|
manxman
Developer
member is offline


Gender: 
Posts: 21
|
 |
Re: Serial port pin driving an interrupt
« Reply #14 on: Jan 6th, 2013, 5:04pm » |
|
My program does indeed run around a tight loop monitoring the status of the DSR pin (using SYS GetCommModemStatus), while also checking the RS232 buffer to check whether there are data to receive. If there are, then the UTC time stamp is created by summing: (UTC start time in seconds + number of PPS ticks + milliseconds since last PPS). This process was described in my previous posts.
I note the concerns you raise. However, as I mentioned before I have no experience in assembly programming and would therefore welcome your help in coding an EV_DSR detector to reduce the chance of missing PPS events, for example when the window is dragged or resized. Alternatively, how do I use the SYS WaitCommEvent function you describe to make my DSR detection more reliable?
My program is currently configured to detect the rising edge of DSR but the alternative of falling edge detection would also be OK. Because the PPS is 500ms wide it is important that the same edge is always used.
Regards,
Manxman
|
« Last Edit: Jan 6th, 2013, 5:05pm by manxman » |
Logged
|
|
|
|
|