What is fetchSample doing?

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

ruperty
Advanced Member
Posts: 161
Joined: Sun Oct 14, 2007 11:21 am

What is fetchSample doing?

Postby ruperty » Sun Jul 13, 2014 3:01 pm

What is fetchSample doing?

If you have code like this, for the accelerometer, for example, I am assuming that the sample size is 3, for the x,y,z values.

Code: Select all

HiTechnicAccelerometer sensor = new HiTechnicAccelerometer(LocalEV3.get().getPort(port));
SampleProvider sp=sensor.getAccelerationMode();
float[] sample = new float[sp.sampleSize()];
sp.fetchSample(sample, 0);

x= sample[0];
y= sample[1];
z= sample[2];


So fetchSample fills out the array with the 3 values, which can then be accessed from the array.

If so, why is the second parameter for fecthSample required?

If not, what is wrong with my assumptions?
Regards,
Rupert
www.perceptualrobots.com

User avatar
gloomyandy
leJOS Team Member
Posts: 4758
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: What is fetchSample doing?

Postby gloomyandy » Sun Jul 13, 2014 3:47 pm

The second argument allows you to specify an offset into the array at which to start storing the data. In general in Java it is good practice to do this, it basically makes up for the lack of pointers in Java compared to C and makes some operations easier/more efficient. Think of for example having 3 single axis accelerometers that you wish to store the values in locations 0, 1, 2 of an array to simulate a 3 axis device. The offset allows for this without having to copy data about.


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 1 guest