Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
fd9066fa83 | |||
fcd949e427 | |||
d260219ddb | |||
0645ed1eb6 |
@@ -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="140626"
|
android:versionCode="140404"
|
||||||
android:versionName="14.06.26" >
|
android:versionName="14.04.04" >
|
||||||
|
|
||||||
<uses-sdk
|
<uses-sdk
|
||||||
android:minSdkVersion="11"
|
android:minSdkVersion="11"
|
||||||
|
30
README.md
30
README.md
@@ -1,28 +1,4 @@
|
|||||||
NxtAR: A generic software architecture for Augmented Reality based mobile robot control.
|
NxtAR-cam
|
||||||
========================================================================================
|
=========
|
||||||
|
|
||||||
Camera module
|
Modulo 1 de mi trabajo especial de grado.
|
||||||
-------------
|
|
||||||
|
|
||||||
### 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
|
|
||||||
|
@@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
<resources>
|
<resources>
|
||||||
|
|
||||||
<string name="app_name">NxtAR-cam</string>
|
<string name="app_name">NxtCAM</string>
|
||||||
<string name="action_settings">Settings</string>
|
<string name="action_settings">Settings</string>
|
||||||
<string name="hello_world">Hello world!</string>
|
<string name="hello_world">Hello world!</string>
|
||||||
<string name="camera_success">Cámara abierta exitosamente</string>
|
<string name="camera_success">Cámara abierta exitosamente</string>
|
||||||
|
@@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
<resources>
|
<resources>
|
||||||
|
|
||||||
<string name="app_name">NxtAR-cam</string>
|
<string name="app_name">NxtCAM</string>
|
||||||
<string name="action_settings">Settings</string>
|
<string name="action_settings">Settings</string>
|
||||||
<string name="hello_world">Hello world!</string>
|
<string name="hello_world">Hello world!</string>
|
||||||
<string name="camera_success">Camera successfully opened</string>
|
<string name="camera_success">Camera successfully opened</string>
|
||||||
|
@@ -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, RECENTER, ROTATE_90};
|
public enum motor_t {NONE, MOTOR_A, MOTOR_B, MOTOR_C, MOTOR_AC, RECENTER};
|
||||||
|
|
||||||
private motor_t motor;
|
private motor_t motor;
|
||||||
private byte power;
|
private byte power;
|
||||||
|
@@ -30,8 +30,6 @@ 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;
|
||||||
@@ -41,10 +39,8 @@ 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 static final String TAG = "SURFVIEW";
|
private final String TAG = "SURFVIEW";
|
||||||
private static final String CLASS_NAME = CameraPreview.class.getSimpleName();
|
private 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;
|
||||||
@@ -58,6 +54,7 @@ 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);
|
||||||
|
|
||||||
@@ -65,99 +62,78 @@ 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;
|
||||||
Size optimal;
|
Camera.Parameters camParams;
|
||||||
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();
|
||||||
sizes = camParams.getSupportedPreviewSizes();
|
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 + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
optimal = getOptimalPreviewSize(sizes, camera.new Size(OPTIMAL_WIDTH, OPTIMAL_HEIGHT));
|
/*Size optimal = getOptimalPreviewSize(sizes, w, h);
|
||||||
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;
|
||||||
|
|
||||||
info = new CameraInfo();
|
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
|
||||||
Camera.getCameraInfo(0, info);
|
android.hardware.Camera.getCameraInfo(0, info);
|
||||||
rotation = parentActivity.getWindowManager().getDefaultDisplay().getRotation();
|
rotation = parentActivity.getWindowManager().getDefaultDisplay().getRotation();
|
||||||
|
|
||||||
switch (rotation) {
|
switch (rotation) {
|
||||||
default:
|
case Surface.ROTATION_0: degrees = 0; break;
|
||||||
case Surface.ROTATION_0:
|
case Surface.ROTATION_90: degrees = 90; break;
|
||||||
degrees = 0;
|
case Surface.ROTATION_180: degrees = 180; break;
|
||||||
break;
|
case Surface.ROTATION_270: degrees = 270; 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 == CameraInfo.CAMERA_FACING_FRONT) {
|
if (info.facing == Camera.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
|
||||||
@@ -170,7 +146,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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,47 +154,51 @@ 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) {
|
||||||
* <p>Tries to obtain the preview size that better matches the target size pixel dimensions.</p>
|
final double ASPECT_TOLERANCE = 0.1;
|
||||||
*
|
double targetRatio = (double) w / h;
|
||||||
* @param sizes An initialized list of supported preview sizes.
|
|
||||||
* @param target The ideal preview size.
|
Logger.log_d(TAG, CLASS_NAME + ".getOptimalPreviewSize() :: Method started.");
|
||||||
* @return The size from the list of sizes that better matches the target. Null if sizes is null or empty.
|
if (sizes == null) return null;
|
||||||
*/
|
|
||||||
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;
|
||||||
|
|
||||||
if (sizes == null || sizes.size() == 0) return null;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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 - TARGET_HEIGHT) < minDiff) {
|
if (Math.abs(size.height - targetHeight) < minDiff) {
|
||||||
optimalSize = size;
|
optimalSize = size;
|
||||||
minDiff = Math.abs(size.height - TARGET_HEIGHT);
|
minDiff = Math.abs(size.height - targetHeight);
|
||||||
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 found is (" + Integer.toString(optimalSize.width) + ", " + Integer.toString(optimalSize.height) + ")");
|
Logger.log_d(TAG, CLASS_NAME + ".getOptimalPreviewSize() :: Optimal size is: (" + Integer.toString(optimalSize.width) +
|
||||||
|
", " + Integer.toString(optimalSize.height) + ")");
|
||||||
return optimalSize;
|
return optimalSize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -73,16 +73,11 @@ public class NxtBTCommThread extends Thread{
|
|||||||
msg[0] |= (event.getMotor() == motor_t.MOTOR_C) ? MotorMasks.MOTOR_C : 0;
|
msg[0] |= (event.getMotor() == motor_t.MOTOR_C) ? MotorMasks.MOTOR_C : 0;
|
||||||
// Set the direction bit.
|
// Set the direction bit.
|
||||||
if(event.getPower() > 0) msg[0] |= MotorMasks.DIRECTION;
|
if(event.getPower() > 0) msg[0] |= MotorMasks.DIRECTION;
|
||||||
// Set the recenter bit.
|
// Set the recenter bits.
|
||||||
msg[0] |= (event.getMotor() == motor_t.RECENTER) ? MotorMasks.RECENTER : 0;
|
msg[0] |= (event.getMotor() == motor_t.RECENTER) ? MotorMasks.RECENTER : 0;
|
||||||
if((msg[0] & MotorMasks.RECENTER) > 0)
|
if((msg[0] & MotorMasks.RECENTER) > 0)
|
||||||
Logger.log_i(TAG, CLASS_NAME + ".run(): Recenter received.");
|
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.
|
// Set the power byte.
|
||||||
msg[1] = (byte)Math.abs(event.getPower());
|
msg[1] = (byte)Math.abs(event.getPower());
|
||||||
|
|
||||||
|
@@ -18,8 +18,7 @@ package ve.ucv.ciens.ccg.nxtcam.network.protocols;
|
|||||||
/**
|
/**
|
||||||
* <p>Bit masks used to code/decode the control instructions sent by NxtAR-cam to
|
* <p>Bit masks used to code/decode the control instructions sent by NxtAR-cam to
|
||||||
* NxtAR-bot.</p>
|
* NxtAR-bot.</p>
|
||||||
*
|
* <p>Expansions 1-3 are currently unused.</p>
|
||||||
* <p>Expansions 2-3 are currently unused.</p>
|
|
||||||
*/
|
*/
|
||||||
public abstract class MotorMasks {
|
public abstract class MotorMasks {
|
||||||
public static final byte MOTOR_A = (byte)0x01;
|
public static final byte MOTOR_A = (byte)0x01;
|
||||||
@@ -27,7 +26,7 @@ public abstract class MotorMasks {
|
|||||||
public static final byte MOTOR_C = (byte)0x04;
|
public static final byte MOTOR_C = (byte)0x04;
|
||||||
public static final byte DIRECTION = (byte)0x08;
|
public static final byte DIRECTION = (byte)0x08;
|
||||||
public static final byte RECENTER = (byte)0x10;
|
public static final byte RECENTER = (byte)0x10;
|
||||||
public static final byte ROTATE_90 = (byte)0x20;
|
public static final byte EXPANSION_1 = (byte)0x20;
|
||||||
public static final byte EXPANSION_2 = (byte)0x40;
|
public static final byte EXPANSION_2 = (byte)0x20;
|
||||||
public static final byte EXPANSION_3 = (byte)0x80;
|
public static final byte EXPANSION_3 = (byte)0x20;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user