Get The Time Difference between two SoundSensor

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

Get The Time Difference between two SoundSensor

Postby maxcost » Fri Apr 11, 2014 4:00 pm

Hello i build a robot with 2 SoundSensors (left and right) and i want to know the difference of sound wave arival time between first sensor and second sensor.
I've tried using Java System.nanoTime() and multithreading,

Code: Select all
package soundfollower;
import lejos.nxt.*;
public class SoundThread extends Thread{
   private SoundSensor s;
   private long time;
   
   
   public SoundThread(SoundSensor s){
      this.s = s;
      time = 0;
      
   }

   public long getTime(){
      return this.time;
   }
   
   @Override
   public void run() {
      // TODO Auto-generated method stub
      while(time==0){
         if(s.readValue() > 8){
            time = System.nanoTime();
            
         }
      }   
   }
}


Code: Select all
package soundfollower;
import lejos.nxt.*;
public class SoundTracker {

   /**
    * @param args
    * @throws InterruptedException
    */
   public static void main(String[] args) throws InterruptedException {
      
      // TODO Auto-generated method stub
      SoundSensor s1 = new SoundSensor(SensorPort.S1,true);
      SoundSensor s2 = new SoundSensor(SensorPort.S2,true);
      
      SoundThread left = new SoundThread(s1);
      SoundThread right = new SoundThread(s2);
      
      left.start();
      right.start();
      while(!Button.ESCAPE.isDown()){
         LCD.drawString(""+left.getTime()+"\n"+right.getTime(),0,0);
         Thread.sleep(100);
         LCD.clear();
      }   
   }
}


but..
Why the time from left sensor always smaller than the right. Even when the sound come from the right side of the robot, where the right sensor is closer to sound source than the left one.

Sorry for my bad english anyway
maxcost
New User
 
Posts: 4
Joined: Fri Apr 11, 2014 3:45 pm

Re: Get The Time Difference between two SoundSensor

Postby gloomyandy » Fri Apr 11, 2014 5:14 pm

Won't work, for two reasons...
1) Although you have two threads you only have one cpu. That cpu is being shared between the two threads by lejos. It does this by running each thread in turn until either the thread gives up the cpu, or until one thread has been running for at least 2mS.
2) The analogue NXT sensors (like the sound sensor) is handled by a second processor in the NXT. It communicates the readings from all four sensor ports every 3mS.

This means that you will need a time difference of at least 3mS between the two sensors (so approx 1m separation), before you will see any reliable difference in the time values (because any period less than this will likely be reported in the same reading from the second processor). Then you will have the uncertainty introduced by the time slicing used by the leJOS scheduler. My best estimate is that you would need to have a difference of around 10mS before you will get any sort of reliable readings.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3899
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Get The Time Difference between two SoundSensor

Postby maxcost » Fri Apr 11, 2014 7:52 pm

gloomyandy wrote:Won't work, for two reasons...
1) Although you have two threads you only have one cpu. That cpu is being shared between the two threads by lejos. It does this by running each thread in turn until either the thread gives up the cpu, or until one thread has been running for at least 2mS.
2) The analogue NXT sensors (like the sound sensor) is handled by a second processor in the NXT. It communicates the readings from all four sensor ports every 3mS.

This means that you will need a time difference of at least 3mS between the two sensors (so approx 1m separation), before you will see any reliable difference in the time values (because any period less than this will likely be reported in the same reading from the second processor). Then you will have the uncertainty introduced by the time slicing used by the leJOS scheduler. My best estimate is that you would need to have a difference of around 10mS before you will get any sort of reliable readings.


Thanks for the answer :)

is that mean the distance between sound sensor must be at least 3~4m ?

can i do something, so the NXT will read the sensor every less than 3mS ?
maxcost
New User
 
Posts: 4
Joined: Fri Apr 11, 2014 3:45 pm

Re: Get The Time Difference between two SoundSensor

Postby gloomyandy » Fri Apr 11, 2014 8:17 pm

Nope the 3mS interval is pretty much baked in.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3899
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Get The Time Difference between two SoundSensor

Postby maxcost » Fri Apr 11, 2014 8:23 pm

gloomyandy wrote:Nope the 3mS interval is pretty much baked in.


last question
so its (the 3ms interval) part of lejos firmware,isnt it?
its not related to the nxt's hardware
?
maxcost
New User
 
Posts: 4
Joined: Fri Apr 11, 2014 3:45 pm

Re: Get The Time Difference between two SoundSensor

Postby gloomyandy » Fri Apr 11, 2014 9:29 pm

It is both, the firmware talks to another processor (this ATMega processor handles the analog hardware and motor control). This communication takes place every 3mS, you can change the leJOS side of things but not the ATMega firmware, it is the ATMega firmware that in effect imposes the sample rate of the sensor.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3899
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Get The Time Difference between two SoundSensor

Postby maxcost » Sat Apr 12, 2014 3:45 am

Thanks for the help :)
maxcost
New User
 
Posts: 4
Joined: Fri Apr 11, 2014 3:45 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 4 guests

more stuff