Hello world project deploy in ESP32 Board.
# How to Create a hello world project from TF Lite:
I follow the instruction in tensorflow guideline to create this project (including components folder, main folder).
## Clone tensorflow v2.2.0
```
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
```
Switch to version 2.2.0 which is more stable for this project than newest versions.
```
git checkout v2.2.0
```
## Generate then example
The examples can be generated by the following command:
```
make -f tensorflow/lite/micro/tools/make/Makefile TARGET=esp generate_hello_world_esp_project
```
## Install ESP IDF
You can follow the instruction to install ESP-IDF to build the project in README_ESP.md.
## Building the example
Go the directory of the example you generated before with command:
```
cd tensorflow/lite/micro/tools/make/gen/esp_xtensa-esp32/prj/hello_world/esp-idf
```
To build this, run:
```
idf.py build
```
## Load and run the example
To flash (replace `/dev/ttyUSB0` with the device serial port):
```
idf.py --port /dev/ttyUSB0 flash
```
Monitor the serial output:
```
idf.py --port /dev/ttyUSB0 monitor
```
Use `Ctrl+]` to exit.
The previous two commands can be combined:
```
idf.py --port /dev/ttyUSB0 flash monitor
```
# Train a sinusoid model and create a harmonic motion with LEDs
## Train a sinusoid model
You can train a sinusoid model by many ways and can easily import the model you trained into the project above.
I suggest you to see an example for training a sinusoid model and convert it using [TensorFlow Lite and xxd](https://colab.research.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/lite/micro/examples/hello_world/train/train_hello_world_model.ipynb).
## Import your model into example project
Get your model.cc that you have from the previous step that we have convert a quantized model. Rename it into sine_model_data.cc, and paste it in **main** folder.
## Control the LED
In this project we would like to deploy our model on ESP32 Board and stimulate the harmonic motion. So I have add some basic logic handlers and control our LED through driver/gpio.h.
In main_function.cc, we have 2 majors change to setup GPIOs and control LEDs.
### Setup GPIOs
Include some libraries for GPIO and setup some GPIO that we need to use later.
```c
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
gpio_num_t pinArray[6] = { GPIO_NUM_0, GPIO_NUM_4, GPIO_NUM_16, GPIO_NUM_17, GPIO_NUM_5, GPIO_NUM_18 };
void setupGPIO() {
gpio_config_t gpioConfig;
gpioConfig.pin_bit_mask = 0;
for (int i = 0; i < 6; i++) {
gpioConfig.pin_bit_mask |= (1 << pinArray[i]);
}
gpioConfig.mode = GPIO_MODE_OUTPUT;
gpioConfig.pull_up_en = GPIO_PULLUP_DISABLE;
gpioConfig.pull_down_en = GPIO_PULLDOWN_DISABLE;
gpioConfig.intr_type = GPIO_INTR_DISABLE;
gpio_config(&gpioConfig);
}
```
Load our models with TF Lite (in setup()).
```c
static tflite::MicroErrorReporter micro_error_reporter;
error_reporter = µ_error_reporter;
model = tflite::GetModel(g_sine_model_data);
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 all the operation implementations we need.
// NOLINTNEXTLINE(runtime-global-variables)
static tflite::ops::micro::AllOpsResolver resolver;
// Build an interpreter to run the model with.
static tflite::MicroInterpreter static_interpreter(
model, resolver, tensor_arena, kTensorArenaSize, error_reporter);
interpreter = &static_interpreter;
// 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;
}
// Obtain pointers to the model's input and output tensors.
input = interpreter->input(0);
output = interpreter->output(0);
```
Basic input, output for inference.
```c
input->data.f[0] = x_val;
// Run inference, and report any error
TfLiteStatus invoke_status = interpreter->Invoke();
if (invoke_status != kTfLiteOk) {
TF_LITE_REPORT_ERROR(error_reporter, "Invoke failed on x_val: %f\n",
static_cast<double>(x_val));
return;
}
// Read the predicted y value from the model's output tensor
float y_val = output->data.f[0];
```
Create function to choose LED after running inference with our model, in this circuit board, I use 6 LEDs so I need to split into 6 equal segments in range [-1. 1]
```c
int chooseLED(float y) {
if (-1 <= y && y < -0.66) return 0;
if (-0.66 <= y && y < -0.33) return 1;
if (-0.33 <= y && y < 0) return 2;
if (0 <= y && y < 0.33) return 3;
if (0.33 <= y && y < 0.66) return 4;
if (0.66 <= y && y <= 1) return 5;
return -1;
}
```
Now for each loop(), we can have an inference of a y_val from x_val with our model, and then control our LEDs to blink in a specific amount of time.
```c
for(int i = 0; i < 6; i++) {
gpio_set_level(pinArray[i], 0);
}
int iLED = chooseLED(y_val);
printf("Choose LED: %d\n", iLED);
gpio_set_level(pinArray[iLED], 1);
vTaskDelay((1000 / SEGMENT) / portTICK_PERIOD_MS);
gpio_set_level(pinArray[iLED], 0);
```
# Create a circuit board for stimulator a harmonic motion with LEDs
Here is an example of a circuit board that I designed for this project in tinkercad.com with Arduino UNO.
![circuit](/images/circuit.png)
# Result
[![Watch the video](https://img.youtube.com/vi/6RySA9YDytg/maxresdefault.jpg)](https://youtu.be/6RySA9YDytg)
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
esp32 部署环境和vscode安装,esp32安装 (1465个子文件)
libtfmicro.a 16.18MB
libwpa_supplicant.a 5.4MB
liblwip.a 4.41MB
libdriver.a 4.38MB
liblibsodium.a 3.78MB
libmbedcrypto.a 3.02MB
libasio.a 2.38MB
libfreemodbus.a 1.96MB
libnghttp.a 1.89MB
libhal.a 1.84MB
libcoap.a 1.56MB
libexpat.a 1.41MB
libnvs_flash.a 1.39MB
libbootloader_support.a 1.07MB
libmbedtls.a 1008KB
libesp_system.a 982KB
libmdns.a 971KB
libesp_eth.a 959KB
libspi_flash.a 941KB
libfreertos.a 876KB
libmain.a 864KB
libwifi_provisioning.a 850KB
libesp_wifi.a 780KB
libspiffs.a 753KB
libfatfs.a 738KB
libesp_netif.a 737KB
libbootloader_support.a 710KB
libsoc.a 694KB
libmbedx509.a 630KB
libsoc.a 607KB
libconsole.a 541KB
libesp32.a 504KB
libcbor.a 498KB
libopenssl.a 479KB
libesp_http_server.a 478KB
libmqtt.a 454KB
libheap.a 443KB
libprotobuf-c.a 435KB
libprotocomm.a 433KB
libsdmmc.a 416KB
libesp_common.a 403KB
libvfs.a 398KB
libsoc_esp32.a 392KB
libesp_event.a 382KB
libjson.a 362KB
libtcp_transport.a 346KB
libesp_http_client.a 330KB
libnewlib.a 327KB
libwear_levelling.a 323KB
libsoc_esp32.a 321KB
libesp_serial_slave_link.a 240KB
libesp_local_ctrl.a 229KB
libesp-tls.a 226KB
libefuse.a 225KB
libunity.a 221KB
libesp_hid.a 217KB
libefuse.a 208KB
libmicro-ecc.a 205KB
libpthread.a 178KB
libesp_pm.a 178KB
libespcoredump.a 174KB
libesp_timer.a 173KB
libesp_websocket_client.a 170KB
libtcpip_adapter.a 167KB
libesp_ringbuf.a 161KB
libxtensa.a 136KB
libapp_update.a 132KB
libesp_gdbstub.a 122KB
libapp_trace.a 117KB
libmbedtls.a 106KB
liblog.a 90KB
libulp.a 89KB
libhal.a 80KB
liblog.a 79KB
libesp_https_ota.a 77KB
libspi_flash.a 77KB
libperfmon.a 73KB
libcxx.a 67KB
libesp_adc_cal.a 55KB
libesp_rom.a 36KB
libesp_rom.a 36KB
libesp_ipc.a 32KB
libcmock.a 29KB
libjsmn.a 27KB
libmain.a 21KB
app-flash_args 75B
hello_world.bin 406KB
bootloader.bin 26KB
feature_tests.bin 6KB
feature_tests.bin 6KB
CMakeDetermineCompilerABI_CXX.bin 4KB
CMakeDetermineCompilerABI_CXX.bin 4KB
CMakeDetermineCompilerABI_C.bin 4KB
CMakeDetermineCompilerABI_C.bin 4KB
partition-table.bin 3KB
.bin_timestamp 173B
.bin_timestamp 163B
bootloader-complete 0B
bootloader-configure 0B
bootloader-done 0B
共 1465 条
- 1
- 2
- 3
- 4
- 5
- 6
- 15
资源评论
东哥爱编程
- 粉丝: 4780
- 资源: 49
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功