PLEASE NOTE: This site is no longer being actively maintained. For frequently updated BeagleBone resources please visit Beaglebone.org.

Friday, August 22, 2014

On 1:00 PM by Anonymous

Overview

Our Beagle is ready for motoring and is stepping out...

Unlike common direct-current (dc) motors that spin continuously, stepper (stepping) motors are interesting motors that move in discrete steps. This unique functionality allows us to control the position of the motor shaft and determine where the shaft is --without position encoders-- (7 steps forward, 3 steps backwards...). Another advantage of stepper motor construction is that it does not use commutation brushes that wear out over time. Stepper motors are frequently used in 3D printers, CNC machines and applications where precise control of position is required.

Let's take a first step forward with this introductory project.

What You Will Need


Introduction

A stepper motor has two parts. A rotor (part that rotates) and a stator (stationary).
In general, the stator holds the windings/coils (or electromagnets) and the rotor is a permanent magnet attached to the shaft of the motor.

There are two fundamental stepper motor types: Unipolar and Bipolar.


Unipolar (5 or more wires)

Unipolar means one polarity.  In this wiring arrangement one side of the stator coil is connected to +V, while the other side is either connected to ground (0V) or disconnected (electronically).

The center common point of the stator windings is typically connected to +V, while the other ends are connected to an electronic switch circuit. (In five-wire types the two +V common points are tied together and a single wire is brought out of the motor casing).



The main advantage of this arrangement is that the electronic switching circuit is simpler.
The disadvantage is that more wire (coil) is required for the same output torque.

We will be using a unipolar stepping motor for this project.

Bipolar (4 wires)

Bipolar means two polarities.  In this wiring arrangement, the electric current with flow in both directions (one direction at-a-time).



The main advantage of this arrangement is that more torque can be produced by a smaller motor.
The disadvantage is that a more complicated electronic control is required (dual H-bridge).

Note:
Some stepper motors have more wires so users can configure the motor for either types.
Also there are torque-vs-speed considerations for the wiring arrangements.

The number of wires coming out of the motor gives you a clue to the type of motor.
You can determine the type of motor using a Digital Multi-Meter (DMM) and measuring the winding connections.

Stepping

Stepper motors require a sequence of pulses to control the rotation.
In Step 1 the rotor (magnet) is pointing between A and B since both coils are energized. In Step 2 B and A' coils are energized, so the rotor now point between the two coils (B, A') and so on.

In half-step mode a single coil is energized, and the rotor will point directly to the energized coil.  This provides addition shaft position resolution (8 steps versus 4 steps), this is at the cost of less torque.




A table of switching sequence summary of stepping sequence (half steps) is presented below.
In this tutorial, we will use full steps (skipping sequences 1,3,5,7). Reversing the sequence (right-to-left) will cause the motor to change direction.




Note that stepper motors can not instantly start to spin at a high step rate.
All mechanical systems take time to come up to speed.  Also the maximum step rate will be affected by the load (inertial mass) on the shaft.  Driving the motor steps too quickly or increasing the rate too fast will result in missed steps, and a jittery shaft motion. Maximum step rate is typically less than 300~500 steps-per-second and will vary by motor construction, load, acceleration and driving technique.

There are many additional motor driving techniques (half-step, micro-stepping, PWM, current drive) which are beyond the scope of this introductory project.

Practical motors have the equivalent of more than the four simplified coils.  This translates into more steps per revolution (a typical motor may have 1.8 degree of rotation per step).

The motor in this project also has built-in gearing which simplifies the mechanical design required in applications. This also means the the shaft will appear to rotate more slowly ( 5.625 deg/64 ) per step.

Instructions

The BeagleBone Relay Plus IO cape, requires a Device-Tree-Overlay file to correctly allocate the system resources (pins). The CBB-Relay-00A0.dtbo file is included in the debian Linux distribution.

At this time, the project has only been verified with Angstom Cloud9 version.

Verify that this overlay file is present in your system.

ls /lib/firmware/CBB-Relay*.*



If this file is missing, download it here, and copy it to /lib/firmware.

