SetData problem

I'm trying to use the fitContrubutor and i god this error 

Could not find symbol 'setData'

i got this

on strings

<string id="estimatedPowerChart">EstimatedPowerChart</string>
<string id="estimatedPowerData">EstimatedPowerData</string>
<string id="estimatedPowerUnits"> </string>

on fitcontributor

<resources>
  <fitContributions>
    <fitField id="0"
        displayInChart="true"
        displayInActivityLaps="false"
        displayInActivitySummary="true"
        sortOrder="0"
        precision="0"
        chartTitle="@Strings.estimatedPowerChart"
        dataLabel="@Strings.estimatedPowerData"
        unitLabel="@Strings.estimatedPowerUnits"
        fillColor="#0fc491" />
  </fitContributions>
</resources>


On the view file:

using Toybox.FitContributor;
using Toybox.ActivityRecording;

var estimatedPower;
var session;

function setupField(session) {
  estimatedPower = session.createField("estimated_Power", 0, FitContributor.DATA_TYPE_FLOAT, {
      :mesgType=>FitContributor.MESG_TYPE_RECORD,
      :units=>"W"
  });
  estimatedPower.setData(0.0);
}

on compute:

estimatedPower = lastPower.toFloat();
estimatedPower.setData(lastPower.toNumber());
return estimatedPower;


what i'd doing wrong??

on manifest file i have only

user profile

