Code Monkey home page Code Monkey logo

usb-devices's Introduction

usb-devices

A Java library to manage USB devices like LIDARs, Arduino boards, IMUs, GPS, etc...

Devices supported

1. 2D LIDAR

The library supports SLAMTEC A1 & A2 models

Example using a RPLIDAR A2

package examples;

import ev3dev.sensors.slamtec.RPLidarA1;
import ev3dev.sensors.slamtec.RPLidarProviderListener;
import ev3dev.sensors.slamtec.model.Scan;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

public @Slf4j class Demo3 {

    private static AtomicInteger counter;

    public static void main(String[] args) throws Exception {

        log.info("Testing RPLidar on a EV3Dev with Java");
        final String USBPort = "/dev/ttyUSB0";
        final RPLidarA1 lidar = new RPLidarA1(USBPort);
        lidar.init();
        lidar.addListener(new RPLidarProviderListener() {
            @Override
            public void scanFinished(Scan scan) {
                //log.info("Iteration: {}, Measures: {}", counter.incrementAndGet(), scan.getDistances().size());
                log.info("Measures: {}", scan.getDistances().size());
                scan.getDistances()
                        .stream()
                        .filter((measure) -> measure.getQuality() > 10)
                        .filter((measure) -> (measure.getAngle() >= 345 || measure.getAngle() <= 15))
                        .filter((measure) -> measure.getDistance() <= 50)
                        .forEach(System.out::println);
            }
        });
        for(int x = 0; x <= 10; x++) {
            lidar.scan();
        }
        lidar.close();
        log.info("End");
        System.exit(0);
    }

}

2. Arduino

Arduino 9 axes motion shield (BNO055)

http://www.arduino.org/products/shields/arduino-9-axes-motion-shield

Example using the Arduino + Shield:


package examples;

import ev3dev.actuators.Sound;
import ev3dev.arduino.sensors.bn055.BNO055;
import ev3dev.arduino.sensors.bn055.BNO055Listener;
import ev3dev.arduino.sensors.bn055.model.BNO055Response;
import ev3dev.arduino.sensors.bn055.model.Euler;
import ev3dev.arduino.sensors.bn055.model.Euler;
import ev3dev.sensors.Battery;
import ev3dev.sensors.Button;
import lombok.extern.slf4j.Slf4j;

public @Slf4j class BNO055TurnTest {

	public static void main(String[] args) throws Exception {

		final String port = "/dev/ttyACM0";
		final BNO055 bno055 = new BNO055(port);
		bno055.init();

		log.debug("{}", Battery.getInstance().getVoltage());

		bno055.addListener(new BNO055Listener() {

			@Override
			public void dataReceived(final BNO055Response response) {

				if(response.getEuler() != null){

					final Euler euler = response.getEuler();

					log.debug("Heading: {}", euler.getHeading());

					if( (euler.getHeading() > 90.0f) &&
						(euler.getHeading() <= 100.00f)) {

						//Sound.getInstance().beep();
						log.info("REACHED");
					}
				}
			}

		});

		Button.waitForAnyPress();
		log.debug("{}", Battery.getInstance().getVoltage());
		bno055.close();
		log.info("Closing connection with Arduino");

	}
}

Video:

https://www.youtube.com/watch?v=OY2B7B0Qi2Y

3. GPS

Example connecting with a USB GPS

package examples;

import ev3dev.sensors.gps.GenericGPS;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GenericGPSTest {

	public static void main(String[] args) throws Exception {

		final String port = "/dev/ttyACM0";
		final GenericGPS gps = new GenericGPS(port);
		gps.init();

		//This method block main thread 10 seconds
		timeCounter(10);

		gps.close();
		log.info("Closing connection with the USB GPS Device");

		log.info("LAT: {} {}, LAT: {} {}, ALT: {}", gps.getLatitude(), gps.getLatitudeDirection(), gps.getLongitude(), gps.getLongitudeDirection(), gps.getAltitude());
		log.info("DATE: {}, TIMESTAMP: {}", gps.getDate(), gps.getTimeStamp());
		log.info("NSTAT: {}", gps.getSatellitesTracked());

	}

	private static void timeCounter(final int seconds) throws InterruptedException {
		log.info("Start reading");
		for(int x = 0; x <= seconds; x++){
			log.info("Iteration: {} \n\n", x);
			Thread.sleep(1000);
		}
	}
}

usb-devices's People

Contributors

jabrena avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.