Pages: [1]   Go Down
Author Topic: Python, arduino and stepper motor won´t work  (Read 1215 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

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:
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)])
    ser.write(data)
bpy.app.handlers.frame_change_post.append(my_handler)

Arduino code:

Code:
#include <AccelStepper.h>

int motorSpeed = 115200;
int motorAccel = 8000;

int motorDirPin = 9;
int motorStepPin = 8;

AccelStepper stepper(1, motorStepPin, motorDirPin);

void setup(){
  Serial.begin(115200);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  stepper.setMaxSpeed(motorSpeed);
  stepper.setSpeed(motorSpeed);
  stepper.setAcceleration(motorAccel);
}

int num;

void loop()
{
  if (stepper.distanceToGo() == 0)
    num = Serial.read();
    stepper.moveTo(num);
    stepper.run();   
}

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!
Logged

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1024
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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

Code:
>>>data = ''
>>>x = 180
>>>a = (x/360)*200
>>>print(a)
100
>>>data = bytes([int(a)])
>>>print(data)
b'd'

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


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11694
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Never call Serial.read() unless you have first checked that there is a character available using
Serial.available() > 0....
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Newbie
*
Karma: 1
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:
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:
x = 180
a = (float(x)/360)*200

and still get the expected result.
Logged

Pages: [1]   Go Up
Jump to: