Append data to array

Hi all,

I'd like to calculate the percentage grade in my data field and thought of the following:

Use two arrays of defined size and populate them with rise and run (e.g. over the course of 5 seconds), then start calculating grade. Each consecutive second, drop the first value and append the most recent value to both arrays, then recalculate grade.

I'm having some trouble finding out how to do the filling of the arrays though, can someone point me in the right direction?

Thanks in advance for any help!
  • I know, your grade is only a rough estimate... but, if you are using Toybox::Position::Info, shouldn't you care about the "when"?
  • I know, your grade is only a rough estimate... but, if you are using Toybox::Position::Info, shouldn't you care about the "when"?


    Could you maybe elaborate on that? I think taking an average over the last 4 seconds will give me a good approximation of the slope, maybe I should even consider extending the time span. I don't want the numbers to jump around all the time due to e.g. GPS signal noise.. Would be interested in hearing your thoughts
  • Could you maybe elaborate on that? I think taking an average over the last 4 seconds will give me a good approximation of the slope, maybe I should even consider extending the time span. I don't want the numbers to jump around all the time due to e.g. GPS signal noise.. Would be interested in hearing your thoughts


    Well, you are calculating distance with measuring speed * 1s ... but if the speed is maintained more then 1s, obviously the distance is longer, so you would have to calculate with the correct timespan.
  • Well, you are calculating distance with measuring speed * 1s ... but if the speed is maintained more then 1s, obviously the distance is longer, so you would have to calculate with the correct timespan.


    The calculation is actually correct as it is now, as I'm summing the 4 different speeds over the last 4 seconds.
    The grade is then calculated by dividing the rise over the run (over the last 4 consecutive seconds)
  • but you are assuming, that the speed is kept over exactly one second
  • metrics are provided once a second, so no other option there. again, by averaging over longer period of time, this effectively becomes a non-issue..
  • I'm trying to replicate what you have done with the below but I'm getting the error message:
    "Could not find symbol mod." what am I missing? It doesn't appear to be in Toybox.Math?

    using Toybox.WatchUi as Ui;
    using Toybox.System as Sys;
    using Toybox.Time as Time;
    using Toybox.Math as mth;

    class DataField extends Ui.SimpleDataField {



    var arrayDistance = new [5];
    var arrayAltitude = new [5];
    var arrayIndex = 0;
    // Constructor
    function initialize() {
    label = "Grade";


    }

    // Handle the update event
    function compute(info) {
    // Calculate current slope
    if (info.altitude != null && info.currentSpeed != null) {
    // if we haven't got any data, fill with the current data
    if (arrayDistance[arrayIndex] == null) {
    for (var i = 0; i < arrayDistance.size(); ++i) {
    arrayDistance= info.currentSpeed;
    }

    for (var i = 0; i < arrayAltitude.size(); ++i) {
    arrayAltitude= info.altitude;
    }
    }
    // fill arrays
    arrayAltitude[arrayIndex] = info.altitude;
    arrayDistance[arrayIndex] = info.currentSpeed;

    // calculate altitude change over last 4 seconds
    var altitudeChange = (arrayAltitude[arrayIndex] - arrayAltitude[mod((arrayIndex + 1), arrayAltitude.size())]);

    // calculate horizontal displacement over last 4 seconds
    var distanceChange = 0;

    for (var i = arrayIndex; i > (arrayIndex - (arrayDistance.size() - 1)); --i) {
    var idx = mod(i, arrayDistance.size());
    distanceChange += arrayDistance[idx];
    }
    // calculate percent grade
    valueGrade = (altitudeChange * 100) / distanceChange;

    // advance to next array index
    arrayIndex = mod((arrayIndex + 1), arrayAltitude.size());
    }
    else {
    valueGrade = 0;
    }
    return valueGrade;
    }
    }[/CODE]
  • Could not find symbol mod. what am I missing? It doesn't appear to be in Toybox.Math?

    There has never been a function in the Math module named mod(). Looking at the code, the function mod() is just an integer modulus, so you could write your own function pretty easily...

    function mod(a, b) {
    return a % b;
    }


    Or, you could just write it in place of the function call and eliminate the unnecessary overhead.

    Travis