beginning coursework

This commit is contained in:
aj 2020-10-29 17:33:18 +00:00
parent 50db86ee9e
commit ceb45b8a2e
11 changed files with 3326 additions and 0 deletions

7
Coursework/Makefile Normal file
View File

@ -0,0 +1,7 @@
CONTIKI_PROJECT = coursework
all: $(CONTIKI_PROJECT)
#UIP_CONF_IPV6=1
CONTIKI = ../..
include $(CONTIKI)/Makefile.include

60
Coursework/buffer.h Normal file
View File

@ -0,0 +1,60 @@
#ifndef _BUFFER_GUARD
#define _BUFFER_GUARD
#include "util.h"
void
aggregateBuffer(float bufferIn[], int lengthIn, float bufferOut[], int lengthOut, int groupSize)
{
int requiredGroups = ceil((float)lengthIn/groupSize);
int finalGroupSize = (lengthIn % groupSize) * groupSize;
if(requiredGroups > lengthOut)
{
putFloat((float)lengthIn/groupSize);
printf(" length out buffer required, %i provided", lengthOut);
return;
}
int g;// for group number
for(g = 0; g < requiredGroups; g++)
{
int length = groupSize;
if(g == requiredGroups - 1 && finalGroupSize != 0) length = finalGroupSize;
bufferOut[g] = calculateMean(&bufferIn + (g*groupSize), length);
}
}
void
clearBuffer(float buffer[], int length)
{
if(length > 0)
{
int i;
for(i = 0; i < length; i++)
{
buffer[i] = 0.0;
}
}
}
void
printBuffer(float buffer[], int length)
{
putchar('[');
if(length > 0)
{
int i;
for(i = 0; i < length; i++)
{
if(i > 0) printf(", ");
putFloat(buffer[i]);
}
}
putchar(']');
}
#endif

BIN
Coursework/contiki-sky.a Normal file

Binary file not shown.

