I’m trying to create a program that will find the distance to a wall and, using trig, find the angle it needs to adjust it’s arm in order to shoot a rubber band at the designated height. I have run into a problem where I get a really large number that will not change no matter what operation is performed on it. Any suggestions would help. Thanks.
Your image file is only about 17 kb. It appears on my screen so small, I would need a microscope to read it. Any other way you can post your code? I’m not familiar with EasyC (if that’s what it is) but other people on the forum are.
If nothing else, try to provide a screen shot with a bigger image, more resolution.
Since I cannot see your original code, and therefore not tell you what to change, I will instead tell you what my approach to this would be so you can see how ours differ.
We need to determine a few variables before we can write out our code. First, we need to determine initial velocity and force of the rubber band.
Assuming Hooke’s Law applies to rubber bands, we can use the following equation to find values we will need later.
F = k*x
Where F is equal to the force required to change the length of the rubber band by x units. We actually need to calculate k in this instance, so use a force sensor or some other means of calculating the force to find k.
We use k to determine the initial velocity of the rubber band. Trust me, this is important in the final equation that we will use in this code. Use the following equation:
v = (sqrt (k/m))x
X again being the distance it is stretched.
Now we can determine the final equation for this function (or, at least start to):
y=inital height + xtan(angle) - (gravity/(2v^2*cos^2(angle)))x^2
That equation took me a while to find. This is the function to determine the path of a launched item, in this case a rubber band. There are a few problems with this, so we cannot use this in our code right away, as their are too many variables, so we have to cut down the variables, and make them constants. First, and the most obvious, is the initial height.
Since you told us you mean to use the angle in your calculation, I assume that you have means of finding the angle. If not, find a way to, as you will need it for this equation.
To find the initial height, first you need to find the height of the shooter at the point of 0 degrees. Record that, as you will need it later.
Next, we will need to use a bit of trigonometry, and we need to find the length of the arm on your robot. We can use the sin of the angle to determine the height of the arm.
sin(angle) = Height/Length of Arm
and therefore
sin(angle)*Length of Arm = Height
Therefore Initial Height can be found by adding the two value we just found, the minimum height of the rubber band shooter, and the actual height from that point.
Since gravity is a constant 9.8N/kg on earth, we can conclude g = 9.8.
In the following code, I decided not to make the distance away from the wall a constant, as making it constant could possibly not make the rubber band be able to hit the designated height.
Also, since the exact value would be hard to get exactly, I say we should use a threshold to determine between what range is acceptable, and this will be a value implemented into the code.
Since the equation in this code is very long, I will refer to it as equation within the code.
Equation -
(minimum height of arm + (sin(angle)Length of Arm) + xtan(angle) - (9.8/(2v^2cos^2(angle)))x^2
Sorry, by the way. This code will essentially be in RobotC, but it shouldn’t be too hard to port over.
Also, when I write //, it is just a comment for the reader.
bool Boolean;
void rubberBandShotFunction (float IndicatedHeight, float threshold, float increment)
{
while (Boolean)
{
if (Angle <= 90)
{
if (Equation - threshold >=Indicated Height)
{
if (Equation + threshold <= IndicatedHeight)
{
RobotDriveForward(x); //Function that tells it to stop when it hits a certain value from a sensor.
RobotRaiseArm(angle); //Function that tells the robot to raise its arm until it hits a certain value from a sensor.
ShootRubberBand(); //Function that actually shoots the rubber band.
Boolean = false; //exits the while loop
}
}
}
angle = angle + increment; //to make the arm go up
else if (Angle > 90)
{
angle = 0;
x = x - increment; // minus because you will be getting closer to the wall
}
}
}
Use the ultrasonic sensor to run the code for moving the robot to stop it.
I would suggest using a potentiometer, but make sure you can convert that to degrees.
This code runs through each possibility for angles, then alters the X value, and checks the angles for that value, and so on. Once it finds values that work, it drives to that location, puts the robot in that position, and then fires the rubber band.
If my math in any of this is incorrect, please correct me. I hope this helps.
Sorry for the late reply. Thank you so much, it helped a lot!