Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Super Contributor
lyon819
Posts: 253
Registered: ‎08-19-2010
My Device: BB10, Playbook, 9900, 9800, 9700, 9550, 9530, 9500, 8700.
My Carrier: TT

Re: How to get previous data point such as acceleration, GPS, or time??

if(paused==2){
                            for (i =0; i < 2; i++){
                                sensor_event_get_xyz(event, &force_x[i], &force_y[i], &force_z[i]);
                                if(force_x[0] != force_x[1]){
                                    ftime(&tmb[i]);
                                    fprintf(f,"%ld%d, %7.3f, %7.3f, %7.3f\n", tmb[i].time, tmb[i].millitm, force_x[i], force_y[i], force_z[i]);
                                }
                            }
                        }

 

Result shows accelerationX, Y, Z. Expected result will be when the previous acceleration is the same as the current acceleration, the acceleration will not print out.

That is, no overlay data in the result.

and,

 

I tried to get STD acceleration X as well.

Therefore, I need to  get accelration[0], acceleration[1],.....acceleration [50], which depends on frequency.

 

But,

I only can get the current acceleration.

 

-Fight with BB10
Please use plain text.
BlackBerry Development Advisor (Retired)
gbeukeboom
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: How to get previous data point such as acceleration, GPS, or time??

In your loop you're referencing the same "event" object, so the acceleration being retrieved in both iterations would be expected to be the same. You need to fill your array as new sensor events are read in.

 

Garett
@garettBeuk
--
Goodbye everybody!
Please use plain text.
BlackBerry Development Advisor
smcveigh
Posts: 660
Registered: ‎11-29-2011
My Device: developer
My Carrier: other

Re: How to get previous data point such as acceleration, GPS, or time??

[ Edited ]

Your code below is broken because you are calling sensor_event_get_xyz() in a loop using the same event over and over.  You need to rearrange your loop to encompass the whole event polling block in your code.

 

                            for (i =0; i < 2; i++){
sensor_event_get_xyz(event, &force_x[i], &force_y[i], &force_z[i]);
if(force_x[0] != force_x[1]){
ftime(&tmb[i]);
fprintf(f,"%ld%d, %7.3f, %7.3f, %7.3f\n", tmb[i].time, tmb[i].millitm, force_x[i], force_y[i], force_z[i]);
}
}

 

As mentioned by others, you will need to do your own caching of "last" acceleration data.  Don't try to cache events, but instead cache the output of sensor_event_get_xyz().

 

Cheers,

Sean

 

EDIT: sorry for the dupe.. just saw Garett's reply.

Please use plain text.
Super Contributor
lyon819
Posts: 253
Registered: ‎08-19-2010
My Device: BB10, Playbook, 9900, 9800, 9700, 9550, 9530, 9500, 8700.
My Carrier: TT

Re: How to get previous data point such as acceleration, GPS, or time??

Thank you guys,

The loop is a issue for sure.

But,

After I change to a correct loop, no acceleration result.

 

I think that the problem is force_x[0] and force_x[1].

But, I don't know how to correct it.

 

 

if(paused==2){
                            for (i =0; i < 2; i++){
                                sensor_event_get_xyz(event, &force_x[i], &force_y[i], &force_z[i]);
                            }
                            if(force_x[0] != force_x[1]){

                                //if(tmb[0].millitm-tmb[1].millitm > 19){fail without data

                                ftime(&tmb[i]);
                                fprintf(f,"%ld%d, %7.3f, %7.3f, %7.3f\n", tmb[i].time, tmb[i].millitm, force_x[i], force_y[i], force_z[i]);
                            }
                        }

 

Thank your attention and suggestion.

 

 

-Fight with BB10
Please use plain text.
BlackBerry Development Advisor (Retired)
gbeukeboom
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: How to get previous data point such as acceleration, GPS, or time??

 for (i =0; i < 2; i++){
                                sensor_event_get_xyz(event, &force_x[i], &force_y[i], &force_z[i]);
                            }

 The code still loops on the same event instance, you will need to wait for the sensor events to propogate naturally and check the events as they are created.

 

ex (from the sample):

 

float force_x, force_y, force_z;

bps_initialize();

if (!sensor_is_supported(SENSOR_TYPE_ACCELEROMETER)) {

        printf("Accelerometer not supported by device!");
        bps_shutdown();
        return EXIT_FAILURE;
}

sensor_set_rate(SENSOR_TYPE_ACCELEROMETER, ACCELEROMETER_RATE);
sensor_set_skip_duplicates(SENSOR_TYPE_ACCELEROMETER, true);
sensor_request_events(SENSOR_TYPE_ACCELEROMETER);

while (!shutdown) {
		bps_event_t *event = NULL;
		bps_get_event(&event, 0);


		if (event) {
			if (bps_event_get_domain(event) == sensor_get_domain()) {
	    if (SENSOR_ACCELEROMETER_READING == bps_event_get_code(event)) {
       sensor_event_get_xyz(event,&force_x,&force_y,&force_z);

       // call your method from here to store the event force values once (for array[n]), the
       // next time an event is thrown you can store it to array[n+1] and compare with array[n]
			  }
		 } 
	 }
}

 

Garett
@garettBeuk
--
Goodbye everybody!
Please use plain text.
BlackBerry Development Advisor
smcveigh
Posts: 660
Registered: ‎11-29-2011
My Device: developer
My Carrier: other

Re: How to get previous data point such as acceleration, GPS, or time??

[ Edited ]

No.  The problem is you are still calling sensor_event_get_xyz() with the same event!

 

Unroll your loop and you get:

 

sensor_event_get_xyx(event, &force_x[0], &force_y[0], &force_z[0]);

sensor_event_get_xyz(event, &force_x[1], &force_y[1], &force_z[1]);

 

Since you are passing the same event in BOTH TIMES, you are getting identical results in force_x[0] and force_x[1].  You need to go back to your event polling loop before calling sensor_event_get_xyz() again.  sensor_event_get_xyz() does not get new events.

 

Cheers,

Sean

 

 

EDIT:  Again, sorry for the dupe!  This one's all your Garett :smileyhappy:

Please use plain text.
Super Contributor
lyon819
Posts: 253
Registered: ‎08-19-2010
My Device: BB10, Playbook, 9900, 9800, 9700, 9550, 9530, 9500, 8700.
My Carrier: TT

Re: How to get previous data point such as acceleration, GPS, or time??

Finally, I got point.

Solved. Thank you guys.

 

the code is at below.

if(paused==2){
for (i =0; i < 2; i++){
//if (SENSOR_ACCELEROMETER_READING == bps_event_get_code(event)) { the line will cause double printint issue
delay( 20 ); /* delay for 50 Hz */
sensor_event_get_xyz(event, &force_x[i], &force_y[i], &force_z[i]);
ftime(&tmb[i]);
//}
}
if (a > 99){
fprintf(f,"%ld%d, %d, %7.3f, %7.3f, %7.3f\n", tmb[0].time, tmb[0].millitm, force_x[0], force_y[0], force_z[0]);
}else if (a <= 99 && a >= 10){
fprintf(f,"%ld0%d, %d, %7.3f, %7.3f, %7.3f\n", tmb[0].time, tmb[0].millitm, force_x[0], force_y[0], force_z[0]);
}else if (a >= 1 && a < 10){
fprintf(f,"%ld00%d, %d, %7.3f, %7.3f, %7.3f\n", tmb[0].time, tmb[0].millitm, force_x[0], force_y[0], force_z[0]);
}else if (a =0){
fprintf(f,"%ld000%d, %d, %7.3f, %7.3f, %7.3f\n", tmb[0].time, tmb[0].millitm, force_x[0], force_y[0], force_z[0]);
}
if (c > 99){
fprintf(f,"%ld%d, %d, %7.3f, %7.3f, %7.3f\n", tmb[0].time, tmb[1].millitm, force_x[0], force_y[0], force_z[0]);
}else if (c <= 99 && c >= 10){
fprintf(f,"%ld0%d, %d, %7.3f, %7.3f, %7.3f\n", tmb[0].time, tmb[1].millitm, force_x[0], force_y[0], force_z[0]);
}else if (c >=1 && c < 10){
fprintf(f,"%ld00%d, %d, %7.3f, %7.3f, %7.3f\n", tmb[0].time, tmb[1].millitm, force_x[0], force_y[0], force_z[0]);
}else if (c =0){
fprintf(f,"%ld000%d, %d, %7.3f, %7.3f, %7.3f\n", tmb[0].time, tmb[1].millitm, force_x[0], force_y[0], force_z[0]);
}
}

-Fight with BB10
Please use plain text.