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,
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:
Now we can check sensors in a loop and find we can set this sensor as FSensor as below,
As you can see, if no sensor find we exit from the procedure. Let’s continue to add some code inside this procedure;
as a result, the finished procedure will be as below,
Now we can sense data from this sensor in a timer in its intervals as below,
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.