13 Commits

11 changed files with 216 additions and 113 deletions

View File

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

View File

@@ -1,4 +1,28 @@
NxtCAM
======
NxtAR: A generic software architecture for Augmented Reality based mobile robot control.
========================================================================================
Modulo 1 de mi trabajo especial de grado.
Camera module
-------------
### Abstract ###
NxtAR is a generic software architecture for the development of Augmented Reality games
and applications centered around mobile robot control. This is a reference implementation
with support for [LEGO Mindstorms NXT][1] mobile robots.
### Module description ###
The camera module is a reference implementation of the image capture module of the NxtAR architecture for
the Android (>=3.0) operating system. This module acts as a bridge between the [robot control][2] and [core][3] modules of
the architecture, capturing images to be processed by the core module and forwarding control instructions to
the robot module.
### Module installation and usage. ###
Install the NxtAR-cam_XXXXXX.apk file on your device. Detailed usage instructions can be found in the [NxtAR Android backend module][4]
documentation.
[1]: http://www.lego.com/en-us/mindstorms/?domainredir=mindstorms.lego.com
[2]: https://github.com/sagge-miky/NxtAR-bot
[3]: https://github.com/sagge-miky/NxtAR-core
[4]: https://github.com/sagge-miky/NxtAR-android

View File

@@ -17,7 +17,7 @@
<resources>
<string name="app_name">NxtCAM</string>
<string name="app_name">NxtAR-cam</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="camera_success">Cámara abierta exitosamente</string>

View File

@@ -17,7 +17,7 @@
<resources>
<string name="app_name">NxtCAM</string>
<string name="app_name">NxtAR-cam</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="camera_success">Camera successfully opened</string>

View File

