Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Encoders in Auto Mode (http://www.chiefdelphi.com/forums/showthread.php?t=91763)

tomy 13-02-2011 22:22

Encoders in Auto Mode
 
Anyone know how to use encoders in auto mode?

ChristopherBuck 14-02-2011 00:24

Re: Encoders in Auto Mode
 
Your question is pretty vague. Could you please provide more details as to what you are trying to accomplish, and what the problem currently is? Also, it would be helpful to know what language you are using to program the robot, so responses could be tailored to that language.

In theory, using encoders in autonomous would be just the same as using them in teleop. You first must open a reference to an encoder, indicating what DIO it is on and any other necessarry variables, and then use the supplied methods, functions, or nodes/SubVIs to start and count ticks to measure distance or rotations.

tomy 14-02-2011 00:34

Re: Encoders in Auto Mode
 
I'm using c++, all I need to know is what funcution do I call to make our robot go forward until the encoders turn x amount of times

mikets 14-02-2011 01:32

Re: Encoders in Auto Mode
 
Still, there are many variations of using encoders. For example, do you care uisng PID? How many encoders do you have? One for left wheel and one for right wheel? Do you have mecanum wheels? In the simplest, your code should look something like this:
Code:

In some periodic function or in a loop:
if ((leftEncoder->GetDistance() + rightEncoder->GetDistance()) /2 < targetDistance)
{
    ArcadeDrive(0.5, 0.0);
}
else
{
    ArcadeDrive(0.0, 0.0);
}


tomy 14-02-2011 10:01

Re: Encoders in Auto Mode
 
We will have 4 encoders and we are using mecanum wheels. Want to just post my code?

mikets 14-02-2011 12:32

Re: Encoders in Auto Mode
 
4 encoders on mecanum wheels are tricky to deal with for using in autonomous drive. Basically, you need to look at the source code RobotDrive.cpp in the WPI library. For example, look at the function MecanumDrive_Polar. Read all four encoder readings (encoder->GetDistance()) and combine them using a reverse algorithm from MecanumDrive_Polar to calculate the magnitude, direction and rotation. Our team used mecanum wheels for last year and use them again this year. So we want to develop a library module to deal with 4 encoders for autonomous drive. Unfortunately, our build head selected gearboxes that made mounting the KOP encoders impossible. So we never get a chance to develop that library module. But if we were really going ahead to implement it, that would be how I would do it.

tomy 14-02-2011 12:43

Re: Encoders in Auto Mode
 
i thought it would be simple cause all we want to do is drive forward (for starters) my thought was that you would say in telop encoder.getdistante() and then play that back in auto mode or is it harder than that?

Ether 14-02-2011 12:54

Re: Encoders in Auto Mode
 
Quote:

Originally Posted by mikets (Post 1022657)
look at the function MecanumDrive_Polar. Read all four encoder readings (encoder->GetDistance()) and combine them using a reverse algorithm from MecanumDrive_Polar to calculate the magnitude, direction and rotation.

Have you developed this algorithm?



mikets 14-02-2011 13:04

Re: Encoders in Auto Mode
 
No, if I had, I would have posted it. I probably would take some time to do this after the season. Too busy at the moment. I have an algorithm in mind, but I need to enter the algorithm into excel and make sure the numbers will go a round trip without problem. i.e. With a given set of magnitude, direction and rotation numbers, use the algorithm in MecanumDrive_Polar to calculate the four wheel speeds and then calculate a set of encoder distances with a given drive time, apply the reverse algorithm to the encoder distances that should give me a set of: magnitude of distance traveled, direction of distrance travel, rotation delta since start. If I divide it by drive time, I should get back a set of number corresponding to the original numbers. I probably got some aspects wrong. That's why I need to simulate that in excel to figure out the details of the reverse algorithm.

tomy 14-02-2011 13:11

Re: Encoders in Auto Mode
 
so then what is the easiest way of doing it in auto mode?

mikets 14-02-2011 13:12

Re: Encoders in Auto Mode
 
Tomy, if you only care about going straight forward, the algorithm may be a lot simpler without considering all the corner cases. When going straight forward, all four wheels will turn in the same direction with the same strength. So you could do the following:
Code:

double distanceTraveled = (leftfrontEncoder->GetDistance() +
                                    rightFrontEncoder->GetDistance() +
                                    leftRearEncoder->GetDistance() +
                                    rightRearEncoder->GetDistance())/4.0;
if (distanceTraveled < targetDistance)
{
    MecaumDrive_Polar(forwardPower, 0.0, 0.0);
}
else
{
  MecaumDrive_Polar(0.0, 0.0, 0.0);
}


tomy 14-02-2011 13:25

Re: Encoders in Auto Mode
 
where do you set the target distance and why do you divide it by 4?

mikets 14-02-2011 13:40

Re: Encoders in Auto Mode
 
Add the four encoder values and divide by 4 is averaging them. targetDistance is the distance you want to travel. For example, if you want to travel 5 ft (60 inches), you first set the encoder unit by calling encoder->SetDistancePerPulse for inches. Then set your targetDistance to 60.0.

tomy 14-02-2011 13:44

Re: Encoders in Auto Mode
 
ok so correct my if im wrong but for 60 inches it would look something like this?


Code:


encoder->Setdistanceperpulse (60.0)
double distanceTraveled = (leftfrontEncoder->GetDistance() +
                                    rightFrontEncoder->GetDistance() +
                                    leftRearEncoder->GetDistance() +
                                    rightRearEncoder->GetDistance())/4.0;
if (distanceTraveled < targetDistance)
{
    MecaumDrive_Polar(forwardPower, 0.0, 0.0);
}
else
{
  MecaumDrive_Polar(0.0, 0.0, 0.0);
}

thanks for the help in advance im new to c++

Ether 14-02-2011 13:55

Re: Encoders in Auto Mode
 
Quote:

Originally Posted by mikets (Post 1022677)
No, if I had, I would have posted it. I probably would take some time to do this after the season. Too busy at the moment. I have an algorithm in mind, but I need to enter the algorithm into excel and make sure the numbers will go a round trip without problem. i.e. With a given set of magnitude, direction and rotation numbers, use the algorithm in MecanumDrive_Polar to calculate the four wheel speeds and then calculate a set of encoder distances with a given drive time, apply the reverse algorithm to the encoder distances that should give me a set of: magnitude of distance traveled, direction of distrance travel, rotation delta since start. If I divide it by drive time, I should get back a set of number corresponding to the original numbers. I probably got some aspects wrong. That's why I need to simulate that in excel to figure out the details of the reverse algorithm.

You might the discussion of forward kinematics on Pages 7,8,&9 of this paper of interest.




mikets 14-02-2011 13:57

Re: Encoders in Auto Mode
 
No, you must call SetDistancePerPulse for all four encoders after they are instantiated (as part of the encoder initialization). The parameter to SetDistancePerPulse must be calculated or experimented. Basically, one revolution of the encoder will give you x pulses (I forgot what the KOP encoder is, probably 1440 pulses per revolution), then you have to multiply with the gear ratio if the encoders are mounted on the motor side instead of the wheel side.
Code:

Do this in your encoder intiialization:
distancePerPulse = (wheel_diameter*PI/pulses_per_revolution)*gear_ratio;
leftfrontEncoder->SetDistancePerPulse(distancePerPulse);
leftrearEncoder->SetDistancePerPulse(distancePerPulse);
rightfrontEncoder->SetDistancePerPulse(distancePerPulse);
rightrearEncoder->SetDistancePerPulse(distnacePerPulse);
 
Then do this in your autonomous loop:
double distanceTraveled = (leftfrontEncoder->GetDistance() +
                                    rightFrontEncoder->GetDistance() +
                                    leftRearEncoder->GetDistance() +
                                    rightRearEncoder->GetDistance())/4.0;
if (distanceTraveled < 60.0)
{
    MecaumDrive_Polar(forwardPower, 0.0, 0.0);
}
else
{
  MecaumDrive_Polar(0.0, 0.0, 0.0);
}


mikets 14-02-2011 13:59

Re: Encoders in Auto Mode
 
Quote:

Originally Posted by Ether (Post 1022728)
You might the discussion of forward kinematics on Pages 7,8,&9 of this paper of interest.

Thanks Ether, I will read it.

tomy 14-02-2011 14:07

Re: Encoders in Auto Mode
 
Quote:

Originally Posted by mikets (Post 1022729)
No, you must call SetDistancePerPulse for all four encoders after they are instantiated (as part of the encoder initialization). The parameter to SetDistancePerPulse must be calculated or experimented. Basically, one revolution of the encoder will give you x pulses (I forgot what the KOP encoder is, probably 1440 pulses per revolution), then you have to multiply with the gear ratio if the encoders are mounted on the motor side instead of the wheel side.
Code:

Do this in your encoder intiialization:
distancePerPulse = (wheel_diameter*PI/pulses_per_revolution)*gear_ratio;
leftfrontEncoder->SetDistancePerPulse(distancePerPulse);
leftrearEncoder->SetDistancePerPulse(distancePerPulse);
rightfrontEncoder->SetDistancePerPulse(distancePerPulse);
rightrearEncoder->SetDistancePerPulse(distnacePerPulse);
 
Then do this in your autonomous loop:
double distanceTraveled = (leftfrontEncoder->GetDistance() +
                                    rightFrontEncoder->GetDistance() +
                                    leftRearEncoder->GetDistance() +
                                    rightRearEncoder->GetDistance())/4.0;
if (distanceTraveled < 60.0)
{
    MecaumDrive_Polar(forwardPower, 0.0, 0.0);
}
else
{
  MecaumDrive_Polar(0.0, 0.0, 0.0);
}


so this funtion will make the robot drive until it goes 60 inches.

my question is isnt there any easier way of doing it because you can fine the distance traveled by using the .getdistance in telop mode. why cant you just say something like

myrobot.drive till encoder turn so many revolutions?

mikets 14-02-2011 14:56

Re: Encoders in Auto Mode
 
That's basically what the code is doing: drive until the encoder->GetDistance() say it has traveled 60 inches.
You could put the code in a function like this:
Code:

void DriveDistance(float targetDistance)
{
    float distanceTraveled = (leftfrontEncoder->GetDistance() +
                                    rightFrontEncoder->GetDistance() +
                                    leftRearEncoder->GetDistance() +
                                    rightRearEncoder->GetDistance())/4.0;
 
    while (distanceTraveled < targetDistance)
    {
        MecanumDrive_Polar(forwardPower, 0.0, 0.0);
    }
    MecanumDrivePolar(0.0, 0.0, 0.0);
}

But this function is blocking. You cannot do anything else why the while loop is waiting. Not to mention this will starve your watchdog although by calling the MecanumDrive_Polar function, you probably will feed the per motor watchdog for this year's WPI library. So it's better to do what I suggested in the previous post of checking the target distance in your autonomous loop.

tomy 14-02-2011 15:23

Re: Encoders in Auto Mode
 
so the best and easiest way is the way you described in post 16

Quote:

Do this in your encoder intiialization:
distancePerPulse = (wheel_diameter*PI/pulses_per_revolution)*gear_ratio;
leftfrontEncoder->SetDistancePerPulse(distancePerPulse);
leftrearEncoder->SetDistancePerPulse(distancePerPulse);
rightfrontEncoder->SetDistancePerPulse(distancePerPulse);
rightrearEncoder->SetDistancePerPulse(distnacePerPulse);

Then do this in your autonomous loop:
double distanceTraveled = (leftfrontEncoder->GetDistance() +
rightFrontEncoder->GetDistance() +
leftRearEncoder->GetDistance() +
rightRearEncoder->GetDistance())/4.0;
if (distanceTraveled < 60.0)
{
MecaumDrive_Polar(forwardPower, 0.0, 0.0);
}
else
{
MecaumDrive_Polar(0.0, 0.0, 0.0);
}


mikets 14-02-2011 15:24

Re: Encoders in Auto Mode
 
Yep, unless you have some other restriction/requirements.

tomy 14-02-2011 15:27

Re: Encoders in Auto Mode
 
ok i will try that thanks for the help

if i need more i will send you a message or put another post here


All times are GMT -5. The time now is 14:25.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi