HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Sleeping in Delphi

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
delphisleepingstackoverflow

Problem

I am programming a rotator using Delphi, with two different angles: azimuth and elevation. Up to now I am testing, so I tried to move the rotator 1 by 1 degree in azimuth and elevation. The rotator starts in (0,0) and finishes in (30,30).

Another important thing is that I need to pause or have the program sleep before the rotator reads the next azimuth and elevation angle because of the operating of the device. If I don't do this, the application crashes. So I implemented this, having the program sleep for 0.64 seconds before increasing the angles.

Do you think that this program is properly implemented? In the future I will program a "Path Wizard" to set the path and the step, so I need to know if there is an easier way to do it. I don't know if using Sleep is the proper way to implement it.

procedure TForm1.btn_testClick(Sender: TObject); 
var p: TMD_ALL_ROTOR_ANGLES_PARAMS;
    error_flag, step: Integer;
begin
     p.calibration := 0;
     p.angles[0] := -1;
     p.angles[1] := -1;
     p.startMotorIx := 0;
     p.dev.group := 1;
     p.dev.number := 1;
     error_flag := 0;
     step:= 1;
     While (p.angles[0]  0 then
     ShowMessage('error');
end;


This is the documentation provided by the manufacturer. I modified the test program slightly to add the function that I described above. In the doc folder there is a file which describes the library, but I didn't find it really helpful.

As you have seen, the documentation that I have is not really helpful, and the manufacturer doesn't answer my emails. Now I'd like to know when the initial angles are reached in order to start moving the rotator. I mean, if I am in position (5,5) and I want to move from (10,10) to (20,20), I move the rotator to (10,10) and I need to know when the rotator is in that position to start moving to (20,20).

I could calculate the time by subtracting the angles and using the time needed per angle. So I could use the Sleep command to have program slept during that time, and t

Solution

A minor comment re. the code style: I don't like angle = -1; while (angle

  • Your device API should give you a way to read the current location of the angle, or to tell you when the current move is finished. If there were an API like that then you could write code like while not is_move_completed() Sleep(10)`



  • Or your device API might (should) even be able to notify you via a callback or 'event' when the move is finished.



  • Or there might be a "synchronous" version of the API which doesn't complete (doesn't return from the md_set_all_rotor_angles procedure) until after it has finished moving and can safely move again.



You might get a better answer if you post a link to the API for the device.

Context

StackExchange Code Review Q#41457, answer score: 5

Revisions (0)

No revisions yet.