raspberry pi python pwm continue after program done -


i have robot working on , controlling pwm. way controlling script sets pwm , exits. need set pwm , keep running. reason need exit because calling script through ssh connection each time x , y values change. normal digital outputs continue after program exits pwm doesn't way have setup.

here code far. contains lot of print statements try me figure out problem was.

#filename: setmotors.py import rpi.gpio gpio sys import argv time import sleep  motor_en_1_pin = 14 motor_a_1_pin = 15 motor_b_1_pin = 18  motor_en_2_pin = 23 motor_a_2_pin = 24 motor_b_2_pin = 25   def mixxy(x, y):     """     mixes x , y joystick values 2 motor drive system     input: x (int or float), y (int or float)     output: (leftmotor (float), rightmotor (float)) tuple     """     leftmotor = y + x     rightmotor = y - x      return (leftmotor, rightmotor)  def setmotorpwms(leftmotor, rightmotor):     #left motor     if leftmotor == 0:         print("left motor 0")         gpio.output(motor_en_1_pin, 0)         motor1a.stop()         motor1b.stop()     elif leftmotor < 0:         print("left motor < 0")         gpio.output(motor_en_1_pin, 1)         motor1a.stop()         motor1b.changedutycycle(abs(leftmotor))     else:         print("left motor else")         gpio.output(motor_en_1_pin, 1)         motor1a.changedutycycle(leftmotor)         motor1b.stop()      #right motor     if rightmotor == 0:         print("right motor 0")         gpio.output(motor_en_2_pin, 0)         motor2a.stop()         motor2b.stop()     elif rightmotor < 0:         print("right motor < 0")         gpio.output(motor_en_2_pin, 1)         motor2a.stop()         motor2b.changedutycycle(abs(rightmotor))     else:         print("right motor else")         gpio.output(motor_en_2_pin, 1)         motor2a.changedutycycle(rightmotor)         motor2b.stop()    gpio.setwarnings(false)  #setup gpio.setmode(gpio.bcm) gpio.setup(motor_en_1_pin, gpio.out) gpio.setup(motor_a_1_pin, gpio.out) gpio.setup(motor_b_1_pin, gpio.out)  gpio.setup(motor_en_2_pin, gpio.out) gpio.setup(motor_a_2_pin, gpio.out) gpio.setup(motor_b_2_pin, gpio.out)  motor1a = gpio.pwm(motor_a_1_pin, 50) motor1b = gpio.pwm(motor_b_1_pin, 50) motor2a = gpio.pwm(motor_a_2_pin, 50) motor2b = gpio.pwm(motor_b_2_pin, 50) motor1a.start(0) motor1b.start(0) motor2a.start(0) motor2b.start(0)   if len(argv) <= 2:     print("need call x , y commandline") else:     motorpwm = mixxy(int(argv[1]), int(argv[2]))     leftmotorpwm = motorpwm[0]     rightmotorpwm = motorpwm[1]     print("left motor:",leftmotorpwm)     print("right motor:", rightmotorpwm)     setmotorpwms(leftmotorpwm, rightmotorpwm)     sleep(5)     print("done") 

the way called sudo python setmotors.py x y. there way of keeping pwm going after program exits or better way of doing this?

the raspberrypi doesn't support hardware pwm, it's emulated using software loop. basically, sets gpio, sleeps little, resets gpio, sleeps little , loops. done in a separated thread killed when program ends.

thus, have find way keep program alive in background. if @ official example on use of pwm, you'll notice endless loop keeps program alive until manually killed.

you should add like

try:     while 1:         time.sleep(0.5) except keyboardinterrupt:     pass p.stop() gpio.cleanup() 

to end of program, or better constructed using signal module.

then, leave process in background before destroying console

sudo python setmotors.py x y & 

you let program daemonized.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -