Robot Control with flask + PWM ? - Raspberry Pi Forums


hi
i'm using following code launch robot.
robot has 2 motor uses pwm control speed.

code: select all

try:     import rpi.gpio gpio except runtimeerror:     print("error importing rpi.gpio! must run root using sudo") flask import flask, render_template  # initialize flask application app = flask(__name__) ######################### # motor pins motor1a = 17    #left fwd motor1b = 18    #left rev motor2a = 23    #right fwd motor2b = 22    #right rev  # freq of pwm outputs pwm_freq = 50 #50hz  # uses processor pin numbering gpio.setmode(gpio.bcm)  # speed = pwm duty cycle, 0 = off, 100 = max speed = 100   # (1,1) = fwd, (2,2) = rev, (1,2) = left, (0,0) = right, (1,0) = fwd left, (0,1) = fwd right, (2,0) = rev left, (0,2) = rev right current_direction = (0, 0)  # setup pins gpio.setup(motor1a, gpio.out) gpio.setup(motor1b, gpio.out) gpio.setup(motor2a, gpio.out) gpio.setup(motor2b, gpio.out) #gpio.setup(pwm_all, gpio.out)  pin1a = gpio.pwm(motor1a, pwm_freq) pin1b = gpio.pwm(motor1b, pwm_freq) pin2a = gpio.pwm(motor2a, pwm_freq) pin2b = gpio.pwm(motor2b, pwm_freq)  pin1a.start (0) pin1b.start (0) pin2a.start (0) pin2b.start (0) #########################  # route '/' , '/index' `index` @app.route('/') @app.route('/index') def index():     # render template 	return render_template('index.html')  @app.route('/<int:id1>/<int:id2>') def article(id1,id2): 	global current_direction 	global speed 	# current_direction = (id1, id2) 	# (1,1) = fwd, (2,2) = rev, (1,2) = left, (0,0) = right, (1,0) = fwd left, (0,1) = fwd right, (2,0) = rev left, (0,2) = rev right 	if (id1 >= 0 , id1 <= 2 , id2 >= 0 , id2 <= 2) : 		print 'id1 = >>> ' + str (id1) + ' - id2 = >>> ' + str (id2) 		current_direction = (id1,id2) 	elif (id1 == 3 , id2 == 10): ## low speed 		speed -= 10 		if speed < 0 : 			speed = 0 		print "speed : "+str(speed)+"\n" 		################################ 	#elif (id1 == 3 , id2 == 20): 	#	print 'id >>> ' + str (id2) 		################################ 	elif (id1 == 3 , id2 == 15): ## high speed 		speed += 10 		if speed > 100 : 			speed = 100 		print "speed : "+str(speed)+"\n" 		################################ 	else: 		current_direction = (0,0) 	#time.sleep(.2) 	motor_change() 	return render_template('index.html', title = id2)  @app.route ('/stop') def stopgpio(): 	pin1a.stop() 	pin1b.stop() 	pin2a.stop() 	pin1b.stop() 	gpio.cleanup() 	 def motor_change():     # motor 1     if (current_direction[0] == 1) :         pin1a.changedutycycle(speed)         pin1b.changedutycycle(0)     elif (current_direction[0] == 2) :         pin1a.changedutycycle(0)         pin1b.changedutycycle(speed)     # if 0 (stop) or invalid stop anyway     else :         pin1a.changedutycycle(0)         pin1b.changedutycycle(0)     # motor 2     if (current_direction[1] == 1) :         pin2a.changedutycycle(speed)         pin2b.changedutycycle(0)     elif (current_direction[1] == 2) :         pin2a.changedutycycle(0)         pin2b.changedutycycle(speed)     # if 0 (stop) or invalid stop anyway     else :         pin2a.changedutycycle(0)         pin2b.changedutycycle(0)	 	 # run if __name__ == '__main__': 	app.run(host = "0.0.0.0",port = 80,debug=true)
when run code , send example "http://raspberrypi-ip/2/2" "index.html", motors move , stand ! (even when speed = 100 , both motors on, motors stop , move !!)
problem?

perhaps instrument code bit, i.e. print diagnostics when each of main functions called indicating parameters.


raspberrypi



Comments

Popular posts from this blog

Rasperry Wifi LAN Controller - Raspberry Pi Forums

Convierte tu Raspberry en un NAS. Firmware fvdw-sl 15.3 - Raspberry Pi Forums

Unlock car with bluetooth and car key