Examples - MX2 GPS log Example

Top  Previous  Next

//-----------------------------------------------------------------------------
// MX2GPSLog.vpl, created 2006-05-31 13:15
//
// The application takes the GPS position every 15 seconds and saves it to both
// the datalogger, and to a file. The file media must be ejected with
// dipswitch 1 before it is removed from the RTCU, otherwise the file will be
// corrupted. The status of the media is shown on the LED on the RTCU.
// When the device is no longer moving it goes into a power saving mode and waits
// for movement.
// This example demonstrates the Filesystem and powermanagement features of the
// MX2 device, and can only be used with a MX2 device
//-----------------------------------------------------------------------------
INCLUDE rtcu.inc
 
VAR_INPUT
  dipEject   : BOOL R_EDGE;
END_VAR;
 
VAR_OUTPUT
  gw_conn : BOOL;
  gps_pos : BOOL;
END_VAR;
 
 
VAR
  log       : logWrite;
  clock     : clockLinsecToTime;
  gps       : gpsFix;
  gps_linsec : DINT;
  gps_pwr   : BOOL;
  gsm_pwr   : BOOL;
  media_open : BOOL;
  fd         : FILE;
  rc         : INT;
  str       : STRING;
  tSleep     : TON;
END_VAR;
 
PROGRAM MX2GPSLog;
 
// Use battery if power fails
pmPowerFail(bat:=TRUE);
 
// Set timer
tSleep.pt := 300000; // 5 min. delay
 
// Open media
fsMediaOpen(media:=0);
fsStatusLEDEnable(enable:=ON);
 
// Initialize datalogger system
IF NOT logIsInitialized(key:=4712) THEN
  logInitialize(key:=4712, numlogvalues:=2, numlogrecords:=0);
END_IF;
 
 
BEGIN
  // LED
  gw_conn := gwConnected();
 
  // Open log file
  IF fsMediaPresent(media:=0) AND NOT media_open THEN
    media_open := TRUE;
  ELSIF NOT fsMediaPresent(media:=0) AND media_open THEN
    media_open := FALSE;
  END_IF;
 
  // Eject media
  IF dipEject THEN
    fsMediaEject(media:=0);
     media_open := FALSE;
  END_IF;
 
  // Open log file  
  IF media_open AND fsFileStatus(fd:=fd) <> 0 THEN
    IF fsFileExists(name:="\datalog.txt") THEN
        DebugMsg(message:="Open File");
        fd := fsFileOpen(name:="\datalog.txt");
    ELSE
        DebugMsg(message:="File Create");
        fd := fsFileCreate(name:="\datalog.txt");
    END_IF;
  END_IF;
 
  // Start GPS and/or GSM
  IF NOT gps_pwr THEN
    DebugMsg(message:="Start GPS");
    gpsPower(power:=ON);
     gps_pwr := ON;
  END_IF;
  IF NOT gsm_pwr THEN
    DebugMsg(message:="Start GSM");
    gsmPower(power:=ON);
    gprsOpen();
     gsm_pwr := ON;
  END_IF;
 
  // Get GPS position
  gps();
  // GPS position valid?
  IF gps.mode > 1 THEN
    // Toggle led
     gps_pos   := NOT gps_pos;
    // log position
    IF clockNow() > gps_linsec THEN
        // Save to datalog
        log(value[1]:=gps.latitude,value[2]:=gps.longitude);
        // Save to file
        IF fsFileStatus(fd:=fd) = 0 THEN
           clock(linsec:=gps.linsec);
           str := strFormat(format:="\1.\2.\3, ",v1:=clock.year,v2:=clock.month,v3:=clock.day) +
                  strFormat(format:="\1:\2:\3, ",v1:=clock.hour,v2:=clock.minute,v3:=clock.second);
          IF gps.latsouth THEN str := str + "-"; END_IF;
           str := str + strFormat(format:="\1*\2.\3, ",v1:=gps.latdeg,v2:=gps.latmin,v3:=gps.latdecmin);
          IF gps.lonwest THEN str := str + "-"; END_IF;
           str := str + strFormat(format:="\1*\2.\3, ",v1:=gps.londeg,v2:=gps.lonmin,v3:=gps.londecmin);
          fsFileWriteStringNL(fd:=fd,str:=str);
        END_IF;
        // Set time for next logging
        gps_linsec := clockNow() + 15;
    END_IF;
  END_IF;
 
  // Has there been any movement?
  IF pmVibration() THEN
     tSleep(trig:=FALSE);
  ELSE
     tSleep(trig:=TRUE);
  END_IF;
 
  // No movement detected in 5 minutes
  IF tSleep.q THEN
    IF NOT batIsCharging() THEN
        // Debug to file
        IF fsFileStatus(fd:=fd) = 0 THEN
          fsFileWriteStringNL(fd:=fd,str:="--- Enter pmWaitEvent ---");
        END_IF;
        // Stop GPS and/or GSM
        IF gps_pwr THEN
          DebugMsg(message:="Stop GPS");
          gpsPower(power:=OFF);
           gps_pwr := OFF;
        END_IF;
        IF gsm_pwr THEN
          DebugMsg(message:="Stop GSM");
          gprsClose();
          gsmPower(power:=OFF);
           gsm_pwr := OFF;
        END_IF;
        // Wait for movement (vibration)
        rc := pmWaitEvent(vibration:=ON);
        DebugMsg(message:="pmWaitEvent - Movement");
        // Debug to file
        IF fsFileStatus(fd:=fd) = 0 THEN
           str := strFormat(format:="--- Exit pmWaitEvent (rc=\1) ---",v1:=rc);
          fsFileWriteStringNL(fd:=fd,str:=str);
        END_IF;
        // Wait for 5 minutes before Power down again
        tSleep(trig:=FALSE);
    END_IF;
  END_IF;
 
END;
 
END_PROGRAM;