Announcement

Collapse
No announcement yet.

How to calculate grade

Collapse
X
  • Time
  • Show
Clear All
new posts

  • 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.
    La Prima Cycling Screen - https://apps.garmin.com/en-US/apps/f...0-f809dc520a58
    LinkedIn - https://www.linkedin.com/in/florianlungu/

  • #2
    This is my solution, but question is how is it accurate..

    Code:
    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);
       
          }
    
    }

    Comment


    • #3
      Originally posted by florianlungu View Post
      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

      Comment

      Working...
      X