you're reading...

Using filters with the sensor framework

leJOS 0.5.0-alpha for the EV3 introduces the sensor framework. The framework focusses on the quality of the data coming from sensors and on the ease of use of sensors. The elements of the framework are described in detail in the wiki. In this post I will discuss filters.

Filters are objects that alter a sample (that is how a measurement is called in LeJOS) in one way or another. They can be used to enhance the quality of a sample. Lejos comes with a set of predefined filters. You can also write  your own filter. Lejos currently has filters that can be used to reduce the noise level in a stream of samples, to integrate a sample, like converting acceleration to speed, to calibrate uncalibrated sensors or to remove offset errors to name a few.

Let us take a look at the way filters are used in code. The code below shows how both a  (unspecified) sensor and a filter instance are constructed. Note that the filter takes the sensor instance as input in its constructor. The last two lines create an array and retrieve a sample. The array should be created only once, a sample should be fetched every time you need a fresh sample.

SampleProvider mySensor = new AnySensor(port);
SampleProvider myFilter = new AnyFilter(mySensor);
float[] sample = new float[myFilter.sampleSize()];
myFilter.fetchSample(sample, 0);

Only one line of code is all it takes to add a filter!
As you can see the sample is not fetched from the sensor but from the filter. The filter passes the request on to the sensor to get a fresh sample. It then takes the opportunity to modify the sample according to its function before returning the sample to the caller. This mechanism makes it possible to stack filters. A second filter can thus process a sample that was just altered by another filter. The example below shows how to add a second filter.

SampleProvider mySensor = new AnySensor(port);
SampleProvider myFilter = new AnyFilter(mySensor);
SampleProvider mySecondFilter = new AnyFilter(myFilter);
float[] sample = new float[mySecondFilter.sampleSize()];
mySecondFilter.fetchSample(sample, 0);

Notice that you only have to worry about the filters once, at the time you create an instance of the sensor. After that you only have to make sure that you get a fresh sample when you need one, the filtering is done automaticly. This is a very poweful feature as it allows you to add a filter to an existing program without having to go through all off your code.

It is possible to write your own filter. As a matter of fact, this is rather easy. The samples project that comes with LeJOS has a sample that demonstrates how to write a filter that automatically calibrates a light or color

I’m looking forward to filters coming from the community.



No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

About leJOS News

leJOS News keeps you up-to-date with leJOS. It features latest news, explains cool features, shows advanced techniques and highlights amazing projects. Be sure to subscribe to leJOS News and never miss an article again. Best of all, subscription is free!
Follow leJOS News on WordPress.com
%d bloggers like this: