Go Back   IceInSpace > Equipment > Software and Computers
Register FAQ Calendar Today's Posts Search

Reply
 
Thread Tools Rate Thread
  #1  
Old 21-10-2010, 10:13 PM
bojan's Avatar
bojan
amateur

bojan is offline
 
Join Date: Jul 2006
Location: Mt Waverley, VIC
Posts: 7,105
Delay function & Delphi

The windows app for focus control via stepper motor is almost ready for posting.. apart from little problem: controlled delay around couple of milliseconds.

I have found a number of delay functions on the web (they are all polling RTC), but instead of milliseconds, they are exiting after seconds, sometimes longer..

The obvious alternative solution would be just a program loop, and it is good for me personally since I can adjust the delay to suit my machine..., but then it will not be good for anyone else... Does anyone know how to solve this properly?

All comments are most welcome and appreciated

Last edited by bojan; 22-10-2010 at 11:11 AM. Reason: question answered
Reply With Quote
  #2  
Old 21-10-2010, 10:32 PM
AndrewJ
Watch me post!

AndrewJ is offline
 
Join Date: Mar 2006
Location: Melbourne
Posts: 1,905
Gday Bojan

If you want a synchronous function, just use sleep().
This works in milliseconds and is synched to the CPU clock automagically.
If you want an asynchronous method, use a TTimer.
You can program this in millisecs as well, and then just set it running as reqd
and create an event for when it stops, or you can test if its running and prevent a specific call going out till it stops.

Andrew
Reply With Quote
  #3  
Old 21-10-2010, 10:37 PM
bojan's Avatar
bojan
amateur

bojan is offline
 
Join Date: Jul 2006
Location: Mt Waverley, VIC
Posts: 7,105
Thanks mate, I will try.

I was also thinkig about this:

procedure TForm1.Delay(msecs:integer);
var
FirstTickCount:longint;
begin
FirstTickCount:=GetTickCount;
repeat
Application.ProcessMessages; {allowing access to other
controls, etc.}
until ((GetTickCount-FirstTickCount) >= Longint(msecs));
end;

Would omitting 'Application.ProcessMessages' help?
I don't mind if machine does nothing else while waiting, this app is for focus, therefore it will be used very occasionally.



Reply With Quote
  #4  
Old 21-10-2010, 10:49 PM
mithrandir's Avatar
mithrandir (Andrew)
Registered User

mithrandir is offline
 
Join Date: Jan 2009
Location: Glenhaven
Posts: 4,161
The traditional C method of doing a sleep with granularity less than 1 second is to use select() with the three fd_sets all being empty and the struct timeval being the required seconds and microseconds.

Code:
       int select(int nfds, fd_set *readfds, fd_set *writefds,
                  fd_set *exceptfds, struct timeval *timeout);
Note that sleep(1) is only guaranteed to sleep to the top of the next second, so not more than one second.
Reply With Quote
  #5  
Old 21-10-2010, 10:51 PM
AndrewJ
Watch me post!

AndrewJ is offline
 
Join Date: Mar 2006
Location: Melbourne
Posts: 1,905
Gday Bojan

Quote:
I don't mind if machine does nothing else while waiting,
Just use the std sleep procedure then.
It automatically converts cpuTicks to millisec, hence is clock independent.
Much simpler than trying to roll yr own timer.

Andrew

PS the "process messages" wouldnt hurt in yr scenario, but will not do anything unless you have an event programmed for the message.
In the case you show, using a timer is probably a neater solution.
Reply With Quote
  #6  
Old 22-10-2010, 06:46 AM
bojan's Avatar
bojan
amateur

bojan is offline
 
Join Date: Jul 2006
Location: Mt Waverley, VIC
Posts: 7,105
Thanks guys,
sleep() actually works as expected (introducing delay couple of milliseconds long..
so I am sticking with this.
The app will be ready for distribution soon, after some more testing :-)
Reply With Quote
  #7  
Old 22-10-2010, 08:05 AM
OneOfOne's Avatar
OneOfOne (Trevor)
Meteor & fossil collector

OneOfOne is offline
 
Join Date: Jul 2005
Location: Bentleigh
Posts: 1,386
Always nice to see people using Delphi!

I had to do more than 5 years of c/c++ when I was in Telstra, but did all of my independent utilities in Delphi. Some VB, bit of Basic, Paradox and lots of Turbo Pascal. It always seemed to me that C can programs can be summarised in one word "obfiscation". My current job only uses Delphi (7) and the code base is at 360,000+ lines, it was 60,000 when I started 10 years ago and only a couple of thousand of those lines still exist.
Reply With Quote
  #8  
Old 22-10-2010, 08:48 AM
bojan's Avatar
bojan
amateur

bojan is offline
 
Join Date: Jul 2006
Location: Mt Waverley, VIC
Posts: 7,105
BTW, in case you didn't know (I didn't), Borland has released turbo Pascal 5.5 as freeware.
http://www.programmersheaven.com/dow.../Download.aspx

Last edited by bojan; 22-10-2010 at 08:59 AM.
Reply With Quote
  #9  
Old 22-10-2010, 09:19 AM
Barrykgerdes
Registered User

Barrykgerdes is offline
 
Join Date: Feb 2007
Location: Beaumont Hills NSW
Posts: 2,900
Hi

I am interested in the methods of using "delay loops". I only program in basic (I am too old to learn something new) and the delay loops of course depend on computer speeds. Programs I wrote for the old PC's and XT's went haywire on 386's and 486's etc. So I needed a way to make delays constant.

When I needed to have a constant delay time I have always had to resort to reading the computer "ticks" over a set time period to produce a modifier to use in the delay loops. It works for me but is a bit messy because it takes a couple of seconds out of the start up time so I am always interested in other methods.

Barry

Last edited by Barrykgerdes; 22-10-2010 at 10:12 AM.
Reply With Quote
  #10  
Old 22-10-2010, 09:26 AM
bojan's Avatar
bojan
amateur

bojan is offline
 
Join Date: Jul 2006
Location: Mt Waverley, VIC
Posts: 7,105
I am also using program loops for delays/timing, when doing something in assembler (this is what I used for stepper driver for my EQ3 - very accurate, crystal controlled timing, as the execution lime for each instructions is known.. and processor (old one, Motorola KJ705) I am using does not have adequate timer).

However, those above-mentioned Delphi delays are funny... instead of milliseconds (what they are supposed to be), they are delaying things by seconds (or more.. I didn't figure out what is happening there). Even sleep(ms) is not quite accurate and it's longer (something to with multitasking in Windows?) but at least it is consistent and short enough to produce acceptable delays for stepper motor pulses duration.

Last edited by bojan; 22-10-2010 at 03:54 PM.
Reply With Quote
  #11  
Old 25-10-2010, 08:35 AM
OneOfOne's Avatar
OneOfOne (Trevor)
Meteor & fossil collector

OneOfOne is offline
 
Join Date: Jul 2005
Location: Bentleigh
Posts: 1,386
Quote:
Originally Posted by bojan View Post
BTW, in case you didn't know (I didn't), Borland has released turbo Pascal 5.5 as freeware.
http://www.programmersheaven.com/dow.../Download.aspx
Yes, we had to use this for a project last year, it had to run super fast under DOS on an old 486 laptop. It took me a while to remember some of the differences, but it fixed the problem, and much quicker to develop than machine code!
Reply With Quote
Reply

Bookmarks


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +10. The time is now 10:30 PM.

Powered by vBulletin Version 3.8.7 | Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Advertisement
Bintel
Advertisement