Gebruikershulpmiddelen

Site-hulpmiddelen


nl:documentation:script_example

Dit is een oude revisie van het document!


FIXME This page is not fully translated, yet. Please help completing the translation.
(remove this paragraph once the translation is finished)

Script voorbeelden

This page give tips and example of scripting functions.
You can also look at the three standard tool box code from within the program.

Algemeen

Laten we eerst in detail kijken naar de code voor de Goto-knop van de observatie gereedschappen.
Dit omvat veel van de basis van het programmeren.

De volledige script code ziet er uit als volgt:

// Slew telescope
  var ra,de: double;
      a,b,r: string;
      c: Tstringlist;
  begin
  memo_1.clear;
  if MenuTelescopeConnect.checked then begin
    if not StrToAR(Edit_1.text,ra) then begin memo_1.lines.add(rserror+' RA!');exit;end;
    if not StrToDE(Edit_2.text,de) then begin memo_1.lines.add(rserror+' DE!');exit;end;
    a:=floattostr(ra);
    b:=floattostr(de);
    GetSL('STRL1',c);
    c.clear;
    c.add('SLEW');
    c.add(a);
    c.add(b);
    r:=cmd('',c);
    c.clear;
    memo_1.lines.add(r);
  end
  else memo_1.lines.add(rsTelescopeNot);
  end.

Laten we ieder deel in detail bekijken:

// Slew telescope

Dit is commentaar, je kun // {..} (*..*) gebruiken op je opmerkingen en commentaar te markeren.


  var ra,de: double;
      a,b,r: string;
      c: Tstringlist;

Definieer de variabele die we later in het script gebruiken.
Belangrijke types van variabelen zijn: integer, double, string.
Het Tstringlist type wordt hier gebruikt om een commando te versturen naar Skychart.


  begin

Het begin van ons programma.


memo_1.clear;

Maak het tekstvenster leeg waarin we onze berichten willen tonen. Dit is om er zeker van te zijn dat deze niet gevuld is met eerdere berichten.


  if MenuTelescopeConnect.checked then begin

We testen of het menu item MenuTelescopeConnect aangevinkt (“Checked”) is. Dat geeft aan dat de telescoopmontering verbonden is met het programma.
Als het resultaat “waar” is, dan voeren we het blok code uit vanaf “begin” tot het corresponderende “end”.


    if not StrToAR(Edit_1.text,ra) then begin memo_1.lines.add(rserror+' RA!');exit;end;

We proberen de RA in HMS formaat om te zetten van de tekst in Edit_1 tekstvenster naar een numerieke waarde. Als de omzetting niet lukt (bijvoorbeeld omdat we onzin getypt hebben in de tekstvenster), toon dan een foutbericht en beëindig uitvoering.


   a:=floattostr(ra);

Converteer de RA terug naar een string-weergave maar met het decimale formaat zoals vereist voor het commando.


GetSL('STRL1',c);
c.clear;

Vraag een TStringList object op geïdentificeerd via STRL1. We wissen alle gegevens die in het object aanwezig zouden kunnen zijn.


    c.add('SLEW');
    c.add(a);
    c.add(b);

Voeg het commando en de vereiste parameters toe (in dit geval RA en DEC) aan de stringlijst.


   r:=cmd('',c);

Voer het commando uit en sla de het resultaat op in de variabele r.


memo_1.lines.add(r);

Toon het resultaat van het commando in het tekstvenster.


else memo_1.lines.add(rsTelescopeNot);

Als MenuTelescopeConnect.checked “onwaar” was, dan voeren we deze regel uit.
Het toont in het tekstvenster een vertaling van de ingestelde taal van het bericht 'Telescoop niet verbonden'.


 end.

Het einde van het programma.

Call an external library

You can define a function in an external library for use within your script as another local function.

This example implement a simple chronometer by using the GetTickCount function of the Windows API.
There is two button Start and Stop and two text box. A global integer variable is used to store the start time.

