01-25-2012 12:21 PM
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.
01-25-2012 12:27 PM
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.
01-25-2012 12:50 PM - edited 01-25-2012 12:52 PM
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.
01-25-2012 01:43 PM
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.
01-25-2012 01:49 PM
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_ACCELEROMET ER, 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,&forc e_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]
}
}
}
}
01-25-2012 01:50 PM - edited 01-25-2012 01:51 PM
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 ![]()
01-26-2012 03:35 PM
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]);
}
}