Skip to content

Commit

Permalink
Merge pull request Seeed-Solution#50 from Seeed-Solution/feat-vision-v2
Browse files Browse the repository at this point in the history
feat: vision v2 demo
  • Loading branch information
Love4yzp authored Mar 1, 2024
2 parents 042c748 + 2887a1a commit 403b67d
Show file tree
Hide file tree
Showing 40 changed files with 10,867 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ dependencies.lock
*.tmp

#tools ignored
tools/flash_firmware
tools/flash_firmware

*.zip
2 changes: 1 addition & 1 deletion components/lvgl/src/extra/libs/sjpg/tjpgdcnf.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/ 1: Enable
*/

#define JD_FASTDECODE 0
#define JD_FASTDECODE 1
/* Optimization level
/ 0: Basic optimization. Suitable for 8/16-bit MCUs.
/ 1: + 32-bit barrel shifter. Suitable for 32-bit MCUs.
Expand Down
37 changes: 37 additions & 0 deletions examples/vision_v2_display/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Language: Cpp
BasedOnStyle: LLVM
IndentWidth: 4
ColumnLimit: 0
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
AlignTrailingComments: true
AlignConsecutiveMacros:
Enabled: true
AcrossEmptyLines: true
AcrossComments: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortBlocksOnASingleLine : true
AlignConsecutiveDeclarations : true
AlignConsecutiveAssignments: AcrossEmptyLinesAndComments
AlignEscapedNewlinesLeft : true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: true
# BreakConstructorInitializersBeforeComma : true
IndentCaseLabels: true
MaxEmptyLinesToKeep : 2
# NamespaceIndentation : 3
SpacesInParentheses: false # 在“()”中插入空格。
SpacesInSquareBrackets: false # 在“[]”中插入空格。
SpaceInEmptyBlock: false
SpacesBeforeTrailingComments: 1 # Adjust the number of spaces as desired
IndentPPDirectives: BeforeHash # 在预处理指令之前缩进。
15 changes: 15 additions & 0 deletions examples/vision_v2_display/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

set(IDF_TARGET esp32s3)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)

set(EXTRA_COMPONENT_DIRS ../../components)

set(EXCLUDE_COMPONENTS LoRaWAN)

add_compile_options(-fdiagnostics-color=always -w)

project(vision_v2_wifi)
13 changes: 13 additions & 0 deletions examples/vision_v2_display/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Grove Vision V2 X SenseCAP Indicator

## Prerequisites

- Get and install ESP-IDF toolchain and its dependencies.
[ESP-IDF Get started](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html)

- Install ESP-IDF on Windows: [Description page](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/windows-setup.html)
- There's a ESP-related IDE too, made by Espressif, containing ESP-IDF, called Espressif-IDE which is based on Eclipse CDT. [Espressif-IDE](https://github.com/espressif/idf-eclipse-plugin/blob/master/docs/Espressif-IDE.md)
- Get the [ESP-IDF offline Windows installer](https://dl.espressif.com/dl/idf-installer/esp-idf-tools-setup-offline-5.1.1.exe?) or [Espressif-IDE Windows installer](https://dl.espressif.com/dl/idf-installer/espressif-ide-setup-2.11.0-with-esp-idf-5.1.1.exe)
- Install it on your Windows system accepting all offered options and default settings. This automagically installs Python, git, CMake, etc all at once under C:\Espressif folder.
- You can start building in command-line from the PowerShell/CMD entries created in the start-menu, but with the help of the included build.bat you can build on a normal commandline too
- Or you can build the project in the IDE GUI, see 'Usage' section.
241 changes: 241 additions & 0 deletions examples/vision_v2_display/RP2040/RP2040.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
#include <ArduinoJson.h>
#include <Base64.h>
#include <PacketSerial.h>
#include <Seeed_Arduino_SSCMA.h>
#include <Wire.h>


#define ARDUINO_ARCH_RP2040 1
#define pcSerial Serial
#ifdef ARDUINO_ARCH_RP2040 /////////////////////////
#include <SoftwareSerial.h>
#define ESP_rxPin 17
#define ESP_txPin 16
#define espSerial Serial1
#define ESP32_COMM_BAUD_RATE (921600)
// #define ESP32_COMM_BAUD_RATE (115200)
// Right Grove -> Check https://files.seeedstudio.com/wiki/SenseCAP/SenseCAP_Indicator/grove.png
#define txPin 20
#define rxPin 21
// SoftwareSerial atSerial = SoftwareSerial(rxPin, txPin);
#define atSerial Serial2
#define _SDA 26
#define _SCL 27
#elif ESP32 // XIAO /////////////////////////
#include <HardwareSerial.h>
HardwareSerial atSerial(0);
#else /////////////////////////
#define atSerial Serial2
#endif

#define _LOG 0 // Disable for high frequency

const char compile_date[] = __DATE__ " " __TIME__;
SSCMA AI;
SSCMA AI_I2C;

PacketSerial myPacketSerial;
#define PKT_TYPE_CMD_BEEP_ON 0xA1
#define PKT_TYPE_CMD_SHUTDOWN 0xA3
#define PKT_TYPE_CMD_POWER_ON 0xA4
#define PKT_TYPE_CMD_MODEL_TITLE 0xA5
/************************ recv cmd from esp32 ****************************/
static bool shutdown_flag = false;

