Saturday 30 September 2017

Stepper motors


Introduction

For some applications, stepper motors are useful. precise alignment of the motor axis in the preset position. The problem with control is that the control signals are sent hundreds, thousands of times per second.

Devices

In my test I use chap driver build on ULN2003 chip and stepper motor  28BYJ-48

Cheap ULN2003 stepper motor driver


Arduino Nano connection with two ULN2003 drivers and 28BYJ-48 motors

Solution


In my idea, control is separated by the following:
- main control app is write in Java and working on RaPi. App use library to communication with Arduino and sending high level intruction: turn left 20 steps, turn right 100 steps, turn on motor pernamently, stop motor, etc.
- on Arduino is installed app, receiveing command from USB port (from RaPi) encode and send appropriate signals to  selected motors.
- app can control unlimited Arduino modules, and control max 3 motors per Arduino (limit is the number of arduino outputs)

Idea of stepper motor control

How to install


Download Arduino sources from stepper_motor_ctrl_by_serial.ino, use Arduino IDE and compile and install on Arduino.

Download jar library stepper-motor-connector-4j-0.1.0.jar and add to your Java project.

Full project sources are available on GitLab https://github.com/SulacoSoft/stepper-motor-connector-4j.

Available functions


Init motor on port COM7 (Windows USB port name, on linux example name is ttyUSB7) and define stepper motor controler pins on Arduino 6, 7, 8, 9:
StepperMotor motor = StepperMotorFabric.create("COM7", 6, 7, 8, 9);

Rotate 2048 steps to left:
motor.rotateLeftSteps(2048);

Waiting to estimated time end operation (rotateLeftSteps or rotateRightSteps):
while (!motor.isReady())
    Thread.sleep(10L);

Turn right 512 steps:
motor.rotateRightSteps(512);

Turn pernamently rotate right:
motor.rotateRightOn();

Turn pernamently rotate left:
motor.rotateLeftOn();

Stop motor (when motor is run rotateLeftOn or rotateRightOn):
motor.rotateOff();

Disconnect motor (releas pins and motor instantion):
StepperMotorFabric.disconnect(motor);

We can change delay time between motor steps in microseconds. This parameter change delay time on all connected motors to selected Arduino board:
motor.setDelay(1200);

We can connect several motors:
StepperMotor motorA = StepperMotorFabric.create("COM7", 2, 3, 4, 5);
StepperMotor motorB = StepperMotorFabric.create("COM7", 6, 7, 8, 9);

Completly example


Full Java example:

import com.sulacosoft.robots.steppermotor.StepperMotor;
import com.sulacosoft.robots.steppermotor.StepperMotorException;
import com.sulacosoft.robots.steppermotor.StepperMotorFabric;

public class Main {
  public static void main(String[] args) throws StepperMotorException {
  StepperMotor motor = StepperMotorFabric.create("COM7", 2, 3, 4, 5);
    try {
      motor.rotateRightSteps(4096);
              while (!motor.isReady())
        Thread.sleep(10L);
              motor.rotateLeftOn();
      Thread.sleep(2000);
      motor.rotateOff();
         } catch (InterruptedException e) {
      e.printStackTrace();
    } finally {
      StepperMotorFabric.disconnect(motor);
    }
  }
}



links:
https://github.com/SulacoSoft/stepper-motor-connector-4j
https://www.codeproject.com/Tips/801262/Sending-and-receiving-strings-from-COM-port-via-jS
https://github.com/scream3r/java-simple-serial-connector
https://github.com/nyholku/purejavacomm
http://kishor15389.blogspot.hk/2011/05/how-to-install-java-communications.html
https://code.google.com/archive/p/java-simple-serial-connector/
http://search.maven.org/#artifactdetails%7Corg.scream3r%7Cjssc%7C2.8.0%7Cjar
http://www.instructables.com/id/BYJ48-Stepper-Motor/
http://42bots.com/tutorials/28byj-48-stepper-motor-with-uln2003-driver-and-arduino-uno/