Hello! I am trying to have multi-processing in one of my Python scripts for a motorcycle build with turn blinkers. I am using time.sleep(0.5) in between turning on and off a touch LED. However, doing this halts the whole program for half a second.
from vex import *
motor_1.set_position(0, DEGREES)
motor_1.set_velocity(25, PERCENT)
motor_1.set_max_torque(100, PERCENT)
def pressed():
while True:
if motor_1.position(DEGREES)<controller.axisC.position()/2:
motor_1.spin(FORWARD)
if motor_1.position(DEGREES)>controller.axisC.position()/2:
motor_1.spin(REVERSE)
if motor_1.position(DEGREES)-1<controller.axisC.position()/2<motor_1.position(DEGREES)/2+1:
motor_1.stop()
def turnblink():
while True:
if controller.buttonLUp.pressing():
lturn.set_color(Color.YELLOW)
wait(0.4, SECONDS)
lturn.set_color(Color.BLACK)
wait(0.4, SECONDS)
I’m new here so there is probably a way to edit posts. I meant to put time.sleep(0.4) and accidentally cut off the code before I told both functions to run. Ignore pressed() that is my way of making a janky servo out of a motor.
Can you clearly explain what the issue is?
It seems like it’s doing what you want it to do so far (wait 0.5 seconds between each flash when the up button is pressed).
I am trying to execute both a function called turnblinkand pressed at the same time or taking the code from turnblink and putting it into pressed as in
def pressed():
while True:
if motor_1.position(DEGREES)<controller.axisC.position()/2:
motor_1.spin(FORWARD)
if motor_1.position(DEGREES)>controller.axisC.position()/2:
motor_1.spin(REVERSE)
if motor_1.position(DEGREES)-1<controller.axisC.position()/2<motor_1.position(DEGREES)/2+1:
motor_1.stop()
if controller.buttonLUp.pressing():
lturn.set_color(Color.YELLOW)
wait(0.4, SECONDS)
lturn.set_color(Color.BLACK)
wait(0.4, SECONDS)
However, by doing this, the wait(0.4, SECONDS) restricts the while True: loop from continuing, thus stopping the servo code (as in the previous relevant code function pressed) which needs to be executed all the time to be checking for the controller’s joystick and motor positions. By hauling the code, the motor (servo) is no longer able to continue checking for these positions, and given how I coded it, the motor will output unintended results.
I was hoping to use a import library that could tell one function to run in the background as in this psuedo code:
pressed()
[run in background] turnblink()
This would allow both functions to run at “independent” times, thus, when function turnblink uses wait(0.4, SECONDS) it would only halt the turnblink function and not the pressed function. Thanks for your help everyone!