Skip to content

Commit

Permalink
Merge pull request #43 from brainmachine/tracker
Browse files Browse the repository at this point in the history
Detector, BinaryPattern & LED class
  • Loading branch information
brainmachine authored Oct 5, 2017
2 parents baf1c80 + 4c7ce0a commit 186c88c
Show file tree
Hide file tree
Showing 12 changed files with 935 additions and 243 deletions.
6 changes: 6 additions & 0 deletions Lightwork-Mapper.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ C:\Users\DellXPS\Documents\of\of_v0.9.8_vs_release\addons\ofxOpenCv\libs\opencv\
<ClCompile Include="..\..\..\addons\ofxOpenCv\src\ofxCvImage.cpp" />
<ClCompile Include="..\..\..\addons\ofxOpenCv\src\ofxCvShortImage.cpp" />
<ClCompile Include="src\Animator.cpp" />
<ClCompile Include="src\BinaryPattern.cpp" />
<ClCompile Include="src\Detector.cpp" />
<ClCompile Include="src\LED.cpp" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\ofApp.cpp" />
<ClCompile Include="..\..\..\addons\ofxCv\libs\CLD\src\ETF.cpp" />
Expand Down Expand Up @@ -485,6 +488,9 @@ C:\Users\DellXPS\Documents\of\of_v0.9.8_vs_release\addons\ofxOpenCv\libs\opencv\
<ClInclude Include="..\..\..\addons\ofxOpenCv\src\ofxCvShortImage.h" />
<ClInclude Include="..\..\..\addons\ofxOpenCv\src\ofxOpenCv.h" />
<ClInclude Include="src\Animator.h" />
<ClInclude Include="src\BinaryPattern.h" />
<ClInclude Include="src\Detector.h" />
<ClInclude Include="src\LED.h" />
<ClInclude Include="src\ofApp.h" />
<ClInclude Include="..\..\..\addons\ofxCv\src\ofxCv.h" />
<ClInclude Include="..\..\..\addons\ofxCv\libs\CLD\include\CLD\ETF.h" />
Expand Down
18 changes: 18 additions & 0 deletions Lightwork-Mapper.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@
<ClCompile Include="src\Animator.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="src\BinaryPattern.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="src\Detector.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="src\LED.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="src">
Expand Down Expand Up @@ -1082,6 +1091,15 @@
<ClInclude Include="src\Animator.h">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="src\BinaryPattern.h">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="src\Detector.h">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="src\LED.h">
<Filter>src</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="icon.rc" />
Expand Down
163 changes: 128 additions & 35 deletions src/Animator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ using namespace std;
Animator::Animator(void) {
cout << "Animator created" << endl;
numLedsPerStrip = 64;
ledBrightness = 200;
ledBrightness = 255;
numStrips = 8;
ledIndex = 0; // Internal counter
mode = ANIMATION_MODE_CHASE;

// TODO: assign pixels for the full setup (all the channels)iter
// TODO: Make pixels private and declare a getter
pixels.assign(numLedsPerStrip*numStrips, ofColor(0,0,0));
testIndex = 0;
frameCount = 0;
frameSkip = 3;

populateLeds();
}

