Question about PID_v1.h library

I am able to run one DC motor successfully on an Arduino Mega using the PID_v1.h library.

My question is whether I can run two DC Motors simultaneously (each will have its own encoders) using the same library in a single sketch.

I can have distinct variables and started doing that until I came to this line:

double inputM1 = 0, outputM1 = 0, setpoint = 0; //M1 is Motor 1

The var setpoint is somehow yellow (like Arduino commands) and if I make it setpointM1 it turns black. Does this mean having two setpoints will not work because it has some special meaning to Arduino?

Also, the line after that -
PID myPID(&input, &output, &setpoint, kp, ki, kd, DIRECT); – can I have two PIDs going like this:

PID myPIDM1(&inputM1, &outputM1, &setpointM1, kp, ki, kd, DIRECT);
PID myPIDM2(&inputM2, &outputM2, &setpointM2, kp, ki, kd, DIRECT);

?

Ignore the colors. They are simply a visual artifact of various keywords in the library and elsewhere.

You will need two setpoints for two motors, and the names setpointM1/setpointM2 are fine.

can I have two PIDs going like this:

Yes, but with that construction, the k values will be the same for the two motors.

jremington:
Ignore the colors. They are simply a visual artifact of various keywords in the library and elsewhere.

You will need two setpoints for two motors, and the names setpointM1/setpointM2 are fine.
Yes, but with that construction, the k values will be the same for the two motors.

Both the motors are identical in every respect so the same k values are not a problem.
Thank you for your prompt response.

The motors are not identical, despite appearances. You will find that when powered by the same voltage supply, motors of the same type and manufacturer will turn at different speeds. A common complaint on this forum is "my robot does not drive straight".

However, the differences are unlikely to lead to different k values.

jremington:
You will need two setpoints for two motors,

Unless you want them to always run at the same speed. :slight_smile:

jremington:
The motors are not identical, despite appearances. You will find that when powered by the same voltage supply, motors of the same type and manufacturer will turn at different speeds. A common complaint on this forum is "my robot does not drive straight".

However, the differences are unlikely to lead to different k values.

Actually, I am using minimal kP and no kD or kI at all. The motors have almost no inertia - so when the power is cut, they stop cold.