Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make firmware (not FBOS) respnsible for retries #1138

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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