Script for the Start button:

 function GetTickCount: Longint; external 'GetTickCount@kernel32.dll stdcall';
 var
   tick: Longint;
 begin  
   tick:=GetTickCount;
   setI('Int1',tick);
   edit_1.text:='Started';
 end.

Script for the Stop button:

function GetTickCount: Longint; external 'GetTickCount@kernel32.dll stdcall';
var
  t: double;
  t1,tick: Longint;
begin
  tick:=GetTickCount;
  getI('Int1',t1);
  t:=double(tick-t1)/1000;
  edit_1.text:='';
  edit_2.text:=formatdatetime('HH:MM:SS.ZZZ',t/24/3600);
end.

You can call any library function this way but beware this is system dependent, the kernel32.dll library is not available on Mac or Linux.

Another limitation is that many library function expect a pointer to a parameter structure. As the script language use byte code internally (as Java) it cannot use a pointer to give the parameters. A solution is to write a C library wrapper that export the function with a flat parameter list.

Using ASCOM directly

This describe how to use an ASCOM device directly from your script without any use of the Skychart internal ASCOM telescope.

This can be use to access another class of device, the example here connect to a dome, or to access additional properties for your telescope.
In the later case you must be careful that your script work as a concurrent to Skychart main program for the device access.

Use the ASCOM chooser

The following code assigned to a button allow to select the ASCOM Dome driver we want to use. The driver name is saved in the text field Edit_1.

 var
   V: variant;
   w,s: widestring;
 begin
   V := CreateOleObject('ASCOM.Utilities.Chooser');
   w:='Dome';
   V.DeviceType:=w;
   s:=edit_1.text;
   s:=V.Choose(s);
   edit_1.text:=s;
   V:=Unassigned;
 end.

Replace w:='Dome'; by Telescope, Focuser, Rotator, Camera, Filter to select another driver class.

Connect to the ASCOM driver

The following code is for the “Connect” button. It connect to the ASCOM Dome driver we select previously. We use the global variable Dome1 to store the ASCOM object.

var
  D: variant;
  s: widestring;
begin
  s:=edit_1.text;
  getV('Dome1',D);
  if VarIsEmpty(D) then
    D := CreateOleObject(s);
  D.connected:=true;
  setV('Dome1',D);
end.

Use the ASCOM driver

Now we want to add a button to open the dome shutter. This is just an example, at this point any ASCOM property can be use.
The first test protect again a program crash if we try to use an initialized variant.
The second test protect again an ASCOM error if the dome is not connected.

var
  D: variant;
begin
  getV('Dome1',D);
  if (not VarIsEmpty(D)) then
    if D.Connected then
       D.OpenShutter;
end.

Disconnect the ASCOM driver

Add a button to disconnect the driver and release the resources.

var
  D: variant;
begin
  getV('Dome1',D);
  if (not VarIsEmpty(D)) then
    if D.Connected then
       D.connected:=false;
  D:=Unassigned;
  setV('Dome1',D);
end.

Open a document

The following code open the Skychart documentation page in the default web browser.
You can use any document type with this function, the document open with the default application the same way as if you double click the document in the file explorer.

begin
  OpenFile('doc\wiki_doc\en\documentation\start.html');
end.

Run a command

There is two different way to run an external command or program, depending if you want to wait for a result or not.

Wait for a result

The following command run the DIR command in the current directory. The result is stored in a stringlist and later show in a text memo. It contain the list of files in the directory.

var r:TstringList;
begin
  GetSL('STRL1',r);
  r.clear;
  RunOutput('dir',r);
  Memo_1.lines.assign(r);
end.

No wait

If the command can run for an undetermined time or do not produce an output you need to use the following form.
This example run the Variable star observer program and exit immediately.

begin
  Run('varobs');
end.
nl/documentation/script_example.1446737839.txt.gz · Laatst gewijzigd: 2015/11/06 20:36 (Externe bewerking)