Ground Sensors

From IridiaWiki
Revision as of 12:20, 27 February 2020 by Dgarzonr (talk | contribs)
Jump to navigationJump to search

Ground sensor

I found at least two ways of using the ground sensors on the e-pucks. This is the one that works best:

  int ground_tmp[6];
  int ground[3];
  /* retrieve the values from the sensor */
  e_i2cp_read(0xC0, 0);
  i = 0;
  for(j = 0; j < 6; j++) {
     if (j % 2 == 0) ground_tmp[j] = e_i2cp_read(0xC0, j + 1);
     else            ground_tmp[j] = e_i2cp_read(0xC0, j - 1);
  /* getting the actual values */
  ground[0] = ((unsigned int)(ground_tmp[0] & 0xff)) +((unsigned int) ground_tmp[1] << 8) - ground_sensor_zero[0];
  ground[1] = ((unsigned int)(ground_tmp[2] & 0xff)) +((unsigned int) ground_tmp[3] << 8) - ground_sensor_zero[1];
  ground[2] = ((unsigned int)(ground_tmp[4] & 0xff)) +((unsigned int) ground_tmp[5] << 8) - ground_sensor_zero[2];

This is the function I use to calibrate the sensors (on black paper):

void GroundSensorCalibrate() {
  int i, j;
  char tmp[50];
  long sensor_tmp[6] = {0};
  long sensor[3] = {0};
  long delay;
  btcomSendString("calibration ground:\n");
  for (i=0; i<16; i++) {
     // read the values
     e_i2cp_read(0xC0, 0);
     for(j = 0; j < 6; j++) {
        if (j % 2 == 0) sensor_tmp[j] = e_i2cp_read(0xC0, j + 1);
        else            sensor_tmp[j] = e_i2cp_read(0xC0, j - 1);
     // store the values
     sensor[0] += ((unsigned int)(sensor_tmp[0] & 0xff)) +((unsigned int) sensor_tmp[1] << 8);
     sensor[1] += ((unsigned int)(sensor_tmp[2] & 0xff)) +((unsigned int) sensor_tmp[3] << 8);
     sensor[2] += ((unsigned int)(sensor_tmp[4] & 0xff)) +((unsigned int) sensor_tmp[5] << 8);
     // pause
     for (delay = 0; delay < 100000; delay++){
  for (i=0; i<3; i++) { ground_sensor_zero[i]=(sensor[i]>>4);
     sprintf(tmp,"%d\t%d",i, ground_sensor_zero[i]);   
  for (delay = 0; delay < 100000; delay++){

How to fix I2C error on ground sensors

It's possible to use contemporaneously both the e-puck extension for gumstix overo and the ground sensor; in this case the camera and the ground sensor will share the same I2C bus and like the camera, also the ground sensor will not be anymore reachable from the e-puck side, but only from the overo side.

The I2C bus is configured to work at 400 KHz in the system running on the gumstix overo, thus we need to change the bus speed to 100 KHz in order to communicate with the ground sensor module; the simplest way is to set a kernel parameter in u-boot:

enter u-boot by pressing any key at boot start

type the following command to init the I2C bus 3 to 100 KHz:

   setenv i2cspeed 3,100 

type the following command to pass an additional parameter to the kernel that is the i2c_bus=${i2cspeed}:

   setenv mmcargs setenv bootargs console=${console} i2c_bus=${i2cspeed} ${optargs} mpurate=${mpurate} vram=${vram} omapfb.mode=dvi:${dvimode} omapdss.def_disp=${defaultdisplay} root=${mmcroot} rootfstype=${mmcrootfstype}

type the following command to save the environment changes


type the folloeing command to start booting


Pay attention that all the peripherals connected to the bus will now work at 100 KHz, thus also the e-puck camera and the LSM330 (accelerometer + gyroscope) present with e-puck HwRev 1.3. If you want to set back the bus speed to 400 KHz you need to follow steps 2-4, by setting the i2cspeed u-boot environment variable with setenv i2cspeed 3,400. Communicating with the ground sensor is as easy as open a device and reading from it; a source code example can be found in the following link You will find this application example in the /home/root/demos/gumstix-common directory; start it by executing ./i2c_groundsensors and the sensors values will be displayed in the terminal. Make sure that you have installed the last system update otherwise you'll encounter some problems. You can find more information about the I2C and gumstix in the following links: