View unanswered posts | View active topics It is currently Sat Apr 19, 2014 3:38 pm






Reply to topic  [ 4 posts ] 
Proportioal Motor Speed 
Author Message
Rookie

Joined: Fri Jan 04, 2013 8:27 pm
Posts: 2
Post Proportioal Motor Speed
We currently have an arm, powered by a motor (port 1). It is attached to a potentiometer, in slot in1.

I am trying to use this code:
Code:
 
if(SensorValue[in1] > 1000) //At rest the arm is at 0, I want it to raise to the height 1000
{

if((1000 - SensorValue[in1])>=150) //If the arm's distance from the target position is more than 150
{
motor[port1] = -127; //Full speed ahead!
}
if((1000- SensorValue[in1])<150) //However, if it is less than 150 close to the target height,
{
motor[port1] = (1000-SensorValue[in1])*(127/150); //My logic here is that, when 1000-SensorValue[in1] = 150, the motor runs at 127. When 1000-SensorValue[in1] = 75, the motor runs at 63.5. It decreases until it finally reaches 0, and in that case, the motor also reaches 0.
}
}


Here is where the problem arises. Apparently the motor value needs to be a constant, and I have no idea how to even make that work.

Basically, what my code does is when the arm tries to reach target height 1000. Starting when it is 150 ticks close, it slows down until it has a force of 0 when it reaches the target position.

There are a few rubber bands pulling it up so we don't have to worry about the motors having 1 force and not being able to move the arm up, and we don't care (right now) if the arm goes over the height, but the main priority is just to figure out how to get this working.

Thanks,
1622


Fri Jan 04, 2013 8:39 pm
Profile
Rookie

Joined: Fri Jan 04, 2013 8:27 pm
Posts: 2
Post Re: Proportioal Motor Speed
Apparently the fix was to define another variable as 1000-sensorvalue[in1], and have it directly multiply and divide:

changedsensorvalue*127/150 works just fine.

Any ideas why?


Fri Jan 04, 2013 8:57 pm
Profile
Novice
User avatar

Joined: Sat Jul 10, 2010 3:06 pm
Posts: 86
Location: Roanoke, VA
Post Re: Proportioal Motor Speed
This is the same issue I ran into on this thread: viewtopic.php?f=11&t=5309

Let's say that SensorValue[in1] is 925.
Then, the robot evaluates the statement like so:
Code:
 motor[port1] = (1000-SensorValue[in1])*(127/150);

Code:
 motor[port1] = (1000-925)*(0.8467);

Code:
 motor[port1] = (75)*(0.8467);

Now, to perform the multiplication, ROBOTC casts the decimal to an integer. This basically means that ROBOTC truncates the decimal values. Now, we have:
Code:
 motor[port1] = (75)*(0);

Code:
 motor[port1] = 0;


There is a minor bug in the compiler that doesn't like this (see linked thread).

A fix to your line of code would be (note lack of parenthesis):
Code:
 motor[port1] = (1000-SensorValue[in1])*127/150;

_________________
Check out my website! www.RoboDesigners.com

VRC Team 2190

Twitter: @RoboDesigners


Sat Jan 05, 2013 3:34 pm
Profile WWW
Moderator
Moderator
User avatar

Joined: Tue Sep 14, 2010 9:19 pm
Posts: 496
Post Re: Proportioal Motor Speed
to evaluate the division as a float, you can also do:
Code:
127.0 / 150

Or cast one of the numbers to a float:
Code:
((float)127) / 150

_________________
sudo rm -rf /


Sat Jan 05, 2013 4:18 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 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.