View unanswered posts | View active topics It is currently Thu Apr 25, 2019 1:57 am

 Page 1 of 1 [ 2 posts ]
 Print view Previous topic | Next topic
Equation evaluates to zero when it shouldn't
Author Message
Rookie

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

Joined: Wed Aug 20, 2014 7:19 pm
Posts: 4
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
Display posts from previous:  Sort by
 Page 1 of 1 [ 2 posts ]

#### Who is online

Users browsing this forum: No registered users and 2 guests

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning