How to show effectiveness of Erlang for programming Robots?

I am currently pursuing Masters in Embedded and for my thesis I have to study the effectiveness of Erlang for programming Robot. AFAIK Erlang's declarative nature and concurrency can be effective, so I made an Erlang code for "Adaptive cruise control" which takes sensor values from C program(because Erlang can not read sensors directly) then perform computation and send back control signal to C program. But the code looks quite big in size(lines). Why am I not able to use declarative nature or there is some other problem? Here is my code snippets.

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

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_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  

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

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

        {Port,{data, Data}} ->
            Data == [102] ->  read ! read_sensor %

This code looks more like a C code.

  • Is my way of implementation wrong?(especially IF...end) or problem itself is small(only 2 processes)

Please suggest me how to show the effectiveness of Erlang in programming robots. All suggestions are welcome.


Well I am agree with @cthulahoops that this problem is not enough to show the effectiveness of Erlang. Can anybody suggest some Robotic application which I can implement in Erlang??


  • Well, firstly I'd say that this doesn't sound like a very good project for showing the effectiveness of Erlang.

    The first thing that comes to mind to make the code more declarative is to split the if out into a separate function like this:

    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.

    Which separates the declaration of how to respond to sensors from the messy business of looping and sending messages, etc. Also, returning atoms rather than the cryptic integers avoids having to put that information into comments. (Following the philosophy of comments tell you where you need to clarify the code.)