Saturday, April 11, 2009

Python + Arduino + Motor Shield + Stepper Motor (Part 1)


This is my first successful attempt at sending a number (higher than 255) from Python to Arduino using Serial Communication. Currently it sends 3 bytes per execution.

The 1st byte for packet identification, and the 2nd and 3rd byte makes up the Base10 number we are sending. I've used bit-wise operators to maximize the number I can construct from 2 bytes. Currently the maximum number I can send is 65535. The code can still be optimized, and currently it does not support negative numbers but hey, I'm taking it one step at a time (pardon the pun). FYI, this exercise is part of a much larger project. You can read about it Here

Edit: After several requests I have put a revised version of the code to make this work online.

Copy and Paste the Arduino Code into a text file and save as a .pde and upload it via the Arduino IDE. Copy and Paste the Python Code into a text file and save as motor.py

To run it in python, type:
import motor [Enter]
motor.position(1,1.8,360) [Enter]
motor.position(1,1.8,0) [Enter] 
This should make a 200 steps per rev motor turn 360degrees and then return to it's original position. The only thing that you may need to change is line 20 in the python code. It depends on what COM port you will be using.

30 comments:

luuude said...

Can you make the code for this public please? I am doing some testing and as a total beginner it would be great to have a peek.

Dan Thompson said...

Sure, what is your email? The code for this test has been updated several times since this video. But I am happy to send it to you. But I will not be able to offer any support until it's up on the openMoco website as it's not really ready for release yet.

Dan.

luuude said...

it´s ludvig.friberg€gmail(dot)com

Thanks a million!

ouazar.yahia said...

What is the card model that thee use arduino

Dan Thompson said...

Yahia,

It's an Arduino Diecimila. But this technique should work with any USB Arduino (almost all of them).

Unknown said...

Dan,

I'm really interested on this project and wish to work on it. I bought the arduino board and motor already. Would you be able to guide me a bit and provide the code?

Thanks

Dan Thompson said...

Tvokto,

As I mentioned in a more recent post. I discovered a fatal flaw in the design of this code as well as some general hardware limitations of the Arduino. So I am planning on switching to the parallax propeller for further development.

This will mean a complete rewrite of the code. Unfortunately I don't have the time to support unfinished Arduino code at the moment due to work and family commitments.

If you can be more specific about what you are interested in, I may be able to point you in the right direction.

Apologies and good luck with you projects,

Dan.

Unknown said...

Dan,

Thanks for the prompt reply. I want to use the board to control the motor to turn to the angles that I specify. Essentially, I want to control the stepper motor the way it is shown in the video. This is my first project and I don't really know where to start. Thank you for your time.

Take Care.

Dan Thompson said...

Tvokto,

if you send me your email I can send you the code, much like I did for luuude. I should warn you that this it will only ever work on one motor at best. If you are planning on doing position control with more multiple motors simultaneously, then this technique is probably not the best one to use.

To fully understand the code you should familiarize yourself with pySerial and Arduino communication. You can find out more about that here:
http://www.arduino.cc/playground/Interfacing/Python

Unknown said...

Dan,

Thanks for the heads up. My email address is ttocto (at) gmail dot com.

Do you mind if I reach out to you if I have more questions?

Thanks

Cédric said...

Would you mind sending me the code ? It's a very insterresting project.
Thanks a lot
Cédric

c.jai at free.fr

ouazar.yahia said...

Hi,I'm enthusiasm to build a robot, and I want to connect with python, can you give me the code with which you are the stepper command, and also (I'm exaggerating lol) the tools with which you connect the stepper with Maya.
my email is : ouazar(dot)yahia at gmail(dot)com

Cédric said...

Thank's a lot Dan !

Dan Thompson said...

FYI Guys,

I have edited this post with links to the source code so you can have a play!

Anonymous said...

First, Thanks a million for posting this. I have been looking for this so bad.

I did everything but get the error that COM4 "No such file or directory"

What would be COM4 in a Linux world and how can I determine which serial port I am using/need to use?

Dan Thompson said...

Check the pySerial docs to find out the differences between Linux and windows com ports. You may also find the comments in this post useful.
http://danthompsonsblog.blogspot.com/2010/12/mac-version-of-servo-tools-for-maya.html

p.s. I believe mac and linux com ports work with the same syntax. So the link above should be relevant.

Good Luck.

Anonymous said...

Yes. Thanks a lot!!

