peekaybee
Posts: 23
Joined: Mon Jul 20, 2015 6:55 pm
Location: Abergavenny
Contact: Website

using a stepper motor

Mon Jul 20, 2015 7:02 pm

Hi There,
I am a newbie so please forgive me.
I have to learn python fast for a project and I need to control a stepper motor.
I use the command import usb.core
but I just get the NameError: name 'usb" is not defined
how can I import the usb.core?
thank you

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: using a stepper motor

Mon Jul 20, 2015 7:29 pm

Windows or Linux?

peekaybee
Posts: 23
Joined: Mon Jul 20, 2015 6:55 pm
Location: Abergavenny
Contact: Website

Re: using a stepper motor

Mon Jul 20, 2015 9:29 pm

Linux I guess....
I have a RaspberryPi 2
Should I use python 2 or 3
IDLE?

in a nutshell this is what I am trying to do

control a moving slit (aperture)
display a sequence of images on a screen.
open and close a shutter

1) Home the aperture
variables are: width of aperture (2,3,or 4mm)
distance aperture travels (= number of exposures).

2) display image

3 initial wait time (1min to 30min)

4) open shutter (exposure time 10 secs to 2 mins approx)

5) close shutter

6) advance aperture (2,3 or 4mm)

7) advance image

8) frame settle time (prob about a min)

repeat stages 4 to 8 twenty to sixty times

easy peasy right?

I have got the pi, control board, stepper motor and shutter

danjperron
Posts: 3508
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: using a stepper motor

Mon Jul 20, 2015 9:54 pm

What kind of stepper?

What is your control board?

Maybe an A4998 stepper controller will be easier.

peekaybee
Posts: 23
Joined: Mon Jul 20, 2015 6:55 pm
Location: Abergavenny
Contact: Website

Re: using a stepper motor

Mon Jul 20, 2015 10:02 pm

Hi there
have a got a stepperbee+ control board and the large stepper motor that goes with it.
pc-control.co.uk
they supplied some code to go with it but get the error message as before.

danjperron
Posts: 3508
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: using a stepper motor

Mon Jul 20, 2015 10:44 pm

I check the website and looks like the stepper b+ is for a X86 windows operating.

Unless they have the driver for linux arm, you won't be able to use it.

Check for a A4998 stepper driver.

ghp
Posts: 1517
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: using a stepper motor

Tue Jul 21, 2015 4:46 am

Hello,
I browsed the website of the company, and hidden somewhere I found the page about connecting to raspberry.
If your python source file is named 'usb.py', this could cause the error. Just rename it to something else.
And did you install pyusb ?

sudo apt-get update
sudo apt-get install python-pip
sudo pip install pyusb

Hope this helps.
Gerhard

danjperron
Posts: 3508
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: using a stepper motor

Tue Jul 21, 2015 10:22 am

I browsed the website of the company, and hidden somewhere I found the page
Do you have the link of that hidden webpage ?


peekaybee
Posts: 23
Joined: Mon Jul 20, 2015 6:55 pm
Location: Abergavenny
Contact: Website

Re: using a stepper motor

Fri Jul 24, 2015 2:18 pm

sudo apt-get update
sudo apt-get install python-pip
sudo pip install pyusb

tried running this and I just get the invalid syntax message

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: using a stepper motor

Fri Jul 24, 2015 3:32 pm

Can you post your code?
Be sure to put it between the code tags in the editor so it looks something like...

Code: Select all

if my_code == dodgy:
    seek_help()
Dave.
Apple say... Monkey do !!

peekaybee
Posts: 23
Joined: Mon Jul 20, 2015 6:55 pm
Location: Abergavenny
Contact: Website

Re: using a stepper motor

Tue Jul 28, 2015 2:33 pm

$ sudo apt-get install pyusb
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package pyusb

peekaybee
Posts: 23
Joined: Mon Jul 20, 2015 6:55 pm
Location: Abergavenny
Contact: Website

Re: using a stepper motor

Wed Jul 29, 2015 10:23 am

o.k have downloaded the pyusb programe
but can't seem to install it

this is from the terminal window. The pyusb programme is in downloads

pi@raspberrypi ~ $ sudo apt-get install pyusb
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package pyusb
pi@raspberrypi ~ $ sudo apt-get install pyusb
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package pyusb
pi@raspberrypi ~ $ sudo apt-cache search pyusb
python-usb - USB interface for Python
pi@raspberrypi ~ $

How do I instruct the pi to find and install the programme that is now in downloads?
Any help greatly appreciated.
cheers

ghp
Posts: 1517
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: using a stepper motor

Wed Jul 29, 2015 7:26 pm

Hello,
I got a barcode scanner connected to python and then to scratch.

The usual pyusb-install did not workwith „pip install pyusb“. This resulted in a ‘backend not found’ exception and „undefined symbol: libusb_strerror“
When I tried to download walac-pyusb-50b1490 from https://github.com/walac/pyusb, this worked, but I had to uninstall the pip-installed code first „sudo pip uninstall pyusb“.

The needed backend packages are already available in raspbian, you can check this with
apt-cache pkgnames | grep libusb

You should find libusb-1.0-0 in the list.

Good luck,
Gerhard

peekaybee
Posts: 23
Joined: Mon Jul 20, 2015 6:55 pm
Location: Abergavenny
Contact: Website

Re: using a stepper motor

Thu Jul 30, 2015 1:05 pm

o.k found that & installed!
thank you for your help, I will try this out now!

ghp
Posts: 1517
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: using a stepper motor

Thu Jul 30, 2015 6:15 pm

Hello,

think this is the 'hello world' for USB.
Is from the pyusb code. Should list up all your connected devices and when this works, most of trouble is solved ? upfront ?

Regards,
Gerhard

