Go Down

Topic: Python, arduino and stepper motor won´t work (Read 1 time) previous topic - next topic



I have a problem for driving a stepper motor out of python(blender). The stepper should make the same rotation as cube in Blender3D. I´m using Blender 2.63, Pyserial 2.6, Arduino Mega2560, a big easy driver and a 1A stepper motor with 200 steps.

Python code:

Code: [Select]
import bpy
import math
from math import degrees
import serial
ser = serial.Serial('COM5',115200,timeout=1)

def my_handler(scene):
    data = ''
    eulx = bpy.data.objects['Cube'].rotation_euler
    x = degrees(eulx.y)
    a = (x/360)*200
    data = bytes([int(a)])

Arduino code:

Code: [Select]
#include <AccelStepper.h>

int motorSpeed = 115200;
int motorAccel = 8000;

int motorDirPin = 9;
int motorStepPin = 8;

AccelStepper stepper(1, motorStepPin, motorDirPin);

void setup(){
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);

int num;

void loop()
  if (stepper.distanceToGo() == 0)
    num = Serial.read();

When I try this, the shaft moves and immediately moves back to its origin position. When I replace the "num" in the arduino code for testing, everything works fine (the shaft moves half of a full rotation).

Has anybody an idea why this could happen?

Thank you very much!


What does the data you are writing out the serial port look like? Is it a byte or integer or string? Are you processing it correctly at the Arduino end? It sonds sort of like the motor is turning back to the ZERO position.



thank you for trying to help me. The output for the example above is:

Code: [Select]
>>>data = ''
>>>x = 180
>>>a = (x/360)*200
>>>data = bytes([int(a)])

So b'd' is the data sent. This works perfectly for my servos....

Any idea why it won´t work on the steppers?

Thank you


Hello! could well turn the engine?
I want to move a stepper motor from Blender and not how.


Never call Serial.read() unless you have first checked that there is a character available using
Serial.available() > 0....
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]


I think that the problem has to do with datatypes. Let's look at the Python code:

Code: [Select]

x = 180
a = (x/360)*200

x is an integer with the value 180. In the next line you divide the integer 180 with the integer 360 which becomes the integer 0 as it's rounded down, multiplied with 200 is still 0.

Change the code to:
Code: [Select]

x = 180
a = (x/360.0)*200.0

and the computation will be a floating point computation. You could also make explicitly convert x to floating point like so:

Code: [Select]

x = 180
a = (float(x)/360)*200

and still get the expected result.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131