What is the gyroscope sensor? How can I use the gyroscope sensor in a C++ app? How can I measure the rotation of a mobile device? Let’s answer these questions.
What is the gyroscope sensor?
Gyroscope sensors, also known as Gyro Sensors, are sensors that measure rotational motion and changes in orientation of a device such as a cell phone or tablet.
There are many different types of gyro sensors: Gyrostat, Micro Electromechanical Systems (MEMS) Gyroscope, Hemispherical Resonator Gyroscope (HRG), Vibrating Structure Gyroscope (VSG) or Coriolis Vibratory Gyroscope (CVG), Dynamically Tuned Gyroscope (DTG), Ring Laser Gyroscope (RLG), Fiber Optic Gyroscope, London Moment Gyroscope. All these types and more details can be found in Wikipedia here
In the last decade, in addition to being used in compasses, aircraft and computer pointing devices, gyro sensors have been introduced into consumer electronics. The popularity of gyroscope started with the first application of the gyroscope in consumer electronics when they were included for the first time in iPhone devices. This was very quickly followed with sensors being used and widely available in nearly all mobile device and IoT brands. Now, most mobile devices have these sensors and can be used to obtain data in application developments.
Vibration gyro sensors are very popular and have found their way into camera-shake detection systems for compact video and still cameras, motion sensing for video games, and vehicle electronic stability control (anti-skid) systems, among other things.
How can I use gyro sensors in C++ Builder?
- Create a new Multi-Device C++ Builder FireMonkey application.
- Define TCustomOrientationSensor (*FSensor), TSensorArray (FSensors); TSensorManager (*FSensorManager) in the private sections of the form header as below,
1 2 3 4 5 6 |
private: // User declarations TCustomOrientationSensor * FSensor; TSensorArray FSensors; TSensorManager * FSensorManager; |
3. In the form creation or at the beginning of using the device we should attempt to get and activate the sensor manager and try to get an orientation sensor as below. For example:
1 2 3 4 5 |
FSensorManager = TSensorManager::Current; FSensorManager->Activate(); FSensors = TSensorManager::Current->GetSensorsByCategory(TSensorCategory::Orientation); |
Now we can check sensors in a loop and find we can set this sensor as FSensor as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
FSensor = NULL ; for (int i = 0; i < FSensors.Length ; i++) { if (static_cast<TCustomOrientationSensor*>(FSensors[i])->SensorType == TOrientationSensorType::Inclinometer3D) { FSensor = static_cast<TCustomOrientationSensor*>(FSensors[i]); break; } } if (FSensor==NULL) { return; // no sensors available } |
As you can see, if no sensor find we exit from the procedure. Let’s continue to add some code inside this procedure;
1 2 3 4 5 6 7 |
if (!(FSensor->Started)) { FSensor->Start(); Timer1->Enabled = true; // we can start obtain data in timer intervals } |
as a result, the finished procedure will be as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
void __fastcall TForm3D2::Form3DCreate(TObject *Sender) { // attempt to get and activate the sensor manager FSensorManager = TSensorManager::Current; FSensorManager->Activate(); // attempt to get an orientation sensor FSensors = TSensorManager::Current->GetSensorsByCategory(TSensorCategory::Orientation); FSensor = NULL ; for (int i = 0; i < FSensors.Length ; i++) { if (static_cast<TCustomOrientationSensor*>(FSensors[i])->SensorType == TOrientationSensorType::Inclinometer3D) { FSensor = static_cast<TCustomOrientationSensor*>(FSensors[i]); break; } } if (FSensor==NULL) { Label1->Text = "Gyro not found"; return; // no sensors available } //start the sensor if it is not started if (!(FSensor->Started)) { FSensor->Start(); Timer1->Enabled = true; } } |
Now we can sense data from this sensor in a timer in its intervals as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
void __fastcall TForm3D2::Timer1Timer(TObject *Sender) { if (FSensors.Length > 0) { if (FSensor) { #if defined(__ANDROID__) //In Android, Tilt property is returned as vector Rectangle3D1->RotationAngle->X = FSensor->TiltZ; Rectangle3D1->RotationAngle->Y = FSensor->TiltX * -1.0; Rectangle3D1->RotationAngle->Z = FSensor->TiltY; #else //In other platforms, Tilt property is returned as degree Rectangle3D1->RotationAngle->X = FSensor->TiltZ * -1.0; Rectangle3D1->RotationAngle->Y = FSensor->TiltX; Rectangle3D1->RotationAngle->Z = FSensor->TiltY * -1.0; #endif } } } |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition