This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Human motion detection (PIR sensor) [14% : 8%]
- Loading branch information
Anton Bielousov
committed
Dec 17, 2017
1 parent
d213ca8
commit 00bd14e
Showing
10 changed files
with
285 additions
and
178 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* Custom Types | ||
*/ | ||
struct uint8_range { | ||
uint8_t min; | ||
uint8_t max; | ||
}; | ||
struct uint16_range { | ||
uint16_t min; | ||
uint16_t max; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* States Definition | ||
* ===================== | ||
*/ | ||
|
||
/* | ||
* Decisions | ||
* ========= | ||
*/ | ||
struct DECISION { | ||
uint16_t timeSincePreviousDecision = 0; | ||
uint8_t weight = 0; | ||
}; | ||
|
||
struct DECISION_STATE { | ||
DECISION eyesBlink; | ||
DECISION eyesPupils; | ||
DECISION sleepMode; | ||
}; | ||
|
||
|
||
// Environment state | ||
struct ENVIRONMENT_STATE { | ||
bool isHumanDetected = true; | ||
uint16_t timeHumanDetected = 0; | ||
uint16_t timeHumanLost = 0; | ||
}; | ||
|
||
// Eyes state | ||
struct EYES_STATE { | ||
byte* currentFrame; | ||
bool isOpened = false; | ||
uint8_t pupilsPosition[2] = {0, 0}; | ||
uint8_t pupilsSize[2] = {2, 2}; | ||
}; | ||
|
||
// Global state store | ||
struct STATE { | ||
DECISION_STATE Decision; | ||
ENVIRONMENT_STATE Environment; | ||
EYES_STATE Eyes; | ||
}; | ||
|
||
/* | ||
* States Initialization | ||
* ===================== | ||
*/ | ||
STATE State = {}; | ||
|
||
/* | ||
* Public Methods | ||
*/ | ||
|
||
uint16_t getTime() { | ||
return millis() / 1000; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Environment sensors thread | ||
Thread environmentThread = Thread(); | ||
|
||
const uint16_t ENVIRONMENT_SCAN_FREQUENCY = 500; // Environment sensors read frequency, in ms | ||
|
||
void initEnvironmet() { | ||
pinMode(PIN_PIR_SENSOR, INPUT); | ||
|
||
environmentThread.onRun(onEnvironmentUpdate); | ||
environmentThread.setInterval(ENVIRONMENT_SCAN_FREQUENCY); | ||
} | ||
|
||
/* | ||
* Public Methods | ||
*/ | ||
bool isHumanDetected() { | ||
return State.Environment.isHumanDetected; | ||
} | ||
|
||
uint16_t getTimeSinceHumanDetected() { | ||
return getTime() - State.Environment.timeHumanDetected; | ||
} | ||
|
||
uint16_t getTimeSinceHumanLost() { | ||
return getTime() - State.Environment.timeHumanLost; | ||
} | ||
|
||
|
||
/* | ||
* Events | ||
*/ | ||
void onHumanDetected() { | ||
if (!State.Environment.isHumanDetected) { | ||
// TODO: State change event | ||
} | ||
|
||
State.Environment.isHumanDetected = true; | ||
State.Environment.timeHumanDetected = getTime(); | ||
} | ||
|
||
void onHumanLost() { | ||
State.Environment.isHumanDetected = false; | ||
State.Environment.timeHumanLost = getTime(); | ||
} | ||
|
||
|
||
/* | ||
* Asynchronous thread process | ||
* =========================== | ||
*/ | ||
void runEnvironmentThread() { | ||
if(environmentThread.shouldRun()) { | ||
environmentThread.run(); | ||
} | ||
} | ||
|
||
|
||
/* | ||
* Thread callback | ||
* --------------- | ||
*/ | ||
void onEnvironmentUpdate() { | ||
bool pirSensorValue = digitalRead(PIN_PIR_SENSOR); | ||
|
||
if (pirSensorValue) { | ||
onHumanDetected(); | ||
} | ||
} |
Oops, something went wrong.