Hey
I've been having some problems running PWM in subprocesses. Just had another post here on this forum that kind of leads into this one.
So I started to experiment with switching my project from using threads to using multiprocessing instead. I got most of the functionality to run like before, except for the functionality that uses PWM. However I've checked that the code actually runs (I can even see the motor driver light up when the commands are sent) without errors. To me it seems like PWM just doesn't respond when it's in a subprocess.
My question if there is some information on this, and perhaps a workaround? I tried searching for this, and the only thing I could find was this stackoverflow post from a guy with almost the exaxt same issue, though no clues.
https://stackoverflow.com/questions/772 ... erry-pi-4b
This is an example/excerpt of some code that is run, but not responding.
Setting up the processClass that is initialized in processFull code is here if it's of any interest (might be some leftover comments and variable names from when I used threading)
https://github.com/Siggerud/roboCar/tre ... ocess2/src
I've been having some problems running PWM in subprocesses. Just had another post here on this forum that kind of leads into this one.
So I started to experiment with switching my project from using threads to using multiprocessing instead. I got most of the functionality to run like before, except for the functionality that uses PWM. However I've checked that the code actually runs (I can even see the motor driver light up when the commands are sent) without errors. To me it seems like PWM just doesn't respond when it's in a subprocess.
My question if there is some information on this, and perhaps a workaround? I tried searching for this, and the only thing I could find was this stackoverflow post from a guy with almost the exaxt same issue, though no clues.
https://stackoverflow.com/questions/772 ... erry-pi-4b
This is an example/excerpt of some code that is run, but not responding.
Setting up the process
Code:
def activate_car_handling(self): thread = Process(target=self._start_car_handling, args=(self.shared_flag,)) self._threads.append(thread) thread.start() def _start_car_handling(self, flag): self._print_button_explanation() self._map_all_objects_to_buttons() while not flag.value: for event in self._xboxControl.get_controller_events(): button, pressValue = self._xboxControl.get_button_and_press_value_from_event(event) if self._xboxControl.check_for_exit_event(button): self._exit_program(flag) break # get the object to take action based on the button pushed try: self._buttonToObjectDict[button].handle_xbox_input(button, pressValue) except KeyError: # if key does not correspond to object, then go to next event continue if self._cameraEnabled: self._cameraHelper.update_control_values_for_video_feed(self.shared_dict) print("Exiting car handling")
Code:
import RPi.GPIO as GPIOfrom roboCarHelper import map_value_to_new_scalefrom itertools import chainclass CarHandling:def __init__(self, leftBackward, leftForward, rightBackward, rightForward, enA, enB):self._leftBackward = leftBackwardself._leftForward = leftForwardself._rightBackward = rightBackwardself._rightForward = rightForwardself._enA = enAself._enB = enBself._pwmMinTT = 20 # this needs to be set to the value where the motors start "biting"self._pwmMaxTT = 60self._speed = 0self._turnLeft = Falseself._turnRight = Falseself._goForward = Falseself._goReverse = Falseself._gpioThrottle = {True: GPIO.HIGH, False: GPIO.LOW}GPIO.setup(leftBackward, GPIO.OUT)GPIO.setup(leftForward, GPIO.OUT)GPIO.setup(rightBackward, GPIO.OUT)GPIO.setup(rightForward, GPIO.OUT)GPIO.setup(enA, GPIO.OUT)GPIO.setup(enB, GPIO.OUT)self._pwmA = GPIO.PWM(enA, 100)self._pwmB = GPIO.PWM(enB, 100)self._pwmA.start(0)self._pwmB.start(0)self._controlsDictTurnButtons = {"Left": "D-PAD left","Right": "D-PAD right",}self._controlsDictThrottle = {"Gas": "RT","Reverse": "LT"}self._turnButtons = list(self._controlsDictTurnButtons.values())self._gasAndReverseButtons = list(self._controlsDictThrottle.values())def handle_xbox_input(self, button, pressValue):if button in self._turnButtons:self._prepare_car_for_turning(button, pressValue)self._move_car()elif button in self._gasAndReverseButtons:self._prepare_car_for_throttle(button, pressValue)self._move_car()def car_buttons(self):completeDict = dict(chain(self._controlsDictThrottle.items(), self._controlsDictTurnButtons.items()))return completeDictdef get_current_speed(self):return int(self._speed)def get_current_turn_value(self):if self._turnLeft:return "Left"elif self._turnRight:return "Right"else:return "-"def _change_duty_cycle(self, pwms, speed):for pwm in pwms:pwm.ChangeDutyCycle(speed)def _adjust_gpio_values(self, gpioValues):leftForwardValue, rightForwardValue, leftBackwardValue, rightBackwardValue = gpioValuesGPIO.output(self._leftForward, self._gpioThrottle[leftForwardValue])GPIO.output(self._rightForward, self._gpioThrottle[rightForwardValue])GPIO.output(self._leftBackward, self._gpioThrottle[leftBackwardValue])GPIO.output(self._rightBackward, self._gpioThrottle[rightBackwardValue])def _move_car(self):if self._goForward:if not self._turnLeft and not self._turnRight:gpioValues = [True, True, False, False]elif self._turnLeft:gpioValues = [False, True, False, False]elif self._turnRight:gpioValues = [True, False, False, False]elif self._goReverse:if not self._turnLeft and not self._turnRight:gpioValues = [False, False, True, True]elif self._turnLeft:gpioValues = [False, False, False, True]elif self._turnRight:gpioValues = [False, False, True, False]elif not self._goReverse and not self._goForward:if not self._turnLeft and not self._turnRight:gpioValues = [False, False, False, False]elif self._turnLeft:gpioValues = [False, True, True, False]elif self._turnRight:gpioValues = [True, False, False, True]self._adjust_gpio_values(gpioValues)def _prepare_car_for_turning(self, button, buttonState):stopTurning = Falseif button == "D-PAD left" and buttonState == 1:self._turnLeft = Trueself._turnRight = Falseelif button == "D-PAD right" and buttonState == 1:self._turnLeft = Falseself._turnRight = Trueelse:self._turnLeft = Falseself._turnRight = FalsestopTurning = Trueif not stopTurning:if not self._goForward and not self._goReverse:self._change_duty_cycle([self._pwmA, self._pwmB], self._pwmMaxTT)def _prepare_car_for_throttle(self, button, buttonPressValue):speed = map_value_to_new_scale(buttonPressValue,self._pwmMinTT,self._pwmMaxTT,2)if speed > self._pwmMinTT + 1: # only change speed if over the tresholdif button == "RT":self._goForward = Trueself._goReverse = Falseelif button == "LT":self._goForward = Falseself._goReverse = Trueelse:speed = 0self._goForward = Falseself._goReverse = Falseself._change_duty_cycle([self._pwmA, self._pwmB], speed)self._speed = speeddef cleanup(self):self._pwmA.stop()self._pwmB.stop()
https://github.com/Siggerud/roboCar/tre ... ocess2/src
Statistics: Posted by Siggerud — Tue Aug 27, 2024 5:39 am