//
you're reading...
How-to's

Publishing and subscribing to samples

publish

A new feature at leJOS 0.9.0 is publishing and subscribing to sample providers.

Any EV3 program that reads sensor data can be very easily modified to use a new PublishFilter sample filter.

This causes availability of the sensor data to be published across the network.

Any PC, Android or EV3 program can then query what sensor data has been published, and connect to the published sensor stream as a subscribed sensor provider.  The publisher specifies the frequency of  the samples so that the subscriber knows how frequently to read them. This does not need to be exact.

There is a sample program to show how to publish samples. It publishes samples for the EV3 IR, Ultrasonic and Gyro sensors:

package org.lejos.ev3.sample.publish;

import java.io.IOException;

import lejos.hardware.Button;
import lejos.hardware.lcd.LCD;
import lejos.hardware.port.SensorPort;
import lejos.hardware.sensor.EV3GyroSensor;
import lejos.hardware.sensor.EV3IRSensor;
import lejos.hardware.sensor.EV3UltrasonicSensor;

import lejos.robotics.SampleProvider;
import lejos.robotics.filter.PublishFilter;
import lejos.utility.Delay;

public class Publish {
	public static void main(String[] args) throws IOException {
		float frequency = 1; // 1 sample per second
		EV3IRSensor irSensor = new EV3IRSensor(SensorPort.S1);
		SampleProvider sp = new PublishFilter(irSensor.getDistanceMode(), "IR range readings", frequency);
		float[] sample = new float[sp.sampleSize()];

		EV3UltrasonicSensor sonicSensor = new EV3UltrasonicSensor(SensorPort.S2);
		SampleProvider sp2 = new PublishFilter(sonicSensor.getDistanceMode(), "Ultrasonic readings", frequency);
		float[] sample2 = new float[sp2.sampleSize()];

		EV3GyroSensor gyroSensor = new EV3GyroSensor(SensorPort.S3);
		SampleProvider sp3 = new PublishFilter(gyroSensor.getRateMode(), "Gyro readings", frequency);
		float[] sample3 = new float[sp3.sampleSize()];

		while(Button.ESCAPE.isUp()) {
			sp.fetchSample(sample, 0);
			LCD.clear(3);
			LCD.drawString("IR: " + sample[0],0,3);
			sp2.fetchSample(sample2, 0);
			LCD.clear(4);
			LCD.drawString("Sonic: " + sample2[0],0,4);
			sp3.fetchSample(sample3, 0);
			LCD.clear(5);
			LCD.drawString("Gyro: " + sample3[0],0,5);
			Delay.msDelay((long) (1000/frequency));
		}

		irSensor.close();
		sonicSensor.close();
		gyroSensor.close();
	}
}

A simple program to subscribe to one of the sources and display the data:

import java.io.IOException;
import java.util.Collection;

import lejos.robotics.filter.PublishedSource;
import lejos.robotics.filter.SubscribedProvider;
import lejos.utility.Delay;

public class Subscriber {
	public static void main(String[] args) {
		Collection sources = PublishedSource.getSources();;

		System.out.println("Number of sources is " + sources.size());

		for(PublishedSource source: sources) {
			System.out.println("IP address\tHost\tPort\tName\t\tSize\tFrequency\tLast message");
			System.out.println(source.getIpAddress() + "\t" + source.getHost() + "\t" +
			                   source.getPort() + "\t" + source.getName() + "\t" + source.sampleSize() + "\t" +
					            source.getFrequency() + "\t\t" + source.getTime());
		}

		for(PublishedSource source: sources) {
			if (source.getName().equals("IR range readings")) {
				float[] sample = new float;

				SubscribedProvider provider = null;
				try {
					provider = source.connect();

					while(provider.isActive()) {
						provider.fetchSample(sample, 0);
						System.out.println(provider.getName() + " from " + provider.getHost() + " at " + provider.getTime() +  " is " + sample[0]);
						Delay.msDelay((long) (1000/source.getFrequency()));
					}
				} catch (IOException e) {
					e.printStackTrace();
				} finally  {
					System.out.println("Closing the provider");
					try {
						if (source != null) source.close();
					} catch (IOException e) {
						// Ignore
					}
				}
			}
		}
	}
}
Advertisements

Discussion

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

You are commenting using your Google+ 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: