Commit db128019 authored by Jeroen Vreeken's avatar Jeroen Vreeken
Browse files

Add test for oneshot block.

Optimize oneshot block and bridge_pwm block:
	-minimize amount of branches by removing sequence points.
	-Use local variables for structure fields to minimize dereferences.
parent 682bbbb4
......@@ -64,24 +64,16 @@ static void calculate(struct controller_block *bridge)
float value;
float in = *priv->in;
int divider = priv->divider;
float remainder = priv->remainder;
if (in > 0.0) {
a = true;
b = false;
} else if (in < 0.0) {
a = false;
b = true;
} else {
a = false;
b = false;
}
a = in > 0.0;
b = in < 0.0;
cnt = cnt % divider;
cnt++;
priv->cnt = cnt;
value = (fminf(fabsf(in), 1.0) * divider) + priv->remainder;
// printf("+ %d %f %f %f\n", cnt, in, value, priv->remainder);
value = (fminf(fabsf(in), 1.0) * divider) + remainder;
if (cnt > value) {
float diff;
......@@ -90,15 +82,10 @@ static void calculate(struct controller_block *bridge)
diff = (float)cnt - value;
if (priv->pwm_prev) {
priv->remainder = 1.0 - diff;
remainder = 1.0 - diff;
}
// printf("- %d %f %f %f\n", cnt, value, (float)cnt, priv->remainder);
} else {
if (!priv->continuous && cnt == divider-1) {
pwm = false;
} else {
pwm = true;
}
pwm = priv->continuous || cnt != divider-1;
}
priv->pwm_prev = pwm;
......
......@@ -72,35 +72,35 @@ struct controller_block_private {
#define OUTPUT(name) (priv->name)
#define INPUT(name) (*priv->name)
#define DEBUG
#undef DEBUG
static void calculate(struct controller_block *block)
{
struct controller_block_private *priv;
bool last_out;
priv = block->private;
last_out = OUTPUT(out);
bool out;
bool set = INPUT(set);
bool reset = INPUT(reset);
uint32_t period = priv->period;
uint32_t ticks_left = priv->ticks_left;
if (INPUT(reset) == true) {
OUTPUT(out) = false;
return;
}
if (INPUT(set) == true) {
OUTPUT(out) = true;
priv->ticks_left = priv->period;
} else {
if ((priv->ticks_left) && (--priv->ticks_left == 0)) {
OUTPUT(out) = false;
}
}
/* set counter to period if set true */
ticks_left += (period - ticks_left) * set;
/* reset counter if reset true */
ticks_left -= ticks_left * reset;
/* if counter not zero output is true */
out = ticks_left;
/* decrement, but not below zero */
ticks_left -= ticks_left != 0;
#ifdef DEBUG
if (OUTPUT(out) != last_out)
if (OUTPUT(out) != out)
log_send(LOG_T_DEBUG, "%s: turned %s", block->name,
OUTPUT(out) ? "on" : "off");
out ? "on" : "off");
#endif
priv->ticks_left = ticks_left;
OUTPUT(out) = out;
}
static struct controller_block_interm_list interms[] = {
......
trigger {
{ "immediate" }
}
blocks (100.0, 0.0) {
{ "oneshot", "oneshot" }
{ "test_input_bool", "set" }
{ "test_input_bool", "reset" }
{ "test_output_bool", "out" }
}
links {
{ "set", "value", "oneshot", "set", true }
{ "reset", "value", "oneshot", "reset", true }
{ "oneshot", "out", "out", "value", true }
}
params {
{ "oneshot", "period", 0.04 }
{ "oneshot", "force", true }
{ "set", "value", 20, (int) {
false, false, false, false, false,
true, false, false, false, false,
true, false, false, true, true,
true, false, false, false, false
}
}
{ "reset", "value", 20, (int) {
false, true, false, false, false,
false, false, false, false, false,
false, false, true, true, false,
false, false, false, false, false
}
}
{ "out", "value", 20, (int) {
true, false, false, false, false,
true, true, true, true, false,
true, true, false, false, true,
true, true, true, true, false
}
}
}
......@@ -93,6 +93,7 @@ CTRL_TESTS += \
$(DIR)/block_limit_var.test.ctrl \
$(DIR)/block_matrix_2x2.test.ctrl \
$(DIR)/block_not.test.ctrl \
$(DIR)/block_oneshot.test.ctrl \
$(DIR)/block_pid_aw.test.ctrl \
$(DIR)/block_rangecheck.test.ctrl \
$(DIR)/block_setpoint_generator_1d.test.ctrl \
......
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