Close

Dog detector Transmitting All Data to Arduino

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 ☠ 10/23/2018 at 17:010 Comments

After a few days of frantic code writing, I managed to cobble together a functional set of programs to send and receive the four coordinates of each box, the number of boxes detected simultaneously and the current box number …. All in a user friendly format that can later be processed into commands to steer the WEEDINATOR machine.

Here's the code used on the Jetson TX2:

int i2cwrite(int writeValue) 
{
  int toReturn = i2c_smbus_write_byte(kI2CFileDescriptor, writeValue);
  if (toReturn < 0) 
  {
    printf(" ************ Write error ************* \n") ;
    toReturn = -1 ;
  }
  return toReturn ;
}

void OpenI2C()
{
  int length;
  unsigned char buffer[60] = {0};
  //----- OPEN THE I2C BUS -----
  char *filename = (char*)"/dev/i2c-1";
  if ((kI2CFileDescriptor = open(filename, O_RDWR)) < 0)
  {
    //ERROR HANDLING: you can check errno to see what went wrong
    printf("*************** Failed to open the i2c bus ******************\n");
        //return;
  }
  if( ioctl( kI2CFileDescriptor, I2C_SLAVE, PADDYADDRESS ) < 0 )
  {
    fprintf( stderr, "Failed to set slave address: %m\n" );
                //return 2;
  }
}

void I2CDataHandler()
{
  printf(" My box number  = %i \n",myBoxNumber);
  for( int j=0; j < 4; j++ )
  {
  if(j==0){i2cwrite(200+myNumberOfBoxes);  }                 // Total number of bounding boxes.
  if(j==0){i2cwrite(140+myBoxNumber);  }                     // Designates bounding box number.
  i2cwrite(120+j);                                           // Designates box corner number
  printf(" intBB[j]   = %i \n",intBB[j]);

  top = intBB[j];                        
  myArray[j][0] = static_cast<int>(top/1000);
  printf(" myArray[j][0]  = %i \n",myArray[j][0]);
  i2cwrite(myArray[j][0]);

  top = (top - myArray[j][0]*1000);
  myArray[j][1] = static_cast<int>(top/100);
  printf(" myArray[j][1]  = %i \n",myArray[j][1]);
  i2cwrite(myArray[j][1]);

  top = (top - myArray[j][1]*100);
  myArray[j][2] = static_cast<int>(top/10);
  printf(" myArray[j][2]  = %i \n",myArray[j][2]);
  i2cwrite(myArray[j][2]);

  top = (top - myArray[j][2]*10);
  myArray[j][3] = static_cast<int>(top);
  printf(" myArray[j][3]  = %i \n",myArray[j][3]); 

  i2cwrite(myArray[j][3]);
  }
}

And the code for recieving the data on an Arduino:

#include <Wire.h>
long y[4][4];
int a;
int b;
int c;
int d;
long x =0;
int i;
int j;
int numberOfBoxes;
int xMax;

void setup() 
{
  Wire.begin(0x70);                // join i2c bus with address
  Wire.onReceive(receiveEvent); // register event
  //Wire.begin(0x50);                // join i2c bus with address
  //Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop() 
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) 
{
  //delay(50);
  int x = Wire.read();    // receive byte as an integer
  //Serial.print("  Integer:  ");Serial.println(x);         // print the integer


  if(x>199)
  {
    numberOfBoxes = x-200;
  }
  if((x>139)&&(x<200))
  { 
    j=x-140;Serial.print("Number of boxes: ");Serial.print(numberOfBoxes);Serial.print(", Box number: ");Serial.println(j); 
  }
  if(x==120){ i =-1; }
  if(i==0){ y[0][0] = x*1000; }
  if(i==1){ y[0][1] = x*100; }
  if(i==2){ y[0][2] = x*10; }
  if(i==3){ y[0][3] = x;}
  a= y[0][0]+y[0][1]+y[0][2]+y[0][3];

  if(x==121){ i = 4;  Serial.print("  corner a:  ");Serial.println(a);}
  if(i==5){ y[1][0] = x*1000; }
  if(i==6){ y[1][1] = x*100; }
  if(i==7){ y[1][2] = x*10; }
  if(i==8){ y[1][3] = x; }
  b = y[1][0]+y[1][1]+y[1][2]+y[1][3];

  if(x==122){ i = 9;  Serial.print("  corner b:  ");Serial.println(b);}
  if(i==10){ y[2][0] = x*1000; }
  if(i==11){ y[2][1] = x*100; }
  if(i==12){ y[2][2] = x*10; }
  if(i==13){ y[2][3] = x;   }
  c= y[2][0]+y[2][1]+y[2][2]+y[2][3];

  if(x==123){ i = 14;  Serial.print("  corner c:  ");Serial.println(c);}
  if(i==15){ y[3][0] = x*1000; }
  if(i==16){ y[3][1] = x*100; }
  if(i==17){ y[3][2] = x*10; }
  if(i==18){ y[3][3] = x;  }
  d= y[3][0]+y[3][1]+y[3][2]+y[3][3];
  if(i==18){  Serial.print("  corner d:  ");Serial.println(d);Serial.println("");}
  
  i++;
}

 All files are on Github HERE.

Discussions