View unanswered posts | View active topics It is currently Tue Sep 30, 2014 3:00 pm






Reply to topic  [ 5 posts ] 
distance -35 with DIST-Nx-v3 
Author Message
Rookie
User avatar

Joined: Sun Nov 11, 2012 4:26 am
Posts: 8
Location: France
Post 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);
  }
}

_________________
ROBOTC 3.61.1

French guy - I Love ROCK and ROBOTS


Sat Jan 12, 2013 10:19 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post 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);
  }
}

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Jan 13, 2013 3:54 am
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post 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);
  }
}

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Jan 13, 2013 3:58 am
Profile WWW
Rookie
User avatar

Joined: Sun Nov 11, 2012 4:26 am
Posts: 8
Location: France
Post 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

_________________
ROBOTC 3.61.1

French guy - I Love ROCK and ROBOTS


Sun Jan 13, 2013 7:57 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3223
Location: Rotterdam, The Netherlands
Post Re: distance -35 with DIST-Nx-v3
I am glad to hear it!

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Jan 13, 2013 8:46 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.