How to show the effectiveness of Erlang for programming robots?

I am currently working on Masters in Embedded, and for my dissertation I must study the effectiveness of Erlang for Robot programming. AFAIK, the declarative nature of Erlang and concurrency can be effective , so I made Erlang code for "Adaptive cruise control", which takes sensor values ​​from program C (since Erlang cannot directly read sensors), then do the calculation and send the control signal to program C. But the code looks quite large in size (lines). Why can't I use a declarative character or is there some other problem? Here are my code snippets.

 start() -> 
    spawn( cr, read_sensor, []),
    spawn(cr, take_decision, []),
    sleep_infinite().
% this will make it to run infinitely 
sleep_infinite() -> 
    receive
        after infinity ->
            true
    end.

read_sensor() -> 
    register(read, self()),
    Port = open_port({spawn , "./cr_cpgm" }, [{packet, 2}]),
    Port ! {self(),{command, [49]}},% for executing read sensor fun in C pgm
    read_reply(Port).

read_reply(Port) -> 
    receive 
        read_sensor -> 
            Port ! { self(), { command, [49]}};

        {Port, {data, Data}} -> 
            [Left,Center,Right,Distance] = Data, % stored values of sensors into variables for further computation
            io:format("value of Left: ~w and Center: ~w and Right: ~w and Distance: ~w~n",[Left,Center,Right,Distance]),

        if         Distance =< 100 -> decision ! {1, out}; % Distance shows the value returned by front sharp sensor
                ((Left > 25) and (Center > 25) and (Right > 25)) -> decision ! {2, out}; % stop robot
                        Center < 25 -> decision ! {3, out}; % move forward
                   ((Left > 25) and (Center > 25)) -> decision ! {4, out}; % turn right
                 ((Right > 25) and (Center > 25)) -> decision ! {5, out}; % turn left
                          true ->   decision ! {6, out}   % no match stop robot  
        end
    end,
    read_reply(Port).

take_decision() ->
    register(decision, self()),
    Port = open_port({spawn , "./cr_cpgm" }, [{packet, 2}]),
    decision_reply(Port).

decision_reply(Port) ->
    receive
        {A, out} ->
            Port ! {self(), {command, [50,A]}};

        {Port,{data, Data}} ->
        if
            Data == [102] ->  read ! read_sensor %
        end
    end,
    decision_reply(Port).

This code is more like code C.

  • ( IF... end) ( 2 ).

, , Erlang . .

..

@cthulahoops, , Erlang. - - , Erlang??

+3
4

, -, , Erlang.

, , - if if , :

choice(Distance, _Left, _Center, _Right) when Distance =< 100 -> something_you_didnt_say_what;
choice(_Distance, Left, Center, Right) when Left > 25, Center > 25, Right > 25 -> stop;
choice(_Distance, Left, _Center, _Right) when Center < 25 -> forward;
choice(_Distance, Left, Center, _Right) when Center > 25, Left > 25 -> right;
choice(_Distance, _Left, Center, Right) when Center > 25, Right > 25 -> left.

, , , .. , , , . ( , , .)

+2

: , - , , erlang.

, , , . erlang , . OTP , . .

erlang.

+2

(, ..), , , . , erlang.

, , OTP - gen_fsm ( ). , ( , ?): → . , , .

+1

, . rpc:abcast UDP, . , , ..

, , / Erlang .

+1

Source: https://habr.com/ru/post/1760865/


All Articles