-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsensor.erl
More file actions
309 lines (288 loc) · 9.6 KB
/
sensor.erl
File metadata and controls
309 lines (288 loc) · 9.6 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
%% This source code and work is provided and developed by DXNN Research Group WWW.DXNNResearch.COM
%%
%Copyright (C) 2012 by Gene Sher, DXNN Research Group, CorticalComputer@gmail.com
%All rights reserved.
%
%This code is licensed under the version 3 of the GNU General Public License. Please see the LICENSE file that accompanies this project for the terms of use.
-module(sensor).
-compile(export_all).
-include("records.hrl").
gen(ExoSelf_PId,Node)->
spawn(Node,?MODULE,prep,[ExoSelf_PId]).
prep(ExoSelf_PId) ->
receive
{ExoSelf_PId,{Id,Cx_PId,Scape,SensorName,VL,Parameters,Fanout_PIds}} ->
loop(Id,ExoSelf_PId,Cx_PId,Scape,SensorName,VL,Parameters,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,ExoSelf_PId,Cx_PId,Scape,SensorName,VL,Parameters,Fanout_PIds)->
receive
{Cx_PId,sync}->
SensoryVector = sensor:SensorName(ExoSelf_PId,VL,Parameters,Scape),
[Pid ! {self(),forward,SensoryVector} || Pid <- Fanout_PIds],
loop(Id,ExoSelf_PId,Cx_PId,Scape,SensorName,VL,Parameters,Fanout_PIds);
{ExoSelf_PId,terminate} ->
%io:format("Sensor:~p is terminating.~n",[Id]),
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 cortex requests so.
distance_scanner(Exoself_PId,VL,[Spread,Density,RadialOffset],Scape)->
case gen_server:call(Scape,{get_all,avatars,Exoself_PId}) of
destroyed->
lists:duplicate(VL,-1);
Avatars ->
Self = lists:keyfind(Exoself_PId,2,Avatars),
Loc = Self#avatar.loc,
Direction = Self#avatar.direction,
distance_scanner(silent,{1,0,0},Density,Spread,Loc,Direction,lists:keydelete(self(), 2, Avatars))
end.
color_scanner(Exoself_PId,VL,[Spread,Density,RadialOffset],Scape)->
%io:format("Scape:~p~n",[Scape]),
case gen_server:call(Scape,{get_all,avatars,Exoself_PId}) of
destroyed->
lists:duplicate(VL,-1);
Avatars ->%io:format("Avatars:~p~n",[Avatars]),
Self = lists:keyfind(Exoself_PId,2,Avatars),
%io:format("Self:~p~n",[Self]),
Loc = Self#avatar.loc,
Direction = Self#avatar.direction,
color_scanner(silent,{1,0,0},Density,Spread,Loc,Direction,lists:keydelete(self(), 2, Avatars))
end.
%Input: ViewAngle= Radian, Density= n, Gaze direction= {SensorLoc,Direction}.
%Output: List of ranges 1/Distance no intersection = -1, with angle starting with Gaze + (ViewAngle/2), and ending with (Gaze - ViewAngle/2), [Dist1...DistDensity].
distance_scanner(Op,{Zoom,PanX,PanY},Density,Spread,Loc,Direction,Avatars)->
case is_even(Density) of
true ->
Resolution = Spread/Density,
SAngle = (Density/2)*Resolution,
StartAngle = -SAngle+Resolution/2;
false ->
Resolution = Spread/Density,
SAngle=trunc(Density/2)*Resolution,
StartAngle = -SAngle
end,
UnitRays = create_UnitRays(Direction,Density,Resolution,StartAngle,[]),
RangeScanList = compose_RangeScanList(Loc,UnitRays,Avatars,[]),
%io:format("RangeScanList:~p~n",[RangeScanList]),
case {Op,get(canvas)} of
{silent,_} ->
done;
{draw,undefined} ->
Canvas = gen_server:call(get(scape),get_canvas),
put(canvas,Canvas);
{draw,Canvas}->
{X,Y} = Loc,
FLoc = {X*Zoom+PanX,Y*Zoom+PanY},
ScanListP=lists:zip(UnitRays,RangeScanList),
Ids = [gs:create(line,Canvas,[{coords,[FLoc,{(X+Xr*Scale)*Zoom+PanX,(Y+Yr*Scale)*Zoom+PanY}]}])||{{Xr,Yr},Scale}<-ScanListP, Scale =/= -1],
timer:sleep(2),
[gs:destroy(Id) || Id<- Ids]
end,
RangeScanList.
compose_RangeScanList(Loc,[Ray|UnitRays],Avatars,Acc)->
{Distance,_Color}=shortest_intrLine({Loc,Ray},Avatars,{inf,void}),
compose_RangeScanList(Loc,UnitRays,Avatars,[Distance|Acc]);
compose_RangeScanList(_Loc,[],_Avatars,Acc)->
lists:reverse(Acc).
color_scanner(Op,{Zoom,PanX,PanY},Density,Spread,Loc,Direction,Avatars)->
case is_even(Density) of
true ->
Resolution = Spread/Density,
SAngle = (Density/2)*Resolution,
StartAngle = -SAngle+Resolution/2;
false ->
Resolution = Spread/Density,
SAngle=trunc(Density/2)*Resolution,
StartAngle = -SAngle
end,
UnitRays = create_UnitRays(Direction,Density,Resolution,StartAngle,[]),
ColorScanList = compose_ColorScanList(Loc,UnitRays,Avatars,[]),
%io:format("ColorScanList:~p~n",[ColorScanList]),
case {Op,get(canvas)} of
{silent,_} ->
done;
{draw,undefined} ->
Canvas = gen_server:call(get(scape),get_canvas),
put(canvas,Canvas);
{draw,Canvas}->
{X,Y} = Loc,
FLoc = {X*Zoom+PanX,Y*Zoom+PanY},
ScanListP=lists:zip(UnitRays,ColorScanList),
Ids = [gs:create(line,Canvas,[{coords,[FLoc,{(X+Xr*25)*Zoom+PanX,(Y+Yr*25)*Zoom+PanY}]},{fg,val2clr(Color)}])||{{Xr,Yr},Color}<-ScanListP],
timer:sleep(2),
[gs:destroy(Id) || Id<- Ids]
end,
ColorScanList.
compose_ColorScanList(Loc,[Ray|UnitRays],Avatars,Acc)->
{_Distance,Color}=shortest_intrLine({Loc,Ray},Avatars,{inf,void}),
compose_ColorScanList(Loc,UnitRays,Avatars,[Color|Acc]);
compose_ColorScanList(_Loc,[],_Avatars,Acc)->
lists:reverse(Acc).
energy_scaner(Op,{Zoom,PanX,PanY},Density,Spread,Loc,Direction,Avatars)->
case is_even(Density) of
true ->
Resolution = Spread/Density,
SAngle = (Density/2)*Resolution,
StartAngle = -SAngle+Resolution/2;
false ->
Resolution = Spread/Density,
SAngle=trunc(Density/2)*Resolution,
StartAngle = -SAngle
end,
UnitRays = create_UnitRays(Direction,Density,Resolution,StartAngle,[]),
EnergyScanList = compose_EnergyScanList(Loc,UnitRays,Avatars,[]),
%io:format("RangeScanList:~p~n",[RangeScanList]),
case Op of
silent ->
done;
draw ->
io:format("EnergyScanList:~p~n",[EnergyScanList])
end,
EnergyScanList.
compose_EnergyScanList(Loc,[Ray|UnitRays],Avatars,Acc)->
{_Distance,_Color,Energy}=shortest_intrLine2({Loc,Ray},Avatars,{inf,void},0),
%io:format("compose_EnergyScanList:~p~n",[Energy]),
compose_EnergyScanList(Loc,UnitRays,Avatars,[Energy/100|Acc]);
compose_EnergyScanList(_Loc,[],_Avatars,Acc)->
lists:reverse(Acc).
shortest_intrLine2(Gaze,[Avatar|Avatars],Val,Energy)->
{D,_} = Val,
{U_D,U_C} = intr(Gaze,Avatar#avatar.objects,Val),
U_Energy = case D == U_D of
true ->
Energy;
false ->
Avatar#avatar.energy
end,
shortest_intrLine2(Gaze,Avatars,{U_D,U_C},U_Energy);
shortest_intrLine2(_Gaze,[],{Distance,Color},Energy)->
case Distance of
inf ->%TODO, perhaps absence of color should be -1, not 1.
{-1,1,Energy};
0.0 ->
{-1,1,Energy};
_ ->
{Distance,clr2val(Color),Energy}
end.
create_UnitRays(_,0,_,_,Acc)->
Acc;
create_UnitRays({X,Y},Density,Resolution,Angle,Acc)->
%io:format("Angle:~p~n",[Angle*180/math:pi()]),
UnitRay = {X*math:cos(Angle) - Y*math:sin(Angle), X*math:sin(Angle) + Y*math:cos(Angle)},
create_UnitRays({X,Y},Density-1,Resolution,Angle+Resolution,[UnitRay|Acc]).
shortest_intrLine(Gaze,[Avatar|Avatars],Val)->
shortest_intrLine(Gaze,Avatars,intr(Gaze,Avatar#avatar.objects,Val));
shortest_intrLine(_Gaze,[],{Distance,Color})->
case Distance of
inf ->%TODO, perhaps absence of color should be -1, not 1.
{-1,1};
0.0 ->
{-1,1};
_ ->
{Distance,clr2val(Color)}
end.
intr(Gaze,[{circle,_Id,Color,_Pivot,C,R}|Objects],{Min,MinColor})->
{S,D} = Gaze,
[{Xc,Yc}] = C,
{Xs,Ys} = S,
{Xd,Yd} = D,
{Xv,Yv} = {Xs-Xc,Ys-Yc},
VdotD = Xv*Xd + Yv*Yd,
Dis = math:pow(VdotD,2) - (Xv*Xv + Yv*Yv - R*R),
%io:format("S:~p D:~p C:~p V:~p R:~p VdotD:~p Dis:~p~n",[S,D,C,{Xv,Yv},R,VdotD,Dis]),
Result=case Dis > 0 of
false ->
inf;
true ->
SqrtDis = math:sqrt(Dis),
I1 = -VdotD - SqrtDis,
I2 = -VdotD + SqrtDis,
case (I1 > 0) and (I2 >0) of
true ->
erlang:min(I1,I2);
false ->
inf
end
end,
{UMin,UMinColor}=case Result < Min of
true ->
{Result,Color};
false ->
{Min,MinColor}
end,
intr(Gaze,Objects,{UMin,UMinColor});
intr(Gaze,[{line,_Id,Color,_Pivot,[{X3,Y3},{X4,Y4}],_Parameter}|Objects],{Min,MinColor})->
{S,D} = Gaze,
{X1,Y1} = S,
{XD0,YD0} = D,
PerpXD1 = Y4-Y3,
PerpYD1 = -(X4-X3),
PerpXD0 = YD0,
PerpYD0 = -XD0,
Result=case PerpXD1*XD0 + PerpYD1*YD0 of
0.0 ->
inf;
Denom ->
RayLength = ((PerpXD1*(X3-X1)) + (PerpYD1*(Y3-Y1)))/Denom,
T = ((PerpXD0*(X3-X1)) + (PerpYD0*(Y3-Y1)))/Denom,
case (RayLength >= 0) and (T >= 0) and (T =< 1) of
true ->
RayLength;
false ->
inf
end
end,
{UMin,UMinColor}=case Result < Min of
true ->
{Result,Color};
false ->
{Min,MinColor}
end,
intr(Gaze,Objects,{UMin,UMinColor});
intr(_Gaze,[],{Min,MinColor})->
{Min,MinColor}.
shortest_distance(OperatorAvatar,Avatars)->
Loc = OperatorAvatar#avatar.loc,
shortest_distance(Loc,Avatars,inf).
shortest_distance({X,Y},[Avatar|Avatars],SD)->
{LX,LY} = Avatar#avatar.loc,
Distance = math:sqrt(math:pow(X-LX,2)+math:pow(Y-LY,2)),
shortest_distance({X,Y},Avatars,erlang:min(SD,Distance));
shortest_distance({_X,_Y},[],SD)->
case SD of
inf ->
-1;
_ ->
SD
end.
clr2val(Color)->
case Color of
black -> -1; %poison
cyan -> -0.75;
green -> -0.5; %plant
yellow -> -0.25;
blue -> 0; %prey
gret -> 0.25;
red -> 0.5; %predator
brown -> 0.75; % wall
_ -> 1%io:format("transducers:clr2val(Color): Color = ~p~n",[Color]), 1 %emptiness
end.
val2clr(Val)->
case Val of
-1 -> black;
-0.75 -> cyan;
-0.5 -> green;
-0.25 -> yellow;
0 -> blue;
0.25 -> grey;
0.5 -> red;
0.75 -> brown;
_ -> white
end.
is_even(Val)->
case (Val rem 2) of
0 ->
true;
_ ->
false
end.