You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@edgent.apache.org by Felipe Gutierrez <fe...@gmail.com> on 2018/11/21 14:57:42 UTC
How to filter on Apache Edgent and also show the values which were filtered?
Hi,
This question is about an application that I am using Apache Edgent, not
really a development effort into Apache Edgent. But as there is no user@list
I am sensding here.
I am using Apache Edgent (Java framework) to poll values from an HCSR04
ultrasonic sensor on a Raspberry Pi every 3 seconds. I use a filter to not
get values from 50cm to 80cm (
https://github.com/felipegutierrez/explore-rpi/blob/master/src/main/java/org/sense/edgent/app/UltrasonicEdgentApp.java
).
UltrasonicStream sensor = new UltrasonicStream();
DirectProvider dp = new DirectProvider();
Topology topology = dp.newTopology();
TStream<Double> tempReadings = topology.poll(sensor, 3, TimeUnit.SECONDS);
TStream<Double> filteredReadings = tempReadings.filter(reading -> reading <
50 || reading > 80);
System.out.println("filter added: tempReadings.filter(reading -> reading <
50 || reading > 80);");
filteredReadings.print();
dp.submit(topology);
I want to show some message when the values are filtered. When the values
do not match with my filter I can poll them, but when they match I am not
returning, that is ok. However, I want just to show that a value was
filtered using Apache Edgent libraries. I know that I can do something on
the `public double get()` method, but I wonder if I could do this trick
with some method of the Apache Edgent (
https://github.com/felipegutierrez/explore-rpi/blob/master/src/main/java/org/sense/sensor/UltrasonicStream.java
).
public class UltrasonicStream implements Supplier<Double> {
private static final long serialVersionUID = -6511218542753341056L;
private static GpioPinDigitalOutput sensorTriggerPin;
private static GpioPinDigitalInput sensorEchoPin;
private static final GpioController gpio = GpioFactory.getInstance();
private double currentDistance = -1.0;
/**
* The HCSR04 Ultrasonic sensor is connected on the physical pin 16 and 18
which
* correspond to the GPIO 04 and 05 of the WiringPi library.
*/
public UltrasonicStream() {
// Trigger pin as OUTPUT
sensorTriggerPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04);
// Echo pin as INPUT
sensorEchoPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_05,
PinPullResistance.PULL_DOWN);
}
/**
* This is the override method of the Supplier interface from Apache Edgent
*/
@Override
public Double get() {
try {
System.out.print("Distance in centimeters: ");
currentDistance = getDistance();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return currentDistance;
}
/**
* Retrieve the distance measured by the HCSR04 Ultrasonic sensor connected
on a
* Raspberry Pi 3+B
*
* @return the distance in centimeters
* @throws InterruptedException
*/
public double getDistance() throws InterruptedException {
double distanceCM = -1;
try {
// Thread.sleep(2000);
sensorTriggerPin.high(); // Make trigger pin HIGH
Thread.sleep((long) 0.01);// Delay for 10 microseconds
sensorTriggerPin.low(); // Make trigger pin LOW
// Wait until the ECHO pin gets HIGH
while (sensorEchoPin.isLow()) {
}
// Store the current time to calculate ECHO pin HIGH time.
long startTime = System.nanoTime();
// Wait until the ECHO pin gets LOW
while (sensorEchoPin.isHigh()) {
}
// Store the echo pin HIGH end time to calculate ECHO pin HIGH time.
long endTime = System.nanoTime();
distanceCM = ((((endTime - startTime) / 1e3) / 2) / 29.1);
// Printing out the distance in centimeters
// System.out.println("Distance: " + distanceCM + " centimeters");
return distanceCM;
} catch (InterruptedException e) {
e.printStackTrace();
}
return distanceCM;
}
}
Thanks,
Felipe
*--*
*-- Felipe Gutierrez*
*-- skype: felipe.o.gutierrez*
*--* *https://felipeogutierrez.blogspot.com
<https://felipeogutierrez.blogspot.com>*
Re: How to filter on Apache Edgent and also show the values which
were filtered?
Posted by Felipe Gutierrez <fe...@gmail.com>.
😀 thanks . it fit like a glove
*--*
*-- Felipe Gutierrez*
*-- skype: felipe.o.gutierrez*
*--* *https://felipeogutierrez.blogspot.com
<https://felipeogutierrez.blogspot.com>*
On Wed, Nov 21, 2018 at 6:14 PM Christofer Dutz <ch...@c-ware.de>
wrote:
> Hi Felipe,
>
> us not having a user list is intentional, so this is absolutely the right
> place to come and ask questions.
>
> As the filter itself is super-trivial, I think it might be the simplest
> solution to implement the filter itself:
>
> TStream<Double> filteredReadings = tempReadings.filter(reading
> -> {
> System.out.println(String.format("The value was %s",
> reading));
> return reading < 50 || reading > 80;
> });
>
> This should do the trick ...
>
> Hope this helps,
> Chris
>
>
>
> Am 21.11.18, 15:59 schrieb "Felipe Gutierrez" <
> felipe.o.gutierrez@gmail.com>:
>
> Hi,
>
> This question is about an application that I am using Apache Edgent,
> not
> really a development effort into Apache Edgent. But as there is no
> user@list
> I am sensding here.
>
> I am using Apache Edgent (Java framework) to poll values from an
> HCSR04
> ultrasonic sensor on a Raspberry Pi every 3 seconds. I use a filter to
> not
> get values from 50cm to 80cm (
>
> https://github.com/felipegutierrez/explore-rpi/blob/master/src/main/java/org/sense/edgent/app/UltrasonicEdgentApp.java
> ).
>
> UltrasonicStream sensor = new UltrasonicStream();
> DirectProvider dp = new DirectProvider();
> Topology topology = dp.newTopology();
> TStream<Double> tempReadings = topology.poll(sensor, 3,
> TimeUnit.SECONDS);
> TStream<Double> filteredReadings = tempReadings.filter(reading ->
> reading <
> 50 || reading > 80);
> System.out.println("filter added: tempReadings.filter(reading ->
> reading <
> 50 || reading > 80);");
> filteredReadings.print();
> dp.submit(topology);
>
> I want to show some message when the values are filtered. When the
> values
> do not match with my filter I can poll them, but when they match I am
> not
> returning, that is ok. However, I want just to show that a value was
> filtered using Apache Edgent libraries. I know that I can do something
> on
> the `public double get()` method, but I wonder if I could do this trick
> with some method of the Apache Edgent (
>
> https://github.com/felipegutierrez/explore-rpi/blob/master/src/main/java/org/sense/sensor/UltrasonicStream.java
> ).
>
> public class UltrasonicStream implements Supplier<Double> {
>
> private static final long serialVersionUID = -6511218542753341056L;
>
> private static GpioPinDigitalOutput sensorTriggerPin;
> private static GpioPinDigitalInput sensorEchoPin;
> private static final GpioController gpio = GpioFactory.getInstance();
> private double currentDistance = -1.0;
>
> /**
> * The HCSR04 Ultrasonic sensor is connected on the physical pin 16 and
> 18
> which
> * correspond to the GPIO 04 and 05 of the WiringPi library.
> */
> public UltrasonicStream() {
> // Trigger pin as OUTPUT
> sensorTriggerPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04);
> // Echo pin as INPUT
> sensorEchoPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_05,
> PinPullResistance.PULL_DOWN);
> }
>
> /**
> * This is the override method of the Supplier interface from Apache
> Edgent
> */
> @Override
> public Double get() {
> try {
> System.out.print("Distance in centimeters: ");
> currentDistance = getDistance();
> } catch (InterruptedException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> return currentDistance;
> }
>
> /**
> * Retrieve the distance measured by the HCSR04 Ultrasonic sensor
> connected
> on a
> * Raspberry Pi 3+B
> *
> * @return the distance in centimeters
> * @throws InterruptedException
> */
> public double getDistance() throws InterruptedException {
>
> double distanceCM = -1;
> try {
> // Thread.sleep(2000);
> sensorTriggerPin.high(); // Make trigger pin HIGH
> Thread.sleep((long) 0.01);// Delay for 10 microseconds
> sensorTriggerPin.low(); // Make trigger pin LOW
>
> // Wait until the ECHO pin gets HIGH
> while (sensorEchoPin.isLow()) {
>
> }
> // Store the current time to calculate ECHO pin HIGH time.
> long startTime = System.nanoTime();
> // Wait until the ECHO pin gets LOW
> while (sensorEchoPin.isHigh()) {
>
> }
> // Store the echo pin HIGH end time to calculate ECHO pin HIGH time.
> long endTime = System.nanoTime();
>
> distanceCM = ((((endTime - startTime) / 1e3) / 2) / 29.1);
> // Printing out the distance in centimeters
> // System.out.println("Distance: " + distanceCM + " centimeters");
>
> return distanceCM;
> } catch (InterruptedException e) {
> e.printStackTrace();
> }
> return distanceCM;
> }
> }
>
> Thanks,
> Felipe
>
> *--*
> *-- Felipe Gutierrez*
>
> *-- skype: felipe.o.gutierrez*
> *--* *https://felipeogutierrez.blogspot.com
> <https://felipeogutierrez.blogspot.com>*
>
>
>
AW: How to filter on Apache Edgent and also show the values which
were filtered?
Posted by Christofer Dutz <ch...@c-ware.de>.
Hi Felipe,
us not having a user list is intentional, so this is absolutely the right place to come and ask questions.
As the filter itself is super-trivial, I think it might be the simplest solution to implement the filter itself:
TStream<Double> filteredReadings = tempReadings.filter(reading -> {
System.out.println(String.format("The value was %s", reading));
return reading < 50 || reading > 80;
});
This should do the trick ...
Hope this helps,
Chris
Am 21.11.18, 15:59 schrieb "Felipe Gutierrez" <fe...@gmail.com>:
Hi,
This question is about an application that I am using Apache Edgent, not
really a development effort into Apache Edgent. But as there is no user@list
I am sensding here.
I am using Apache Edgent (Java framework) to poll values from an HCSR04
ultrasonic sensor on a Raspberry Pi every 3 seconds. I use a filter to not
get values from 50cm to 80cm (
https://github.com/felipegutierrez/explore-rpi/blob/master/src/main/java/org/sense/edgent/app/UltrasonicEdgentApp.java
).
UltrasonicStream sensor = new UltrasonicStream();
DirectProvider dp = new DirectProvider();
Topology topology = dp.newTopology();
TStream<Double> tempReadings = topology.poll(sensor, 3, TimeUnit.SECONDS);
TStream<Double> filteredReadings = tempReadings.filter(reading -> reading <
50 || reading > 80);
System.out.println("filter added: tempReadings.filter(reading -> reading <
50 || reading > 80);");
filteredReadings.print();
dp.submit(topology);
I want to show some message when the values are filtered. When the values
do not match with my filter I can poll them, but when they match I am not
returning, that is ok. However, I want just to show that a value was
filtered using Apache Edgent libraries. I know that I can do something on
the `public double get()` method, but I wonder if I could do this trick
with some method of the Apache Edgent (
https://github.com/felipegutierrez/explore-rpi/blob/master/src/main/java/org/sense/sensor/UltrasonicStream.java
).
public class UltrasonicStream implements Supplier<Double> {
private static final long serialVersionUID = -6511218542753341056L;
private static GpioPinDigitalOutput sensorTriggerPin;
private static GpioPinDigitalInput sensorEchoPin;
private static final GpioController gpio = GpioFactory.getInstance();
private double currentDistance = -1.0;
/**
* The HCSR04 Ultrasonic sensor is connected on the physical pin 16 and 18
which
* correspond to the GPIO 04 and 05 of the WiringPi library.
*/
public UltrasonicStream() {
// Trigger pin as OUTPUT
sensorTriggerPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04);
// Echo pin as INPUT
sensorEchoPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_05,
PinPullResistance.PULL_DOWN);
}
/**
* This is the override method of the Supplier interface from Apache Edgent
*/
@Override
public Double get() {
try {
System.out.print("Distance in centimeters: ");
currentDistance = getDistance();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return currentDistance;
}
/**
* Retrieve the distance measured by the HCSR04 Ultrasonic sensor connected
on a
* Raspberry Pi 3+B
*
* @return the distance in centimeters
* @throws InterruptedException
*/
public double getDistance() throws InterruptedException {
double distanceCM = -1;
try {
// Thread.sleep(2000);
sensorTriggerPin.high(); // Make trigger pin HIGH
Thread.sleep((long) 0.01);// Delay for 10 microseconds
sensorTriggerPin.low(); // Make trigger pin LOW
// Wait until the ECHO pin gets HIGH
while (sensorEchoPin.isLow()) {
}
// Store the current time to calculate ECHO pin HIGH time.
long startTime = System.nanoTime();
// Wait until the ECHO pin gets LOW
while (sensorEchoPin.isHigh()) {
}
// Store the echo pin HIGH end time to calculate ECHO pin HIGH time.
long endTime = System.nanoTime();
distanceCM = ((((endTime - startTime) / 1e3) / 2) / 29.1);
// Printing out the distance in centimeters
// System.out.println("Distance: " + distanceCM + " centimeters");
return distanceCM;
} catch (InterruptedException e) {
e.printStackTrace();
}
return distanceCM;
}
}
Thanks,
Felipe
*--*
*-- Felipe Gutierrez*
*-- skype: felipe.o.gutierrez*
*--* *https://felipeogutierrez.blogspot.com
<https://felipeogutierrez.blogspot.com>*