ROBOTC.net forums
http://www.robotc.net/forums/

distance -35 with DIST-Nx-v3
http://www.robotc.net/forums/viewtopic.php?f=41&t=5351
Page 1 of 1

Author:  robotcexperiences [ Sat Jan 12, 2013 10:19 am ]
Post subject:  distance -35 with DIST-Nx-v3

Hye,

I've bought a the MindSensor DIST-Nx-v3. The using program is attached to this post.
Sometimes it will be works, but Sometimes the result is only - 35. So I need to power off the brick and restart it.


I use Robotc 3.55 or 3.54 and the firmware is the standard file NXT_0958.rfw

Can you help me ?

Excuse me for my bad english, I'am french ...

Code:
//
// mesure de distance simple
//

#pragma config(Sensor, S2,     TOR_US,         sensorTouch)
#pragma config(Sensor, S4,     COLOR,          sensorCOLORRED)
#pragma config(Motor,  motorA,          MOT_DRT,       tmotorNXT, PIDControl, encoder)
#pragma config(Motor,  motorB,          MOT_DIST,      tmotorNXT, PIDControl, encoder)
#pragma config(Motor,  motorC,          MOT_GHE,       tmotorNXT, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//


const int PORT = S1;           // Capteur distance sur le port S1
void Cde_CapteurIR(bool vCde){
TI2CStatus nStatus;
sbyte I2C_envoie[4];
    I2C_envoie[0] = 3;// nombre de byte a envoyer
   I2C_envoie[1] = 0x02; // adresse capteur
   I2C_envoie[2] = 0x41;// commande en ecriture
   if(vCde)I2C_envoie[3] = 0x45;// commande de mise en marche du capteur
   else I2C_envoie[3] = 0x44;
   while (nI2CStatus[PORT] == STAT_COMM_PENDING)
   {}
   // Attente Bus libre
   nStatus = nI2CStatus[PORT];  // Test l'etat du capteur
   if(nStatus == NO_ERR){        // Si l'etat est OK
      sendI2CMsg (PORT,&I2C_envoie[0],0); // On envoie une ligne de code
      wait1Msec(10);
    }
}

int Mesure_Distance(){
TI2CStatus nStatus;
sbyte I2C_envoie[3];
sbyte I2C_recoit[2];
int Distance;

  I2C_envoie[0] = 2;// nombre de byte a envoyer
  I2C_envoie[1] = 0x02; // adresse capteur
  I2C_envoie[2] = 0x42;// commande en lecture

  nStatus = nI2CStatus[PORT];  // Test l'etat du capteur
  while (nI2CStatus[PORT] == STAT_COMM_PENDING){
  }
  // Attente Bus libre
  if(nStatus == NO_ERR)        // Si l'etat est OK
  {
     sendI2CMsg (PORT,&I2C_envoie[0],2); // On envoie une ligne de code et on attend 2 bytes en retour
     wait1Msec(20);
     readI2CReply (S1,&I2C_recoit[0],2);
     Distance = ( 0x00FF & I2C_recoit[0] );
    Distance += ( (0x00FF & I2C_recoit[1]) <<8 );
    while (nI2CStatus[PORT] == STAT_COMM_PENDING){
  // Attente Bus libre
    }

  }
  return Distance;
}

task main(){
   SensorType(PORT)= sensorI2CCustom9V;
   Cde_CapteurIR(true);
   wait10Msec (5);
  while(true){
     nxtDisplayCenteredTextLine (0,"%3d",  Mesure_Distance());
     wait1Msec (15);
  }
}

Author:  mightor [ Sun Jan 13, 2013 3:54 am ]
Post subject:  Re: distance -35 with DIST-Nx-v3

Perhaps you should consider using my driver suite (link below in my signature) and have a look at the mindsensors-irdist* files. You should not really be using 3.54 if you want to retain your sanity, stick to 3.55B2.
Anyway, I have modified your code and pasted it below. Some notes:
  • Do not use tight while loops (with nothing in them), use EndTimeSlice() instead.
  • Make sure you use unsigned bytes (ubytes), not sbytes when reading from your sensors
  • Use writeDebugStreamLine() in your code to see where things go wrong
  • Do not put text above the #pragma lines, it messes with the sensor and motor configuration tool
  • Unless there is no other way, avoid using SetSensorType to configure a sensor as I2C, it's better (and easier) to use the sensor and motor configuration tool, especially if you're already using it for other sensors.
  • Write your code with expectation of sharing it, that means use English variable and function names and comments. It's OK to add both English and whatever your native tongue happens to be (French in your case). Fortunately, my French is OK, so I was able to understand most of, but not everyone has that ability.
  • Take a look at the ROBOTC Driver Suite, it will save you a LOT of work.

Hope this helps! The code below was tested with a Dist-NX V2 (functionally the same as the V3) on ROBOTC 3.55 B2.

