Utility Functions

Utility Functions

Các công cụ tiện ích giúp giảm code lặp và tăng tính rõ ràng khi lập trình.

Các công cụ tiện ích giúp giảm code lặp và tăng tính rõ ràng khi lập trình.

Level

Intermediate

Source

Source

Author

Author

FTC Lib

FTC Lib

Translator

Translator

FTC26749 aDudu

FTC26749 aDudu

Date Published

Date Published

Jan 18, 2026

Jan 18, 2026

package com.arcrobotics.ftclib.util;
package com.arcrobotics.ftclib.util;

FTCLib đi kèm với nhiều hàm tiện ích (Utility Functions) khác nhau:

  • Look Up Tables

  • Timing Functions

  • Math Utilities

  • Directional Enums

Look Up Tables

What is a Look Up Table?

Look up table hay viết tắt là LUT, được dùng để lưu trữ các giá trị và cho phép truy xuất chúng một cách nhanh chóng.

FTCLib cung cấp 2 biến thể khác nhau của look up table. Trong trò chơi năm nay, chúng có thể được dùng để lưu trữ các vận tốc hoặc góc đã được thiết lập và kiểm nghiệm. Bạn có thể hoặc là lấy giá trị tham chiếu gần nhất, hoặc là nội suy giữa các giá trị đó.

LUT (Look Up Table)

Lớp này cung cấp một cách để lưu trữ các giá trị trong một bảng nhằm truy xuất nhanh chóng. Ví dụ, nó có thể được dùng để lưu các vận tốc hoặc góc khác nhau dựa trên những khoảng cách nhất định. Lớp này cho phép bạn tìm giá trị gần nhất (closest entry) với đầu vào.

Ví dụ nếu bạn nhập:

Input

Output

0

0

1

1

2

1

Khi bạn yêu cầu giá trị 1.1, nó sẽ trả về 1.

Example Usage:

import com.arcrobotics.ftclib.util.LUT;
LUT<Double, Double> speeds = new LUT<Double, Double>()
{{
    add(5.0, 1.0);
    add(4.0, 0.9);
    add(3.0, 0.75);
    add(2.0, 0.5);
    add(1.0, 0.2);
}};
double distance = odometry.getPose().getTranslation().getDistance(new Translation2d(5, 10));
shooter.set(speeds.getClosest(distance));
import com.arcrobotics.ftclib.util.LUT;
LUT<Double, Double> speeds = new LUT<Double, Double>()
{{
    add(5.0, 1.0);
    add(4.0, 0.9);
    add(3.0, 0.75);
    add(2.0, 0.5);
    add(1.0, 0.2);
}};
double distance = odometry.getPose().getTranslation().getDistance(new Translation2d(5, 10));
shooter.set(speeds.getClosest(distance));

InterpLUT (Interpolated Look Up Table)

InterpLUT cung cấp một cách để lấp đầy các khoảng trống trong dữ liệu. Tương tự như LUT ở trên, lớp này cho phép bạn thêm các điểm dữ liệu và truy xuất một điểm dữ liệu tương ứng với đầu vào.

Sự khác biệt giữa LUT thông thườngInterpLUT là LUT nội suy sử dụng toán học (math) để lấp đầy tất cả các khoảng trống, về bản chất là tạo ra dữ liệu bổ sung dựa trên các dữ liệu xung quanh.

Example Usage:

import com.arcrobotics.ftclib.util.InterpLUT;
//Init the Look up table
InterpLUT lut = new InterpLUT();
//Adding each val with a key
lut.add(1.1, 0.2);
lut.add(2.7, .5);
lut.add(3.6, 0.75);
lut.add(4.1, 0.9);
lut.add(5, 1);
//generating final equation
lut.createLUT();
double distance = odometry.getPose().getTranslation().getDistance(new Translation2d(5, 10));
shooter.set(lut.get(distance));
//getting the velo required and passing it to the shooter.
import com.arcrobotics.ftclib.util.InterpLUT;
//Init the Look up table
InterpLUT lut = new InterpLUT();
//Adding each val with a key
lut.add(1.1, 0.2);
lut.add(2.7, .5);
lut.add(3.6, 0.75);
lut.add(4.1, 0.9);
lut.add(5, 1);
//generating final equation
lut.createLUT();
double distance = odometry.getPose().getTranslation().getDistance(new Translation2d(5, 10));
shooter.set(lut.get(distance));
//getting the velo required and passing it to the shooter.

Timing Functions

FTCLib đi kèm với nhiều bộ đếm thời gian (timers)hàm thời gian (Timing Functions). Chúng cho phép bạn thiết lập độ dài, đơn vị thời gian, có thể hoạt động như một đồng hồ bấm giờ (stopwatch) hoặc thậm chí trả về thời gian vòng lặp (loop time).

Timer

Một Timer có thể được tạo với một độ dài, hoặc độ dài kèm theo đơn vị thời gian (Time Unit). Các hàm khác nhau được chứa trong đối tượng Timer bao gồm:

Function

Return Type

Description

timer.start()

Void

Bắt đầu Timer

timer.pause()

Void

Tạm dừng Timer

timer.resume()

Void

Tiếp tục Timer

timer.elapsedTime()

long

Trả về thời gian đã trôi qua

timer.remainingTime()

long

Trả về thời gian còn lại

timer.done()

Boolean

Trả về Timer đã hoàn thành hay chưa

timer.isTimerOn()

Boolean

Trả về Timer có đang hoạt động hay không

Math Utilities

Hiện tại, FTCLib cung cấp 1 tiện ích toán học (math utility)clamp. Hàm này cho phép bạn giới hạn một giá trị trong một giá trị nhỏ nhất (min)giá trị lớn nhất (max), và có thể dùng cho cả doubleint.

Example Usage:

Double Method:

import com.arcrobotics.ftclib.util;
double ValueToClamp;
double LowestPossibleValue;
double HighestPossibleValue;
double OutputVal = clamp(ValueToClamp,
                         LowestPossibleValue,
                         HighestPossibleValue);
import com.arcrobotics.ftclib.util;
double ValueToClamp;
double LowestPossibleValue;
double HighestPossibleValue;
double OutputVal = clamp(ValueToClamp,
                         LowestPossibleValue,
                         HighestPossibleValue);

Int Method:

import com.arcrobotics.ftclib.util;
int ValueToClamp;
int LowestPossibleValue;
int HighestPossibleValue;
int OutputVal = clamp(ValueToClamp,
                         LowestPossibleValue,
                         HighestPossibleValue);
import com.arcrobotics.ftclib.util;
int ValueToClamp;
int LowestPossibleValue;
int HighestPossibleValue;
int OutputVal = clamp(ValueToClamp,
                         LowestPossibleValue,
                         HighestPossibleValue);

Directional Enums

FTCLib cung cấp nhiều enum hướng (directional enums) để đáp ứng mọi nhu cầu về hướng! Bạn có thể sử dụng chúng cho Autonomous, TeleOP States hoặc bất cứ mục đích nào bạn muốn.

Direction

Index

LEFT

0

RIGHT

1

UP

2

DOWN

3

FORWARD

4

BACKWARDS

5

ADUDU

A proud team of passionate Robotics Enthusiasts competing in nation-wide Technology competitions in Vietnam, the FIRST Tech Challenge and the FIRST Robotics Competition.

Copyright ©

, all rights reserved

Made by aDudu's Programming Department

made by aDudu

made by aDudu

Utility Functions