fit contributor

 

  • i used moxy field and use a fitcontributor file and the chart is now displayed with no data i got this on the file

    using Toybox.WatchUi;
    using Toybox.System;
    using Toybox.FitContributor;
    using Toybox.ActivityRecording;
    
    class PowerEdgeFieldContributor {
    
        var estimatedPower;
    
        function initialize(dataField) {
            estimatedPower = dataField.createField("estimated_Power", 0, FitContributor.DATA_TYPE_UINT16, {
                :mesgType=>FitContributor.MESG_TYPE_RECORD,
                :units=>"W"
            });
            estimatedPower.setData(0.0);
        }
    
        function compute(lastPower) {
            estimatedPower = lastPower.toFloat();
            estimatedPower.setData(lastPower.toNumber());
            return estimatedPower;
        }
    }

  • - I've modified your posts a bit to use code blocks so that they will be more readable. Hope you don't mind.

  • It might be helpful to provide the data from the eclipse console that shows the stack trace. It isn't clear if you're getting the error from compute, or from setupField. My guess is that you're seeing it in compute, most likely because setupField has not yet been called.

    Given the code in your first post, it appears that you're trying to write a watch-app, but watch apps don't call compute on anything. Given that and the code from your second post, I'm lead to believe you're trying to implement a datafield app.

    I'm fairly certain the reason you're getting no data is because nothing is calling compute on your contributor class, so you're never updating the data in the FIT file. I'd expect something like this:

    using Toybox.Application;
    using Toybox.FitContributor;
    using Toybox.WatchUi;
    
    class PowerEdgeFieldContributor {
    
        hidden var _powerField;
        hidden var _powerValue;
    
        function initialize(dataField) {
            _powerValue = 0;
    
            _powerField = dataField.createField("estimated_Power", 0, FitContributor.DATA_TYPE_UINT16, {
                :mesgType=>FitContributor.MESG_TYPE_RECORD,
                :units=>"W"
            });
            _powerField.setData(_powerValue);
        }
    
        function compute(currentPower) {
            if (currentPower == null) {
                // don't forget to handle null in case sensor connection is lost
                // or no sensor is found
                _powerValue = 0;
            }
            else {
                // do your calculation here. i'm just doing an average of
                // the current power and the previous average power for
                // funzies. yours will hopefully be better than mine.
                _powerValue = (_powerValue + currentPower) / 2;
            }
    
            _powerField.setData(_powerValue.toNumber());
    
            return _powerValue.toFloat();
        }
    }
    
    class PowerEdgeField extends WatchUi.SimpleDataField
    {
        hidden var _powerContributor;
    
        function initialize() {
            SimpleDataField.initialize();
            label = "Power Edge";
    
            _powerContributor = new PowerEdgeFieldContributor(self);
        }
    
        function compute(info) {
            return _powerContributor.compute(info.currentPower);
        }
    }
    
    class PowerEdgeApp extends Application.AppBase
    {
        function initialize() {
            AppBase.initialize();
        }
    
        function getInitialView() {
            return [ new PowerEdgeField() ];
        }
    }

    Of course if you look at that code, you can probably see that the code from PowerEdgeFieldContributor could just be folded into PowerEdgeField, but that is another issue entirely.

  • thanks a lot!!!!! you ara very kind i'll try

  • i copied all exactly as you write it and the charts is still black

    i think the problem is here

    i can not do the _powerValue here so is in the view file so i changed this string

    _powerValue = (_powerValue + currentPower) / 2;

    for this string

    _powerValue = lastPower;

    trying to get this lastPower value from the view file

    is this correct?

  • is this correct?

    I have no idea. The code I posted above passes the power data from the DataField (the View class) to the contributor as a parameter called currentPower, so it is correct in my code.

  • I tweaked this to be a watch-app so that I could create a .FIT file and look at it using MonkeyGraph. Here is my test code:

    using Toybox.Application;
    using Toybox.FitContributor;
    using Toybox.WatchUi;
    
    class PowerEdgeFieldContributor
    {
        hidden var _powerField;
        hidden var _powerValue;
    
        function initialize(fitContributor) {
            _powerValue = 0;
    
            _powerField = fitContributor.createField("estimated_Power", 0, FitContributor.DATA_TYPE_UINT16, {
                :mesgType=>FitContributor.MESG_TYPE_RECORD,
                :units=>"W"
            });
            _powerField.setData(_powerValue);
        }
    
        function compute(currentPower) {
            if (currentPower == null) {
                // don't forget to handle null in case sensor connection is lost
                // or no sensor is found
                _powerValue = 0;
            }
            else {
                // do your calculation here. i'm just returning the current
                // power directly from the provided sensor data
                _powerValue = currentPower;
            }
    
            _powerField.setData(_powerValue.toNumber());
    
            return _powerValue.toFloat();
        }
    }
    
    class PowerEdgeModel
    {
        hidden var _fitSession;
    	hidden var _fitContributor;
    	hidden var _fitValue;
    	
    	function onStart() {
    		Sensor.enableSensorEvents(self.method(:onSensor));
    	}
    
    	function onStop() {
    		Sensor.enableSensorEvents(null);
    	}
    
    	function onSelect() {
      		if ((_fitSession == null) || !_fitSession.isRecording()) {
    			
           		_fitSession = ActivityRecording.createSession({
           			:name=>"Generic",
     				:sport=>ActivityRecording.SPORT_GENERIC,
     				:subSport=>ActivityRecording.SUB_SPORT_GENERIC
       			});
       			_fitContributor = new PowerEdgeFieldContributor(_fitSession);
       			
       			_fitSession.start();
       		}
       		else {
           		_fitSession.stop();
       			_fitSession.save();
       			_fitSession = null;
       			_fitContributor = null;
           }
    
    	   return true;
    	}
    	
    	function onSensor(info) {
    		if (_fitContributor) {
    			_fitValue = _fitContributor.compute(info.power);
    			WatchUi.requestUpdate();
    		}
    	}
    	
    	function getValue() {
    		return _fitValue;
    	}
    }
    
    class PowerEdgeDelegate extends WatchUi.BehaviorDelegate
    {
    	hidden var _model;
    	
    	function initialize(model) {
    		BehaviorDelegate.initialize();
    		_model = model;
    	}
    	
    	function onSelect() {
    		return _model.onSelect();		
    	}
    }
    
    class PowerEdgeField extends WatchUi.View
    {
    	hidden var _model;
    
        function initialize(model) {
            View.initialize();
            _model = model;
        }
        
        function onUpdate(dc) {
        	dc.setColor(Graphics.COLOR_WHITE, Graphics.COLOR_BLACK);
        	dc.clear();
        	
        	var cx = dc.getWidth() / 2;
        	var cy = dc.getHeight() / 2;
        	
        	var text = _model.getValue();
        	if (text == null) {
        		text = "--";
        	}
        	else {
        		text = text.toString();
        	}
    
        	dc.drawText(cx, cy, Graphics.FONT_MEDIUM, text, Graphics.TEXT_JUSTIFY_CENTER);
        }
    }
    
    class PowerEdgeApp extends Application.AppBase
    {
        function initialize() {
            AppBase.initialize();
        }
        
        hidden var _model;
        
        function onStart(state) {
         	_model = new PowerEdgeModel();
         	_model.onStart();
        }
        
        function onStop(state) {
        	_model.onStop();
        	_model = null;
        }
    
        function getInitialView() {
            return [ new PowerEdgeField(_model), new PowerEdgeDelegate(_model) ];
        }
    }
    

    Here is the resulting MonkeyGraph:

    If you're testing on a watch, you shouldn't need to do this. You should just be able to use the code from my first post.

  • Now it's working!!!! i can't believe it!!!!! thanks a lot men!!! i have a mention for you on my app store really thanks i'm very happy!!!!

    apps.garmin.com/.../3b33a74f-341a-40ef-854e-69acd180a0b9

  • just one more question, how can i obtain average estimated power?? how can i collect all data and divided by distance or by time?

  • // n is distance or time
    average = average + ((new_sample - average) / n);