Commit 11a4e112 authored by Daan Vreeken's avatar Daan Vreeken
Browse files

Add 'schmitt_trigger' block.

Its output will rise when (positive > negative + window/2) and fall when
(positive < negative - window/2).

	new file:   block/block_schmitt_trigger.c
	modified:   block/build.mk
parent b5e57dcc
/*
* Copyright (c) 2014,
* Daan Vreeken <Daan @ Vitsch . nl> - Vitsch Electronics
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <controller/controller_block.h>
#include <log/log.h>
/*
inputs outputs
-----------------------------
| |
---| positive out |----
---| negative |
| |
-----------------------------
The block has a parameter 'window'.
The output will be set to 'true' when (positive > negative + window/2)
and it will be set to 'false' when (positive < negative - window/2)
*/
struct controller_block_private {
// inputs
float *positive, *negative;
// outputs
bool out;
// params
float window;
};
#define OUTPUT(name) (priv->name)
#define INPUT(name) (*priv->name)
static void calculate(struct controller_block *block)
{
struct controller_block_private *priv;
priv = block->private;
if (INPUT(positive) > INPUT(negative) + priv->window / 2)
OUTPUT(out) = true;
if (INPUT(positive) < INPUT(negative) - priv->window / 2)
OUTPUT(out) = false;
}
static struct controller_block_interm_list interms[] = {
{ "positive", CONTROLLER_BLOCK_TERM_FLOAT,
offsetof(struct controller_block_private, positive) },
{ "negative", CONTROLLER_BLOCK_TERM_FLOAT,
offsetof(struct controller_block_private, negative) },
{ NULL },
};
static struct controller_block_outterm_list outterms[] = {
{ "out", CONTROLLER_BLOCK_TERM_BOOL,
offsetof(struct controller_block_private, out) },
{ NULL },
};
static int param_set_window(struct controller_block *block, char *param,
int argc, va_list val)
{
float window;
window = va_arg(val, double);
if (window < 0.0) {
log_send(LOG_T_ERROR, "%s: 'window' must be >= 0.0",
block->name);
return -1;
}
block->private->window = window;
return 0;
}
static struct controller_block_param_list params[] = {
{ "window", false, param_set_window, .args = { "double", NULL } },
{ NULL },
};
static struct controller_block * block_schmitt_trigger_create(char *name,
int argc, va_list val)
{
struct controller_block *block;
struct controller_block_private *priv;
block = controller_block_alloc("schmitt_trigger", name,
sizeof(struct controller_block_private));
if (block == NULL)
return NULL;
priv = block->private;
OUTPUT(out) = false;
if (controller_block_interm_list_init(block, interms))
goto err_block;
if (controller_block_outterm_list_init(block, outterms))
goto err_block;
if (controller_block_param_list_add(block, params))
goto err_block;
block->calculate = calculate;
controller_block_add(block);
return block;
err_block:
controller_block_free(block);
return NULL;
}
BLOCK_CREATE(schmitt_trigger) = {
.create = block_schmitt_trigger_create,
.args = { NULL },
};
...@@ -41,7 +41,8 @@ BLOCKS := \ ...@@ -41,7 +41,8 @@ BLOCKS := \
value \ value \
value_bool \ value_bool \
value_uint32 \ value_uint32 \
comparator comparator \
schmitt_trigger
BLOCKS_IL := \ BLOCKS_IL := \
and2 \ and2 \
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment