Close

Re-visiting Code from 9 Months Ago

A project log for Autonomous Agri-robot Control System

Controlling autonomous robots the size of a small tractor for planting, weeding and harvesting

capt-flatus-oflahertyCapt. Flatus O'Flaherty ☠ 06/12/2019 at 10:120 Comments
Nine months ago, the machine was using a Pixy2 camera to track barcode labels thrown down on the ground. The machine would steer towards the label and then stop dead when the barcode appeared at a pre-defined location on the x axis. Furthermore, the code revolves around calculating a simple 'panError' for the y axis, which is a slightly confusing name as it's not like a de-bugging error or such like. I'm going to change the name to 'deviance' for the future.

Since only one barcode label was being recognised at the time, the calculation was very simple - just subtract the actual camera y coordinate from the desired coordinate. The same is true for the upgraded camera, where it is detecting the coordinate position of the individual crops, except that there's normally going to be about 6 plants and some of them might not be recognised properly or might just have died or been eaten by a pigeon!

The principle is exactly the same as for the Pixy2 and the solution is equally simple - just add up the individual 'deviances', divide by the number of plants detected and then subtract from the desired coordinate. The main difference is that the same type of calculation is going to be used for both the x and y axes.

The calculations themselves are made on the Jetson TX2 during detection itself and then output via I2C to an Arduino Nano intermediate as simple steering or drive commands eg integer '3' means 'stop dead'.

The following code uses the term 'nonant' which refers to a 3 x 3 matrix but really should really be called 'sextant' as the matrix currently detected is 3 x 2. I'm just too lazy to change the name just yet
//printf(" Box Area (%i) = %i \n",n,myBoxArea[n]);                          // Print the area (size) of the box.
//printf(" Box Centre (x,%i) = %i \n",n,myBoxCentreX[n]);                   // Print the box centre (x) coordinate.
//printf(" Box Centre (y,%i) = %i \n",n,myBoxCentreY[n]);                   // Print the box centre (y) coordinate.
// Divide up into 'Nonants' (the 9 version of quadrants).                 // 1200/3 = 400, 720/3 = 240.
// Or divide into sextants eg 1920/3 = 640:
if (( myBoxCentreX[n] <= 640 ) && ( myBoxCentreY[n] <= 540 ))
{
    nonant = 0;
    nonantDevianceX[0] =  myBoxCentreX[n] -320;
    nonantDevianceY[0] =  myBoxCentreY[n] -270;
    //printf(" Nonant (%i) = %i \n",n,nonant);
}
if (( myBoxCentreX[n] >= 641 ) && ( myBoxCentreX[n] <= 1280 ) && ( myBoxCentreY[n] <= 540 ))
{
    nonant = 1;
    nonantDevianceX[1] =  myBoxCentreX[n] -960;
    nonantDevianceY[1] =  myBoxCentreY[n] -270;
    //printf(" Nonant (%i) = %i \n",n,nonant);
}
if (( myBoxCentreX[n] >= 1281 ) && ( myBoxCentreY[n] <= 540 ))
{
    nonant = 2;
    nonantDevianceX[2] =  myBoxCentreX[n] -1600;
    nonantDevianceY[2] =  myBoxCentreY[n] -270;
    //printf(" Nonant (%i) = %i \n",n,nonant);
}
if (( myBoxCentreX[n] <= 640 ) && ( myBoxCentreY[n] >= 541 ))
{
    nonant = 3;
    nonantDevianceX[3] =  myBoxCentreX[n] -320;
    nonantDevianceY[3] =  myBoxCentreY[n] -810;
    //printf(" Nonant (%i) = %i \n",n,nonant);
}
if (( myBoxCentreX[n] >= 641 ) && ( myBoxCentreX[n] <= 1281 ) && ( myBoxCentreY[n] >= 541 ))
{
    nonant = 4;
    nonantDevianceX[4] =  myBoxCentreX[n] -960;
    nonantDevianceY[4] =  myBoxCentreY[n] -810;
    //printf(" Nonant (%i) = %i \n",n,nonant);
}
if (( myBoxCentreX[n] >= 1281 ) && ( myBoxCentreY[n] >= 541 ))
{
    nonant = 5;
    nonantDevianceX[5] =  myBoxCentreX[n] -1600;
    nonantDevianceY[5] =  myBoxCentreY[n] -810;
    //printf(" Nonant (%i) = %i \n",n,nonant);
}

//printf(" Nonant (%i) = %i \n",n,nonant);
//printf("..................................................... \n");
//I2CDataHandler();
}

Discussions