View unanswered posts | View active topics It is currently Fri Dec 15, 2017 1:44 pm






Reply to topic  [ 2 posts ] 
Equation evaluates to zero when it shouldn't 
Author Message
Rookie

Joined: Wed Aug 20, 2014 7:19 pm
Posts: 4
Post 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.


Wed May 24, 2017 4:45 pm
Profile
Rookie

Joined: Wed Aug 20, 2014 7:19 pm
Posts: 4
Post 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.


Wed May 24, 2017 9:44 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.