Click here to Skip to main content
13,894,726 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 23 Dec 2012
Licenced CPOL

MDrive – Part 2 of 4

, 23 Dec 2012
Rate this:
Please Sign up or sign in to vote.
Controlling intelligent precision electric motors from Windows applications written in C#

These posts focus on controlling intelligent precision electric motors from Windows applications written in C#. Specifically, we will be controlling a Schneider Electric M-Drive stepper motor with an integrated encoder and controller.


Posts in this Series

Reference Manual

The MCode Reference Manual is an invaluable resource for learning how to program the MDrive. Everything I describe here can be found in the reference manual.

Configuration and Movement Overview

MS=256 ‘ This sets micro-stepping to 256 micro steps per step. If MS=1, then the motor does not micro step. Micro stepping provides a higher resolution for shaft rotation, but also a much smoother movement at low speeds.

EE=1 ‘ This enables the built in encoder which monitors physical shaft rotations at a resolution of 2048 positions per revolution. With the encoder, the motor compensates for cases where the shaft does not turn with every step, and also detects how much the shaft rotates when the motor is not running.

Movement commands (described below) are dependent on MS and EE:

* MS=1 and EE=0, all movement is in units of 1/200 of a revolution
* MS=256 and EE=0, all movement is in units of 1/51200 of a revolution
* MS=256 and EE=1, all movement is in units of 1/2048 of a revolution

The rest of this section is written assuming MS=256 and EE=1.

A=2048 ‘ This sets the rate of shaft acceleration to 1 revolution per second.

D=4096 ‘ This sets the rate of shaft deceleration to 2 revolutions per second.

SL 2048 ‘ This will rotate the shaft continuously clockwise at 2048 counts per second, or 60 rpm. The shaft will accelerate to this 60 rpm using A. SL 0 will stop the shaft from rotating using the deceleration rate specified by D.

VI=10 ‘ This sets the initial shaft rotation speed to 10 counts per second.

VM=20480 ‘ This sets the maximum shaft rotation speed to 600 rpm.

MR 2048 ‘ This will rotate the shaft clockwise one full revolution. The speed of rotation is dependent on VI, A, VM, and D as the shaft respectively starts rotating, accelerates, reaches the maximum velocity, and decelerates. If the shaft should overshoot one full revolution, it will backtrack at the speed indicated by VI. Maximum velocity will not be achieved if there is not enough time to accelerate before having to decelerate.

P=0 ‘ This sets the current shaft position counter to 0. This does not move the shaft.

MA 2048 ‘ If P=0, this will move the shaft clockwise one full revolution. If P=4096, this will move the shaft counterclockwise one full revolution.

It is important to know that issuing multiple MR 2048 commands is not the same as issuing MA 2048, MA 4096, MA 6144, …. This is because the shaft may move a bit between each MR command and the errors may slowly accumulate. The MA moves to the indicated position even if the shaft moves a bit between each MA command.

VI is also the slowest speed to use when moving to a target position. Setting this value too low causes the motor to take a long time to find a target position. This value is also used in homing sequences. It is my opinion that these other uses of VI are a mistake by Schneider, and they should introduce other setting names for these uses.

DB=1 ‘ This sets how close to the target the shaft must be rotated before the movement is completed. The larger the dead band, the quicker the motor can achieve the desired rotation, but at the cost of accuracy. Recall that a high VI will also help speed up the final phase of each rotation. Yet if VI is too high, the shaft will oscillate back and forth on either side of the target – kind of like a bad day of golf where each approach shot passes over the green in a never-ending death spiral.

RC=25 ‘ This sets the percentage of current to deliver to the motor for movement. Setting this too low causes the motor not to be able to move the load on the shaft. Setting this too high causes the motor to use excessive current, get warm, and to cause light loads to be jerked around.

HC=0 ‘ This sets the percentage of current to deliver to the motor when not moving. Setting this too low allows the shaft to rotate without any resistance. Setting this too high causes the motor to use excessive current and get warm – though it is very difficult to move the stationary shaft.

SF=0 ‘ When running the motor, there is an expectation that the shaft rotate at such a speed to remain synchronized with the electric magnetic pulses of the motor. It is possible, due to a load, that the shaft does not rotate properly and falls behind. The embedded computer can detect this because it monitors the encoder while pulsing the magnets. It is not uncommon for the shaft to miss a few steps but still perform adequately for a given application. This setting allows you to define how many encoder steps the shaft needs to fall behind before the motor recognizes a stall.

SM=1 ‘ This setting defines whether or not the motor should stop if the shaft falls behind number of steps defined in SF.

Simple Example

The following example can be entered at the terminal command prompt “>”. For the commands I did not discuss above, refer to the MCode Reference Manual.

P=0 ‘ set the encoder to position 0
SL 2048 ‘ spin the shaft continuously
wait 30 seconds
SL 0 ‘ stops the motor
wait until the motor stops
PR P ‘ prints out the shaft position
MA 0 ‘ returns the shaft to position 0 (should take 30 seconds)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

John Michael Hauck
Software Developer (Senior) LECO Corporation
United States United States
John Hauck has been developing software professionally since 1981, and focused on Windows-based development since 1988. For the past 17 years John has been working at LECO, a scientific laboratory instrument company, where he manages software development. John also served as the manager of software development at Zenith Data Systems, as the Vice President of software development at TechSmith, as the lead medical records developer at Instrument Makar, as the MSU student who developed the time and attendance system for Dart container, and as the high school kid who wrote the manufacturing control system at Wohlert. John loves the Lord, his wife, their three kids, and sailing on Lake Michigan.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190306.1 | Last Updated 23 Dec 2012
Article Copyright 2012 by John Michael Hauck
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid