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] < 30) And (p.angles[1] < 30) do
begin
p.angles[0] := p.angles[0] + step;
p.angles[1] := p.angles[1] + step;
case md_set_all_rotor_angles(@app_id, @p) of
MD_NO_ERROR: Sleep(640);
else
error_flag := error_flag + 1;
end;
end;
if error_flag > 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 then start the trajectory.
I have another file in case it could be helpful, but it is not for me.
1 Answer 1
A minor comment re. the code style: I don't like angle = -1; while (angle < 30) begin angle = angle + 1; use angle
. If angle were an unsigned integer then angle = -1
wouldn't work properly. I'd prefer to see a for loop because that ensues that the variable is never set to anything except a correct value.
Re.the algorithm, Sleep(640) is suspicious. You said, "If I don't do this, the application crashes" ... what does "crashes" mean?
I would guess that:
- The number (640) might need to be bigger if you were moving through a bigger 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.
sleep
functions only provide a rough estimation. It can be that they sleep too long or too short, are you aware of that? \$\endgroup\$