View unanswered posts | View active topics It is currently Tue Jul 29, 2014 2:21 am






Reply to topic  [ 20 posts ]  Go to page 1, 2  Next
ultrasonic sensor single shot function needed 
Author Message
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post ultrasonic sensor single shot function needed
hi,
I want to attach 2 or 3 ultrasonic sensors to my robot which show into the same direction (just at different heights).
Now I need to avoid "ultrasonic noise pollution", and thus I want to get each US sensor value by a "single shot value request" - there must not be any US activity if I don't want it to.
So if I request a value like
Code:
SetSensor(S1, SensorSonar);
int distance=SensorValue(S1);

the specified US sensor has to fire just at this time, otherwise it has to be quiet.

How can this be done?

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Thu Sep 25, 2008 6:06 am
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: ultrasonic sensor single shot function needed
Helmut,

Have a look at this http://nxtasy.org/2007/02/28/ultra-soun ... ping-ping/

I know it's for NXT-G, but the principle is the same :) I know it's I2C, but you can do it! If you get stuck, let me know and I'll see what I can do.

Edit: I have mailed you the specs. I couldn't attach them here, the doc was too big.

Regards,
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]


Thu Sep 25, 2008 12:03 pm
Profile WWW
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: ultrasonic sensor single shot function needed
hi Xander,
yes it's just the I wanted it to be!
just the 2 sensor mode is the thing I need, although I don't need different distance echoes to be detected in the moment (but maybe some time?).
But I²C is nothing to me... :(
The other way I've been thinking about was to alternately initialize and de-initialize the sensor by
SensorType(S1, SensorSonar);
myvar=sensorvalue(S1);
SensorType(S1, SensorTouch); // which normally doesn't exist: just for disabling the USS
...
but the re-initialization is supposed to last too long for the next value call....

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Thu Sep 25, 2008 12:14 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: ultrasonic sensor single shot function needed
You would issue a "single shot command" by writing 1 to register 0x41 and that you would have to wait a couple of ms while the sonar does its thing. Then you read register 0x42 for the first measurement. I am not sure if there is a way to find out if the sensor is currently measuring. Perhaps Dick or someone else knows.

Regards,
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]


Thu Sep 25, 2008 12:36 pm
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: ultrasonic sensor single shot function needed
Have a look at this: [LINK]

_________________
| 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]


Thu Sep 25, 2008 12:39 pm
Profile WWW
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: ultrasonic sensor single shot function needed
you mean the following, I think:
Code:
void InitializeSonar(tSensors nPort) {
  static const byte kSonarInitialize[] = {3, 0x02, 0x41, 0x01};

  SensorType[nPort] = sensorI2CCustomStd9V;
  sendI2CMsg(nPort, kSonarInitialize[0], 0);
  wait10Msec(5);
}

void PingSonar(tSensors nPort) {
  static const byte kSonarPing[] = {3, 0x02, 0x41, 0x01};
  static const byte kSonarRead[] = {2, 0x02, 0x42};
  const int nSonarReplySize = 1;

  byte replyMsg[1];

  sendI2CMsg(nPort, kSonarPing[0], 0);
  wait1Msec(10); // wait 10ms, enough for echos to return to sensor.

  nI2CBytesReady[nPort] = 0; // Clear any pending bytes
  sendI2CMsg(nPort, kSonarRead[0], nSonarReplySize);
  while (nI2CStatus[nPort]==STAT_COMM_PENDING)
    wait1Msec(2); // Wait till I2C communication ends

  readI2CReply(nPort, replyMsg[0], nSonarReplySize);
  SensorValue[nPort] = replyMsg[0];
}

const tSensors kUS1 = S4;
const tSensors kUS2 = S3;

task main() {
   InitializeSonar(kUS1);
   InitializeSonar(kUS2);

   while (true)    {
    PingSonar(kUS1);
     PingSonar(kUS2);
    nxtDisplayTextLine(2, "US1: %d", SensorValue[kUS1]);
     nxtDisplayTextLine(7, "US2: %d", SensorValue[kUS2]);
     wait1Msec(50);
   }
   return;
}



I haven't already tried, but I will as soon as possible!
Many thanks!

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Thu Sep 25, 2008 12:46 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: ultrasonic sensor single shot function needed
Helmut,

What you could do, instead of having those 10ms waits in your main program, make a task that pings the sensors, one after the other and fill an array. That way you could just read that array from your main program and do other things.

Regards,
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]


Thu Sep 25, 2008 1:28 pm
Profile WWW
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: ultrasonic sensor single shot function needed
Xander,
I always do it just this way, and several tasks (for several behaviours) do this with several different sensors.
But for the USS, only 1 may fire at one time with enough time for the sonar sound to fade, before the other one starts to fire.
So the way shown above should be the best.

