Skip to content
Snippets Groups Projects
Commit 46d8f417 authored by jamesimmanuel.magsino@stud.h-da.de's avatar jamesimmanuel.magsino@stud.h-da.de
Browse files

Fixed average

parent a29c1671
No related branches found
No related tags found
No related merge requests found
...@@ -4,9 +4,8 @@ ...@@ -4,9 +4,8 @@
#include <unistd.h> #include <unistd.h>
#include <hardware/pwm.h> #include <hardware/pwm.h>
double percentage_1 = 0; double rollingperc[4] = {0, 0, 0, 0};
double percentage_2 = 0; int pos = 0;
bool flipflop = false;
const uint8_t init[4] = {0x21, 0xa0, 0xef, 0x81}; const uint8_t init[4] = {0x21, 0xa0, 0xef, 0x81};
const uint8_t zeile[8] = {0x00, 0x02, 0x04, 0x06, 0x08, 0x10, 0x12, 0x14}; const uint8_t zeile[8] = {0x00, 0x02, 0x04, 0x06, 0x08, 0x10, 0x12, 0x14};
...@@ -165,18 +164,14 @@ void callback(uint gpio, uint32_t events) ...@@ -165,18 +164,14 @@ void callback(uint gpio, uint32_t events)
volatile int percentage = (distance / 10) * 100; volatile int percentage = (distance / 10) * 100;
if (flipflop) rollingperc[pos] = percentage;
pos++;
if (pos > 3)
{ {
percentage_1 = percentage; pos = 0;
flipflop = false;
}
else
{
percentage_2 = percentage;
flipflop = true;
} }
int avg = (percentage + percentage_1 + percentage_2) / 3; int avg = (rollingperc[0] + rollingperc[1] + rollingperc[2] + rollingperc[3]) / 4;
// if zwischen 90, 110, stop // if zwischen 90, 110, stop
if (avg > 90 && avg < 110) if (avg > 90 && avg < 110)
...@@ -192,7 +187,7 @@ void callback(uint gpio, uint32_t events) ...@@ -192,7 +187,7 @@ void callback(uint gpio, uint32_t events)
{ {
motor_backward(); motor_backward();
} }
set_progress((percentage + percentage_1 + percentage_2) / 3); set_progress(avg);
} }
else if (events & GPIO_IRQ_EDGE_RISE) else if (events & GPIO_IRQ_EDGE_RISE)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment