added thread for process, aggregation, print

This commit is contained in:
aj 2020-11-16 22:05:44 +00:00
parent 120e9cb80d
commit 111481b37d
7 changed files with 1517 additions and 1443 deletions

View File

@ -2,31 +2,38 @@
#define _BUFFER_GUARD #define _BUFFER_GUARD
#include "util.h" #include "util.h"
#include "math.h"
float*
getBuffer(int size)
{
return malloc(size * sizeof(float));
}
void void
aggregateBuffer(float *bufferIn, int lengthIn, float *bufferOut, int lengthOut, int groupSize) aggregateBuffer(float *bufferIn, int lengthIn, float *bufferOut, int lengthOut, int groupSize)
{ {
int requiredGroups = ceil((float)lengthIn/groupSize); int requiredGroups = ceil((float)lengthIn/groupSize); // number of groups
int finalGroupSize = (lengthIn % groupSize) * groupSize; int finalGroupSize = (lengthIn % groupSize) * groupSize;
if(requiredGroups > lengthOut) if(requiredGroups > lengthOut) // error
{ {
putFloat((float)lengthIn/groupSize); putFloat((float)lengthIn/groupSize);
printf(" length out buffer required, %i provided\n", lengthOut); printf(" length out buffer required, %i provided\n", lengthOut);
return; return;
} }
int g;// for group number int g; // for group number
float *inputPtr = bufferIn; float *inputPtr = bufferIn; // cursor for full buffer
float *outputPtr = bufferOut; float *outputPtr = bufferOut; // cursor for output buffer
for(g = 0; g < requiredGroups; g++) for(g = 0; g < requiredGroups; g++)
{ {
int length = groupSize; int length = groupSize; // length of this group's size
if(g == requiredGroups - 1 && finalGroupSize != 0) length = finalGroupSize; if(g == requiredGroups - 1 && finalGroupSize != 0) length = finalGroupSize; // shorten if necessary
*outputPtr = calculateMean(inputPtr, length); *outputPtr = calculateMean(inputPtr, length); // SET
inputPtr += length; inputPtr += length; // increment both
outputPtr++; outputPtr++;
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,7 @@
<simulation> <simulation>
<title>Aggregator Coursework</title> <title>Aggregator Coursework</title>
<speedlimit>1.0</speedlimit> <speedlimit>1.0</speedlimit>
<randomseed>123456</randomseed> <randomseed>123461</randomseed>
<motedelay_us>1000000</motedelay_us> <motedelay_us>1000000</motedelay_us>
<radiomedium> <radiomedium>
se.sics.cooja.radiomediums.UDGM se.sics.cooja.radiomediums.UDGM
@ -61,11 +61,11 @@
</simulation> </simulation>
<plugin> <plugin>
se.sics.cooja.plugins.SimControl se.sics.cooja.plugins.SimControl
<width>417</width> <width>321</width>
<z>0</z> <z>0</z>
<height>160</height> <height>160</height>
<location_x>400</location_x> <location_x>48</location_x>
<location_y>0</location_y> <location_y>710</location_y>
</plugin> </plugin>
<plugin> <plugin>
se.sics.cooja.plugins.Visualizer se.sics.cooja.plugins.Visualizer
@ -73,11 +73,11 @@
<skin>se.sics.cooja.plugins.skins.IDVisualizerSkin</skin> <skin>se.sics.cooja.plugins.skins.IDVisualizerSkin</skin>
<viewport>0.9090909090909091 0.0 0.0 0.9090909090909091 153.72871631902638 172.41033658502872</viewport> <viewport>0.9090909090909091 0.0 0.0 0.9090909090909091 153.72871631902638 172.41033658502872</viewport>
</plugin_config> </plugin_config>
<width>393</width> <width>314</width>
<z>4</z> <z>5</z>
<height>430</height> <height>179</height>
<location_x>1</location_x> <location_x>40</location_x>
<location_y>1</location_y> <location_y>20</location_y>
</plugin> </plugin>
<plugin> <plugin>
se.sics.cooja.plugins.TimeLine se.sics.cooja.plugins.TimeLine
@ -89,11 +89,11 @@
<split>-1</split> <split>-1</split>
<zoomfactor>500.0</zoomfactor> <zoomfactor>500.0</zoomfactor>
</plugin_config> </plugin_config>
<width>397</width> <width>1217</width>
<z>3</z> <z>3</z>
<height>443</height> <height>183</height>
<location_x>0</location_x> <location_x>424</location_x>
<location_y>433</location_y> <location_y>852</location_y>
</plugin> </plugin>
<plugin> <plugin>
se.sics.cooja.plugins.Notes se.sics.cooja.plugins.Notes
@ -102,7 +102,7 @@
<decorations>true</decorations> <decorations>true</decorations>
</plugin_config> </plugin_config>
<width>6</width> <width>6</width>
<z>5</z> <z>6</z>
<height>160</height> <height>160</height>
<location_x>680</location_x> <location_x>680</location_x>
<location_y>0</location_y> <location_y>0</location_y>
@ -115,10 +115,10 @@
<scrollpos>0,0</scrollpos> <scrollpos>0,0</scrollpos>
</plugin_config> </plugin_config>
<width>1201</width> <width>1201</width>
<z>1</z> <z>2</z>
<height>706</height> <height>706</height>
<location_x>397</location_x> <location_x>408</location_x>
<location_y>162</location_y> <location_y>15</location_y>
</plugin> </plugin>
<plugin> <plugin>
se.sics.cooja.plugins.MoteInterfaceViewer se.sics.cooja.plugins.MoteInterfaceViewer
@ -128,10 +128,23 @@
<scrollpos>0,0</scrollpos> <scrollpos>0,0</scrollpos>
</plugin_config> </plugin_config>
<width>397</width> <width>397</width>
<z>2</z> <z>1</z>
<height>179</height> <height>179</height>
<location_x>2</location_x> <location_x>2</location_x>
<location_y>247</location_y> <location_y>247</location_y>
</plugin> </plugin>
<plugin>
se.sics.cooja.plugins.MoteInterfaceViewer
<mote_arg>0</mote_arg>
<plugin_config>
<interface>Sky LED</interface>
<scrollpos>0,0</scrollpos>
</plugin_config>
<width>317</width>
<z>4</z>
<height>206</height>
<location_x>28</location_x>
<location_y>445</location_y>
</plugin>
</simconf> </simconf>

View File

@ -1,40 +1,89 @@
#define READING_INTERVAL 3 //in Hz #define READING_INTERVAL 1 //in Hz
#define BUFFER_SIZE 12 // length of buffer to populate #define BUFFER_SIZE 12 // length of buffer to populate
#define SD_THRESHOLD 0 // whether to aggregate or flatten #define SD_THRESHOLD 3 // whether to aggregate or flatten
#define AGGREGATION_GROUP_SIZE 4 // group size to aggregate (4 in spec) #define AGGREGATION_GROUP_SIZE 4 // group size to aggregate (4 in spec)
#include "contiki.h" #include "contiki.h"
#include "dev/light-sensor.h"
#include <stdio.h> /* For printf() */ #include <stdio.h> /* For printf() */
#include "io.h"
#include "util.h" // for print methods #include "util.h" // for print methods
#include "math.h" #include "math.h"
#include "buffer.h" #include "buffer.h"
//text to uncomment and make the sim reload new source // text to uncomment and make the sim reload new source
// get float from light sensor including transfer function static process_event_t event_buffer_full;
float
getLight(void) /*---------------------------------------------------------------------------*/
PROCESS(sensing_process, "Sensing process");
PROCESS(aggregator_process, "Aggregator process");
AUTOSTART_PROCESSES(&sensing_process, &aggregator_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(sensing_process, ev, data)
{ {
int lightData = light_sensor.value(LIGHT_SENSOR_PHOTOSYNTHETIC); /*INIT*/
static struct etimer timer;
PROCESS_BEGIN();
event_buffer_full = process_alloc_event();
etimer_set(&timer, CLOCK_SECOND/READING_INTERVAL);
float V_sensor = 1.5 * lightData / 4096; SENSORS_ACTIVATE(light_sensor);
float I = V_sensor/1e5; leds_off(LEDS_ALL);
float light = 0.625 * 1e6 * I * 1000;
return light; static float* buffer;
buffer = getBuffer(BUFFER_SIZE);
clearBuffer(buffer, BUFFER_SIZE);
printBuffer(buffer, BUFFER_SIZE);putchar('\n');putchar('\n');
/*END INIT*/
static int counter = 0;
while(1)
{
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
leds_off(LEDS_RED);
float light_lx = getLight(); // GET
buffer[counter] = light_lx; // STORE
printf("%2i/%i: ", counter + 1, BUFFER_SIZE);putFloat(light_lx);putchar('\n'); // DISPLAY VALUE
//printBuffer(buffer, BUFFER_SIZE);putchar('\n'); // DISPLAY CURRENT BUFFER
counter++;
if(counter == BUFFER_SIZE) // CHECK WHETHER FULL
{
process_post(&aggregator_process, event_buffer_full, &buffer);
counter = 0;
buffer = getBuffer(BUFFER_SIZE);
}
etimer_reset(&timer);
}
PROCESS_END();
} }
/*---------------------------------------------------------------------------*/
// Process final buffer following aggregation PROCESS_THREAD(aggregator_process, ev, data)
void
handleFinalBuffer(float *buffer, int length)
{ {
printf("Final buffer output: "); PROCESS_BEGIN();
printBuffer(buffer, length);putchar('\n');putchar('\n');
}
while (1)
{
PROCESS_WAIT_EVENT_UNTIL(ev == event_buffer_full);
leds_on(LEDS_RED);
float *fullBuffer = *(float **)data;
handleBufferRotation(fullBuffer, BUFFER_SIZE);
free(fullBuffer);
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
// Buffer filled with readings, process and aggregate // Buffer filled with readings, process and aggregate
void void
handleBufferRotation(float *buffer, int length) handleBufferRotation(float *buffer, int length)
@ -44,67 +93,29 @@ handleBufferRotation(float *buffer, int length)
Stats sd = calculateStdDev(buffer, length); // GET BUFFER STATISTICS Stats sd = calculateStdDev(buffer, length); // GET BUFFER STATISTICS
if(sd.std > SD_THRESHOLD) if(sd.std > SD_THRESHOLD)
{// buffer length by 4 {// buffer length by 4
printf("Significant STD: "); printf("Significant STD: ");putFloat(sd.std);printf(", compressing buffer\n");
putFloat(sd.std);
printf(", compressing buffer\n");
float outBuffer[BUFFER_SIZE]; // CREATE OUTPUT BUFFER float *outBuffer = getBuffer(length); // CREATE OUTPUT BUFFER
clearBuffer(&outBuffer, BUFFER_SIZE); clearBuffer(outBuffer, length);
int outLength = ceil((float)length/AGGREGATION_GROUP_SIZE); // CALCULATE NUMBER OF OUTPUT ELEMENTS int outLength = ceil((float)length/AGGREGATION_GROUP_SIZE); // CALCULATE NUMBER OF OUTPUT ELEMENTS
aggregateBuffer(buffer, length, &outBuffer, outLength, AGGREGATION_GROUP_SIZE); aggregateBuffer(buffer, length, outBuffer, outLength, AGGREGATION_GROUP_SIZE);
handleFinalBuffer(&outBuffer, outLength); // PASS FINAL BUFFER handleFinalBuffer(outBuffer, outLength); // PASS FINAL BUFFER
free(outBuffer); // RELEASE
}else }else
{// buffer length to 1 {// buffer length to 1
printf("Insignificant STD: "); printf("Insignificant STD: ");putFloat(sd.std);printf(", squashing buffer\n");
putFloat(sd.std);
printf(", squashing buffer\n");
handleFinalBuffer(&sd.mean, 1); // PASS FINAL BUFFER handleFinalBuffer(&sd.mean, 1); // PASS FINAL BUFFER
} }
clearBuffer(buffer, length);
} }
float buffer[BUFFER_SIZE]; // Process final buffer following aggregation
void
/*---------------------------------------------------------------------------*/ handleFinalBuffer(float *buffer, int length)
PROCESS(aggregator_process, "Aggregator process");
AUTOSTART_PROCESSES(&aggregator_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(aggregator_process, ev, data)
{ {
/*INIT*/ printf("Final buffer output: ");
static struct etimer timer; printBuffer(buffer, length);putchar('\n');putchar('\n');
PROCESS_BEGIN();
etimer_set(&timer, CLOCK_SECOND/READING_INTERVAL);
SENSORS_ACTIVATE(light_sensor);
clearBuffer(&buffer, BUFFER_SIZE);
printBuffer(&buffer, BUFFER_SIZE);putchar('\n');putchar('\n');
/*END INIT*/
static int counter = 0;
while(1)
{
PROCESS_WAIT_EVENT_UNTIL(ev=PROCESS_EVENT_TIMER);
float light_lx = getLight(); // GET
buffer[counter] = light_lx; // STORE
printBuffer(&buffer, BUFFER_SIZE);putchar('\n'); // DISPLAY
counter++;
if(counter == BUFFER_SIZE) // CHECK WHETHER FULL
{
handleBufferRotation(buffer, BUFFER_SIZE); // PASS OUT IF FULL
counter = 0;
}
etimer_reset(&timer);
}
PROCESS_END();
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/

Binary file not shown.

19
Coursework/io.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef _IO_GUARD
#define _IO_GUARD
#include "dev/light-sensor.h"
#include "dev/leds.h"
// get float from light sensor including transfer function
float
getLight(void)
{
int lightData = light_sensor.value(LIGHT_SENSOR_PHOTOSYNTHETIC);
float V_sensor = 1.5 * lightData / 4096;
float I = V_sensor/1e5;
float light = 0.625 * 1e6 * I * 1000;
return light;
}
#endif

View File

@ -7,7 +7,7 @@ typedef struct Stats {
} Stats; } Stats;
int int
ceil(float in) ceil(float in) // self-implement ceil func, no math.h
{ {
int num = (int) in; int num = (int) in;
if(in - num > 0) num++; if(in - num > 0) num++;
@ -15,7 +15,7 @@ ceil(float in)
} }
float float
sqrt(float in) sqrt(float in) // self-implement ceil sqrt, no math.h
{ {
float sqrt = in/2; float sqrt = in/2;
float temp = 0; float temp = 0;