TimeScript

From ElphelWiki
Jump to: navigation, search

About

This PHP script allows to trigger timed events based on the current frame number. This means that all Elphel related parameters can be altered at a specific keyframe.

Code

The application consists of 2 files: the interpreter and the script. The script contains a list of events and the time when these events should be triggered. The interpreter loads the script and makes sure that the events are executed at the correct time.

The Script Interpreter

<?
if (!isset($_GET['script'])) {
  die ("error no script provided");
} else {
  $scriptname = $_GET['script'];
}
</highlightSyntax>
$frame_count = 0;
$startframecount = elphel_get_P_value(ELPHEL_FRAME); 
$starttime = date("U");

$fh = fopen($scriptname, 'r');
$script_file_content = fread($fh, filesize($scriptname));
fclose($fh);

$events = array();

$scriptlines = explode ("\n", $script_file_content);
$index = 0;
foreach ($scriptlines as $scriptline) {
  if ((substr($scriptline, 0, 1) == "#") || (substr($scriptline, 0, 2) == "//") || ($scriptline == "")) {
    continue;
  } else {
    $parts = explode (":", $scriptline);
    $frame = $parts[0];
    $command = $parts[1];
    $events[$index]['frame'] = $frame;
    $events[$index]['command'] = $command;
    $index++;
  }
}

foreach ($events as $event) {
  echo "time: ".$event['frame']." code: ".$event['command']."\r\n";
}

$run = false;

echo "script starting<br>\r\n"; @flush();

$nextexecutionindex = 0;
while ($frame_count < $events[count($events)-1]['frame']) {
  $frame_count = elphel_get_P_value(ELPHEL_FRAME) - $startframecount;
  if ($currentframecount != $frame_count) {
    echo "Frame: ".$frame_count."\r\n"; @flush();
    if($frame_count == $events[$nextexecutionindex]['frame']) {
      echo "executing command from script: ".$events[$nextexecutionindex]['command']."\r\n";
      eval($events[$nextexecutionindex]['command']);
      $nextexecutionindex++;
    }
    $currentframecount = $frame_count;
  }
}

echo "script completed after: ".(date("U") - $starttime)." seconds<br>\r\n";@flush();
?>

The Script

# Apertus Camera Script
# Syntax:
# <Framenumber>:<PHP code to execute>
# Framenumber must be increasing with lines or the interpreter will break
25:elphel_set_P_value(ELPHEL_EXPOS, 40000);
50:elphel_set_P_value(ELPHEL_EXPOS, 50000);
75:elphel_set_P_value(ELPHEL_EXPOS, 60000);

Note that the script uses frame numbers so the actual time when the events are executed depends on the current frame-rate of the camera. If the above sample script is run with 25fps it will change exposure values after 1 second, 2 seconds and 3 seconds. The beginning (frame 0) is when the interpreter loads the script.

Usage

Simply run it by accessing: http://192.168.0.9/interpreter.php?script=scriptfilename