LeJOS GyroSensor sample always 0 - java

I would like to read the current angle from my Ev3GyroSensor, however the returned value is always 0.
I tested multiple Ports nothing worked.
If I test the sensor with the LeJOS integrated Tool, everything works fine.
EV3GyroSensor ev3GyroSensor = new EV3GyroSensor(SensorPort.S4);
SampleProvider sampleProvider = ev3GyroSensor.getAngleAndRateMode();
float[] sample = new float[sampleProvider.sampleSize()];
sampleProvider.fetchSample(sample, 0);
System.out.println(sample[0]);
//I move the robot with my hand
Delay.msDelay(3000);
sampleProvider.fetchSample(sample, 0);
System.out.println(sample[0]);
ev3GyroSensor.close();

try using only angle mode, from my experience, angle and rate mode doesn't always work correctly.
There is also a possibility that you are reading the rate from the sample, but I'm not really sure.

Related

Camera mode change to video with Zenmuse XT2 fails with error: Not Support

When I call:
camera.setMode(SettingsDefinitions.CameraMode.RECORD_VIDEO, null);
using the Zenmuse XT2 I get the error: Not Support
I have checked if the camera is storing a photo as I heard elsewhere that you cannot change mode while that is in progress, it is not.
The code works fine using the Zenmuse XT and Zenmuse X3 cameras. Is there an alternative procedure to use for the XT2?

Android graphview and data plotting timing issue?

I am currently attempting to "fix" an Android application intended to control a device over BLE. The application was written entirely by someone else, and I currently know very little Java. So if there are glaring flaws please just point them out and I'll fix them. I won't be able to give the rationale very everything you see here, but I'll do my best. While there are a myriad of problems and things to fix, my current issue is the data plotter. GraphView was implemented to create the plots. The issue is that the data plotted is not representative of the actual sampling rate. When using a separate BLE data logger I can verify that the data is being sent at the correct rate and it plots out correctly using excel.
These are created on startup.
graph1 = (GraphView) findViewById(R.id.graph1);
airPressureSeries = new LineGraphSeries<>();
fluidPressureSeries = new LineGraphSeries<>();
airPressureSeries.setDrawAsPath(true);
fluidPressureSeries.setDrawAsPath(true);
graph1.getViewport().setXAxisBoundsManual(true);
graph1.getViewport().setMinX(0);
graph1Num = 100;
graph1.getViewport().setMaxX(graph1Num);
graph2 = (GraphView) findViewById(R.id.graph2);
airTemperatureSeries = new LineGraphSeries<>();
fluidTemperatureSeries = new LineGraphSeries<>();
airTemperatureSeries.setDrawDataPoints(true);
fluidTemperatureSeries.setDrawDataPoints(true);
graph2.getViewport().setXAxisBoundsManual(true);
graph2.getViewport().setMinX(0);
graph2Num = graph1Num;
graph2.getViewport().setMaxX(graph2Num);
Then this is done when data plotting is to be shown.
plot1Dropdown.setText("Air Pressure");
graph1.addSeries(airPressureSeries);
plot1Title.setText(R.string.pressureTitle);
plot2Dropdown.setText("Fluid Pressure");
plot2Title.setText(R.string.pressureTitle);
graph2.addSeries(fluidPressureSeries);
This timer, which I don't quite understand and could be wrong, is started. They said it should run at 100Hz. The code inside it should just increment an x value for the plot and then grab a global variable which is updated elsewhere.
mTimer1 = new Runnable() {
#Override
public void run() {
graph1LastXValue += 1d;
airPressureSeries.appendData(new DataPoint(graph1LastXValue,
airPressureValue), true, graph1Num);
fluidPressureSeries.appendData(new DataPoint(graph1LastXValue,
fluidPressureValue), true, graph1Num);
graph2LastXValue += 1d;
airTemperatureSeries.appendData(new DataPoint(graph2LastXValue,
airTemperatureValue), true, graph2Num);
fluidTemperatureSeries.appendData(new DataPoint(graph2LastXValue
, fluidTemperatureValue), true, graph2Num);
mHandler.postDelayed(this, 5);
}
};
mHandler.postDelayed(mTimer1, 5);
The global variable is updated after new data has been received and parsed by an AsyncTask. In the doInBackground of the AsyncTask it assigns the parsed values to the global variables like this.
DeviceControlActivity.airPressureValue = airPressure;
DeviceControlActivity.airTemperatureValue = airTemperature;
DeviceControlActivity.fluidPressureValue = fluidPressure;
DeviceControlActivity.fluidTemperatureValue = fluidTemperature;
I have done a few things to check what's going on. I am certain that the timer is running fast enough to update the values. I am also certain that data is being received at the correct rate. I used logging to make sure of that. I also am sure that the async task in running and completing in a reasonable amount of time. In the post execute for it I told it to log the amount of time passed between pre and post execute function calls. It was consistently 1 ms which is fine for the application.
I think the issue has to do with how the values are set or retrieved. Based on the way the code inside the timer works it plots a new point even if the data has not been updated so I get regions of the plot that are all the same value for some amount of time greater than is reasonable.
Can someone point me in the right direction?

Set the initial directory in SWT FileDialog

