Skip to content

Commit

Permalink
Refactors alt/az moves, exposes a TelescopeApi module
Browse files Browse the repository at this point in the history
  • Loading branch information
documents-design committed Dec 27, 2022
1 parent 1b17efa commit 27e7b41
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 79 deletions.
157 changes: 78 additions & 79 deletions lib/scope/telescope.ex
Expand Up @@ -12,17 +12,12 @@ defmodule Scope.Telescope do

@alt_time 15
@alt_divisions 900
@az_divisions 3600
@az_time 60
@alt_increment :math.pi() / 2 / @alt_divisions
@time_interval trunc(@alt_time / @alt_divisions * 1000)

def show_constants() do
%{
alt_time: @alt_time,
alt_divisions: @alt_divisions,
alt_increment: @alt_increment,
time_interval: @time_interval
}
end
@az_increment :math.pi() * 2 / @az_divisions
@alt_time_interval trunc(@alt_time / @alt_divisions * 1000)
@az_time_interval trunc(@az_time / @az_divisions * 1000)

use GenServer
alias Scope.Telescope
Expand All @@ -42,99 +37,101 @@ defmodule Scope.Telescope do
}}
end

def home(pid) do
GenServer.call(pid, :home, 10000)
end

def show(pid) do
GenServer.cast(pid, :show)
end

def start_move_up(pid) do
GenServer.cast(pid, :start_move_up)
end
def handle_call(:home, _, state), do: do_home(state)
def handle_info(:continue_move, %Telescope{moving: :no} = state), do: {:noreply, state}
def handle_info(:continue_move, %Telescope{moving: _dir} = state), do: continue_move(state)

def stop_move_up(pid) do
GenServer.cast(pid, :stop_move_up)
end
def handle_cast(:show, state), do: {:noreply, IO.inspect(state)}

def homed?(%Telescope{position_alt: -1}), do: false
def homed?(_), do: true
def handle_cast(:start_move_down, state), do: start_move(:down, state)
def handle_cast(:start_move_left, state), do: start_move(:left, state)
def handle_cast(:start_move_up, state), do: start_move(:up, state)
def handle_cast(:start_move_right, state), do: start_move(:right, state)

def handle_info(:continue_move, %Telescope{moving: :no} = state) do
{:noreply, state}
def handle_cast(:move_up, state), do: do_move(:up, state)
def handle_cast(:move_down, state), do: do_move(:down, state)
def handle_cast(:move_left, state), do: do_move(:right, state)
def handle_cast(:move_right, state), do: do_move(:left, state)

def handle_cast(:stop_move, %Telescope{} = state), do: stop_move(state)

def valid_move_preconditions?(_, %{position_alt: -1}), do: false
def valid_move_preconditions?(:down, %{lower_alt_stop: true}), do: false
def valid_move_preconditions?(:up, %{upper_alt_stop: true}), do: false
def valid_move_preconditions?(_, _), do: true

def dir_to_msg(dir) do
case dir do
:up -> :move_up
:down -> :move_down
:right -> :move_right
:left -> :move_left
_ -> nil
end
end

def handle_info(:continue_move, %Telescope{moving: dir} = state) do
msg =
case dir do
:up -> :move_up
:down -> :move_down
:right -> :move_right
:left -> :move_left
_ -> nil
end
def start_move(dir, state) do
msg = dir_to_msg(dir)

if !is_nil(msg) do
if valid_move_preconditions?(dir, state) and !is_nil(msg) do
GenServer.cast(self(), msg)
{:noreply, %{state | moving: dir}}
else
{:noreply, state}
end
end

{:noreply, state}
def do_move(_dir, %Telescope{moving: :no} = state), do: {:noreply, state}
def do_move(dir, %Telescope{} = state) do
case dir do
:left -> do_move_az(-1 * @az_increment, state)
:right -> do_move_az(@az_increment, state)
:up -> do_move_alt(@alt_increment, state)
:down -> do_move_alt(-1 * @alt_increment, state)
_ -> {:noreply, state}
end
end

def handle_cast(:show, state) do
IO.inspect(state)
{:noreply, state}
def do_move_az(inc, %Telescope{} = state) do
pos = state.position_az + inc
turns = trunc(pos / :math.pi())
normalized = pos - turns * :math.pi()
home_az = normalized == 0
Process.send_after(self(), :continue_move, @az_time_interval)
{:noreply, %{state | home_az: home_az, position_az: normalized}}
end

def handle_cast(:start_move_up, %Telescope{} = state) do
if not state.upper_alt_stop do
GenServer.cast(self(), :move_up)
def do_move_alt(inc, %Telescope{} = state) do
pos = state.position_alt + inc
mmax = :math.pi() / 2
lower_stop = pos <= 0
upper_stop = pos >= mmax
normalized = min(mmax, max(pos, 0))
if lower_stop or upper_stop do
GenServer.cast(self(), :stop_move)
else
Process.send_after(self(), :continue_move, @alt_time_interval)
end

{:noreply,
%Telescope{
state
| moving: :up
}}
{:noreply, %{state |
lower_alt_stop: lower_stop,
upper_alt_stop: upper_stop,
position_alt: normalized
}}
end

def handle_cast(:move_up, %Telescope{moving: :up} = state) do
pos = state.position_alt + @alt_increment
def continue_move(%Telescope{} = state) do
msg = dir_to_msg(state.moving)

{new_pos, new_stop_status} =
if pos >= :math.pi() / 2, do: {:math.pi() / 2, true}, else: {pos, false}

new_state = %Telescope{
state
| position_alt: new_pos,
upper_alt_stop: new_stop_status
}

if new_stop_status do
IO.inspect("Hit upper alt endstop.")
GenServer.cast(self(), :stop_move_up)
else
IO.inspect("Moving up to #{new_state.position_alt |> Float.floor(2)} rad")
Process.send_after(self(), :continue_move, @time_interval)
if !is_nil(msg) do
GenServer.cast(self(), msg)
end

{:noreply, new_state}
end

def handle_cast(:move_up, %Telescope{moving: :no} = state) do
{:noreply, state}
end

def handle_cast(:stop_move_up, %Telescope{} = state) do
{:noreply,
%Telescope{
state
| moving: :no
}}
end

def handle_call(:home, _, state) do
def do_home(state) do
new_state = %{
state
| lower_alt_stop: true,
Expand All @@ -146,4 +143,6 @@ defmodule Scope.Telescope do

{:reply, :ok, new_state}
end

def stop_move(state), do: {:noreply, %{state | moving: :no}}
end
14 changes: 14 additions & 0 deletions lib/scope/telescope_api.ex
@@ -0,0 +1,14 @@
defmodule Scope.TelescopeApi do
def create(name), do: GenServer.start_link(Scope.Telescope, name)

def home(pid), do: GenServer.call(pid, :home, 10000)

def show(pid), do: GenServer.cast(pid, :show)

def up(pid), do: GenServer.cast(pid, :start_move_up)
def down(pid), do: GenServer.cast(pid, :start_move_down)
def left(pid), do: GenServer.cast(pid, :start_move_left)
def right(pid), do: GenServer.cast(pid, :start_move_right)

def stop(pid), do: GenServer.cast(pid, :stop_move)
end

0 comments on commit 27e7b41

Please sign in to comment.