Skip to content

Commit

Permalink
Possible fix for #990
Browse files Browse the repository at this point in the history
  • Loading branch information
RickCarlino committed Feb 8, 2020
1 parent 5ef053a commit c4f1cc3
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions farmbot_os/lib/farmbot_os/sys_calls/movement.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ defmodule FarmbotOS.SysCalls.Movement do
end

def move_absolute(x, y, z, speed) do
do_move_absolute(x, y, z, speed, max_retries())
do_move_absolute(x, y, z, speed)
end

defp do_move_absolute(x, y, z, speed, retries, errors \\ [])
defp do_move_absolute(x, y, z, speed, errors \\ [])

# This is the final attempt before movement is aborted.
defp do_move_absolute(x, y, z, speed, 0, errors) do
Expand Down Expand Up @@ -101,7 +101,7 @@ defmodule FarmbotOS.SysCalls.Movement do
end
end

defp do_move_absolute(x, y, z, speed, retries, errors) do
defp do_move_absolute(x, y, z, speed, errors) do
with {:ok, speed_x} <- param_read(:movement_max_spd_x),
{:ok, speed_y} <- param_read(:movement_max_spd_y),
{:ok, speed_z} <- param_read(:movement_max_spd_z),
Expand All @@ -120,15 +120,18 @@ defmodule FarmbotOS.SysCalls.Movement do
{:error, "emergency_lock"}

{:error, reason} ->
FarmbotCore.Logger.error(
1,
"Movement failed. Retrying up to #{retries} more time(s)"
)
handle_movement_error(reason)

do_move_absolute(x, y, z, speed, retries - 1, [reason | errors])
reason ->
handle_movement_error(reason)
end
end

def handle_movement_error(reason) do
msg = "Movement failed. #{inspect(reason)}"
FarmbotCore.Logger.error(1, msg)
end

def calibrate(axis) do
axis = assert_axis!(axis)

Expand Down Expand Up @@ -173,21 +176,13 @@ defmodule FarmbotOS.SysCalls.Movement do
end
end

defp max_retries do
case param_read(:param_mov_nr_retry) do
{:ok, nr} -> floor(nr)
_ -> 3
end
end

defp assert_axis!(axis) when is_atom(axis),
do: axis

defp assert_axis!(axis) when axis in ~w(x y z),
do: String.to_existing_atom(axis)

defp assert_axis!(axis) do
# {:error, "unknown axis #{axis}"}
raise("unknown axis #{axis}")
end
end

0 comments on commit c4f1cc3

Please sign in to comment.