Skip to content

Commit

Permalink
linear position
Browse files Browse the repository at this point in the history
  • Loading branch information
ci42 committed Feb 16, 2012
1 parent 1432527 commit 1f9a4b5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
56 changes: 49 additions & 7 deletions src/saw_position.erl
Expand Up @@ -19,7 +19,7 @@
%%% Created :
%%% -------------------------------------------------------------------
-module(saw_position).

-define(DELAY, 2 * 1000 div 256).
-behaviour(gen_server).
%% --------------------------------------------------------------------
%% Include files
Expand All @@ -31,17 +31,18 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([start_link/0]).
-export([start/0]).
-export([nulldurchlauf/1]).
-export([nulldurchlauf/2]).

%% ====================================================================
%% External functions
%% ====================================================================
nulldurchlauf(Durchlaufzeit) ->
ok.
nulldurchlauf(T_abs, Durchlaufzeit) ->
gen_server:cast(?MODULE, {nulldurchlauf, T_abs, Durchlaufzeit}).

%% --------------------------------------------------------------------
%% record definitions
%% --------------------------------------------------------------------
-record(state, {socket}).
-record(state, {t_abs, durchlaufzeit}).
%% ====================================================================
%% Server functions
%% ====================================================================
Expand All @@ -50,7 +51,7 @@ nulldurchlauf(Durchlaufzeit) ->
%% Description: Starts the server
%%--------------------------------------------------------------------
start_link() ->
gen_server:start_link({global, ?MODULE}, ?MODULE, [], []).
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

start() ->
start_link().
Expand Down Expand Up @@ -84,6 +85,11 @@ handle_call(Msg, _From, State) ->
%% {noreply, State, Timeout} |
%% {stop, Reason, State} (terminate/2 is called)
%% --------------------------------------------------------------------
handle_cast({nulldurchlauf, T_abs, Durchlaufzeit}, State) ->
TimerRef = erlang:send_after(?DELAY, ?MODULE, {col, 64, up}),
%% error_logger:info_msg("start: col 64 up", []),
{noreply, #state{t_abs=T_abs, durchlaufzeit=Durchlaufzeit}};

handle_cast(Msg, State) ->
{noreply, State}.

Expand All @@ -94,6 +100,27 @@ handle_cast(Msg, State) ->
%% {noreply, State, Timeout} |
%% {stop, Reason, State} (terminate/2 is called)
%% --------------------------------------------------------------------

handle_info({col, 127, up}, State) ->
erlang:send_after(delay(State#state.durchlaufzeit), ?MODULE, {col, 127, down}),
error_logger:info_msg("col 127 up", []),
{noreply, State};

handle_info({col, No, up}, State) ->
erlang:send_after(delay(State#state.durchlaufzeit), ?MODULE, {col, No+1, up}),
error_logger:info_msg("col ~p up", [No]),
{noreply, State};

handle_info({col, 0, down}, State) ->
erlang:send_after(delay(State#state.durchlaufzeit), ?MODULE, {col, 0, up}),
error_logger:info_msg("col 0 down", []),
{noreply, State};

handle_info({col, No, down}, State) ->
erlang:send_after(delay(State#state.durchlaufzeit), ?MODULE, {col, No-1, down}),
error_logger:info_msg("col ~p down", [No]),
{noreply, State};

handle_info(Msg, State) ->
{noreply, State}.

Expand All @@ -118,6 +145,21 @@ code_change(OldVsn, State, Extra) ->
%% --------------------------------------------------------------------
send_message() ->
ok.

col(No, Direction) ->
gen_server:cast(?MODULE, {col, No, Direction}).

delay(Durchlaufzeit) ->
Durchlaufzeit div 256 div 1000.

delay(Index, Direction, Durchlaufzeit, T_abs) ->
T_diff = timer:now_diff(erlang:now(), T_abs),
T_ist = T_diff rem Durchlaufzeit,
T_soll = sollDelay(),
(T_soll - T_ist) div 1000.

sollDelay() ->
ok.
%% --------------------------------------------------------------------
%%% Test functions
%% --------------------------------------------------------------------
Expand All @@ -126,4 +168,4 @@ send_message() ->
%% --------------------------------------------------------------------
-include_lib("eunit/include/eunit.hrl").
-ifdef(TEST).
-endif.
-endif.
3 changes: 2 additions & 1 deletion src/saw_taktgeber.erl
Expand Up @@ -98,11 +98,12 @@ handle_info(timeout, State) ->

handle_info({udp, _Socket, IPtuple, InPortNo, [0]}, State) ->
%% error_logger:info_msg("~n~nFrom IP: ~p~nPort: ~p~nData: ~p~n", [IPtuple, InPortNo, Packet]),
%% TODO: Zeitkorrektur (Konstante)
T_now = erlang:now(),
case State of
{MegaSec, Sec, MircoSec} ->
T_delta = timer:now_diff(T_now, State),
saw_position:nulldurchlauf(T_delta),
saw_position:nulldurchlauf(T_now, T_delta),
error_logger:info_msg("Durchlaufzeit: ~p ms~n", [T_delta div 1000]);
_ ->
error_logger:info_msg("waiting for second run", [])
Expand Down

0 comments on commit 1f9a4b5

Please sign in to comment.