#!/usr/bin/env php -q . *! -----------------------------------------------------------------------------** *! $Log: imu_setup.php,v $ *! Revision 1.8 2012/07/03 20:54:36 elphel *! bug fix *! *! Revision 1.7 2011/12/22 05:40:36 elphel *! missed revision *! *! Revision 1.5 2011/09/06 22:02:49 elphel *! added 'list' link *! *! Revision 1.4 2011/09/06 21:56:31 elphel *! help typo *! *! Revision 1.3 2011/09/06 21:53:49 elphel *! adding to help *! *! Revision 1.2 2011/09/06 21:31:48 elphel *! added help *! *! Revision 1.1 2011/09/06 19:07:50 elphel *! initial release, based on start_gps_compass.php *! */ #require 'i2c.inc'; set_include_path ( get_include_path () . PATH_SEPARATOR . '/www/pages/include' ); require 'i2c_include.php'; // $xclk_freq=80000000; // 80 MHz in NC353 $xclk_freq = 100000000; // 100 MHz in NC393 $messageOffset = 0xb0; // offset to start of "odometer" message in the device driver file $config_name = "/etc/elphel393/imu_logger.xml"; $gps_compass_xml = "/www/pages/gps_compass.xml"; $i2cbus = 4; if (isset ( $_SERVER ["SERVER_PROTOCOL"] )) { // echo "web server\n"; if (isset ( $_GET ['msg'] )) { setMessage ( urldecode ( $_GET ['msg'] ) ); } else if (isset ( $_GET ['lmsg'] )) { setMessage ( urldecode ( $_GET ['msg'] ) ); // software toggle input "odometer" line } // / modify parameters if specified $conf = readSettings ( init_default_config () ); // read current settings $apply = 0; // echo "
\n";
	if (count ( $_GET ) == 0) {
		help ();
		exit ( 0 );
	}
	if (array_key_exists ( 'source', $_GET )) {
		$source = file_get_contents ( $_SERVER ['SCRIPT_FILENAME'] );
		header ( "Content-Type: text/php" );
		header ( "Content-Length: " . strlen ( $source ) . "\n" );
		header ( "Pragma: no-cache\n" );
		echo $source;
		exit ( 0 );
	}
	
	foreach ( $_GET as $key => $value ) {
		$access = split ( ':', $key );
		if ($value === '')
			continue;
		if (count ( $access ) < 1)
			continue;
		if (! isset ( $conf [$access [0]] ))
			continue;
		if (count ( $access ) == 1) {
			if (($access [0] == 'imu_period') && ($value == 'auto'))
				$value = 0xffffff00;
			if (is_string ( $conf [$access [0]] ))
				$conf [$access [0]] = $value;
			else
				$conf [$access [0]] = $value + 0;
			$apply ++;
		} else { // count($access)>1
			if (! isset ( $conf [$access [0]] [$access [1]] ))
				continue;
			if (count ( $access ) == 2) {
				if (is_string ( $conf [$access [0]] [$access [1]] ))
					$conf [$access [0]] [$access [1]] = $value;
				else
					$conf [$access [0]] [$access [1]] = $value + 0;
				$apply ++;
			} else { // count($access)>2)
				if (count ( $access ) > 3)
					continue;
				if ($access [2] == 'sentence')
					$access [2] = 0;
				else if ($access [2] == 'format')
					$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 ++;
			}
		}
		/*
		 * echo 'key='.$key."\n";
		 * print_r($access);
		 * var_dump($value);
		 */
	}
	if ($apply > 0) {
		echo 'modified ' . $apply . "parameters
\n"; // print_r($conf); setup_IMU_logger ( $conf ); } if (isset ( $_GET ['save'] )) { // if ($verbose) echo 'Writing IMU logger configuration to '. $config_name."\n"; $conf = readSettings ( init_default_config () ); $logger_xml = loggerConfigToXML ( $conf ); $conf_file = fopen ( $config_name, "w" ); fwrite ( $conf_file, $logger_xml->asXML () ); fclose ( $conf_file ); exec ( 'sync' ); } if (isset ( $_GET ['list'] )) { $conf = readSettings ( init_default_config () ); $logger_xml = loggerConfigToXML ( $conf ); $logger_xml_text = $logger_xml->asXML (); header ( "Content-Type: text/xml" ); header ( "Content-Length: " . strlen ( $logger_xml_text ) . "\n" ); header ( "Pragma: no-cache\n" ); printf ( $logger_xml_text ); } else echo 'OK'; exit ( 0 ); } else { // echo "CGI\n"; } /* * echo ""; * echo 'OK'; * exit(0); */ // ! Here comes if CLI /** TODO: Not yet updated for NC393 - use start_gps_compass.php */ $ids = scanGrandDaughters (); $b_index = indexGrandDaughters ( $ids ); // print_r($ids); // print_r($b_index); $baud = null; if (isset ( $b_index [103696] )) { $baud = $ids [$b_index [103696]] ['baud']; if ($baud === 0) $baud == 'auto'; } // exit (0); $wait_time = 20; // wait for $wait_time if both devices were not found, retry $compass = null; $GPS = null; $verbose = true; foreach ( $_SERVER ['argv'] as $param ) { if ((substr ( $param, 0, 2 ) == "-q") || (substr ( $param, 0, 8 ) == "--silent")) { $verbose = false; } } // / kill those GPS and compass if they are running already exec ( 'killall -q garminusb2exif' ); exec ( 'killall -q nmea2exif' ); exec ( 'killall -q compass' ); // find_gps_compass(); $devs = find_gps_compass ( $baud ); // echo "compass: ";var_dump($compass);echo "\n"; // 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"; sleep ( $wait_time ); if ($verbose) echo "Retrying detection\n"; $devs = find_gps_compass ( $baud ); } listSensorDevices ( $devs, $b_index, $ids ); // var_dump($GPS); // if (file_exists ($sensor_state_file)) $sensor_board=parse_ini_file($sensor_state_file); // exec ('sync'); $logger_config = null; $write_config = false; if (file_exists ( $config_name )) { 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"; // $default_config=init_default_config(); // $logger_config=init_default_config(); $logger_config = array (); if (isset ( $GPS ) && isset ( $b_index [103696] )) { $logger_config ['baud_rate'] = $GPS ['baud']; $logger_config ['gps_slot'] = $ids [$b_index [103696]] ['port']; $logger_config ['gps_mode'] = $ids [$b_index [103696]] ['sync']; } if (isset ( $b_index [103695] )) { $logger_config ['imu_slot'] = $ids [$b_index [103695]] ['port']; $logger_config ['imu_sa'] = $b_index [103695]; } $write_config = true; // write configuration to /etc after applying defaults } if (isset ( $logger_config )) { // var_dump($logger_config); $logger_config = combineLoggerConfigs ( $logger_config, init_default_config () ); // var_dump($logger_config); // var_dump($default_config); $logger_xml = loggerConfigToXML ( $logger_config ); $conf_file = fopen ( '/var/html/logger_config.xml', "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"; $conf_file = fopen ( $config_name, "w" ); fwrite ( $conf_file, $logger_xml->asXML () ); fclose ( $conf_file ); exec ( 'sync' ); } } // echo "compass: ";var_dump($compass);echo "\n"; // 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"; exec ( $cmd ); if ($GPS) { if ($verbose) echo "Starting " . $GPS ["name"] . " as " . $GPS ["file"] . "\n"; if (strpos ( $GPS ['name'], 'NMEA' ) !== false) { $cmd = "/usr/local/bin/nmea2exif " . $GPS ["file"] . " &"; } else { $cmd = "/usr/local/bin/garminusb2exif " . $GPS ["file"] . " &"; } if ($verbose) echo "exec: $cmd \n"; popen ( $cmd, "r" ); } if ($compass) { 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"; popen ( $cmd, "r" ); } exit ( (isset ( $b_index [103695] ) ? 4 : 0) | ($compass ? 2 : 0) | ($GPS ? 1 : 0) ); function help() { $GP = '$GP'; $GPRMC = '$GPRMC'; $GPGGA = '$GPGGA'; $GPGSA = '$GPGSA'; $GPVTG = '$GPVTG'; $script_name = $_SERVER ['SCRIPT_NAME']; $prefix_url = 'http://' . $_SERVER ['SERVER_ADDR'] . $_SERVER ['SCRIPT_NAME']; // echo << Control links: //