Skip to content
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

New power limiting #19

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/ShapeDisplayManagers/InFormIOManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ InFormIOManager::InFormIOManager() {
// Size the 2d heights array appropriately for the specific shape display hardware, and initialize it with zero values.
// This needs to happen in the subclass constructor because the superclass constructor fires first, and won't yet have the subclass specific constants.
heightsForShapeDisplay.resize(shapeDisplaySizeX, std::vector<unsigned char>(shapeDisplaySizeY, 0));
// Also size the array that receives height values from the shape display.
// Also size the array that receives height values from the shape display, and the "previous" heights
heightsFromShapeDisplay.resize(shapeDisplaySizeX, std::vector<unsigned char>(shapeDisplaySizeY, 0));
previousHeightsForShapeDisplay.resize(shapeDisplaySizeX, std::vector<unsigned char>(shapeDisplaySizeY, 0));

pinHeightMin = 50;
pinHeightMax = 210;
Expand Down
2 changes: 2 additions & 0 deletions src/ShapeDisplayManagers/InFormIOManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class InFormIOManager : public SerialShapeIOManager {
vector<PinLocation> getDisabledPins() {
return {};
}

double getMaxPowerLoad() {return 0.5;}

// should pins that appear stuck be turned off at regular intervals?
bool enableStuckPinSafetyToggle = false;
Expand Down
25 changes: 25 additions & 0 deletions src/ShapeDisplayManagers/SerialShapeIOManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "SerialShapeIOManager.hpp"
#include "constants.h"
#include "utils.hpp"

//--------------------------------------------------------------
//
Expand Down Expand Up @@ -213,6 +214,29 @@ void SerialShapeIOManager::clipAllHeightValuesToBeWithinRange() {
}
}

// Limit all values, to reduce the maximum transient power draw
void SerialShapeIOManager::limitPowerDraw() {
double totalCost = 0;
for(int i = 0; i < shapeDisplaySizeX; i++) {
for(int j = 0; j < shapeDisplaySizeY; j++) {
double pinDiff = heightsForShapeDisplay[i][j] - (float)previousHeightsForShapeDisplay[i][j];
pinDiff /= pinHeightMax - pinHeightMin; // 1 for min -> max, 0 for h -> h, -1 for max -> min
totalCost += abs(pinDiff);
}
}

if (totalCost > shapeDisplaySizeX * shapeDisplaySizeY * getMaxPowerLoad()) {
double scaleRatio = (shapeDisplaySizeX * shapeDisplaySizeY *getMaxPowerLoad())/totalCost;
for(int i = 0; i < shapeDisplaySizeX; i++) {
for(int j = 0; j < shapeDisplaySizeY; j++) {
heightsForShapeDisplay[i][j] = previousHeightsForShapeDisplay[i][j] + (heightsForShapeDisplay[i][j] - (int)previousHeightsForShapeDisplay[i][j]) * scaleRatio;
}
}
}

previousHeightsForShapeDisplay = heightsForShapeDisplay;
}

// Copy data from storage in the 2D array to the corresponding arduino board
// structures. Flip height values where needed to match the board's orientation.
void SerialShapeIOManager::readyDataForArduinos() {
Expand Down Expand Up @@ -266,6 +290,7 @@ void SerialShapeIOManager::update() {

// prepare height data for sending to shape display
clipAllHeightValuesToBeWithinRange();
limitPowerDraw();
readyDataForArduinos();

// send height data. if the display talks back, ask it what it's doing
Expand Down
8 changes: 8 additions & 0 deletions src/ShapeDisplayManagers/SerialShapeIOManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ class SerialShapeIOManager {
// shape display height values (both intended and actual values)
std::vector<std::vector<unsigned char>> heightsForShapeDisplay;
std::vector<std::vector<unsigned char>> heightsFromShapeDisplay;
// make sure to resize is child constructor
std::vector<std::vector<unsigned char>> previousHeightsForShapeDisplay;

// [0, 1] range, the maximum power draw alowable
virtual double getMaxPowerLoad() {return 1.0;}

// prevent over-draw current
void limitPowerDraw();

// pin behavior configurations
std::vector<std::vector<PinConfigs>> pinConfigsForShapeDisplay;
Expand Down
4 changes: 3 additions & 1 deletion src/ShapeDisplayManagers/TransformIOManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

#include "TransformIOManager.hpp"
#include "SerialShapeIOManager.hpp"

// Create new transformIOManager instance, setting up transFORM-specific board
// configuration
Expand All @@ -23,8 +24,9 @@ TransformIOManager::TransformIOManager() {
// Size the 2d heights array appropriately for the specific shape display hardware, and initialize it with zero values.
// This needs to happen in the subclass constructor because the superclass constructor fires first, and won't yet have the subclass specific constants.
heightsForShapeDisplay.resize(shapeDisplaySizeX, std::vector<unsigned char>(shapeDisplaySizeY, 0));
// Also size the array that receives height values from the shape display.
// Also size the array that receives height values from the shape display, and the "previous" heights
heightsFromShapeDisplay.resize(shapeDisplaySizeX, std::vector<unsigned char>(shapeDisplaySizeY, 0));
previousHeightsForShapeDisplay.resize(shapeDisplaySizeX, std::vector<unsigned char>(shapeDisplaySizeY, 0));

pinHeightMin = 50;
pinHeightMax = 210;
Expand Down
36 changes: 36 additions & 0 deletions src/Utils/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,39 @@ double elapsedTimeInSeconds() {
return clock() / (double) CLOCKS_PER_SEC;
}

//ofImage applyFunctionToPixels(const ofImage& first, const ofImage& second, pixelFunction fn) {
// ofImage out = ofImage(first);
//
// ofPixels& outPixels = out.getPixels();
// const ofPixels& secondPixels = second.getPixels();
//
// // weighted average of pixel
// for (int i = 0; i < outPixels.size(); i++) {
// outPixels[i] = fn(outPixels[i], secondPixels[i]);
// }
//
// out.update();
// return out;
//
//}
//
//ofImage absoluteDifference(const ofImage& first, const ofImage& second) {
// return applyFunctionToPixels(first, second, [](unsigned char a, unsigned char b){return (unsigned char)abs(a - int(b));});
//}
//
//ofImage weightedAverage(const ofImage& first, const ofImage& second, double weight) {
// ofImage out = ofImage(first);
// weight = std::clamp(weight, 0.0, 1.0);
//
// ofPixels& outPixels = out.getPixels();
// const ofPixels& secondPixels = second.getPixels();
//
// // weighted average of pixel
// for (int i = 0; i < outPixels.size(); i++) {
// outPixels[i] = (1.0 - weight) * outPixels[i] + weight * secondPixels[i];
// }
//
// out.update();
// return out;
//}

7 changes: 7 additions & 0 deletions src/Utils/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,12 @@

double elapsedTimeInSeconds();

typedef unsigned char (*pixelFunction) (unsigned char, unsigned char);
//ofImage applyFunctionToPixels(const ofImage& first, const ofImage& second, pixelFunction function);
//// weight * second + (1 - weight) * first ----- with [0, 1] bounds on weight
//ofImage weightedAverage(const ofImage& first, const ofImage& second, double weight);
//// the absolute value of the difference between each pixel
//ofImage absoluteDifference(ofImage first, ofImage second);

#endif /* utils_hpp */