No livro TinyML: Machine Learning with TensorFlow Lite on Arduino and Ultra-Low-Power Microcontrollers de Pete Warden, Daniel Situnayake é apresentado elemento básicos para implementar a rede neural para microcontroladores.
Os autores apresentam uma receitinha básica. Organizei essa receita num classe, o que facilita a integração em projetos mais gerais. O TensorFlow lite, junto dessa classe de redes neurais para o ESP32 está disponível no meu git hub no endereço https://github.com/duducosmos/neuroesp32
Salve o conteúdo do repositório anterior na pasta lib do seu projeto:

Aqui irei fazer uma breve descrição da classe. O código abaixo é o header da nossa classe:
#ifndef __NeuralNetwork__
#define __NeuralNetwork__
#include <stdint.h>
namespace tflite
{
template <unsigned int tOpCount>
class MicroMutableOpResolver;
class ErrorReporter;
class Model;
class MicroInterpreter;
} // namespace tflite
struct TfLiteTensor;
class NeuralNetwork
{
private:
tflite::MicroMutableOpResolver<10> *resolver;
tflite::ErrorReporter *error_reporter;
const tflite::Model *model;
tflite::MicroInterpreter *interpreter;
TfLiteTensor *input;
TfLiteTensor *output;
uint8_t *tensor_arena;
public:
NeuralNetwork(const unsigned char *custom_model);
float *getInputBuffer();
NeuralNetwork();
int predict();
};
#endif
Code language: PHP (php)
O arquivo cpp correspondente ao header anterior é:
#include "NeuralNetwork.h"
#include "tensorflow/lite/micro/all_ops_resolver.h"
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "tensorflow/lite/version.h"
const int kArenaSize = 20000;
NeuralNetwork::NeuralNetwork(const unsigned char *custom_model)
{
error_reporter = new tflite::MicroErrorReporter();
model = tflite::GetModel(custom_model);
if (model->version() != TFLITE_SCHEMA_VERSION)
{
TF_LITE_REPORT_ERROR(error_reporter, "Model provided is schema version %d not equal to supported version %d.",
model->version(), TFLITE_SCHEMA_VERSION);
return;
}
// This pulls in the operators implementations we need
resolver = new tflite::MicroMutableOpResolver<10>();
resolver->AddFullyConnected();
resolver->AddMul();
resolver->AddAdd();
resolver->AddLogistic();
resolver->AddReshape();
resolver->AddQuantize();
resolver->AddDequantize();
tensor_arena = (uint8_t *)malloc(kArenaSize);
if (!tensor_arena)
{
TF_LITE_REPORT_ERROR(error_reporter, "Could not allocate arena");
return;
}
// Build an interpreter to run the model with.
interpreter = new tflite::MicroInterpreter(
model, *resolver, tensor_arena, kArenaSize, error_reporter);
// Allocate memory from the tensor_arena for the model's tensors.
TfLiteStatus allocate_status = interpreter->AllocateTensors();
if (allocate_status != kTfLiteOk)
{
TF_LITE_REPORT_ERROR(error_reporter, "AllocateTensors() failed");
return;
}
size_t used_bytes = interpreter->arena_used_bytes();
TF_LITE_REPORT_ERROR(error_reporter, "Used bytes %d\n", used_bytes);
// Obtain pointers to the model's input and output tensors.
input = interpreter->input(0);
output = interpreter->output(0);
}
float *NeuralNetwork::getInputBuffer()
{
return input->data.f;
}
float NeuralNetwork::predict()
{
interpreter->Invoke();
return output->data.f[0];
}
Code language: PHP (php)
Essa é uma classe básica, para casos mais gerais será necessário modificar o método predict. Num post futuro irei mostrar como modificar esse método para problemas de classificação.
Além disso a classe carrega apenas os operadores necessárias para o problema. O seguinte trecho de código indica quais operadores utilizadas:
resolver = new tflite::MicroMutableOpResolver<10>();
resolver->AddFullyConnected();
resolver->AddMul();
resolver->AddAdd();
resolver->AddLogistic();
resolver->AddReshape();
resolver->AddQuantize();
resolver->AddDequantize();
Code language: PHP (php)
Adicionar mais operadores irá aumentar o tamanho do código compilado, como nosso sistema é bem limitado em memória, compensa muito avaliar no modelo criado anteriormente quais são os operadores que realmente iremos utilizar. Você pode customizar esse trecho de código para adicionar o que for necessário.
No post anterior (http://www.trilhaspython.com.br/2020/11/08/tensorflow-lite-no-esp32-parte-2/) utilizamos o Keras para criar o modelo e exporta a nossa rede neural para o código c++.
Vamos criar dois arquivos, um cahamado model_data.cc e outro com o nome model_data.h na pasta src do projeto:

O conteúdo do arquivo model_data.h será:
extern unsigned char hcsr04_1_tflite[];
Code language: CSS (css)
Já no arquivo model_data.cpp iremos copiar o modelo que geramos anteriormente. O código resultante será:
unsigned char hcsr04_1_tflite[] = {
0x1c, 0x00, 0x00, 0x00, 0x54, 0x46, 0x4c, 0x33, 0x00, 0x00, 0x12, 0x00,
0x1c, 0x00, 0x04, 0x00, 0x08, 0x00, 0x0c, 0x00, 0x10, 0x00, 0x14, 0x00,
0x00, 0x00, 0x18, 0x00, 0x12, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0xd8, 0x05, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x68, 0x01, 0x00, 0x00,
0x34, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0xd0, 0xfa, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00,
0x0b, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x6d, 0x69, 0x6e, 0x5f,
0x72, 0x75, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x5f, 0x76, 0x65, 0x72, 0x73,
0x69, 0x6f, 0x6e, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x28, 0x01, 0x00, 0x00,
0x14, 0x01, 0x00, 0x00, 0xec, 0x00, 0x00, 0x00, 0xcc, 0x00, 0x00, 0x00,
0x7c, 0x00, 0x00, 0x00, 0x5c, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00,
0x4c, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
0x1c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x1e, 0xff, 0xff, 0xff,
0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x31, 0x2e, 0x35, 0x2e,
0x30, 0x00, 0x00, 0x00, 0xd4, 0xfa, 0xff, 0xff, 0xd8, 0xfa, 0xff, 0xff,
0x3a, 0xff, 0xff, 0xff, 0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x11, 0xa1, 0x3d, 0x40, 0xf8, 0xfa, 0xff, 0xff, 0xfc, 0xfa, 0xff, 0xff,
0x5e, 0xff, 0xff, 0xff, 0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
0x16, 0x24, 0x1b, 0xbf, 0xbc, 0x66, 0xe2, 0xbe, 0xc6, 0xb0, 0x4c, 0xbf,
0xec, 0xa0, 0xe8, 0x3e, 0x7a, 0xff, 0xff, 0xff, 0x04, 0x00, 0x00, 0x00,
0x40, 0x00, 0x00, 0x00, 0x4d, 0xe1, 0x44, 0x3f, 0x5e, 0xb3, 0xa4, 0xbe,
0x50, 0x9b, 0x9a, 0xbd, 0x3d, 0xb9, 0x53, 0x3e, 0x58, 0x4d, 0xbc, 0xbd,
0xb0, 0xda, 0x05, 0xbf, 0x84, 0xde, 0x62, 0x3e, 0xf0, 0x96, 0xf7, 0xbe,
0x78, 0xd2, 0x99, 0xbe, 0x64, 0x4d, 0x72, 0x3e, 0x2b, 0xd4, 0x48, 0x3f,
0xe4, 0xbb, 0x67, 0x3f, 0xac, 0x33, 0x37, 0xbf, 0xe4, 0xd8, 0x0b, 0xbe,
0x56, 0x98, 0xd1, 0x3e, 0x2d, 0x10, 0x29, 0x3e, 0xc6, 0xff, 0xff, 0xff,
0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0xac, 0x49, 0x52, 0xbe,
0x58, 0xb9, 0x1f, 0x3e, 0xe9, 0xc3, 0x09, 0x3f, 0x82, 0x12, 0xe7, 0xbe,
0xe2, 0xff, 0xff, 0xff, 0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
0xc5, 0x6d, 0x76, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x19, 0x1e, 0x72, 0x40,
0x25, 0x86, 0x74, 0xc0, 0x00, 0x00, 0x06, 0x00, 0x08, 0x00, 0x04, 0x00,
0x06, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x55, 0xd9, 0x72, 0x40, 0xb8, 0xfb, 0xff, 0xff, 0x0f, 0x00, 0x00, 0x00,
0x54, 0x4f, 0x43, 0x4f, 0x20, 0x43, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x74,
0x65, 0x64, 0x2e, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
0x0c, 0x00, 0x14, 0x00, 0x04, 0x00, 0x08, 0x00, 0x0c, 0x00, 0x10, 0x00,
0x0c, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0xe4, 0x00, 0x00, 0x00,
0xd8, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x90, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0xce, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x08, 0x18, 0x00, 0x00, 0x00,
0x0c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x1c, 0xfc, 0xff, 0xff,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x07, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
0x00, 0x00, 0x0e, 0x00, 0x14, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0c, 0x00,
0x07, 0x00, 0x10, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
0x1c, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0xba, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00,
0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00,
0x16, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0c, 0x00, 0x07, 0x00, 0x10, 0x00,
0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x24, 0x00, 0x00, 0x00,
0x18, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
0x08, 0x00, 0x07, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x10, 0x03, 0x00, 0x00,
0xa4, 0x02, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0xf4, 0x01, 0x00, 0x00,
0xac, 0x01, 0x00, 0x00, 0x48, 0x01, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00,
0xb4, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x26, 0xfd, 0xff, 0xff, 0x3c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x0c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x18, 0xfd, 0xff, 0xff,
0x22, 0x00, 0x00, 0x00, 0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x74, 0x69,
0x61, 0x6c, 0x5f, 0x31, 0x34, 0x2f, 0x64, 0x65, 0x6e, 0x73, 0x65, 0x5f,
0x33, 0x39, 0x2f, 0x4d, 0x61, 0x74, 0x4d, 0x75, 0x6c, 0x5f, 0x62, 0x69,
0x61, 0x73, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x6e, 0xfd, 0xff, 0xff, 0x50, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
0x0c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x60, 0xfd, 0xff, 0xff,
0x36, 0x00, 0x00, 0x00, 0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x74, 0x69,
0x61, 0x6c, 0x5f, 0x31, 0x34, 0x2f, 0x64, 0x65, 0x6e, 0x73, 0x65, 0x5f,
0x33, 0x39, 0x2f, 0x4d, 0x61, 0x74, 0x4d, 0x75, 0x6c, 0x2f, 0x52, 0x65,
0x61, 0x64, 0x56, 0x61, 0x72, 0x69, 0x61, 0x62, 0x6c, 0x65, 0x4f, 0x70,
0x2f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x73, 0x65, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0xce, 0xfd, 0xff, 0xff, 0x34, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
0x0c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xc0, 0xfd, 0xff, 0xff,
0x1b, 0x00, 0x00, 0x00, 0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x74, 0x69,
0x61, 0x6c, 0x5f, 0x31, 0x34, 0x2f, 0x64, 0x65, 0x6e, 0x73, 0x65, 0x5f,
0x33, 0x38, 0x2f, 0x52, 0x65, 0x6c, 0x75, 0x00, 0x02, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x12, 0xfe, 0xff, 0xff,
0x3c, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x04, 0xfe, 0xff, 0xff, 0x22, 0x00, 0x00, 0x00,
0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x5f, 0x31,
0x34, 0x2f, 0x64, 0x65, 0x6e, 0x73, 0x65, 0x5f, 0x33, 0x38, 0x2f, 0x4d,
0x61, 0x74, 0x4d, 0x75, 0x6c, 0x5f, 0x62, 0x69, 0x61, 0x73, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x5a, 0xfe, 0xff, 0xff,
0x50, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x4c, 0xfe, 0xff, 0xff, 0x36, 0x00, 0x00, 0x00,
0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x5f, 0x31,
0x34, 0x2f, 0x64, 0x65, 0x6e, 0x73, 0x65, 0x5f, 0x33, 0x38, 0x2f, 0x4d,
0x61, 0x74, 0x4d, 0x75, 0x6c, 0x2f, 0x52, 0x65, 0x61, 0x64, 0x56, 0x61,
0x72, 0x69, 0x61, 0x62, 0x6c, 0x65, 0x4f, 0x70, 0x2f, 0x74, 0x72, 0x61,
0x6e, 0x73, 0x70, 0x6f, 0x73, 0x65, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xba, 0xfe, 0xff, 0xff,
0x34, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0xac, 0xfe, 0xff, 0xff, 0x1b, 0x00, 0x00, 0x00,
0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x5f, 0x31,
0x34, 0x2f, 0x64, 0x65, 0x6e, 0x73, 0x65, 0x5f, 0x33, 0x37, 0x2f, 0x52,
0x65, 0x6c, 0x75, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0xfe, 0xfe, 0xff, 0xff, 0x3c, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0xf0, 0xfe, 0xff, 0xff, 0x22, 0x00, 0x00, 0x00, 0x73, 0x65, 0x71, 0x75,
0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x5f, 0x31, 0x34, 0x2f, 0x64, 0x65,
0x6e, 0x73, 0x65, 0x5f, 0x33, 0x37, 0x2f, 0x4d, 0x61, 0x74, 0x4d, 0x75,
0x6c, 0x5f, 0x62, 0x69, 0x61, 0x73, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00, 0x46, 0xff, 0xff, 0xff, 0x50, 0x00, 0x00, 0x00,
0x05, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x38, 0xff, 0xff, 0xff, 0x36, 0x00, 0x00, 0x00, 0x73, 0x65, 0x71, 0x75,
0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x5f, 0x31, 0x34, 0x2f, 0x64, 0x65,
0x6e, 0x73, 0x65, 0x5f, 0x33, 0x37, 0x2f, 0x4d, 0x61, 0x74, 0x4d, 0x75,
0x6c, 0x2f, 0x52, 0x65, 0x61, 0x64, 0x56, 0x61, 0x72, 0x69, 0x61, 0x62,
0x6c, 0x65, 0x4f, 0x70, 0x2f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f,
0x73, 0x65, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0xa6, 0xff, 0xff, 0xff, 0x48, 0x00, 0x00, 0x00,
0x07, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
0x08, 0x00, 0x0c, 0x00, 0x04, 0x00, 0x08, 0x00, 0x08, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x7f, 0x43, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0e, 0x00, 0x00, 0x00, 0x64, 0x65, 0x6e, 0x73, 0x65, 0x5f, 0x33, 0x37,
0x5f, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00,
0x14, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x0c, 0x00, 0x10, 0x00,
0x0e, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00,
0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x49, 0x64, 0x65, 0x6e,
0x74, 0x69, 0x74, 0x79, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x07, 0x00,
0x00, 0x00, 0x08, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,
0x03, 0x00, 0x00, 0x00
};
unsigned int hcsr04_1_tflite_len = 1564;
Vamos unificar tudo no arquivo main.cpp que está na pasta src:
#include <Arduino.h>
#include "model_data.h"
#include "NeuralNetwork.h"
NeuralNetwork *nn;
const int trigPin = 4; //D4
const int echoPin = 2; //D3
long duration;
void neuralf();
void gen_data();
int get_duration();
void setup()
{
Serial.begin(9600);
nn = new NeuralNetwork(hcsr04_1_tflite);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
neuralf();
}
int get_duration()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
return duration;
}
void gen_data()
{
Serial.println(get_duration());
delay(100);
}
void neuralf()
{
int duration = get_duration();
nn->getInputBuffer()[0] = duration;
float predicted = nn->predict();
float dist = duration * 0.343 / 2;
Serial.printf("Pred %.2f Calculated %.2f \n", predicted, dist);
delay(1000);
}
Code language: PHP (php)
Observe que no seguinte código estamos instanciando a nossa rede neural a partir do modelo que criamos previamente:
nn = new NeuralNetwork(hcsr04_1_tflite);
Code language: JavaScript (javascript)
No código abaixo utilizamos a rede neural para fazer previsões. Além disso, comparamos o resultado com o que é esperado assumindo o valor da velocidade do som.
void neuralf()
{
int duration = get_duration();
nn->getInputBuffer()[0] = duration;
float predicted = nn->predict();
float dist = duration * 0.343 / 2;
Serial.printf("Pred %.2f Calculated %.2f \n", predicted, dist);
delay(1000);
}
Code language: PHP (php)
A vantagem de se utilizar uma rede neural para esse caso está no fato de que estamos embutindo no nosso sistema o tratamento de erros, provenientes de variações de tensão e qualidade do sensor. A rede neural irá fornecer um resultado com baixa variância para a determinação da distância, se comparado com o método de cálculo direto. A desvantagem está na utilização de memória. Contudo, com a classe de rede neurais, podemos ter várias micro redes integrando sensores diversos. Sensores de cores, distância, câmeras, em que cada um tem sua rede neural e o nosso sistema integra os diversos componentes.
Temos ai um micro cérebro cibernético conectado a internet.