2896
Coursework/contiki-sky.map Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,137 @@
<?xml version="1.0" encoding="UTF-8"?>
<simconf>
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/mrm</project>
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/mspsim</project>
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/avrora</project>
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/serial_socket</project>
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/collect-view</project>
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/powertracker</project>
<simulation>
<title>Aggregator Coursework</title>
<speedlimit>1.0</speedlimit>
<randomseed>123456</randomseed>
<motedelay_us>1000000</motedelay_us>
<radiomedium>
se.sics.cooja.radiomediums.UDGM
<transmitting_range>50.0</transmitting_range>
<interference_range>100.0</interference_range>
<success_ratio_tx>1.0</success_ratio_tx>
<success_ratio_rx>1.0</success_ratio_rx>
</radiomedium>
<events>
<logoutput>40000</logoutput>
</events>
<motetype>
se.sics.cooja.mspmote.SkyMoteType
<identifier>sky1</identifier>
<description>Sky Mote Type #sky1</description>
<source EXPORT="discard">[CONTIKI_DIR]/surrey/Coursework/coursework.c</source>
<commands EXPORT="discard">make coursework.sky TARGET=sky</commands>
<firmware EXPORT="copy">[CONTIKI_DIR]/surrey/Coursework/coursework.sky</firmware>
<moteinterface>se.sics.cooja.interfaces.Position</moteinterface>
<moteinterface>se.sics.cooja.interfaces.RimeAddress</moteinterface>
<moteinterface>se.sics.cooja.interfaces.IPAddress</moteinterface>
<moteinterface>se.sics.cooja.interfaces.Mote2MoteRelations</moteinterface>
<moteinterface>se.sics.cooja.interfaces.MoteAttributes</moteinterface>
<moteinterface>se.sics.cooja.mspmote.interfaces.MspClock</moteinterface>
<moteinterface>se.sics.cooja.mspmote.interfaces.MspMoteID</moteinterface>
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyButton</moteinterface>
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyFlash</moteinterface>
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyCoffeeFilesystem</moteinterface>
<moteinterface>se.sics.cooja.mspmote.interfaces.Msp802154Radio</moteinterface>
<moteinterface>se.sics.cooja.mspmote.interfaces.MspSerial</moteinterface>
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyLED</moteinterface>
<moteinterface>se.sics.cooja.mspmote.interfaces.MspDebugOutput</moteinterface>
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyTemperature</moteinterface>
</motetype>
<mote>
<breakpoints />
<interface_config>
se.sics.cooja.interfaces.Position
<x>29.99841204907099</x>
<y>-115.95137024353158</y>
<z>0.0</z>
</interface_config>
<interface_config>
se.sics.cooja.mspmote.interfaces.MspMoteID
<id>1</id>
</interface_config>
<motetype_identifier>sky1</motetype_identifier>
</mote>
</simulation>
<plugin>
se.sics.cooja.plugins.SimControl
<width>417</width>
<z>0</z>
<height>160</height>
<location_x>400</location_x>
<location_y>0</location_y>
</plugin>
<plugin>
se.sics.cooja.plugins.Visualizer
<plugin_config>
<skin>se.sics.cooja.plugins.skins.IDVisualizerSkin</skin>
<viewport>0.9090909090909091 0.0 0.0 0.9090909090909091 153.72871631902638 172.41033658502872</viewport>
</plugin_config>
<width>393</width>
<z>4</z>
<height>430</height>
<location_x>1</location_x>
<location_y>1</location_y>
</plugin>
<plugin>
se.sics.cooja.plugins.TimeLine
<plugin_config>
<mote>0</mote>
<showRadioRXTX />
<showRadioHW />
<showLEDs />
<split>-1</split>
<zoomfactor>500.0</zoomfactor>
</plugin_config>
<width>397</width>
<z>3</z>
<height>443</height>
<location_x>0</location_x>
<location_y>433</location_y>
</plugin>
<plugin>
se.sics.cooja.plugins.Notes
<plugin_config>
<notes>Enter notes here</notes>
<decorations>true</decorations>
</plugin_config>
<width>6</width>
<z>5</z>
<height>160</height>
<location_x>680</location_x>
<location_y>0</location_y>
</plugin>
<plugin>
se.sics.cooja.plugins.MoteInterfaceViewer
<mote_arg>0</mote_arg>
<plugin_config>
<interface>Serial port</interface>
<scrollpos>0,0</scrollpos>
</plugin_config>
<width>1201</width>
<z>1</z>
<height>706</height>
<location_x>397</location_x>
<location_y>162</location_y>
</plugin>
<plugin>
se.sics.cooja.plugins.MoteInterfaceViewer
<mote_arg>0</mote_arg>
<plugin_config>
<interface>Temperature and Light</interface>
<scrollpos>0,0</scrollpos>
</plugin_config>
<width>397</width>
<z>2</z>
<height>179</height>
<location_x>2</location_x>
<location_y>247</location_y>
</plugin>
</simconf>

107
Coursework/coursework.c Normal file
View File

