juanmf / StepperMotors

Python stepper motor library with customizable acceleration strategies and responsive, interruptible motor operations.
https://github.com/juanmf/StepperMotors
MIT License
19 stars 0 forks source link

Builders don't make it through multiprocess spawning, thus only factory methods work for now on MP. #10

Closed juanmf closed 8 months ago

juanmf commented 8 months ago

    def _initMpDrivers(self, controllerFactory: MultiProcessingControllerFactory):
        azimuthDriverShared = Value(ctypes.c_int, 0)
        elevationDriverShared = Value(ctypes.c_int, 0)

        azimuthObserver = MultiprocessObserver(eventObserver=partial(self.childProcessEventObserver, "azimuth"),
                                               eventPublisher=self.sharedDataProcessing,
                                               sharedMemory=azimuthDriverShared)
        elevationObserver = MultiprocessObserver(eventObserver=partial(self.childProcessEventObserver, "elevation"),
                                               eventPublisher=self.sharedDataProcessing,
                                               sharedMemory=elevationDriverShared)

        nema = Nema17_42Ncm_17HS4401(True)

        # This builder works on mono process only:
        builder = (ControllerBuilder.getBasicBuilder(stepperMotor=nema, directionGpioPin=8, stepGpioPin=25,
                                                     enableGpioPin=1)
                   .withTorqueCurve(None)
                   .withNavigationStyleSynchronized()
                   .withCustomTorqueCurveAccelerationAcceleration()
                   .withClientMultiprocessObserver(elevationObserver))

        self.azimuthDriver, self.elevationDriver = (controllerFactory
                .setUpProcess()
                # Todo: Add builder to MP scenario.
                .withDriver(multiprocessObserver=azimuthObserver,
                            factoryFnReference=controllerFactory.getMpCustomTorqueCharacteristicsDRV8825With,
                            stepperMotor=PG35S_D48_HHC2(True), directionGpioPin=13, stepGpioPin=19, sleepGpioPin=12)

                .withDriver(multiprocessObserver=elevationObserver,
                            factoryFnReference=controllerFactory.getMpCustomTorqueCharacteristicsTMC2209With,
                            stepperMotor=nema, directionGpioPin=8, stepGpioPin=25, enableGpioPin=1)
                # Alternative:
                # .withDriverBuilder(builder=builder, builderMethodRef=builder.buildTMC2209StandaloneDriver)

                .spawn())

When I run .withDriverBuilder(builder=builder, builderMethodRef=builder.buildTMC2209StandaloneDriver) nothing fails but the motor driver is not getting the jobs in child process.

juanmf commented 8 months ago

It wasn't related to builders. BasicSynchronizedNavigation's multiton approach based on __new__() breaks, it happened before with EventDispatcher, using classic getInstance() approach now.