PID Line Follower

Good evening everyone,

I hear you thinking… not again a PID line follower.
I did have search the web for it alot, but all of them use a libary from QTRsensors. And I don’t have any sensors from them.
I connected 5 IR sensors beneath the robot. One in middle, 1 cm from middle left and right. And from there 1.5 cm to the left and right.

Anyway,

It doesn’t work right now, it follows a straight line a little bit, but when there is a turn it totally skip it. I am using a geared motor:
Ali

Is my code correct, or is it totally wrong. If yes, please help me a little bit with it.

Thanks

Lijnvolger.ino (3.65 KB)

I did have search the web for it alot, but all of them use a libary from QTRsensors. And I don't have any sensors from them.

That does not necessarily mean that you can't use their library.

You don't have to, of course. What you do need to do is determine how the sensor data that you do get defines how far off course you are. That error then is fed into the PID algorithm to determine a correction factor that you need to somehow apply to getting back on course.

    sensor[0] = analogRead(A0);
    sensor[1] = analogRead(A1);
    sensor[2] = analogRead(A2);
    sensor[3] = analogRead(A3);
    sensor[4] = analogRead(A4);

Generally, when reading from different analog pins, the first value read from a different pin is suspect, and discarded.

      if (sensor[i] > 700)

Magic numbers suck. What does a reading above 700 mean?

Is my code correct

Does it work?

It doesn't work right now

Then, no, it is not correct.

but when there is a turn it totally skip it.

What do your Serial.print()s tell you is happening? Why don't you have any?

PaulS:
Generally, when reading from different analog pins, the first value read from a different pin is suspect, and discarded.

How come?

Rupert909:
How come?

There is only one analog to digital converter. It involves a capacitor and resistor. The cap is charged by whatever is connected to the pin. The resistor defines how long it takes to charge the cap. An analog read discharges the cap and times how long it takes to charge up again.

With different sources supplying current to the pin, the cap may not be fully charged/discharged by the current source, after only one read cycle. So, dump the first reading. The second will be accurate.

PaulS:
There is only one analog to digital converter. It involves a capacitor and resistor. The cap is charged by whatever is connected to the pin. The resistor defines how long it takes to charge the cap. An analog read discharges the cap and times how long it takes to charge up again.

With different sources supplying current to the pin, the cap may not be fully charged/discharged by the current source, after only one read cycle. So, dump the first reading. The second will be accurate.

I knew that the analog pins are multiplexed, but would think that the read cycle was complete with regards to resetting the internals of the converter. I thought the AD converter consisted of a ladder of resistors doing voltage divisions, plus some magic, but then I'm a software guy .... :slight_smile:

I'll keep this in mind!

Sensors(i) above 700 for the black line detect.
Works fine, ptoblem now is that it follows it but it is steering wayy too late, so it follows the line by going around it.

Any ideas?

Solved,

Needed to add a (millis) delay from around 50ms and now it works perfect.

thanks anyways

PaulS:
There is only one analog to digital converter. It involves a capacitor and resistor. The cap is charged by whatever is connected to the pin. The resistor defines how long it takes to charge the cap. An analog read discharges the cap and times how long it takes to charge up again.

With different sources supplying current to the pin, the cap may not be fully charged/discharged by the current source, after only one read cycle. So, dump the first reading. The second will be accurate.

Disregard all that.

The correct information is:

If the source of the voltage you are measuring has an output impedance of 10k or less, extra
reads are entirely pointless. If the impedance is much higher you should read twice
after switching to a different pin, or you can add a 100nF capacitor to ground on the pin so
its impedance is low enough.

If the impedance is 40k, say, you can decide whether you care about a few bits of error or not,
since the cross talk will be of that order - the higher the impedance to more the input will see
crosstalk as the ADC mux is switched.

The ADC does not discharge the capacitor, which is why you can use much higher impedance
inputs when only ever reading one analog pin. The sample and hold capacitor can only pass
charge between the different analog pins. The DC input resistance of the ADC is very large indeed.

MarkT:
Disregard all that.

The correct information is:

If the source of the voltage you are measuring has an output impedance of 10k or less, extra
reads are entirely pointless. If the impedance is much higher you should read twice
after switching to a different pin, or you can add a 100nF capacitor to ground on the pin so
its impedance is low enough.

If the impedance is 40k, say, you can decide whether you care about a few bits of error or not,
since the cross talk will be of that order - the higher the impedance to more the input will see
crosstalk as the ADC mux is switched.

The ADC does not discharge the capacitor, which is why you can use much higher impedance
inputs when only ever reading one analog pin. The sample and hold capacitor can only pass
charge between the different analog pins. The DC input resistance of the ADC is very large indeed.

So you are saying the sample and hold capacitor of the DA converter is not drained between reads, as long as not changing the MUX to refer to another pin, and so on high impedance outputs, you will get it fully charged only after an extra read?

However, when changing the MUX, the S&H cap is drained, so as not to put current out on for example a grounded pin?

Rupert909:
So you are saying the sample and hold capacitor of the DA converter is not drained between reads, as long as not changing the MUX to refer to another pin, and so on high impedance outputs, you will get it fully charged only after an extra read?

However, when changing the MUX, the S&H cap is drained, so as not to put current out on for example a grounded pin?

No, I did not say that at all.