On Wed, 9 May 2018 16:17:36 -0700 (PDT), Mala Dies
<
fun...@gmail.com> declaimed the following:
Your biggest admission "my lack of knowledge on Python," is probably
critical... I strongly suggest you study the Python Language Reference
Manual (unless things have changed, the standard Python documentation set
should have been included with the installer -- ActiveState Windows builds
them as regular help files; "man python" on Linux provides links to the
online version including a download page). The Beagleboard forum is not
really an appropriate venue for teaching one the basics of Python
programming.
Actually -- read the standard documentation Tutorial (same place you
find the reference manual). You may also want to consider the tutor list:
https://mail.python.org/mailman/listinfo/tutor (NOTE: I do not follow the
tutor list). The tutor list would not be a place to ask BBB-specific
questions -- so, as I show at the end of this post, if you can dummy up
your problem code using print-statements and describe what you expect vs
what you are actually seeing, the tutor list could be most suited. Once the
logic flow reflects what you want the program to do, then plug in the BBB
specific stuff, and if the hardware doesn't follow the expectations, then
come here...
>I am using sleep now instead of time.sleep, esp. since you let me know
>about my import of sleep from time. If you want to look over this software
>and reply, by all means, please do. If not, I understand. This software
>created the motor to turn after initializing and then went into a rough
>stop. Afterwards, the motor turned in the opposite direction. The program
>ended and the motor stopped, i.e. unlike w/ the while loop and the for
>loop. Oh, here is that odd software from my side:
The "while" loop was useful -- to repeat the entire program. But the
"for" loops were meaningless.
You could replace the "while" with a "for" which would mean you don't
need to use <ctrl-c> to exit -- you don't loop "forever" but for a specific
number of times.
for jnk in range(10):
#rest of program
will run 10 times and then exit.
>
>import Adafruit_BBIO.GPIO as GPIO
>from time import sleep
>
>EnA = "P9_21"
>ln1 = "P9_22"
>ln2 = "P9_12"
>
The purpose of these is to provide named constants -- to be used where
ever the cryptic "Px_yy" notation appears later. A convention is to make
"named constants" all upper-case, as a hint that one should never put them
on the left side of an "=" later in the code.
ENA = "P9_21"
>print "Initialize"
>
>GPIO.setup("P9_21", GPIO.OUT)
>GPIO.setup("P9_22", GPIO.OUT)
>GPIO.setup("P9_12", GPIO.OUT)
>
Like here...
GPIO.setup(ENA, GPIO.OUT)
etc. Using the named constant lets someone reading the code later
understand just what function is being affected, without having to note
where they are and look up elsewhere what the value ("P9_21") is associated
with.
It is easy to mistype "P9_21" as "P9_12" and not notice (both are valid
values for a GPIO pin), but much harder to type "IN2" when it is supposed
to be "ENA".
>if EnA != GPIO.output("P9_21", GPIO.HIGH):
Third time: GPIO.output() does NOT RETURN A VALUE! This comparison is
comparing a string to Python's default function value of None
"P9_21" != None
is always True.
> ln1 = GPIO.output("P9_22", GPIO.HIGH)
> ln2 = GPIO.output("P9_12", GPIO.LOW)
> sleep(2)
>
Two seconds running in "forward"
> ln1 = GPIO.output("P9_22", GPIO.LOW)
> ln2 = GPIO.output("P9_12", GPIO.HIGH)
> sleep(2)
>
Two seconds running in "reverse" (you don't buffer the motor by
stopping first)
> ln1 = GPIO.output("P9_22", GPIO.LOW)
> ln2 = GPIO.output("P9_12", GPIO.LOW)
> sleep(2)
Two seconds hard stop before the print statement.
> print "I am here now!"
>
>else:
>
Never reached, all of the "else" block is dead code.
> for i in range(1, 9):
> EnA = GPIO.output("P9_21", GPIO.HIGH)
> ln1 = GPIO.output("P9_22", GPIO.LOW)
> ln2 = GPIO.output("P9_12", GPIO.HIGH)
>
> EnA = GPIO.output("P9_21", GPIO.HIGH)
> ln1 = GPIO.output("P9_22", GPIO.HIGH)
> ln2 = GPIO.output("P9_12", GPIO.LOW)
> sleep(0.2)
>
> EnA = GPIO.output("P9_21", GPIO.LOW)
> ln1 = GPIO.output("P9_22", GPIO.HIGH)
> ln2 = GPIO.output("P9_12", GPIO.HIGH)
> sleep(0.2)
> print "I am still almost there!"
>
>try:
> EnA = GPIO.output("P9_21", GPIO.OUT)
Pardon -- that should be LOW or HIGH, not OUT
> print "time.sleep(3)"
And that statement does not sleep, it just prints a statement saying it
is sleeping
>
>except(KeyboardInterrupt):
> print "I got it over w/!"
> GPIO.cleanup()
> quit()
>
If you are using the try/except just to exit a non-exiting program, you
should really put the "try" at the start, around the rest of the main
program. Given where it is now, it will only be active while Python
executes the two statements inside the "try" block. Hitting <ctrl-c>
anywhere else in the program will just dump a traceback and do no cleanup.
Also, your cleanup is ONLY called if the program is killed by <ctrl-c>
while IN the "try", it is not called if the program exits on its own from
lack of any statements to execute.
Suggest you leave out all the GPIO manipulation and experiment with
just using "print" statements to get a program that pretends to do
something with the motor. When you get it to display what you want, then
start plugging in the GPIO and confirm that the motor behaves as you said
it should.
Compare using "while":
-=-=-=-
import time
print "Initialize GPIO"
try:
while True:
print "motor FORWARD"
time.sleep(5)
print "motor STOP"
time.sleep(1)
print "motor REVERSE"
time.sleep(5)
print "motor STOP"
time.sleep(1)
except (KeyboardInterrupt):
print "BREAK"
except:
print "Something unexpected happened"
print "Cleanup"
-=-=-=-
vs using "for"
-=-=-=-
import time
print "Initialize GPIO"
for jnk in range(5):
print "motor FORWARD"
time.sleep(5)
print "motor STOP"
time.sleep(1)
print "motor REVERSE"
time.sleep(5)
print "motor STOP"
time.sleep(1)
print "Cleanup"
-=-=-=-
and the second with the GPIO installed
-=-=-=-
import time
import Adafruit_BBIO.GPIO as G
ENA = "P9_21"
IN1 = "P9_22"
IN2 = "P9_12"
print "Initialize GPIO"
G.setup(ENA, G.OUT)
G.setup(IN1, G.OUT)
G.setup(IN2, G.OUT)
G.output(ENA, G.LOW)
G.output(IN1, G.LOW)
G.output(IN2, G.LOW)
G.output(ENA, G.HIGH) #enable motor driver
for jnk in range(5):
print "motor FORWARD"
G.output(IN1, G.HIGH)
G.output(IN2, G.LOW)
time.sleep(5)
print "motor STOP"
G.output(IN1, G.HIGH)
G.output(IN2, G.HIGH)
time.sleep(1)
print "motor REVERSE"
G.output(IN1, G.LOW)
G.output(IN2, G.HIGH)
time.sleep(5)
print "motor STOP"
G.output(IN1, G.LOW)
G.output(IN2, G.LOW)
time.sleep(1)
print "Cleanup"
G.cleanup()
-=-=-=-
This program should take just over one minute to run. The motor will
run forward for 5 seconds, stop for 1, reverse for 5, and stop for 1 (~12
seconds per cycle, and 5 cycles makes ~60 seconds).
No quit() is needed as this program runs until it exits off the bottom.