Subject:
|
Re: Proximity sensor ( Was: Re: 8 power levels, and only 8?)
|
Newsgroups:
|
lugnet.robotics.rcx.java
|
Date:
|
Sat, 29 Sep 2001 21:34:43 GMT
|
Viewed:
|
3329 times
|
| |
| |
Well. Sixth time lucky maybe. I was trying to say this (it is part of leJOS
1.0.3.beta2):
package josx.platform.rcx;
/**
* A 'sensor' to detect object proximity. Sends a short
* message out of the infra-red port every so often and looks for
* the reflection using the light sensor (which happens to be
* very infra-red sensitive).
*
* @author Paul Andrews
*/
public class ProximitySensor implements SensorListener {
static final byte[] data = { -1 };
Sensor sensor;
int threshhold;
/**
* Constructor. Threshhold defaults to 15.
* @param sensor the sensor to which the light detector is connected.
* the constructor will initialize the sensor and set up the infrared
* transmitter.
*/
public ProximitySensor(Sensor sensor) {
this(sensor, 15);
}
/**
* Constructor.
* @param sensor the sensor to which the light detector is connected.
* the constructor will initialize the sensor and set up the infrared
* transmitter.
* @param threshhold the bigger the number the closer we get.
*/
public ProximitySensor(Sensor sensor, int threshhold) {
this.sensor = sensor;
sensor.setTypeAndMode(SensorConstants.SENSOR_TYPE_LIGHT,
SensorConstants.SENSOR_MODE_PCT);
sensor.activate();
Serial.setRangeLong();
this.threshhold = threshhold;
sensor.addSensorListener(this);
new Emitter().start();
}
/**
* Block the current thread until a near object is detected.
* A user could simply do their own sensor.wait() as that is
* all this method does.
*
* @param millis wait at most millis milliseconds. 0 means wait forever
* @throws InterruptedException if some thread calls interrupt() on the
* calling thread.
*/
public void waitTillNear(long millis) throws InterruptedException
{
synchronized (sensor) {
sensor.wait(millis);
}
}
/**
* Called from a thread private to sensor that runs at MAX_PRIORITY
* If newValue > oldValue by more than the threshhold, notify anything
* wait()ing on the sensor.
*/
public void stateChanged(Sensor sensor, int oldValue, int newValue) {
if (newValue - oldValue > threshhold)
synchronized(sensor) {
sensor.notifyAll();
}
}
/**
* A thread to continuously send out infrared pulses. Each
* pulse lasts about 40ms and the thread waits for 100ms between
* pulses. Thread runs at MAX_PRIORITY to guarantee that it runs.
*/
class Emitter extends Thread
{
public Emitter()
{
setDaemon(true);
setPriority(Thread.MAX_PRIORITY);
}
public void run()
{
while (true)
{
Serial.sendPacket(data, 0, 1);
try {
sleep(100);
} catch (InterruptedException ie) {
}
}
}
}
}
"Martin Egholm Nielsen" <martin@egholm-nielsen.dk> wrote in message
news:3BB5865E.5A7DDC45@egholm-nielsen.dk...
> > In lugnet.robotics.rcx.java, Martin Egholm Nielsen writes:
> Thats right Paul, but what are you saying? :o)
>
> Regards,
> Martin
>
> > > Hi Paul,
> > >
> > > > Here is an interesting paper on using NQC to control motor speed. A similar
> > > > technique could be used in leJOS:
> > > >
> > > > http://www.cs.ruu.nl/people/markov/lego/tutorial.pdf
> > > >
> > > > This paper has lots of interesing ideas in it besides motor speed control.
> > > > My favourite being the proximity sensor which I have remodelled in leJOS and
> > > > which continues to amaze me that it works at all.
> > > Hmmm... can you please post your code for that? I tried it
> > > this weekend (I found it here:
> > > http://www.mop.no/~simen/legoproxi.htm), but I couldn't
> > > quite get it to work.
> > > Did you activate the sensor?
> > >
> > > Regards,
> > > Martin
|
|
Message has 1 Reply:
Message is in Reply To:
12 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
This Message and its Replies on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|