ANT+ (ebike) Datafield for EDGE - Problem to reinitialize ANT channel after wakeup from sleep

I built a small datafield to learn about ANT+ for ebikes (LEV - Light Electric Vehicel) on the base of the sample "Moxy Datafield".

My test datafield works pretty well - but there is a (small) problem:
When the Edge was sent to sleep - after wakeup the ANT channel does not exist anymore and needs to be re-initialized.

GARMIN says (years ago...):
...the issue is that when the Edge enters low power, we power down the ANT module and forget the state of all running channels (we also reset the state of the ANT radio stack). Then when the Edge exits low power mode the data field expects its channel to still be open. Calling open/close after exiting low power wouldn't work in this case as the original channel would have been reset.
Until we get a fix in for this an app developer will need to set up a watchdog for their channel where if they don't receive a message for X seconds (with X being larger than their search timeout), the developer will reinitialize their channel.

So, I built a watchdog in view.mc which works.

        /// WATCHDOG:
        //lastMessage = sensor.getData().getLastMessage();  // see line above!
        timeNow = Time.now().value();
        if (lastMessage != null) {
            if ( lastMessage < timeNow - 5 ) {
                // WatchDog barks
                levStatus = 0;
                lastMessage = null;
                // now reInitialize the ANT channel - BUT HOW TO DO?????
            } 
        }

But I don't find a way to call a re-initialize of the ANT channel in LEVSensor.mc.

I submit the whole (small) datafiedl, so - if you want - can better see.

LEVField.zip

And a screenshot of the Edge for better information:

I would be glad if any of you can show me the solution.

Many thanks in advance!

Top Replies

All Replies

  • Thank you both! I will give it a try that way.

  • Dear all, thank you for your help - but I can't manage :-(

    My goal:
    the eBike connection should be rebuilt:
    -after simply switching the ebike off and on (eg during a ride) - works also without "watchdog" enabled.
    -after waking up the Edge from sleep

    I now have a solution that enables both - but only to a limited extent: the Edge device must first be sent to sleep mode and only then should the eBike be switched off (to have var levStatus = 1 / connected). For wake-up, the procedure must be reversed.
    I am attaching the entire small project here again in case anyone would like to take a closer look. This is the version which works - limited.

    1258.LEVField.zip

    I have also done tests as suggested by  and  , but I can't get any further here...

    I added to the function reinitializeSensor() in App a _sensor.closeSensor() to rebuild the sensor in any case. To have this function called as soon a timeout of more than 10 sec exists I have deactivated "lastMessage = null;" because if I do not, the function is only called once - and if the bike is not ready yet, it will never be called.

    But this leads to an appcrash on line reinitializeSensor() - I assume that this function is called more than one time because timeout will be corrected only if a connection was established, which can take some seconds.

    ErrorLog:

    ---
    Error: Unhandled Exception
    Time: 2023-11-26T11:58:09Z
    Part-Number: 006-B3843-00
    Firmware-Version: '19.19'
    Language-Code: deu
    ConnectIQ-Version: 4.2.3
    Filename: LEVField
    Appname: LEVField
    Stack: 
      - pc: 0x1000038f
        File: 'C:\Garmin_IQ_Projekte\LEVField\source\LEVSensor.mc'
        Line: 141
        Function: initialize
      - pc: 0x10000cfc
        File: 'C:\Garmin_IQ_Projekte\LEVField\source\LEVFieldApp.mc'
        Line: 59
        Function: reinitializeSensor
      - pc: 0x10000837
        File: 'C:\Garmin_IQ_Projekte\LEVField\source\LEVFieldView.mc'
        Line: 99
        Function: compute

    If it is not too much to ask, I would like to ask @maca88 to look into the code this time and maybe give me a hint.

    In any way - thank you both for your help!

  • 1258.LEVField_V2.zip

    I did the following modifications to the code:

    1. Increased the time for re-init the sensor to 30 seconds. You should re-init the sensor only if you detect that "LEVSensor.onMessage" is not called anymore. You set "searchTimeoutLowPriority" to 10, which means that every 25 seconds the "LEVSensor.onMessage" should be called with event "MSG_CODE_EVENT_CHANNEL_CLOSED" if no e-bike is found. In that case you just need to reopen the channel, not recreate it. The cannel should be recreated only if after 25 seconds event MSG_CODE_EVENT_CHANNEL_CLOSED was not triggered, which you will see by checking the lastMessage field.
    2. Used "GenericChannel.release" in "closeSensor" method, which will release the ANT channel back to the system so that you can recreate it.
    3. Uncommented the code in "reinitializeSensor" to close the current channel before creating a new one
    4. Updated "compute" method so that "lastMessage" field is set even if "sensor.isSearching() == true" as it is not desired to recreate a channel if we know that LEVSensor still works
    5. Added "_closed" field to LEVSensor in order to prevent reopening the channel after explicitly closing the channel by using "closeSensor" method

    Give it a try and let us know if it works.

  • thank you so much!

    I gave it a short try and it seems to work now! (Thanks for your comments which help me a lot to understand!)
    I will test all combinations of device shutdown/sleep/start and ebike shutdown/start tomorrow. 

    I will finally report back, but have many thanks already both of you - and @maca88 !
    Your help was just great!

  • Now I have tested different combinations of turn off and on with short and long pauses in between.
    All working perfectly.

    MANY THANKS! to  and  !