🧹 add cleanup routine

This commit is contained in:
Felix Pankratz 2024-12-14 13:17:12 +01:00
parent b9bd58f34b
commit e71df69612

View File

@ -36,12 +36,9 @@
MatrixPanel_I2S_DMA *matrix = nullptr;
//#define NUM_AGENTS 300
//#define AGENT_MOVE_DISTANCE 1
#define AGENT_DROP_AMOUNT 1 //5
//#define AGENT_ANGLE 0.175
#define NUM_ITERATIONS 1000
#define AGENT_DROP_AMOUNT 10
#define NUM_ITERATIONS 500
struct SlimeAgent {
int x_position;
int y_position;
@ -57,7 +54,6 @@ float AGENT_SENSOR_ANGLE;
float attractant[PANEL_WIDTH][PANEL_HEIGHT] = {};
int iterations = 0;
//SlimeAgent agents[NUM_AGENTS];
std::vector<SlimeAgent> agents;
void init_attractant() {
@ -107,10 +103,15 @@ void init_agents() {
//agents[a].x_position = random(12, 52);
//agents[a].y_position = random(12, 20);
agents.push_back({
random(0,64),
random(0,32),
random(2,63),
random(2,31),
(random(0, 100) / 100.0) * (PI*2)
});
//agents.push_back({
// random(6,58),
// random(4,28),
// (random(0, 100) / 100.0) * (PI*2)
//});
//agents[a].x_position = random(0, 64);
//agents[a].y_position = random(0, 32);
//agents[a].heading = (random(0, 100) / 100.0) * (PI*2);
@ -166,7 +167,7 @@ void draw_bg() {
void draw_attractant() {
for(int x = 0; x<PANEL_WIDTH; x++) {
for(int y = 0; y<PANEL_HEIGHT; y++){
if (attractant[x][y] > 20.0) {
if (attractant[x][y] > 17.0) {
matrix->drawPixel(x, y, matrix->color565(
int((255.0 / 255.0) * round(attractant[x][y])),
int((80.0 / 255.0) * round(attractant[x][y])),
@ -188,7 +189,6 @@ void setupGaussianKernel(float* kernel, int width, float sigma) {
for(int i; i < width; i++) {
kernel[i] = sum/width;
}
Serial.printf("%f %f %f\n", kernel[0], kernel[1], kernel[2]);
}
#define GAUSS_WIDTH 1
@ -197,7 +197,6 @@ void setupGaussianKernel(float* kernel, int width, float sigma) {
float first_pass[PANEL_WIDTH][PANEL_HEIGHT];
void gaussian_blur() {
float kernel[GAUSS_WIDTH];
// first_pass[PANEL_WIDTH][PANEL_HEIGHT];
memset(first_pass, 0.0, sizeof first_pass);
setupGaussianKernel(kernel, GAUSS_WIDTH, GAUSS_SIGMA);
// horizontal pass
@ -279,17 +278,41 @@ void box_blur() {
}
float result = (sum/additions); // * DECAY_FACTOR;
//Serial.printf("result: %f", result); //additions: %d\n", sum, additions);
//attractant[x][y] = (((8.0*attractant[x][y]) + result) / 9.0);
attractant[x][y] = result;
}
}
}
int cleanup_x = 0;
void cleanup() {
int progress = iterations - NUM_ITERATIONS;
matrix->drawFastVLine(cleanup_x, 0, PANEL_HEIGHT, matrix->color565(254, 242, 255));
if (cleanup_x > 0) {
matrix->fillRect(0, 0, cleanup_x, PANEL_HEIGHT, matrix->color565(16, 0, 16));
}
int h_lines = round(PANEL_HEIGHT / 7);
int v_lines = round(cleanup_x / 7);
for(int v = 0; v < cleanup_x; v++) {
if (v%7 == 2) {
matrix->drawFastVLine(v, 0, PANEL_HEIGHT, matrix->color565(41, 17, 76));
}
}
for(int h = 0; h <= h_lines; h++) {
matrix->drawFastHLine(0, 7*h + 1, cleanup_x-1, matrix->color565(41, 17, 76));
}
cleanup_x++;
if (cleanup_x > PANEL_WIDTH) {
iterations = 0;
cleanup_x = 0;
init_attractant();
init_agents();
}
}
void loop() {
matrix ->flipDMABuffer();
delay(10);
//matrix->clearScreen();
matrix->fillScreenRGB888(15, 0, 10);
// draw background and organism
@ -298,8 +321,8 @@ void loop() {
for(int a = 0; a < NUM_AGENTS; a++) {
//matrix->drawPixel(agents[a].x_position, agents[a].y_position, matrix->color565(0, 0, attractant[agents[a].x_position][agents[a].y_position]));
// motor step
// check if agent can move forward
// motor step: check if agent can move forward
// if yes, do so, if not, turn randomly
float target_x = agents[a].x_position + cos(agents[a].heading) * AGENT_MOVE_DISTANCE;
float target_y = agents[a].y_position + sin(agents[a].heading) * AGENT_MOVE_DISTANCE;
int move_x = round(target_x);
@ -312,10 +335,9 @@ void loop() {
}
} else {
agents[a].heading = (random(0, 100)/100.0) * (PI*2);
// agents[a].heading = (2*PI) - agents[a].heading;//(random(0, 100)/100.0) * (PI*2);
}
}
// sample step
// sample step - check the 3 positions
for(int a = 0; a < NUM_AGENTS; a++) {
//matrix->drawPixel(agents[a].x_position, agents[a].y_position, matrix->color565(0, 0, attractant[agents[a].x_position][agents[a].y_position]));
int front_x = round(agents[a].x_position + cos(agents[a].heading) * AGENT_SENSOR_DISTANCE);
@ -338,15 +360,13 @@ void loop() {
}
}
iterations++;
if (iterations % 4 == 0) { //64 && iterations % 64 == 0) { // (( == 0) {
//box_blur();
if (iterations % 2 == 0) { //64 && iterations % 64 == 0) { // (( == 0) {
box_blur();
//gaussian_blur();
;
}
if (iterations >= NUM_ITERATIONS) {
init_attractant();
init_agents();
iterations = 0;
cleanup();
}
}