简体   繁体   English

我的(工作)树莓派程序在启动时运行不正常

[英]My (working) raspberry pi program doesn't work right when I run it on boot

I'm writing a script that (basically) controls some motors from raspi gpio pins.我正在编写一个脚本(基本上)从 raspi gpio 引脚控制一些电机。 I've been making it right for a while now, and when I boot the pi and run the program manually it works just fine.我已经做了一段时间了,当我启动 pi 并手动运行程序时,它工作得很好。 I also have a portion of the code that allows me to use my phone to connect via bluetooth and send some data to control the motors.我还有一部分代码允许我使用我的手机通过蓝牙连接并发送一些数据来控制电机。 That also works fine on a manual run of the program.这也适用于手动运行程序。

Now I am trying to make the program start automatically on boot, as this will eventually go in a larger machine (boat) and I won't be hooking a monitor etc. to it.现在我试图让程序在启动时自动启动,因为这最终会进入更大的机器(船),我不会将监视器等挂在它上面。 I'm currently doing this through a cron job with the @reboot tag.我目前正在通过带有 @reboot 标签的 cron 作业来执行此操作。 Looks like this:看起来像这样:

#This enables GPIO (as far as I know). The program fails without this command being run first.
@reboot sudo pigpiod

#This runs the python program. ampersand forks the process because it should run continuously.
@reboot python3 /home/pi/Desktop/BoatBrain.py &

#and this lets me connect my phone over bluetooth. The python program has
#a portion takes data from that connection. ampersand forks the process, which
#seems like the right thing to do, since it looks like it blocks other things.
#That is also why it is at the end of the cron table.
@reboot sudo rfcomm watch hci0 &

When I reboot, the jobs all run, and I can connect my phone, so it must have passed the line executing the python script, but the servo I have connected just jitters in place uncontrollably.当我重新启动时,作业全部运行,并且我可以连接我的手机,所以它肯定已经通过了执行 python 脚本的线路,但是我连接的伺服器只是无法控制地抖动到位。 Let me restate that when I take the cronjobs away and run this manually, the program works correctly with few to no jitters, so it doesn't feel like an electrical problem...让我重申一下,当我拿走 cronjobs 并手动运行它时,程序可以正常工作,几乎没有抖动,所以感觉不像是电气问题......

If you need any more information please let me know and I'll be happy to provide it.如果您需要更多信息,请告诉我,我很乐意提供。 I have a tendency to leave things out without realizing XD我倾向于在没有意识到的情况下忽略事情XD

Thanks!谢谢!

Did you add anything to ~/.profile ?你有没有在~/.profile添加任何东西? It might be why it works when you invoke the commands yourself.这可能就是当您自己调用命令时它起作用的原因。 If so, create a file in sudo vi /etc/profile.d/servo.sh with the same couple lines you added to ~/.profile .如果是这样,请在sudo vi /etc/profile.d/servo.sh创建一个文件,其中包含添加到~/.profile的相同几行。 Then the system will have those on reboot.然后系统将在重新启动时具有这些。

Also, you could put all the three commands in one shell script and just put the script in the crontab.此外,您可以将所有三个命令放在一个 shell 脚本中,然后将脚本放在 crontab 中。 then the script can control that they start in order.然后脚本可以控制它们按顺序启动。 You could also have the cronjob write output to a logfile and then see what it says.您还可以让 cronjob 将输出写入日志文件,然后查看它的内容。 Also you can check when the cron runs by looking in /var/log/syslog您还可以通过查看/var/log/syslog来检查 cron 何时运行

Oh, also for testing, you can change @reboot to a start time like 10 * * * * and then you can get the cron working without having to reboot.哦,同样为了测试,您可以将 @reboot 更改为10 * * * *类的开始时间,然后您就可以让 cron 工作而无需重新启动。 Then later, change it back to @reboot to try it with reboot.稍后,将其更改回@reboot 以尝试重新启动。

Either something is missing that your login has (.profile), the commands are starting too quickly at the same time and need to start in a controlled order or the system isn't completely ready yet, but I doubt that one.要么您的登录名 (.profile) 缺少某些东西,要么同时启动命令太快,需要以受控顺序启动,要么系统尚未完全准备好,但我对此表示怀疑。

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

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