@ -0,0 +1,107 @@
#define READING_INTERVAL 3 //in Hz
#define BUFFER_SIZE 5
#define SD_THRESHOLD 3
#define AGGREGATION_GROUP_SIZE 4
#include "contiki.h"
#include "dev/light-sensor.h"
#include <stdio.h> /* For printf() */
#include "util.h" // for print methods
#include "math.h"
#include "buffer.h"
// get float from light sensor including T 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;
}
// Process final buffer following aggregation
void
handleFinalBuffer(float buffer[], int length)
{
printf("Final buffer output: ");
printBuffer(buffer, length);putchar('\n');putchar('\n');
}
// Buffer filled with readings, process and aggregate
void
handleBufferRotation(float buffer[], int length)
{
printf("Buffer full, aggregating\n\n");
float aggregated[BUFFER_SIZE];
Stats sd = calculateStdDev(buffer, length);
if(sd.std > SD_THRESHOLD)
{// buffer length by 4
printf("Significant STD: ");
putFloat(sd.std);
printf(", compressing buffer\n");
float outBuffer[BUFFER_SIZE];
int outLength = ceil((float)length/AGGREGATION_GROUP_SIZE);
aggregateBuffer(buffer, length, outBuffer, outLength, AGGREGATION_GROUP_SIZE);
handleFinalBuffer(outBuffer, outLength);
}else
{// buffer length to 1
printf("Insignificant STD: ");
putFloat(sd.std);
printf(", squashing buffer\n");
handleFinalBuffer(&sd.mean, 1);
}
clearBuffer(buffer, length);
}
float buffer[BUFFER_SIZE];
/*---------------------------------------------------------------------------*/
PROCESS(aggregator_process, "Aggregator process");
AUTOSTART_PROCESSES(&aggregator_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(aggregator_process, ev, data)
{
/*INIT*/
static struct etimer timer;
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();
buffer[counter] = light_lx;
printBuffer(buffer, BUFFER_SIZE);putchar('\n');
counter++;
if(counter == BUFFER_SIZE)
{
handleBufferRotation(buffer, BUFFER_SIZE);
counter = 0;
}
etimer_reset(&timer);
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/

BIN
Coursework/coursework.sky Normal file

Binary file not shown.

75
Coursework/math.h Normal file
View File

@ -0,0 +1,75 @@
#ifndef _MATH_GUARD
#define _MATH_GUARD
typedef struct Stats {
float mean;
float std;
} Stats;
int
ceil(float in)
{
int num = (int) in;
if(in - num > 0) num++;
return num;
}
float
sqrt(float in)
{
float sqrt = in/2;
float temp = 0;
while(sqrt != temp)
{
temp = sqrt;
sqrt = (in/temp + temp) / 2;
}
return sqrt;
}
float
calculateMean(float buffer[], int length)
{
if(length <= 0)
{
printf("%i items is not valid length\n", length);
return 0;
}
float sum = 0;
int i;
for(i = 0; i < length; i++)
{
sum += buffer[i];
}
return sum / length;
}
Stats
calculateStdDev(float buffer[], int length)
{
Stats stats;
if(length <= 0)
{
printf("%i items is not valid length\n", length);
return stats;
}
stats.mean = calculateMean(buffer, length);
float sum = 0;
int i;
for(i = 0; i < length; i++)
{
float diffFromMean = buffer[i] - stats.mean;
sum += diffFromMean*diffFromMean;
}
stats.std = sqrt(sum/length);
return stats;
}
#endif

4
Coursework/symbols.c Normal file
View File

@ -0,0 +1,4 @@
#include "symbols.h"
const int symbols_nelts = 0;
const struct symbols symbols[] = {{0,0}};

3
Coursework/symbols.h Normal file
View File

@ -0,0 +1,3 @@
#include "loader/symbols.h"
extern const struct symbols symbols[1];

37
Coursework/util.h Normal file
View File

@ -0,0 +1,37 @@
#ifndef _UTIL_GUARD
#define _UTIL_GUARD
typedef unsigned short USHORT;
//print a unsigned short (as returned from rand) picewise char by char
void
putShort(USHORT in)
{
// recursively shift each digit of the int to units from most to least significant
if (in >= 10)
{
putShort(in / 10);
}
// isolate unit digit from each number by modulo and add '0' char to turn integer into corresponding ascii char
putchar((in % 10) + '0');
}
void
putFloat(float in)
{
if(in < 0)
{
putchar('-'); // print negative sign if required
in = -in;
}
USHORT integerComponent = (USHORT) in; // truncate float to integer
float fractionComponent = (in - integerComponent) * 1000; // take fraction only and promote to integer
if (fractionComponent - (USHORT)fractionComponent >= 0.5) fractionComponent++; // round
putShort(integerComponent);
putchar('.');
putShort((USHORT) fractionComponent);
}
#endif