Corrupted Data Values Received From Logger

edited October 2021 in Android

I've been developing an app to use an Mbient MMRL board (along with some added circuitry) to periodically measure the resistance of a sensor and store the resulting data in the onboard flash module for later retrieval. This seemed fairly straightforward, and I'm able to successfully route data to the flash and download it via the Logger module.

However, I noticed that the resistance values I was getting back were much lower than expected. Curiously, when I stream the data directly to my android device, I get the correct values.

My code for configuring the GPIO and Logger modules is as follows:

private void configureModules(){
        gpio = metawear.getModule(Gpio.class);
        currentEn = gpio.pin(CURR_EN_PIN);

        currentEn.setPullMode(Gpio.PullMode.PULL_UP); // Ensure current driver is off by default
        currentEn.setOutput(); // Turn off the current driver

        adc = gpio.pin((byte) SENSE_PIN).analogAbsRef();

        timer = metawear.getModule(Timer.class); // Used to drive ADC sampling at a fixed interval

        log = metawear.getModule(Logging.class);
    }

private void configureRoutes(){
        // Setup a subscription to the ADC data stream
        adc.addRouteAsync(new RouteBuilder() {
            @Override
            public void configure(RouteComponent source) {
                source.log(null);
            }
        }).continueWithTask(task -> (gpio.getVirtualPin((byte) 0x15)).analogAbsRef().addRouteAsync(source -> source.log((Subscriber) (data, env) -> {
            float val = data.value(Float.class);
            val /= CURRENT;
            receiveADC(val);
            Log.v("MainActivity", "measured resistance = " + val);
        })));

        // Command to execute when the on-board timer fires
        //  The enclosed function lowers CURR_EN_PIN and delays 100us before reading the ADC
        CodeBlock adcRead = () -> adc.read((byte) Gpio.UNUSED_READ_PIN, (byte) CURR_EN_PIN, (short) 100, (byte) 0x15); 

        // Setup the timer to fire at 25Hz and trigger an ADC read
        timer.scheduleAsync(40, true, adcRead)
                .continueWithTask(timerTask -> {
                    Timer.ScheduledTask scheduledTask = timerTask.getResult();

                    if (scheduledTask == null) {
                        if (timerTask.getError() != null)
                            Log.v("MainActivity", "Error in timer task result: " + timerTask.getError());
                    }
                    else
                        scheduledTask.start();
                    return null;
                });

    }

And for downloading the logged data:

private void downloadLog(){
        log.stop();
        log.flushPage();

        log.downloadAsync(100, new Logging.LogDownloadUpdateHandler() {
            @Override
            public void receivedUpdate(long nEntriesLeft, long totalEntries) {
                Log.i("MainActivity", "Progress Update = " + nEntriesLeft + "/" + totalEntries);
                int progress = (int)((((float)totalEntries - (float)nEntriesLeft) / (float)totalEntries) * 100);
                if(dataConnection != null)
                    dataConnection.onDownloadProgressReceived(progress);
            }
        }, new Logging.LogDownloadErrorHandler() {
            @Override
            public void receivedError(Logging.DownloadError errorType, byte logId, Calendar timestamp, byte[] data) {
                Log.i("MainActivity", "Error: " + errorType.toString());
            }
        }).continueWithTask(new Continuation<Void, Task<Void>>() {
            @Override
            public Task<Void> then(Task<Void> task) throws Exception {
                Log.i("MainActivity", "Download completed");
                dataFile.buildFileAndSave();
                dataConnection.onDownloadProgressReceived(0);
                log.start(true);
                return null;
            }
        });
 }

When streaming the data, I get back resistance values clustered around 4ohms, as expected. When logging, the data is centered around about 1.3ohms.

It's clear to me that either 1) I am mistaken about the interchangeability of RouteComponent.log and RouteComponent.stream or 2) the Logger module is corrupting the data in some way, either due to improper configuration or because of some flaw in the design of the firmware.

Does anyone have any ideas as to why I might be experiencing this and how I can go about correcting the problem?

Sign In or Register to comment.