static volatile bool get_title = false;

void onPacketReceived(const uint8_t *buffer, size_t size)
{
if (size < 1) {
return;
}
switch (buffer[0]) {
case PKT_TYPE_CMD_SHUTDOWN: {
Serial.println("cmd shutdown");
shutdown_flag = true;
break;
}
case PKT_TYPE_CMD_BEEP_ON: {
beep_on();
break;
}
case PKT_TYPE_CMD_MODEL_TITLE: {
get_title = true;
break;
}
default:
break;
}
}
/************************ beep ****************************/

#define Buzzer 19 // Buzzer GPIO

void beep_init(void)
{
pinMode(Buzzer, OUTPUT);
}
void beep_off(void)
{
digitalWrite(19, LOW);
}
void beep_on(void)
{
analogWrite(Buzzer, 127);
delay(50);
analogWrite(Buzzer, 0);
}

void setup()
{
#ifdef pcSerial
pcSerial.begin(9600);
// while (!pcSerial)
// ;
pcSerial.println(compile_date);
#endif

#ifdef ARDUINO_ARCH_RP2040
#ifdef pcSerial
pcSerial.println("Setting ESP32 UART For inner RP2040");
#endif
espSerial.setRX(ESP_rxPin);
espSerial.setTX(ESP_txPin);
espSerial.begin(ESP32_COMM_BAUD_RATE);

myPacketSerial.setStream(&espSerial);
myPacketSerial.setPacketHandler(&onPacketReceived);
#ifdef pcSerial
pcSerial.println("Using UART OF AI");
#endif
atSerial.setFIFOSize(16 * 1024);
AI.begin(&atSerial);
// AI_I2C.begin();// For I2C Port
#endif
}

