C ++ Ünite Kitaplığı

WPILib is coupled with a Units library for C++ teams. This library leverages the C++ type system to enforce proper dimensionality for method parameters, automatically perform unit conversions, and even allow users to define arbitrary defined unit types. Since the C++ type system is enforced at compile-time, the library has essentially no runtime cost.

Ünite Kitaplığını Kullanma

Birim kitaplığı, yalnızca başlık içeren bir kitaplıktır. Kullanmak istediğiniz birimler için kaynak dosyalarınıza ilgili başlığı eklemelisiniz. İşte mevcut birimlerin listesi.

#include <units/acceleration.h>
#include <units/angle.h>
#include <units/angular_acceleration.h>
#include <units/angular_velocity.h>
#include <units/area.h>
#include <units/capacitance.h>
#include <units/charge.h>
#include <units/concentration.h>
#include <units/conductance.h>
#include <units/current.h>
#include <units/curvature.h>
#include <units/data.h>
#include <units/data_transfer_rate.h>
#include <units/density.h>
#include <units/dimensionless.h>
#include <units/energy.h>
#include <units/force.h>
#include <units/frequency.h>
#include <units/illuminance.h>
#include <units/impedance.h>
#include <units/inductance.h>
#include <units/length.h>
#include <units/luminous_flux.h>
#include <units/luminous_intensity.h>
#include <units/magnetic_field_strength.h>
#include <units/magnetic_flux.h>
#include <units/mass.h>
#include <units/moment_of_inertia.h>
#include <units/power.h>
#include <units/pressure.h>
#include <units/radiation.h>
#include <units/solid_angle.h>
#include <units/substance.h>
#include <units/temperature.h>
#include <units/time.h>
#include <units/torque.h>
#include <units/velocity.h>
#include <units/voltage.h>
#include <units/volume.h>

units/math.h başlığı, units::math::abs() gibi birime duyarlı işlevler sağlar.

Ünite Tipleri ve Taşıyıcı Sınıf Tipleri

C ++ ünite kitaplığı iki tür tür tanımına dayanır: birim türleri ve kapsayıcı türleri.

Ünite Tipleri

Birim türleri, herhangi bir gerçek depolanmış değer olmaksızın soyut bir birim kavramına karşılık gelir. Birim türleri, birimler kitaplığının temel “building block-yapı taşı” dır , az sayıda “basic-temel” birim türünden (örneğin `` metre `` , `` saniye `` vb.) yapıcı bir şekilde (`` compound_unit-birleşik_birim`` şablonu kullanılarak) tanımlanır.

While unit types cannot contain numerical values, their use in building other unit types means that when a type or method uses a template parameter to specify its dimensionality, that parameter will be a unit type.

Container-Taşıyıcı Sınıf Türleri

Container-Taşıyıcı Sınıf türleri, bir birime göre boyutlandırılmış gerçek bir miktara karşılık gelir - yani, bunlar gerçekte sayısal değeri tutan şeydir. Container-Taşıyıcı Sınıf türleri, unit_t şablonuna sahip birim türlerinden oluşturulur. Çoğu birim türünün son eki _t ile aynı adı taşıyan karşılık gelen bir kap türü vardır - örneğin, birim türü units::meter, konteyner türü units::meter_t ye karşılık gelir.

