From 172f393696b967f23e1d166fc7eb952a11994c5d Mon Sep 17 00:00:00 2001 From: Matthew Barth Date: Mon, 14 Aug 2017 11:07:46 -0500 Subject: Add factor parameter to inc/dec action functions The factor parameter is used to correctly determine the increase or decrease speed delta based on sensor inputs that have a scale. To get the net speed delta change, the factor is applied to the difference in sensor value and the defined state at which a speed change should be requested. Change-Id: I2aaa8f6b294b38d1f38238593dd851c3a205eb7a Signed-off-by: Matthew Barth --- control/actions.hpp | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'control') diff --git a/control/actions.hpp b/control/actions.hpp index ebdff60..167149b 100644 --- a/control/actions.hpp +++ b/control/actions.hpp @@ -252,6 +252,7 @@ auto set_ceiling_from_average_sensor_value( * request that new target speed. * * @param[in] state - State to compare the group's property value to + * @param[in] factor - Factor to apply to the calculated net delta * @param[in] speedDelta - Speed delta of the group * * @return Lambda function @@ -259,17 +260,17 @@ auto set_ceiling_from_average_sensor_value( * a new target speed with that increase for the zone. */ template -auto set_net_increase_speed(T&& state, uint64_t speedDelta) +auto set_net_increase_speed(T&& state, T&& factor, uint64_t speedDelta) { return [speedDelta, + factor = std::forward(factor), state = std::forward(state)](auto& zone, auto& group) { - T singleDelta = 1; auto netDelta = zone.getIncSpeedDelta(); std::for_each( group.begin(), group.end(), - [&zone, &state, &speedDelta, &singleDelta, &netDelta]( + [&zone, &state, &factor, &speedDelta, &netDelta]( auto const& entry) { try @@ -282,12 +283,16 @@ auto set_net_increase_speed(T&& state, uint64_t speedDelta) // state types for comparison if (value >= state) { - // Increase by at least a single delta + // Increase by at least a single delta(factor) // to attempt bringing under 'state' - auto delta = std::max((value - state), singleDelta); - // Increase is the difference times - // the given speed delta - netDelta = std::max(netDelta, delta * speedDelta); + auto delta = std::max( + (value - state), + factor); + // Increase is the factor applied to the + // difference times the given speed delta + netDelta = std::max( + netDelta, + (delta/factor) * speedDelta); } } catch (const std::out_of_range& oore) @@ -308,6 +313,7 @@ auto set_net_increase_speed(T&& state, uint64_t speedDelta) * request that speed change occur on the next decrease interval. * * @param[in] state - State to compare the group's property value to + * @param[in] factor - Factor to apply to the calculated net delta * @param[in] speedDelta - Speed delta of the group * * @return Lambda function @@ -315,16 +321,17 @@ auto set_net_increase_speed(T&& state, uint64_t speedDelta) * a new target speed with that decrease for the zone. */ template -auto set_net_decrease_speed(T&& state, uint64_t speedDelta) +auto set_net_decrease_speed(T&& state, T&& factor, uint64_t speedDelta) { return [speedDelta, + factor = std::forward(factor), state = std::forward(state)](auto& zone, auto& group) { auto netDelta = zone.getDecSpeedDelta(); std::for_each( group.begin(), group.end(), - [&zone, &state, &speedDelta, &netDelta](auto const& entry) + [&zone, &state, &factor, &speedDelta, &netDelta](auto const& entry) { try { @@ -338,14 +345,15 @@ auto set_net_decrease_speed(T&& state, uint64_t speedDelta) { if (netDelta == 0) { - netDelta = (state - value) * speedDelta; + netDelta = ((state - value)/factor) * speedDelta; } else { - // Decrease is the difference times - // the given speed delta - netDelta = std::min(netDelta, - (state - value) * speedDelta); + // Decrease is the factor applied to the + // difference times the given speed delta + netDelta = std::min( + netDelta, + ((state - value)/factor) * speedDelta); } } } -- cgit v1.2.1