struggling to increase and decrease motor speed - Raspberry Pi Forums
hi guys need bit of getting code bellow working works kind of outputs data when run code cant increase , decrease speed want. please advise need change
forward 0 reverse 0 acceleration 0
forward 10 reverse 0 acceleration 10
forward 20 reverse 0 acceleration 20
forward 30 reverse 0 acceleration 30
forward 40 reverse 0 acceleration 40
forward 50 reverse 0 acceleration 50
forward 60 reverse 0 acceleration 60
forward 70 reverse 0 acceleration 70
forward 80 reverse 0 acceleration 80
forward 90 reverse 0 acceleration 90 speed @ peak
forward 90 reverse 0 acceleration 90
forward 90 reverse 0 acceleration 90
forward 90 reverse 0 acceleration 90
forward 90 reverse 0 acceleration 90
forward 90 reverse 0 acceleration 90-
forward 80 reverse 0 acceleration 80
forward 70 reverse 0 acceleration 70
forward 60 reverse 0 acceleration 60
forward 50 reverse 0 acceleration 50
forward 40 reverse 0 acceleration 40
forward 30 reverse 0 acceleration 30
forward 20 reverse 0 acceleration 20
forward 10 reverse 0 acceleration 10- code not change reversing till after
forward 0 reverse 0 acceleration 0
forward 0 reverse 10 acceleration -10 -starts @ max reverse , decrease speed (want other way)
forward 0 reverse 20 acceleration -20
forward 0 reverse 30 acceleration -30
forward 0 reverse 40 acceleration -40
forward 0 reverse 50 acceleration -50
forward 0 reverse 60 acceleration -60
forward 0 reverse 70 acceleration -70
forward 0 reverse 80 acceleration -80
forward 0 reverse 90 acceleration -90
forward 0 reverse 90 acceleration -90
forward 0 reverse 90 acceleration -90
forward 0 reverse 90 acceleration -90
forward 0 reverse 90 acceleration -90
forward 0 reverse 0 acceleration 0
forward 10 reverse 0 acceleration 10
forward 20 reverse 0 acceleration 20
forward 30 reverse 0 acceleration 30
forward 40 reverse 0 acceleration 40
forward 50 reverse 0 acceleration 50
forward 60 reverse 0 acceleration 60
forward 70 reverse 0 acceleration 70
forward 80 reverse 0 acceleration 80
forward 90 reverse 0 acceleration 90 speed @ peak
forward 90 reverse 0 acceleration 90
forward 90 reverse 0 acceleration 90
forward 90 reverse 0 acceleration 90
forward 90 reverse 0 acceleration 90
forward 90 reverse 0 acceleration 90-
forward 80 reverse 0 acceleration 80
forward 70 reverse 0 acceleration 70
forward 60 reverse 0 acceleration 60
forward 50 reverse 0 acceleration 50
forward 40 reverse 0 acceleration 40
forward 30 reverse 0 acceleration 30
forward 20 reverse 0 acceleration 20
forward 10 reverse 0 acceleration 10- code not change reversing till after
forward 0 reverse 0 acceleration 0
forward 0 reverse 10 acceleration -10 -starts @ max reverse , decrease speed (want other way)
forward 0 reverse 20 acceleration -20
forward 0 reverse 30 acceleration -30
forward 0 reverse 40 acceleration -40
forward 0 reverse 50 acceleration -50
forward 0 reverse 60 acceleration -60
forward 0 reverse 70 acceleration -70
forward 0 reverse 80 acceleration -80
forward 0 reverse 90 acceleration -90
forward 0 reverse 90 acceleration -90
forward 0 reverse 90 acceleration -90
forward 0 reverse 90 acceleration -90
forward 0 reverse 90 acceleration -90
code: select all
import rpi.gpio io io.setmode(io.bcm) import sys, tty, termios, time import os motorrightpin = 22 motorleftpin = 10 io.setup(motorleftpin, io.out) io.setup(motorrightpin, io.out) motorfdpin = 24 motorrdpin = 23 io.setup(motorfdpin, io.out) io.setup(motorrdpin, io.out) motorpwm2 = io.pwm(24,100) motorpwm2 = io.pwm(23,100) motorpwm2.start(0) motorpwm2.changedutycycle(0) # catch method can determine key has been pressed # user on keyboard. def getch(): fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.tcsadrain, old_settings) return ch # here define methods used determine # whether motor needs spin forward or backwards. # both pins match, motor not turn. def reverse(): io.output(motorrdpin, true) io.output(motorfdpin, false) def forward(): io.output(motorrdpin, false) io.output(motorfdpin, true) # stop motors def stop(): io.output(motorrdpin, false) io.output(motorfdpin, false) # set variables motor speed. motorfspeed = 0 motorrspeed = 0 maxspeed = 90 minspeed = 0 acceleration = 0 speedstep = 10 # functions sets motor speed. def setmotorspeed(motorfspeed, motorrspeed,acceleration): if(acceleration > 0): reverse() if(acceleration > maxspeed): acceleration = maxspeed motorfspeed = acceleration #motorrspeed = acceleration elif(acceleration == 0): motorfspeed = acceleration motorrspeed = acceleration stop() elif(acceleration < (maxspeed * -1)): acceleration = (maxspeed * -1) else: forward() #motorfspeed = (acceleration * -1) motorrspeed = (acceleration * -1) motorfspeed, motorrspeed = check_motorpseed(motorfspeed, motorrspeed) return motorfspeed, motorrspeed, acceleration # function used left steering def left(): io.output(motorrightpin, io.low) io.output(motorleftpin, io.high) #this function used right steering def right(): io.output(motorrightpin, io.high) io.output(motorleftpin, io.low) def center(): io.output(motorrightpin, io.low) io.output(motorleftpin, io.low) # check motorspeed if correct , in max/min range def check_motorpseed(motorfspeed, motorrspeed): if (motorfspeed < minspeed): motorfspeed = minspeed if (motorfspeed > maxspeed): motorfspeed = maxspeed if (motorrspeed < minspeed): motorrspeed = minspeed if (motorrspeed > maxspeed): motorrspeed = maxspeed return motorfspeed, motorrspeed # setting pwm pins false motors not move # until user presses first key io.output(motorrdpin, false) io.output(motorfdpin, false) # instructions when user has interface print("w/s: direction") print("a/d: steering") print("q: stops motors") print("x: exit") # infinite loop # loop not end until user presses # exit key 'x' or program crashes... while true: # keyboard character retrieval method. method save # pressed key variable char char = getch() print "forward",motorfspeed, "reverse", motorrspeed, "acceleration", acceleration # car drive forward when "w" key pressed if(char == "w"): acceleration = acceleration + speedstep motorfspeed, motorrspeed, acceleration= setmotorspeed(motorfspeed, motorrspeed, acceleration) motorpwm2.changedutycycle(motorfspeed) # car reverse when "s" key pressed if(char == "s"): acceleration = acceleration - speedstep motorfspeed, motorrspeed, acceleration = setmotorspeed(motorfspeed, motorrspeed, acceleration) motorpwm2.changedutycycle(motorrspeed) # stop motors if(char == "q"): motorfspeed = 0 motorrspeed = 0 stop() # "d" key toggle steering right if(char == "d"): right() stop() # "a" key toggle steering left if(char == "a"): left() stop() # "e" key toggle steering center if(char == "e"): center() stop() # "x" key break loop , exit program if(char == "x"): print("program ended") break # keyboard character variable char has set blank. need # set blank save next key pressed user char = "" # program clean gpio settings , terminates io.cleanup() # end
your code bit confusing.
1) trying control 1 motor, or two?
2) control signals driver board expecting? (per motor)
each motor can independently control direction , speed via pwm.
2 wire control usually
dira + dirb
or
dir + pwm
3 wire control usually
en dira dirb
1) trying control 1 motor, or two?
2) control signals driver board expecting? (per motor)
each motor can independently control direction , speed via pwm.
2 wire control usually
dira + dirb
or
dir + pwm
3 wire control usually
en dira dirb
raspberrypi
Comments
Post a Comment