ROBOTC.net forums
http://www.robotc.net/forums/

Equation evaluates to zero when it shouldn't
http://www.robotc.net/forums/viewtopic.php?f=11&t=19565
Page 1 of 1

Author:  binarydude [ Wed May 24, 2017 4:45 pm ]
Post subject:  Equation evaluates to zero when it shouldn't

Hello,

I have written a function to move a motor to a particular position proportionally using a potentiometer.

Here is the code:
Code:
void goToPosition(int pos)
{ // handle moving deposition area to a particular position
   int potVal = SensorValue(pot);
   writeDebugStreamLine("pos is %d, potVal is %d", pos, potVal);
   int distanceToTarget;
   if (!distanceToTargetCalculated)
   {
      distanceToTarget = abs(pos - potVal);
      distanceToTargetCalculated = true;
   }
   else if ((potVal == pos) || ((potVal <= (pos + ERROR_TOLERATION)) && (potVal >= (pos - ERROR_TOLERATION))))
   {
      motor[motorSort] = 0;
      currentStatus = DEPOSITING;
   }
   else
   {
      int currentDistanceToTarget;
      int direction;
      currentDistanceToTarget = abs(pos - potVal);
      if (potVal < pos)
      {
         direction = 1;
      }
      else if (potVal > pos)
      {
         direction = -1;
      }
      float motorPower = 50 * (currentDistanceToTarget / distanceToTarget);
      writeDebugStreamLine("initial motorPower is %f", motorPower);
      if (motorPower < MINUMUM_MOTOR_POWER)
      {
         motorPower = MINUMUM_MOTOR_POWER * direction;
      }
      else
      {
         motorPower *= direction;
      }
      writeDebugStreamLine("motorPower is %f, distanceToTarget is %d, currentDistanceToTarget is %d, direction is %d", motorPower, distanceToTarget, currentDistanceToTarget, direction);
      motor[motorSort] = (int) motorPower;
   }
}


The current issue is that motorPower is being evaluated to zero and thus is being set to the minimum motor power. How can I get it so that it doesn't evaluate to zero?

Any help is appreciated.

Author:  binarydude [ Wed May 24, 2017 9:44 pm ]
Post subject:  Re: Equation evaluates to zero when it shouldn't

I have determined a solution to this issue.

Code:
motorPower = 50 * ((float) currentDistanceToTarget / (float) distanceToTarget);


If I cast currentDistanceToTarget and distanceToTarget to floats, the equation evaluates correctly.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/