15 Commits

9 changed files with 189 additions and 110 deletions

View File

@@ -17,8 +17,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ve.ucv.ciens.ccg.nxtcam" package="ve.ucv.ciens.ccg.nxtcam"
android:versionCode="1" android:versionCode="140626"
android:versionName="1.0" > android:versionName="14.06.26" >
<uses-sdk <uses-sdk
android:minSdkVersion="11" android:minSdkVersion="11"

View File

@@ -1,4 +1,4 @@
NxtCAM NxtAR-cam
====== =========
Modulo 1 de mi trabajo especial de grado. Modulo 1 de mi trabajo especial de grado.

View File

@@ -5,7 +5,7 @@ import java.io.Serializable;
public class MotorEvent implements Serializable{ public class MotorEvent implements Serializable{
private static final long serialVersionUID = 9989L; private static final long serialVersionUID = 9989L;
public enum motor_t {NONE, MOTOR_A, MOTOR_B, MOTOR_C, MOTOR_AC}; public enum motor_t {NONE, MOTOR_A, MOTOR_B, MOTOR_C, MOTOR_AC, RECENTER, ROTATE_90};
private motor_t motor; private motor_t motor;
private byte power; private byte power;

View File

@@ -16,7 +16,8 @@
package ve.ucv.ciens.ccg.nxtcam; package ve.ucv.ciens.ccg.nxtcam;
import ve.ucv.ciens.ccg.nxtcam.camera.CameraPreview; import ve.ucv.ciens.ccg.nxtcam.camera.CameraPreview;
import ve.ucv.ciens.ccg.nxtcam.network.LCPThread; import ve.ucv.ciens.ccg.nxtcam.network.NxtBTCommThread;
import ve.ucv.ciens.ccg.nxtcam.network.SensorReportThread;
import ve.ucv.ciens.ccg.nxtcam.network.VideoStreamingThread; import ve.ucv.ciens.ccg.nxtcam.network.VideoStreamingThread;
import ve.ucv.ciens.ccg.nxtcam.utils.Logger; import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants; import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants;
@@ -40,7 +41,8 @@ public class CamActivity extends Activity{
private CameraPreview cPreview; private CameraPreview cPreview;
private CameraSetupTask camSetupTask; private CameraSetupTask camSetupTask;
private VideoStreamingThread imThread; private VideoStreamingThread imThread;
private LCPThread botThread; private NxtBTCommThread botThread;
private SensorReportThread sensorThread;
private String serverIp; private String serverIp;
/******************* /*******************
@@ -57,8 +59,11 @@ public class CamActivity extends Activity{
imThread = new VideoStreamingThread(serverIp); imThread = new VideoStreamingThread(serverIp);
imThread.start(); imThread.start();
botThread = new LCPThread(serverIp); botThread = new NxtBTCommThread(serverIp);
botThread.start(); botThread.start();
sensorThread = new SensorReportThread(serverIp);
sensorThread.start();
} }
@Override @Override
@@ -92,14 +97,14 @@ public class CamActivity extends Activity{
camSetupTask = new CameraSetupTask(); camSetupTask = new CameraSetupTask();
camSetupTask.execute(); camSetupTask.execute();
// imThread.start(); // TODO: resumethe imThread, botThread and sensorThread objects.
} }
@Override @Override
public void onPause(){ public void onPause(){
super.onPause(); super.onPause();
// TODO: pause the imThread and botThread objects. // TODO: pause the imThread, botThread and sensorThread objects.
if(cPreview != null){ if(cPreview != null){
cPreview.removePreviewCallback(); cPreview.removePreviewCallback();
@@ -116,6 +121,9 @@ public class CamActivity extends Activity{
botThread.finish(); botThread.finish();
botThread = null; botThread = null;
sensorThread.finish();
sensorThread = null;
} }
@Override @Override

View File

@@ -30,6 +30,8 @@ import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.hardware.Camera; import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size; import android.hardware.Camera.Size;
import android.os.Build; import android.os.Build;
import android.view.Surface; import android.view.Surface;
@@ -39,8 +41,10 @@ import android.view.SurfaceView;
/** A basic Camera preview class */ /** A basic Camera preview class */
@SuppressLint("ViewConstructor") @SuppressLint("ViewConstructor")
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback { public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {
private final String TAG = "SURFVIEW"; private static final String TAG = "SURFVIEW";
private final String CLASS_NAME = CameraPreview.class.getSimpleName(); private static final String CLASS_NAME = CameraPreview.class.getSimpleName();
private static final int OPTIMAL_WIDTH = 352;
private static final int OPTIMAL_HEIGHT = 288;
private CameraImageMonitor imgMonitor; private CameraImageMonitor imgMonitor;
private Activity parentActivity; private Activity parentActivity;
@@ -54,7 +58,6 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
super(context); super(context);
parentActivity = (Activity)context; parentActivity = (Activity)context;
// surfaceView = new SurfaceView(context);
holder = getHolder(); holder = getHolder();
holder.addCallback(this); holder.addCallback(this);
@@ -62,78 +65,99 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
} }
/**
* <p>Sets the camera for this surface view.</p>
*
* @param camera The camera to set.
*/
public void setCamera(Camera camera){ public void setCamera(Camera camera){
this.camera = camera; this.camera = camera;
if(this.camera != null){ if(this.camera != null){
Logger.log_d(TAG, CLASS_NAME + ".setCamera() :: Setting camera."); Logger.log_d(TAG, CLASS_NAME + ".setCamera(): Setting camera.");
imgMonitor = CameraImageMonitor.getInstance(); imgMonitor = CameraImageMonitor.getInstance();
requestLayout(); requestLayout();
Logger.log_d(TAG, CLASS_NAME + ".setCamera() :: Camera set."); Logger.log_d(TAG, CLASS_NAME + ".setCamera(): Camera set.");
} }
} }
@Override
public void surfaceCreated(SurfaceHolder holder){ public void surfaceCreated(SurfaceHolder holder){
// The Surface has been created, now tell the camera where to draw the preview. // The Surface has been created, now tell the camera where to draw the preview.
Logger.log_d(TAG, CLASS_NAME + ".surfaceCreated() :: Creating surface view."); Logger.log_d(TAG, CLASS_NAME + ".surfaceCreated(): Creating surface view.");
try { try {
if(camera != null) if(camera != null)
camera.setPreviewDisplay(holder); camera.setPreviewDisplay(holder);
} catch (IOException e) { } catch (IOException e) {
Logger.log_e(TAG, CLASS_NAME + ".surfaceCreated() :: Error creating camera: " + e.getMessage()); Logger.log_e(TAG, CLASS_NAME + ".surfaceCreated(): Error creating camera: " + e.getMessage());
} }
} }
@Override
public void surfaceDestroyed(SurfaceHolder holder){ public void surfaceDestroyed(SurfaceHolder holder){
if(camera != null) if(camera != null)
camera.stopPreview(); camera.stopPreview();
} }
@Override
public void surfaceChanged(SurfaceHolder tmpHolder, int format, int w, int h){ public void surfaceChanged(SurfaceHolder tmpHolder, int format, int w, int h){
int result; int result;
int rotation; int rotation;
int degrees = 0; int degrees = 0;
Camera.Parameters camParams; Size optimal;
CameraInfo info;
List<Size> sizes;
Parameters camParams;
Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged() :: Method started."); Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged(): Method started.");
if(this.holder.getSurface() == null || camera == null){ if(this.holder.getSurface() == null || camera == null){
Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged() :: Holder and/or camera are null."); Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged(): Holder and/or camera are null.");
return; return;
} }
try{ camera.stopPreview(); }catch (Exception e){ } try{ camera.stopPreview(); }catch (Exception e){ }
Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged(): Request layout.");
requestLayout(); requestLayout();
Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged(): Get parameters.");
camParams = camera.getParameters(); camParams = camera.getParameters();
camParams.getSupportedPreviewSizes(); sizes = camParams.getSupportedPreviewSizes();
List<Size> sizes = camParams.getSupportedPreviewSizes();
for(Size size: sizes){ for(Size size: sizes){
Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged() :: Supported preview size (" + size.width + ", " + size.height + ")"); Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged(): Supported preview size (" + size.width + ", " + size.height + ")");
} }
/*Size optimal = getOptimalPreviewSize(sizes, w, h); optimal = getOptimalPreviewSize(sizes, camera.new Size(OPTIMAL_WIDTH, OPTIMAL_HEIGHT));
Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged() :: Preview size set at (" + optimal.width + ", " + optimal.height + ")"); Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged(): Preview size set at (" + optimal.width + ", " + optimal.height + ")");
camParams.setPreviewSize(optimal.width, optimal.height);*/ camParams.setPreviewSize(optimal.width, optimal.height);
camParams.setPreviewSize(352, 288);
camera.setParameters(camParams); camera.setParameters(camParams);
/*previewWidth = optimal.width; previewWidth = optimal.width;
previewHeight = optimal.height;*/ previewHeight = optimal.height;
previewWidth = 352;
previewHeight = 288;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); info = new CameraInfo();
android.hardware.Camera.getCameraInfo(0, info); Camera.getCameraInfo(0, info);
rotation = parentActivity.getWindowManager().getDefaultDisplay().getRotation(); rotation = parentActivity.getWindowManager().getDefaultDisplay().getRotation();
switch (rotation) { switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break; default:
case Surface.ROTATION_90: degrees = 90; break; case Surface.ROTATION_0:
case Surface.ROTATION_180: degrees = 180; break; degrees = 0;
case Surface.ROTATION_270: degrees = 270; break; break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
} }
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360; result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror result = (360 - result) % 360; // compensate the mirror
} else { // back-facing } else { // back-facing
@@ -146,7 +170,7 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
camera.setPreviewDisplay(this.holder); camera.setPreviewDisplay(this.holder);
camera.startPreview(); camera.startPreview();
}catch (Exception e){ }catch (Exception e){
Logger.log_e(TAG, CLASS_NAME + ".surfaceChanged() :: Error starting camera preview: " + e.getMessage()); Logger.log_e(TAG, CLASS_NAME + ".surfaceChanged(): Error starting camera preview: " + e.getMessage());
} }
} }
@@ -154,51 +178,47 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
public void onPreviewFrame(byte[] data, Camera camera){ public void onPreviewFrame(byte[] data, Camera camera){
if(imgMonitor.hasChanged()) if(imgMonitor.hasChanged())
imgMonitor.setImageParameters(previewWidth, previewHeight); imgMonitor.setImageParameters(previewWidth, previewHeight);
Logger.log_d(TAG, CLASS_NAME + ".onPreviewFrame() :: Preview received");
Logger.log_d(TAG, CLASS_NAME + ".onPreviewFrame() :: Frame has" + (imgMonitor.hasChanged() ? "" : " not") + " been consumed.");
imgMonitor.setImageData(data); imgMonitor.setImageData(data);
} }
/**
* <p>If camera is not null then disable the camera preview.</p>
*/
public void removePreviewCallback(){ public void removePreviewCallback(){
if(camera != null) if(camera != null)
camera.setPreviewCallback(null); camera.setPreviewCallback(null);
} }
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) { /**
final double ASPECT_TOLERANCE = 0.1; * <p>Tries to obtain the preview size that better matches the target size pixel dimensions.</p>
double targetRatio = (double) w / h; *
* @param sizes An initialized list of supported preview sizes.
Logger.log_d(TAG, CLASS_NAME + ".getOptimalPreviewSize() :: Method started."); * @param target The ideal preview size.
if (sizes == null) return null; * @return The size from the list of sizes that better matches the target. Null if sizes is null or empty.
*/
private Size getOptimalPreviewSize(List<Size> sizes, final Size target) {
final int TARGET_HEIGHT = target.height;
Size optimalSize = null; Size optimalSize = null;
double minDiff = Double.MAX_VALUE; double minDiff = Double.MAX_VALUE;
int targetHeight = h; if (sizes == null || sizes.size() == 0) return null;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement // Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) { if (optimalSize == null) {
Logger.log_d(TAG, CLASS_NAME + ".getOptimalPreviewSize(): Ignoring aspect ratio.");
minDiff = Double.MAX_VALUE; minDiff = Double.MAX_VALUE;
for (Size size : sizes) { for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) { if (Math.abs(size.height - TARGET_HEIGHT) < minDiff) {
optimalSize = size; optimalSize = size;
minDiff = Math.abs(size.height - targetHeight); minDiff = Math.abs(size.height - TARGET_HEIGHT);
Logger.log_d(TAG, CLASS_NAME + ".getOptimalPreviewSize(): Size = (" + Integer.toString(size.width) + ", " + Integer.toString(size.width) + ")");
Logger.log_d(TAG, CLASS_NAME + ".getOptimalPreviewSize(): minDiff = " + Double.toString(minDiff));
} }
} }
} }
Logger.log_d(TAG, CLASS_NAME + ".getOptimalPreviewSize() :: Method ended.");
Logger.log_d(TAG, CLASS_NAME + ".getOptimalPreviewSize() :: Optimal size is: (" + Integer.toString(optimalSize.width) + Logger.log_d(TAG, CLASS_NAME + ".getOptimalPreviewSize(): Optimal size found is (" + Integer.toString(optimalSize.width) + ", " + Integer.toString(optimalSize.height) + ")");
", " + Integer.toString(optimalSize.height) + ")");
return optimalSize; return optimalSize;
} }
} }

