# VeX - Keeping arm at an angle

So my team made a Vex robot for the toss-up competition, but we need the arm up during the autonomous. The problem is that it's too heavy to stay up on its own. I was going to use encoders to count what angle the arm is at. I was going to use this code, but I'm not sure if there's a better way.

 while(MotorEncoder[rightMotor] < 1000)
{
motor[rightMotor] = 80;
motor[leftMotor] = 80;
}


Would anyone recommend a better solution or is this the best way? This is untested by the way.

Some clarifications before i answer. when you say "its too heavy to stay up on its own" do you mean that the arm is too heavy for the motor, or the arm doesn't stay at an angle when the motor is not powered? also, why are you controlling two motors, and reading only one encoder? perhaps you should provide a better description of your arm.

that being said, i would use simple proportional control for this. (proportional control being the "P" in what is more commonly called PID control). what you have above is called "bang" control.

basically, you just set up a linear relationship between encoder readings and motor current. (actually the difference between encoder readings of where the arm is and where you want it to be). For example:

 void set_arm_to_angle(int angle)
{
int P = 200;
int error = angle - MotorEncoder[rightMotor];
motor[rightMotor] = P * error
}


you should be able to find lots of info on proportional control on the web. but basically, if the arm is lower than where you want, positive current is applied to raise the arm. the farther away from your desired angle, the more current. if the arm is too high, current is reversed.

it can be a little tricky figuring out the right gain (P). too low, and the arm won't move, too high and the arm will oscillate. i wouldn't worry about the I and D terms. but adding some friction to the arm might help.

• My arm doesn't stay up when the motors are powered off. So I can raise it perfectly fine, but if I let go of a button, the arm falls straight down. And currently I only have one encoder because I figured the second wasn't needed because they're going to be proportional. The way the arm is set up is there are two towers, each with a motor powering a gear that powers a gear attached to the arm. The arm is one solid frame. And thank you very much for that. I like the idea and I'll have to try it out. I wish I could upvote it. – Michael Blake Feb 20 '14 at 4:10

Can't comment but expanding on Ben's answer. To get the general magnitude of P you consider the range of your inputs and outputs. For vex motor control you have a control parameter range of -127 to +127. Your angle input if coming straight from the encoder value is 360 per revolution with the Cortex. One recommendation: for an arm with a fixed range, you are better off using a potentiometer which gives you an absolute rather than relative range position. Just make sure you mount it in a way that the arm doesn't move outside of the pots range. Motion range is about 250 degrees.

Back to the question of P. You want P such that the input to the motor is in its range. Figure out the maximum angle error. For example if you want the arm at position 80 and it starts out at 0, the initially the error is 80. Since you want the motor input to be as large at 127, P=127/80 . For the pot, output range is about 4000 and your maximum error might be on the order of 2000, so initially try P=127/2000.

A final bit of advice, since you are using a simple proportional controller (only P, not I and D terms), you will have some droop. That is, you'll never be exactly at 80 since this means no error and hence no drive. So the arm will settle in at some point below that - hence droop. The simple solution used in this case is to have your set point a bit above the desired point so the set point - droop = desired point. You can adjust this after you get P established.

A final bit of advice, have your competition switch hooked up and have someone standing by to move the switch to off. If you get values really wrong, your arm could go crazy and hurt some one, or even worse, break something on the robot. I've seen shafts (and done it myself) that are twisted like taffy.

• Thank you very much for that. I wish I could +1 it. I actually had that issue that you described where the arm would go crazy once in a while and bend everything. I'll keep your tips in mind. Thank you. – Michael Blake Feb 28 '14 at 0:15

proportional controller may yield steady state error. it's better to use PI control. that your arm will fall when the motors are not powered is due to the fact that no special backdrive protection is included in the motor or the mechansim. you may want some actual brake to stop the backdrive