Hello,
Im trying to send the ObjectX and ObjectY values to arduino from this
app
http://www.robotsee.com/android.html
Here is the code
// ColorBot by Eric Gregori (
www.EMGRobotics.com )
//
// This source is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
package
colorbot.emgrobotics.com;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.SeekBar;
import android.widget.TextView;
import android.hardware.Camera.PreviewCallback;
import android.view.Menu;
import android.view.MenuItem;
import android.app.AlertDialog;
import at.abraxas.amarino.Amarino;
public class colorbot extends Activity {
private static final String DEVICE_ADDRESS = "00:06:66:42:1F:C8";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Amarino.connect(this, DEVICE_ADDRESS);
requestWindowFeature(Window.FEATURE_NO_TITLE);
ParmData.initParms();
Preview mPreview = new Preview(this);
DrawOnTop mDraw = new DrawOnTop(this);
setContentView(mPreview);
addContentView(mDraw, new LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0,0,0,"Histogram");
menu.add(0,1,0,"About");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case (1):
AlertDialog alertDialog = new
AlertDialog.Builder(this).create();
alertDialog.setTitle("ColorBot");
alertDialog.setMessage("Written by Eric Gregori\n
(
www.EMGRobotics.com)");
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
return true;
case (0) :
ParmData.setShowHistogram();
return true;
}
return false;
}
}
class DrawOnTop extends View {
int testx, testy;
int width, height;
int Cr, Cb;
Paint paint;
Paint paint1;
Paint paint2;
Paint paint3;
Paint PaintWhite;
Paint PaintBlack;
Paint selectColor[];
boolean ColorSelected;
boolean FirstTime;
static int NumberOfColorSamples=16;
static int TurnSquareHeight = 200;
public DrawOnTop(Context context )
{
super(context);
testx = 0;
testy = 0;
// Create paint objects at init
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint1 = new Paint();
paint1.setStyle(Paint.Style.FILL);
paint1.setColor(Color.BLUE);
paint2 = new Paint();
paint2.setStyle(Paint.Style.STROKE);
paint2.setColor(Color.GREEN);
PaintBlack = new Paint();
PaintBlack.setStyle(Paint.Style.FILL);
PaintBlack.setColor(Color.BLACK);
PaintWhite = new Paint();
PaintWhite.setStyle(Paint.Style.FILL);
PaintWhite.setColor(Color.WHITE);
// Create paint objects for color pallattes
selectColor = new Paint[NumberOfColorSamples];
for( int i=0; i<NumberOfColorSamples; i++ )
{
selectColor[i] = new Paint();
selectColor[i].setStyle(Paint.Style.FILL);
}
width = 0;
ColorSelected = false;
FirstTime = true;
}
@Override
protected void onDraw(Canvas canvas)
{
if( width == 0 )
{
width = canvas.getWidth();
height = canvas.getHeight();
}
// Update color pallette based on color of object to track
if( ParmData.SelectUpdate() )
{
int R,G,B;
Cb = ParmData.getSelectU()-128;
Cr = ParmData.getSelectV()-128;
for( int Y=NumberOfColorSamples; Y<256; Y+=(256/
(NumberOfColorSamples-1)) )
{
B = Y + ((454 * Cb) >> 8);
if(B < 0) B = 0; else if(B > 255) B = 255;
G = Y - ((88 * Cb + 183 * Cr) >> 8);
if(G < 0) G = 0; else if(G > 255) G = 255;
R = Y + ((359 * Cr) >> 8);
if(R < 0) R = 0; else if(R > 255) R = 255;
selectColor[Y/
NumberOfColorSamples-1].setColor(Color.rgb(R,G,B));
}
ColorSelected = true;
FirstTime = false;
}
// Draw black box behind tracking display
canvas.drawRect(0,0,160,120,PaintBlack);
if( FirstTime )
{
canvas.drawText( "Touch object color in", 10, 20,
PaintWhite );
canvas.drawText( "camera preview window", 10, 40,
PaintWhite );
canvas.drawText( "below, to track.", 10, 60,
PaintWhite );
}
else
{
// Update tracking display
for( int index=0; index<(320*240/4); index++ )
{
if( ParmData.readParms(index) > 0 )
canvas.drawPoint(index%160, (index/160),
selectColor[NumberOfColorSamples/2]);
}
}
//canvas.drawText( "MAXU="+ParmData.getMaxu(), 170, 30,
paint);
//canvas.drawText( "MINU="+ParmData.getMinu(), 170, 60,
paint);
//canvas.drawText( "MAXV="+ParmData.getMaxv(), 170, 90,
paint);
//canvas.drawText( "MINV="+ParmData.getMinv(), 170, 120,
paint);
//canvas.drawText( "Testx="+testx, 170, 150, paint );
//canvas.drawText( "Testy="+testy, 170, 180, paint );
//canvas.drawText( "selectCr="+Cr, 170, 210, paint );
//canvas.drawText( "selectCb="+Cb, 170, 240, paint );
// Show tracking color pallette
int ColorHeight = 120/NumberOfColorSamples;
for( int i=0; i<(NumberOfColorSamples-1); i++ )
canvas.drawRect( 160, (i*ColorHeight), 185, (i*ColorHeight)
+ColorHeight, selectColor[i] );
// Draw circle around center of Object
int ObjectIndex = ParmData.getObjectIndex();
int ObjectX = ObjectIndex % 160;
int ObjectY = ObjectIndex / 160;
canvas.drawCircle(ObjectX, ObjectY, 10, paint2 );
canvas.drawRect( 185,0,width,120, PaintBlack);
canvas.drawText( "ObjectX="+ObjectX, 195, 20, PaintWhite );
canvas.drawText( "ObjectY="+ObjectY, 195, 80, PaintWhite );
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'x',
ObjectX); //!!! !!!!!!!!!!!!!!!!!!! !!!!!!!! !!!!!!!!
Amarino.sendDataToArduino(this, DEVICE_ADDRESS, 'y',
ObjectY); //!!!!! !!!!!!!!!! !!!!!!!! !!!!!!!!! !!!!!!
if( !ParmData.getShowHistogram() )
{
if( !ColorSelected )
{
canvas.drawRect(0,height-TurnSquareHeight,width/2,height,
PaintBlack );
canvas.drawRect(width/2,height-
TurnSquareHeight,width,height, PaintBlack );
}
else
{
/*
// Show turn control squares
if( ObjectY > 50 && ObjectY < 70 )
{
// forward
canvas.drawRect(0,height-TurnSquareHeight,width/2,height,
PaintWhite );
canvas.drawRect(width/2,height-
TurnSquareHeight,width,height, PaintWhite );
}
if( ObjectY <= 50 )
{
// turn
canvas.drawRect(0,height-TurnSquareHeight,width/2,height,
PaintWhite );
canvas.drawRect(width/2,height-
TurnSquareHeight,width,height, PaintBlack );
}
if( ObjectY >= 70 )
{
// turn
canvas.drawRect(0,height-TurnSquareHeight,width/2,height,
PaintBlack );
canvas.drawRect(width/2,height-
TurnSquareHeight,width,height, PaintWhite );
}*/
}
}
// Show histogram
else
{
for( int i=0; i<256; i++ )
{
if( i%10 == 0 )
canvas.drawLine(0 , 250+i, 255, 250+i, paint2);
else
{
if( ParmData.getHistogramu(i) <
ParmData.getHistogramv(i) )
{
canvas.drawLine(0 , 250+i, ParmData.getHistogramv(i),
250+i, paint);
canvas.drawLine(0 , 250+i, ParmData.getHistogramu(i),
250+i, paint1);
}
else
{
canvas.drawLine(0 , 250+i, ParmData.getHistogramu(i),
250+i, paint1);
canvas.drawLine(0 , 250+i, ParmData.getHistogramv(i),
250+i, paint);
}
}
}
ParmData.resetHistogram();
} // ShowHistogram
ParmData.PostDisplay();
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
testx = (int)x;
testy = (int)y;
if( y < 120 )
{
ColorSelected = false;
break;
}
int selecty = (int)(y * (240.0/(float)height));
int selectx = (int)(x * (320.0/(float)width));
testx = selectx/2;
testy = selecty/2;
ParmData.setSelect( testy*160+testx );
//invalidate();
break;
case MotionEvent.ACTION_MOVE:
//invalidate();
break;
case MotionEvent.ACTION_UP:
// invalidate();
break;
}
return true;
}
}
//----------------------------------------------------------------------
class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
int ProcessRate;
Preview(Context context ) {
super(context);
// Install a SurfaceHolder.Callback so we get notified when
the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
ProcessRate = 5;
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell
it where
// to draw.
mCamera = Camera.open();
try
{
mCamera.setPreviewDisplay(holder);
mCamera.setPreviewCallback(new PreviewCallback()
{
public void onPreviewFrame(byte[] data, Camera _camera)
{
// TODO Do something with the preview image.
//These are two-plane versions of the YUV 4:2:0 format. The
three components are separated into two sub-images or planes. The Y
plane is first. The Y plane has one byte per pixel. For
V4L2_PIX_FMT_NV12, a combined CbCr plane immediately follows the Y
plane in memory. The CbCr plane is the same width, in bytes, as the Y
plane (and of the image), but is half as tall in pixels. Each CbCr
pair belongs to four pixels. For example, Cb0/Cr0 belongs to Y'00,
Y'01, Y'10, Y'11. V4L2_PIX_FMT_NV21 is the same except the Cb and Cr
bytes are swapped, the CrCb plane starts with a Cr byte.
//If the Y plane has pad bytes after each row, then the CbCr
plane has as many pad bytes after its rows.
//Example 2-1. V4L2_PIX_FMT_NV12 4 Γ— 4 pixel image
//Byte Order. Each cell is one byte.
//start + 0: Y'00 Y'01 Y'02 Y'03
//start + 4: Y'10 Y'11 Y'12 Y'13
//start + 8: Y'20 Y'21 Y'22 Y'23
//start + 12: Y'30 Y'31 Y'32 Y'33
//start + 16: Cr00 Cb00 Cr01 Cb01
//start + 20: Cr10 Cb10 Cr11 Cb11
//Log.d("EMG:", "Callback");
if( --ProcessRate == 0 )
{
int offset = 320*240;
int v, u;
//int vmin, vmax, umin, umax;
//vmin = ParmData.getSelectV() - 10;
//if( vmin < 0 ) vmin = 0;
//vmax = vmin + 20;
//if( vmax > 255 ) vmax = 255;
//umin = ParmData.getSelectU() - 10;
//if( umin < 0 ) umin = 0;
//umax = umin + 20;
//if( umax > 255 ) umax = 255;
for( int index=0; index<(320*240/4); index++ )
{
// CbCr comes in from driver with values between 16 and 240
( unsigned )
// BUT, byte is a signed type in java ( -128 to 127 ) -> 0x80
to 0x7f
// So Java sees 16(0x10) as +16, 240(0xf0) as -16
v = (0x000000ff & data[offset]);
u = (0x000000ff & data[offset+1]);
ParmData.updateStats( v, u, index );
// if( (v>=vmin)&&(v<=vmax)&&(u>=umin)&&(u<=umax) )
// ParmData.setParms(index);
// else
// ParmData.clearParms(index);
offset += 2;
}
postInvalidate();
ProcessRate = 5;
}
} // onpreviewframe
});
} catch (IOException exception) {
mCamera.release();
mCamera = null;
// TODO: add more exception handling logic here
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the
preview.
// Because the CameraDevice object is not a shared resource,
it's very
// important to release it when the activity is paused.
mCamera.stopPreview();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int
w, int h) {
// Now that the size is known, set up the camera parameters
and begin
// the preview.
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(320, 240);
//parameters.setRotation(90);
//parameters.set("orientation", "portrait");
mCamera.setParameters(parameters);
mCamera.startPreview();
}
}
Im getting this error DEVICE_ADDRESS cannot be resolved to a
variable.
Does anybody knows why?
Thank you