Programming Motor Controllers:
How To Use Flowstone Software With Popular Robotics Platforms
Welcome to the FlowStone Workshop where this time we give a beginner’s guide to programming robotics. This issue we are going to cover Motor Controllers, what they are, and how to program them using the FlowStone free graphical programming language. Before we start coding lets look at some of the current range of motor controllers available and how they work. Motor controllers are probably one of the most fundamental components used in robotics today. If you think about it, robotics is all about movement and the majority of that movement is created via electric motors. In order to control an electric motor you need a motor controller. Electric motors come in three major types, DC Motors, AC Motors and Stepper Motors. AC Motors are mainly used in heavy industry so we will focus on DC and Stepper Motors in this feature.
DC Motors are controlled via Direct Current (DC) this is typically a voltage from a battery say 12V DC. They can go both for- wards and backwards depending on the polarity of the voltage, so swapping the wires will reverse the direction. The issue with DC motors is that if you connect them to a voltage they run at a fixed speed. If you lower the voltage the speed isn’t affected a linear manner and the motor becomes inefficient. However there is a solution: Pulse Width Modulation (PWM)! If you read the FlowStone Workshop on Servo Controllers in the last issue of Robot we explained in detail about pulse width modulation as it is also used for servos. To recap — pulse width modulation is simply turning on and off the full voltage for a short period of time; the amount of time is called the pulse width. For a DC Motor you can vary the pulse width thousands of times a second to gain accurate control of the motor speed.
In order to get the best efficiency out of the motor, high PWM frequencies are used, usu- ally around 20-50kHz. You could use a slightly lower frequency say 15kHz but then you are entering the audio frequency range and you can often hear the motor singing. So generally higher frequencies are used outside of the human audio range, e.g., > 20kHz.
The other issue with DC motors is the amount of electrical current required.
If motors run freely a nominal or quiescent current is required. If you load your motor (e.g., going uphill or lifting something with a robot arm) the required current to maintain the speed raises considerably. So unlike our servo examples where the PWM signal were all TTL logic levels, for the motor we need some heavy duty FET transistors to deliver the increased current. These can get very hot so often large heat sinks are used to help dissipate the heat. The final issue with DC motors is positional information, there isn’t any! So they will run forwards or backwards, fast or slow, but you have no idea where the shaft is pointing at any given time in its rotation. To solve this issue some sort of positional shaft encoder or potentiometer is needed to give feedback on the motor position. This is in fact how a servo motor works, as the motor moves a rotational potentiometer (POT) for feedback on how far the servo has moved.
Stepper motors are very different to DC motors in that they can move in steps (as the name suggests). These also use DC current but instead of a single pair of wires they have several pairs of wires to control individual coils inside the motor. Brushless motors are also a form of stepper motor with fixed permanent magnets. Whereas with a DC motor you can just connect it to a battery and it will run, with a stepper motor you need sophisticated control electronics to sequence the individual coils to get the motor to move. Fortunately this is all self contained in the motor controller, but it does all add to the cost!
To explain how a stepper motor works you can think of it as an electric kitchen clock. Imagine that there is only one hand and that on the end of the hand is a magnet. At each hour mark there is a magnetic coil (so 12 coils in total) that when energized attracts the end of the magnetic hand. If we energize the 12:00 o’clock coil the hand points to 12; if we then turn off the 12 and energize the 1:00 o’clock coil, the hand will move to the 1 position. If we repeat this process turning on and off the coils in sequence we will have a precise rotational movement. The key benefit being that at any given time we know exactly where the hand on the clock is pointing. If we expand on this you can see that by changing the sequence to run backwards we can change the direction of the motor.
We could also do other things like turn on two adjacent coils to move the hand to a half hour position or extend the hand to twice the length and put a magnet on each end, this way you could energize the coils in opposite pairs to increase the holding power.
In reality there are various techniques employed to maximize the number of steps and also the holding power and these can be seen in the stepper motor specifications. These can be from the number of steps per revolution to the motor torque available to hold it’s position.
DC VS STEPPER
So now you have an insight to how these motors work, which is the best for your application? As always it usually comes down to price, DC motors are very cost effective and powerful but are not so great when precision is required. Stepper motors are more expensive, bigger (for the same power), more complex, but are very precise.
USB VS RS232 VS PWM VS CV
On the whole the various motor controllers are controlled via USB, RS232, PWM or “control voltage” (CV). The USB models have their own driver and applications interface (API) in this case already programmed in FlowStone. Some use a standard Com port and RS232, or even a virtual comport via USB, or some have an option to take a PWM TTL signal (like out of a servo controller) to control the motor speed. USB & RS232 are the most common and they have different merits. USB is limited by cable length, usually around 6ft, and can carry low current power for the motor controller electronics (but not for the Motor Power!). RS232 can go up to around 50 ft but has no power. However you will always need some high current power for the motor anyway, so this is no big deal. For PWM you will need an additional servo controller as well as the motor controller. Finally CV allows you to send DC voltage say from 0 to 5V to control the speed.
|Pololu Simple Motor Controller||DC||USB, RS232, PWM,CV|
|Dimension Engineering Sabertooth||DC||RS232, PWM, CV|
|Robot Electronics MD 25||DC||RS232, 12C|
|Phidgets DC Motor Controllers||DC||USB (API)|
|Phidgets Stepper Motor Controllers||Stepper||USB (API)|
NOW THE PROGRAMMING
Since most of the motor controllers use some form of com port (Virtual or RS232) we will start by looking at how to program these using FlowStone. If you haven’t done so already, you can download FlowStone FREE from the DSPRobotics site. The first thing to do is connect your motor controller to the PC and identify the com port number it is connected to. You can do this by looking in the Windows Device Manager under ‘Ports (LPT & Com)’. You can also edit the setting if necessary to change the allocated com port number (some PC systems only work if the com port number is <8).
In FlowStone there is a Com Port module ready done for you so that you just need to add some parameters and send out the correct data to the motor controller.
COM PORT SETTINGS
Com Ports need the following information to work: Port Number, Baud Rate, Stop Bits, Parity, & Byte Size.
Port Number: This is the Com Port Number.
Baud Rate: this is the speed the data is sent. Stop Bits: These indicate the end of the data word and typically are set to 1. Parity: This is error checking, nowadays this is not used so usu- ally set to zero. Byte Size: This is the number of bits that make up the data byte. Termination: Usually (Carriage Return, AKA New Line or ASCII 13) The final choice is how the data is represented, as ASCII or as Hex?
Baud Rate: this is the speed the data is sent.
Stop Bits: These indicate the end of the data word and typically are set to 1.
Parity: This is error checking, nowadays this is not used so usu- ally set to zero.
Byte Size: This is the number of bits that make up the data byte.
Termination: Usually (Carriage Return, AKA New Line or ASCII 13) The final choice is how the data is represented, as ASCII or as Hex?
WIRING UP THE COM PORT MODULE IN FLOWSTONE
Search for the ‘Com Port’ module in FlowStone and drag it on to the schematic window; from here you can add the required settings to make your comport work. Add three Integer boxes to add the set- tings for the: Port number (eg. 1), Baud Rate (eg. 9600) and Byte size (eg. 8). The stop bits and parity can be left blank as the default values will work for us. We can then add a String box to enter our data string for the motor control information plus a terminator if necessary added to this data string (not to be confused with the incoming Terminator input on the Com port module this is for incoming data!). Finally we add an After Load module that sends out a trigger pulse when the project is opened. This is useful for opening the Com Port on start up so that everything works automatically.
CREATE YOUR DATA STRING
The data string is the core information sent to the motor controller to tell it what to do. For our example we are going to send control data to the motor controller to vary the speed from zero to full in both a forwards and backwards direction. Each motor controller manufacturer will have its own protocol for talking to their board, so you will need to read the manuals to see what format this data takes.
POLOLU SIMPLE MOTOR CONTROLLER
We are using the Simple Motor Controller 18V15 but the data structure here will control all eight of the Simple Pololu Motor Controllers, the only difference being the power and max voltage of the individual boards.
For this example we are going to use the Binary Pololu Compact Protocol. For this to work with the FlowStone Com Port we need to use the Hex mode (Set the Com Port Hex I/P to True).
Exit Safe Start: 0x83 – Send this byte first to enable the motor. Failure to send this will disable the motor output.
Motor Forward: 0x85, Data1, Data2
This command tells the controller how fast it should move in the forward direction. The Max speed is a value of 3200 split into two data bytes. In order to split the speed variable into two bytes you need to do a bit of math.
Speed (Data1) = Speed & 0x1F Speed (Data2) = Speed / 32 We made a simple FlowStone Module to do this and convert the two bytes into Hex. So for example half speed forward = 1600 = 850032 (in Hex) Motor Reverse: 0x86, Data1, Data2
This is the same as above only in the other direction so we can use the same speed math but swap the first byte for 0x86 to set the reverse direction.
So for example half speed reverse = 1600 = 860032 (in Hex)
Graphical Control: In order to have some graphical control we added a slider module set to 0 – 3600 and a direction switch Forward or Backwards.
Send Data: In order to send the data to the Motor Controller we used a Tick25 module that generates a trigger pulse 25 times a second. This was connected to the Send Data input.
DIMENSION ENGINEERING SABERTOOTH CONTROLLERS
We are using the Sabertooth 2×10 Motor Controller, this has two motor outputs and has a very simple data structure via RS232. The baud rate is 38000 and the data is sent via a single Hex byte (0-255 or 0x00-0xFF). The connection is via TTL RS232 so we used a USB – TTL RS232 convertor.
Reverse: 1 – 63 or 0x01 – 0x3F
Stop: 64 or 0x40
Forward: 65 -127 or 0x41 – 0x7F
Reverse: 128 – 191 or 0x80 – 0xBF
Stop: 191 or 0xC0
Forward: 193 – 255 or 0xC1 – 0xFF
Graphical Control: The way we controlled this using FlowStone was to use two sliders one for each motor. Slider1 was set to 1-127 and Slider2 was set to 128-255. Both sliders were connected to an Int Switch that just selects the input that last changed. Finally we converted the numbers into Hex using the Int-Hex module before it went to the data input.
Send Data: As with the Pololu boards we used a Tick25 to send the data 25 times a second.
ROBOT ELECTRONICS MD25
We are using the RD02 drive system or basically a MD25 + motors and wheels. The data structure here uses a sync byte to signal new data is coming, plus various modes to choose if both wheels should turn or just one, etc.
Set Mode: 0x34 0= 0 (Full Reverse) 128 (Stop) 255 (Full Forward).
2 = Speed1 controls both motors, and speed2 becomes the turn value:Data is in the range of 0 (Full Reverse) 128 (Stop) 255 (Full Forward).
3 = Mode 3 is similar to Mode 2, only the Data is in the range of – 128 (Full Reverse) 0 (Stop) 127 (Full Forward)
Data Word Set Mode 0 = 003400 (Hex) Motor 1 Forward: 0x31
If the Mode is 0 (Default) then: Sync: 0x00 Code: 0x31 Data: 0xFF (Full Forward) Data Word = 0031FF (Hex)
Motor 1 Reverse: 0x31 Sync: 0x00 Code: 0x31 Data: 0x00 (Full Reverse) Data Word = 003100 (Hex)
Motor 2: Is the same as above only the data code is 0x32
Graphical Control: Again we used two sliders one for each motor. Both sliders were set to 0- 255 and converted to hex to add to the sync byte and the code byte. Send Data: In this case we just connected the data string to the send trigger input so that the data would be sent when ever the data changes.
PHIDGETS DC MOTOR CONTROLLERS
We are using the Phidget 1064 & 1065 motor controllers, but since these are already fully supported in FlowStone there really isn’t much programming required, no com ports, just plug and play. The key thing is to remember to start the device using the Start Input on the FlowStone module, then it’s just a case of setting the velocity input.
Motor Forward:Velocity = +100 Motor
Stop: Velocity = 0 Motor
Reverse: Velocity = -100 There are other inputs to control the acceleration and select a different motor if appropriate.
Graphical Control: To control our motor we just added a slider with the range set to +/- 100, this was connected to the Velocity input on the Phidgets Motor Controller Module.
PHIDGETS STEPPER MOTOR CONTROLLERS
We are using the Phidget 1062 & 1063 stepper motor controllers and as with the DC Motor controllers this is already pre-programmed inside FlowStone. So it’s just a case if wiring up the inputs on the module to get it working. Since this is a stepper motor the control is all about position. So the main input control is Motor position +/-20000 steps, there are also inputs for velocity and acceleration, plus current for the holding torque.
Motor Position = 5000 Motor Velocity = 200 Motor Acceleration = 1000
Graphical Control: We have added a slider set to +/- 20000 for the motor position and left the velocity and acceleration as constants.
One option you can use inside FlowStone is the PID Module (Proportional Integral Derivative) to help control your motor speed. This is the same type of algorithm used in car cruise control, where you can set a target speed and feedback the actual speed. The PID algorithm will then increase the motor speed proportionally until it reaches the target speed. This is very useful as it creates a smooth acceleration without any overshoot, plus it will compensate for extra load like going up a hill.
Hopefully, this introduction has provided a good insight into how motor controllers are programmed on a basic level. Many of the controllers featured have far more features than we have covered here, like temperature sensing, speed sensing, feedback inputs, and more, but what we have shown will certainly get you going. As always the FlowStone projects featured are available to download from the DSPRobotics support forum under Example Schematics. Using FlowStone can really speed up your first steps into programming robotics and lets you concentrate on the job at hand – interacting with your robot. Next issue we will be looking at how to program Data Acquisition (DAQ) boards.
Dimension Engineering, www.dimensionengineering.com
FlowStone FREE Download, www.dsprobotics.com
Phidgets USA, www.phidgetsusa.com
Robot Electronics, www.robot-electronics.co.uk