Skip to content

Update Himax motion detection feature. #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions libraries/Himax_HM01B0/himax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ static regval_list_t himax_default_regs[] = {
{SINGLE_THR_HOT, 0x90}, // single hot pixel th
{SINGLE_THR_COLD, 0x40}, // single cold pixel th
{0x1012, 0x00}, // Sync. shift disable
{0x2000, 0x07},
{STATISTIC_CTRL, 0x07}, // AE stat en | MD LROI stat en | magic
{0x2003, 0x00},
{0x2004, 0x1C},
{0x2007, 0x00},
Expand Down Expand Up @@ -156,7 +156,7 @@ static regval_list_t himax_default_regs[] = {
{FS_50HZ_H, 0x00},
{FS_50HZ_L, 0x32},

{MD_CTRL, 0x30},
{MD_CTRL, 0x00},
{FRAME_LEN_LINES_H, HIMAX_FRAME_LENGTH_QVGA>>8},
{FRAME_LEN_LINES_L, HIMAX_FRAME_LENGTH_QVGA&0xFF},
{LINE_LEN_PCK_H, HIMAX_LINE_LEN_PCK_QVGA>>8},
Expand Down Expand Up @@ -315,20 +315,21 @@ int HIMAX_SetFramerate(uint32_t framerate)
int HIMAX_EnableMD(bool enable)
{
int ret = HIMAX_ClearMD();
if (enable) {
ret |= HIMAX_RegWrite(MD_CTRL, 0x03);
} else {
ret |= HIMAX_RegWrite(MD_CTRL, 0x30);
}
ret |= HIMAX_RegWrite(MD_CTRL, enable ? 1:0);
return ret;
}

int HIMAX_SetMDThreshold(uint32_t low, uint32_t high)
int HIMAX_SetMDThreshold(uint32_t threshold)
{
int ret = 0;
ret |= HIMAX_RegWrite(MD_THL, low & 0xff);
ret |= HIMAX_RegWrite(MD_THH, high & 0xff);
return ret;
// Set motion detection threshold/sensitivity.
// The recommended threshold range is 0x03 to 0xF0.
//
// Motion is detected according to the following:
// |MD_LROI_MEAN – MD_LROI_IIR_MEAN| > ( MD_LROI_MEAN * MD_THL / 64)
//
// In other words, motion is detected if the abs difference of the ROI mean and the
// average ROI mean of the last 8 or 16 frames is higher than (ROI mean * threshold / 64).
return HIMAX_RegWrite(MD_THL, (threshold < 3) ? 3 : (threshold > 0xF0) ? 0xF0 : threshold);
}

int HIMAX_SetLROI(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2)
Expand Down
2 changes: 1 addition & 1 deletion libraries/Himax_HM01B0/himax.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ int HIMAX_Mode(uint8_t mode);
int HIMAX_SetResolution(uint32_t resolution);
int HIMAX_SetFramerate(uint32_t framerate);
int HIMAX_EnableMD(bool enable);
int HIMAX_SetMDThreshold(uint32_t low, uint32_t high);
int HIMAX_SetMDThreshold(uint32_t threshold);
int HIMAX_SetLROI(uint32_t x, uint32_t y, uint32_t w, uint32_t h);
int HIMAX_PollMD();
int HIMAX_ClearMD();
Expand Down
4 changes: 2 additions & 2 deletions libraries/Portenta_Camera/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,12 +543,12 @@ int CameraClass::standby(bool enable)
}
}

int CameraClass::motionDetectionThreshold(uint32_t low, uint32_t high)
int CameraClass::motionDetectionThreshold(uint32_t threshold)
{
if (this->initialized == false) {
return -1;
}
return HIMAX_SetMDThreshold(low, high);
return HIMAX_SetMDThreshold(threshold);
}

int CameraClass::motionDetectionWindow(uint32_t x, uint32_t y, uint32_t w, uint32_t h)
Expand Down
2 changes: 1 addition & 1 deletion libraries/Portenta_Camera/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CameraClass {
int standby(bool enable);
int motionDetection(bool enable, md_callback_t callback=NULL);
int motionDetectionWindow(uint32_t x, uint32_t y, uint32_t w, uint32_t h);
int motionDetectionThreshold(uint32_t low, uint32_t high);
int motionDetectionThreshold(uint32_t threshold);
int motionDetected();
int testPattern(bool walking);
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ void setup() {

pinMode(LEDB, OUTPUT);
digitalWrite(LEDB, HIGH);

// Init the cam QVGA, 30FPS
cam.begin(CAMERA_R320x240, 60);
cam.begin(CAMERA_R320x240, 30);

// Set motion detection threshold (0 -> 255).
// The lower the threshold the higher the sensitivity.
cam.motionDetectionThreshold(0);

cam.motionDetectionThreshold(100, 200);
// Set motion detection window/ROI.
cam.motionDetectionWindow(0, 0, 320, 240);

// The detection can also be enabled without any callback
cam.motionDetection(true, on_motion);
}
Expand Down