Can't get the servos to move on android USB

Supporting 4.1 and up
EEPBppt
Phidgetsian
Posts: 9
Joined: Fri Nov 19, 2010 12:02 pm
Contact:

Can't get the servos to move on android USB

Postby EEPBppt » Mon Jun 18, 2012 1:18 am

Hi there, I just started to use android with phidgets. I ussed to have a galaxy tab and an SBC, controlling things wireless based on the InterfaceKitExample, but now im trying to use my Advance Servo Phidget to control a robot I'm building and I'm attempting to use my tablet (Toshiba thrive). I have made some apps for my own use in my tablet and work great, but when it comes to using phidgets through USB i've experienced some dificulties. Im atampting to move a servo with a seek bar. I get to attach the phidget but, i cant get the servo to move. I´ve been using the setPosition( numServo degrees) with no luck.
Any help would be greatly thanked

The code
package com.phidgets.InterfaceKitUSB;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.phidgets.AdvancedServoPhidget;
import com.phidgets.Phidget;
import com.phidgets.PhidgetException;
import com.phidgets.event.AttachEvent;
import com.phidgets.event.AttachListener;
import com.phidgets.event.DetachEvent;
import com.phidgets.event.DetachListener;

public class InterfaceKitUSBExampleActivity extends Activity implements OnSeekBarChangeListener {
AdvancedServoPhidget asp;



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


try
{
com.phidgets.usb.Manager.Initialize(this);
asp = new AdvancedServoPhidget();
asp.addAttachListener(new AttachListener() {
public void attached(final AttachEvent ae) {
AttachDetachRunnable handler = new AttachDetachRunnable(ae.getSource(), true);
synchronized(handler)
{
runOnUiThread(handler);
try {
handler.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
asp.addDetachListener(new DetachListener() {
public void detached(final DetachEvent ae) {
AttachDetachRunnable handler = new AttachDetachRunnable(ae.getSource(), false);
synchronized(handler)
{
runOnUiThread(handler);
try {
handler.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
asp.openAny();
asp.waitForAttachment();
//String poss =getString((int) asp.getPosition(0));

Toast.makeText(InterfaceKitUSBExampleActivity.this, "poss", Toast.LENGTH_SHORT).show();
asp.setPositionMax(0, 180);
asp.setPositionMin(0, 0);
asp.setVelocityLimit(0, 6300);


SeekBar seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
seekBar1.setOnSeekBarChangeListener(this);

for(int i=0; i<9;i++)
{
asp.setPosition(i, 90);

}
}
catch (PhidgetException pe)
{
pe.printStackTrace();
}
try {
if(asp.isAttached()){
try {
asp.setPosition(0, 90);
} catch (PhidgetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (PhidgetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
try {
asp.close();
} catch (PhidgetException e) {
e.printStackTrace();
}
com.phidgets.usb.Manager.Uninitialize();
}



class AttachDetachRunnable implements Runnable {
Phidget phidget;
boolean attach;
public AttachDetachRunnable(Phidget phidget, boolean attach)
{
this.phidget = phidget;
this.attach = attach;
}
public void run() {
TextView attachedTxt = (TextView) findViewById(R.id.attachedTxt);
if(attach)
{
attachedTxt.setText("Attached");
try {
TextView nameTxt = (TextView) findViewById(R.id.nameTxt);
TextView serialTxt = (TextView) findViewById(R.id.serialTxt);
TextView versionTxt = (TextView) findViewById(R.id.versionTxt);
TextView labelTxt = (TextView) findViewById(R.id.labelTxt);

nameTxt.setText(phidget.getDeviceName());
serialTxt.setText(Integer.toString(phidget.getSerialNumber()));
versionTxt.setText(Integer.toString(phidget.getDeviceVersion()));

labelTxt.setText(phidget.getDeviceLabel());



} catch (PhidgetException e) {
e.printStackTrace();
}
}
else
attachedTxt.setText("Detached");
//notify that we're done
synchronized(this)
{
this.notify();
}
}
}



@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
try {
asp.setPosition(0, 90);
} catch (PhidgetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

}

Return to “Android”

Who is online

Users browsing this forum: No registered users and 1 guest