Using functions in PIDController

We are trying to use the PIDController class. However, it seems that most of the functions are virtual and therefore unusable. Although we can still use the PIDController, it would be nice if we could do things like get the current error without writing our own functions. Does anybody know if there is a way to use these virtual functions, like through a possible derived class somewhere?

The functions are virtual so they are overridable in derived classes. In case you want to make a PID controller with gravity compensation or static friction feedforward or something. They’re all implemented in the base PIDController class, so you should be able to call them without exceptions.

Just because a method is virtual doesn’t mean there’s no implementation behind it. The PIDController::GetError() method has a definition behind it.


/**
 * Returns the current difference of the input from the setpoint
 * @return the current error
 */
float PIDController::GetError() const {
  double pidInput;
  {
    std::lock_guard<priority_recursive_mutex> sync(m_mutex);
    pidInput = m_pidInput->PIDGet();
  }
  return GetSetpoint() - pidInput;
}

The virtual keyword just ensures you get the “right” method call if you extend PIDController and want your own implementation of GetError to run. So, if you extend PIDController and make a new ShooterPIDController and you implement your own implementation of GetError() and your instance of ShooterPIDController is cast into PIDController and you call ->GetError() on that pointer you will get the ShooterPIDController::GetError implementation. If it wasn’t virtual you’d get PIDController::GetError() because then the decision would be made at compile time and not runtime.

You’re only forced to implement a virtual method when the base class makes it null. So something declared: virtual float GetError() = 0; would force you to actually implement it if you wanted to use it, and that would have to be in a derived class.

I do not miss C++ at all. :slight_smile:

I did some hunting in the class hierarchy and there are no derived classes. That’s why we’re confused. Maybe it is a good idea to create our own derived classes instead of using PIDController directly?

You CAN use PIDController directly, because it has all those functions implemented. Why don’t you try it first, then tell us if you have any problems with how it’s working.

Unless you have specific reasons to do so, such as a custom error calculation or other changes from the default PID calculations done by WPILib, it’s easier to just use the built in implementation. As everyone else has said, the functions are virtual so that IF you decide to create a custom controller, you can override the default implementation.

I have tried it, that’s why I’m asking for help. I’m able to instantiate the PIDController class just fine. The problem is that when I try to use the virtual functions it causes problems, and eclipse says it has something to do with those functions being virtual.

If you want helpful advice, you need to be more specific about what exactly you’re having trouble with. What do you expect to happen when you call the functions, and what happens instead? What is the actual error message from Eclipse?