From 5bda7bf67d58c88021f9bd16af04851b97387269 Mon Sep 17 00:00:00 2001 From: Blake Bourque Date: Fri, 29 Aug 2014 16:53:32 -0400 Subject: [PATCH 1/4] Update cheesyvision.py to use mDNS --- cheesyvision.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cheesyvision.py b/cheesyvision.py index a1910fb..ae37a77 100644 --- a/cheesyvision.py +++ b/cheesyvision.py @@ -68,8 +68,8 @@ import socket import time -# CHANGE THIS TO BE YOUR TEAM'S cRIO IP ADDRESS! -HOST, PORT = "10.2.54.2", 1180 +# CHANGE THIS TO BE YOUR TEAM NUMBER IP ADDRESS! +HOST, PORT = "roborio-254.local", 1180 # Name of displayed window WINDOW_NAME = "CheesyVision" From a9e3feb7cb480beee5739f0ceb704bed3a47d9c3 Mon Sep 17 00:00:00 2001 From: Blake Bourque Date: Fri, 29 Aug 2014 16:55:45 -0400 Subject: [PATCH 2/4] Update CheesyVisionRobot.java to use the Java8 Socket class --- .../src/com/team254/CheesyVisionRobot.java | 188 +++++++++++++++--- 1 file changed, 160 insertions(+), 28 deletions(-) diff --git a/examples/Java-CheesyVisionSample/src/com/team254/CheesyVisionRobot.java b/examples/Java-CheesyVisionSample/src/com/team254/CheesyVisionRobot.java index dcccaa1..53be0ac 100644 --- a/examples/Java-CheesyVisionSample/src/com/team254/CheesyVisionRobot.java +++ b/examples/Java-CheesyVisionSample/src/com/team254/CheesyVisionRobot.java @@ -1,42 +1,174 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) FIRST 2008. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -package com.team254; - - -import com.team254.lib.CheesyVisionServer; -import edu.wpi.first.wpilibj.IterativeRobot; +package com.team254.lib; /** * @author Tom Bottiglieri * Team 254, The Cheesy Poofs */ -public class CheesyVisionRobot extends IterativeRobot { - CheesyVisionServer server = CheesyVisionServer.getInstance(); - public final int listenPort = 1180; +import edu.wpi.first.wpilibj.Timer; + +import java.io.IOException; +import java.io.InputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Vector; + + +public class CheesyVisionServer implements Runnable { + + private static CheesyVisionServer instance_; + Thread serverThread = new Thread(this); + private int listenPort_; + private Vector connections_; + private boolean counting_ = false; + private int leftCount_ = 0, rightCount_ = 0, totalCount_ = 0; + private boolean curLeftStatus_ = false, curRightStatus_ = false; + double lastHeartbeatTime_ = -1; + private boolean listening_ = true; + + public static CheesyVisionServer getInstance() { + if (instance_ == null) { + instance_ = new CheesyVisionServer(); + } + return instance_; + } + + public void start() { + serverThread.start(); + } + + public void stop() { + listening_ = false; + } - public void robotInit() { - server.setPort(listenPort); - server.start(); + private CheesyVisionServer() { + this(1180); + } + + private CheesyVisionServer(int port) { + listenPort_ = port; + connections_ = new Vector(); + } + + public boolean hasClientConnection() { + return lastHeartbeatTime_ > 0 && (Timer.getFPGATimestamp() - lastHeartbeatTime_) < 3.0; + } + + public void setPort(int port) { + listenPort_ = port; + } + + private void updateCounts(boolean left, boolean right) { + if (counting_) { + leftCount_ += left ? 1 : 0; + rightCount_ += right ? 1 : 0; + totalCount_++; } + } + + public void startSamplingCounts() { + counting_ = true; + } - public void autonomousInit() { - server.reset(); - server.startSamplingCounts(); + public void stopSamplingCounts() { + counting_ = false; + } + + public void reset() { + leftCount_ = rightCount_ = totalCount_ = 0; + curLeftStatus_ = curRightStatus_ = false; + } + + public int getLeftCount() { + return leftCount_; + } + + public int getRightCount() { + return rightCount_; + } + + public int getTotalCount() { + return totalCount_; + } + + public boolean getLeftStatus() { + return curLeftStatus_; + } + + public boolean getRightStatus() { + return curRightStatus_; + } + + // This class handles incoming TCP connections + private class VisionServerConnectionHandler implements Runnable { + + Socket connection; + + public VisionServerConnectionHandler(Socket c) { + connection = c; } - - public void disabledInit() { - server.stopSamplingCounts(); + + public void run() { + try { + InputStream is = connection.getInputStream(); + + //int ch = 0; + byte[] b = new byte[1024]; + double timeout = 10.0; + double lastHeartbeat = Timer.getFPGATimestamp(); + CheesyVisionServer.this.lastHeartbeatTime_ = lastHeartbeat; + while (Timer.getFPGATimestamp() < lastHeartbeat + timeout) { + //boolean gotData = false; + while (is.available() > 0) { + //gotData = true; + int read = is.read(b); + for (int i = 0; i < read; ++i) { + byte reading = b[i]; + boolean leftStatus = (reading & (1 << 1)) > 0; + boolean rightStatus = (reading & (1 << 0)) > 0; + CheesyVisionServer.this.curLeftStatus_ = leftStatus; + CheesyVisionServer.this.curRightStatus_ = rightStatus; + CheesyVisionServer.this.updateCounts(leftStatus, rightStatus); + } + lastHeartbeat = Timer.getFPGATimestamp(); + CheesyVisionServer.this.lastHeartbeatTime_ = lastHeartbeat; + } + + try { + Thread.sleep(50); // sleep a bit + } catch (InterruptedException ex) { + System.out.println("Thread sleep failed."); + } + } + is.close(); + connection.close(); + + } catch (IOException e) { + } } - + } - public void autonomousPeriodic() { - System.out.println("Current left: " + server.getLeftStatus() + ", current right: " + server.getRightStatus()); - System.out.println("Left count: " + server.getLeftCount() + ", right count: " + server.getRightCount() + ", total: " + server.getTotalCount() + "\n"); + // run() to implement Runnable + // This method listens for incoming connections and spawns new + // VisionServerConnectionHandlers to handle them + public void run() { + ServerSocket s = null; + try { + s = new ServerSocket(listenPort_); + while (listening_) { + Socket connection = (Socket) s.accept(); + Thread t = new Thread(new CheesyVisionServer.VisionServerConnectionHandler(connection)); + t.start(); + connections_.addElement(connection); + try { + Thread.sleep(100); + } catch (InterruptedException ex) { + System.out.println("Thread sleep failed."); + } + } + } catch (IOException e) { + System.out.println("Socket failure."); + e.printStackTrace(); } + } } From eb22564884e83ce6d33980e3b302ea8a403cb409 Mon Sep 17 00:00:00 2001 From: Blake Bourque Date: Fri, 29 Aug 2014 17:03:34 -0400 Subject: [PATCH 3/4] Update CheesyVisionRobot.java to use the Java8 Comm classes --- .../com/team254/lib/CheesyVisionServer.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/examples/Java-CheesyVisionSample/src/com/team254/lib/CheesyVisionServer.java b/examples/Java-CheesyVisionSample/src/com/team254/lib/CheesyVisionServer.java index c7796f0..53be0ac 100644 --- a/examples/Java-CheesyVisionSample/src/com/team254/lib/CheesyVisionServer.java +++ b/examples/Java-CheesyVisionSample/src/com/team254/lib/CheesyVisionServer.java @@ -6,19 +6,20 @@ */ import edu.wpi.first.wpilibj.Timer; + import java.io.IOException; import java.io.InputStream; +import java.net.ServerSocket; +import java.net.Socket; import java.util.Vector; -import javax.microedition.io.Connector; -import javax.microedition.io.ServerSocketConnection; -import javax.microedition.io.SocketConnection; + public class CheesyVisionServer implements Runnable { private static CheesyVisionServer instance_; Thread serverThread = new Thread(this); private int listenPort_; - private Vector connections_; + private Vector connections_; private boolean counting_ = false; private int leftCount_ = 0, rightCount_ = 0, totalCount_ = 0; private boolean curLeftStatus_ = false, curRightStatus_ = false; @@ -46,7 +47,7 @@ private CheesyVisionServer() { private CheesyVisionServer(int port) { listenPort_ = port; - connections_ = new Vector(); + connections_ = new Vector(); } public boolean hasClientConnection() { @@ -101,25 +102,25 @@ public boolean getRightStatus() { // This class handles incoming TCP connections private class VisionServerConnectionHandler implements Runnable { - SocketConnection connection; + Socket connection; - public VisionServerConnectionHandler(SocketConnection c) { + public VisionServerConnectionHandler(Socket c) { connection = c; } public void run() { try { - InputStream is = connection.openInputStream(); + InputStream is = connection.getInputStream(); - int ch = 0; + //int ch = 0; byte[] b = new byte[1024]; double timeout = 10.0; double lastHeartbeat = Timer.getFPGATimestamp(); CheesyVisionServer.this.lastHeartbeatTime_ = lastHeartbeat; while (Timer.getFPGATimestamp() < lastHeartbeat + timeout) { - boolean gotData = false; + //boolean gotData = false; while (is.available() > 0) { - gotData = true; + //gotData = true; int read = is.read(b); for (int i = 0; i < read; ++i) { byte reading = b[i]; @@ -151,11 +152,11 @@ public void run() { // This method listens for incoming connections and spawns new // VisionServerConnectionHandlers to handle them public void run() { - ServerSocketConnection s = null; + ServerSocket s = null; try { - s = (ServerSocketConnection) Connector.open("serversocket://:" + listenPort_); + s = new ServerSocket(listenPort_); while (listening_) { - SocketConnection connection = (SocketConnection) s.acceptAndOpen(); + Socket connection = (Socket) s.accept(); Thread t = new Thread(new CheesyVisionServer.VisionServerConnectionHandler(connection)); t.start(); connections_.addElement(connection); From c3bc435dbaad2fc34c707660b3f21e2be06e81f7 Mon Sep 17 00:00:00 2001 From: Blake Bourque Date: Fri, 29 Aug 2014 17:04:54 -0400 Subject: [PATCH 4/4] Revert mistaken changes to CheesyVisionRobot.java --- .../src/com/team254/CheesyVisionRobot.java | 188 +++--------------- 1 file changed, 28 insertions(+), 160 deletions(-) diff --git a/examples/Java-CheesyVisionSample/src/com/team254/CheesyVisionRobot.java b/examples/Java-CheesyVisionSample/src/com/team254/CheesyVisionRobot.java index 53be0ac..dcccaa1 100644 --- a/examples/Java-CheesyVisionSample/src/com/team254/CheesyVisionRobot.java +++ b/examples/Java-CheesyVisionSample/src/com/team254/CheesyVisionRobot.java @@ -1,174 +1,42 @@ -package com.team254.lib; +/*----------------------------------------------------------------------------*/ +/* Copyright (c) FIRST 2008. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +package com.team254; + + +import com.team254.lib.CheesyVisionServer; +import edu.wpi.first.wpilibj.IterativeRobot; /** * @author Tom Bottiglieri * Team 254, The Cheesy Poofs */ +public class CheesyVisionRobot extends IterativeRobot { -import edu.wpi.first.wpilibj.Timer; - -import java.io.IOException; -import java.io.InputStream; -import java.net.ServerSocket; -import java.net.Socket; -import java.util.Vector; - - -public class CheesyVisionServer implements Runnable { - - private static CheesyVisionServer instance_; - Thread serverThread = new Thread(this); - private int listenPort_; - private Vector connections_; - private boolean counting_ = false; - private int leftCount_ = 0, rightCount_ = 0, totalCount_ = 0; - private boolean curLeftStatus_ = false, curRightStatus_ = false; - double lastHeartbeatTime_ = -1; - private boolean listening_ = true; - - public static CheesyVisionServer getInstance() { - if (instance_ == null) { - instance_ = new CheesyVisionServer(); - } - return instance_; - } - - public void start() { - serverThread.start(); - } - - public void stop() { - listening_ = false; - } + CheesyVisionServer server = CheesyVisionServer.getInstance(); + public final int listenPort = 1180; - private CheesyVisionServer() { - this(1180); - } - - private CheesyVisionServer(int port) { - listenPort_ = port; - connections_ = new Vector(); - } - - public boolean hasClientConnection() { - return lastHeartbeatTime_ > 0 && (Timer.getFPGATimestamp() - lastHeartbeatTime_) < 3.0; - } - - public void setPort(int port) { - listenPort_ = port; - } - - private void updateCounts(boolean left, boolean right) { - if (counting_) { - leftCount_ += left ? 1 : 0; - rightCount_ += right ? 1 : 0; - totalCount_++; + public void robotInit() { + server.setPort(listenPort); + server.start(); } - } - - public void startSamplingCounts() { - counting_ = true; - } - public void stopSamplingCounts() { - counting_ = false; - } - - public void reset() { - leftCount_ = rightCount_ = totalCount_ = 0; - curLeftStatus_ = curRightStatus_ = false; - } - - public int getLeftCount() { - return leftCount_; - } - - public int getRightCount() { - return rightCount_; - } - - public int getTotalCount() { - return totalCount_; - } - - public boolean getLeftStatus() { - return curLeftStatus_; - } - - public boolean getRightStatus() { - return curRightStatus_; - } - - // This class handles incoming TCP connections - private class VisionServerConnectionHandler implements Runnable { - - Socket connection; - - public VisionServerConnectionHandler(Socket c) { - connection = c; + public void autonomousInit() { + server.reset(); + server.startSamplingCounts(); } - - public void run() { - try { - InputStream is = connection.getInputStream(); - - //int ch = 0; - byte[] b = new byte[1024]; - double timeout = 10.0; - double lastHeartbeat = Timer.getFPGATimestamp(); - CheesyVisionServer.this.lastHeartbeatTime_ = lastHeartbeat; - while (Timer.getFPGATimestamp() < lastHeartbeat + timeout) { - //boolean gotData = false; - while (is.available() > 0) { - //gotData = true; - int read = is.read(b); - for (int i = 0; i < read; ++i) { - byte reading = b[i]; - boolean leftStatus = (reading & (1 << 1)) > 0; - boolean rightStatus = (reading & (1 << 0)) > 0; - CheesyVisionServer.this.curLeftStatus_ = leftStatus; - CheesyVisionServer.this.curRightStatus_ = rightStatus; - CheesyVisionServer.this.updateCounts(leftStatus, rightStatus); - } - lastHeartbeat = Timer.getFPGATimestamp(); - CheesyVisionServer.this.lastHeartbeatTime_ = lastHeartbeat; - } - - try { - Thread.sleep(50); // sleep a bit - } catch (InterruptedException ex) { - System.out.println("Thread sleep failed."); - } - } - is.close(); - connection.close(); - - } catch (IOException e) { - } + + public void disabledInit() { + server.stopSamplingCounts(); } - } + - // run() to implement Runnable - // This method listens for incoming connections and spawns new - // VisionServerConnectionHandlers to handle them - public void run() { - ServerSocket s = null; - try { - s = new ServerSocket(listenPort_); - while (listening_) { - Socket connection = (Socket) s.accept(); - Thread t = new Thread(new CheesyVisionServer.VisionServerConnectionHandler(connection)); - t.start(); - connections_.addElement(connection); - try { - Thread.sleep(100); - } catch (InterruptedException ex) { - System.out.println("Thread sleep failed."); - } - } - } catch (IOException e) { - System.out.println("Socket failure."); - e.printStackTrace(); + public void autonomousPeriodic() { + System.out.println("Current left: " + server.getLeftStatus() + ", current right: " + server.getRightStatus()); + System.out.println("Left count: " + server.getLeftCount() + ", right count: " + server.getRightCount() + ", total: " + server.getTotalCount() + "\n"); } - } }