The BeagleBone device manager will read the data stored on the Relay Plus IO cape eeprom and load this file at boot time. See the Relay cape tutorial and product manual for more information.

NOTES:
1) Driving a stepper motor requires more current than can be provided by the USB cable.
2) An external power source is required.  Use this Power Adapter DC5V/2A.
3) You should not attempt to drive a motor requiring more than 250mA per phase.

STEP 1- Wiring the Stepper Motor



On the Relay Plus IO cape select VOUT to +5V by installing the jumper as shown.
Wire the motor using jumper wire pack.

You can use the relays to control a DC motor, small laser or other devices in combination with the stepper motor. There is also a R/C Servo Motor port for additional control. Four inputs allow you to add limit-switches to indicate when the motors have reached the end of desired travel.

Step 2 - BoneScript Program

Download stepDemo.js File
(and save it without the txt extension) to
/var/lib/cloud9

Program Listing

// Logic Supply - Tinkerer Workshop
// inspire.logicsupply.com
// Richard St-Pierre
// Feb 2014
//
// CBB-Relay Cape

var b = require('bonescript');

// Declare pins and variables
var M1A_Pin     = "P9_42";      //MotorA
var M1AN_Pin    = "P9_22";      //MotorA*
var M1B_Pin     = "P9_21";      //MotorB
var M1BN_Pin    = "P9_16";      //MotorB*
var K1_relay    = "P8_18";  
var K2_relay    = "P9_27"; 
var Fwd_button   = "P8_11";     // foward
var Rev_button   = "P8_15";     // reverse
var LED_Blue_Pin = "P9_24";     // LED
var K1_button    = "P8_12";
var K2_button    = "P8_14";

var newStepA     = 0;
var stepsPerRev = 1024;         //5.625 deg/64
//var dirA    = 0;    

console.log('Logic Supply: Stepper Motor Demo');

// setup pin Modes
b.pinMode(M1A_Pin,      b.OUTPUT);
b.pinMode(M1AN_Pin,     b.OUTPUT);
b.pinMode(M1B_Pin,      b.OUTPUT);
b.pinMode(M1BN_Pin,     b.OUTPUT);
b.pinMode(K1_relay,     b.OUTPUT);
b.pinMode(K2_relay,     b.OUTPUT);
b.pinMode(LED_Blue_Pin, b.OUTPUT);
b.pinMode(Fwd_button,   b.INPUT);
b.pinMode(Rev_button,   b.INPUT);
b.pinMode(K1_button,    b.INPUT);
b.pinMode(K2_button,    b.INPUT);

motorOff();
    
// Start with a small demo/test

    for (var i=0; i< stepsPerRev; i++){
        Fwd();
    }
    
    for (var i=0; i< stepsPerRev; i++){
        Rev();
    }

function Fwd(){
    newStepA++;
    if (newStepA >3)newStepA = 0;
    
    step(newStepA);    
}

function Rev(){                 // cw
    newStepA--;
    if (newStepA < '0') newStepA = 3;
    
    step(newStepA);
}
    
    

// Press FWD  (S1)  button to move Forward
// Press REV  (S4) button to movve Backwards !

console.log("Press and Hold  S1(FWD)   S4(REV)");
// sample input buutons every 200ms 
setInterval(inputHandler,50);  

function inputHandler(){
    b.digitalRead(Fwd_button, goFwd);
    b.digitalRead(Rev_button, goRev);
} 


function goFwd(x){
     
    if (x.value == '0'){
    b.digitalWrite(LED_Blue_Pin, b.HIGH);
    newStepA++;
    if (newStepA >3)newStepA=0;
    step(newStepA); 
    }
     b.digitalWrite(LED_Blue_Pin, b.LOW);
}

function goRev(x) {
    if (x.value =='0'){
         b.digitalWrite(LED_Blue_Pin, b.HIGH);
        newStepA--;
        if (newStepA < 0) newStepA=3;
        step(newStepA);
     }
     b.digitalWrite(LED_Blue_Pin, b.LOW);
}    

function k1(x){
    if (x.value =='0'){
    b.digitalWrite(K1_relay, b.HIGH);    
    }
    b.digitalWrite(K1_relay, b.LOW);  
}

