简体   繁体   English

使用cron启动时Beaglebone Black pwm保持高位(python)

[英]Beaglebone Black pwm at startup using cron just stays high (python)

I am trying to make my Beaglebone Black (BBB) output pwm at startup. 我正在尝试在启动时使Beaglebone Black(BBB)输出为pwm。 The program runs correctly when started from the command line. 从命令行启动时,程序可以正确运行。 However when I try to get the program to run at startup (using cron) the output of the pin is just high. 但是,当我尝试让程序在启动时运行(使用cron)时,引脚的输出就很高。

Here is the code I am running: 这是我正在运行的代码:

#!/usr/bin/python
import Adafruit_BBIO.GPIO as GPIO    #import GPIO library
import Adafruit_BBIO.PWM as PWM      #import PWM library

#Boost Converter Control
boost_frequency = 1000000            #in Hz, 1000000 = 1MHz
boost_duty_cycle = 50                #in % (0-100)

PWM.start("P8_13", boost_duty_cycle, boost_frequency)

I saved this as fes_control.py in /root/exo_code 我将此保存为fes_control.py在/ root / exo_code中

When I run it from the command line using the following I get correct pwm at the output: 当我使用以下命令从命令行运行它时,我在输出中得到正确的pwm:

python fes_control.py

I want to be able to run this at startup. 我希望能够在启动时运行它。

I typed the following into the command line: 我在命令行中输入了以下内容:

sudo crontab -e

Which opens up a file which I add the following line to at the bottom: 这将打开一个文件,在底部添加以下行:

@reboot python /root/exo_code/fes_control.py &

I then save and exit. 然后,我保存并退出。

However when I reboot the BBB the output of the pin is just constantly high. 但是,当我重启BBB时,该引脚的输出一直处于高电平。 I have the same issue at 1kHz pwm frequency. 在1kHz pwm频率下,我有同样的问题。 I also tried putting "sudo" in between "@reboot" and "python" in the cron file but that didn't change anything. 我还尝试在cron文件中的“ @reboot”和“ python”之间放置“ sudo”,但没有任何改变。 Any ideas what could be going on? 有什么想法怎么回事?

Edit: I have also tried writing a script to run my python program and placing it in /etc/init.d (like what has been done in this tutorial: http://www.pietervanos.net/knowledge/start-python-script-from-init-d/ ) and I'm having exactly the same issue as above. 编辑:我还尝试编写脚本来运行我的python程序并将其放在/etc/init.d中(就像本教程中所做的那样: http : //www.pietervanos.net/knowledge/start-python- script-from-init-d / ),而我遇到的问题与上述完全相同。

crontab is generally used to specify a time a task will run crontab通常用于指定任务运行的时间

Your command is missing the time specifiers 您的命令缺少时间说明符

$nano /etc/crontab $ nano / etc / crontab

Will show you some examples. 将向您展示一些示例。 Here is mine 这是我的

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command 

17 * * * * root cd / && run-parts --report /etc/cron.hourly 17 * * * * root cd / &&运行部件--report /etc/cron.hourly

25 6 * * * root test -x /usr/sbin/anacron || 25 6 * * *根测试-x / usr / sbin / anacron || ( cd / && run-parts --report /etc/cron.daily ) (cd / && run-parts --report /etc/cron.daily)

47 6 * * 7 root test -x /usr/sbin/anacron || 47 6 * * 7根测试-x / usr / sbin / anacron || ( cd / && run-parts --report /etc/cron.weekly ) (cd / && run-parts --report /etc/cron.weekly)

52 6 1 * * root test -x /usr/sbin/anacron || 52 6 1 * *根测试-x / usr / sbin / anacron || ( cd / && run-parts --report /etc/cron.monthly ) # (cd / && run-parts --report /etc/cron.monthly)#

So I think a command like this would work 所以我认为这样的命令会起作用

* * * * * root python /root/exo_code/fes_control.py

Okay I figured it out. 好吧,我知道了。 Turns out that the code I am running needs some things which weren't initilised when the code was being run at startup. 原来,我正在运行的代码需要一些在启动时正在运行的代码未初始化的东西。 I added a delay of 10 seconds at the start of my python code and now it works perfectly at startup :) 我在python代码的开头添加了10秒的延迟,现在它在启动时可以完美运行:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM