fix hardware params persistance, and tool offests in sequences

pull/228/head
connor rigby 2017-01-13 14:33:21 -08:00
parent 1dad38ce6b
commit c540d95ee2
8 changed files with 127 additions and 19 deletions

View File

@ -5,6 +5,7 @@ defmodule Farmbot.BotState.Hardware do
require Logger
alias Farmbot.StateTracker
@behaviour StateTracker
use StateTracker,
name: __MODULE__,
@ -27,6 +28,45 @@ defmodule Farmbot.BotState.Hardware do
@type pins :: map
@type end_stops :: {integer,integer,integer,integer,integer,integer}
# Callback that happens when this module comes up
def load([]) do
{:ok, p} = get_config("params")
initial_state = %State{mcu_params: p}
# BUG: THIS CAN'T BE HERE BECAUSE IT TRIES TO CALL THIS GENSERVER DERP
spawn fn() ->
case set_initial_params(initial_state) do
{:ok, :no_params} ->
Logger.debug ">> is reading default Hardware params."
Farmbot.CeleryScript.Command.read_all_params(%{}, [])
:ok ->
Logger.debug ">> Hardware Params set"
{:error, reason} ->
Logger.error(">> Error setting Hardware Params: #{inspect reason}")
end
end
{:ok, initial_state}
end
@doc """
Takes a Hardware State object, and makes it happen
"""
@spec set_initial_params(State.t) :: {:ok, :no_params} | :ok | {:error, term}
def set_initial_params(%State{} = state) do
if Enum.empty?(state.mcu_params) do
{:ok, :no_params}
else
# iterate over mcu_params and read each one
config_pairs = Enum.map(state.mcu_params, fn({param, val}) ->
%Farmbot.CeleryScript.Ast{kind: "pair",
args: %{label: param, value: val}, body: []}
end)
Farmbot.CeleryScript.Command.config_update(%{package: "arduino_firmware"},
config_pairs)
end
end
def handle_call({:get_pin, pin_number}, _from, %State{} = state) do
dispatch Map.get(state.pins, Integer.to_string(pin_number)), state
end
@ -80,6 +120,7 @@ defmodule Farmbot.BotState.Hardware do
def handle_cast({:set_param, {param_string, value}}, %State{} = state) do
new_params = Map.put(state.mcu_params, param_string, value)
put_config("params", new_params)
dispatch %State{state | mcu_params: new_params}
end

View File

@ -0,0 +1,54 @@
defmodule Farmbot.Camera do
@moduledoc """
Test module for taking photos with the rpi camera.
"""
@params ["-o", "/tmp/image.jpg", "-e", "jpg", "-t", "1",
"-w","1024", "-h", "1024"
]
@command "raspistill"
require Logger
def ls do
{f, _} = System.cmd("ls", ["-lah", "/tmp"])
IO.puts f
end
def capture, do: System.cmd(@command, @params)
def get_form_data_auth do
req = Farmbot.HTTP.get "/api/storage_auth"
req.body |> Poison.decode!
end
def blah do
req = Farmbot.HTTP.get("/api/storage_auth")
f = Poison.decode!(req.body)
url = "https:" <> f["url"]
form_data = f["form_data"]
binary = File.read!("/tmp/image.jpg")
payload =
Enum.map(form_data, fn({key, value}) ->
if key == "file", do: {"file", binary}, else: {key, value}
end)
headers = [
{"Content-Type", "multipart/form-data"},
{"User-Agent", "FarmbotOS"}
]
Logger.debug "URL: #{url}"
Logger.debug "Payload: #{inspect payload}"
Logger.debug "Headers: #{inspect headers}"
blah = HTTPoison.post(url, {:multipart, payload}, headers)
Logger.debug "THIS THING HERE: #{inspect blah}"
attachment_url = url<>form_data["key"]
Logger.debug "here: #{attachment_url}"
# json = Poison.encode!(
# %{"attachment_url" => attachment_url, "meta" => %{x: 1, y: 1, z: 1}}
# )
#
# Farmbot.HTTP.post "/api/images", json
end
end

View File

@ -89,20 +89,19 @@ defmodule Farmbot.CeleryScript.Command do
args: %{x: _x, y: _y, z: _z},
body: []} = already_done), do: already_done
def ast_to_coord(%Ast{kind: "tool", args: %{tool_id: id}, body: []}) do
blah =
Amnesia.transaction do
[asdf] =
ToolSlot.where(tool_id == id)
|> Amnesia.Selection.values
asdf
end
if blah do
coordinate(%{x: blah.x, y: blah.y, z: blah.z}, [])
else
Logger.error ">> could not find tool_slot with tool_id: #{id}"
:error
# NOTE(connor): don't change `tool_id_` back to `tool_id` what was happening
# Amnesia builds local variables by the name of "tool_id", so it was looking
# fortool_id == tool_id, which returned
# all of them, because every toolslots tool_id
# always equals that toolslots tool id lol
def ast_to_coord(%Ast{kind: "tool", args: %{tool_id: tool_id_}, body: []}) do
blah = Amnesia.transaction do
ToolSlot.where(tool_id == tool_id_) |> Amnesia.Selection.values
end
case blah do
[ts] -> coordinate(%{x: ts.x, y: ts.y, z: ts.z}, [])
_ -> Logger.error ">> could not find tool_slot with tool_id: #{tool_id_}"
:error
end
end
@ -301,6 +300,7 @@ defmodule Farmbot.CeleryScript.Command do
32,33,41,42,43,51,52,53,
61,62,63,71,72,73,101,102,103]
for param <- magic_numbers do
Process.sleep(10) # Makes this a bit more stable
GHan.block_send("F21 P#{param}")
end
end

