This is the "hello world" demo of TensorFlow Lite. It has a simple model that has been trained to generate a sine wave when a linear input is given. It's a good way to verify you have a working toolchain!

If you want to load demo this immediately to your PyBadge/EdgeBadge, here is the UF2 files which you can 'drag-n-drop' onto your BOOT diskdrive to load the example (follow the instructions here on how to load UF2 files if you've never done it before)

Serial plotter sine wave demo compile & upload

Let's start with the plain Arduino TensorFlow demo. Don't forget you have to perform all the steps in the previous page for installing Arduino IDE, Adafruit SAMD support, libraries, and board/port selection!

Compile & upload this example!

Upon success, you may see the LED on the board pulsing. The best way to see the output is to select the Serial Plotter

You'll see a sine wave on the plotter!

If you want to see a more sinusoidal output go to arduino_constants.cpp

and change

const int kInferencesPerCycle = 1000;

to

const int kInferencesPerCycle = 100;

Then re-upload

Arcada display output sine demo compile & upload

Arcada is our library for handling displays and input - we have so many different boards and displays, we need a unifying library that would handle displays, filesystems, buttons, etc. For many boards, you don't need to do anything special to figure out the pinouts or part numbers!

Load up the Adafruit_TFLite->hello_world_arcada example

You can upload this sketch to your board and you'll get an animated ball on the screen.

The majority of the work is in this file that initializes the display on the first inference, then draws a ball (while erasing the last location) on every successful inference.

/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "output_handler.h"
#include "Arduino.h"
#include "Adafruit_Arcada.h"
extern Adafruit_Arcada arcada;

// The pin of the Arduino's built-in LED
int led = LED_BUILTIN;

// Track whether the function has run at least once
bool initialized = false;

// helper function to let us scale floating point values nicely
double mapf(double x, double in_min, double in_max, double out_min, double out_max) {
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

// Animates a dot across the screen to represent the current x and y values
void HandleOutput(tflite::ErrorReporter* error_reporter, float x_value,
                  float y_value) {
  // Do this only once
  if (!initialized) {
    // Add some text to describe what's up!
    arcada.display->fillScreen(ARCADA_BLACK);
    arcada.display->setTextColor(ARCADA_WHITE);
    arcada.display->setTextSize(1);
    const char *header = "TensorFlow Lite";
    const char *footer = "Sine wave model";
    arcada.display->setCursor((arcada.display->width()-strlen(header)*6)/2, 0);
    arcada.display->print(header);
    arcada.display->setCursor((arcada.display->width()-strlen(footer)*6)/2, arcada.display->height()-8);
    arcada.display->print(footer);
    initialized = true;
  }

  // map the x input value (0-2*PI) and the y value (-1.5 to 1.5)
  // to the size of the display
  float pixel_x, pixel_y;
  static float last_pixel_x, last_pixel_y;
  pixel_x = mapf(x_value, 0, 2*3.1415, 0, arcada.display->width());
  pixel_y = mapf(y_value, -1.75, 1.75, 0, arcada.display->height());
  if (pixel_x == 0) {
     // clear the screen
     arcada.display->fillRect(0, 10, arcada.display->width(), arcada.display->width()-20, ARCADA_BLACK);
  }

  arcada.display->fillCircle(pixel_x, pixel_y, 3, ST77XX_RED);
  last_pixel_x = pixel_x;
  last_pixel_y = pixel_y;

  // slow it down so we can see the ball!
  delay(3);
}

This guide was first published on Jul 23, 2019. It was last updated on Jul 23, 2019.

This page (Sine Wave Demo) was last updated on May 08, 2021.

Text editor powered by tinymce.