jetracer入门教程-自主巡线
说明:
- 介绍jetracer如何自主巡线
步骤:
在浏览器地址栏输入http://<jetson_ip_address>:8888连接到小车,找到jetracer/noteboot/,打开road_following.ipynb文件
程序运行到此段后目录中会生产使用touch2trt转换和优化的模型文件road_following_model_trt.pth。这个优化过程可能需要几分钟才能完成
import torch
import torchvision
CATEGORIES = ['apex']
device = torch.device('cuda')
model = torchvision.models.resnet18(pretrained=False)
model.fc = torch.nn.Linear(512, 2 * len(CATEGORIES))
model = model.cuda().eval().half()
model.load_state_dict(torch.load('road_following_model.pth'))
- 创建小车和摄像头
from jetracer.nvidia_racecar import NvidiaRacecar
car = NvidiaRacecar()
from jetcam.csi_camera import CSICamera
camera = CSICamera(width=224, height=224, capture_fps=65)
- 如果摄像头提示错误,运行如下命令重启摄像头再运行
$ sudo systemctl restart nvargus-daemon
- 将图像处理的输出output,控制小车运行
from utils import preprocess
import numpy as np
STEERING_GAIN = 0.75
STEERING_BIAS = 0.00
car.throttle = 0.14
while True:
image = camera.read()
image = preprocess(image).half()
output = model_trt(image).detach().cpu().numpy().flatten()
x = float(output[0])
car.steering = x * STEERING_GAIN + STEERING_BIAS
print(car.steering)
print(car.throttle)
- car.steering_offset:修正小车机械误差导致的小车初始状态偏移,这个参数需要改为小车实际的数值
- car.throttle:设置小车运行最大速度,次值设置过大会导致小车容易跑出轨道
- 根据jetracer小车的重量,修改car.throttle,使得小车运行,car.throttle范围:0.14 ~ 0.25
演示视频
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号