Commit ed26a679 authored by Andrey Filippov's avatar Andrey Filippov
Browse files

working on event logger

parent 14632e65
Loading
Loading
Loading
Loading
+866 −759
Original line number Original line Diff line number Diff line
@@ -47,7 +47,7 @@
set_include_path ( get_include_path () . PATH_SEPARATOR . '/www/pages/include' );
set_include_path ( get_include_path () . PATH_SEPARATOR . '/www/pages/include' );
require 'i2c_include.php';
require 'i2c_include.php';


#  $xclk_freq=80000000; //  80 MHz in NC353
// $xclk_freq=80000000; // 80 MHz in NC353
$xclk_freq = 100000000; // 100 MHz in NC393
$xclk_freq = 100000000; // 100 MHz in NC393
$messageOffset = 0xb0; // offset to start of "odometer" message in the device driver file
$messageOffset = 0xb0; // offset to start of "odometer" message in the device driver file
$config_name = "/etc/elphel393/imu_logger.xml";
$config_name = "/etc/elphel393/imu_logger.xml";
@@ -80,34 +80,49 @@ require 'i2c_include.php';
	
	
	foreach ( $_GET as $key => $value ) {
	foreach ( $_GET as $key => $value ) {
		$access = split ( ':', $key );
		$access = split ( ':', $key );
      if ($value==='') continue;
		if ($value === '')
      if (count($access)<1) continue;
			continue;
      if (!isset($conf[$access[0]])) continue;
		if (count ( $access ) < 1)
			continue;
		if (! isset ( $conf [$access [0]] ))
			continue;
		if (count ( $access ) == 1) {
		if (count ( $access ) == 1) {
        if (($access[0]=='imu_period') && ($value=='auto')) $value = 0xffffff00;
			if (($access [0] == 'imu_period') && ($value == 'auto'))
        if (is_string($conf[$access[0]])) $conf[$access[0]]=$value;
				$value = 0xffffff00;
        else  $conf[$access[0]]=$value+0;
			if (is_string ( $conf [$access [0]] ))
				$conf [$access [0]] = $value;
			else
				$conf [$access [0]] = $value + 0;
			$apply ++;
			$apply ++;
		} else { // count($access)>1
		} else { // count($access)>1
        if (!isset($conf[$access[0]][$access[1]])) continue;
			if (! isset ( $conf [$access [0]] [$access [1]] ))
				continue;
			if (count ( $access ) == 2) {
			if (count ( $access ) == 2) {
          if (is_string($conf[$access[0]][$access[1]])) $conf[$access[0]][$access[1]]=$value;
				if (is_string ( $conf [$access [0]] [$access [1]] ))
          else  $conf[$access[0]][$access[1]]=$value+0;
					$conf [$access [0]] [$access [1]] = $value;
				else
					$conf [$access [0]] [$access [1]] = $value + 0;
				$apply ++;
				$apply ++;
			} else { // count($access)>2)
			} else { // count($access)>2)
          if (count($access)>3) continue;
				if (count ( $access ) > 3)
          if      ($access[2]=='sentence') $access[2]=0;
					continue;
          else if ($access[2]=='format')   $access[2]=1;
				if ($access [2] == 'sentence')
          if (!isset($conf[$access[0]][$access[1]][$access[2]])) continue;
					$access [2] = 0;
          if (is_string($conf[$access[0]][$access[1]][$access[2]])) $conf[$access[0]][$access[1]][$access[2]]=$value;
				else if ($access [2] == 'format')
          else  $conf[$access[0]][$access[1]][$access[2]]=$value+0;
					$access [2] = 1;
				if (! isset ( $conf [$access [0]] [$access [1]] [$access [2]] ))
					continue;
				if (is_string ( $conf [$access [0]] [$access [1]] [$access [2]] ))
					$conf [$access [0]] [$access [1]] [$access [2]] = $value;
				else
					$conf [$access [0]] [$access [1]] [$access [2]] = $value + 0;
				$apply ++;
				$apply ++;
			}
			}
		}
		}
		/*
		/*
echo 'key='.$key."\n";
		 * echo 'key='.$key."\n";
print_r($access);
		 * print_r($access);
var_dump($value);
		 * var_dump($value);
		 */
		 */
	}
	}
	if ($apply > 0) {
	if ($apply > 0) {
@@ -125,7 +140,6 @@ var_dump($value);
		exec ( 'sync' );
		exec ( 'sync' );
	}
	}
	
	

	if (isset ( $_GET ['list'] )) {
	if (isset ( $_GET ['list'] )) {
		$conf = readSettings ( init_default_config () );
		$conf = readSettings ( init_default_config () );
		$logger_xml = loggerConfigToXML ( $conf );
		$logger_xml = loggerConfigToXML ( $conf );
@@ -134,20 +148,23 @@ var_dump($value);
		header ( "Content-Length: " . strlen ( $logger_xml_text ) . "\n" );
		header ( "Content-Length: " . strlen ( $logger_xml_text ) . "\n" );
		header ( "Pragma: no-cache\n" );
		header ( "Pragma: no-cache\n" );
		printf ( $logger_xml_text );
		printf ( $logger_xml_text );
    } else echo 'OK';
	} else
		echo 'OK';
	exit ( 0 );
	exit ( 0 );
} else {
} else {
	// echo "CGI\n";
	// echo "CGI\n";
}
}
/*
/*
    echo "<!--\n";
 * echo "<!--\n";
    var_dump($_GET);
 * var_dump($_GET);
    var_dump($_SERVER);
 * var_dump($_SERVER);
    echo "-->";
 * echo "-->";
    echo 'OK';
 * echo 'OK';
    exit(0);
 * exit(0);
 */
 */
