So i think i have pinned the communication problem. It is stable now. Moving towards the stablization code soon. I still havnt tried the quad I know its dumb but I am a bit scared. Will try it this week promise as the days are coming close. I feel it is time now.
Sunday, March 16, 2014
Saturday, March 15, 2014
Large delays small delays
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;
}
}
}
Java and signed stuff
Dont be fooled by the signed and unsigned stuff for example. I do this:
short msp=(short)recvBuf[4]; //where recvBuf is a serial port inputstream and i am
//capturing the 4th place of that inputstream storing it into short.
System.out.println(msp); //e.g shows -56 but in reality is 200
but if i do
if(msp==200)
//this loop will work
signed and unsigned is just an illusion in the end the processor only sees the bits and compare
short msp=(short)recvBuf[4]; //where recvBuf is a serial port inputstream and i am
//capturing the 4th place of that inputstream storing it into short.
System.out.println(msp); //e.g shows -56 but in reality is 200
but if i do
if(msp==200)
//this loop will work
signed and unsigned is just an illusion in the end the processor only sees the bits and compare
RXTXcomm
Problem understanding where to place the SerialPortListener. Should it be on the Thread or on the Class which has all the serial protocols implemented. It is currently in the Class SerialTest which implements the SerialPortListener but no values are being given back by it. Thinking of moving the SerialPort Listener to the thread. Hope it works.
Thursday, March 13, 2014
Where it all starts!!
So my story is a bit complex in nature. Born in Gilgit at the time when mobiles and laptops were considered dreams. Things like microprocessors were not in the knowledge of anyone there. I was the younger son of an Army Officer how was strong in his beliefs and always fought against anything he thought was unjust. A true embodiment of a tharakutz: straight, street smart and dominating in nature. My life was spent usually travelling across the country and staying at the most far flung places Pakistan has to offer. From Gilgit to Bahawalpur, from Landi kotal to Miranshah I have seen the most beautiful and the most ugly places in Pakistan. The Nature of my father was of an adventurer so we travelled within these places too. Short trips to places surrounding our "then home" on weekends, listening to Junaid Jamshed's songs while we took long drives. It was a simple and good life. One thing that was missing was the relative interaction, its not that we didn't have contact with them at all but still we were like the outcasts or the like the people who get banished. Then came our grand father ( from our mother-side), a teacher in nature, he taught us all he had to offer not just mathematics but a way of thinking. I always thought of him as a philosopher. Although he only taught us maths but still to me maths became a gateway to understanding this world and the things around me. I saw systems within systems while I was quite young. I was unique amongst my siblings because I was a left-handed person and that made all the difference in how I saw the world.Our wiring is different.
Events, Events and more Events
I am on a mission to improve the GUI on the android side tenfolds. Hope it also solves my dilemma of the escs jumping up at 1100 and suddenly giving a lot of thrust. D6 pin also not working when RCSERIAL is enabled in the multiwii. Whats going on? I think they both are linked in some manner. Anyhow found are really good altimeter will introduce it with the motor power graphics. Hope it looks good by the end of weekend. Hope I solve my problems in 2 weeks.
Just my Luck
So the problem was not the overclocking it was actually the sd card getting out of its socket in the RPI. Very nice move slick. I lost all my environments in the RPI. Sad day
Overclocking the RPi
Overclocking the RPi is a fun thing to do. at 800 MHz things start to become smooth. Problem occured for me when I overclocked it to 950 MHz. The Raspbian didnt boot up and I was looking at a black HDMI screen infront of me. I did alot of work on that OS. I am a bit scared now as I have to fetch the RXTX library again and I have forgotten the javac and java cmd in the terminal. It was a long line so I had it in the RPi history of terminal. Sad I will have to start from scratch. Good thing I have my code ^_^. So the RPi will be up in 2-3 hrs again hopefully.
Subscribe to:
Posts (Atom)