Sunday, 15 May 2011

raspberry pi python pwm continue after program done -


I have a robot on which I am working and controlling it with PWM. The way I am controlling it is a script that sets up PWM and then goes out. I need to set up PWM and then keep going. The reason for this is that I want to get out because I call this script through an SSH connection, changing the value of X and Y every time. Normal digital output is released after the program is out but not with the way PWM sets up

Here's my code so far I have a lot of print statements that try to help me find the problem.

  #filename: setMotors.py Import RPI As Jeepiaio import sleep time CIS import 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 mixXY (x, y) def: a second motor drive system values ​​for the input " "" Mixes X and X from a joystick: y (intake or boat), Y (int or float) output: (left motor (float), right motor (float) Tupal "" left motors = y + x Correct motors = y - x Returns (left motor, right motor) def set motorpw Ms (left motor, right motor): # left motor if left mount == 0: print ("left motor 0") GPIO.output (MOTOR_EN_1_PIN, 0) motor1A.stop () motor1B.stop () elif leftMotor & Lt; 0: print ("left motor and lt; 0") GPIO.output (MOTOR_EN_1_PIN, 1) motor1A.stop () motor 1B.ChangeDutyCycle (abs (left motor)) Other: print ("left motor") GPIO motor2B .output (MOTOR_EN_1_PIN, 1) motor1A.ChangeDutyCycle (leftMotor) motor1B.stop () #right motor that rightMotor == 0: print ( "right motor 0") GPIO.output (MOTOR_EN_2_PIN, 0) motor2A.stop (). Stop () elif rightMotor & LT; 0: print ( "right motor & LT; 0") GPIO.output (MOTOR_EN_2_PIN, 1) motor2A.stop () motor2B.ChangeDutyCycle (abs (rightMotor)) else: print ( "and the motor") 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 Ksetap (Motr_bi_l_pin, Gpiokaut) Gpioksetap (Motr_in_2_pin, Gpiokaut) Gpioksetap (Motr_a_2_pin, Gpiokaut) Gpioksetap (Motr_bi_2_pin, Gpiokaut) MotrlA = Gpiokpwm (Motr_a_l_pin, 50) MotrlB = Gpiokpwm (Motr_bi_l_pin, 50) Motr2A = Gpiokpwm (Motr_a_2_pin, 50) Motr2B = Gpiokpwm (Motr_bi_2_pin, 50) MotrlAkstart (0) MotrlBkstart (0) Motr2Akstart (0) Motr2B. Start (0) if lane (rgv) & lt; = 2: Print ("Need to call with X and Y from command line") Other: MotorPWM = Mix XY (int (AGR [1]), int (AGR [2]) rest MOTRWW = Motor PWM [0 ] Right motorpwm = motorpwm [1] print ("left motor:", left motors PWM) print ("right motor:", right motorspwm) setmotorpwms (left motors pwm, rightmotorpwm) sleep (5) print (" Done ")  

to call the sudo Python Setmotores UP . Is there a way to keep PWM after being out of the program or a better way of doing it?

Raspberry does not support hardware PWM, so this software emulates using the loop. Actually, it sets GPIO, sleeps a bit, GPIO resets, sleeps a little more loop, it is done when the program ends.

In this way, you have to find a way to keep your program alive in the background. If you look at it, you will see an endless loop that keeps the program alive until it is killed manually.

You should add anything

  Try: While 1: except for time.sleep (0.5) KeyboardInterrupt: pass p .stop () GPIO.cleanup ()  

At the end of your program, or something

  sudo python setMotors.py xy and amp; Amp;  

You can also get your program.


No comments:

Post a Comment