Fix moveing to a point.

This commit is contained in:
connor rigby 2017-11-22 08:43:27 -08:00
parent 13c8cd675f
commit 1662ad94ee
4 changed files with 31 additions and 16 deletions

View file

@ -35,7 +35,7 @@ defmodule Farmbot.BotState.Transport.AMQP do
{:ok, _tag} <- Basic.consume(chan, queue_name),
state <- struct(State, [conn: conn, chan: chan, queue_name: queue_name, bot: device])
do
Logger.success(3, "Connected to real time services.")
# Logger.success(3, "Connected to real time services.")
{:consumer, state, subscribe_to: [Farmbot.BotState, Farmbot.Logger]}
else
{:error, {:auth_failure, msg}} = fail ->

View file

@ -55,7 +55,7 @@ defmodule Farmbot.BotState.Transport.GenMQTT.Client do
def on_connect(state) do
GenMQTT.subscribe(self(), [{bot_topic(state.device), 0}])
GenMQTT.subscribe(self(), [{sync_topic(state.device), 0}])
Logger.success(3, "Connected to real time services.")
# Logger.success(3, "Connected to real time services.")
if state.cache do
GenMQTT.publish(self(), status_topic(state.device), Poison.encode!(state.cache), 0, false)

View file

@ -2,23 +2,36 @@ defmodule Farmbot.CeleryScript.Utils do
@moduledoc false
alias Farmbot.Firmware.Vec3
alias Farmbot.CeleryScript.AST
alias AST.Node.{Tool, Coordinate, Point}
alias Farmbot.Repo.Point, as: DBPoint
import Ecto.Query
def ast_to_vec3(%AST{kind: AST.Node.Tool} = ast) do
def ast_to_vec3(%AST{kind: Tool} = ast) do
tool_id = ast.args.tool_id
case Farmbot.Repo.A.one(from p in Farmbot.Repo.Point, where: p.tool_id == ^tool_id) do
case Farmbot.Repo.current_repo().one(from p in DBPoint, where: p.tool_id == ^tool_id) do
%{x: x, y: y, z: z} ->
{:ok, new_vec3(x, y, z)}
nil -> {:error, "Could not find tool by id: #{tool_id}"}
end
end
def ast_to_vec3(%AST{kind: AST.Node.Coordinate, args: %{x: x, y: y, z: z}}) do
def ast_to_vec3(%AST{kind: Coordinate, args: %{x: x, y: y, z: z}}) do
{:ok, new_vec3(x, y, z)}
end
def ast_to_vec3(%AST{kind: Point} = ast) do
point_id = ast.args.pointer_id
case Farmbot.Repo.current_repo().one(from p in DBPoint, where: p.id == ^point_id) do
%{x: x, y: y, z: z} ->
{:ok, new_vec3(x, y, z)}
nil -> {:error, "Could not find point by id: #{point_id}"}
end
end
def ast_to_vec3(%Vec3{} = vec3), do: {:ok, vec3}
def ast_to_vec3(%AST{kind: kind}) do
{:error, "can not convert: #{kind} to a coordinate."}
end

View file

@ -214,6 +214,18 @@ defmodule Farmbot.Firmware do
{nil, state}
end
defp handle_gcode(:error, state) do
Logger.warn 1, "Got error gcode!"
maybe_cancel_timer(state.timer)
if state.current do
Logger.error 1, "Failed to execute #{state.current.fun}#{inspect state.current.args}"
GenStage.reply(state.current.from, {:error, :firmware_error})
{nil, %{state | current: nil}}
else
{nil, state}
end
end
defp handle_gcode({:report_current_position, x, y, z}, state) do
{:location_data, %{position: %{x: round(x), y: round(y), z: round(z)}}, state}
end
@ -358,6 +370,7 @@ defmodule Farmbot.Firmware do
end
defp handle_gcode({:report_axis_calibration, param, val}, state) do
# have to spawn this fun otherwise we have to functions waiting on eachother.
spawn fn() ->
case Farmbot.Firmware.update_param(param, val) do
:ok -> Logger.success 1, "Calibrated #{param}: #{val}"
@ -367,17 +380,6 @@ defmodule Farmbot.Firmware do
{nil, state}
end
defp handle_gcode(:error, state) do
maybe_cancel_timer(state.timer)
if state.current do
Logger.error 1, "Failed to execute #{state.current.fun}#{inspect state.current.args}"
GenStage.reply(state.current.from, {:error, :firmware_error})
{nil, %{state | current: nil}}
else
{nil, state}
end
end
defp handle_gcode(:noop, state) do
{nil, state}
end