pygame program on boot being killed with no errors

Viewed 94

I have a program that is being executed on reboot via crontab. The script and programs work as expected if I run the script from terminal manually myself, but on running the script on boot its as if my RoverDriver.py program is being killed for no reason with no errors.

@reboot sleep 15 && sh /home/pi/Desktop/Rover-Driver/launcher.sh >/home/pi/Desktop/Rover-Driver/logs/cronlog 2>&1

The shell script is:

cd /
cd /home/pi/Desktop/Rover-Driver
sudo python3 bluetooth.py
cd /

The bluetooth.py program checks to see if our bluetooth wireless controller is connected to the pi, if so start the driver program

import pexpect
import time

attempts = 0
while attempts < 30:
 
 child = pexpect.spawn('bluetoothctl')
 child.sendline('paired-devices')
 output = child.read_nonblocking(size = 200, timeout = -1).decode('utf-8')
 target = 'Xbox Wireless Controller'

 if output.find(target > -1:
  print("Target Found")
  print(pexpect.run('python RoverDriver.py'))
  break
 else:
  print("No target found")
  attempts = attempts + 1
  time.sleep(1)

The RoverDiver.py is:

import pyfirmata
import pygame
import time
from pygame.locals import *

pygame.init()

# controller setup
joysticks = []

for i in range(pygame.joystick.get_count()):
    joysticks.append(pygame.joystick.Joystick(i))
    joysticks[-1].init()

controller = joysticks[0]


board = pyfirmata.ArduinoMega('/dev/ttyACM0')

# setting pins
onboard_led =       board.get_pin('d:13:o')
l_motor_pin =       board.get_pin('d:11:p')
r_motor_pin =       board.get_pin('d:12:p')
motor_toggle_pin =  board.get_pin('d:24:o')

# constantly updates statuses for example if reading analog input from potentiometer
it = pyfirmata.util.Iterator(board)
it.start()

motor_toggle_pin.write(0)
l_motor_pin.write(.49804)
r_motor_pin.write(.49804)

print("Have not entered while loop yet")

while(1):
    pygame.event.pump()

    # puts rover in "stop" state if no input, avoids rover from "running away"
    motor_toggle_pin.write(0)
    l_motor_pin.write(.49804)
    r_motor_pin.write(.49804)

    if(controller.get_button(11)):
        print("Exiting...")
        exit()

    # divider ensure only half power given unless "A" button pressed down
    divider = 0.125
    reverse = False
    right_power = controller.get_axis(4)
    left_power = controller.get_axis(5)

    # "A" button pressed, give full power
    if(controller.get_button(0)):
        divider =  0.25

    right_power =   divider * (right_power + 1)
    left_power =    divider * (left_power + 1)

    # Bumpers are pressed, go reverse. Must be doing both to avoid breaking bot
    if(controller.get_button(6) and controller.get_button(7)):
        left_power =    0.5 - left_power
        right_power =   0.5 - right_power
        reverse = True
    else:
        left_power =    0.5 + left_power
        right_power =   0.5 + right_power

    # send motors their values
    motor_toggle_pin.write(1)
    l_motor_pin.write(left_power)
    r_motor_pin.write(right_power)

    print(f"L Power:{left_power} |R Power:{right_power}")
    # avoid cpu overloading
    time.sleep(.05)

# after exiting while loop, "turn off" motors
motor_toggle_pin.write(0)
l_motor_pin.write(.49804)
r_motor_pin.write(.49804)

print("End.")

I am expecting a rather large blob of text in the logs (because of the print statement of power levels), or at least the before/after print statements of the while loop however all i get is this:

Target Found.
b'pygame 1.9.4post1\r\nHello from the pygame community. https://www.pygame.org/contribute.html\r\n'

I know that its formatted that way because I am not decoding it. I am hypothesizing it has something to do with pygame but not sure what?

EDIT: To anyone who comes across this post. I switched my controls completely and no longer even rely on pygame. I switched to connecting to the pi via sockets and sending command strings over. This still needed to be setup on boot so I tried using crontab (unsuccessfully again) and instead found out about systemd which works beautifully.

0 Answers
Related