From 79586fbc660758642151940dd47fb6b69725cac8 Mon Sep 17 00:00:00 2001 From: Felix Pankratz Date: Tue, 10 Dec 2024 19:17:51 +0100 Subject: [PATCH] ignore the blurring for now... --- organism/src/main.cpp | 60 ++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/organism/src/main.cpp b/organism/src/main.cpp index 57f03a3..8bd7c2e 100644 --- a/organism/src/main.cpp +++ b/organism/src/main.cpp @@ -33,7 +33,7 @@ MatrixPanel_I2S_DMA *matrix = nullptr; -#define NUM_AGENTS 64 +#define NUM_AGENTS 128 #define AGENT_MOVE_DISTANCE 1 #define AGENT_DROP_AMOUNT 1 #define AGENT_ANGLE 0.175 @@ -44,18 +44,18 @@ typedef struct { float heading; } SlimeAgent; -uint8_t attractant[PANEL_WIDTH][PANEL_HEIGHT] = {}; +float attractant[PANEL_WIDTH][PANEL_HEIGHT] = {}; int iterations = 0; SlimeAgent agents[NUM_AGENTS]; void init_attractant() { - memset(attractant, (uint8_t) 0, sizeof attractant); + memset(attractant, 0.0, sizeof attractant); // draw a circle out of attractant //for (int p = 0; p < 64; p++) { // float angle = ((PI * 2) / 64) * p; // int x = 31 + round(10 * sin(angle)); // int y = 15 + round(10 * cos(angle)); - // attractant[x][y] = 15; + // attractant[x][y] = 15.0; //} } @@ -66,8 +66,10 @@ void init_agents() { //agents[a].y_position = 15 + round(10*cos(angle)); // agents[a].heading = ((PI * 2) / NUM_AGENTS) * ); - agents[a].x_position = random(12, 52); - agents[a].y_position = random(12, 20); + //agents[a].x_position = random(12, 52); + //agents[a].y_position = random(12, 20); + agents[a].x_position = random(0, 64); + agents[a].y_position = random(0, 32); agents[a].heading = (random(0, 100) / 100.0) * (PI*2); } } @@ -76,6 +78,9 @@ void init_agents() { void setup(){ Serial.begin(BAUD_RATE); + //while (!Serial) { + // ; // wait for serial port to connect. Needed for native USB + //} pinMode(ONBOARD_LED, OUTPUT); // redefine pins if required @@ -118,11 +123,11 @@ void draw_bg() { void draw_attractant() { for(int x = 0; x 0) { + if (attractant[x][y] > 20.0) { matrix->drawPixel(x, y, matrix->color565( - int((255.0 / 255.0) * attractant[x][y]), - int((80.0 / 255.0) * attractant[x][y]), - int((83.0 / 255.0) * attractant[x][y]) + int((255.0 / 255.0) * round(attractant[x][y])), + int((80.0 / 255.0) * round(attractant[x][y])), + int((83.0 / 255.0) * round(attractant[x][y])) ) ); } @@ -145,7 +150,7 @@ void setupGaussianKernel(float* kernel, int width, float sigma) { #define GAUSS_WIDTH 5 #define GAUSS_SIGMA 1.0 -#define DECAY_FACTOR 0.9 +#define DECAY_FACTOR 0.95 void gaussian_blur() { float kernel[GAUSS_WIDTH]; uint8_t first_pass[PANEL_WIDTH][PANEL_HEIGHT]; @@ -184,20 +189,22 @@ void gaussian_blur() { additions++; } } - attractant[x][y] = (int) round((sum/GAUSS_WIDTH) * DECAY_FACTOR); + attractant[x][y] = (sum/GAUSS_WIDTH) * DECAY_FACTOR; } } } #define BLUR_KERNEL_SIZE 3 +float first_pass[PANEL_WIDTH][PANEL_HEIGHT]; void box_blur() { - uint8_t first_pass[PANEL_WIDTH][PANEL_HEIGHT]; - memset(first_pass, (uint8_t) 0, sizeof first_pass); - + //Serial.println("memset: starting"); + memset(first_pass, 0.0, sizeof first_pass); + //Serial.println("memset: passed"); // horizontal pass for (int x = 0; x < PANEL_WIDTH; x++) { for(int y = 0; y < PANEL_HEIGHT; y++) { - int sum = attractant[x][y]; //0.0; + float sum; + sum = attractant[x][y]; //0.0; int additions = 1; for (int x_offset = 1; x_offset < BLUR_KERNEL_SIZE; x_offset++) { if (is_in_bounds(x+x_offset, y)) { @@ -209,7 +216,7 @@ void box_blur() { additions++; } } - first_pass[x][y] = (int) round((sum/additions) * DECAY_FACTOR); + first_pass[x][y] = (sum/additions); } } // vertical pass @@ -227,7 +234,11 @@ void box_blur() { additions++; } } - attractant[x][y] = (int) round((sum/additions) * DECAY_FACTOR); + + 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] = attractant[x][y] * DECAY_FACTOR; } } } @@ -252,7 +263,7 @@ void loop() { if (is_in_bounds(move_x, move_y)) { agents[a].x_position = move_x; agents[a].y_position = move_y; - if (attractant[move_x][move_y] != 255) { + if (attractant[move_x][move_y] < 254.0) { attractant[move_x][move_y] += AGENT_DROP_AMOUNT; } } else { @@ -265,9 +276,9 @@ void loop() { int left_y = round(agents[a].y_position + sin(agents[a].heading - (AGENT_ANGLE * (PI * 2)))); int right_x = round(agents[a].x_position + cos(agents[a].heading + (AGENT_ANGLE * (PI * 2)))); int right_y = round(agents[a].y_position + sin(agents[a].heading + (AGENT_ANGLE * (PI * 2)))); - int front_value = attractant[front_x][front_y]; - int left_value = attractant[left_x][left_y]; - int right_value = attractant[right_x][right_y]; + float front_value = attractant[front_x][front_y]; + float left_value = attractant[left_x][left_y]; + float right_value = attractant[right_x][right_y]; if (front_value > right_value and front_value > left_value) { ; } else if (front_value < right_value and front_value < left_value) { @@ -280,9 +291,10 @@ void loop() { } delay(10); iterations++; - if (iterations % 64 == 0) { + if (iterations > 64 && iterations % 64 == 0) { // (( == 0) { //gaussian_blur(); - box_blur(); + //box_blur(); + ; } if (iterations >= NUM_ITERATIONS) { init_attractant();