@@ -5,7 +5,7 @@ import java.io.Serializable;
public class MotorEvent implements Serializable{
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 byte power;

View File

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

View File

@@ -30,6 +30,8 @@ import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import android.os.Build;
import android.view.Surface;
@@ -39,8 +41,10 @@ import android.view.SurfaceView;
/** A basic Camera preview class */
@SuppressLint("ViewConstructor")
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {
private final String TAG = "SURFVIEW";
private final String CLASS_NAME = CameraPreview.class.getSimpleName();
private static final String TAG = "SURFVIEW";
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 Activity parentActivity;
@@ -54,7 +58,6 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
super(context);
parentActivity = (Activity)context;
// surfaceView = new SurfaceView(context);
holder = getHolder();
holder.addCallback(this);
@@ -62,78 +65,99 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
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){
this.camera = camera;
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();
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){
// 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 {
if(camera != null)
camera.setPreviewDisplay(holder);
} 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){
if(camera != null)
camera.stopPreview();
}
@Override
public void surfaceChanged(SurfaceHolder tmpHolder, int format, int w, int h){
int result;
int rotation;
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){
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;
}
try{ camera.stopPreview(); }catch (Exception e){ }
Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged(): Request layout.");
requestLayout();
Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged(): Get parameters.");
camParams = camera.getParameters();
camParams.getSupportedPreviewSizes();
List<Size> sizes = camParams.getSupportedPreviewSizes();
sizes = camParams.getSupportedPreviewSizes();
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);
Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged() :: Preview size set at (" + optimal.width + ", " + optimal.height + ")");
camParams.setPreviewSize(optimal.width, optimal.height);*/
camParams.setPreviewSize(352, 288);
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 + ")");
camParams.setPreviewSize(optimal.width, optimal.height);
camera.setParameters(camParams);
/*previewWidth = optimal.width;
previewHeight = optimal.height;*/
previewWidth = 352;
previewHeight = 288;
previewWidth = optimal.width;
previewHeight = optimal.height;
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(0, info);
info = new CameraInfo();
Camera.getCameraInfo(0, info);
rotation = parentActivity.getWindowManager().getDefaultDisplay().getRotation();
switch (rotation) {
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
default:
case Surface.ROTATION_0:
degrees = 0;
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 = (360 - result) % 360; // compensate the mirror
} else { // back-facing
@@ -146,7 +170,7 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
camera.setPreviewDisplay(this.holder);
camera.startPreview();
}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){
if(imgMonitor.hasChanged())
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);
}
/**
* <p>If camera is not null then disable the camera preview.</p>
*/
public void removePreviewCallback(){
if(camera != null)
camera.setPreviewCallback(null);
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) w / h;
Logger.log_d(TAG, CLASS_NAME + ".getOptimalPreviewSize() :: Method started.");
if (sizes == null) return null;
/**
* <p>Tries to obtain the preview size that better matches the target size pixel dimensions.</p>
*
* @param sizes An initialized list of supported preview sizes.
* @param target The ideal preview size.
* @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;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// 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);
}
}
if (sizes == null || sizes.size() == 0) return null;
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
Logger.log_d(TAG, CLASS_NAME + ".getOptimalPreviewSize(): Ignoring aspect ratio.");
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
if (Math.abs(size.height - TARGET_HEIGHT) < minDiff) {
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) +
", " + Integer.toString(optimalSize.height) + ")");
Logger.log_d(TAG, CLASS_NAME + ".getOptimalPreviewSize(): Optimal size found is (" + Integer.toString(optimalSize.width) + ", " + Integer.toString(optimalSize.height) + ")");
return optimalSize;
}
}

View File

@@ -21,7 +21,6 @@ import java.io.ObjectOutputStream;
import java.net.Socket;
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.nxtcam.robotcontrol.MotorEventQueue;
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
@@ -64,7 +63,7 @@ public class MotorControlThread extends Thread {
if(event != null){
queue.addEvent(event);
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()));
}else{
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.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.utils.Logger;
public class LCPThread extends Thread{
public class NxtBTCommThread extends 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 reportSensors;
private BTCommunicator btComm;
private MotorControlThread motorControl;
private SensorReportThread sensorReport;
private MotorEventQueue queue;
public LCPThread(String serverIp){
public NxtBTCommThread(String serverIp){
super("Robot Control Main Thread");
btComm = BTCommunicator.getInstance();
done = false;
motorControl = new MotorControlThread(serverIp);
sensorReport = new SensorReportThread(serverIp);
queue = MotorEventQueue.getInstance();
}
public void run(){
long then, now, delta;
MotorEvent event;
byte[] msg = new byte[2];
sensorReport.start();
motorControl.start();
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){
if(btComm.isBTEnabled() && btComm.isConnected()){
msg[0] = 0x00;
msg[1] = 0x00;
event = queue.getNextEvent();
try{
btComm.writeMessage(
LegoCommunicationProtocol.setOutputState(
event.getMotor() == motor_t.MOTOR_A ? LegoCommunicationProtocol.PORT_0 : (event.getMotor() == motor_t.MOTOR_B ? LegoCommunicationProtocol.PORT_1 : LegoCommunicationProtocol.PORT_2),
event.getPower())
);
// Set the motor bit.
msg[0] |= (event.getMotor() == motor_t.MOTOR_A) ? MotorMasks.MOTOR_A : 0;
msg[0] |= (event.getMotor() == motor_t.MOTOR_B) ? MotorMasks.MOTOR_B : 0;
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.");
try{ sleep(40); }catch(InterruptedException ie){ }
@@ -87,10 +95,6 @@ public class LCPThread extends Thread{
}catch(IOException io){
Logger.log_e(TAG, CLASS_NAME + ".run() :: IOException sending message to the robot: " + io.getMessage());
}
if(reportSensors){
}
}else{
Logger.log_e(TAG, CLASS_NAME + ".run() :: The robot disconnected or was never available.");
break;

View File

@@ -1,7 +1,7 @@
package ve.ucv.ciens.ccg.nxtcam.network;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import ve.ucv.ciens.ccg.nxtcam.utils.Logger;
@@ -14,21 +14,36 @@ public class SensorReportThread extends Thread{
private Socket socket;
private String serverIp;
private boolean done;
private ObjectOutputStream writer;
private OutputStream writer;
private boolean connected;
private BTCommunicator btComm;
public SensorReportThread(String serverIp){
super("Sensor Report Thread");
this.serverIp = serverIp;
done = false;
connected = false;
btComm = BTCommunicator.getInstance();
}
@Override
public void run(){
byte[] lightReading;
if(connectToServer()){
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{
Logger.log_e(TAG, CLASS_NAME + ".run() :: Could not connect to the server.");
@@ -43,7 +58,7 @@ public class SensorReportThread extends Thread{
boolean connected;
try{
socket = new Socket(serverIp, ProjectConstants.SENSOR_REPORT_PORT);
writer = new ObjectOutputStream(socket.getOutputStream());
writer = socket.getOutputStream();
connected = true;
}catch(IOException io){
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;
}