Hi Holoratte.
Cool that it's work i think i try with the Due to see if we can track at a longuer distance.
In the perimeter tracking i am going to see that i tell you to read the perimeter each 15ms but it's better to verify the good value for the Mega board.
The perimetermag value is refresh in the readsensors() and the tracking need to read this value as fast as possible for a correct PID compute.
To find the optimal value you need to make a debug record with the console while mower is in tracking mode.
first set the console speed to 115200
next add the 5 debug lines at the beginning of the motorControlPerimeter()
Code:
// PID controller: track perimeter
void Robot::motorControlPerimeter() {
Console.print(millis());
Console.print(" perimeterMagMaxValue ");
Console.print(perimeterMagMaxValue);
Console.print(" perimeterMag ");
Console.println(perimeterMag);
if (millis() < nextTimeMotorPerimeterControl) return;
nextTimeMotorPerimeterControl = millis() + 15; //bb read the perimeter each 50 ms
then in the void Robot::readSensors() {
set the if (stateCurr == STATE_PERI_TRACK) nextTimePerimeter = millis() + 10; to 10ms
to read as faster as possible
Code:
if ((perimeterUse) && (millis() >= nextTimePerimeter)){
//bb
if (stateCurr == STATE_PERI_TRACK) nextTimePerimeter = millis() + 15;
else nextTimePerimeter = millis() + 50; // 50
perimeterMag = readSensor(SEN_PERIM_LEFT);
if (stateCurr == STATE_PERI_FIND)perimeterMagMedian.add(abs(perimeterMag));
if ((perimeter.isInside(0) != perimeterInside)){
And now lets the mower track in a straight line.
The result in the console normaly get the same perimeterMag value for 3 or 5 lines of each near 10ms so 30 or 50 ms.
Remove the duplicate value after the first one and the millis difference can tell you the perfect value you need to use in the 2 function.
Here result for the DUE with Azurit 1.08 and original ADCMAN without the repeat value
27302 perimeterMagMaxValue 2183 perimeterMag -41
27319 perimeterMagMaxValue 2183 perimeterMag 24
27336 perimeterMagMaxValue 2183 perimeterMag 41
27353 perimeterMagMaxValue 2183 perimeterMag 51
27371 perimeterMagMaxValue 2183 perimeterMag 55
27404 perimeterMagMaxValue 2183 perimeterMag 23
27421 perimeterMagMaxValue 2183 perimeterMag -41
27438 perimeterMagMaxValue 2183 perimeterMag -105
27456 perimeterMagMaxValue 2183 perimeterMag -139
27475 perimeterMagMaxValue 2183 perimeterMag -135
Standard ADCMAN average 17 or 18 ms to refresh the Mag
So change the 15ms to 20ms for DUE to be sure the pid do not receive the same value 2 times
Here the result for the DUE with sunray ADCMAN..
10355 perimeterMagMaxValue 2000 perimeterMag 179
10368 perimeterMagMaxValue 2000 perimeterMag 222
10381 perimeterMagMaxValue 2000 perimeterMag 175
10406 perimeterMagMaxValue 2000 perimeterMag 196
10419 perimeterMagMaxValue 2000 perimeterMag 168
10432 perimeterMagMaxValue 2000 perimeterMag 192
10458 perimeterMagMaxValue 2000 perimeterMag 232
10471 perimeterMagMaxValue 2000 perimeterMag 264
10484 perimeterMagMaxValue 2000 perimeterMag 260
Sunray ADCMAN average 13 or 14 ms to refresh the Mag
As i use this ADCMAN it's why i put 15 ms in my code .
If someone have a mega and find the correct value we can make the diffence between these 2 boards with #ifdef __AVR__
Faster we read the Mag and better is the tracking.