// Destructor
Expand All @@ -32,17 +34,31 @@ Animator::~Animator(void) {
//////////////////////////////////////////////////////////////
// Setters and getters
//////////////////////////////////////////////////////////////
void Animator::setLedInterface(ofxOPC *interface) {
opcClient = interface;
}

void Animator::setMode(animation_mode_t m) {
mode = m;
ledIndex = 0; // TODO: resetInternalVariables() method?
testIndex = 0;
frameCount = 0;
resetPixels();

}

void Animator::setLedBrightness(int brightness) {
ledBrightness = brightness;
opcClient->autoWriteData(getPixels());
}

void Animator::setNumLedsPerStrip(int num) {
ofLogNotice("Setting up Animator");
ofLogNotice("animator") << "setNumLedsPerStrip(): " << num;
numLedsPerStrip = num;

// Update OPC client
opcClient->setLedsPerStrip(numLedsPerStrip);

// Reset LEDs vector
resetPixels();
}

Expand All @@ -51,6 +67,7 @@ int Animator::getNumLedsPerStrip() {
}

void Animator::setNumStrips(int num) {
ofLogNotice("animator") << "setNumStrips(): " << num;
numStrips = num;
resetPixels();
}
Expand All @@ -59,33 +76,78 @@ int Animator::getNumStrips() {
return numStrips;
}

void Animator::setFrameSkip(int num) {
frameSkip = num;
}

int Animator::getFrameSkip() {
return frameSkip;
}

// Internal method to reassign pixels with a vector of the right length. Gives all pixels a value of (0,0,0) (black/off).
void Animator::resetPixels() {
vector <ofColor> pix;
pix.assign(numLedsPerStrip*numStrips, ofColor(0,0,0));
pixels = pix;
populateLeds();
}

// Return pixels (to update OPC or PixelPusher)
vector <ofColor> Animator::getPixels() {
vector <ofColor> pixels;
for (int i = 0; i<leds.size(); i++) {
pixels.push_back(leds[i].color);
}
return pixels;
}

// Reset the LED vector
void Animator::populateLeds() {

int bPatOffset = 150; // Offset to get more meaningful patterns (and avoid 000000000);
int numLeds = numLedsPerStrip*numStrips;
ofLogNotice("animator") << "populateLeds() -> numLeds: " << numLeds;

leds.clear();
for (int i = 0; i < numLeds; i++) {
leds.push_back(LED());
leds[i].setAddress(i);
leds[i].binaryPattern.generatePattern(i+bPatOffset); // Generate a unique binary pattern for each LED
}
ofLogNotice("animator") << "known patterns: ";
//for (int i = 0; i < leds.size(); i++) {
// //ofLogNotice("animator") << leds[i].binaryPattern.binaryPatternString;
//}
}


//////////////////////////////////////////////////////////////
// Animation Methods
//////////////////////////////////////////////////////////////

void Animator::update() {
switch(mode) {
case ANIMATION_MODE_CHASE: {
chase();
break;
}
case ANIMATION_MODE_TEST: {
test();
break;
}
};
//if (frameCount % frameSkip == 0) {
switch(mode) {
case ANIMATION_MODE_CHASE: {
chase();
break;
}
case ANIMATION_MODE_TEST: {
test();
break;
}
case ANIMATION_MODE_BINARY: {
binaryAnimation();
}

case ANIMATION_MODE_OFF: {
setAllLEDColours(ofColor(0, 0, 0));
}
};
// }
// Advance the internal counter
frameCount++;

// Update pixels on external interface
opcClient->autoWriteData(this->getPixels()); // Send pixel values to OPC

}
// Update the pixels for all the strips
// This method does not return the pixels, it's up to the users to send animator.pixels to the driver (FadeCandy, PixelPusher).
Expand All @@ -98,7 +160,7 @@ void Animator::chase() {
else {
col = ofColor(0, 0, 0);
}
pixels.at(i) = col;
leds.at(i).color = col;
}

ledIndex++;
Expand All @@ -110,27 +172,58 @@ void Animator::chase() {
// Set all LEDs to the same colour (useful to turn them all on or off).
void Animator::setAllLEDColours(ofColor col) {
for (int i = 0; i < numLedsPerStrip*numStrips; i++) {
pixels.at(i) = col;
leds.at(i).color = col;
}
opcClient->autoWriteData(this->getPixels()); // TODO: Review this
}

//LED Pre-flight test
void Animator::test() {
setAllLEDColours(ofColor(0,0,255));
/*
int start = ofGetFrameNum(); // needs global variables to work properly
int currFrame = start;
int diff = currFrame - start;
if (diff <300){
if (diff < 100) { setAllLEDColours(ofColor(255, 0, 0)); }
else if (diff <200 && diff >100){ setAllLEDColours(ofColor(0, 255, 0)); }
else if (diff < 300 && diff >200) { setAllLEDColours(ofColor(0, 0, 255)); }
testIndex++;

if (testIndex < 30) {
setAllLEDColours(ofColor(255, 0, 0));
}
else if (testIndex > 30 && testIndex < 60) {
setAllLEDColours(ofColor(0, 255, 0));
}
else if (testIndex > 60 && testIndex < 90) {
setAllLEDColours(ofColor(0, 0, 255));
}
currFrame = ofGetFrameNum();
diff = currFrame - start;

if (diff >= 300) {
setAllLEDColours(ofColor(0, 0, 0));
if (testIndex > 90) {
testIndex = 0;

}
*/
}

void Animator::binaryAnimation() {
// LED binary state. START -> GREEN, HIGH -> BLUE, LOW -> RED, OFF -> (off)

// Slow down the animation, set new state every 3 frames

// cout << leds.size() << endl;
for (int i = 0; i < leds.size(); i++) {
switch (leds[i].binaryPattern.state){ // 0
case BinaryPattern::LOW: {
leds.at(i).color = ofColor(ledBrightness, 0, 0); // RED
break;
}
case BinaryPattern::HIGH: { // 1
leds.at(i).color = ofColor(0, 0, ledBrightness); // BLUE
break;
}
case BinaryPattern::START: { // 2
leds.at(i).color = ofColor(0, ledBrightness, 0); // GREEN
break;
}
case BinaryPattern::OFF: { // 3
leds.at(i).color = ofColor(0, 0, 0); // BLACK
break;
}
}

leds[i].binaryPattern.advance();
}

}
31 changes: 26 additions & 5 deletions src/Animator.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,42 @@

#include <stdio.h>
#include "ofMain.h"
#include "ofxOPC.h"
#include "LED.h"

#endif /* Animator_h */

#include "BinaryPattern.h"


using namespace std;

enum animation_mode_t {ANIMATION_MODE_CHASE, ANIMATION_MODE_TEST};
enum animation_mode_t {ANIMATION_MODE_CHASE, ANIMATION_MODE_TEST, ANIMATION_MODE_BINARY, ANIMATION_MODE_OFF
};

class Animator {

public:
Animator(); // Constructor
~Animator(); // Destructor

// Pointer to client
ofxOPC *opcClient; // TODO: Replace this witha in Interface class encapsulating different types of LED interfaces
void setLedInterface(ofxOPC *interface);

animation_mode_t mode;
void setMode(animation_mode_t m);
void setLedBrightness(int brightness);

// Setters and getters
void setNumLedsPerStrip(int num);
void setNumStrips(int num);
void setFrameSkip(int num);

int getNumLedsPerStrip();
int getNumStrips();
int getFrameSkip();


vector <ofColor> getPixels();

Expand All @@ -42,21 +56,28 @@ class Animator {
void chase();
void setAllLEDColours(ofColor col);
void test();
void binaryAnimation();

// vector <BinaryPattern> binaryPatterns; // TODO make this dynamic

vector <LED> leds;

private:

vector <ofColor> pixels;
// vector <ofColor> pixels;

int ledIndex; // Index of LED being mapped (lit and detected).
int numLedsPerStrip; // Number of LEDs per strip
int numStrips; // How many strips total
// Used for LED test pattern toggle
int ledBrightness; // Brightness of LED's in the animation sequence. Currently hard-coded but
// will be determined by camera frame brightness (to avoid flaring by
// excessively bright LEDs).


int testIndex; // Used for the test() animation sequence
int frameCount; // Internal framecounter
int frameSkip; // How many frames to skip between updates
void resetPixels(); // Reassign pixels vector to fit numLedsPerStrip * numStrips

void populateLeds(); // Create the leds vector

};

Expand Down
Loading

0 comments on commit 186c88c

Please sign in to comment.