I figured that out in the end. So for everybody who runs into the same beginner problem. Look up under Arduino-Tools-Serial Port which one you are using. It will pop up when you connect the Arduino. Most likely it will be /dev/tty/something.

And on my Mac I always get a pop up that tells me I am connecting a usbmodem. Dont bother if that happens.

Thanks again for that. Do you have any idea where I can get a good tutorial on how to connect external power source to an Arduino for a motor (Stepper). Or do you know how to solve that. I know that there are two power connectors on the shield:

1. EXT_PWR
2. PWR

All tutorials I read say that I have to remove the jumper and connect the external power source to the Shield. What is the jumper in this case 1 or 2?

And then the Motor shield is autonomous elecricitywise. And then I can supply whatever current the motor needs. In my case I have two steppers. One with 7 V and one with 12 V. Ultimatively I would like to use the 12 V one. And the shield can hold 6V -36 V is that correct? How about the mA?

An does the Voltage have to be exactly the amount the motor needs?

So if you would be so kind to take a photo of your setup that would be the most awesome thing in the world. I am really trying to get that going and it seems so trivial, but it really blocks me in what I want to do. After all I dont want to burn my board and start from 0 again.

Thanks so much for your time and attention. And sorry for my newbie questions.

Anonymous said...

Hi. I just made it work. Fantastic. I have a question about the motor.move() function you entered at the commandline in the video. When i used that it says, the object has no attribute "move". Is that a different code or did I miss something?

Thanks for posting this!!!

Dan Thompson said...

Hi Anonymous,

Glad to hear it's working for you!
The motor.move was for and older version of the code.

I changed the name of the command to position to better reflect what is happening with motor

motor.position(1,1.8,360)

means you are positioning the motor to 360. 360 is an absolute position value as apposed to "moving" the motor 360 steps from where the motor is at when you run the command.

So for example:
if you are already at position 350 and you run:
motor.position(1,1.8,360)
then you motor will only step 10 times to get to position 360.

sam said...

Hello, I write you to wonder if you could send to me or say to me where you have download the driving mode for python.
Thank you in advance

Dan Thompson said...

Hi sam, the links to the code are in the description of the post as well as usage instructions

Sam said...

Thanks but there are the mode "Motor" and the mode "serial" for Python and i have found the mode "serial" but i have not found the mode "Motor". Can you give me the link for the download. Thaks

sam said...

I have an error in python code because there is not the python module:

"Traceback (most recent call last):
File "C:\Users\...\motor", line 15, in
import motor
ImportError: No module named motor"

What is the solution?
Thanks

sam said...

Sorry sorry, i have found my error
I m very sorry for the disturbance

Anonymous said...

Hi Dan.

I don't write english very good, sorry.

I have a phidgets stepper controller. I don't need arduino.

Maya freeze with "while loop".

I can move my motor with this code:
stepper.setTargetPosition(0, X)
X is a step number.

I want to move the motor with the graph editor, bezier curve.

Please, Please, please, i need your help.

My code:

import maya.cmds as cmds
cmds.camera( n='camaraJL' )
cmds.addAttr( shortName='motor', attributeType='long' )
cmds.connectAttr( 'camaraJL1.tx', 'camaraJL1.motor' )

while True:
if stepper.getCurrentPosition(0) != cmds.getAttr('camaraJL1.motor'):
stepper.setTargetPosition(0, cmds.getAttr('camaraJL1.motor'))
else:
print "no move"

Dan Thompson said...

Hi Anonymous,

please see the servo tools for maya section on this blog. It's the next step in evolution from this post. And it does exactly what you are trying to do.

http://danthompsonsblog.blogspot.com.au/search/label/ServoToolsForMayaTutorials

Follow the download links to download the plugin and give it a go!

Dan Thompson said...

Once you have that install and working. checkout this video http://danthompsonsblog.blogspot.com.au/2009/05/moco-tools-for-maya-under-hood.html

It explains the difference between using the servo and steppers if servo tools for maya.

Sorry it's been 3 years since I've worked on this project so I can really only point you to what I have documeneted in the past.

Anonymous said...

hi.
i was wondering if u could mail me the codes, I'm new to python script and i bought the motor shield recently. shahriar_akbari2003 (at) yahoo.com

thnx.

Unknown said...

hey Dan thank you for sharing this great work . can you please send me the python library motor or the download page.

Unknown said...

hi dan
can you send me the (python-arduino-motor-shield-stepper) Arduino and Python code link in the blog no longer available please can share the code with me my email 3dlakshan@gmail.com thanks..