Log in

View Full Version : Encoders and rpm


Fry42guy
13-02-2015, 00:06
How do i set the rpms of a motor using an encoder. the encoder is connected to the roborio d i/o, i am using can for the jaguars and programming in java.

GeeTwo
13-02-2015, 00:32
Wow. What an open question.
The first thing you need to decide is "what is it that you're trying to achieve?" If you're considering an encoder, you should have some goal in mind that isn't easily met by just letting a practiced driver have a proportional joystick.

Fry42guy
13-02-2015, 00:41
i have two motors that i want to sync in auto. mode, i have little knowledge of programming encoders. my thoughts, if i can program one wheel to spin at 30 rpm i could just do the same for the other wheel... basically i want to know how to program motor1.setspeed(30rpm); if there is such a thing.

GeeTwo
13-02-2015, 08:11
Look at extending PIDSubsystem. There's a lot of info on PIDs at this thread (http://www.chiefdelphi.com/forums/showthread.php?t=134341). You could also search CD for "PID" and "Drive" together and check threads. I have never used an encoder on a drive system, but they certainly can be effective.

search CD for PID Drive (http://www.chiefdelphi.com/forums/search.php?searchid=6154861)
One thread found in that search that may be helpful (http://www.chiefdelphi.com/forums/showthread.php?t=134359&highlight=PID+Drive)

Ether
13-02-2015, 10:36
i have two motors that i want to sync in auto. mode ... program one wheel to spin at 30 rpm ... do the same for the other wheel

Have you considered an alternative approach: program the two motors to make the same number of turns, while remaining synchronized?

Rakusan2
13-02-2015, 10:42
If you only want to keep the motors in sync and would not mind not knowing the rpm then you can try something like what I have bellow.

PS: I have not yet tried this code so you might want to play with the initial motor values and by how much they are changed

Encoder leftEncoder = new Encoder(0,1), rightEncoder = new Encoder(2,3);
Timer time = new Timer();
Talon motor1 = new Talon(0), motor2 = new Talon(1);
public void autonomousInit(){
time.start();
leftEncoder.reset();
rightEncoder.reset();
}
public void autonomousPeriodic() {
if(time.get() < 5){
double left=0.8 , right=0.8;
int dif = leftEncoder.get()-rightEncoder.get();
if (dif>0){
left-=0.1;
right+=0.1;
}
else if(dif<0){
left+=0.1;
right-=0.1;
}
motor1.set(left);
motor2.set(right);
}
}

Ozuru
13-02-2015, 10:46
I have a DriveForDistance method that uses encoders. I'm assuming you're using command based. Think of what you want to do:

1. Clear the current count of the encoders

I recommend you create a method in your driveTrain called clearEncoders():
public void clearEncoder() {
_leftEncoder.reset();
_rightEncoder.reset();
}

While you're in your drive train subsystem, go ahead and create getter methods for your encoders.

public double getRightDistance() {
return _rightEncoder.getDistance();
}


In your initialize, you want to clear the encoders.

In your execute, you want to tell the motors to go the appropriate direction that the robot should be moving.

In your isFinished:


// code from Joe 2729
double left = Robot._driveTrain.getLeftDistance();
double right = Robot._driveTrain.getRightDistance();
double distance = Math.abs(left) > Math.abs(right) ? left : right;
if(_forw) {
return distance >= _distance;
} else {
return distance <= _distance;
}


That's how the general layout of your program should be if you want to drive for a distance.

Ether
13-02-2015, 12:37
Let's say you want to go from position P=0 to P=Pf in autonomous

Ideally, you'd want your control algorithm to do something like this:

1) ramp the speed up vs time (to limit acceleration to an acceptable level to avoid wheel slip or excessive rocking of the vehicle) until the desired top speed is reached, then hold that speed until you are within distance L of the desired stopping point (i.e. P=Pf-L).

2) then ramp the speed down as a function of L (to avoid excessive deceleration), so that speed=0 when L=0 (i.e P=Pf).

3) all the while keeping the encoder counts synchronized between the left and right motors