Examples -  GNSS Tracker (simple)

Top  Previous  Next

//-----------------------------------------------------------------------------
// GNSS Tracker.vpl, created 2003-04-28 13:02
//
// Program that sends a VSMS PDU message each 10 seconds, IF it has a valid gnss position
//
// In order to run this example, you need the following:
// 1. Install and configure the RTCU Communication Hub product
// 2. Insert a Cellular network enabled SIM card in the RTCU unit
// 3. Configure the TCP/IP and RCH settings in the RTCU, using the RTCU M2M Studio
// 4. Download this project into the RTCU unit
//
// When running correctly, LED1 will turn on after 30..50 seconds, signalling that 
// the unit is now connected to the RCH. LED2 will blink every time
// the unit gets a correct GNSS position (2D or 3D)
// The program will then send a VSMS message every 10 seconds to the "DestNodeID"
// with information about the current GNSS time (in UTC) and lattitude/longitude
//
// Protocol:
//   <gnsstime> <lattitude> <longitude>
//   where all 3 fields are 4 byte words, little endian format, 12 bytes in total
// 
//-----------------------------------------------------------------------------
INCLUDE rtcu.inc
 
VAR_OUTPUT
   gnssOK   : BOOL; | Monitors valid GNSS fix
   gwOK     : BOOL; | Monitors connection to the RCH.
END_VAR;
 
//  These are the global variables of the program
VAR
  DestNodeID   : STRING:="@1234";   // The nodeid of the receiver
  TXBuf        : ARRAY[0..139] OF SINT; // Holds the transmit buffer
  tSend        : DINT; // Linsec value of next time to send a VSMS message
 
  gnss         : gpsFix;  // GNSS position information
  Lat          : DINT;    // Lattitude
  Lon          : DINT;    // Longitude
  gnssTime     : DINT;    // GNSS time (in UTC !)
END_VAR;
 
//----------------------------------------------------------------------------
// Misc. helper functions
// Pack/Unpack INT and DINT's from an array of SINT
//----------------------------------------------------------------------------
function getINT:int;
var_input
   adr : ptr;
end_var;
memcpy(dst:=addr(getInt),src:=adr,len:=sizeof(getINT));
end_function;
 
function getDINT:dint;
var_input
   adr : ptr;
end_var;
memcpy(dst:=addr(getDINT),src:=adr,len:=sizeof(getDINT));
end_function;
 
function setINT;
var_input
   adr : ptr;
   v   : int;
end_var;
memcpy(dst:=adr,src:=addr(v),len:=sizeof(v));
end_function;
 
function setDINT;
var_input
   adr : ptr;
   v   : dint;
end_var;
memcpy(dst:=adr,src:=addr(v),len:=sizeof(v));
end_function;
 
//----------------------------------------------------------------------------
 
 
PROGRAM GNSS_Tracker;
 
// Switch power on to the Cellular and GNSS modules
gpsPower(power:=true);
gsmPower(power:=true);
 
// Send next position in 10 seconds
tSend:=clockNow() + 10;
 
// Activate the Cellular network support, and connect to the internet
DebugFmt(message:="netOpen=\1", v1:=netOpen(iface:=_NET_IFACE_CELLULAR));
 
BEGIN
  gnss();
  // If time to send a new position...
  IF clockNow() > tSend THEN
     // If we have a 2D or 3D fix, go build buffer, and send as a PDU SMS message
     IF gnss.mode > 1 THEN
        // Calculate next time we need to send
        tSend:=clockNow() + 10;
        
        // Get GNSS time as linsec value
        gnssTime:=gnss.linsec;
        
        // Get Lat/Lon as two DINT values
        Lat:=gnss.latitude;   //  ddmm.mmmm * 10000
        Lon:=gnss.longitude;  // dddmm.mmmm * 10000
 
        // Show the calculated values in the debug window
        DebugFmt(message:="gnssTime=\4", v4:=gnssTime);
        DebugFmt(message:="Latitude=\4", v4:=Lat);
        DebugFmt(message:="Longitude=\4", v4:=Lon);
 
        // Pack lattitude, longitude and GNSS time into the TX buffer (as little endians !) 
        setDint(adr:=addr(TXBuf[0]), v:=gnssTime); 
        setDint(adr:=addr(TXBuf[4]), v:=Lat); 
        setDint(adr:=addr(TXBuf[8]), v:=Lon);
 
        // demonstrates how to extract data from a "raw" buffer (not needed in this example)
        //DebugFmt(message:="gnssTime=\4", v4:=getDINT(adr:=addr(TXBuf[0])));
        //DebugFmt(message:="Lattitude=\4", v4:=getDINT(adr:=addr(TXBuf[4])));
        //DebugFmt(message:="Longitude=\4", v4:=getDINT(adr:=addr(TXBuf[8])));
        
        // Send a binary SMS message with the contents to the destination node (via the RCH)
        DebugFmt(message:="gsmSendPDU=\1", v1:=gsmSendPDU(phonenumber:=DestNodeID, message:=ADDR(TXBuf), length:=12));
        
        // Enable the next line to get the PDU message in the Unit->SMS messages window also
        //gsmSendPDU(phonenumber:="9999", message:=ADDR(TXBuf), length:=12);
     END_IF;
  END_IF;
 
  gwOK:=gwConnected();
  
  // If valid fix from GNSS, change state of LED
  IF gnss.mode > 1 THEN
     gnssOK:=NOT gnssOK;
  END_IF;
  
END;
END_PROGRAM;