dbartlett
Posts: 1
Joined: Mon Feb 18, 2019 5:52 pm

EC2002 Issues getting Dynamic Speed from OSM

Wed Feb 12, 2020 11:43 pm

Hello,

I am having an issue using the guide for EC2002 with the scripted Eco Report. For some reason the report is not populating the data when our drivers are speeding. I have event rules set up to send me emails when our drivers go 10mph over the speed limit posted by OSM. We are using this data to create a report card based on their driving habits. The harsh breaking and acceleration are populating correctly but none of the speeding events are matching what I am receiving via the email alerts. What follows is the script that I am currently using in the Eco Report Event Rule. There are 2 sections for speeding. 1 that checks if they were going 10 - 20 over and another to check if they were going 20 over. Any help would be greatly appreciated.

Code: Select all

//Count number of times something (name) happens
//"lastTrackOrNewDriverOrVehicle" is needed to cover cases for midnight, see firstRecordedFromDriverOrVehicle
var countTime = function(value, name) {
  if (value && !state[name + "State"] && !lastTrackOrNewDriverOrVehicle) {
    state["Start" + name] = trackPoint.utcTimestamp.getTime();
    state[name + "State"] = true;
    if (state["First" + name] == 0) {
      state["First" + name] = trackPoint.utcTimestamp.getTime();
    }
  } else if (!value && state[name + "State"] || (value && lastTrackOrNewDriverOrVehicle)) {
    state[name + "Time"] += trackPoint.utcTimestamp.getTime() - state["Start" + name];
    state[name + "State"] = false;
    state["Last" + name] = trackPoint.utcTimestamp.getTime();
  }
}
var additionOverdue = function(name, value) //Count number of times something (name) happens
  {
    if (value) {
      if (state[name + "Start"] == 0) {
        state[name + "Start"] = trackPoint.utcTimestamp.getTime();
      }
      if (!state["Is" + name]) {
        state["Count" + name]++;
        state["Is" + name] = true;
      }
    } else {
      state[name + "Start"] = 0;
      state["Is" + name] = false;
    }
  }
  //Replace below to get "Dynamic Speedlimits from OSM"
  var OverspeedTen = function(mPerSec) {
  	var speed = mPerSec * 3.6;
	//log("OverSpeedTen-Speed " + speed)
      var SPEED_LIMIT_UNREAL = 500; //Speeds above this are unrealistic for vehicles on the ground
      var roadInfo;
      //"Try & catch" added here to not care if Maps are initialized for script validation when saving the Event Rule.
      try {
        roadInfo = road.getRoad();
      } catch (e) {
        return false;
      }
      context.setVariable("roadType", roadInfo.type);
      //Return true if Speed is above an OSM speedlimit
		//log("OverSpeedTen-roadInfo.speedLimit " + roadInfo.speedLimit)
      if ((roadInfo.speedLimit * 3.6) < SPEED_LIMIT_UNREAL && speed > ((roadInfo.speedLimit * 3.6) + 16.0934) && (speed - ((roadInfo.speedlimit * 3.6) + 16.0934)) > 16.1 && (speed - ((roadInfo.speedlimit * 3.6) + 16.0934)) < 32.2) {
        return true;
	   } else {
     	 return false;
      }
  }

  var OverspeedTwenty = function(mPerSec) {
	var speed = mPerSec * 3.6;
    var SPEED_LIMIT_UNREAL = 500; //Speeds above this are unrealistic for vehicles on the ground
    var roadInfo;
    //"Try & catch" added here to not care if Maps are initialized for script validation when saving the Event Rule.
    try {
      roadInfo = road.getRoad();
    } catch (e) {
      return false;
    }
    context.setVariable("roadType", roadInfo.type);
    //Return true if Speed is above an OSM speedlimit
    if (roadInfo.speedLimit < SPEED_LIMIT_UNREAL && speed > ((roadInfo.speedLimit * 3.6) + 16.0934) && (speed - ((roadInfo.speedlimit * 3.6) + 16.0934)) >= 32.2) {
      return true;
    } else {
    	return false;
	}
  }