View File

@@ -21,7 +21,6 @@ import java.io.ObjectOutputStream;
import java.net.Socket; import java.net.Socket;
import ve.ucv.ciens.ccg.networkdata.MotorEvent; import ve.ucv.ciens.ccg.networkdata.MotorEvent;
import ve.ucv.ciens.ccg.networkdata.MotorEvent.motor_t;
import ve.ucv.ciens.ccg.networkdata.MotorEventACK; import ve.ucv.ciens.ccg.networkdata.MotorEventACK;
import ve.ucv.ciens.ccg.nxtcam.robotcontrol.MotorEventQueue; import ve.ucv.ciens.ccg.nxtcam.robotcontrol.MotorEventQueue;
import ve.ucv.ciens.ccg.nxtcam.utils.Logger; import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
@@ -64,7 +63,7 @@ public class MotorControlThread extends Thread {
if(event != null){ if(event != null){
queue.addEvent(event); queue.addEvent(event);
Logger.log_i(TAG, CLASS_NAME + ".run() :: Motor control message enqueued."); Logger.log_i(TAG, CLASS_NAME + ".run() :: Motor control message enqueued.");
Logger.log_i(TAG, CLASS_NAME + ".run() :: Motor ID: " + (event.getMotor() == motor_t.MOTOR_A ? "MOTOR_A" : "MOTOR_C")); Logger.log_i(TAG, CLASS_NAME + ".run() :: Motor ID: " + event.getMotor().toString());
Logger.log_i(TAG, CLASS_NAME + ".run() :: Motor power: " + Byte.toString(event.getPower())); Logger.log_i(TAG, CLASS_NAME + ".run() :: Motor power: " + Byte.toString(event.getPower()));
}else{ }else{
Logger.log_i(TAG, CLASS_NAME + ".run() :: Message could not be verified;"); Logger.log_i(TAG, CLASS_NAME + ".run() :: Message could not be verified;");

View File

@@ -19,36 +19,33 @@ import java.io.IOException;
import ve.ucv.ciens.ccg.networkdata.MotorEvent; import ve.ucv.ciens.ccg.networkdata.MotorEvent;
import ve.ucv.ciens.ccg.networkdata.MotorEvent.motor_t; import ve.ucv.ciens.ccg.networkdata.MotorEvent.motor_t;
import ve.ucv.ciens.ccg.nxtcam.network.protocols.LegoCommunicationProtocol; import ve.ucv.ciens.ccg.nxtcam.network.protocols.MotorMasks;
import ve.ucv.ciens.ccg.nxtcam.robotcontrol.MotorEventQueue; import ve.ucv.ciens.ccg.nxtcam.robotcontrol.MotorEventQueue;
import ve.ucv.ciens.ccg.nxtcam.utils.Logger; import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
public class LCPThread extends Thread{ public class NxtBTCommThread extends Thread{
private static final String TAG = "LCP_THREAD"; private static final String TAG = "LCP_THREAD";
private static final String CLASS_NAME = LCPThread.class.getSimpleName(); private static final String CLASS_NAME = NxtBTCommThread.class.getSimpleName();
private boolean done; private boolean done;
private boolean reportSensors;
private BTCommunicator btComm; private BTCommunicator btComm;
private MotorControlThread motorControl; private MotorControlThread motorControl;
private SensorReportThread sensorReport;
private MotorEventQueue queue; private MotorEventQueue queue;
public LCPThread(String serverIp){ public NxtBTCommThread(String serverIp){
super("Robot Control Main Thread"); super("Robot Control Main Thread");
btComm = BTCommunicator.getInstance(); btComm = BTCommunicator.getInstance();
done = false; done = false;
motorControl = new MotorControlThread(serverIp); motorControl = new MotorControlThread(serverIp);
sensorReport = new SensorReportThread(serverIp);
queue = MotorEventQueue.getInstance(); queue = MotorEventQueue.getInstance();
} }
public void run(){ public void run(){
long then, now, delta; long then, now, delta;
MotorEvent event; MotorEvent event;
byte[] msg = new byte[2];
sensorReport.start();
motorControl.start(); motorControl.start();
then = System.currentTimeMillis(); then = System.currentTimeMillis();
@@ -62,24 +59,35 @@ public class LCPThread extends Thread{
} }
} }
if((reportSensors = sensorReport.isConnected())){
Logger.log_d(TAG, CLASS_NAME + ".run() :: Sensor data can be reported.");
}else{
Logger.log_e(TAG, CLASS_NAME + ".run() :: Thread sensorReport could not connect to the server.");
Logger.log_e(TAG, CLASS_NAME + ".run() :: Sensor data will not be reported to server app.");
}
while(!done){ while(!done){
if(btComm.isBTEnabled() && btComm.isConnected()){ if(btComm.isBTEnabled() && btComm.isConnected()){
msg[0] = 0x00;
msg[1] = 0x00;
event = queue.getNextEvent(); event = queue.getNextEvent();
try{ try{
btComm.writeMessage( // Set the motor bit.
LegoCommunicationProtocol.setOutputState( msg[0] |= (event.getMotor() == motor_t.MOTOR_A) ? MotorMasks.MOTOR_A : 0;
event.getMotor() == motor_t.MOTOR_A ? LegoCommunicationProtocol.PORT_0 : (event.getMotor() == motor_t.MOTOR_B ? LegoCommunicationProtocol.PORT_1 : LegoCommunicationProtocol.PORT_2), msg[0] |= (event.getMotor() == motor_t.MOTOR_B) ? MotorMasks.MOTOR_B : 0;
event.getPower()) msg[0] |= (event.getMotor() == motor_t.MOTOR_C) ? MotorMasks.MOTOR_C : 0;
); // Set the direction bit.
if(event.getPower() > 0) msg[0] |= MotorMasks.DIRECTION;
// Set the recenter bit.
msg[0] |= (event.getMotor() == motor_t.RECENTER) ? MotorMasks.RECENTER : 0;
if((msg[0] & MotorMasks.RECENTER) > 0)
Logger.log_i(TAG, CLASS_NAME + ".run(): Recenter received.");
// Set the rotate bit.
msg[0] |= (event.getMotor() == motor_t.ROTATE_90) ? MotorMasks.ROTATE_90 : 0;
if((msg[0] & MotorMasks.ROTATE_90) > 0)
Logger.log_i(TAG, CLASS_NAME + ".run(): Rotate 90 received.");
// Set the power byte.
msg[1] = (byte)Math.abs(event.getPower());
// Send the message.
btComm.writeMessage(msg);
Logger.log_i(TAG, CLASS_NAME + ".run() :: Message sent to the robot."); Logger.log_i(TAG, CLASS_NAME + ".run() :: Message sent to the robot.");
try{ sleep(40); }catch(InterruptedException ie){ } try{ sleep(40); }catch(InterruptedException ie){ }
@@ -87,10 +95,6 @@ public class LCPThread extends Thread{
}catch(IOException io){ }catch(IOException io){
Logger.log_e(TAG, CLASS_NAME + ".run() :: IOException sending message to the robot: " + io.getMessage()); Logger.log_e(TAG, CLASS_NAME + ".run() :: IOException sending message to the robot: " + io.getMessage());
} }
if(reportSensors){
}
}else{ }else{
Logger.log_e(TAG, CLASS_NAME + ".run() :: The robot disconnected or was never available."); Logger.log_e(TAG, CLASS_NAME + ".run() :: The robot disconnected or was never available.");
break; break;

View File

@@ -1,7 +1,7 @@
package ve.ucv.ciens.ccg.nxtcam.network; package ve.ucv.ciens.ccg.nxtcam.network;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectOutputStream; import java.io.OutputStream;
import java.net.Socket; import java.net.Socket;
import ve.ucv.ciens.ccg.nxtcam.utils.Logger; import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
@@ -14,21 +14,36 @@ public class SensorReportThread extends Thread{
private Socket socket; private Socket socket;
private String serverIp; private String serverIp;
private boolean done; private boolean done;
private ObjectOutputStream writer; private OutputStream writer;
private boolean connected; private boolean connected;
private BTCommunicator btComm;
public SensorReportThread(String serverIp){ public SensorReportThread(String serverIp){
super("Sensor Report Thread"); super("Sensor Report Thread");
this.serverIp = serverIp; this.serverIp = serverIp;
done = false; done = false;
connected = false; connected = false;
btComm = BTCommunicator.getInstance();
} }
@Override @Override
public void run(){ public void run(){
byte[] lightReading;
if(connectToServer()){ if(connectToServer()){
while(!done){ while(!done){
if(btComm.isBTEnabled() && btComm.isConnected()){
try{
lightReading = btComm.readMessage(1);
writer.write(lightReading);
}catch(IOException io){
Logger.log_e(TAG, CLASS_NAME + "run(): IOException: " + io.getMessage());
done = true;
}
}else{
Logger.log_e(TAG, CLASS_NAME + ".run() :: The robot disconnected or was never available.");
break;
}
} }
}else{ }else{
Logger.log_e(TAG, CLASS_NAME + ".run() :: Could not connect to the server."); Logger.log_e(TAG, CLASS_NAME + ".run() :: Could not connect to the server.");
@@ -43,7 +58,7 @@ public class SensorReportThread extends Thread{
boolean connected; boolean connected;
try{ try{
socket = new Socket(serverIp, ProjectConstants.SENSOR_REPORT_PORT); socket = new Socket(serverIp, ProjectConstants.SENSOR_REPORT_PORT);
writer = new ObjectOutputStream(socket.getOutputStream()); writer = socket.getOutputStream();
connected = true; connected = true;
}catch(IOException io){ }catch(IOException io){
Logger.log_e(TAG, CLASS_NAME + ".connectToServer() :: IOException caught: " + io.getMessage()); Logger.log_e(TAG, CLASS_NAME + ".connectToServer() :: IOException caught: " + io.getMessage());

View File

@@ -0,0 +1,33 @@
/*
* Copyright (C) 2014 Miguel Angel Astor Romero
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ve.ucv.ciens.ccg.nxtcam.network.protocols;
/**
* <p>Bit masks used to code/decode the control instructions sent by NxtAR-cam to
* NxtAR-bot.</p>
*
* <p>Expansions 2-3 are currently unused.</p>
*/
public abstract class MotorMasks {
public static final byte MOTOR_A = (byte)0x01;
public static final byte MOTOR_B = (byte)0x02;
public static final byte MOTOR_C = (byte)0x04;
public static final byte DIRECTION = (byte)0x08;
public static final byte RECENTER = (byte)0x10;
public static final byte ROTATE_90 = (byte)0x20;
public static final byte EXPANSION_2 = (byte)0x40;
public static final byte EXPANSION_3 = (byte)0x80;
}