void AI_func(SSCMA &instance)
{
if (!instance.invoke(1, false, true)) {
JsonDocument doc; // Adjust size as needed

// Performance metrics
int index = 0;
doc["preprocess"] = instance.perf().prepocess;
doc["inference"] = instance.perf().inference;
doc["postprocess"] = instance.perf().postprocess;

// Last image
if (instance.last_image().length()) {
doc["img"] = instance.last_image();
}
// Boxes
index = 0;
for (auto &box : instance.boxes()) {
int currentBox[6] = {box.x, box.y, box.w, box.h, box.score, box.target};
copyArray(currentBox, doc["boxes"][index++]);
}

// Classes
index = 0;
for (auto &classObj : instance.classes()) {
int currentClass[2] = {classObj.target, classObj.score};
copyArray(currentClass, doc["classes"][index++]);
}
// Points
index = 0;
for (auto &point : instance.points()) {
int currtentPoint[4] = {point.target, point.score, point.x, point.y};
copyArray(currtentPoint, doc["points"][index++]);
}

// Keypoints
index = 0;
for (auto &keypoint : instance.keypoints()) {
int currentKeypoint[6] = {keypoint.box.target, keypoint.box.score, keypoint.box.x, keypoint.box.y, keypoint.box.w, keypoint.box.h};
copyArray(currentKeypoint, doc["keypoints"][index]);
int j = 0;
for (auto &point : keypoint.points) {
int arrayKeypoints[2] = {point.x, point.y};
copyArray(arrayKeypoints, doc["keypoints"][index]["points"][j++]);
}
index++;
}
#if _LOG
serializeJsonPretty(doc, pcSerial); // Serialize and print the JSON document
#endif
#ifdef espSerial
serializeJson(doc, espSerial); // Serialize and print the JSON document
#endif
}

if (get_title) {
get_title = false;
#if _LOG
pcSerial.println("Recieved get title event");
#endif
if (!instance.fetch_info()) {
// base64 解码成 JSON
String base64String = instance.info();
// pcSerial.printf("instance.info(): %s \n", base64String.c_str());

int inputStringLength = base64String.length() + 1;
char inputString[inputStringLength];
base64String.toCharArray(inputString, inputStringLength);

int decodedLength = Base64.decodedLength(inputString, inputStringLength - 1); // 计算解码长度
char decodedString[decodedLength + 1]; // 分配额外的空间用于空终止符

Base64.decode(decodedString, inputString, inputStringLength - 1); // 解码Base64字符串
decodedString[decodedLength] = '\0'; // 手动添加空终止符确保字符串正确终止

// pcSerial.print("Decoded string is:\t");
// pcSerial.println(decodedString);

JsonDocument doc; // 创建一个足够大的JsonDocument
DeserializationError error = deserializeJson(doc, decodedString); // 将解码后的字符串反序列化到JSON文档

if (error) { // 检查反序列化是否成功
// pcSerial.print("deserializeJson() failed: ");
// pcSerial.println(error.c_str());
return;
}
// 获取JSON的info 信息
JsonDocument send_doc;
send_doc["model_name"] = doc["name"];
#if _LOG
serializeJson(send_doc, pcSerial);
#endif
#ifdef espSerial
serializeJson(send_doc, espSerial); // Serialize and print the JSON document
#endif
}
}
}

void loop()
{ // For UART Port
AI_func(AI);
// AI_func(AI_I2C); // For I2C Port
}

void setup1()
{ // For I2C Port
beep_init();
// Serial.println("Beep init");
delay(500);
// beep_on();

#ifdef pcSerial
pcSerial.println("Setting Wire1 for AI_I2C");
#endif
Wire1.setSCL(_SCL);
Wire1.setSDA(_SDA);
}

void loop1()
{
myPacketSerial.update();
if (myPacketSerial.overflow()) {
}
}
35 changes: 35 additions & 0 deletions examples/vision_v2_display/RP2040/pins_arduino.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once


// Serial
#define PIN_SERIAL1_TX (16u)
#define PIN_SERIAL1_RX (17u)

//#define PIN_SERIAL2_TX (8u)
//#define PIN_SERIAL2_RX (9u)
#define PIN_SERIAL2_TX (20u)
#define PIN_SERIAL2_RX (21u)

// SPI
#define PIN_SPI0_MISO (0u)
#define PIN_SPI0_MOSI (3u)
#define PIN_SPI0_SCK (2u)
#define PIN_SPI0_SS (1u)

#define PIN_SPI1_MISO (12u)
#define PIN_SPI1_MOSI (11u)
#define PIN_SPI1_SCK (10u)
#define PIN_SPI1_SS (13u)

// Wire
#define PIN_WIRE0_SDA (20u)
#define PIN_WIRE0_SCL (21u)

#define PIN_WIRE1_SDA (26u)
#define PIN_WIRE1_SCL (27u)

#define SERIAL_HOWMANY (3u)
#define SPI_HOWMANY (2u)
#define WIRE_HOWMANY (2u)

#include "../generic/common.h"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 403b67d

Please sign in to comment.