在启动时运行的脚本不记录输出
我一直试图在启动时运行在树莓派下面的代码:在启动时运行的脚本不记录输出
#!/usr/bin/python3
import numpy
import math
import cv2
#this is python 3 specific
import urllib.request
from enum import Enum
from VisionProcessor import VisionProcessor
from GripPipeline import GripPipeline
from networktables import NetworkTables
import time
import logging
from networktables.util import ntproperty
#proper networktables setup
logging.basicConfig(level=logging.DEBUG)
NetworkTables.initialize(server='10.17.11.76')
#create the field to talk to on the network table
class NTClient(object):
angle_difference = ntproperty('/Raspberry Pi/angle difference', 0)
distance_from_target = ntproperty('/Raspberry Pi/distance from target', 0)
n = NTClient()
frame = cv2.VideoCapture('https://frc:[email protected]/mjpg/video.mjpg')
if(frame == None):
print("error: camera not found. check connection")
#pipeline = GripPipeline()
pipeline = VisionProcessor()
print("pipeline created")
def get_image():
ret, img_array = frame.read()
# cv2.imwrite("frame.jpg", img_array)
return img_array
def find_distance(width, height, y):
#distances are in inches
KNOWN_WIDTH = 6.25
KNOWN_DISTANCE = 12.0
KNOWN_PIXELS = 135.5
KNOWN_HEIGHT = 424.0
focal_length = (KNOWN_PIXELS * KNOWN_DISTANCE)/KNOWN_WIDTH
#hypotenuse = (KNOWN_WIDTH * focal_length)/width
distance = (KNOWN_WIDTH * focal_length)/width
#0.2125 degrees per pixel vertical
# theta = (0.2125) * (240 - y)
# distance = KNOWN_HEIGHT * (math.tan((math.pi/2) - math.radians(theta)))
return distance
x = True
while x:
print("while loop entered")
img = get_image()
print("image gotten")
center_point = [160, 120]
file = open('output.txt', 'a')
try:
current_point, size, y = pipeline.process(img)
#negative means turn left, positive means turn right
pixel_difference = center_point[0] - current_point[0]
#4.7761 pixels per degree
angle_difference = (float)(pixel_difference)/4.7761
n.angle_difference = angle_difference
target_width = size[0]
target_height = size[1]
distance = find_distance(target_width, target_height, y)
n.distance_from_target = distance
print("angle")
file.write("angle: ")
print(n.angle_difference)
file.write(str(angle_difference))
print(" distance: ")
file.write("distance")
print(distance)
file.write(str(distance))
file.write("\n")
except UnboundLocalError:
print(":(")
except (TypeError, cv2.error) as e:
print(":(")
# x = False
我已经通过编辑/etc/rc.local
文件这样做,和脚本已成功运行。 Pi在启动时显示约25%的CPU使用率,并且在脚本运行时保持一致,所以我可以看到它何时处于活动状态(我没有在此Pi上运行任何其他进程)。使用ps -aux
显示活动的python3进程。但是,它不会输出任何内容,或者输出到output.txt
文件或网络表。
我的最终目标是让它成功输出到网络表。如果我正常运行它(例如,不在启动时,通过终端中的python3 pipeline-test.py
),它会正确输出到output.txt
和网络表。我添加了output.txt
作为确保我获得正确输出的一种方式,除了在启动时运行时,它工作得很好。
有没有人有什么可能是错的想法?如果需要更多信息,我可以尽我所能提供。
编辑:出于某种原因,当我从Github复制我的代码时,它失去了所有的缩进。正在使用的代码是here。
首先,/etc/rc.local脚本以root身份执行,因此在根目录中执行。您需要将完整的文件路径添加到您的python程序中。这可能会也可能不会解决问题。
python /dir/dir/python_program
您可以将此程序的输出记录在错误文件中。创建文件
sudo nano /home/pi/error.log
在该文件中,只需键入任何内容,然后退出(ctrl + x)保存更改。然后就可以编辑rc.local中,这样的消息被附加到文件
python /dir/dir/python_program > /home/pi/error.log &
现在执行重新启动
sudo reboot
的PI将启动,并运行程序,几分钟后,pkill的python并查看/home/pi/error.log文件。这会让你更好地了解你的程序发生了什么“失败状态”
我注意到在你的程序中你调用了一个文件。而不是output.txt,您将需要该文件的完整路径,因为该程序在启动时在根目录中执行。这将需要在您的程序调用任何文件的所有情况下进行更改。
,如果你再拿到在日志文件权限错误,运行以下
sudo chmod 777 -R /filepath_to_your_script
这将有助于如果有问题的代码被正确地缩进。 – martineau
@martineau对不起,我把它从我的Github中复制出来,出于某种原因,缩进没有结束。我修复了它。缩进在正在运行的位置是正确的。 –
为什么你的循环中有'open'语句?您一遍又一遍地打开文件。它应该在循环之外,可能你应该看看使用'with open' ...“模式。你的缩进还没有结束......你的除了需要与你的尝试相同的水平。 – RobertB