-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.erl
32 lines (28 loc) · 1.13 KB
/
sensor.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
-module(sensor).
-compile(export_all).
-include("records.hrl").
gen(ExoSelf_PId,Node) ->
spawn(Node,?MODULE,loop,[ExoSelf_PId]).
loop(ExoSelf_PId) ->
receive
{ExoSelf_PId,{Id,Cx_PId,SensorName,VL,Fanout_PIds}} ->
loop(Id,Cx_PId,SensorName,VL,Fanout_PIds)
end.
%When gen/2 is executed it spawns the sensor element and immediately begins to wait for its initial state message.
loop(Id,Cx_PId,SensorName,VL,Fanout_PIds) ->
receive
{Cx_PId,sync} ->
SensoryVector = sensor:SensorName(VL),
[PId ! {self(),forward,SensoryVector} || PId <- Fanout_PIds],
loop(Id,Cx_PId,SensorName,VL,Fanout_PIds);
{Cx_PId,terminate} ->
ok
end.
%The sensor process accepts only 2 types of messages, both from the cortex. The sensor can either be triggered to begin gathering sensory data based on its sensory role, or terminate if the cortext requests so.
rng(VL) ->
rng(VL,[]).
rng(0,Acc) ->
Acc;
rng(VL,Acc) ->
rng(VL-1,[random:uniform()|Acc]).
%'rng' is a simple random number generator that produces a vector of random values, each between 0 and 1. The length of the vector is defined by the VL, which itself is specified within the sensor record.