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  2008/04/07 09:14:47  elphel
00027 *!  Discover/startup GPS and compass
00028 *!
00029 */
00030 //look uptime, sleep if just started (no USB yet)
00031  $wait_time=20; // wait for $wait_time if both devices were not found, retry
00032     $compass=null;
00033     $GPS=null;
00034     find_gps_compass();
00035 //    echo "compass: ";var_dump($compass);echo "\n";
00036 //    echo "GPS:     ";var_dump($GPS);echo "\n";
00037    if ((!$compass || !$GPS) && ((exec('date +%s')+0) <30)) {
00038           echo "waiting $wait_time sec for USB devices to start up\n";
00039           sleep($wait_time);
00040           echo "Retrying detection\n";
00041           find_gps_compass();
00042     }
00043 //    echo "compass: ";var_dump($compass);echo "\n";
00044 //    echo "GPS:     ";var_dump($GPS);echo "\n";
00045     $noGPS=    ($compass || $GPS)?"" : "noGPS"; 
00046     $nocompass=($compass)?"" : "nocompass"; 
00047     $cmd="/usr/html/exif.php init $noGPS $nocompass";
00048     echo "Initializing Exif template: $cmd\n";
00049     exec($cmd);
00050 // kill those GPS and compass if they are running already
00051     exec("killall -q garminusb2exif");
00052     exec("killall -q compass");
00053     if ($GPS) {
00054       echo "Starting ".$GPS["name"]. " as ". $GPS["file"]. "\n";
00055       $cmd ="/usr/local/bin/garminusb2exif ". $GPS["file"]." &";
00056       echo "exec: $cmd \n";
00057       popen($cmd,"r");
00058     }
00059     if ($compass) {
00060       echo "Starting ".$compass["name"]. " as ". $compass["file"]. "\n";
00061       exec ("stty -F ".$compass["file"]." -echo speed 19200");
00062       $cmd="/usr/local/bin/compass ". $compass["file"]." &";
00063       echo "exec: $cmd \n";
00064       popen($cmd,"r");
00065     }
00066     exit (0);
00067 
00068  function find_gps_compass() {
00069     global $GPS, $compass;
00070     $devices=array(
00071               "compass"=>array(array("name"=>"Ocean Server OS-5000","driver"=>"cp2101")),
00072               "GPS"    =>array(array("name"=>"Garmin GPS 18 USB","driver"=>"garmin_gps")));
00073     exec("ls /sys/bus/usb-serial/devices",$usb_ser_devs); // ("ttyUSB0","ttyUSB1")
00074     $devs = new SimpleXMLElement("<?xml version='1.0' standalone='yes'?><USB_serial_devices/>");
00075     
00076     foreach ($usb_ser_devs as $dev) {
00077       $arr=split("/",exec("ls /sys/bus/usb-serial/devices/".$dev."/driver -l"));
00078       $driver=$arr[count($arr)-1];
00079       foreach ($devices["compass"] as $d) {
00080         if ($d["driver"]==$driver) {
00081           $dd=$devs->addChild ('compass');
00082           $dd->addChild ('name',$d["name"]);
00083           $dd->addChild ('driver',$d["driver"]);
00084           $dd->addChild ('file',"/dev/".$dev);
00085         }
00086       }
00087       foreach ($devices["GPS"] as $d) {
00088         if ($d["driver"]==$driver) {
00089           $dd=$devs->addChild ('GPS');
00090           $dd->addChild ('name',$d["name"]);
00091           $dd->addChild ('driver',$d["driver"]);
00092           $dd->addChild ('file',"/dev/".$dev);
00093         }
00094       }
00095     }
00096 //    var_dump($devs);
00097     $state_file=fopen("/var/html/gps_compass.xml","w");
00098     fwrite($state_file,$devs->asXML());
00099     fclose($state_file);
00100 
00101 // start here from XML
00102     foreach ($devs->compass as $a) {
00103       $compass=array("file"=>(string) $a->file, "name"=> (string) $a->name);
00104       break; // use first one
00105     }
00106     foreach ($devs->GPS as $a) {
00107       $GPS=array("file"=>(string) $a->file, "name"=> (string) $a->name);
00108       break; // use first one
00109     }
00110 }
00111   
00112 //ls /sys/bus/usb-serial/devices/ttyUSB0/driver -l
00113 //         $xml->addChild ('error','No sync capable board detected, use "role=self" for the onboard timer');
00114 //    $sxml=$xml->asXML();
00115 ?>

Generated on Thu Aug 7 16:18:59 2008 for elphel by  doxygen 1.5.1