Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

How do I use the "release()" function and how do I control s
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How do I use the "release()" function and how do I control s

by JC_LaFleur on Mon Jun 01, 2020 7:18 pm

Hello,

I am using a Adafruit DC & Stepper Motor Bonnet for Raspberry Pi and a 28BYJ-48 stepper motor to pan a time-lapse camera 180 degrees over a 12 hour period and then return to it's starting position to wait for the next day's time-lapse sequence.
My plan is to automate this setup to take a 12 hour (sun-up to sun-set) time-lapse while panning 180 degrees from east to west, then have the camera return to it's starting place (reverse 180 degrees to face east again), then I will use a Whittypi 3 Mini to shutdown the Pi and wait for the next sunrise, then automatically startup and repeat the process.

This is the code I am using for the motor...
Code: Select all | TOGGLE FULL SIZE
from adafruit_motorkit import MotorKit
from adafruit_motor import stepper
from time import sleep

kit = MotorKit()

for j in range(1024):
    for i in range(1):
        kit.stepper1.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)

    print("Step %s" % (j + 1))

    sleep(42.187)

for i in range(1024):
        kit.stepper1.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)



This is working fine as far as the motion I need, however I really need to "release()" the coils AFTER each motor step and BEFORE the "sleep(42.187)" time delay in order to save power and prevent the motor from getting hot. This project will be running on a battery (and possibly solar) so I need the motor coils to turn off between each motor step during the 12 hour rotation as well as after the return (reverse 180).

Can someone please show me the correct syntax for using the "release()" function in my code? I can not find any example references that seem to work... I just end up with an error when I try it, and none of the "tutorials" or documentation I have found explain in detail how to use this function.

Also, I would like to know how to control the speed of a stepper motor using the Adafruit DC & Stepper Motor Bonnet for Raspberry Pi, I know that the 28BYJ-48 has a maximum speed of up to 15 rpm, however I can not seem to get mine to go any faster than 6 or 7 rpm. It seems kind of useless to have a stepper motor controller board that can't precisely control the speed of a stepper motor. How can this be accomplished, and is there a more concise reference or instructions for all of the available functions in the MotorKit and Stepper libraries?

Thank you
JCL

JC_LaFleur
 
Posts: 27
Joined: Thu May 28, 2020 10:29 pm

Re: How do I use the "release()" function and how do I contr

by dastels on Mon Jun 01, 2020 11:41 pm

From what I see
Code: Select all | TOGGLE FULL SIZE
kit.stepper1.release()

should do it.

What code is giving you an error, and what error?

Stepper motors don't have the concept of "speed" the way a DC motor does. All it can do is make a step. If you have it step at regular intervals you can get somewhat smooth rotation. The effective speed of rotation is a direct result of how fast you step it. So it'll only turn as fast as you can step it, and that's limited by the code. In this case, the execution speed of Python will put an upper limit on how fast you can step which will be far lower than you could accomplish coding in C++. You might also have trouble getting a smooth rotation since you're using a Pi rather than a microcontroller due to Linux being a multi-tasking OS. Python's garbage collection behavior may impact that as well.

Dave

dastels
 
Posts: 3000
Joined: Tue Oct 20, 2015 3:22 pm

Re: How do I use the "release()" function and how do I contr

by JC_LaFleur on Tue Jun 02, 2020 7:09 am

Sorry, your suggestion didn't work.

This is the code I'm running...
Code: Select all | TOGGLE FULL SIZE
from picamera import PiCamera
from adafruit_motorkit import MotorKit
from adafruit_motor import stepper
from time import sleep

kit = MotorKit()
camera = PiCamera()

for j in range(1024):
    for i in range(1):
        kit.stepper1.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)
        kit.stepper1.release()
    print("Step %s" % (j + 1))

    camera.start_preview()
    sleep(2)
    camera.capture('/home/pi/timelapse/image0%s.jpg' % j)
    camera.stop_preview()
    sleep(40)

for i in range(1024):
        kit.stepper1.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)



