Go to Post we used to walk barefoot through five feet of snow to get to school, uphill both ways, with just barbed wire for mittens, and we liked it! - dlavery [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 03-12-2014, 18:38
Jalerre's Avatar
Jalerre Jalerre is offline
Registered User
FRC #4965 (FIRE)
Team Role: Alumni
 
Join Date: Apr 2014
Rookie Year: 2013
Location: Anderson, SC
Posts: 147
Jalerre is on a distinguished road
Using Lejos to program an FTC style robot

My mentor has tasked me along with a few other students to build an FTC style robot (tetrix build kit with an NXT brain) to show off to students in the area for recruiting. We no longer have an FTC team but we think its something simple that will spark people's interests in robotics without having to lug our FRC bot around. The only problem is that we no longer have an active RobotC license and our mentor doesn't think its worth it to purchase one and wants us to think outside of the box and challenge ourselves. I have decided to use the Lejos NXJ library for Java because it seems to suit our purposes and I think it will be good practice for myself and some newer members for this upcoming season. It seems simple enough to write an autonomous program but I was wondering if anybody could give me some help with writing a program for driver control using a joystick controller (Logitech F310 Gamepad) through bluetooth. Any help would be greatly appreciated even if its just about using Lejos in general and other helpful tips.

TL;DR: I need help to write a Lejos NXJ program using a joystick controller through bluetooth.

Last edited by Jalerre : 03-12-2014 at 20:50.
Reply With Quote
  #2   Spotlight this post!  
Unread 03-12-2014, 19:41
Team3266Spencer's Avatar
Team3266Spencer Team3266Spencer is offline
Team Captain and Lead Programmer
AKA: Spencer Lanman
FRC #3266 (Robots-R-US)
Team Role: Programmer
 
Join Date: Oct 2011
Rookie Year: 2012
Location: Richmond, Indiana
Posts: 280
Team3266Spencer is an unknown quantity at this point
Re: Using Lejos to program an FTC style robot

I did some Bluetooth control with LeJos and Lego robots I will try and dig up my code for you.
__________________
2012: Buckeye Regional, Queen City Regional, Human Player
2013: Queen City Regional, Buckeye Regional, Crossroads Regional
Shooter Operator
2014: Crossroads Regional, Queen City Regional
Catapult Operator
2015: Georgia Southern Classic Regional (Winner), Queen City Regional
Chainsaw Operator
Want to talk? TeamSpeak: team3266.noip.me
Reply With Quote
  #3   Spotlight this post!  
Unread 03-12-2014, 23:18
Team3266Spencer's Avatar
Team3266Spencer Team3266Spencer is offline
Team Captain and Lead Programmer
AKA: Spencer Lanman
FRC #3266 (Robots-R-US)
Team Role: Programmer
 
Join Date: Oct 2011
Rookie Year: 2012
Location: Richmond, Indiana
Posts: 280
Team3266Spencer is an unknown quantity at this point
Re: Using Lejos to program an FTC style robot

I know that there's a lot of sample material you can essentially cut and paste on with the Bluetooth connections (as it can be somewhat code intensive but fairly redundant). For the joystick you could use a library like Jinput.

Here's some simple code I rooted up from like 2 years ago. This was just an experiment to see how the Bluetooth worked. It allowed me to drive my robot with the WASD keys.

There is both computer (client) and robot (server) side code here:

ShooterClient.java
Code:
package control.client.shooterBot;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.DataOutputStream;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import lejos.pc.comm.NXTConnector;

public class ShooterClient extends JFrame implements KeyListener {

	private static final long serialVersionUID = -1955110733792225783L;

	//COMMANDS
	public static final int 
		TURN_LEFT = 1,
		TURN_RIGHT = 2,
		FWD = 3,
		BWD = 4,
		SHOOT = 5,
		STOP = 6,
		STOPSHOOT = 7,
		TERMINATE = 99;
	
	//KEYBOARD INPUTS
	public static final int
		FORWARD = 87, //W
		BACKWARD = 83, //S
		LEFT = 65, //A
		RIGHT = 68, //D
		FIRE = 32, //SPACEBAR
		QUIT = 81; //Q
	
	boolean steerReleased = true;
	boolean driveReleased = true;
	
	NXTConnector bt;
	DataOutputStream out;
	
	JTextField text = new JTextField(15);
	
	public ShooterClient (String title) {
		
		super(title);
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(500, 300);
		
		text.setEnabled(false);
		
		Container pane = this.getContentPane();
		pane.setLayout(new FlowLayout());
		pane.add(text);
		
		this.setVisible(true);
		
		bt = new NXTConnector();
		boolean connected = bt.connectTo("btspp://");
		
		if (!connected) {
			
			System.err.println("Failed to connect to NXT!");
			text.setText("Failed to connect to NXT!");
		} else {	
			out = new DataOutputStream(bt.getOutputStream());
			this.addKeyListener(this);
		}
	}
	
	public static void main(String[] args) {
		
		new ShooterClient("Enter Commands");
	}
	
	public void keyTyped(KeyEvent e) {
		
		;
	}
	
	public void keyPressed(KeyEvent e) {
		
		switch (e.getKeyCode()) {
			
			case FORWARD:
				if (driveReleased) {
					giveCmd(FWD);
					text.setText("Forward");
					driveReleased= false;
				}
				break;
			
			case BACKWARD:
				if (driveReleased) {
					giveCmd(BWD);
					text.setText("Backward");
					driveReleased= false;
				}
				break;
			
			case LEFT:
				if (steerReleased) {
					giveCmd(TURN_LEFT);
					text.setText("Left");
					steerReleased= false;
				}
				break;
			
			case RIGHT:
				if (steerReleased) {
					giveCmd(TURN_RIGHT);
					text.setText("Right");
					steerReleased= false;
				}
				break;
			
			case FIRE:
				giveCmd(SHOOT);
				text.setText("Shoot");
				break;
		}
	}
	
	public void keyReleased(KeyEvent e) {
		
		switch (e.getKeyCode()) {
			
			case FORWARD:	
			case BACKWARD:
				giveCmd(STOP);
				driveReleased= true;
				text.setText("Stopped");
				break;
				
			case LEFT:
			case RIGHT:
				giveCmd(STOP);
				steerReleased= true;
				text.setText("Stopped");
				break;
				
			case FIRE:
				giveCmd(STOPSHOOT);
				text.setText("Stopped Shooting");
				break;
				
			case QUIT:
				giveCmd(TERMINATE);
				text.setText("Quitting");
				System.exit(1);
				break;
		}
	}
	
	public void giveCmd(int cmd) {
		try {
			
			out.writeInt(cmd);
			out.flush();
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
ShooterServer.java
Code:
package control.server.shooterBot;

import java.io.DataInputStream;

import lejos.nxt.Motor;
import lejos.nxt.Sound;
import lejos.nxt.comm.Bluetooth;
import lejos.nxt.comm.NXTConnection;
import lejos.robotics.navigation.DifferentialPilot;

public class ShooterServer {
	
	//COMMANDS
	public static final int
		LEFT = 1,
		RIGHT = 2,
		FWD = 3,
		BWD = 4,
		SHOOT = 5,
		STOP = 6,
		STOPSHOOT = 7,
		QUIT = 99;
	
	public static boolean
		isLeft = false,
		isRight = false,
		isFwd = false,
		isBwd = false;
	
	private static double radius = 0;
	private static double distance = 0;
	
	public static DifferentialPilot robot;
	
	public static WaitForThread WFT;
	
	public static void main(String[] args) {
		
		Motor.A.setSpeed(900);
		
		robot = new DifferentialPilot(3.8, 13.1, Motor.C, Motor.B, false);
		
		WFT = new WaitForThread();
		
		WFT.start();
		
		while(true) {
			
			System.out.println("Waiting for Connection...");
			NXTConnection bt = Bluetooth.waitForConnection();
			DataInputStream stream = bt.openDataInputStream();
			System.out.println("Connected");
			
			Sound.beepSequenceUp();
			
			int input = 0;
			
			do {
				try {
					
					input = stream.readInt();
					System.out.println("Command: "+input);
					
				} catch (Exception e) {
					;
				}
				
				giveCmd(input);
			} while(input != QUIT);
			
			try {
				
				stream.close();
				Thread.sleep(100);
				bt.close();
				
				Sound.beepSequence();
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private static void giveCmd(int cmd) {
		
		switch (cmd) {
		
			case LEFT:
				robot.rotateLeft();
				break;
				
			case RIGHT:
				robot.rotateRight();
				break;
				
			case FWD:
				robot.forward();
				break;
				
			case BWD:
				robot.backward();
				break;
				
			case SHOOT:
				Motor.A.forward();
				break;
				
			case STOP:
				robot.stop();
				break;
				
			case STOPSHOOT:
				Motor.A.stop();
				break;
		}
	}
}
WaitForThread.java
Code:
package control.server.shooterBot;
import lejos.nxt.SensorPort;
import lejos.nxt.TouchSensor;
import lejos.nxt.UltrasonicSensor;

public class WaitForThread extends Thread {
	
	public TouchSensor TS;
	public UltrasonicSensor US;
	
	public WaitForThread() {
		
		TS = new TouchSensor(SensorPort.S2);
		US = new UltrasonicSensor(SensorPort.S4);
	}
	
	public void run() {
		
		try {
			sleep(500);
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}
		
		while (true) {
			
			if (TS.isPressed()) break;
		}
		
		System.exit(1);
	}
}
__________________
2012: Buckeye Regional, Queen City Regional, Human Player
2013: Queen City Regional, Buckeye Regional, Crossroads Regional
Shooter Operator
2014: Crossroads Regional, Queen City Regional
Catapult Operator
2015: Georgia Southern Classic Regional (Winner), Queen City Regional
Chainsaw Operator
Want to talk? TeamSpeak: team3266.noip.me

Last edited by Team3266Spencer : 03-12-2014 at 23:19. Reason: Just realized you are using NXT so my comment about EV3 is irrelevant
Reply With Quote
  #4   Spotlight this post!  
Unread 04-12-2014, 17:53
Jalerre's Avatar
Jalerre Jalerre is offline
Registered User
FRC #4965 (FIRE)
Team Role: Alumni
 
Join Date: Apr 2014
Rookie Year: 2013
Location: Anderson, SC
Posts: 147
Jalerre is on a distinguished road
Re: Using Lejos to program an FTC style robot

Thanks. This is really helpful. If anyone has any experience with JInput, tips for that would be helpful too. I considered using it but I didn't know if there was any better option. I don't have much experience using it but I think I can figure it out.
Reply With Quote
  #5   Spotlight this post!  
Unread 04-12-2014, 19:34
Team3266Spencer's Avatar
Team3266Spencer Team3266Spencer is offline
Team Captain and Lead Programmer
AKA: Spencer Lanman
FRC #3266 (Robots-R-US)
Team Role: Programmer
 
Join Date: Oct 2011
Rookie Year: 2012
Location: Richmond, Indiana
Posts: 280
Team3266Spencer is an unknown quantity at this point
Re: Using Lejos to program an FTC style robot

I'm glad this is helpful! I'm really glad I found these files. They were in some obscure location on my computer. XD
__________________
2012: Buckeye Regional, Queen City Regional, Human Player
2013: Queen City Regional, Buckeye Regional, Crossroads Regional
Shooter Operator
2014: Crossroads Regional, Queen City Regional
Catapult Operator
2015: Georgia Southern Classic Regional (Winner), Queen City Regional
Chainsaw Operator
Want to talk? TeamSpeak: team3266.noip.me
Reply With Quote
  #6   Spotlight this post!  
Unread 04-12-2014, 22:50
DonRotolo's Avatar
DonRotolo DonRotolo is offline
Back to humble
FRC #0832
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Atlanta GA
Posts: 7,011
DonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond repute
Re: Using Lejos to program an FTC style robot

I suppose I could just Google it, but what is a Lejo?
__________________

I am N2IRZ - What's your callsign?
Reply With Quote
  #7   Spotlight this post!  
Unread 04-12-2014, 22:56
cgmv123's Avatar
cgmv123 cgmv123 is online now
FRC RI/FLL Field Manager
AKA: Max Vrany
FRC #1306 (BadgerBOTS)
Team Role: College Student
 
Join Date: Jan 2011
Rookie Year: 2011
Location: Madison, WI
Posts: 2,084
cgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond reputecgmv123 has a reputation beyond repute
Re: Using Lejos to program an FTC style robot

Quote:
Originally Posted by DonRotolo View Post
I suppose I could just Google it, but what is a Lejo?
Java for the LEGO Mindstorms controllers.
__________________
BadgerBOTS Robotics|@team1306|Facebook: BadgerBOTS
2016 FIRST Championship Tesla Division | 2016 Wisconsin Regional Engineering Inspiration Award

2015 FIRST Championship Carson Division | 2015 Wisconsin Regional Chairman's Award

2013 FIRST Championship Curie Division | 2013 Wisconsin Regional Chairman's Award

2012 FIRST Championship Archimedes Division | 2012 Wisconsin Regional Engineering Inspiration Award, Woodie Flowers Finalist Award (Lead Mentor Ben Senson)

Reply With Quote
  #8   Spotlight this post!  
Unread 06-12-2014, 18:12
Team3266Spencer's Avatar
Team3266Spencer Team3266Spencer is offline
Team Captain and Lead Programmer
AKA: Spencer Lanman
FRC #3266 (Robots-R-US)
Team Role: Programmer
 
Join Date: Oct 2011
Rookie Year: 2012
Location: Richmond, Indiana
Posts: 280
Team3266Spencer is an unknown quantity at this point
Re: Using Lejos to program an FTC style robot

leJOS stands for Lego Java Operating System
__________________
2012: Buckeye Regional, Queen City Regional, Human Player
2013: Queen City Regional, Buckeye Regional, Crossroads Regional
Shooter Operator
2014: Crossroads Regional, Queen City Regional
Catapult Operator
2015: Georgia Southern Classic Regional (Winner), Queen City Regional
Chainsaw Operator
Want to talk? TeamSpeak: team3266.noip.me
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 21:11.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi