-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35ea55b
commit e55a3de
Showing
16 changed files
with
471 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
defmodule Ev3.Communicating do | ||
use Behaviour | ||
|
||
@doc "Communicate info to other robots in a team" | ||
defcallback communicate(info :: any, team :: atom) :: :any | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
defmodule Ev3.LegoCommunication do | ||
@moduledoc "Lego inter-robot communication" | ||
|
||
require Logger | ||
alias Ev3.Device | ||
|
||
@doc "Get all available communicator" | ||
def communicators() do | ||
[:pg2] | ||
|> Enum.map(&(init_communicator("#{&1}", module_for(&1)))) | ||
end | ||
|
||
@doc"Find a communicator device by type" | ||
def communicator(type: type) do | ||
communicators() | ||
|> Enum.find(&(type(&1) == type)) | ||
end | ||
|
||
@doc "Get the type of the communicator device" | ||
def type(communicator) do | ||
communicator.type | ||
end | ||
|
||
@doc "Execute a cound command" | ||
def execute_command(communicator, command, params) do | ||
apply(Ev3.LegoCommunication, command, [communicator | params]) | ||
communicator | ||
end | ||
|
||
@doc "Communicate through a communicator" | ||
def communicate(communicator_device, %{info: info, team: team}) do | ||
apply(communicator_device.path, :communicate, [communicator_device, info, team]) | ||
end | ||
|
||
### Private | ||
|
||
defp init_communicator(type, module) do | ||
%Device{class: :comm, | ||
path: module, | ||
port: nil, | ||
type: type | ||
} | ||
end | ||
|
||
defp module_for(type) do | ||
case type do | ||
:pg2 -> Ev3.PG2Communicator | ||
other -> | ||
error = "Unknown type #{type} of communicator" | ||
Logger.error(error) | ||
raise error | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
defmodule Ev3.PG2Communicator do | ||
@moduledoc "Communicating with other robots via pg2" | ||
|
||
@behaviour Ev3.Communicating | ||
use GenServer | ||
alias Ev3.Percept | ||
alias Ev3.CNS | ||
require Logger | ||
|
||
@name __MODULE__ | ||
|
||
### API | ||
|
||
@doc "Star the communication server" | ||
def start_link() do | ||
Logger.info("Starting #{@name}") | ||
GenServer.start_link(@name, [], [name: @name]) | ||
end | ||
|
||
@doc "Communicate a percept to the team" | ||
def communicate(device, info, team) do | ||
GenServer.cast(@name, {:communicate, device, info, team}) | ||
end | ||
|
||
|
||
### CALLBACK | ||
|
||
def init([]) do | ||
group = Application.get_env(:ev3, :group) | ||
:pg2.start() | ||
:pg2.create(group) | ||
:pg2.join(group, self()) | ||
{:ok, %{group: group}} | ||
end | ||
|
||
def handle_cast({:communicate, device, info, team}, state = %{group: group}) do | ||
:pg2.get_members(group) | ||
|> Enum.each(&(GenServer.cast(&1, {:communication, Node.self(), info, team, device.props.ttl}))) | ||
Logger.info("COMMUNICATOR communicated #{inspect info} to team #{team}") | ||
{:noreply, state} | ||
end | ||
|
||
def handle_cast({:communication, source, info, team, ttl}, state) do # ttl for what's communicated | ||
Logger.info("COMMUNICATOR heard #{inspect info} for team #{team} from #{inspect source}") | ||
percept = Percept.new(about: :heard, value: %{source: source, team: team, info: info}) | ||
CNS.notify_perceived(%{percept | | ||
ttl: ttl, | ||
source: @name}) | ||
{:noreply, state} | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.