And this is the error I'm getting...
Code: Select all | TOGGLE FULL SIZE
pi@motortest:~ $ python3 slow.py
  File "slow.py", line 12
    kit.stepper1.release()
                         ^
TabError: inconsistent use of tabs and spaces in indentation
pi@motortest:~ $


Any suggestions as to what I am doing wrong here?

JC_LaFleur
 
Posts: 27
Joined: Thu May 28, 2020 10:29 pm

Re: How do I use the "release()" function and how do I contr

by dastels on Tue Jun 02, 2020 10:12 am

Fix your indentation. Python is very picky that indentation is done consistently, and consistently using either tabs or spaces.

Dave

dastels
 
Posts: 3000
Joined: Tue Oct 20, 2015 3:22 pm

Re: How do I use the "release()" function and how do I contr

by JC_LaFleur on Tue Jun 02, 2020 5:14 pm

Thank you Dave!
That was indeed my issue.
I thought the "^" marker in the error printout was telling me the problem was at the end of that line rather than the beginning (I had used a tab rather than spaces).


As far as controlling the RPM... I find it hard to believe that the limitations on how fast I can make a stepper motor rotate are caused by how fast (or slow) the Pi can cycle through individual lines of the python code. I fully understand that maximum RPM is different for every stepper motor, and I am also familiar with the difference between real-time and batch processing. However I have used a few other stepper motor controllers (both stand-alone and computer controlled) and almost all of them have some means of adjusting the PWM frequency that is actually driving the motor. And it is the stepper motor controller board, not the Pi that is generating the pulse modulation, right? So there should be no reason that speed is limited by how fast the Pi can run through the code. I have seen support in some older python libraries for adjusting the PWM/RPM of stepper motors, but I have not seen how to do it with the new MotorKit or Stepper libraries I am using now. I know that it is possible to do PWM over I2C as well, so there has to be somewhere in the library code or some parameter where you can adjust the modulation in order to control the RPM of a stepper motor rather than having to use the "sleep(#)" function between each rotor step.

Thanks again for your help... I should probably stop trying to write code at 3am... coding and insomnia are apparently a bad combination :-P
JCL

JC_LaFleur
 
Posts: 27
Joined: Thu May 28, 2020 10:29 pm

Re: How do I use the "release()" function and how do I contr

by dastels on Tue Jun 02, 2020 10:38 pm

Steppers don't use PWM, The control lines get manipulated to turn the rotor as desired. You might be thinking of servos, which do use a PWM signal. The code does that manipulation and there's a limit to how fast it can, thus there's an upper limit to how fast the motor can be stepped and thus how fast it can spin. The Pi isn't all that fast. Fast enough for plenty of things, but not compared to a moderately high end desktop or laptop. Then again it is a couple orders of magnitude cheaper.

See https://learn.adafruit.com/all-about-stepper-motors for more information on stepper motors.

Dave

dastels
 
Posts: 3000
Joined: Tue Oct 20, 2015 3:22 pm

Re: How do I use the "release()" function and how do I contr

by kriz on Tue Jul 21, 2020 4:55 am

Hello everybody, not sure if I can hook on to this thread or I should start a new one (my first post ever here)

But here my "problem" is:

I use the https://www.adafruit.com/product/2348 with the correct steppers for a project and everything works fine. After doing the stepping I release() the steppers for obvious reasons (heat and power-consumption). it all works fine when I start my Python-script directly from the commandline:

python3 DMO_buiten.py

But when I run this script in rc.local (for autobooting) or directly from the commandline with "sudo"

sudo python3 DMO_buiten.py

Everything works except the release() function. I get this errror:

Traceback (most recent call last):
File "DMO_buiten.py", line 87, in <module>
kit.stepper1.release()
AttributeError: 'StepperMotor' object has no attribute 'release'

What I'm I doing wrong?
Thanks for the help in advance.

kriz
 
Posts: 4
Joined: Tue Jul 21, 2020 4:40 am

Re: How do I use the "release()" function and how do I contr

by dastels on Tue Jul 21, 2020 9:53 am

Please start a new one.

Dave

dastels
 
Posts: 3000
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.