Arduino PID library question

Hi,

I use PIDs library to control servo(camera object tracking). But i want to rotate servo(camera) with potentiometer and start tracking from point i want. Problem is that Pids input is related with Pids output. How to "recalculate" servo position? Mayby some how to manipulate with PID output limits ?

For a PID-control measuring the REAL position (the actual value) is the most wanted thing the PID-control shall do.
Your potentiometer has to feed the setpoint into the PID-algorithm.

For a more detailed advice you have to provide a much more detailed description of what you want to do.

  • what hardware
  • how is everything wired together
  • your sketch

Make it as detailed as you can. There is no limit to the grade of detailing that can't be useful.
As long as you can't provide these details your question will sound like:

I have an unclear vision of ....

Can somebody finish my project through thinking about all the details and then deliver ready to flash code

Ok. Sorry I'm exaggerating but just for making things more clear.
best regards Stefan

sitas:
I use PIDs library to control servo(camera object tracking). But i want to rotate servo(camera) with potentiometer and start tracking from point i want. Problem is that Pids input is related with Pids output. How to "recalculate" servo position? Mayby some how to manipulate with PID output limits ?

You can either show your sketch or answer a bunch of questions about how the sketch works. Without information, it is very hard to guess what changes might work in your sketch.
What is the Setpoint of your PIDs?
What is the Input of your PIDs?
What do you do with the Output of your PIDs?

Everything is a little more complicated. Because I'm building an FPV camera control center. This center connects to the computer via usb where object tracking is performed and back data is sent through this control center to the transmitter via PPM signal. I can also control the functions of the tracking program from this center, i can to start and end the tracking, select the size of the region of interest, the resolution, etc. The program for windows is written with python. Joystik functions are also available i can pan tilt , zoom, and other things. All PWM values is converted to PPM with PWM to PPM converter.

Setpoint is max screen resoliution/2

Input is ROI center data (number of pixels from 0 to max rezoliution for valuex and valuey send from laptop via usb to arduino nano).

Output is pwm value for camera servo on the drone.

sketch_may24a.ino (11.4 KB)

sitas:
Setpoint is max screen resolution/2

I assume you mean the X/Y center of the screen.

sitas:
Input is ROI center data (number of pixels from 0 to max resolution for value-x and value-y send from laptop via usb to arduino nano).

So the position of the object being tracked, relative to the center of the screen.

sitas:
Output is pwm value for camera servo on the drone.

So the Output is attempting to point the camera so that the object being tracked is in the center of the screen.

Now, what did you want the joystick to do?

  • Do you want it to continue to track the SAME object but keep it in a different part of the screen (change Setpoint)?
  • Or do you want to change the object being tracked?
  • Or is there something else you want it to do?

I want to change the object being tracked. So i need to stop tracking(button push) then rotate camera with joystick to the new position and start tracking again(button push). Problem is that camera rotates to previuos position when i start new tracking. because Pid input is related with pid output. How to "recalculate" pid output or servo for new tracking position?

sitas:
I want to change the object being tracked. So i need to stop tracking(button push) then rotate camera with joystick to the new position and start tracking again(button push). Problem is that camera rotates to previuos position when i start new tracking. because Pid input is related with pid output. How to "recalculate" pid output for new tracking position?

I think I need to see your sketch. At least all of the parts of your sketch that uses the PIDs to control the servos.

Do the Outputs of the two PID's directly control the two servos? Is the Output range set to 0..180 (for servo.write()) or 1000..2000 (for servo.writeMicroseconds())?

When you have tracking turned off and the joysticks are in control, do they have direct control (centered joystick = centered servos) or relative control (joystick left turns servo left until it hits maximum left)?

sitas:
I want to change the object being tracked. So i need to stop tracking(button push) then rotate camera with joystick to the new position and start tracking again(button push). Problem is that camera rotates to previuos position when i start new tracking. because Pid input is related with pid output. How to "recalculate" pid output or servo for new tracking position?

How did you set things up so it can track the first object?

Presumably you just need to do all that again to track a different object?

...R

Hi johnwasser .
One pid Output control one servo for one x axis (now i testing one PID output for one servo x axis so later if i solve the problem i will write code for y axis).
PID Output range is set to 1000..2000 (servo.writeMicroseconds()).
When tracking is turned off the joystick is in relative control (joystick left turns servo left until it hits maximum left.)

Hi Robin2.
I start tracking by short pushing 3pos switch(not fixed pos switch) to position2( switch is on the joysticks hub) and at the same time sending signal to laptop to start tracking. Then i get data back from laptop to arduino.
YES. I need to do all that again to track a different object.

The arduino code is huge and messy. So i will try to attach it.

sketch_may24a.ino (11.4 KB)

sitas:
Hi Robin2.
I start tracking by short pushing 3pos switch(not fixed pos switch) to position2

I did not mean that, I meant what is the code that runs when you press that switch and which sets all the variables to the correct values for the first object? You probably just need to go through all that again when you switch to a new object.

Also keep in mind that some of the preparatory code might be in setup() and that may also need to be repeated when you want to change to a new object.

...R

Rather than using "joystick centered" to switch between auto-tracking and manual control, I think you want something like this where a global flag is set to indicate the mode. When you turn off auto-tracking the joystick becomes active and when you turn auto-tracking back on the joystick is disabled.

void loop()
{
  if (AutoTrackEnabled)
  {
    AutoTracking()
  }
  else
  {
    // Manual servo control
    servoPosition[0] = map(analogRead(JOYSTICK_X_PIN), 0, 1024, 1000, 2000);
    servoPosition[1] = map(analogRead(JOYSTICK_Y_PIN), 0, 1024, 1000, 2000);


    myServo[0].writeMicroseconds(servoPosition[0]);
    myServo[1].writeMicroseconds(servoPosition[1]);
  }
  
  // Other stuff
}

Robin2.Problem is because i even cant to find in the PID library information or example about what variable responsible for servo position at startup. No matter what variables i set in setup() I always get 1000ms(servo to max to the left) at the start tracking. Or maybe my joystick part of the code conflicts with the tracking part of the code. I tired from this problem because the code is huge and i am lost.

johnwasser thank you. I will try to rewrite the code.

I forgot that the joystick was supposed to have relative control...

  else // Not AutoTrackEnabled
  {
    // Manual servo control
    int x_offset = map(analogRead(JOYSTICK_X_PIN), 0, 1024, -10, 10);
    if (x_offset < -1 || x_offset > 1) // Dead Zone
    {
      servoPosition[0] = constrain(servoPosition[0] + x_offset, 1000, 2000);
      myServo[0].writeMicroseconds(servoPosition[0]);
    }


    int y_offset = map(analogRead(JOYSTICK_Y_PIN), 0, 1024, -10, 10);
    if (y_offset < -1 || y_offset > 1) // Dead Zone
    {
      servoPosition[1] = constrain(servoPosition[1] + y_offset, 1000, 2000);
      myServo[1].writeMicroseconds(servoPosition[1]);
    }
  }

sitas:
Problem is because i even cant to find in the PID library information or example about what variable responsible for servo position at startup.

The PID library is not responsible for the initial positions - they have to be set by other parts of your program. All the PID library does is tell you how much adjustment is needed when the setpoint is X and the actual position is Y.

There is a nice simple PID function in this link.

...R