From 96b8a1aee6860567f44e5fde8827d1d44f2ab6e0 Mon Sep 17 00:00:00 2001 From: Douglas Clowes Date: Fri, 23 Mar 2007 18:43:43 +1100 Subject: [PATCH] Fixing blocked motor detection r1710 | dcl | 2007-03-23 18:43:43 +1100 (Fri, 23 Mar 2007) | 2 lines --- site_ansto/motor_dmc2280.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/site_ansto/motor_dmc2280.c b/site_ansto/motor_dmc2280.c index da0b8087..592462de 100644 --- a/site_ansto/motor_dmc2280.c +++ b/site_ansto/motor_dmc2280.c @@ -774,11 +774,16 @@ static int checkMotion(void *pData) { return 1; } /* calculate observed and expected steps per count ratios */ - ratio_obs = (steps - self->lastSteps) / (counts - self->lastCounts); + if (counts == self->lastCounts) /* prevent divide by zero */ + ratio_obs = (steps - self->lastSteps); + else + ratio_obs = (steps - self->lastSteps) / (counts - self->lastCounts); ratio_exp = (float) self->stepsPerX / (float) self->cntsPerX; ratio_cmp = ratio_obs / ratio_exp; - /* less than half or more than double is trouble */ - if (ratio_cmp > self->blockage_ratio || (1.0 / ratio_cmp) > self->blockage_ratio) { + /* wrong signs, less than half, or more than double is trouble */ + if (ratio_cmp < 0.0 || + ratio_cmp > self->blockage_ratio || + (1.0 / ratio_cmp) > self->blockage_ratio) { char msg[132]; snprintf(msg, sizeof(msg), "Motion check fail: obs=%f, exp=%f", ratio_obs, ratio_exp);