How to calculate grade

Does anyone have a code snippet to calculate grade for cycling? Everything that I have tried so far has been too wildly inaccurate to be useful.
  • This is my solution, but question is how is it accurate..

    class ClimbView extends Ui.DataField {

    function initialize() {
    DataField.initialize();
    }

    var prevV = new [5];
    var prevD = new [5];

    var vzdalenost5s = 0;
    var vyska5s = 0;

    function compute(info) {

    if(info.altitude != null) {

    if (prevV[0] != null && prevV[1] != null && prevV[2] != null && prevV[3] != null)
    {vyska5s = (info.altitude - prevV[3]);}
    else
    {vyska5s = 0;}

    if (prevV[0] == null) {
    prevV[0] = info.altitude;
    }
    else if (prevV[1] == null) {
    prevV[1] = info.altitude;
    }
    else if (prevV[2] == null) {
    prevV[2] = info.altitude;
    }
    else if (prevV[3] == null) {
    prevV[3] = info.altitude;
    }
    else {
    prevV[3] = prevV[2];
    prevV[2] = prevV[1];
    prevV[1] = prevV[0];
    prevV[0] = info.altitude;
    }
    }

    if(info.elapsedDistance != null) {

    if (prevD[0] != null && prevD[1] != null && prevD[2] != null && prevD[3] != null)
    {vzdalenost5s = (info.elapsedDistance - prevD[3]);}
    else
    {vzdalenost5s = 0;}

    if (prevD[0] == null) {
    prevD[0] = info.elapsedDistance;
    }
    else if (prevD[1] == null) {
    prevD[1] = info.elapsedDistance;
    }
    else if (prevD[2] == null) {
    prevD[2] = info.elapsedDistance;
    }
    else if (prevD[3] == null) {
    prevD[3] = info.elapsedDistance;
    }
    else {
    prevD[3] = prevD[2];
    prevD[2] = prevD[1];
    prevD[1] = prevD[0];
    prevD[0] = info.elapsedDistance;
    }


    }
    }

    function onUpdate(dc) {

    var width = dc.getWidth();
    var height = dc.getHeight();
    var barva1;
    var barva2;
    var barva3;
    var procenta = 0;

    if (vzdalenost5s != 0 && vyska5s != 0){
    procenta = (100 * vyska5s) / vzdalenost5s;
    }
    else{
    procenta = 0;}

    if(procenta >= 100){
    procenta = 0;}

    if (getBackgroundColor() == Gfx.COLOR_BLACK) {
    barva1 = Gfx.COLOR_BLACK;
    barva2 = Gfx.COLOR_WHITE;
    barva3 = Gfx.COLOR_LT_GRAY;
    } else {
    barva1 = Gfx.COLOR_WHITE;
    barva2 = Gfx.COLOR_BLACK;
    barva3 = Gfx.COLOR_DK_GRAY;
    }


    // pozadi
    dc.setColor(barva1, Gfx.COLOR_TRANSPARENT);
    dc.fillRectangle(0, 0, width, height);



    dc.setColor(barva2, Gfx.COLOR_TRANSPARENT);
    dc.drawText(width/2, height/2, 7, procenta.format("%.01f") + "%" , Gfx.TEXT_JUSTIFY_CENTER | Gfx.TEXT_JUSTIFY_VCENTER);

    dc.setColor(barva3, Gfx.COLOR_TRANSPARENT);
    dc.drawText(width/2, 6, 0, "Climb" , Gfx.TEXT_JUSTIFY_CENTER | Gfx.TEXT_JUSTIFY_VCENTER);

    }

    }




  • Does anyone have a code snippet to calculate grade for cycling? Everything that I have tried so far has been too wildly inaccurate to be useful.


    Here (https://forums.garmin.com/forum/deve...41#post1280941, and in the rest of the thread) I explain my approximation to the problem. The method is still not fully tested, and I think it consumes too much battery due the computations, but you can find the code here: https://bitbucket.org/jgpallero/pvv. When the 2.4 version of the SDK runs on Edge 520 I'll try to use the atmospheric pressure instead of Activity::altitude data to compute the slope
  • I'm going to solve this enigma. Here is my current test field as I work on all the conditions needed to handle grade noise and delay. I expect to finalize this over the next few weeks. My primary device is an EDGE 1050, but I also have an EDGE 1030 and Forerunner 955 to test on, which should cover most of the barometric sensor types out there - which is the cause of most of the issues. https://apps.garmin.com/apps/f0ddadf0-4eb1-41fc-ae62-3b94575109ab