//var Overspeed = function(speed) {
//    return ((3.6 * speed) > speedLimit);
//  }
  //Replace above to get "Dynamic Speedlimits from OSM"

var addition = function(name, value) {
  if (value) {
    state[name]++;
  }
}
var compute = function() { //Calculate "Eco-Driving"
  var bHarshAcc = fields.get("HarshAccelerationDigital", false);
  var bHarshBrk = fields.get("HarshBreakingDigital", false);
  var bHarshTrn = fields.get("HarshTurningDigital", false);
  var moving = fields.get("Speed", false) > 0.5;
  var SeatBeltStatus = fields.get("SeatBeltStatus", false);
  var currentSpeed = fields.get("Speed", 0);
  var ignitionOff = !fields.get("Ignition", false);
  var idling = !ignitionOff && !moving;

  state.MaxSpeed = Math.max(state.MaxSpeed, currentSpeed);
  additionOverdue("OverSpeedTen", OverspeedTen(currentSpeed));
  additionOverdue("OverSpeedTwenty", OverspeedTwenty(currentSpeed));
  if (moving) {
    addition("CountHarshAcc", bHarshAcc);
    addition("CountHarshBrk", bHarshBrk);
    addition("CountHarshTrn", bHarshTrn);
    additionOverdue("SeatBelt", SeatBeltStatus, 0);
  }
  countTime(idling, "Idling");
  countTime(moving, "Driving");
  countTime(ignitionOff, "IgnitionOff");
  countTime(state.IsOverSpeed, "OverSpeed");
}

var init = function() { //Initialize the state
  report.context.setDistance(0);
  return {
    CountHarshAcc: 0, //Harsh Accelaration
    CountHarshBrk: 0, //Harsh Breaking
    CountHarshTrn: 0, //Harsh Turning
    CountOverSpeedTen: 0, //Count number of over speeding events
    CountOverSpeedTwenty: 0,
    OverSpeedStart: 0, //Over speed start time
    CountSeatBelt: 0, //Count number of seat-bealts neglects
    SeatBeltStart: 0, //Seatbealt neglect start time
    DriverOrVehicle: session.driver.username, // place 1
	//DriverOrVehicle: session.vehicle.username,
    DriverOrVehicleDebugg: fields.get("CustomString2", false), //Debug-keep track of DriverOrVehicle
    MaxSpeed: 0, //Maximum speed
    StartDriving: 0, //Start time for XX (XX=Driving)
    FirstDriving: 0, //First time for XX during a day
    LastDriving: 0, //Last time for XX during a day
    DrivingTime: 0, //XX time for a day
    DrivingState: false, //Was last track point XX = true/false?
    StartIgnitionOff: 0, // Exchange XX to IgnitionOff
    FirstIgnitionOff: 0,
    LastIgnitionOff: 0,
    IgnitionOffTime: 0,
    IgnitionOffState: false,
    StartOverSpeed: 0, //Exchange XX to OverSpeed
    FirstOverSpeed: 0,
    LastOverSpeed: 0,
    OverSpeedTime: 0,
    OverSpeedState: false,
    StartIdling: 0, //Exchange XX to Idling
    FirstIdling: 0,
    LastIdling: 0,
    IdlingTime: 0,
    IdlingState: false
  };
}

//VARIABLES
//Replace below to get "Custom Fields"
var harshAccWeight = 2; //30;
var harshBrakeWeight = 6; //30;
var harshTurnWeight = 0; //30;
var oSpeedWeightTen = 10; //30;
var oSpeedWeightTwenty = 20; //var sBeltWeight = 30;
var tripIdleWeight = 0; //30;
var excellent = 100;
var good = 200;
var fair = 400;
//Replace above to get "Custom Fields"

//Replace below to get "Dynamic Speedlimits from OSM"
//var speedLimit = 90;
var livingStreet = 40; //See Open Street Map (OSM)
var motorWay, trunk = 100;
var motorWayLink = 110;
var primary, trunkLink = 80;
var primaryLink, secondary, service, tertiary = 70;
var residential = 40;
var road, secondaryLink, unlassified = 50;
//Replace above to get "Dynamic Speedlimits from OSM"
var state = context.state || init();
var MIN_DISTANCE = 160.934; //Only trips longer than this, are analysed, (m)
var GEN_WEIGHT = 1; //100000; //General weight for all points.
var ecoClass = "N/A";
var totalScore = 0;
var avgSpeed = 0;
var accPts = 0;
var brakePts = 0;
var turnPts = 0;
var oSpeedPtsTen = 0;
var oSpeedPtsTwenty = 0; //var sBeltPts = 0;
var eventScore = 0;
var idlePts = 0;
//Eventrule applied to vehicles
var newDriverOrVehicle = (state.DriverOrVehicle != session.driver.username);//place 2
//Eventrule applied to drivers
//var newDriverOrVehicle = (state.DriverOrVehicle != session.vehicle.username);
//Use this when debugging to see historical data
//var newDriverOrVehicle =  (state.DriverOrVehicleDebugg != fields.get("CustomString2", false));
var lastTrackOrNewDriverOrVehicle = (report.context.isLastInDay || newDriverOrVehicle);

if (lastTrackOrNewDriverOrVehicle) {
  if (report.context.isLastInDay) {
    compute();
  }
  if (report.context.distance >= MIN_DISTANCE) {
    accPts = GEN_WEIGHT * harshAccWeight * state.CountHarshAcc;
    brakePts = GEN_WEIGHT * harshBrakeWeight * state.CountHarshBrk;
    turnPts = GEN_WEIGHT * harshTurnWeight * state.CountHarshTrn;
    oSpeedPtsTen = GEN_WEIGHT * oSpeedWeightTen * state.CountOverSpeedTen;
    oSpeedPtsTwenty = GEN_WEIGHT * oSpeedWeightTwenty * state.CountOverSpeedTwenty; //sBeltPts = GEN_WEIGHT * sBeltWeight * state.CountSeatBelt;
    eventScore = accPts + brakePts + turnPts + oSpeedPtsTen + oSpeedPtsTwenty;
    idlePts = tripIdleWeight * state.IdlingTime;
  }
  context.setVariable("Distance", Math.round(report.context.distance)); //RA0
  context.setVariable("MaxSpeed", Math.round(state.MaxSpeed)); //RA1
  context.setVariable("CountHarshBrk", state.CountHarshBrk); //RA2
  context.setVariable("BrakePts", brakePts); //RA3
  context.setVariable("CountHarshAcc", state.CountHarshAcc); //RA4
  context.setVariable("AccPts", accPts); //RA5
  context.setVariable("CountHarshTrn", state.CountHarshTrn); //RA6
  context.setVariable("TurnPts", turnPts); //RA7
  context.setVariable("CountOverSpeedTen", state.CountOverSpeedTen); //RA8
  context.setVariable("oSpeedPtsTen", oSpeedPtsTen); //RA9
  context.setVariable("CountOverSpeedTwenty", state.CountOverSpeedTwenty); //RA10 context.setVariable("CountSeatBelt", state.CountSeatBelt); //RA10
  context.setVariable("oSpeedPtsTwenty", oSpeedPtsTwenty); //RA11 context.setVariable("SBeltPts", sBeltPts); //RA11
  context.setVariable("EventScore", eventScore); //RA12
  context.setVariable("Trip", state.DrivingTime / 1000); //RA13
  context.setVariable("Idle", state.IdlingTime / 1000); //RA14
  context.setVariable("IdlePts", idlePts / 1000); //RA15
  context.setVariable("OverSpeedTime", state.OverSpeedTime / 1000); //RA16
  context.setVariable("MinDistance", MIN_DISTANCE); //RA17
  context.setVariable("Excellent", excellent); //RA18
  context.setVariable("Good", good); //RA19
  context.setVariable("Fair", fair); //RA20

  if (newDriverOrVehicle) {
    state = init();
  } else {
    state = null;
  }
  context.setState(state);
  return false;
}
compute();
state.DriverOrVehicle = session.driver.username; // place 3
//state.DriverOrVehicle = session.vehicle.username;
//state.DriverOrVehicleDebugg = fields.get("CustomString2", false), //Debug-keep track of driver
context.setState(state);
return true;


Return to “GpsGate for Developers”