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

Commit

Permalink
State struct [14% : 7%]
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Bielousov committed Dec 15, 2017
1 parent f36e9ab commit d213ca8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 40 deletions.
45 changes: 45 additions & 0 deletions src/Robot/0_State.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* States Definition
* =====================
*/

// Decisions state
struct DECISION_STATE {
uint16_t timeSincePreviousDecision = 0;
};

// Environment state
struct ENVIRONMENT_STATE {
bool isHumanDetected = false;
uint16_t timeWhenHumanArrived = 0;
uint16_t timeWhenHumanDeparted = 0;
};

// Eyes state
struct EYES_STATE {
bool isOpen = false;
uint8_t pupilsPosition[2] = {0, 0};
uint8_t pupilsSize[2] = {2, 2};
byte* currentFrame;
};

// 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;
}
36 changes: 10 additions & 26 deletions src/Robot/3_Eyes.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,12 @@ const uint8_t EYES_CLOSE_ANIMATION_SEQUENCE[5] = {4, 3, 2, 1, 0};
*/
LEDMatrixDriver lmd(1, PIN_EYES_LED_CS);

/*
* Eyes state
*/
struct EYES_STATE {
bool isOpen;
uint8_t pupilsSize[2];
int8_t pupilsPosition[2];
byte* currentFrame;
};

EYES_STATE eyesState = {
false, // I start with my eyes closed
{2, 2}, // Pupils size
{0, 1}, // Pupils position, center based
(byte*)eyeBlinkAnimationBitmap[0]
};

/*
* Setup
* =====
*/
void initEyes() {

lmd.setEnabled(true);
lmd.setIntensity(EYES_BRIGHTNESS);
lmd.clear();
Expand All @@ -53,6 +37,7 @@ void initEyes() {
eyesAnimationThread.setInterval(EYES_ANIMATION_FPS);

/* Start by opening my eyes */
State.Eyes.currentFrame = (byte*)eyeBlinkAnimationBitmap[0];
openEyes();
}

Expand Down Expand Up @@ -91,10 +76,10 @@ bool applyPupilMask(uint8_t x, uint8_t y, bool ledPixel) {
return ledPixel;
}

uint8_t xStartPosition = EYES_ORIGIN_POSITION + eyesState.pupilsPosition[0];
uint8_t xEndPosition = xStartPosition + eyesState.pupilsSize[0];
uint8_t yStartPosition = EYES_ORIGIN_POSITION + eyesState.pupilsPosition[1];
uint8_t yEndPosition = yStartPosition + eyesState.pupilsSize[1];
uint8_t xStartPosition = EYES_ORIGIN_POSITION + State.Eyes.pupilsPosition[0];
uint8_t xEndPosition = xStartPosition + State.Eyes.pupilsSize[0];
uint8_t yStartPosition = EYES_ORIGIN_POSITION + State.Eyes.pupilsPosition[1];
uint8_t yEndPosition = yStartPosition + State.Eyes.pupilsSize[1];

if (x >= xStartPosition && x < xEndPosition && y >= yStartPosition && y < yEndPosition) {
return false;
Expand All @@ -104,11 +89,11 @@ bool applyPupilMask(uint8_t x, uint8_t y, bool ledPixel) {
}

void onPupilsMove() {
eyesState.pupilsPosition[0] = random(-2, 2);
eyesState.pupilsPosition[1] = random(-2, 2);
State.Eyes.pupilsPosition[0] = random(-2, 2);
State.Eyes.pupilsPosition[1] = random(-2, 2);

if (isAnimationQueueEmpty()) {
pushFrameToAnimationQueue(eyesState.currentFrame);
pushFrameToAnimationQueue(State.Eyes.currentFrame);
}

}
Expand All @@ -131,7 +116,7 @@ void drawEyes(byte* bitmap) {
}
}

eyesState.currentFrame = bitmap;
State.Eyes.currentFrame = bitmap;
lmd.display();
}

Expand All @@ -145,7 +130,6 @@ void runEyesThread() {
}
}


/*
* Thread callback
* ---------------
Expand Down
16 changes: 2 additions & 14 deletions src/Robot/4_Decisions.ino → src/Robot/5_Decisions.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@ Thread decisionThread = Thread();
const uint8_t DECISION_FREQUENCY = 200; // Decisions frequency, in ms
const uint8_t DECISION_MAX_WEIGHT = 255; // Decisions max weight

/*
* Decisions model state
* =====================
*/
struct DECISION_STATE {
uint16_t timeSincePreviousDecision;
};

DECISION_STATE decisionState = {
0, // Time since after previous decision made
};

/*
* Decisions
* =========
Expand Down Expand Up @@ -112,7 +100,7 @@ void makeDecision (struct DECISION *decision, void calback()) {
}

// Positive decision
decisionState.timeSincePreviousDecision = 0;
State.Decision.timeSincePreviousDecision = 0;
decision->timeSincePreviousDecision = 0;
decision->weight = 0;
calback();
Expand All @@ -134,7 +122,7 @@ void runDecisionsThread() {
*/
void onDecision() {
// Update decision state
decisionState.timeSincePreviousDecision += DECISION_FREQUENCY;
State.Decision.timeSincePreviousDecision += DECISION_FREQUENCY;

makeDecisionEyesBlink();
makeDecisionEyesPupilsMove();
Expand Down

0 comments on commit d213ca8

Please sign in to comment.