Go to Post This is where a tenacious NEM comes in very handy. - Mark McLeod [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 4 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 26-01-2011, 15:44
prometheus prometheus is offline
Registered User
FRC #2992
 
Join Date: Jan 2011
Location: Mandeville
Posts: 5
prometheus is an unknown quantity at this point
PID output to a variable

I am trying to use a PID loop from the WPI library to control my robot drive in the autonomous mode. I don't know how to make the PID loop output to a variable that I can set to the drive speed. I tried making a bogus victor and setting a float variable equal to that value, but I got an error when I built the code saying "cannot convert `Victor*' to `float' in assignment". Any ideas out there?

Thanks in advance,
will
Reply With Quote
  #2   Spotlight this post!  
Unread 26-01-2011, 15:56
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 667
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: PID output to a variable

The way the PID controller in WPI lib works is that you inherit the PIDOutput class and provide a PIDWrite callback function. The PID controller will call the PIDOutput:: PIDWrite function to run your motors. In essence, do something similar to this:
Code:
class MyRobot: public SimpleRobot, public PIDOutput
{
 
public:
    void PIDWrite(float output)
    {
        //program your motors accordingly with the output value.
    }
 
    MyRobot()
    {
        //Create the PID controller object.
    }
}
__________________
Reply With Quote
  #3   Spotlight this post!  
Unread 26-01-2011, 16:15
prometheus prometheus is offline
Registered User
FRC #2992
 
Join Date: Jan 2011
Location: Mandeville
Posts: 5
prometheus is an unknown quantity at this point
Re: PID output to a variable

Thanks mikets. That looks very helpful, but I don't know a whole lot of C++ and I'm not exactly sure about what I am supposed to do with that code that you gave me. Do I need to make a subclass? Do I put the code that controls the motors in the subclass?

So far what I have been trying is:

----------------------------------
class IterativeDemo : public IterativeRobot
{
RobotDrive *myRobot;

PIDEncoder *leftencoder;
PIDEncoder *rightencoder;

float leftAutoDrive;
float rightAutoDrive;

public:
myRobot = new RobotDrive(1, 3, 2, 4);

leftencoder = new PIDEncoder(7,8,true,Encoder::k4X);
leftencoder->SetDistancePerPulse(0.04318); //inches
leftencoder->Start();

rightencoder = new PIDEncoder(9,10,true,Encoder::k4X);
rightencoder->SetDistancePerPulse(0.04318); //inches
rightencoder->Start();

leftAutoControl = new PIDController(0.1, 0.01, 0.001, leftencoder, bogusLeft);
rightAutoControl = new PIDController(0.1, 0.01, 0.001, rightencoder, bogusRight);
}

void AutonomousInit(void) {
auto_periodic_loops = 0; // Reset the loop counter for autonomous mode
disabled_periodic_loops = 0;
tele_periodic_loops = 0;

leftAutoControl->Enable();
rightAutoControl->Enable();

//Initialization
LoopsPerSec = 50;

}

void AutonomousPeriodic(void) {
// feed the user watchdog at every period when in autonomous
GetWatchdog().Feed();

auto_periodic_loops++;
dash_periodic_loops++;


leftAutoControl->SetSetpoint(18);
rightAutoControl->SetSetpoint(18);

leftAutoDrive = bogusLeft;
rightAutoDrive = bogusRight;
myRobot->TankDrive(rightAutoDrive, leftAutoDrive); // drive with tank style

}
---------------------------

The error is in the "leftAutoDrive = bogusLeft" and "rightAutoDrive = bogusRight" parts.

Last edited by prometheus : 26-01-2011 at 16:23. Reason: Added Code
Reply With Quote
  #4   Spotlight this post!  
Unread 26-01-2011, 17:16
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 667
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: PID output to a variable

Are you going to use the encoders to drive straight or do you also want to use the encoders to turn a certain angle? Your code suggested you have 4-motor drive system. Are you doing mecanum or just normal wheels? Where are the encoders mounted? These questions are important to determine how the code is written.
__________________
Reply With Quote
  #5   Spotlight this post!  
Unread 26-01-2011, 17:22
prometheus prometheus is offline
Registered User
FRC #2992
 
Join Date: Jan 2011
Location: Mandeville
Posts: 5
prometheus is an unknown quantity at this point
Re: PID output to a variable

We are just trying to go straight for now. We are planning on using a 4-motor drive on our final robot, but the prototype chassis we have to play with our programming on only has two motors. We are just doing normal wheels. Thank you for taking time to give me such a specific answer.
Reply With Quote
  #6   Spotlight this post!  
Unread 26-01-2011, 18:39
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 667
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: PID output to a variable

The PID controller in WPIlib has the following constructor:
Code:
PIDController(float p, float i, float d, PIDSource *source, PIDOutput output, float period = 0.05);
It means it takes a PIDSource and a PIDOutput object pointers. In the PID controller object, it periodically calls the PIDGet() function from the PIDSource object to obtain the input value (your encoders) and applies the PID algorithm to calculate the output. Then it calls the PIDWrite() function from the PIDOutput object to write to the motors.
This means you need to provide the PIDSource and PIDOutput objects when creating the PIDController object. The easiest way to do it is to have your IterativeDemo class inheriting both PIDSource and PIDOutput. Essentially, you are saying IterativeDemo is both PIDSource and PIDOutput as well. Then in the IterativeDemo class, you need to implement PIDGet() and PIDWrite().
If you just need to go straight, you need only one PID controller. Something similiar to the following:
Code:
class IterativeDemo : public IterativeRobot, public PIDSource, public PIDOutput
{
    ...
    double PIDGet()
    {
        return (leftEncoder->GetDistance() + rightEncoder->GetDistance())/2;
    }
 
    void PIDWrite(float output)
    {
        myRobot->TankDrive(output, output);
    }
 
    //
    // Constructor
    //
    IterativeDemo()
    {
        leftEncoder = new PIDEncoder(7,8,true,Encoder::k4X);
        leftEncoder->SetDistancePerPulse(???);
        rightEncoder = new PIDEncoder(9,10,true,Encoder::k4X);
        rightEncoder->SetDistancePerPulse(???);
        driveControl = new PIDController(0.1, 0.01, 0.001, this, this);
        ...
    }
 
    void AutonomousInit()
    {
        driveControl->Enabled();
    }
 
    void AutonomousPeriodic()
    {
        driveControl->SetSetpoint(18);
    }
}
Now if you also want to do PID control turn, that's a lot more challenging
You then need two PID controllers and they are not independent of each other.
__________________

Last edited by mikets : 26-01-2011 at 18:53.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 02:35.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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