简体   繁体   中英

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. 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. 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. 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. 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...

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

Thanks!

Did you add anything to ~/.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 . 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. 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. Also you can check when the cron runs by looking in /var/log/syslog

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. Then later, change it back to @reboot to try it with 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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