There's still another application I'm thinking about, for this I'll need 3-4 USS at 3-4 different nxt's: but also for this, there always may be 1 of them briefly "on duty" while all others must be silent - you surely can imagine what I'm thinking of...? ;-)


Thu Sep 25, 2008 1:49 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: ultrasonic sensor single shot function needed
Ford Prefect wrote:
Xander,
But for the USS, only 1 may fire at one time with enough time for the sonar sound to fade, before the other one starts to fire.

Basically fire one after the other. You can actually tell the sensors to also check if there is another one running somewhere :) Read the PDF for more info on that, hehe.
Quote:
There's still another application I'm thinking about, for this I'll need 3-4 USS at 3-4 different nxt's: but also for this, there always may be 1 of them briefly "on duty" while all others must be silent - you surely can imagine what I'm thinking of...? ;-)

I am not sure, can you elaborate?

Regards,
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]


Thu Sep 25, 2008 2:02 pm
Profile WWW
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: ultrasonic sensor single shot function needed
the idea still is not elaborated enough to be published :(
but back to the single shot USS:

the best way was to have a SensorSonarPing to work all with the standard RobotC syntax, just like

SensorType(S1, SensorSonarPing): // maybe together with your MightyDistanceBoard ;-)
SensorType(S2, SensorSonarPing):
myVar=SensorValue (S1);

Could this be done?

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Thu Sep 25, 2008 2:27 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: ultrasonic sensor single shot function needed
The problem with the single shot mode is that it takes MUCH longer for the data to be gathered by the sensor than it does for an I2C reply to be sent back to the NXT. That is why there's the continuous mode. You just get the last completed reading when you query that register. I don't think it is desirable to have a function that takes 10-15ms to return (with the exception for wait and all those). If you do the maths (and I know you have, you're a mathematician), if the object is at max distance, say 2m55, that means that it will take no less than 15ms to reach that target, assuming an air speed of 340m/s (that's 474 754 227 fathoms per lunar month). The "ping" command does not return the value of the shortest distance found, an additional read command does. So it's two commands with a 15 ms wait between them, not very nice and with 4 sensors, you could only get 14-16 readings per second per sensor, at best.

I think this is something you'll have to implement yourself, but I am sure you will have no problems :)

Regards,
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]


Thu Sep 25, 2008 2:55 pm
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3164
Location: Rotterdam, The Netherlands
Post Re: ultrasonic sensor single shot function needed
I was just thinking. How about having a single active pinging sonar and 3 or more passive ones listening? I am not sure what the "Event capture command" does on the Sonar, but it sounds a lot like it sort of just listens:
Quote:
Within this mode the sensor will measure whether any other ultrasonic sensors are within the vicinity. With this information a program can evaluate when it is best to make a new measurement which will not conflict with other ultrasonic sensors.

It would be fun to experiment with that command :)

Regards,
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]


Thu Sep 25, 2008 3:01 pm
Profile WWW
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: ultrasonic sensor single shot function needed
Xander,
1st, I am no mathematician. I'm just a journalist from the vicinity of Beteigeuze.
2nd, for the 2 USS ping mode, the example above shows a good way to do it
3rd, for my not already elaborated 4-USS-4-nxt-application 4 readings in 1 second will do
4th, just for reasons of simplifications and unification it could be fine to have a sensortype SensorSonarPing,
5th, above this, it also was fine to have a mode both "sonarFire" and "SonarListen" for objects which are at a greater distance than 255 cm, and a time value of up to 500-1000 msec may be returned as a sensor value:

void(S1, SonarFire);
detectTime=SensorValue(S1, SonarListen); // in msec
wait1mSec (1000);
distance=340*detectTime/2; (for sonar sound there and back)

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Thu Sep 25, 2008 3:15 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: ultrasonic sensor single shot function needed
ahh - here you've been 1 second faster than me with the reply.
You nearly got it, you're quite close to my idea - that's the next step:

the 2nd sends, the 1st one listens
the 3rd one sends, the 1st listens again
the 4th one sends, again the 1st listens.
- - pause - -
the 2nd sends, the 1st listens
the 3rd one sends, the 1st listens again
the 4th one sends, again the 1st listens.
- - pause - -

all are triggered by bluetiooth signals from the 1st nxt.
What can be done by this? ;-)

But let's start with the 1st step: an ultrasonic sensor single shot function...!

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Thu Sep 25, 2008 3:22 pm
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 614
Post Re: ultrasonic sensor single shot function needed
I would first try two ultrasonic sensors without any special code and see if you're actually getting interference. I've seen some previous posts from end users elsewhere (nxtasy.org?) that imply it works fine.

Also be cautious of the undocumented feature of the ultrasonic sensor that it doesn't like to be polled to fast. It appears that if you happen to read it while it is in the middle of doing a sonar "ping" it aborts the ping in process and returns a distance that is the same as if the ping had returned at the time the read was issued.


Sat Sep 27, 2008 2:43 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 20 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users and 2 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.