function k2(x){
    if (x.value == '0'){
    b.digitalWrite(K2_relay,b.HIGH);
    }
    b.digitalWrite(K2_relay,b.LOW);
}
    

// create Stepper Motor Phases - Full Steps 
function step (newStepA){   
   
   console.log(newStepA);
    
    switch(newStepA){
        case(0):
          b.digitalWrite(M1A_Pin, b.HIGH);
          b.digitalWrite(M1AN_Pin,b.HIGH);
          b.digitalWrite(M1B_Pin, b.LOW);
          b.digitalWrite(M1BN_Pin,b.LOW);
          break;    
          case(1):
          b.digitalWrite(M1A_Pin, b.LOW);
          b.digitalWrite(M1AN_Pin,b.HIGH);
          b.digitalWrite(M1B_Pin, b.HIGH);
          b.digitalWrite(M1BN_Pin,b.LOW);
          break;
          case(2):
          b.digitalWrite(M1A_Pin, b.LOW);
          b.digitalWrite(M1AN_Pin,b.LOW);
          b.digitalWrite(M1B_Pin, b.HIGH);
          b.digitalWrite(M1BN_Pin,b.HIGH);
          break;
          case(3):
          b.digitalWrite(M1A_Pin, b.HIGH);
          b.digitalWrite(M1AN_Pin,b.LOW);
          b.digitalWrite(M1B_Pin, b.LOW);
          b.digitalWrite(M1BN_Pin,b.HIGH);
          break;
          default:
          }
}          
          
function motorOff(){
          b.digitalWrite(LED_Blue_Pin, b.LOW);
          b.digitalWrite(M1A_Pin, b.LOW);
          b.digitalWrite(M1AN_Pin,b.LOW);
          b.digitalWrite(M1B_Pin, b.LOW);
          b.digitalWrite(M1BN_Pin,b.LOW);
          console.log("motor off");
}

Program Overview

The Java-script program starts by loading the installed bonescript library.  The BeagleBone signal pins, that we will use, are then defined.  This makes it easier to follow the program. A quick message to the console let's us know we loaded the correct program.  We then need to initiate the signal pins as outputs or inputs.

It is a good idea to turn the motor OFF, using the motorOff() function, before we go any further, this establishes a known state (reset). An initial routine steps the motor forwards and then reverse to verify that our motor is wired and operating properly.  The Relay cape LEDs will blinks as the phases are changed. Note the shaft output is geared down (for more torque). If the motor does not turn in sequence (fwd then rev) verify the wiring of the motor wires.  You can change the step rate to study the phase sequence by changing the SetInterval timeout value.

After the initial demo, the motor can be controlled using the Relay cape push-buttons. Press-and-hold S1 (Fwd) or S4 (Rev) to move the motor shaft. Remember 64 internal steps are required to see the shaft move externally.

 We created two functions to move the motor forward 'Fwd()' and reverse 'Rev()'. These two functions simply increase or decrease the phase number (0..3). The function 'step()' actually energizes the motor coils, by applying the correct phase sequence for a 'full-step' sequence. A switch-case statement makes it easy to follow the code execution.

The functions k1() and k2() can be used to energize the Relays K1 and K2. These functions are not used in this project.

A motor OFF function comes in handy to remove power from the motor when not in use (keeping power ON will cause the motor to get warm, and waste energy).  Keeping motor power ON is required in some application so the shaft won't move under load.  This is a geared motor and the shaft not likely to move when de-energized.

Step 3 - Running the Program

Point you browser to the Cloud9 IDE (192.168.7.2:3000) and select the stepDemo.js file you downloaded in STEP 2.  Click the green [RUN] button in the Cloud9 IDE.

Remember to place the jumper VOUT to the +5V position and install the 5V/2A ac-adapter to power the Relay cape and motor.

Going Further

This is an introductory project about stepper motors, that has been requested by many. There is so much more to this exciting technology and many applications. High performance projects should consider using the PRUs built-in to the BeagleBone AM3358 ARM Cortex as an advanced way to control stepper motors (PRU overview coming soon).

We plan to add more advanced stepper motor projects soon, let us know what you would like to see in the comment below.