Skip to content

Commit

Permalink
Add bang-bang controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
crertel committed Oct 18, 2021
1 parent 639f624 commit cc7d1f9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
11 changes: 11 additions & 0 deletions lib/bangbang_controller.ex
@@ -0,0 +1,11 @@
defmodule ControlLoop.BangBangController do
require Record

Record.defrecord(:bb_controller, setpoint: 0, output_off: 0, output_on: 0)

def update( bb_controller(setpoint: setpoint, output_off: output_off, output_on: output_on) = controller, input) do
output = if input > setpoint, do: output_on, else: output_off

{output, controller}
end
end
13 changes: 0 additions & 13 deletions lib/control_loop.ex
Expand Up @@ -2,17 +2,4 @@ defmodule ControlLoop do
@moduledoc """
Documentation for `ControlLoop`.
"""

@doc """
Hello world.
## Examples
iex> ControlLoop.hello()
:world
"""
def hello do
:world
end
end
3 changes: 3 additions & 0 deletions lib/pid_controller.ex
@@ -0,0 +1,3 @@
defmodule ControlLoop.PIDController do

end
30 changes: 30 additions & 0 deletions test/bang_bang_controller_test.exs
@@ -0,0 +1,30 @@
defmodule BangBangControllerTest do
use ExUnit.Case
import ControlLoop.BangBangController, only: :macros
alias ControlLoop.BangBangController

describe "setup" do
test "basic creating and fetching" do
c = bb_controller(setpoint: 1, output_off: 2, output_on: 3)

assert bb_controller(c, :setpoint) == 1
assert bb_controller(c, :output_off) == 2
assert bb_controller(c, :output_on) == 3
end
end

describe "updating" do
setup do
%{c: bb_controller(setpoint: 1, output_off: 2, output_on: 3)}
end
test "update returns off value for below or at setpoint", %{c: controller} do
assert { 2, controller } == BangBangController.update(controller, 0)
assert { 2, controller } == BangBangController.update(controller, 1)
end

test "update returns on value for above setpoint", %{c: controller} do
assert { 3, controller } == BangBangController.update(controller, 2)
end
end

end
8 changes: 0 additions & 8 deletions test/control_loop_test.exs

This file was deleted.

0 comments on commit cc7d1f9

Please sign in to comment.