Bir birimin belirli bir miktarı kullanıldığında (değişken veya yöntem parametresi olarak), bu, Container-Taşıyıcı Sınıf türünün bir örneği olacaktır. Varsayılan olarak, Container-Taşıyıcı Sınıf türleri gerçek değeri `` double`-çift`` olarak depolar - ileri düzey kullanıcılar bunu manuel olarak unit_t şablonunu çağırarak değiştirebilir.

A full list of unit and container types can be found in the documentation.

Ünite Örnekleri Oluşturma

Belirli bir ünitenin örneğini oluşturmak için, container türünün bir örneğini oluşturuyoruz:

// The variable speed has a value of 5 meters per second.
units::meters_per_second_t speed{5.0};

Alternatively, the units library has type literals defined for some of the more common container types. These can be used in conjunction with type inference via auto to define a unit more succinctly:

// The variable speed has a value of 5 meters per second.
auto speed = 5_mps;

Türler birbirleri arasında dönüştürülebildiği sürece, birimler başka bir container türünün değeri kullanılarak da başlatılabilir. Örneğin, bir foot_t değerinden bir meter_t değeri oluşturulabilir.

auto feet = 6_ft;
units::meter_t meters{feet};

Aslında, dönüştürülebilir birim türlerini temsil eden tüm kap türleri implicitly convertible. Bu nedenle, aşağıdakiler tamamen doğrudur:

units::meter_t distance = 6_ft;

Kısacası, kodumuzun herhangi bir yerinde * herhangi bir -any other* uzunluk birimi * yerine * herhangi-any * uzunluk birimini kullanabiliriz; birimler kitaplığı bizim için otomatik olarak doğru dönüşümü gerçekleştirecektir.

Hesap Yapma Ünitesi

Container-Taşıyıcı türleri, işlemin * boyutsal olarak * sağlam olması gerektiği ek koşuluyla, temel alınan veri türlerinin tüm sıradan aritmetik işlemlerini destekler. Bu nedenle, ekleme her zaman iki uyumlu container türünde yapılmalıdır:

// Add two meter_t values together
auto sum = 5_m + 7_m; // sum is 12_m
// Adds meters to feet; both are length, so this is fine
auto sum = 5_m + 7_ft;
// Tries to add a meter_t to a second_t, will throw a compile-time error
auto sum = 5_m + 7_s;

Çarpma, herhangi bir kap türü çiftinde gerçekleştirilebilir ve bir bileşik birimin kap türünü verir:

Not

When a calculation yields a compound unit type, this type will only be checked for validity at the point of operation if the result type is specified explicitly. If auto is used, this check will not occur. For example, when we divide distance by time, we may want to ensure the result is, indeed, a velocity (i.e. units::meters_per_second_t). If the return type is declared as auto, this check will not be made.

// Multiply two meter_t values, result is square_meter_t
auto product = 5_m * 7_m; // product is 35_sq_m
// Divide a meter_t value by a second_t, result is a meter_per_second_t
units::meters_per_second_t speed = 6_m / 0.5_s; // speed is 12_mps

Fonksiyonlar

Bazı standart işlevler (`` kelepçe-clamp`` gibi), aritmetik işlemlerin gerçekleştirilebileceği herhangi bir türü kabul edecek şekilde şablonlanmıştır. container-konteyner türleri olarak depolanan miktarlar bu işlevlerle sorunsuz çalışacaktır.

Bununla birlikte, diğer standart işlevleri yalnızca sıradan sayısal türlerde çalışır (örneğin, double). Birim kitaplığının `` units :: math`` ad alanı, birimleri kabul eden bu işlevlerin birçoğu için sarmalayıcılar içerir. Bu tür işlevlere örnek olarak `` sqrt ``, `` pow ``,vb. verilebilir.

auto area = 36_sq_m;
units::meter_t sideLength = units::math::sqrt(area);

Ünite/Birim Sargısının Çıkarılması

To convert a container type to its underlying value, use the value() method. This serves as an escape hatch from the units type system, which should be used only when necessary.

units::meter_t distance = 6.5_m;
double distanceMeters = distance.value();

WPILib Kodundaki Ünite Kitaplığı Örneği

WPILib’in yeni özelliklerindeki yöntemler için çeşitli argümanlar (ör kinematics) Birimler kitaplığını kullanır. İşte bir örnek yörüngeyi örneklemek.

// Sample the trajectory at 1.2 seconds. This represents where the robot
// should be after 1.2 seconds of traversal.
Trajectory::State point = trajectory.Sample(1.2_s);
// Since units of time are implicitly convertible, this is exactly equivalent to the above code
Trajectory::State point = trajectory.Sample(1200_ms);

Bazı WPILib sınıfları, birden çok birim türü seçeneğiyle doğal olarak çalışabilen nesneleri temsil eder - örneğin, bir hareket profili doğrusal mesafede (ör. Metre) veya açısal mesafede (ör. Radyan) çalışabilir. Bu tür sınıflar için, birim türü şablon parametresi olarak gereklidir:

// Creates a new set of trapezoidal motion profile constraints
// Max velocity of 10 meters per second
// Max acceleration of 20 meters per second squared
frc::TrapezoidProfile<units::meters>::Constraints{10_mps, 20_mps_sq};
// Creates a new set of trapezoidal motion profile constraints
// Max velocity of 10 radians per second
// Max acceleration of 20 radians per second squared
frc::TrapezoidProfile<units::radians>::Constraints{10_rad_per_s, 20__rad_per_s / 1_s};

For more detailed documentation, please visit the official GitHub page for the units library.