私は現在、組み込みの修士号を取得しようとしています。論文では、Robot のプログラミングにおける Erlang の有効性を研究する必要があります。私の知る限り、 Erlang の宣言型の性質と同時実行性は効果的である可能性があるため、C プログラムからセンサー値を取得し(Erlang はセンサーを直接読み取ることができないため)、計算を実行して制御信号を C プログラムに送り返す「アダプティブ クルーズ コントロール」用の Erlang コードを作成しました。しかし、コードのサイズ (行数) はかなり大きく見えます。宣言型の性質を使用できないのはなぜですか、または他の問題がありますか? これが私のコードスニペットです。
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).
このコードは C コードに似ています。
- 私の実装方法が間違っているのでしょうか? (特に IF...end) または問題自体が小さい (2 プロセスのみ)
ロボットのプログラミングにおける Erlang の有効性を示す方法を教えてください。すべての提案を歓迎します。
ありがとう..
この問題は Erlang の有効性を示すのに十分ではないという @cthulahoops に同意します。Erlang で実装できるロボット アプリケーションを提案できる人はいますか??