C++Builder What Is An Accelerometer And How To Use It in C++?

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
What Is An Accelerometer And How To Use It in C++?
By Yilmaz Yoru January 5, 2022

What is a motion sensor? What is an accelerometer? Which Для просмотра ссылки Войди или Зарегистрируйся supports the use of motion sensors? How can I use a motion sensor in my C++ code? What is the difference between a motion detector and motion sensor? How can I detect the motion of a device? How can I measure the acceleration of device? Let’s answer these questions.

What is a motion sensor?​

Strictly speaking, a Для просмотра ссылки Войди или Зарегистрируйся, sometimes known as a motion sensor, is an electrical/electronic device that utilizes special hardware to detect motion of an object or objects. Generally it detects the motion nearby. Such a device is often integrated as a component of a system that automatically performs a task or alerts a user of motion in an area. These kinds of sensors are generally used for security and things like automated lighting control, home control, energy efficiency, and other useful systems.

A Motion Sensor – rather than detector – is generally used for the motion sensors included in devices such as mobile devices, IoT equipment and so on. It allows programs to detect motion of the device rather than motion near to the device. These sensors are generally use a type of hardware technology called an accelerometer. Because of this they are often called Acceloremeters. It is able to detect acceleration of the device in X Y Z directions. If you code can measure the acceleration in the X Y and Z directions, you can then detect the velocity and motion of the device.

What is TMotionSensor in C++?​

In C++ Builder, Motion Sensor (Для просмотра ссылки Войди или Зарегистрируйся) is a class wrapper for the Для просмотра ссылки Войди или Зарегистрируйся that receives data from the sensor (or sensors) of device like the acceleration in X,Y, and Z directions and angular acceleration in the X, Y and Z axes.

TMotionSensor or TCustomMotionSensor, offer information about the acceleration, angle, state, and speed of the device’s motion.
All TMotionSensor Properties can be found Для просмотра ссылки Войди или Зарегистрируйся and Methods can be found Для просмотра ссылки Войди или Зарегистрируйся

How to use TMotionSensor in C++ Builder?​

Rad Studio, C++ Builder comes with Mobile Snippets in C++ that can be found in the “C:\Users\Public\Documents\Embarcadero\Studio\22.0\Samples\CPP\Mobile Snippets\Accelerometer” folder.

The Accelerometer sample demonstrates how to use the accelerometer in iOS or Android applications. The sample uses TMotionSensor component and TCustomMotionSensor class to offer information about the acceleration, angle, state, and speed of the device motion. Note that TCustomMotionSensor class supports Android, iOS and Windows platforms. However, each platform provides different motion sensors and each type of sensor supports a different set of properties (see TCustomMotionSensor for a complete list of platform, sensors and supported properties). Some functionalities of this sample could be disabled depending on the target platform. You can find more official detailed explanations Для просмотра ссылки Войди или Зарегистрируйся.

Let’s see how we can use TMotionSensor component in C++ Builder,
  1. Create a new Multi-Device C++ Builder FireMonkey application that allows us to develop Android or iOS applications, can be used with other IoTs too. Save all projects to a folder.
  2. Drag TMotionSensor from the component palette onto Form design.
  3. You can activate this MotionSensor1 component on design time or on run time. We highly recommend to activate on run time. You should use set its Active property to true as given example below,
C++:
MotionSensor1->Active = true;
Generally before reading sensor values we should check MotionSensor1->Sensor as below,
C++:
if (MotionSensor1->Sensor)
{
   // Check and read sensor data
}
and we should check if the sensor of this device has the property that we want to measure. We should use AvailableProperties.Contains() method to check required property as below,
C++:
if (MotionSensor1->Sensor->AvailableProperties.Contains(...) )
{
 // Read the property that exists in this sensor
}
Finally we should put the property flag, for example for the X direction Acceleration we should use TCustomMotionSensor::TProperty::AccelerationX. Here is the full procedure that checks sensors, its property then gets the required value .
C++:
if (MotionSensor1->Sensor)
{
 if (MotionSensor1->Sensor->AvailableProperties.Contains(TCustomMotionSensor::TProperty::AccelerationX))
 {
 float aX = MotionSensor1->Sensor->AccelerationX;
 float aY = MotionSensor1->Sensor->AccelerationY;
 float aZ = MotionSensor1->Sensor->AccelerationZ;
 }
}
As you see we can easily read Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся and Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся properties and more properties of TMotionSensor in C++ Builder. This example works on both Android, iOS and supported IoT devices.