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
Contributor
Posts: 19
Registered: ‎06-21-2012
My Device: z10
My Carrier: Bell

Issue with gyro sensor reading vs range

Hi, I am wirting an simple app that can take gyro reading and put reading into a data file.

It works, but i fell like the data i collected may be wrong. The thing is that I foud out my range should from-2000 to 2000, but my gyro readings are between -2 to 2. I dont really understand why. Did i do wrong or it is supposed to be.Below is part of my code. Thanks.

_______________________________________________________________________________________

SensorBpsEventHandler:Smiley FrustratedensorBpsEventHandler()
{
    m_isMeasurement = false;

    x = 0.0;
    y = 0.0;
    z = 0.0;

    bps_initialize();
    cout << "----------------------------------" << endl;
    if(sensor_is_supported(SENSOR_TYPE_GYROSCOPE))
    {
        int res = BPS_FAILURE;
        cout << "SensorBpsEventHandler: SENSOR_TYPE_gyro is supported!" << endl;
        subscribe(sensor_get_domain());
        sensor_request_events(SENSOR_TYPE_GYROSCOPE);
        //sensor_set_skip_duplicates(SENSOR_TYPE_LIGHT, true);

//        unsigned int new_rate = 140000;
//        res = sensor_set_rate(SENSOR_TYPE_LIGHT, new_rate);
//        if(BPS_SUCCESS == res)
//        {
//            cout << "SensorBpsEventHandler: SENSOR_TYPE_LIGHT: successfully set delay to " << new_rate << " microseconds" << endl;
//        }
//        else
//        {
//            cout << "SensorBpsEventHandler: SENSOR_TYPE_LIGHT: cannot set delay to " << new_rate << " microseconds" << endl;
//        }

        sensor_info_t* p_sinfo = NULL;
        res = sensor_info(SENSOR_TYPE_GYROSCOPE, &p_sinfo);
        if(BPS_SUCCESS == res && NULL != p_sinfo)
        {
            float resolution = sensor_info_get_resolution(p_sinfo);
            float rmin = sensor_info_get_range_minimum(p_sinfo);
            float rmax = sensor_info_get_range_maximum(p_sinfo);
            unsigned int dmin =  sensor_info_get_delay_minimum(p_sinfo);
            unsigned int dmax = sensor_info_get_delay_maximum(p_sinfo);
            unsigned int ddef = sensor_info_get_delay_default(p_sinfo);
            float power = sensor_info_get_power(p_sinfo);

            cout << "resolution = " << resolution << endl;
            cout << "range_minimum = " << rmin << endl;
            cout << "range_maximum = " << rmax << endl;
            cout << "delay_minimum = " << dmin << " microseconds" << endl;
            cout << "delay_maximum = " << dmax << " microseconds" << endl;
            cout << "delay_default = " << ddef << " microseconds" << endl;
            cout << "The power consumed by the sensor = " << power << endl;

            m_max = m_current= rmax;

            sensor_info_destroy(p_sinfo);
        }
        else
        {
            cout << "SensorBpsEventHandler: cannot extract sensor_info for SENSOR_TYPE_ACC! return value = " << res << endl;
        }
    }
    else
    {
        cout << "SensorBpsEventHandler: SENSOR_TYPE_ACC isn't supported!" << endl;
    }


    cout << "----------------------------------" << endl;
}

SensorBpsEventHandler::~SensorBpsEventHandler()
{
    sensor_stop_events(SENSOR_TYPE_GYROSCOPE);
    bps_shutdown();
}

void SensorBpsEventHandler::event( bps_event_t *event ) {
//    cout << "SensorBpsEventHandler::event" << endl;
    FILE *fp;    //the blank position of file pointer
    size_t size;
    fp=fopen("shared/documents/gyro.csv", "a");//open or create a csv file
    if (fp == NULL)    //check if has file open error
       printf("I couldn't open for writing.\n");
    else{
    }
    if (bps_event_get_domain(event) == sensor_get_domain())//reading sensor
    {
           uint16_t code = bps_event_get_code(event);
          if (code == SENSOR_GYROSCOPE_READING)
          {
             // Keyboard is now visible. Handle event...
//             cout << "SENSOR_LIGHT_READING event..." << endl;

             sensor_event_get_xyz(event,&x,&y,&z);
             uint64_t flags = sensor_event_get_flags(event);
             sensor_accuracy_t acc = sensor_event_get_accuracy(event);
             uint64_t ts = sensor_event_get_timestamp(event);

             if(m_isMeasurement)//there is always a measurement
             {
                 //cout << ts/1000000 << "ms"<<setw(15);
                 //printf("%lu ms", ts/1000000);
                cout<<"Printing"<<endl;
                 fprintf(fp,"%.9f,%.9f ,%.9f ", x,y,z);//x y z reading in the same row

                 fprintf(fp,"\n");    // next line

             }

                 m_isMeasurement = true;

          }
          else
          {
              cout << "Received event # " << code << endl;
          }
    }

    else
    {
        cout << "Received unknown domain event" << endl;
    }
    fclose(fp);
}

 

Developer
Posts: 36
Registered: ‎01-16-2013
My Device: Z10
My Carrier: Telus

Re: Issue with gyro sensor reading vs range

Ya, just by shaking the phone in my hand I get around 2.  Maybe if you bolt the phone to your car tire and accellerate hard on the highway you'll get higher values...   my guess that max of 2000 is what the sensor will not give over, no matter what happens.   It's a float value, so you can scale it to whatever you want.

 

As a side note, I work with sensors on my app and I found it's very useful to go on blackberry World and download a bunch of "sensor" apps that display the sensors in nice line graphs so I get a feel for how they work.