Commit 14632e65 authored by Andrey Filippov's avatar Andrey Filippov
Browse files

debugging exif, moved here other gps/imu related scripts

parent 09f51911
Loading
Loading
Loading
Loading
+14 −4
Original line number Diff line number Diff line

WWW_PAGES = /www/pages
#TODO: pass $(LN) from bitbake 
LN = ln
WWW_PAGES = /www/pages/
BINDIR    = /usr/bin/
VARHTML   = /var/volatile/html/
CONFDIR   = /etc/elphel393/
INSTMODE  = 0755
INSTDOCS =  0644
INSTOWNER = root
@@ -9,7 +12,8 @@ INSTGROUP = root
PROGS     = nmea2exif log_imu
SRCS =      nmea2exif.c log_imu.c
OBJS =      nmea2exif.o
PHPSCRIPTS= logger_launcher.php imu_setup.php start_gps_compass.php
PHPSCRIPTS= start_gps_compass.php
PHPWEB=     logger_launcher.php imu_setup.php

CFLAGS   += -Wall -I$(STAGING_DIR_HOST)/usr/include-uapi
LDLIBS   += -lm
@@ -27,7 +31,13 @@ install: $(PROGS)
	$(INSTALL) -d $(DESTDIR)$(BINDIR)
	$(INSTALL) -d $(DESTDIR)$(WWW_PAGES)
	$(INSTALL) -m $(INSTMODE) -o $(INSTOWNER) -g $(INSTGROUP) $(PROGS) $(DESTDIR)$(BINDIR)
	$(INSTALL) -o $(INSTOWNER) -m $(INSTDOCS) $(PHPSCRIPTS)  $(DESTDIR)$(WWW_PAGES)
	$(INSTALL) -m $(INSTMODE) -o $(INSTOWNER) -g $(INSTGROUP) $(PHPSCRIPTS) $(DESTDIR)$(BINDIR)
	$(INSTALL) -o $(INSTOWNER) -m $(INSTDOCS) $(PHPWEB)  $(DESTDIR)$(WWW_PAGES)
	$(INSTALL) $(OWN)-d $(DESTDIR)$(VARHTML)
	$(LN) -sf $(VARHTML) $(DESTDIR)$(WWW_PAGES)var
	$(INSTALL) $(OWN)-d $(DESTDIR)$(CONFDIR)
	$(LN) -sf $(CONFDIR) $(DESTDIR)$(WWW_PAGES)etc
	

clean:
	rm -rf $(PROGS) *.o *~
+40 −5
Original line number Diff line number Diff line
@@ -49,8 +49,8 @@

//#define EXIF_DEV "/dev/exif_meta"
#define PROGRAM_SERIAL 0
//#define D(x)
#define D(x) x
#define D(x)
//#define D(x) x

