Example: User-defined log file from Mikado

This tutorial shows how log files can be written in .csv format from the Mikado program flow. Use cases can be tests for statistical evaluation. The JavaScript task is used, whose interface is also described in the extension chapter JavaScript Task.

The entire script first logs a header line and then data lines. This is logged once in each program run and appended to a log file. The log file can then be imported using conventional table editors, for example. The individual script modules are explained below.

 1//Variablen aus Mikado laden
 2var testlauf = dataStore.getJsObject("nameTestlauf")
 3var ausgabeDateiFunde = dataStore.getJsObject("ausgabeOrdner")
 4var durchlaufN = dataStore.getJsObject("durchlaufN")
 5var fundeListe = dataStore.getJsObject("objektListe")
 6
 7// Konstanten, .csv Spalten Seperator, Zielpfad
 8var sep = ";"
 9ausgabeDateiFunde = ausgabeDateiFunde + testlauf + ".csv"
10
11//Logge in Mikado Konsole
12console.log("JS Auswertung: Durchlauf " + durchlaufN + " schreibe Datei nach: " + ausgabeDateiFunde)
13
14//Überschrift, wird nur im ersten Durchlauf geschrieben
15var ausgabeString = durchlaufN == 0 ? "Zeit;Durchlauf;Anzahl Funde" + "\n" : ""
16
17//Zeitstempel über Mikadoschnittstelle "lab" bekommen
18var timeString = lab.getCurrentDateTime()
19
20//Auswertung
21ausgabeString = ausgabeString + timeString + sep + durchlaufN + sep + fundeListe.length + "\n"
22
23//Ausgabe in Datei
24var append = true //überschreibt Datei, falls false
25lab.writeStringToFile(ausgabeDateiFunde, ausgabeString, append)
26
27console.log("DEBUG JsScript Logging: \n" + ausgabeString)

Suitable Mikado program

The Mikado program contains a simple task sequence within a sequence:

  • Find objects: (requires a fully configured product), for testing purposes fundPosen can also be a static pose list initialized with dummy values, in which case this task is omitted

  • Javascript: Loads and executes the discussed script

  • Increase number: Increase the variable throughputN

  • number greater than: Comparison of the throughputN variable with the maxN variable, jump to the beginning if this has not yet been reached, i.e. in the false case

  • Exit program Task

Variables of the Mikado program

Several variables must be created in the Mikado program on which the script is based:

  • nameTestrun (String): Identifier of this run, also used as the name of the target file

  • outputFolder (String): Absolute path to the output folder, ends with “/”, e.g. “/home/mikado/”

  • runN (integer): Counter of the logging iteration

  • maxN (Integer): Maximum counter after which the logging iteration should be completed

  • fundPosen (PoseList): List containing poses of found objects

Script details

The script is now explained step by step. Divided into initial actions, operation and final actions.

1//Variablen aus Mikado laden
2var testlauf = dataStore.getJsObject("nameTestlauf")
3var ausgabeDateiFunde = dataStore.getJsObject("ausgabeOrdner")
4var durchlaufN = dataStore.getJsObject("durchlaufN")
5var fundeListe = dataStore.getJsObject("fundPosen")
6
7// Konstanten, .csv Spalten Seperator, Zielpfad
8var sep = ";"
9ausgabeDateiFunde = ausgabeDateiFunde + testlauf + ".csv"

In this initial block, the contents of the micadovariables are read first. The output file path is also compiled from this.

12//Logge in Mikado Konsole
13console.log("JS Auswertung: Durchlauf " + durchlaufN + " schreibe Datei nach: " + ausgabeDateiFunde)
14
15//Überschrift, wird nur im ersten Durchlauf geschrieben
16var ausgabeString = durchlaufN == 0 ? "Zeit;Durchlauf;Anzahl Funde" + "\n" : ""
17
18//Zeitstempel über Mikadoschnittstelle "lab" bekommen
19var timeString = lab.getCurrentDateTime()
20
21//Auswertung
22ausgabeString = ausgabeString + timeString + sep + durchlaufN + sep + fundeListe.length + "\n"

The “calculation operations” are carried out in this block. These include logging in the Mikado logs in order to obtain feedback on the script execution. Then the string of the heading is assembled, using a tertiary operator via runN but only in the initial run. The current log line is filled under “Evaluation”, the values are separated by separators. The final line break using “n” will write further content to the following line in the next run.

25//Ausgabe in Datei
26var append = true //überschreibt Datei, falls false
27lab.writeStringToFile(ausgabeDateiFunde, ausgabeString, append)

Finally, the file is written to the file system. In an extended version, the append variable could also be set to false the first time if an initial overwriting of the log file is desired.

Example: Sort Mikado PoseList

This example shows how a Mikado variable of type PoseList can be sorted according to certain criteria. One possible use case is, for example, to sort the result of the Find object task in Mikado according to the z-coordinate in order to give priority to the parts at the top. In the example shown here, a list of poses is therefore sorted in descending order according to the z-coordinate. However, by modifying the example, any PoseList variables can also be sorted in a different way, e.g. ascending instead of descending or according to the x or y coordinate as well as the coordinate angles.

Attention: In the example shown, the original unsorted Mikado variable “ExamplePoseList” is overwritten with the sorted result. If the original variable is to be retained instead, a change is also necessary.

 1// Zu sortierende PoseList-Variable aus Mikado laden
 2var MikadoVariableToBeSorted = 'ExamplePoseList'
 3var PoseList = dataStore.getJsObject(MikadoVariableToBeSorted)
 4
 5// Länge überprüfen, Listen mit weniger als 2 Einträgen können nicht sortiert werden
 6if (PoseList.length <= 1) {
 7    task.status = "PoseList mit 0 oder 1 Einträgen kann nicht sortiert werden!"
 8}
 9// Sortieren und Ergebnis zurück in Mikado-Variable schreiben
10else {
11    PoseList.sort((PoseA, PoseB) => PoseB.z - PoseA.z);
12    task.status = "PoseList \"" + MikadoVariableToBeSorted + "\" absteigend nach der z-Koordinate sortiert"
13    dataStore.setJsObject(MikadoVariableToBeSorted, PoseList)
14}

The JavaScript method Array sort() with a user-defined comparison function is used to sort the pose list. The behavior of the sorting can be changed accordingly by modifying the comparison function in line 11. The individual components of the poses can be accessed using the keywords x, y, z, roll, pitch and yaw. The following are examples of some alternative sorting options:

  • Sort by z-coordinate ascending

    PoseList.sort((PoseA,PoseB) => PoseA.z - PoseB.z);
    
  • Sort by x-coordinate descending

    PoseList.sort((PoseA,PoseB) => PoseB.x - PoseA.x);
    
  • Sort by yaw angle descending

    PoseList.sort((PoseA,PoseB) => PoseB.yaw - PoseA.yaw);