Code:
#pragma config(Sensor, S1,     DISTNX,         sensorI2CCustom9V)
#pragma config(Sensor, S2,     TOR_US,         sensorTouch)
#pragma config(Sensor, S4,     COLOR,          sensorCOLORRED)
#pragma config(Motor,  motorA,          MOT_DRT,       tmotorNXT, PIDControl, encoder)
#pragma config(Motor,  motorB,          MOT_DIST,      tmotorNXT, PIDControl, encoder)
#pragma config(Motor,  motorC,          MOT_GHE,       tmotorNXT, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//


void Cde_CapteurIR(bool vCde)
{
  ubyte I2C_envoie[4];
  I2C_envoie[0] = 3;// nombre de byte a envoyer
  I2C_envoie[1] = 0x02; // adresse capteur
  I2C_envoie[2] = 0x41;// commande en ecriture

  // commande de mise en marche du capteur (or not)
  I2C_envoie[3] = (vCde) ? 0x45 : 0x44;


  while (nI2CStatus[DISTNX] == STAT_COMM_PENDING)
    EndTimeSlice();

  if(nI2CStatus[DISTNX] == NO_ERR)
  {        // Si l'etat est OK
    sendI2CMsg (DISTNX,&I2C_envoie[0], 0); // On envoie une ligne de code
    wait1Msec(10);
  }
  else
  {
    PlaySound(soundException);
    writeDebugStreamLine("ERROR: Cde_CapteurIR!");
    while(bSoundActive)
      EndTimeSlice();
  }
}

int Mesure_Distance(){
  ubyte I2C_envoie[3];
  ubyte I2C_recoit[2];
  int Distance = -1;

  I2C_envoie[0] = 2;// nombre de byte a envoyer
  I2C_envoie[1] = 0x02; // adresse capteur
  I2C_envoie[2] = 0x42;// commande en lecture

  while (nI2CStatus[DISTNX] == STAT_COMM_PENDING)
    EndTimeSlice();

  // Attente Bus libre
  if(nI2CStatus[DISTNX] == NO_ERR)        // Si l'etat est OK
  {
    sendI2CMsg (DISTNX, &I2C_envoie[0], 2); // On envoie une ligne de code et on attend 2 bytes en retour

    // Wait for the message to be sent
    while (nI2CStatus[DISTNX] == STAT_COMM_PENDING)
      EndTimeSlice();

    // Make sure no error occurred when we sent that message
    if(nI2CStatus[DISTNX] != NO_ERR)
    {
      PlaySound(soundException);
      writeDebugStreamLine("ERROR: Mesure_Distance!");
      while(bSoundActive)
        EndTimeSlice();
      return -1;
    }

    readI2CReply (S1, &I2C_recoit[0], 2);
    Distance = I2C_recoit[0] + (I2C_recoit[1] << 8);
  }
  else
  {
    PlaySound(soundException);
    writeDebugStreamLine("ERROR: Mesure_Distance!");
    while(bSoundActive)
      EndTimeSlice();
  }
  return Distance;
}

task main(){
  Cde_CapteurIR(true);
  wait1Msec(50);  // it is much easier to see that you want to wait
                  // 50 ms when you use that number, rather than the 10ms wait call
  while(true){
    nxtDisplayCenteredTextLine (0,"%3d",  Mesure_Distance());
    wait1Msec (15);
  }
}

Author:  mightor [ Sun Jan 13, 2013 3:58 am ]
Post subject:  Re: distance -35 with DIST-Nx-v3

Using the driver suite, your program would've looked like this:
Code:
#pragma config(Sensor, S1,     DISTNX,         sensorI2CCustom9V)
#pragma config(Sensor, S2,     TOR_US,         sensorTouch)
#pragma config(Sensor, S4,     COLOR,          sensorCOLORRED)
#pragma config(Motor,  motorA,          MOT_DRT,       tmotorNXT, PIDControl, encoder)
#pragma config(Motor,  motorB,          MOT_DIST,      tmotorNXT, PIDControl, encoder)
#pragma config(Motor,  motorC,          MOT_GHE,       tmotorNXT, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

#include "drivers/mindsensors-irdist.h"


task main(){
  while(true){
    nxtDisplayCenteredTextLine (0,"%3d",  MSDISTreadDist(DISTNX));
    wait1Msec (15);
  }
}

Author:  robotcexperiences [ Sun Jan 13, 2013 7:57 am ]
Post subject:  Re: distance -35 with DIST-Nx-v3

Hye Xander,

I've found yesterday your driver and i use it to change my program. Everything is all right now.

Thank you, very much :)

Best regards

Author:  mightor [ Sun Jan 13, 2013 8:46 am ]
Post subject:  Re: distance -35 with DIST-Nx-v3

I am glad to hear it!

= Xander

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/