Skip to content
This repository has been archived by the owner on Jan 4, 2024. It is now read-only.

Commit

Permalink
add uart output
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed Aug 4, 2023
1 parent 6d57869 commit 509a73d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
2 changes: 2 additions & 0 deletions examples/camera_opencv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ by default code use opencv to get balck rectangle line, like this:

![black rectangle](./assets/find_rectangle.jpg) ![black rectangle2](./assets/m2dock_opencv_find_rectangle_red_point.jpg)

and send result by UART(serial), you can use this board as a module to your project.

## Build

See [README.md](../../README.md) for build instructions and compile `examples/hello-world` first.
Expand Down
8 changes: 1 addition & 7 deletions examples/camera_opencv/config_defaults.mk
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,9 @@ CONFIG_LIBMAIX_ENABLED=y

# CONFIG_COMPONENT_MAIX_ENABLE is not set
CONFIG_COMPONENT_IMAGE_ENABLE=y
# CONFIG_COMPONENT_MAIX_DLS831_ENABLE is not set
CONFIG_COMPONENT_MAIX_DLS831_ENABLE=y
# CONFIG_COMPONENT_MAIX_GS831_ENABLE is not set
# CONFIG_COMPONENT_LIBMS_ENABLE is not set

#
# libmaix configuration
#
# end of libmaix configuration

CONFIG_COMPONENT_3RD_PARTY_ENABLE=y

#
Expand Down
1 change: 1 addition & 0 deletions examples/camera_opencv/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ append_srcs_dir(ADD_SRCS "src")

###### Add required/dependent components ######
list(APPEND ADD_REQUIREMENTS maix_cv_image libmaix)
list(APPEND ADD_REQUIREMENTS maix_dls831) # uart
if(CONFIG_IMLIB_ENABLE)
list(APPEND ADD_REQUIREMENTS third_party)
endif()
Expand Down
37 changes: 33 additions & 4 deletions examples/camera_opencv/main/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#include "libmaix_cv_image.h"
#include <opencv2/opencv.hpp>

////////// UART(serial) //////////
#include "linux_uart.h"
//////////////////////////////////

#ifdef CONFIG_IMLIB_ENABLE
#include "imlib.h"
#endif
Expand All @@ -41,6 +45,8 @@
#include "sys/time.h"

static struct timeval old, now;
static int uart_fd = -1;
static char uart_buff[1024] = {0};

static void cap_set()
{
Expand Down Expand Up @@ -111,13 +117,26 @@ int app_init(int cam_w, int cam_h, int cam2_w, int cam2_h)
imlib_init_all();
#endif

// uart init
// /dev/ttyS1, for maix-ii-dock(v831) TX pin is PG6, RX pin is PG7
uart_t uart_conf;
uart_conf.baud = 115200;
uart_conf.data_bits = 8;
uart_conf.stop_bits = 1;
uart_conf.parity = 'N';

uart_fd = linux_uart_init((char*)"/dev/ttyS1", &uart_conf);

app.running = 1;

return 0;
}

void app_exit()
{
// uart deinit
if(uart_fd > 0)
linux_uart_deinit(uart_fd);

if (NULL != app.cam0)
libmaix_cam_destroy(&app.cam0);
Expand Down Expand Up @@ -154,7 +173,7 @@ void opencv_ops(cv::Mat &rgb)
// get the largets contour
int largest_area = 0;
int largest_contour_index = 0;
for (int i = 0; i < contours.size(); i++)
for (size_t i = 0; i < contours.size(); i++)
{
double area = cv::contourArea(contours[i]);
if (area > largest_area)
Expand Down Expand Up @@ -187,7 +206,7 @@ void opencv_ops(cv::Mat &rgb)
// get the largets contour
int largest_area2 = 0;
int largest_contour_index2 = 0;
for (int i = 0; i < contours2.size(); i++)
for (size_t i = 0; i < contours2.size(); i++)
{
double area = cv::contourArea(contours2[i]);
if (area > largest_area2)
Expand All @@ -203,19 +222,29 @@ void opencv_ops(cv::Mat &rgb)
// 在 rgb 图上画出凸包
cv::polylines(rgb, hull, true, cv::Scalar(0, 255, 0), 2);
// 在 rgb 图上画出 hull 点
for (int i = 0; i < hull.size(); i++)
for (size_t i = 0; i < hull.size(); i++)
{
cv::circle(rgb, hull[i], 5, cv::Scalar(0, 0, 255), 2);
}
}

// get the center point of the largest contour
cv::Point2f mc;
if(contours2.size() > 0)
{
cv::Moments mu = cv::moments(contours2[largest_contour_index2], false);
cv::Point2f mc = cv::Point2f(mu.m10 / mu.m00, mu.m01 / mu.m00);
mc = cv::Point2f(mu.m10 / mu.m00, mu.m01 / mu.m00);
cv::circle(rgb, mc, 5, cv::Scalar(255, 0, 255), 2);
}

// send result(center point, points) to uart
snprintf(uart_buff, sizeof(uart_buff), "red: %d, %d, points: %d, ", (int)mc.x, (int)mc.y, (int)hull.size());
for(size_t i = 0; i < hull.size(); i++)
{
snprintf(uart_buff + strlen(uart_buff), sizeof(uart_buff) - strlen(uart_buff), "%d, %d, ", (int)hull[i].x, (int)hull[i].y);
}
snprintf(uart_buff + strlen(uart_buff), sizeof(uart_buff) - strlen(uart_buff), "\r\n");
write(uart_fd, uart_buff, strlen(uart_buff));
}

#ifdef CONFIG_IMLIB_ENABLE
Expand Down

0 comments on commit 509a73d

Please sign in to comment.