// ! Here comes if CLI
// ! Here comes if CLI
/** TODO: Not yet updated for NC393 - use start_gps_compass.php */

$ids = scanGrandDaughters ();
$ids = scanGrandDaughters ();
$b_index = indexGrandDaughters ( $ids );
$b_index = indexGrandDaughters ( $ids );
// print_r($ids);
// print_r($ids);
@@ -155,7 +172,8 @@ var_dump($value);
$baud = null;
$baud = null;
if (isset ( $b_index [103696] )) {
if (isset ( $b_index [103696] )) {
	$baud = $ids [$b_index [103696]] ['baud'];
	$baud = $ids [$b_index [103696]] ['baud'];
       if ($baud===0) $baud=='auto';
	if ($baud === 0)
		$baud == 'auto';
}
}


// exit (0);
// exit (0);
@@ -179,9 +197,11 @@ var_dump($value);
// echo "GPS: ";var_dump($GPS);echo "\n";
// echo "GPS: ";var_dump($GPS);echo "\n";
// if ((!$compass || !$GPS) && ((exec('date +%s')+0) <30)) {
// if ((!$compass || !$GPS) && ((exec('date +%s')+0) <30)) {
if ((count ( $devs ) == 0) && ((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 );
	sleep ( $wait_time );
          if ($verbose) echo "Retrying detection\n";
	if ($verbose)
		echo "Retrying detection\n";
	$devs = find_gps_compass ( $baud );
	$devs = find_gps_compass ( $baud );
}
}
listSensorDevices ( $devs, $b_index, $ids );
listSensorDevices ( $devs, $b_index, $ids );
@@ -191,11 +211,13 @@ var_dump($value);
$logger_config = null;
$logger_config = null;
$write_config = false;
$write_config = false;
if (file_exists ( $config_name )) {
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 );
	$config_xml = simplexml_load_file ( $config_name );
	$logger_config = loggerConfigFromXML ( $config_xml );
	$logger_config = loggerConfigFromXML ( $config_xml );
} else if (isset ( $b_index [103696] ) || isset ( $b_index [103696] )) {
} 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();
		// $default_config=init_default_config();
		// $logger_config=init_default_config();
		// $logger_config=init_default_config();
	$logger_config = array ();
	$logger_config = array ();
