What is a motion sensor? What is an accelerometer? Which C++ IDE 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 motion detector, 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 (TMotionSensor) is a class wrapper for the TCustomMotionSensor 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.
- AccelerationX, AccelerationY and AccelerationZ return the acceleration in gals (g) for the X,Y, and Z axis.
- AngleAccelX, AngleAccelY and AngleAccelZ return the angular acceleration in degrees per second squared (°/s²) for the X,Y, and Z axis.
- Speed determines the speed of the device in meters per second (m/s).
- Motion determines whether the device is currently in motion or not.
All TMotionSensor Properties can be found here and Methods can be found here
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 here.
Let’s see how we can use TMotionSensor component in C++ Builder,
- 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.
- Drag TMotionSensor from the component palette onto Form design.
- 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,
1 2 3 |
MotionSensor1->Active = true; |
Generally before reading sensor values we should check MotionSensor1->Sensor as below,
1 2 3 4 5 6 |
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,
1 2 3 4 5 6 |
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 .
1 2 3 4 5 6 7 8 9 10 11 |
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 AccelerationX, AccelerationY, AccelerationZ and AngleAccelX, AngleAccelY, AngleAccelZ properties and more properties of TMotionSensor in C++ Builder. This example works on both Android, iOS and supported IoT devices.
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