Getting a pid to operate a model boat

Hi
As a newcomer I have been building up a sketch for a model boat.

The whole thing as attached works but, the pid really jerks the servo around.

Purpose of the sketch is:

• That the boat operates normally using a transmitter and receiver, in that rudder and power all work. AuxIn (line 182) throws a couple of auxilliary relays to do whatever..

• However when the unHydroIn < 1460 (line 223), it records the compass bearing

• And when unHydroIn is < 1570 is used, (line 206) (it corresponds to the trim tab on the transmitter) it attaches the output to the rudder to the pid

• So when the compass heading is recorded, the tab is used and the boat will maintain a heading.
The compass reading is smoothed by 10 division. (Line 40)

I have calibrated the compass and even when still it moves between 178 and 182 degrees maximum.

I have also added 3 capacitors to the power and signal of the servo, but that didn’t seem to do anything at all.

The interesting thing is that when I activate the print lines (line 141) the pid smoothes out quite a lot. When I deactivate the print function it really gets out of hand

I can’t get this to operate smoothly at all. I have tried numerous variations of pid (line 20) and this is the best I can do.

Is there anything obvious I can do??

I have another versions of this that doesn’t have the pid at all. It just operates using the error on the compass. That is nice but not very reactive, but the servo behaves nicely. I intend to try all the sketches on the pond soon.

John

_3channels6manualaddpid.ino (9.79 KB)

The interesting thing is that when I activate the print lines (line 141) the pid smoothes out quite a lot. When I deactivate the print function it really gets out of hand

This suggests that the time taken to print the data slows the process down enough to smooth things out.

  // DEBUG LOGGING
  unsigned long currentMillis = millis();
  if (currentMillis - PreviousSerialWriteMillis > SerialWritInterval) {
    PreviousSerialWriteMillis = currentMillis;

  }

How is this useful, if you don't actually do any printing?

The output from the PID process should be the amount of change needed, shouldn't it? You are assuming that the output should be the new rudder angle.

I'd dump a lot of that code, until I understood what the single PID-controlled mode was doing.

OK. I will strip this thing down to the pid, compass and necessary code, and then look again and come on here for advice at that point if required. Cheers