package org.quad.finalyearproject;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.List;
import com.example.safsdf.R;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity implements SensorEventListener {
Handler hand = new Handler();
private ToggleButton button,arm;
private TextView tv;
boolean armon,sent=false;
SeekBar sb;
String motorvalues;
Communicator c;
boolean onetime=true;
boolean twotime=true;
int armed=0;
int h=0;
int yaw; // View to draw a compass
int pitch;
int roll;
int pyaw=0,ppitch=0,proll=0;
int throttle=1000;
private SensorManager sensorService;
private Sensor accel;
private Sensor gyro;
long dtMili = System.currentTimeMillis();
private List<Sensor> sensor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (ToggleButton) findViewById(R.id.connect); //reference to the send button
arm=(ToggleButton)findViewById(R.id.arm);
sensorService = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
tv=(TextView)findViewById(R.id.textView1);
sensor=sensorService.getSensorList(Sensor.TYPE_ALL);
accel = sensorService.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
gyro =sensorService.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sb=(SeekBar)findViewById(R.id.seekBar1);
sb.setVisibility(View.INVISIBLE);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
//90 91
if(System.currentTimeMillis()-200>=dtMili)
{
throttle=arg1+50;
throttle=throttle*9;
dtMili = System.currentTimeMillis();
tv.setText(throttle+"");
}
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}});
Iterator<Sensor> iterator = sensor.iterator();
while (iterator.hasNext()) {
Sensor d=iterator.next();
System.out.println(d.getName());
}
}
protected void onPause() {
super.onPause();
sensorService.unregisterListener(this);
}
protected void onResume() {
super.onResume();
sensorService.registerListener(this, accel, SensorManager.SENSOR_DELAY_NORMAL);
sensorService.registerListener(this, gyro, SensorManager.SENSOR_DELAY_NORMAL);
}
public void onarmclicked(View view) throws IOException
{
boolean armon = ((ToggleButton) view).isChecked();
if(armon)
c.armmotors();
if(!armon)
c.disarmotors();
}
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
c=new Communicator(MainActivity.this);
c.execute();
Toast.makeText(MainActivity.this, "Network connection available.", Toast.LENGTH_SHORT).show();
sb.setVisibility(View.VISIBLE);
} else {
Toast.makeText(MainActivity.this, "No network connection available.", Toast.LENGTH_SHORT).show();
}
} else {
if(c.online())
{
boolean f=c.offline();
if(f==true)
{Toast.makeText(MainActivity.this, "Connection Terminated", Toast.LENGTH_SHORT).show();
sb.setVisibility(View.INVISIBLE);
}
else
Toast.makeText(MainActivity.this, "Connection couldn't be Terminated", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
float[] mGravity;
float[] mGeomagnetic;
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values.clone();
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values.clone();
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success) {
String s="";
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
yaw =(int)( Math.toDegrees( orientation[0] ) + 0.5 ); // orientation contains: azimut, pitch and roll
pitch=(int)( Math.toDegrees( orientation[1] ) + 0.5 );
roll=(int)( Math.toDegrees( orientation[2] ) + 0.5 );//102
if(onetime)
{ pyaw=yaw;
ppitch=pitch;
proll=roll;
onetime=false;}
if(yaw<pyaw-2 || yaw>pyaw+2) //100
{
s=yaw+" ";
pyaw=yaw;}
else
s=pyaw+" ";
if(pitch<ppitch-2 || pitch>ppitch+2) //102
{s=s+pitch+" ";
ppitch=pitch;}
else
s=s+ppitch+" ";
if(roll<proll-2|| roll>proll+2) //102
{s=s+roll+" ";
proll=roll;}
else
s=s+proll+" ";
tv.setText(s);s="";
}
}
}
public class Communicator extends AsyncTask<Void, String, Void> {
public static final String SERVER_IP = "192.168.3.105";
public static final int SERVERPORT = 3732;
String messsage;
private Socket client;
ObjectInputStream oIn;
ObjectOutputStream oOut;
boolean Activated=false;
public boolean online()
{
return Activated;
}
public void disarmotors() throws IOException {
armed=1;
sendDataToServer(new COM_DATA("disarm",true,4),oOut);
try {
Thread.sleep(150);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public boolean offline()
{
try {
oOut.close();
oIn.close();
client.close();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public Communicator(Context context)
{
}
private COM_DATA getDataFromServer( ObjectInputStream oIn ) throws IOException
{
COM_DATA tDataFromClient = new COM_DATA("nothing",true,55);
System.out.println(oIn.available());
try {
tDataFromClient = (COM_DATA) oIn.readObject();
System.out.println(tDataFromClient.iRet);
} catch ( ClassNotFoundException e ) {
System.out.println( "ClassNotFound" );
}
catch(IOException e)
{
System.out.println("NO STREAM");
}
catch(ClassCastException e)
{System.out.println("TOO FAST");}
return tDataFromClient;
}
void armmotors() throws IOException
{armed=1;
sendDataToServer(new COM_DATA("arm",true,3),oOut);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
boolean sendDataToServer( COM_DATA tDataToClient, ObjectOutputStream oOut ) throws IOException
{
oOut.writeObject( tDataToClient );
oOut.flush();
return true;
}
@Override
protected void onProgressUpdate(String...arg0) {
tv.setText(arg0[0]);
}
@Override
protected Void doInBackground(Void... arg0) {
int h=0;
COM_DATA datafromserver=new COM_DATA("hello",true,10);
try {Activated=true;
if(twotime==true){
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
client = new Socket(serverAddr, SERVERPORT);
oOut = new ObjectOutputStream( client.getOutputStream() );
oIn = new ObjectInputStream( client.getInputStream() );
do{
if(armed==1)
{
sendDataToServer(new COM_DATA(throttle+"",true,1),oOut);
h++;
if(h==2)
sent=true;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(sent)
{
datafromserver=getDataFromServer(oIn);
if(datafromserver.iRet==8)
motorvalues=datafromserver.comData;
publishProgress(motorvalues);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
while(c.online());
}
}
catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}
}
}
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.List;
import com.example.safsdf.R;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity implements SensorEventListener {
Handler hand = new Handler();
private ToggleButton button,arm;
private TextView tv;
boolean armon,sent=false;
SeekBar sb;
String motorvalues;
Communicator c;
boolean onetime=true;
boolean twotime=true;
int armed=0;
int h=0;
int yaw; // View to draw a compass
int pitch;
int roll;
int pyaw=0,ppitch=0,proll=0;
int throttle=1000;
private SensorManager sensorService;
private Sensor accel;
private Sensor gyro;
long dtMili = System.currentTimeMillis();
private List<Sensor> sensor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (ToggleButton) findViewById(R.id.connect); //reference to the send button
arm=(ToggleButton)findViewById(R.id.arm);
sensorService = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
tv=(TextView)findViewById(R.id.textView1);
sensor=sensorService.getSensorList(Sensor.TYPE_ALL);
accel = sensorService.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
gyro =sensorService.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sb=(SeekBar)findViewById(R.id.seekBar1);
sb.setVisibility(View.INVISIBLE);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
//90 91
if(System.currentTimeMillis()-200>=dtMili)
{
throttle=arg1+50;
throttle=throttle*9;
dtMili = System.currentTimeMillis();
tv.setText(throttle+"");
}
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}});
Iterator<Sensor> iterator = sensor.iterator();
while (iterator.hasNext()) {
Sensor d=iterator.next();
System.out.println(d.getName());
}
}
protected void onPause() {
super.onPause();
sensorService.unregisterListener(this);
}
protected void onResume() {
super.onResume();
sensorService.registerListener(this, accel, SensorManager.SENSOR_DELAY_NORMAL);
sensorService.registerListener(this, gyro, SensorManager.SENSOR_DELAY_NORMAL);
}
public void onarmclicked(View view) throws IOException
{
boolean armon = ((ToggleButton) view).isChecked();
if(armon)
c.armmotors();
if(!armon)
c.disarmotors();
}
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
c=new Communicator(MainActivity.this);
c.execute();
Toast.makeText(MainActivity.this, "Network connection available.", Toast.LENGTH_SHORT).show();
sb.setVisibility(View.VISIBLE);
} else {
Toast.makeText(MainActivity.this, "No network connection available.", Toast.LENGTH_SHORT).show();
}
} else {
if(c.online())
{
boolean f=c.offline();
if(f==true)
{Toast.makeText(MainActivity.this, "Connection Terminated", Toast.LENGTH_SHORT).show();
sb.setVisibility(View.INVISIBLE);
}
else
Toast.makeText(MainActivity.this, "Connection couldn't be Terminated", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
float[] mGravity;
float[] mGeomagnetic;
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values.clone();
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values.clone();
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success) {
String s="";
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
yaw =(int)( Math.toDegrees( orientation[0] ) + 0.5 ); // orientation contains: azimut, pitch and roll
pitch=(int)( Math.toDegrees( orientation[1] ) + 0.5 );
roll=(int)( Math.toDegrees( orientation[2] ) + 0.5 );//102
if(onetime)
{ pyaw=yaw;
ppitch=pitch;
proll=roll;
onetime=false;}
if(yaw<pyaw-2 || yaw>pyaw+2) //100
{
s=yaw+" ";
pyaw=yaw;}
else
s=pyaw+" ";
if(pitch<ppitch-2 || pitch>ppitch+2) //102
{s=s+pitch+" ";
ppitch=pitch;}
else
s=s+ppitch+" ";
if(roll<proll-2|| roll>proll+2) //102
{s=s+roll+" ";
proll=roll;}
else
s=s+proll+" ";
tv.setText(s);s="";
}
}
}
public class Communicator extends AsyncTask<Void, String, Void> {
public static final String SERVER_IP = "192.168.3.105";
public static final int SERVERPORT = 3732;
String messsage;
private Socket client;
ObjectInputStream oIn;
ObjectOutputStream oOut;
boolean Activated=false;
public boolean online()
{
return Activated;
}
public void disarmotors() throws IOException {
armed=1;
sendDataToServer(new COM_DATA("disarm",true,4),oOut);
try {
Thread.sleep(150);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public boolean offline()
{
try {
oOut.close();
oIn.close();
client.close();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public Communicator(Context context)
{
}
private COM_DATA getDataFromServer( ObjectInputStream oIn ) throws IOException
{
COM_DATA tDataFromClient = new COM_DATA("nothing",true,55);
System.out.println(oIn.available());
try {
tDataFromClient = (COM_DATA) oIn.readObject();
System.out.println(tDataFromClient.iRet);
} catch ( ClassNotFoundException e ) {
System.out.println( "ClassNotFound" );
}
catch(IOException e)
{
System.out.println("NO STREAM");
}
catch(ClassCastException e)
{System.out.println("TOO FAST");}
return tDataFromClient;
}
void armmotors() throws IOException
{armed=1;
sendDataToServer(new COM_DATA("arm",true,3),oOut);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
boolean sendDataToServer( COM_DATA tDataToClient, ObjectOutputStream oOut ) throws IOException
{
oOut.writeObject( tDataToClient );
oOut.flush();
return true;
}
@Override
protected void onProgressUpdate(String...arg0) {
tv.setText(arg0[0]);
}
@Override
protected Void doInBackground(Void... arg0) {
int h=0;
COM_DATA datafromserver=new COM_DATA("hello",true,10);
try {Activated=true;
if(twotime==true){
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
client = new Socket(serverAddr, SERVERPORT);
oOut = new ObjectOutputStream( client.getOutputStream() );
oIn = new ObjectInputStream( client.getInputStream() );
do{
if(armed==1)
{
sendDataToServer(new COM_DATA(throttle+"",true,1),oOut);
h++;
if(h==2)
sent=true;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(sent)
{
datafromserver=getDataFromServer(oIn);
if(datafromserver.iRet==8)
motorvalues=datafromserver.comData;
publishProgress(motorvalues);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
while(c.online());
}
}
catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}
}
}
No comments:
Post a Comment