# Vex gear rack

Has anyone used this yet?
And if so, Can you tell me how far a Servo will extend the rack?
I know it is really used for the Servo Motors, But I need to controll the movement alittle better than the motor will do.
Calvin

That will depend on the gearing between the servo and the rack. The rack has 19 teeth and is 2.5" long. A Vex servo is spec’d to rotate at least 120 degrees, or one third of a complete turn.

If you put a 12t gear on the servo, that means it can rotate through about 4 teeth. This translates to 4/19 of the 2.5" rack, or just over half an inch.

If you put a 36t gear on the servo, that means it can rotate through about 12 teeth. This translates to 12/19 of the 2.5" rack, or just over an inch and a half.

As the gear you use gets larger, the speed and travel go up, but the force goes down.

Cheers,

• Dean

Just use a motor and a rotation counter. Works just as well and you don’t need to worry about servos.

1 1/2 inch is what I was looking for in travel, But the speed I need to keep it moderate. I wonder if I can control the speed by code?
Calvin

Certainly you can, though there isn’t a speed setting for servos, just position. So to make this work the way you want, you’ll have to keep changing the servo position, moving it in small increments and waiting a bit in between each step.

If you don’t need an exact speed, then you can just move the servo one unit every ‘n’ times through your main program loop. You’ll have to play with ‘n’ to get a speed you are happy with.

A traditional way to code this is to have three constants:
[LIST]
*]LowLimit (the lowest position number for the servo, 0…255)
*]HighLimit (the highest position number for, 0…255)
*]Step (the number of main loops to go through per servo step. This is ‘n’ from above, and can be omitted if it is equal to 1)
[/LIST]
You’ll need a few variables too:
[LIST]
*]Position (the current servo position, 0…255)
*]Cycle (to keep track of which main loop you are on. You can omit this is Step==1)
*]Direction (-1 for going down, 0 for stopped, +1 for going up. Set this to trigger movement)
[/LIST]
Here is some pseudo-code:

``````if (Direction) {     // Direction is not zero, so we are on the move
if (Cycle++ >= Step) {     // (you can omit if Step==1)
Cycle = 0;     // (you can omit if Step==1)
Position += Direction;
if (Position >= HighLimit) {     // Limit upward travel
Position = HighLimit;
Direction = Cycle = 0;
}
if (Position <= LowLimit) {     // Limit downward travel
Position = LowLimit;
Direction = Cycle = 0;
}
pwm08 = Position;     // substitute whichever pwm channel you want
}     // (you can omit if Step==1)
}
``````

To use this code, just declare the variables and constants as described above and insert this code in your main program loop. When you want the servo to move, just set Direction to -1 or +1 (depending on the direction you want). You can check to see if the servo is in motion by checking the value of Direction. If it is +1 or -1, then it is in motion, and 0 if stopped.

Avoid setting Position directly, since the servo will (try to) quickly jump to that position next time you need to move, which may be bad for your mechanism.

If you need an exact speed, then you’ll have to work a timer in. Having not yet used any timers for Vex programming, I can’t say for sure how you’d do that. I suspect you’d rip out the Cycle and Step logic in my code above and replace it with a check against a free-running timer to see when it was time to step.

Cheers,

• Dean

Yes, you can. In the Motor Module command, the 3 preset speeds are CW (255), Stop (127), and CCW (0). But you can also put a number in “User Value” in the range of 0 to 255. The closer the number is to 127/128, the slower it goes. So 140 goes slowly CW, 150 is a bit faster, etc. until you hit 255. In the CCW direction, 115 goes slowly CCW, 105 is a bit faster, etc. until you hit 0. The actual speed also depends on the load placed on the motor and the battery charge, so using motor speed and timing is a rather unreliable way to get a preset distance. If you have an optical shaft encoder to clock the number of rotations (or fraction thereof), this is more reliable.

The easiest way to play with motor speeds is to use the on-line window from the Build and Download menu. There you can use the sliders to test various speeds on a motor plugged into any of the 8 ports.

The O.P. was asking about controlling the speed of a servo, not a motor. Controlling servo speed of a requires a lot more code.

As you mentioned, using a motor would be much easier in software, but it would require limit switches, a shaft encoder, or a potentiometer to sense the travel limits.

Cheers,

• Dean