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

Commit

Permalink
Human motion detection (PIR sensor) [14% : 8%]
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Bielousov committed Dec 17, 2017
1 parent d213ca8 commit 00bd14e
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 178 deletions.
45 changes: 0 additions & 45 deletions src/Robot/0_State.ino

This file was deleted.

11 changes: 11 additions & 0 deletions src/Robot/0_Types.ino
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;
};
56 changes: 56 additions & 0 deletions src/Robot/1_State.ino
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.
19 changes: 19 additions & 0 deletions src/Robot/2_Animation.ino → src/Robot/3_Animation.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@

#include <QList.h>

const uint8_t ANIMATION_BUFFER_SIZE = 32;

// Eyes Animation Queue
QList <byte*> animationQueue;
//byte animationBuffer[ANIMATION_BUFFER_SIZE][8];
//
//void initAnimationQueue() {
// for (uint8_t i = 0; i < ANIMATION_BUFFER_SIZE; i++) {
// animationBuffer[i] = NULL;
// }
//}

void addAnimationSequence(
const byte animationBitmap[][8],
Expand All @@ -33,3 +42,13 @@ byte* popFrameFromAnimationQueue() {
bool isAnimationQueueEmpty() {
return animationQueue.size() == 0;
}

//byte findEmptyAnimationBuffer() {
// for (uint8_t i = 0; i < ANIMATION_BUFFER_SIZE - 1; i++) {
// if(animationBuffer[i] == NULL) {
// return i;
// }
// }
//
// return ANIMATION_BUFFER_SIZE - 1;
//}
10 changes: 8 additions & 2 deletions src/Robot/3_Eyes.ino → src/Robot/4_Eyes.ino
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,25 @@ void initEyes() {


void closeEyes() {
// Update state
State.Eyes.isOpened = false;

// Stop blinking when my eye are closed
resetEyesBlinkDecision(false);
resetEyesBlinkDecision();

// Add animation sequence to queue
addAnimationSequence(eyeBlinkAnimationBitmap, EYES_CLOSE_ANIMATION_SEQUENCE, sizeof(EYES_CLOSE_ANIMATION_SEQUENCE));
}

void openEyes() {
// Update state
State.Eyes.isOpened = true;

// Add animation sequence to queue
addAnimationSequence(eyeBlinkAnimationBitmap, EYES_OPEN_ANIMATION_SEQUENCE, sizeof(EYES_OPEN_ANIMATION_SEQUENCE));

// When my eyes are open I want to blink
resetEyesBlinkDecision(true);
resetEyesBlinkDecision();
}

/*
Expand Down
130 changes: 0 additions & 130 deletions src/Robot/5_Decisions.ino

This file was deleted.

68 changes: 68 additions & 0 deletions src/Robot/5_Environment.ino
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();
}
}
Loading

0 comments on commit 00bd14e

Please sign in to comment.