FRC编程快速入门
Limelight支持REST/HTTP、Websocket、Modbus和NetworkTables协议来传输目标数据、状态数据和实时配置。 可以使用JSON和原始输出格式。更多信息请参见文档的API部分。
对于FRC团队,推荐使用NetworkTables协议。Limelight以100Hz的频率将所有目标数据(包括完整的JSON数据)发送到NetworkTables。 团队也可以通过NetworkTables设置控制参数,如LED模式、裁剪窗口等。 FRC团队可以使用Limelight Lib Java和C++库来快速开始使用Limelight。Limelight Lib是最简单的入门方式。
LimelightLib:
- Java
- C++
- Python
double tx = LimelightHelpers.getTX("");
#include "LimelightHelpers.h"
double tx = LimelightHelpers::getTX("");
double ty = LimelightHelpers::getTY("");
wip
如果你想跳过LimelightLib直接使用NetworkTables编程:
- Java
- C++
- LabView
- Python
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableEntry;
import edu.wpi.first.networktables.NetworkTableInstance;
NetworkTable table = NetworkTableInstance.getDefault().getTable("limelight");
NetworkTableEntry tx = table.getEntry("tx");
NetworkTableEntry ty = table.getEntry("ty");
NetworkTableEntry ta = table.getEntry("ta");
//周期性读取值
double x = tx.getDouble(0.0);
double y = ty.getDouble(0.0);
double area = ta.getDouble(0.0);
//周期性发送到smart dashboard
SmartDashboard.putNumber("LimelightX", x);
SmartDashboard.putNumber("LimelightY", y);
SmartDashboard.putNumber("LimelightArea", area);
#include "frc/smartdashboard/Smartdashboard.h"
#include "networktables/NetworkTable.h"
#include "networktables/NetworkTableInstance.h"
#include "networktables/NetworkTableEntry.h"
#include "networktables/NetworkTableValue.h"
#include "wpi/span.h"
std::shared_ptr<nt::NetworkTable> table = nt::NetworkTableInstance::GetDefault().GetTable("limelight");
double targetOffsetAngle_Horizontal = table->GetNumber("tx",0.0);
double targetOffsetAngle_Vertical = table->GetNumber("ty",0.0);
double targetArea = table->GetNumber("ta",0.0);
double targetSkew = table->GetNumber("ts",0.0);
import cv2
import numpy as np
# runPipeline()函数会被Limelight后端每帧调用
def runPipeline(image, llrobot):
# 将输入图像转换为HSV颜色空间
img_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# 通过移除不在以下HSV最小/最大值范围内的像素
# 将HSV转换为二值图像
img_threshold = cv2.inRange(img_hsv, (60, 70, 70), (85, 255, 255))
# 在新的二值图像中查找轮廓
contours, _ = cv2.findContours(img_threshold,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
largestContour = np.array([[]])
# 初始化一个空数组用于发送数据回机器人
llpython = [0,0,0,0,0,0,0,0]
# 如果检测到轮廓,则绘制它们
if len(contours) > 0:
cv2.drawContours(image, contours, -1, 255, 2)
# 记录最大轮廓
largestContour = max(contours, key=cv2.contourArea)
# 获取包围轮廓的未旋转边界框
x,y,w,h = cv2.boundingRect(largestContour)
# 绘制未旋转的边界框
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,255),2)
# 记录一些自定义数据发送回机器人
llpython = [1,x,y,w,h,9,8,7]
#返回最大轮廓用于LL十字准线、修改后的图像和自定义机器人数据
return largestContour, image, llpython