// TODO: use only existing sensor channels?
const char *exif_paths[SENSOR_PORTS] = {
@@ -345,7 +345,37 @@ void process_GPGGA(char*response, struct meta_GPSInfo_t *meta) {
/// Skip 14   = Diff. reference station ID#
}


void debug_meta(struct meta_GPSInfo_t *meta)
{
    unsigned char * buf = (unsigned char *) meta;
    int i;
    printf("GPSLatitudeRef         %c)\n",   meta->GPSLatitudeRef);
    printf("GPSLatitude_deg_nom    0x%x)\n",__be32_to_cpu(meta->GPSLatitude_deg_nom));
    printf("GPSLatitude_deg_denom  0x%x)\n",__be32_to_cpu(meta->GPSLatitude_deg_denom));
    printf("GPSLatitude_min_nom    0x%x)\n",__be32_to_cpu(meta->GPSLatitude_min_nom));
    printf("GPSLatitude_min_denom  0x%x)\n",__be32_to_cpu(meta->GPSLatitude_min_denom));
    printf("GPSLongitudeRef        %c)\n",   meta->GPSLongitudeRef);
    printf("GPSLongitude_deg_nom   0x%x)\n",__be32_to_cpu(meta->GPSLongitude_deg_nom));
    printf("GPSLongitude_deg_denom 0x%x)\n",__be32_to_cpu(meta->GPSLongitude_deg_denom));
    printf("GPSLongitude_min_nom   0x%x)\n",__be32_to_cpu(meta->GPSLongitude_min_nom));
    printf("GPSLongitude_min_denom 0x%x)\n",__be32_to_cpu(meta->GPSLongitude_min_denom));
    printf("GPSAltitudeRef         %c)\n",   meta->GPSAltitudeRef);
    printf("GPSAltitude_nom        0x%x)\n",__be32_to_cpu(meta->GPSAltitude_nom));
    printf("GPSAltitude_denom      0x%x)\n",__be32_to_cpu(meta->GPSAltitude_denom));
    printf("GPSTimeStamp_hrs_nom   0x%x)\n",__be32_to_cpu(meta->GPSTimeStamp_hrs_nom));
    printf("GPSTimeStamp_hrs_denom 0x%x)\n",__be32_to_cpu(meta->GPSTimeStamp_hrs_denom));
    printf("GPSTimeStamp_min_nom   0x%x)\n",__be32_to_cpu(meta->GPSTimeStamp_min_nom));
    printf("GPSTimeStamp_min_denom 0x%x)\n",__be32_to_cpu(meta->GPSTimeStamp_min_denom));
    printf("GPSTimeStamp_sec_nom   0x%x)\n",__be32_to_cpu(meta->GPSTimeStamp_sec_nom));
    printf("GPSTimeStamp_sec_denom 0x%x)\n",__be32_to_cpu(meta->GPSTimeStamp_sec_denom));
    printf("GPSDateStamp           %s)\n",   meta->GPSDateStamp);
    printf("GPSMeasureMode         %c)",     meta->GPSMeasureMode);
    for (i = 0; i < sizeof (struct meta_GPSInfo_t); i++){
        if ((i & 0xf) == 0) printf("\n%02x:",i);
        printf(" %02x",(int) buf[i]);
    }
    printf("\n");
}



@@ -373,9 +403,9 @@ int main(int argc, char *argv[]) {
           fd_exif[chn] = open(exif_paths[chn], O_RDWR);
           if (fd_exif[chn] < 0) {
               fprintf(stderr,"Can not open device file %s\n",exif_paths[chn]);
           }
           } else good_chns++;
       }
   }
   if (!good_chns){
       fprintf(stderr,"Could not open any of EXIF channel device files, aborting\n");
       exit (-1);
@@ -434,14 +464,19 @@ int main(int argc, char *argv[]) {
//               D(printf ("Got GPRMC\n"));
               D(printf ("Got NMEA string >>%s<<\n",response+1));
               process_GPRMC(response, &meta);
               D(printf ("sizeof(meta)=0x%x\n",sizeof(meta)));
               D(debug_meta(&meta));
               for (chn =0; chn < SENSOR_PORTS; chn++) if (fd_exif[chn]>=0){
                   lseek (fd_exif[chn],Exif_GPSInfo_GPSLatitudeRef,SEEK_END); /// position file pointer at the beginning of the data field for GPSLatitudeRef
                   D(printf ("filepos(%d)=0x%x\n",chn, (int)lseek (fd_exif[chn],0,SEEK_CUR)));
                   write (fd_exif[chn], &meta, sizeof(meta));
               }
             } else if (!memcmp(response+1,"GPGGA", sizeof("GPGGA")-1)) {
//               D(printf ("Got GPRMC\n"));
               D(printf ("Got NMEA string >>%s<<\n",response+1));
               process_GPGGA(response, &meta);
               D(printf ("sizeof(meta)=0x%x\n",sizeof(meta)));
               D(debug_meta(&meta));
               for (chn =0; chn < SENSOR_PORTS; chn++) if (fd_exif[chn]>=0){
                   lseek (fd_exif[chn],Exif_GPSInfo_GPSLatitudeRef,SEEK_END); /// position file pointer at the beginning of the data field for GPSLatitudeRef
                   write (fd_exif[chn], &meta, sizeof(meta));
+604 −524
Original line number Diff line number Diff line
#!/usr/bin/env php -q
#!/usr/bin/env php
<?php
/*
*! FILE NAME  : start_gps_compass.php
@@ -53,8 +53,11 @@
#require 'i2c.inc';
set_include_path ( get_include_path () . PATH_SEPARATOR . '/www/pages/include' );
require 'i2c_include.php';
    $config_name = "/etc/elphel393/imu_logger.xml";
    $gps_compass_xml = "/www/pages/gps_compass.xml";
$config_name = '/etc/elphel393/imu_logger.xml';
$gps_compass_xml = '/www/pages/gps_compass.xml';
$tmp_config = '/var/volatile/html/logger_config.xml';
$exif_php = "/www/pages/exif.php";
$bindir = '/usr/bin/';
$i2cbus = 4;
$ids = scanGrandDaughters ();
$b_index = indexGrandDaughters ( $ids );
@@ -63,7 +66,8 @@ require 'i2c_include.php';
$baud = null;
if (isset ( $b_index [103696] )) {
	$baud = $ids [$b_index [103696]] ['baud'];
       if ($baud===0) $baud=='auto';
	if ($baud === 0)
		$baud == 'auto';
}

// exit (0);
@@ -87,9 +91,11 @@ require 'i2c_include.php';
// echo "GPS: ";var_dump($GPS);echo "\n";
// if ((!$compass || !$GPS) && ((exec('date +%s')+0) <30)) {
if ((count ( $devs ) == 0) && ((exec ( 'date +%s' ) + 0) < 30)) {
          if ($verbose) echo "waiting $wait_time sec for USB devices to start up\n";
	if ($verbose)
		echo "waiting $wait_time sec for USB devices to start up\n";
	sleep ( $wait_time );
          if ($verbose) echo "Retrying detection\n";
	if ($verbose)
		echo "Retrying detection\n";
	$devs = find_gps_compass ( $baud );
}
listSensorDevices ( $devs, $b_index, $ids );
@@ -99,11 +105,13 @@ require 'i2c_include.php';
$logger_config = null;
$write_config = false;
if (file_exists ( $config_name )) {
      if ($verbose) echo 'Reading IMU logger configuration from '. $config_name."\n";
	if ($verbose)
		echo 'Reading IMU logger configuration from ' . $config_name . "\n";
	$config_xml = simplexml_load_file ( $config_name );
	$logger_config = loggerConfigFromXML ( $config_xml );
} else if (isset ( $b_index [103696] ) || isset ( $b_index [103696] )) {
      if ($verbose) echo "Generating IMU logger configuration\n";
	if ($verbose)
		echo "Generating IMU logger configuration\n";
		// $default_config=init_default_config();
		// $logger_config=init_default_config();
	$logger_config = array ();
@@ -124,12 +132,13 @@ require 'i2c_include.php';
	// var_dump($logger_config);
	// var_dump($default_config);
	$logger_xml = loggerConfigToXML ( $logger_config );
      $conf_file=fopen('/var/html/logger_config.xml',"w");
	$conf_file = fopen ( $tmp_config, "w" );
	fwrite ( $conf_file, $logger_xml->asXML () );
	fclose ( $conf_file );
	setup_IMU_logger ( $logger_config );
	if ($write_config) {
        if ($verbose) echo 'Writing IMU logger configuration to '. $config_name."\n";
		if ($verbose)
			echo 'Writing IMU logger configuration to ' . $config_name . "\n";
		$conf_file = fopen ( $config_name, "w" );
		fwrite ( $conf_file, $logger_xml->asXML () );
		fclose ( $conf_file );
@@ -141,24 +150,34 @@ require 'i2c_include.php';
// echo "GPS: ";var_dump($GPS);echo "\n";
$noGPS = ($compass || $GPS) ? "" : "noGPS";
$nocompass = ($compass) ? "" : "nocompass";
    $cmd="/usr/html/exif.php init $noGPS $nocompass";
    if ($verbose) echo "Initializing Exif template: $cmd\n";


// Just debugging:
//$nocompass = "";

$cmd = $exif_php." init $noGPS $nocompass";
if ($verbose)
	echo "Initializing Exif template: $cmd\n";
exec ( $cmd );
if ($GPS) {
      if ($verbose) echo "Starting ".$GPS["name"]. " as ". $GPS["file"]. "\n";
	if ($verbose)
		echo "Starting " . $GPS ["name"] . " as " . $GPS ["file"] . "\n";
	if (strpos ( $GPS ['name'], 'NMEA' ) !== false) {
        $cmd ="/usr/local/bin/nmea2exif ". $GPS["file"]." &";
		$cmd = $bindir."nmea2exif " . $GPS ["file"] . " &";
	} else {
        $cmd ="/usr/local/bin/garminusb2exif ". $GPS["file"]." &";
		$cmd = $bindir."garminusb2exif " . $GPS ["file"] . " &"; // BUG: Not ported!
	}
      if ($verbose) echo "exec: $cmd \n";
	if ($verbose)
		echo "exec: $cmd \n";
	popen ( $cmd, "r" );
}
if ($compass) {
      if ($verbose) echo "Starting ".$compass["name"]. " as ". $compass["file"]. "\n";
	if ($verbose)
		echo "Starting " . $compass ["name"] . " as " . $compass ["file"] . "\n";
	exec ( "stty -F " . $compass ["file"] . " -echo speed 19200" );
      $cmd="/usr/local/bin/compass ". $compass["file"]." &";
      if ($verbose) echo "exec: $cmd \n";
	$cmd = $bindir."compass " . $compass ["file"] . " &";
	if ($verbose)
		echo "exec: $cmd \n";
	popen ( $cmd, "r" );
}
exit ( (isset ( $b_index [103695] ) ? 4 : 0) | ($compass ? 2 : 0) | ($GPS ? 1 : 0) );
@@ -180,7 +199,8 @@ function scanGrandDaughters(){
}
function indexGrandDaughters($ids) {
	$index = array ();
  foreach($ids as $i=>$id) if (isset($id['model'])) {
	foreach ( $ids as $i => $id )
		if (isset ( $id ['model'] )) {
			$index [$id ['model']] = $i;
		}
	return $index;
@@ -203,12 +223,24 @@ function find_gps_compass($baud=null,$timeout=5) {
	);
	if (isset ( $baud ) && ($baud != 'auto') && ($baud != 0)) {
		$ttyOptions = array (
       '-echo speed '.$baud);
				'-echo speed ' . $baud 
		);
	}
	// global $GPS, $compass;
	$devices = array (
              "compass"=>array(array("name"=>"Ocean Server OS-5000","driver"=>"cp2101")),
              "GPS"    =>array(array("name"=>"Garmin GPS 18 USB","driver"=>"garmin_gps")));
			"compass" => array (
					array (
							"name" => "Ocean Server OS-5000",
							"driver" => "cp2101" 
					) 
			),
			"GPS" => array (
					array (
							"name" => "Garmin GPS 18 USB",
							"driver" => "garmin_gps" 
					) 
			) 
	);
	exec ( "ls /sys/bus/usb-serial/devices", $usb_ser_devs ); // ("ttyUSB0","ttyUSB1")
	$devs = new SimpleXMLElement ( "<?xml version='1.0' standalone='yes'?><USB_serial_devices/>" );
	$serialDevices = array ();
@@ -233,7 +265,8 @@ function find_gps_compass($baud=null,$timeout=5) {
				}
			}
		}
      if ($serialDevices[$dev]) continue;
		if ($serialDevices [$dev])
			continue;
		foreach ( $devices ["GPS"] as $d ) {
			if ($d ["driver"] == $driver [$dev]) {
				$dd = $devs->addChild ( 'GPS' );
@@ -246,21 +279,27 @@ function find_gps_compass($baud=null,$timeout=5) {
		}
	}
	// / Do we need to look for a NMEA GPS?
    if (!$gpsPresent) foreach ($serialDevices as $dev=>$type) if (!$type && !$gpsPresent) {
      if ($verbose) printf ("could be $dev\n");
	if (! $gpsPresent)
		foreach ( $serialDevices as $dev => $type )
			if (! $type && ! $gpsPresent) {
				if ($verbose)
					printf ( "could be $dev\n" );
				foreach ( $ttyOptions as $ttyOpt ) {
					$cmd = 'stty -F /dev/' . $dev . ' ' . $ttyOpt;
					if (count ( $ttyOptions ) == 1) { // / single baud option, will not wait for confirmation. Has to be only one serial adapter !!
          if ($verbose) echo "Setting:  $cmd\n";
						if ($verbose)
							echo "Setting:  $cmd\n";
						exec ( $cmd );
						$gpsPresent = true;
					} else {
          if ($verbose) echo "Trying:  $cmd\n";
						if ($verbose)
							echo "Trying:  $cmd\n";
						exec ( $cmd );
						unset ( $fullOutput );
						exec ( 'timeout ' . $timeout . ' cat /dev/' . $dev, $fullOutput ); // &$fullOutput - Fatal error: Call-time pass-by-reference has been removed in php
						                                                           // var_dump($fullOutput);
          foreach ($fullOutput as $line) if (strpos ($line, '$GP')===0) {
						foreach ( $fullOutput as $line )
							if (strpos ( $line, '$GP' ) === 0) {
								$gpsPresent = true;
								break;
							}
@@ -273,31 +312,39 @@ function find_gps_compass($baud=null,$timeout=5) {
						$arr = split ( " ", $ttyOpt );
						$dd->addChild ( 'baud', $arr [count ( $arr ) - 1] );
						$serialDevices [$dev] = 'gps';
          if ($verbose) echo "\nfound NMEA GPS unit\n";
						if ($verbose)
							echo "\nfound NMEA GPS unit\n";
						break;
					}
					// var_dump($fullOutput);
				}
			}
		// / list unused USB serial adapters
    foreach ($serialDevices as $dev=>$type) if (!$type) {
	foreach ( $serialDevices as $dev => $type )
		if (! $type) {
			$dd = $devs->addChild ( 'unused' );
			$dd->addChild ( 'name', 'USB serial converter' );
			$dd->addChild ( 'driver', $driver [$dev] );
			$dd->addChild ( 'file', "/dev/" . $dev );
		}
	return $devs;

}
function listSensorDevices($devs, $b_index, $ids) {
	global $GPS, $compass, $gps_compass_xml;
	
	foreach ( $devs->compass as $a ) {
      $compass=array("file"=>(string) $a->file, "name"=> (string) $a->name);
		$compass = array (
				"file" => ( string ) $a->file,
				"name" => ( string ) $a->name 
		);
		break; // use first one
	}
	foreach ( $devs->GPS as $a ) {
      $GPS=array("file"=>(string) $a->file, "name"=> (string) $a->name, "baud"=>(string) $a->baud);
		$GPS = array (
				"file" => ( string ) $a->file,
				"name" => ( string ) $a->name,
				"baud" => ( string ) $a->baud 
		);
		if (isset ( $b_index [103696] )) {
			$a->addChild ( 'part_number', $ids [$b_index [103696]] ['part'] );
			$a->addChild ( 'port', $ids [$b_index [103696]] ['port'] );
@@ -352,12 +399,16 @@ function loggerConfigFromXML($confXML){
				$conf [$key] = array ();
				foreach ( $item as $reg ) {
					$rname = substr ( $reg->getName (), 2 );
            $conf[$key][$rname+0]=array((string) $reg->sentence, (string) $reg->format);
					$conf [$key] [$rname + 0] = array (
							( string ) $reg->sentence,
							( string ) $reg->format 
					);
				}
				break;
			case 'imu_period' :
				$conf [$key] = ( string ) $item;
         if ($conf[$key]!='auto') $conf[$key]+=0;
				if ($conf [$key] != 'auto')
					$conf [$key] += 0;
				break;
			case 'message' :
				$conf [$key] = ( string ) $item;
@@ -369,9 +420,6 @@ function loggerConfigFromXML($confXML){
	}
	return $conf;
}



function init_default_config() {
	$default_config = array (
			'imu_period' => 'auto', // 0xFFFFFFFF, // 0 - off, >=0xff000000 - "when ready", else - number of SCLK periods
@@ -419,22 +467,36 @@ function init_default_config(){
					0x0e, // temperature
					0x70, // time m/s
					0x72, // time d/h
    0x74// time y/m
  ),
					0x74 
			) // time y/m
,
			'nmea' => array (
					// / first three letters - sentence to log (letters after "$GP"). next "n"/"b" (up to 24 total) - "n" number (will be encoded 4 digits/byte, follwed by "0xF"
					// / "b" - byte - all but last will have MSB 0 (& 0x7f), the last one - with MSB set (| 0x80). If there are no characters in the field 0xff will be output
    array('RMC','nbnbnbnnnnb'),
    array('GGA','nnbnbnnnnbnbbb'),
    array('GSA','bnnnnnnnnnnnnnnnn'),
    array('VTG','nbnbnbnb')
					array (
							'RMC',
							'nbnbnbnnnnb' 
					),
  'message'=>'Odometer message' // Message - up to 56 bytes
  );
					array (
							'GGA',
							'nnbnbnnnnbnbbb' 
					),
					array (
							'GSA',
							'bnnnnnnnnnnnnnnnn' 
					),
					array (
							'VTG',
							'nbnbnbnb' 
					) 
			),
			'message' => 'Odometer message' 
	) // Message - up to 56 bytes
;
	return $default_config;
}
/**
Combines $conf and $dflt arrays - all driver parameters are overwritten aftre the call
 * Combines $conf and $dflt arrays - all driver parameters are overwritten aftre the call
 */
function combineLoggerConfigs($new_conf, $dflt = null) {
	global $default_config;
@@ -446,15 +508,20 @@ function combineLoggerConfigs($new_conf,$dflt=null){
		$dflt = $default_config;
	}
	$conf = $dflt;
  if (isset($new_conf)) foreach ($conf as $key=>$oldvalue) if (isset($new_conf[$key])) $conf[$key]=$new_conf[$key];
	if (isset ( $new_conf ))
		foreach ( $conf as $key => $oldvalue )
			if (isset ( $new_conf [$key] ))
				$conf [$key] = $new_conf [$key];
		// / fix insufficient data from default
	
  while (count($conf['imu_registers'])<28) $conf['imu_registers'][count($conf['imu_registers'])]=0; // zero pad if less than 28 registers
	while ( count ( $conf ['imu_registers'] ) < 28 )
		$conf ['imu_registers'] [count ( $conf ['imu_registers'] )] = 0; // zero pad if less than 28 registers
			                                                                                                  // / Configure NMEA sentences to log. If there are less than 4 - copy additional (unique) from defaults
	while ( count ( $conf ['nmea'] ) < 4 ) {
		for($i = 0; $i < 4; $i ++) {
			$new = true;
      for ($j=0;$j<count($conf['nmea']);$j++) if ($conf['nmea'][$j][0]==$dflt['nmea'][$i][0]){ // compare sentence code last 3 letters
			for($j = 0; $j < count ( $conf ['nmea'] ); $j ++)
				if ($conf ['nmea'] [$j] [0] == $dflt ['nmea'] [$i] [0]) { // compare sentence code last 3 letters
					$new = false;
					break;
				}
@@ -465,7 +532,8 @@ function combineLoggerConfigs($new_conf,$dflt=null){
		}
	}
	for($i = 0; $i < 4; $i ++) {
    while (strlen($conf['nmea'][$i][0])<3) $conf['nmea'][$i][0].="A"; // just to avoid errors - nonexistent sentences will not hurt, just never logged
		while ( strlen ( $conf ['nmea'] [$i] [0] ) < 3 )
			$conf ['nmea'] [$i] [0] .= "A"; // just to avoid errors - nonexistent sentences will not hurt, just never logged
	}
	
	return $conf;
@@ -477,20 +545,26 @@ function setup_IMU_logger($conf){
	$data = array ();
	$index = 0;
	// / program period
  if (strtolower($conf['imu_period'])=='auto') $conf['imu_period']=0xffffffff;
  else $conf['imu_period']+=0;
	if (strtolower ( $conf ['imu_period'] ) == 'auto')
		$conf ['imu_period'] = 0xffffffff;
	else
		$conf ['imu_period'] += 0;
	$data [$index ++] = $conf ['imu_period'] & 0xff;
	$data [$index ++] = ($conf ['imu_period'] >> 8) & 0xff;
	$data [$index ++] = ($conf ['imu_period'] >> 16) & 0xff;
	$data [$index ++] = ($conf ['imu_period'] >> 24) & 0xff;
	// / program SCLK divisor and SPI stall time
	$sclk_div = round ( $xclk_freq / $conf ['sclk_freq'] / 2 );
  if      ($sclk_div<1)   $sclk_div=1;
  else if ($sclk_div>255) $sclk_div=255;
	if ($sclk_div < 1)
		$sclk_div = 1;
	else if ($sclk_div > 255)
		$sclk_div = 255;
	$conf ['sclk_freq'] = $xclk_freq / $sclk_div / 2;
	$stall = (($conf ['stall'] * ($xclk_freq / $sclk_div)) / 1000000);
  if      ($stall<  1) $stall=  1; /// does 0 work? need to verify
  else if ($stall>255) $stall=255;
	if ($stall < 1)
		$stall = 1; // / does 0 work? need to verify
	else if ($stall > 255)
		$stall = 255;
	$conf ['stall'] = $stall * 1000000 / ($xclk_freq / $sclk_div);
	$data [$index ++] = $sclk_div;
	$data [$index ++] = $stall;
@@ -547,36 +621,41 @@ function setup_IMU_logger($conf){
	$data [$index ++] = ($conf ['sleep_busy'] >> 24) & 0xff;
	// / Set IMU register addresses to logger
	// echo "1\n";
  for ($i=0;$i<28;$i++) $data[$index++]= $conf['imu_registers'][$i]; // truncate to 28
	for($i = 0; $i < 28; $i ++)
		$data [$index ++] = $conf ['imu_registers'] [$i]; // truncate to 28
			                                                                   // / Configure NMEA sentences to log. If there are less than 4 - copy additional (unique) from defaults
			                                                                   // echo "2\n";
	for($i = 0; $i < 4; $i ++) {
		$d = str_split ( $conf ['nmea'] [$i] [0] );
		// print_r($d);
    for ($j=0;$j<3;$j++) $data[$index++]=ord($d[$j]);
		for($j = 0; $j < 3; $j ++)
			$data [$index ++] = ord ( $d [$j] );
		$d = str_split ( $conf ['nmea'] [$i] [1] );
		// print_r($d);
    for ($j=0;$j<29;$j++) $data[$index++]=($j<count($d))?ord($d[$j]):0;
		for($j = 0; $j < 29; $j ++)
			$data [$index ++] = ($j < count ( $d )) ? ord ( $d [$j] ) : 0;
	}
	// echo "3\n";
	
	// / Set default message
	$d = str_split ( $conf ['message'] );
  for ($j=0;$j<56;$j++) $data[$index++]=($j<count($d))?ord($d[$j]):0;
	for($j = 0; $j < 56; $j ++)
		$data [$index ++] = ($j < count ( $d )) ? ord ( $d [$j] ) : 0;
		
		// Print the result array as hex data
		/*
  print_r($conf);

  echo "\n";
  for ($i=0;$i<count($data);$i++) {
    if (($i & 0xf)==0) printf ("\n%04x:",$i);
    printf (" %02x",$data[$i]);
  }
  echo "\n";
	 * print_r($conf);
	 *
	 * echo "\n";
	 * for ($i=0;$i<count($data);$i++) {
	 * if (($i & 0xf)==0) printf ("\n%04x:",$i);
	 * printf (" %02x",$data[$i]);
	 * }
	 * echo "\n";
	 */
	$bindata = "";
  for($i=0;$i<count($data);$i++) $bindata.=chr($data[$i]);
	for($i = 0; $i < count ( $data ); $i ++)
		$bindata .= chr ( $data [$i] );
	$dev_imu_ctl = fopen ( '/dev/imu_ctl', 'w' );
	fwrite ( $dev_imu_ctl, $bindata, strlen ( $bindata ) );
	fseek ( $dev_imu_ctl, 3, SEEK_END ); // start IMU
@@ -587,7 +666,8 @@ function setup_IMU_logger($conf){
	if ($verbose) {
		echo "\n";
		for($i = 0; $i < count ( $data ); $i ++) {
      if (($i & 0xf)==0) printf ("\n%04x:",$i);
			if (($i & 0xf) == 0)
				printf ( "\n%04x:", $i );
			printf ( " %02x", ord ( $data [$i] ) );
		}
		echo "\n";