View File

@ -100,10 +100,14 @@ defmodule Farmbot.Serial.Gcode.Handler do
Sends a message and blocks until it completes, or times out.
The default timeout is ten seconds.
"""
@spec block_send(binary, integer) :: atom
@spec block_send(binary, integer) :: {:error, :no_serial} | atom
def block_send(str, timeout \\ 10_000) do
GenServer.cast(Farmbot.Serial.Gcode.Handler,{:send, str, self()})
__MODULE__.block(timeout)
if Farmbot.Serial.Handler.available? do
GenServer.cast(Farmbot.Serial.Gcode.Handler,{:send, str, self()})
__MODULE__.block(timeout) # is there a reason why i did this?
else
{:error, :no_serial}
end
end
@doc """

View File

@ -43,6 +43,12 @@ defmodule Farmbot.Serial.Handler do
GenServer.call(__MODULE__, :resume)
end
def available?, do: GenServer.call(__MODULE__, :available?)
def handle_call(:available?, _, nil), do: {:reply, false, nil}
def handle_call(:available?, _, {nerves, tty, handler}),
do: {:reply, true, {nerves, tty, handler}}
def handle_call(:e_stop, _from, {nerves, tty, handler}) do
UART.write(nerves, "E")
UART.close(nerves)

View File

@ -34,6 +34,7 @@ defmodule Farmbot.Transport.GenMqtt.Client do
end
def handle_cast(%Ser{} = ser, %Token{} = token) do
IO.puts "!!! sending state"
json = Poison.encode!(ser)
GenMQTT.publish(self(), status_topic(token), json, 1, false)
{:ok, token}
@ -46,6 +47,7 @@ defmodule Farmbot.Transport.GenMqtt.Client do
end
def handle_cast({:emit, msg}, %Token{} = token) do
# IO.puts "[!!! sending message]"
json = Poison.encode! msg
GenMQTT.publish(self(), frontend_topic(token), json, 1, false)
{:noreply, token}

View File

@ -11,7 +11,7 @@
"axios": "^0.15.3",
"bootstrap": "^3.3.7",
"css-loader": "^0.26.1",
"farmbot": "2.5.0-rc12",
"farmbot": "2.6.0",
"file-loader": "^0.9.0",
"font-awesome": "^4.7.0",
"lodash": "^4.17.2",

View File

@ -21,7 +21,8 @@
"gen_mqtt": {:hex, :gen_mqtt, "0.3.1", "6ce6af7c2bcb125d5b4125c67c5ab1f29bcec2638236509bcc6abf510a6661ed", [:mix], [{:vmq_commons, "1.0.0", [hex: :vmq_commons, optional: false]}]},
"gen_stage": {:hex, :gen_stage, "0.10.0", "ce1eb93a3f9708f2e215f70b3d3c7f55dea4a4ed7e9615195e28d543c9086656", [:mix], []},
"gettext": {:hex, :gettext, "0.13.0", "daafbddc5cda12738bb93b01d84105fe75b916a302f1c50ab9fb066b95ec9db4", [:mix], []},
"hackney": {:hex, :hackney, "1.6.3", "d489d7ca2d4323e307bedc4bfe684323a7bf773ecfd77938f3ee8074e488e140", [:mix, :rebar3], [{:certifi, "0.7.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
"hackney": {:hex, :hackney, "1.6.5", "8c025ee397ac94a184b0743c73b33b96465e85f90a02e210e86df6cbafaa5065", [:rebar3], [{:certifi, "0.7.0", [hex: :certifi, optional: false]}, {:idna, "1.2.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
"httpoison": {:hex, :httpoison, "0.10.0", "4727b3a5e57e9a4ff168a3c2883e20f1208103a41bccc4754f15a9366f49b676", [:mix], [{:hackney, "~> 1.6.3", [hex: :hackney, optional: false]}]},
"httpotion": {:hex, :httpotion, "3.0.2", "525b9bfeb592c914a61a8ee31fdde3871e1861dfe805f8ee5f711f9f11a93483", [:mix], [{:ibrowse, "~> 4.2", [hex: :ibrowse, optional: false]}]},
"ibrowse": {:hex, :ibrowse, "4.2.2", "b32b5bafcc77b7277eff030ed32e1acc3f610c64e9f6aea19822abcadf681b4b", [:rebar3], []},
"idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [:rebar3], []},