apps/web/imgsrv/start_gps_compass.php

Go to the documentation of this file.
00001 #!/usr/local/sbin/php -q
00002 <?php
00003 /*
00004 *! FILE NAME  : start_gps_compass.php
00005 *! DESCRIPTION: Looks for USB GPS (currently Garmin GPS 18 USB) and compass
00006 *!              (currently Ocean Server OS-5000), re-initializes Exif header
00007 *!              and starts the devices if found
00008 *!
00009 *! Copyright (C) 2008 Elphel, Inc
00010 *! -----------------------------------------------------------------------------**
00011 *!
00012 *!  This program is free software: you can redistribute it and/or modify
00013 *!  it under the terms of the GNU General Public License as published by
00014 *!  the Free Software Foundation, either version 3 of the License, or
00015 *!  (at your option) any later version.
00016 *!
00017 *!  This program is distributed in the hope that it will be useful,
00018 *!  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *!  GNU General Public License for more details.
00021 *!
00022 *!  You should have received a copy of the GNU General Public License
00023 *!  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00024 *! -----------------------------------------------------------------------------**
00025 *!  $Log: start_gps_compass.php,v $
00026 *!  Revision 1.1.1.1  2008/11/27 20:04:01  elphel
00027 *!
00028 *!
00029 *!  Revision 1.2  2008/11/20 07:03:40  elphel
00030 *!  exit value now encodes detected devices
00031 *!
00032 *!  Revision 1.1  2008/04/07 09:14:47  elphel
00033 *!  Discover/startup GPS and compass
00034 *!
00035 */
00036 //look uptime, sleep if just started (no USB yet)
00037  $wait_time=20; // wait for $wait_time if both devices were not found, retry
00038     $compass=null;
00039     $GPS=null;
00040     find_gps_compass();
00041 //    echo "compass: ";var_dump($compass);echo "\n";
00042 //    echo "GPS:     ";var_dump($GPS);echo "\n";
00043    if ((!$compass || !$GPS) && ((exec('date +%s')+0) <30)) {
00044           echo "waiting $wait_time sec for USB devices to start up\n";
00045           sleep($wait_time);
00046           echo "Retrying detection\n";
00047           find_gps_compass();
00048     }
00049 //    echo "compass: ";var_dump($compass);echo "\n";
00050 //    echo "GPS:     ";var_dump($GPS);echo "\n";
00051     $noGPS=    ($compass || $GPS)?"" : "noGPS"; 
00052     $nocompass=($compass)?"" : "nocompass"; 
00053     $cmd="/usr/html/exif.php init $noGPS $nocompass";
00054     echo "Initializing Exif template: $cmd\n";
00055     exec($cmd);
00056 // kill those GPS and compass if they are running already
00057     exec("killall -q garminusb2exif");
00058     exec("killall -q compass");
00059     if ($GPS) {
00060       echo "Starting ".$GPS["name"]. " as ". $GPS["file"]. "\n";
00061       $cmd ="/usr/local/bin/garminusb2exif ". $GPS["file"]." &";
00062       echo "exec: $cmd \n";
00063       popen($cmd,"r");
00064     }
00065     if ($compass) {
00066       echo "Starting ".$compass["name"]. " as ". $compass["file"]. "\n";
00067       exec ("stty -F ".$compass["file"]." -echo speed 19200");
00068       $cmd="/usr/local/bin/compass ". $compass["file"]." &";
00069       echo "exec: $cmd \n";
00070       popen($cmd,"r");
00071     }
00072     exit (($compass?2:0) | ($GPS?1:0));
00073 
00074  function find_gps_compass() {
00075     global $GPS, $compass;
00076     $devices=array(
00077               "compass"=>array(array("name"=>"Ocean Server OS-5000","driver"=>"cp2101")),
00078               "GPS"    =>array(array("name"=>"Garmin GPS 18 USB","driver"=>"garmin_gps")));
00079     exec("ls /sys/bus/usb-serial/devices",$usb_ser_devs); // ("ttyUSB0","ttyUSB1")
00080     $devs = new SimpleXMLElement("<?xml version='1.0' standalone='yes'?><USB_serial_devices/>");
00081     
00082     foreach ($usb_ser_devs as $dev) {
00083       $arr=split("/",exec("ls /sys/bus/usb-serial/devices/".$dev."/driver -l"));
00084       $driver=$arr[count($arr)-1];
00085       foreach ($devices["compass"] as $d) {
00086         if ($d["driver"]==$driver) {
00087           $dd=$devs->addChild ('compass');
00088           $dd->addChild ('name',$d["name"]);
00089           $dd->addChild ('driver',$d["driver"]);
00090           $dd->addChild ('file',"/dev/".$dev);
00091         }
00092       }
00093       foreach ($devices["GPS"] as $d) {
00094         if ($d["driver"]==$driver) {
00095           $dd=$devs->addChild ('GPS');
00096           $dd->addChild ('name',$d["name"]);
00097           $dd->addChild ('driver',$d["driver"]);
00098           $dd->addChild ('file',"/dev/".$dev);
00099         }
00100       }
00101     }
00102 //    var_dump($devs);
00103     $state_file=fopen("/var/html/gps_compass.xml","w");
00104     fwrite($state_file,$devs->asXML());
00105     fclose($state_file);
00106 
00107 // start here from XML
00108     foreach ($devs->compass as $a) {
00109       $compass=array("file"=>(string) $a->file, "name"=> (string) $a->name);
00110       break; // use first one
00111     }
00112     foreach ($devs->GPS as $a) {
00113       $GPS=array("file"=>(string) $a->file, "name"=> (string) $a->name);
00114       break; // use first one
00115     }
00116 }
00117   
00118 //ls /sys/bus/usb-serial/devices/ttyUSB0/driver -l
00119 //         $xml->addChild ('error','No sync capable board detected, use "role=self" for the onboard timer');
00120 //    $sxml=$xml->asXML();
00121 ?>

Generated on Fri Nov 28 00:06:23 2008 for elphel by  doxygen 1.5.1