Code: Select all

import usb.core
import usb.util

devices = usb.core.find(find_all=True)

for p in devices:
    print( p)

peekaybee
Posts: 23
Joined: Mon Jul 20, 2015 6:55 pm
Location: Abergavenny
Contact: Website

Re: using a stepper motor

Fri Jul 31, 2015 9:40 am

brilliant. can now see the product is there. just got to control it now!
thanks for your help

peekaybee
Posts: 23
Joined: Mon Jul 20, 2015 6:55 pm
Location: Abergavenny
Contact: Website

Re: using a stepper motor

Fri Jul 31, 2015 11:11 am

import usb.core
import usb.util

devices = usb.core.find(find_all=True)

for p in devices:
print( p)
I can see the device
Screen Shot 2015-07-31 at 10.14.30.png
Screen Shot 2015-07-31 at 10.14.30.png (38.82 KiB) Viewed 2004 times
but if I try and run the supplied script I get the error message
Screen Shot 2015-07-31 at 11.56.57.png
Screen Shot 2015-07-31 at 11.56.57.png (11.1 KiB) Viewed 2004 times
and if I type (or copy and paste the script into idle) the “if dev is None” line, the “None” is purple rather than orange.

I have tried this both in Python & Python3


Any help greatly appreciated.
Attachments
Screen Shot 2015-07-31 at 11.42.48.png
Screen Shot 2015-07-31 at 11.42.48.png (23.84 KiB) Viewed 2004 times

ghp
Posts: 1517
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: using a stepper motor

Fri Jul 31, 2015 11:40 am

Code: Select all

if dev == None:
    raise ....
else:
   do something else...
Think the 'is None' is not correct ?

Regards,
Gerhard

peekaybee
Posts: 23
Joined: Mon Jul 20, 2015 6:55 pm
Location: Abergavenny
Contact: Website

Re: using a stepper motor

Sun Aug 02, 2015 11:11 am

still cant get this code to run:

import usb.core

dev = usb.core.find(idVendor=0x04d8, idProduct=0x005d)

if dev is None:
raise ValueError('Device not found')
else:
try:
dev.detach_kernel_driver(0)
except:
pass
dev.set_configuration()

timeLSB = 100
timeMSB = 1
stepsLSB = 50
stepsMSB = 1
direction = 1
outputs = 0b00000110

data=[2, direction, timeLSB, timeMSB, stepsLSB, stepsMSB, outputs]
dev.write(2,data)
if I run it in idle it says : ValueError: Device not found.
if I write it in the shell, I get as far as the command except: which it describes as "invalid syntax"
I tried writing both dev == None: & dev is None: with the same conclusion.
The device is connected and visible.

any help greatly appreciated

ghp
Posts: 1517
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: using a stepper motor

Sun Aug 02, 2015 1:18 pm

Hello,
could you place code-tags around your code in the post above ? Makes it easier to read it.
see viewtopic.php?f=32&t=84477
Regards,
Gerhard

Then, place a "print (dev)" in the line before the "if dev is None:"-line. Just to see what is going on.

peekaybee
Posts: 23
Joined: Mon Jul 20, 2015 6:55 pm
Location: Abergavenny
Contact: Website

Re: using a stepper motor

Sun Aug 02, 2015 1:27 pm

Is this o.k?

Code: Select all

import usb.core

dev = usb.core.find(idVendor=0x04d8, idProduct=0x005d)

if dev is None:
    raise ValueError('Device not found')
else:
    try: 
        dev.detach_kernel_driver(0)
    except:
        pass
    dev.set_configuration()

    timeLSB = 100
    timeMSB = 1
    stepsLSB = 50
    stepsMSB = 1
    direction = 1
    outputs = 0b00000110

    data=[2, direction, timeLSB, timeMSB, stepsLSB, stepsMSB, outputs]
    dev.write(2,data)

peekaybee
Posts: 23
Joined: Mon Jul 20, 2015 6:55 pm
Location: Abergavenny
Contact: Website

Re: using a stepper motor

Sun Aug 02, 2015 1:33 pm

if I type: sudo idle into the terminal window before running the command in the idle environment, I don't get any error messages but a restart in the python prompt.

peekaybee
Posts: 23
Joined: Mon Jul 20, 2015 6:55 pm
Location: Abergavenny
Contact: Website

Re: using a stepper motor

Sun Aug 02, 2015 1:51 pm

Screen Shot 2015-08-02 at 14.46.38.png
Screen Shot 2015-08-02 at 14.46.38.png (51.79 KiB) Viewed 1770 times
The attachment Screen Shot 2015-08-02 at 14.46.38.png is no longer available
with print (dev) included

ghp
Posts: 1517
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: using a stepper motor

Sun Aug 02, 2015 2:11 pm

Hello, have added some code which I found to work.
Best approach is to copy the code to a leafpad window, and save it to a file, say 'test.py'
Then, in a terminal window, run it by "python test.py".
If there are errors in console. mark them and copy paste here.

Code: Select all

import usb.core
import usb.util


dev = usb.core.find(idVendor=0x04d8, idProduct=0x005d)
print (dev)


if dev is None:
    raise ValueError('Device not found')
else:
    if dev.is_kernel_driver_active(0) is True:
        # tell the kernel to detach
        dev.detach_kernel_driver(0)
        # claim the device
        usb.util.claim_interface(dev, 0)
        
    # most possibly this thing has only one configuration
    # so no need to set it
    # dev.set_configuration()

    timeLSB = 100
    timeMSB = 1
    stepsLSB = 50
    stepsMSB = 1
    direction = 1
    outputs = 0b00000110

    data=[2, direction, timeLSB, timeMSB, stepsLSB, stepsMSB, outputs]
    dev.write(2,data)
    

Return to “Python”