@@ -221,7 +243,8 @@ var_dump($value);
	fclose ( $conf_file );
	fclose ( $conf_file );
	setup_IMU_logger ( $logger_config );
	setup_IMU_logger ( $logger_config );
	if ($write_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" );
		$conf_file = fopen ( $config_name, "w" );
		fwrite ( $conf_file, $logger_xml->asXML () );
		fwrite ( $conf_file, $logger_xml->asXML () );
		fclose ( $conf_file );
		fclose ( $conf_file );
@@ -234,23 +257,28 @@ var_dump($value);
$noGPS = ($compass || $GPS) ? "" : "noGPS";
$noGPS = ($compass || $GPS) ? "" : "noGPS";
$nocompass = ($compass) ? "" : "nocompass";
$nocompass = ($compass) ? "" : "nocompass";
$cmd = "/usr/html/exif.php init $noGPS $nocompass";
$cmd = "/usr/html/exif.php init $noGPS $nocompass";
    if ($verbose) echo "Initializing Exif template: $cmd\n";
if ($verbose)
	echo "Initializing Exif template: $cmd\n";
exec ( $cmd );
exec ( $cmd );
if ($GPS) {
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) {
	if (strpos ( $GPS ['name'], 'NMEA' ) !== false) {
		$cmd = "/usr/local/bin/nmea2exif " . $GPS ["file"] . " &";
		$cmd = "/usr/local/bin/nmea2exif " . $GPS ["file"] . " &";
	} else {
	} else {
		$cmd = "/usr/local/bin/garminusb2exif " . $GPS ["file"] . " &";
		$cmd = "/usr/local/bin/garminusb2exif " . $GPS ["file"] . " &";
	}
	}
      if ($verbose) echo "exec: $cmd \n";
	if ($verbose)
		echo "exec: $cmd \n";
	popen ( $cmd, "r" );
	popen ( $cmd, "r" );
}
}
if ($compass) {
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" );
	exec ( "stty -F " . $compass ["file"] . " -echo speed 19200" );
	$cmd = "/usr/local/bin/compass " . $compass ["file"] . " &";
	$cmd = "/usr/local/bin/compass " . $compass ["file"] . " &";
      if ($verbose) echo "exec: $cmd \n";
	if ($verbose)
		echo "exec: $cmd \n";
	popen ( $cmd, "r" );
	popen ( $cmd, "r" );
}
}
exit ( (isset ( $b_index [103695] ) ? 4 : 0) | ($compass ? 2 : 0) | ($GPS ? 1 : 0) );
exit ( (isset ( $b_index [103695] ) ? 4 : 0) | ($compass ? 2 : 0) | ($GPS ? 1 : 0) );
@@ -363,7 +391,6 @@ function help(){
</p>
</p>
</body>
</body>
USAGE;
USAGE;

}
}
function scanGrandDaughters() {
function scanGrandDaughters() {
	global $i2cbus;
	global $i2cbus;
@@ -404,12 +431,24 @@ function find_gps_compass($baud=null,$timeout=5) {
	);
	);
	if (isset ( $baud ) && ($baud != 'auto') && ($baud != 0)) {
	if (isset ( $baud ) && ($baud != 'auto') && ($baud != 0)) {
		$ttyOptions = array (
		$ttyOptions = array (
       '-echo speed '.$baud);
				'-echo speed ' . $baud 
		);
	}
	}
	// global $GPS, $compass;
	// global $GPS, $compass;
	$devices = array (
	$devices = array (
              "compass"=>array(array("name"=>"Ocean Server OS-5000","driver"=>"cp2101")),
			"compass" => array (
              "GPS"    =>array(array("name"=>"Garmin GPS 18 USB","driver"=>"garmin_gps")));
					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")
	exec ( "ls /sys/bus/usb-serial/devices", $usb_ser_devs ); // ("ttyUSB0","ttyUSB1")
	$devs = new SimpleXMLElement ( "<?xml version='1.0' standalone='yes'?><USB_serial_devices/>" );
	$devs = new SimpleXMLElement ( "<?xml version='1.0' standalone='yes'?><USB_serial_devices/>" );
	$serialDevices = array ();
	$serialDevices = array ();
@@ -434,7 +473,8 @@ function find_gps_compass($baud=null,$timeout=5) {
				}
				}
			}
			}
		}
		}
      if ($serialDevices[$dev]) continue;
		if ($serialDevices [$dev])
			continue;
		foreach ( $devices ["GPS"] as $d ) {
		foreach ( $devices ["GPS"] as $d ) {
			if ($d ["driver"] == $driver [$dev]) {
			if ($d ["driver"] == $driver [$dev]) {
				$dd = $devs->addChild ( 'GPS' );
				$dd = $devs->addChild ( 'GPS' );
@@ -447,21 +487,27 @@ function find_gps_compass($baud=null,$timeout=5) {
		}
		}
	}
	}
	// / Do we need to look for a NMEA GPS?
	// / Do we need to look for a NMEA GPS?
    if (!$gpsPresent) foreach ($serialDevices as $dev=>$type) if (!$type && !$gpsPresent) {
	if (! $gpsPresent)
      if ($verbose) printf ("could be $dev\n");
		foreach ( $serialDevices as $dev => $type )
			if (! $type && ! $gpsPresent) {
				if ($verbose)
					printf ( "could be $dev\n" );
				foreach ( $ttyOptions as $ttyOpt ) {
				foreach ( $ttyOptions as $ttyOpt ) {
					$cmd = 'stty -F /dev/' . $dev . ' ' . $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 (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 );
						exec ( $cmd );
						$gpsPresent = true;
						$gpsPresent = true;
					} else {
					} else {
          if ($verbose) echo "Trying:  $cmd\n";
						if ($verbose)
							echo "Trying:  $cmd\n";
						exec ( $cmd );
						exec ( $cmd );
						unset ( $fullOutput );
						unset ( $fullOutput );
						exec ( 'timeout ' . $timeout . ' cat /dev/' . $dev, $fullOutput ); // &$fullOutput - Fatal error: Call-time pass-by-reference has been removed in php
						exec ( 'timeout ' . $timeout . ' cat /dev/' . $dev, $fullOutput ); // &$fullOutput - Fatal error: Call-time pass-by-reference has been removed in php
						                                                           // var_dump($fullOutput);
						                                                           // var_dump($fullOutput);
          foreach ($fullOutput as $line) if (strpos ($line, '$GP')===0) {
						foreach ( $fullOutput as $line )
							if (strpos ( $line, '$GP' ) === 0) {
								$gpsPresent = true;
								$gpsPresent = true;
								break;
								break;
							}
							}
@@ -474,31 +520,39 @@ function find_gps_compass($baud=null,$timeout=5) {
						$arr = split ( " ", $ttyOpt );
						$arr = split ( " ", $ttyOpt );
						$dd->addChild ( 'baud', $arr [count ( $arr ) - 1] );
						$dd->addChild ( 'baud', $arr [count ( $arr ) - 1] );
						$serialDevices [$dev] = 'gps';
						$serialDevices [$dev] = 'gps';
          if ($verbose) echo "\nfound NMEA GPS unit\n";
						if ($verbose)
							echo "\nfound NMEA GPS unit\n";
						break;
						break;
					}
					}
					// var_dump($fullOutput);
					// var_dump($fullOutput);
				}
				}
			}
			}
		// / list unused USB serial adapters
		// / list unused USB serial adapters
    foreach ($serialDevices as $dev=>$type) if (!$type) {
	foreach ( $serialDevices as $dev => $type )
		if (! $type) {
			$dd = $devs->addChild ( 'unused' );
			$dd = $devs->addChild ( 'unused' );
			$dd->addChild ( 'name', 'USB serial converter' );
			$dd->addChild ( 'name', 'USB serial converter' );
			$dd->addChild ( 'driver', $driver [$dev] );
			$dd->addChild ( 'driver', $driver [$dev] );
			$dd->addChild ( 'file', "/dev/" . $dev );
			$dd->addChild ( 'file', "/dev/" . $dev );
		}
		}
	return $devs;
	return $devs;

}
}
function listSensorDevices($devs, $b_index, $ids) {
function listSensorDevices($devs, $b_index, $ids) {
	global $GPS, $compass, $gps_compass_xml;
	global $GPS, $compass, $gps_compass_xml;
	
	
	foreach ( $devs->compass as $a ) {
	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
		break; // use first one
	}
	}
	foreach ( $devs->GPS as $a ) {
	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] )) {
		if (isset ( $b_index [103696] )) {
			$a->addChild ( 'part_number', $ids [$b_index [103696]] ['part'] );
			$a->addChild ( 'part_number', $ids [$b_index [103696]] ['part'] );
			$a->addChild ( 'port', $ids [$b_index [103696]] ['port'] );
			$a->addChild ( 'port', $ids [$b_index [103696]] ['port'] );
@@ -553,12 +607,16 @@ function loggerConfigFromXML($confXML){
				$conf [$key] = array ();
				$conf [$key] = array ();
				foreach ( $item as $reg ) {
				foreach ( $item as $reg ) {
					$rname = substr ( $reg->getName (), 2 );
					$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;
				break;
			case 'imu_period' :
			case 'imu_period' :
				$conf [$key] = ( string ) $item;
				$conf [$key] = ( string ) $item;
         if ($conf[$key]!='auto') $conf[$key]+=0;
				if ($conf [$key] != 'auto')
					$conf [$key] += 0;
				break;
				break;
			case 'message' :
			case 'message' :
				$conf [$key] = ( string ) $item;
				$conf [$key] = ( string ) $item;
@@ -570,9 +628,6 @@ function loggerConfigFromXML($confXML){
	}
	}
	return $conf;
	return $conf;
}
}



function init_default_config() {
function init_default_config() {
	$default_config = array (
	$default_config = array (
			'imu_period' => 'auto', // 0xFFFFFFFF, // 0 - off, >=0xff000000 - "when ready", else - number of SCLK periods
			'imu_period' => 'auto', // 0xFFFFFFFF, // 0 - off, >=0xff000000 - "when ready", else - number of SCLK periods
@@ -620,26 +675,40 @@ function init_default_config(){
					0x0e, // temperature
					0x0e, // temperature
					0x70, // time m/s
					0x70, // time m/s
					0x72, // time d/h
					0x72, // time d/h
    0x74// time y/m
					0x74 
  ),
			) // time y/m
,
			'nmea' => array (
			'nmea' => array (
					// / first three letters - sentence to log (letters after "$GP"). next "n"/"b" (up to 24 total) - "n" number (will be encoded 2 digits/byte, follwed by "0xF"
					// / first three letters - sentence to log (letters after "$GP"). next "n"/"b" (up to 24 total) - "n" number (will be encoded 2 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
					// / "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 (
    array('GGA','nnbnbnnnnbnbbb'),
							'RMC',
    array('GSA','bnnnnnnnnnnnnnnnn'),
							'nbnbnbnnnnb' 
    array('VTG','nbnbnbnb')
					),
					),
  '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;
	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) {
function combineLoggerConfigs($new_conf, $dflt = null) {
	global $default_config;
	global $default_config;
  #  $xclk_freq=80000000; //  80 MHz in NC353
	// $xclk_freq=80000000; // 80 MHz in NC353
	$xclk_freq = 100000000; // 100 MHz in NC393
	$xclk_freq = 100000000; // 100 MHz in NC393
	$data = array ();
	$data = array ();
	$index = 0;
	$index = 0;
@@ -647,15 +716,20 @@ function combineLoggerConfigs($new_conf,$dflt=null){
		$dflt = $default_config;
		$dflt = $default_config;
	}
	}
	$conf = $dflt;
	$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
		// / 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
			                                                                                                  // / Configure NMEA sentences to log. If there are less than 4 - copy additional (unique) from defaults
	while ( count ( $conf ['nmea'] ) < 4 ) {
	while ( count ( $conf ['nmea'] ) < 4 ) {
		for($i = 0; $i < 4; $i ++) {
		for($i = 0; $i < 4; $i ++) {
			$new = true;
			$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;
					$new = false;
					break;
					break;
				}
				}
@@ -666,7 +740,8 @@ function combineLoggerConfigs($new_conf,$dflt=null){
		}
		}
	}
	}
	for($i = 0; $i < 4; $i ++) {
	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;
	return $conf;
@@ -676,20 +751,26 @@ function setup_IMU_logger($conf){
	$data = array ();
	$data = array ();
	$index = 0;
	$index = 0;
	// / program period
	// / program period
  if (strtolower($conf['imu_period'])=='auto') $conf['imu_period']=0xffffffff;
	if (strtolower ( $conf ['imu_period'] ) == 'auto')
  else $conf['imu_period']+=0;
		$conf ['imu_period'] = 0xffffffff;
	else
		$conf ['imu_period'] += 0;
	$data [$index ++] = $conf ['imu_period'] & 0xff;
	$data [$index ++] = $conf ['imu_period'] & 0xff;
	$data [$index ++] = ($conf ['imu_period'] >> 8) & 0xff;
	$data [$index ++] = ($conf ['imu_period'] >> 8) & 0xff;
	$data [$index ++] = ($conf ['imu_period'] >> 16) & 0xff;
	$data [$index ++] = ($conf ['imu_period'] >> 16) & 0xff;
	$data [$index ++] = ($conf ['imu_period'] >> 24) & 0xff;
	$data [$index ++] = ($conf ['imu_period'] >> 24) & 0xff;
	// / program SCLK divisor and SPI stall time
	// / program SCLK divisor and SPI stall time
	$sclk_div = round ( $xclk_freq / $conf ['sclk_freq'] / 2 );
	$sclk_div = round ( $xclk_freq / $conf ['sclk_freq'] / 2 );
  if      ($sclk_div<1)   $sclk_div=1;
	if ($sclk_div < 1)
  else if ($sclk_div>255) $sclk_div=255;
		$sclk_div = 1;
	else if ($sclk_div > 255)
		$sclk_div = 255;
	$conf ['sclk_freq'] = $xclk_freq / $sclk_div / 2;
	$conf ['sclk_freq'] = $xclk_freq / $sclk_div / 2;
	$stall = (($conf ['stall'] * ($xclk_freq / $sclk_div)) / 1000000);
	$stall = (($conf ['stall'] * ($xclk_freq / $sclk_div)) / 1000000);
  if      ($stall<  1) $stall=  1; /// does 0 work? need to verify
	if ($stall < 1)
  else if ($stall>255) $stall=255;
		$stall = 1; // / does 0 work? need to verify
	else if ($stall > 255)
		$stall = 255;
	$conf ['stall'] = $stall * 1000000 / ($xclk_freq / $sclk_div);
	$conf ['stall'] = $stall * 1000000 / ($xclk_freq / $sclk_div);
	$data [$index ++] = $sclk_div;
	$data [$index ++] = $sclk_div;
	$data [$index ++] = $stall;
	$data [$index ++] = $stall;
@@ -746,36 +827,41 @@ function setup_IMU_logger($conf){
	$data [$index ++] = ($conf ['sleep_busy'] >> 24) & 0xff;
	$data [$index ++] = ($conf ['sleep_busy'] >> 24) & 0xff;
	// / Set IMU register addresses to logger
	// / Set IMU register addresses to logger
	// echo "1\n";
	// 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
			                                                                   // / Configure NMEA sentences to log. If there are less than 4 - copy additional (unique) from defaults
			                                                                   // echo "2\n";
			                                                                   // echo "2\n";
	for($i = 0; $i < 4; $i ++) {
	for($i = 0; $i < 4; $i ++) {
		$d = str_split ( $conf ['nmea'] [$i] [0] );
		$d = str_split ( $conf ['nmea'] [$i] [0] );
		// print_r($d);
		// 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] );
		$d = str_split ( $conf ['nmea'] [$i] [1] );
		// print_r($d);
		// 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";
	// echo "3\n";
	
	
	// / Set default message
	// / Set default message
	$d = str_split ( $conf ['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 the result array as hex data
		/*
		/*
  print_r($conf);
	 * print_r($conf);

	 *
  echo "\n";
	 * echo "\n";
  for ($i=0;$i<count($data);$i++) {
	 * 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",$data[$i]);
	 * printf (" %02x",$data[$i]);
  }
	 * }
  echo "\n";
	 * echo "\n";
	 */
	 */
	$bindata = "";
	$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' );
	$dev_imu_ctl = fopen ( '/dev/imu_ctl', 'w' );
	fwrite ( $dev_imu_ctl, $bindata, strlen ( $bindata ) );
	fwrite ( $dev_imu_ctl, $bindata, strlen ( $bindata ) );
	fseek ( $dev_imu_ctl, 3, SEEK_END ); // start IMU
	fseek ( $dev_imu_ctl, 3, SEEK_END ); // start IMU
@@ -786,7 +872,8 @@ function setup_IMU_logger($conf){
	if ($verbose) {
	if ($verbose) {
		echo "\n";
		echo "\n";
		for($i = 0; $i < count ( $data ); $i ++) {
		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] ) );
			printf ( " %02x", ord ( $data [$i] ) );
		}
		}
		echo "\n";
		echo "\n";
@@ -797,9 +884,11 @@ function setMessage($msg) {
	$data = array ();
	$data = array ();
	$d = str_split ( $msg );
	$d = str_split ( $msg );
	$index = 0;
	$index = 0;
  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;
	$bindata = "";
	$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' );
	$dev_imu_ctl = fopen ( '/dev/imu_ctl', 'w' );
	fseek ( $dev_imu_ctl, $messageOffset, SEEK_SET ); // start IMU
	fseek ( $dev_imu_ctl, $messageOffset, SEEK_SET ); // start IMU
	echo ftell ( $dev_imu_ctl ) . "<br/>\n";
	echo ftell ( $dev_imu_ctl ) . "<br/>\n";
@@ -807,27 +896,25 @@ function setMessage($msg) {
	// fseek($dev_imu_ctl,3,SEEK_END); // start IMU
	// fseek($dev_imu_ctl,3,SEEK_END); // start IMU
	fclose ( $dev_imu_ctl );
	fclose ( $dev_imu_ctl );
	var_dump ( $bindata );
	var_dump ( $bindata );

}
}

function readSettings($conf = null) {
function readSettings($conf = null) {
	global $xclk_freq; // =80000000; // 80 MHz
	global $xclk_freq; // =80000000; // 80 MHz
	$bd = file_get_contents ( '/dev/imu_ctl' );
	$bd = file_get_contents ( '/dev/imu_ctl' );
	$data = str_split ( $bd );
	$data = str_split ( $bd );
  for ($i=0;$i<count($data);$i++) $data[$i]=ord($data[$i]);
	for($i = 0; $i < count ( $data ); $i ++)
		$data [$i] = ord ( $data [$i] );
		/*
		/*
//  if ($verbose) { 
	 * // if ($verbose) {
    echo "\n";
	 * echo "\n";
    for ($i=0;$i<count($data);$i++) {
	 * 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",$data[$i]);
	 * printf (" %02x",$data[$i]);
    }
	 * }
    echo "\n";
	 * echo "\n";
//  }
	 * // }
	 */
	 */
	if ($conf != null) {
	if ($conf != null) {
		
		

		// / read current period
		// / read current period
		$conf ['imu_period'] = $data [$index ++];
		$conf ['imu_period'] = $data [$index ++];
		$conf ['imu_period'] |= $data [$index ++] << 8;
		$conf ['imu_period'] |= $data [$index ++] << 8;
@@ -902,24 +989,43 @@ function readSettings($conf=null) {
		$conf ['sleep_busy'] |= $data [$index ++] << 16;
		$conf ['sleep_busy'] |= $data [$index ++] << 16;
		$conf ['sleep_busy'] |= $data [$index ++] << 24;
		$conf ['sleep_busy'] |= $data [$index ++] << 24;
		
		

		// / read current IMU register addresses to logger
		// / read current IMU register addresses to logger
		// echo "1\n";
		// 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
  for ($i=0;$i<28;$i++) $conf['imu_registers'][$i]=$data[$index++];
		for($i = 0; $i < 28; $i ++)
			$conf ['imu_registers'] [$i] = $data [$index ++];
			
			
			// / read current NMEA sentences to log. If there are less than 4 - copy additional (unique) from defaults
			// / read current NMEA sentences to log. If there are less than 4 - copy additional (unique) from defaults
			// echo "2\n";
			// echo "2\n";
  $conf['nmea']=array(array("",""),array("",""),array("",""),array("",""));
		$conf ['nmea'] = array (
				array (
						"",
						"" 
				),
				array (
						"",
						"" 
				),
				array (
						"",
						"" 
				),
				array (
						"",
						"" 
				) 
		);
		for($i = 0; $i < count ( $conf ['nmea'] ); $i ++) {
		for($i = 0; $i < count ( $conf ['nmea'] ); $i ++) {
			// $d =str_split($conf['nmea'][$i][0]);
			// $d =str_split($conf['nmea'][$i][0]);
			// for ($j=0;$j<3;$j++) $data[$index++]=ord($d[$j]);
			// for ($j=0;$j<3;$j++) $data[$index++]=ord($d[$j]);
    for ($j=0;$j<3;$j++) $conf['nmea'][$i][0].=chr($data[$index++]);
			for($j = 0; $j < 3; $j ++)
				$conf ['nmea'] [$i] [0] .= chr ( $data [$index ++] );
				// $d =str_split($conf['nmea'][$i][1]);
				// $d =str_split($conf['nmea'][$i][1]);
				// 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;
			for($j = 0; $j < 29; $j ++) {
			for($j = 0; $j < 29; $j ++) {
				$d = $data [$index ++];
				$d = $data [$index ++];
     if ($d!=0) $conf['nmea'][$i][1].=chr($d);
				if ($d != 0)
					$conf ['nmea'] [$i] [1] .= chr ( $d );
				else {
				else {
					$index += (29 - 1 - $j);
					$index += (29 - 1 - $j);
					break;
					break;
@@ -933,7 +1039,8 @@ function readSettings($conf=null) {
		$conf ['message'] = "";
		$conf ['message'] = "";
		for($j = 0; $j < 56; $j ++) {
		for($j = 0; $j < 56; $j ++) {
			$d = $data [$index ++];
			$d = $data [$index ++];
     if ($d!=0) $conf['message'].=chr($d);
			if ($d != 0)
				$conf ['message'] .= chr ( $d );
			else {
			else {
				$index += (56 - 1 - $j);
				$index += (56 - 1 - $j);
				break;
				break;
+87 −80
Original line number Original line Diff line number Diff line
@@ -50,7 +50,7 @@
 */
 */
// look uptime, sleep if just started (no USB yet)
// look uptime, sleep if just started (no USB yet)
// require '/usr/html/includes/i2c.inc';
// require '/usr/html/includes/i2c.inc';
#require 'i2c.inc';
// require 'i2c.inc';
set_include_path ( get_include_path () . PATH_SEPARATOR . '/www/pages/include' );
set_include_path ( get_include_path () . PATH_SEPARATOR . '/www/pages/include' );
require 'i2c_include.php';
require 'i2c_include.php';
$config_name = '/etc/elphel393/imu_logger.xml';
$config_name = '/etc/elphel393/imu_logger.xml';
@@ -64,6 +64,7 @@ $b_index = indexGrandDaughters ( $ids );
// print_r($ids);
// print_r($ids);
// print_r($b_index);
// print_r($b_index);
$baud = null;
$baud = null;
$binfile = "/var/imu_config.bin"; // comment out if not needed - debug feature
if (isset ( $b_index [103696] )) {
if (isset ( $b_index [103696] )) {
	$baud = $ids [$b_index [103696]] ['baud'];
	$baud = $ids [$b_index [103696]] ['baud'];
	if ($baud === 0)
	if ($baud === 0)
@@ -151,9 +152,13 @@ if (isset ( $logger_config )) {
$noGPS = ($compass || $GPS) ? "" : "noGPS";
$noGPS = ($compass || $GPS) ? "" : "noGPS";
$nocompass = ($compass) ? "" : "nocompass";
$nocompass = ($compass) ? "" : "nocompass";



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

/*
 * Actually there is a problem - when compass is disabled, imgsrv incorrectly decodes meta (image number, sensor number)
 * Even if re-srated after running exif.php
 */


$cmd = $exif_php . " init $noGPS $nocompass";
$cmd = $exif_php . " init $noGPS $nocompass";
if ($verbose)
if ($verbose)
@@ -468,8 +473,7 @@ function init_default_config() {
					0x70, // time m/s
					0x70, // time m/s
					0x72, // time d/h
					0x72, // time d/h
					0x74 
					0x74 
			) // time y/m
			), // time y/m
,
			'nmea' => array (
			'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"
					// / 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
					// / "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
@@ -491,8 +495,8 @@ function init_default_config() {
					) 
					) 
			),
			),
			'message' => 'Odometer message' 
			'message' => 'Odometer message' 
	) // Message - up to 56 bytes
	); // Message - up to 56 bytes
;

	return $default_config;
	return $default_config;
}
}
/**
/**
@@ -539,7 +543,7 @@ function combineLoggerConfigs($new_conf, $dflt = null) {
	return $conf;
	return $conf;
}
}
function setup_IMU_logger($conf) {
function setup_IMU_logger($conf) {
	global $verbose;
	global $verbose, $binfile;
	// $xclk_freq=80000000; // 80 MHz in NC353
	// $xclk_freq=80000000; // 80 MHz in NC353
	$xclk_freq = 100000000; // 100 MHz in NC393
	$xclk_freq = 100000000; // 100 MHz in NC393
	$data = array ();
	$data = array ();
@@ -610,12 +614,12 @@ function setup_IMU_logger($conf) {
	// / (DFLT_SLAVE_ADDR << 24))
	// / (DFLT_SLAVE_ADDR << 24))
	$logger_conf |= ($conf ['slow_spi'] & 0x1) << 23;
	$logger_conf |= ($conf ['slow_spi'] & 0x1) << 23;
	$logger_conf |= ($conf ['imu_sa'] & 0x7) << 24;
	$logger_conf |= ($conf ['imu_sa'] & 0x7) << 24;
	$data [$index ++] = $logger_conf & 0xff;
	$data [$index ++] = $logger_conf & 0xff;                // DWORD 0x03
	$data [$index ++] = ($logger_conf >> 8) & 0xff;
	$data [$index ++] = ($logger_conf >> 8) & 0xff;
	$data [$index ++] = ($logger_conf >> 16) & 0xff;
	$data [$index ++] = ($logger_conf >> 16) & 0xff;
	$data [$index ++] = ($logger_conf >> 24) & 0xff;
	$data [$index ++] = ($logger_conf >> 24) & 0xff;
	// / Set time driver will go to sleep if the data is not ready yet (less than full sample in the buffer)
	// / Set time driver will go to sleep if the data is not ready yet (less than full sample in the buffer)
	$data [$index ++] = $conf ['sleep_busy'] & 0xff;
	$data [$index ++] = $conf ['sleep_busy'] & 0xff;        // DWORD 0x04 (software only)   
	$data [$index ++] = ($conf ['sleep_busy'] >> 8) & 0xff;
	$data [$index ++] = ($conf ['sleep_busy'] >> 8) & 0xff;
	$data [$index ++] = ($conf ['sleep_busy'] >> 16) & 0xff;
	$data [$index ++] = ($conf ['sleep_busy'] >> 16) & 0xff;
	$data [$index ++] = ($conf ['sleep_busy'] >> 24) & 0xff;
	$data [$index ++] = ($conf ['sleep_busy'] >> 24) & 0xff;
@@ -660,6 +664,13 @@ function setup_IMU_logger($conf) {
	fwrite ( $dev_imu_ctl, $bindata, strlen ( $bindata ) );
	fwrite ( $dev_imu_ctl, $bindata, strlen ( $bindata ) );
	fseek ( $dev_imu_ctl, 3, SEEK_END ); // start IMU
	fseek ( $dev_imu_ctl, 3, SEEK_END ); // start IMU
	fclose ( $dev_imu_ctl );
	fclose ( $dev_imu_ctl );
	
	if (isset($binfile)){
		$dev_imu_ctl = fopen ( $binfile, 'w' );
		fwrite ( $dev_imu_ctl, $bindata, strlen ( $bindata ) );
		fclose ( $dev_imu_ctl );
	}
	
	// / Readback - just testing
	// / Readback - just testing
	$bd = file_get_contents ( '/dev/imu_ctl' );
	$bd = file_get_contents ( '/dev/imu_ctl' );
	$data = str_split ( $bd );
	$data = str_split ( $bd );
@@ -674,10 +685,6 @@ function setup_IMU_logger($conf) {
	}
	}
}
}





  
// ls /sys/bus/usb-serial/devices/ttyUSB0/driver -l
// ls /sys/bus/usb-serial/devices/ttyUSB0/driver -l
// $xml->addChild ('error','No sync capable board detected, use "role=self" for the onboard timer');
// $xml->addChild ('error','No sync capable board detected, use "role=self" for the onboard timer');
// $sxml=$xml->asXML();
// $sxml=$xml->asXML();