I'm working on an Eclipse RCP project and need to let the user select some file.
For convenience, based on some conditions, the initial directory of the file choosing dialog should be set prior to opening it.
As I'm bound to Eclipse RCP / SWT, I am working with the org.eclipse.swt.widgets.FileDialog.
The documentation of this FileDialog points out to use the setFilterPath(String string)-method which should do exactly what I need (see documentation).
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setFilterExtensions(new String [] {"*.html"});
dialog.setFilterPath("c:\\temp");
String result = dialog.open();
Unfortunately it is not working, at least not "every time".
I have currently no installation to check on it, but I'm quite sure that the feature would work totally fine on a Windows 200/XP/Vista machine.
I am working with a Windows 7 machine and I think I am suffering from the behaviour described here for lpstrInitialDir.
At least, this is exactly the behaviour I am facing: The path is good the first time I open the dialog, but the second time, the path is initially set to the last chosen path.
This seems to be convenient in most cases, but it is not in mine.
Can this be right?
If so, have I any chance on changing the behaviour according to my needs?
Thanks for any helping answer!
I ran into the same problem on Windows 10 and found a solution that seems to be working for me. A code snippet from the DirectoryDialog led to the right direction:
if (filterPath != null && filterPath.length() > 0) {
String path = filterPath.replace('/', '\\');
char[] buffer = new char[path.length() + 1];
path.getChars(0, path.length(), buffer, 0);
if (COM.SHCreateItemFromParsingName(buffer, 0, COM.IID_IShellItem, ppv) == OS.S_OK) {
IShellItem psi = new IShellItem(ppv[0]);
/*
* SetDefaultDirectory does not work if the dialog has
* persisted recently used folder. The fix is to clear the
* persisted data.
*/
fileDialog.ClearClientData();
fileDialog.SetDefaultFolder(psi);
psi.Release();
}
}
The FileDialog misses this statement 'fileDialog.ClearClientData()'. My solution is to execute the following code before setting the path and open the dialog:
long [] ppv = new long [1];
if (COM.CoCreateInstance(COM.CLSID_FileOpenDialog, 0, COM.CLSCTX_INPROC_SERVER, COM.IID_IFileOpenDialog, ppv) == OS.S_OK) {
IFileDialog fileDialog = new IFileDialog(ppv[0]);
fileDialog.ClearClientData();
fileDialog.Release();
}
Now you can set the filterpath without Windows messing things up.
I found a simple Solution for the Problem you described (I had the exact same Problem).
Just rearrange the your code like this:
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setFilterPath("c:\\temp"); // This line is switched with the following line
dialog.setFilterExtensions(new String [] {"*.html"});
String result = dialog.open();
Somehow the Order of the methods called is relevant.
Are you using the same FileDialog object when you re-open it?
I ran a few quick tests and found that, if you re-set the filterPath, the dialog opens in the correct location.
If I open the same object again, it starts in the previously selected location.

How to get supported video camera resolutions in android?

I am writing an app where I am allowing the user to capture video using the phones camera. I am using my own code to record the video as opposed to Androids built in camera app.
Everything is working OK except I need to be able to access the list of supported camera resolutions so I can choose at runtime which one to use. I am looking for something like getSupportedPictureSizes() but for video. Android 3.0 has this functionality but I am looking for something for 2.2.
As of right now I am using CamcorderProfile.QUALITY_HIGH / QUALITY_LOW, but this only gives me two options and on the phones I have been testing on, the file sizes are at each extreme.(QUALITY_LOW is 216 kb/s and QUALITY_HIGH is > 3 MB/s)
Any help would be greatly appreciated,
Thank You!
Did you try to use the getSupportedVideoSizes() method from Camera.Parameters class?
public List<Camera.Size> getSupportedVideoSizes()
This method returns a list of Size objects. It will return null if the camera does not have separate preview and video output. The answer here indicates that when this returns null you may use the getSupportedPreviewSizes() list.
Ok I think I figured it out. It seems to work correctly on the phones I have been testing on.
List<Size> tmpList = camera.getParameters().getSupportedPreviewSizes();
final List<Size> sizeList = new Vector<Size>();
//compair the apsect ratio of the candidate sizes against the real ratio
Double aspectRatio = (Double.valueOf(getWindowManager().getDefaultDisplay().getHeight()) / getWindowManager().getDefaultDisplay().getWidth());
for(int i=0; i<tmpList.size(); i++){
Double tmpRatio = Double.valueOf(tmpList.get(i).height) / tmpList.get(i).width;
if(Math.abs(aspectRatio - tmpRatio) < .15){
sizeList.add(tmpList.get(i));
}
}

How to set volume of a SourceDataLine in Java

I'm trying to make an mp3 player in java and I can`t figure out how to control the volume in it.
I've tried something like this:
// Adjust the volume on the output line.
if (dataLine.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
FloatControl volume = (FloatControl) dataLine.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(100.0F);
}
Everything I have written until this code worked fine but apparently the dataLine is NOT control Supported because it jumps over this IF statement.
My question is this : Do you have any idea why this is happening and how could I work this issue around so that I could control the volume of my application?
OK GUYS,
I found my mess-up. I actually forgot to call the dataLine.open(audioFormat) function which acquires the system resources.
So the code workes just fine, in case anyone has this kind of problems too
Have you tried to see what dataLine.getControls() will return ?
Obtains the set of controls associated
with this line. Some controls may only
be available when the line is open. If
there are no controls, this method
returns an array of length 0.
If you want volume wouldn't you want to test for the FloatControl.Type.VOLUME control ?
float vol=50;
final FloatControl volumeControl = (FloatControl) auline.getControl( FloatControl.Type.MASTER_GAIN );
volumeControl.setValue( 20.0f * (float) Math.log10( vol / 100.0 ) );
vol=0 means mute.

Categories

Resources