DC Motors
The beginners tutorial explained how DC motors worked and how to control them with a micro controller or the Robocore. This intermediate tutorial will look a bit more closely at the DC motor and its characteristics.
We learned that reversing the polarity of the supply current controls the direction the motor rotates. This is not the only technique that can be used to control the motor. Changing the voltage supplied to the motor can also vary its speed. But your motor controller only has 2 settings, on and off, so how can the voltage be temporarily changed? Enter a technique called pulse width modulation.
Pulse Width Modulation:
This is a technique where pulses of electricity are fed into the motor at a fairly fast rate to produce an average voltage effect. To help us understand this lets look at a few examples.
Lets say that for our pulse we'll turn on 10 volts for 40mS (40 thousandths of a second) and then we'll turn the voltage off for 10mS. If we repeat this cycle over and over the voltage is changing so quickly that the on's and off's become an average voltage. In this case the voltage is off for 20% of the time, so the average voltage to the motor is 80% of 10 volts, which is 8 volts. This will cause the motor to run slower than at 10 volts.
Therefore the speed of the motor can be changed by varying the amount of time the current is on and the current is off.
The DACPin command can be used with the motor drivers on the Robocore. Full syntax for the command can be found in the document files supplied with the Basix X software but the basic command is:
Call DACpin(Pin, Voltage, DACcounter)
Pin = The output pin
Voltage = Byte value between 0 and 255
DACcounter = The function must return a value in this variable. If more than one pin is using DACPin then each pin must use a differently names variable.
The actual results obtained once the pulse has run through the motor driver chips will vary depending on the voltage used but typically a 25% reduction in power can be achieved. With most DC motors any further reductions will not supply the motor with enough power to operate.
Torque:
At this point I would like to say a little bit about torque. Torque is a measurement of the motors power. The higher the torque of the motor the more weight it can move. DC motors provide different amounts of torque depending on their running speed, which is measured in RPM (revolutions per minute). At low RPM DC motors produce poor torque, and generally the higher the RPM, the better the motors torque.
So what does this mean in practical robotics terms? Lets say that a robot is propelled by 2 DC motors. Using gears to reduce the overall speed of the robot and running the motors at top speed will result in the most power being delivered to the wheels. Using pulse width modulation too slow the motor will result in the motors not delivering less torque to drive your robot forward.
Pulse width modulation is still a very useful technique to use as it gives the programmer control over the robots speed purely using software. Sometimes you might want to slow your robot down a little, for reversing away from obstacles or turning on the spot for example.
The beginners section generally talked about what servos are and what they can do. This section is going to look more closely at how servos work and how we can program them.
How Servos Work:
To help us to understand how to control servos it may be helpful to take a closer look at how they work. Inside the servo is a control board, a set of gears, a potentiometer (a variable resistor) and a motor. The potentiometer is connected to the motor via the gear set. A control signal gives the motor a position to rotate to and the motor starts to turn. The potentiometer rotates with the motor, and as it does so its resistance changes. The control circuit monitors its resistance, as soon as it reaches the appropriate value the motor stops and the servo is in the correct position.
Controlling Servos:
Servos are positioned using a technique called pulse width modulation. This is a continuous stream of pulses sent to the servo. The pulse normally lasts for between 1ms and 2ms, depending on the positioning of the servo. The pulse has to be continually repeated for the servo to hold its position, usually around 50 to 60 times a second. It is the actual pulse that controls the position of the servo, not the number of times it's repeated every second.
A 1ms pulse will position the servo at 0 degrees, where as a 2ms pulse will position the servo at the maximum position that it can rotate to. A pulse of 1.5ms will position the servo half way round its rotation. The diagram below shows 3 typical pulses.
The diagram is not to scale but hopefully demonstrates that each pulse must be the same length. That is the combined time that the pulse is on and off.
Wednesday, February 13, 2008
Robotics Tutorials - Motors
Programming Tutorials
Beginner - Programming Introduction
This is an introduction to programming using the BasicX micro controller, if you have never programmed before this will help you on your way. More experienced readers should still skim through this to get the basics of the language.
Programming is a common language between you and your Robocore, it lets you tell it what to do. In robotics, programming is necessary to make machines that can work by themselves, without human intervention. These are called autonomous robots.
1) Lets start by connecting your Robocore to the power supply and the serial port of your computer, and loading up the BasicX software.
2) When this is loaded, click on the Monitor Port menu and select the COM port that the Robocore is connected to (probably COM1). The Download Port menu should be set to the same COM port.
3) Click the editor button. A window should open asking for a filename. Just type a name for your first program, lets say Demoprog. It will tell you that the file does not exist, so do you want to create it. Click yes.
Now lets write our first program!
To test that the BasicX is working, we will make it send a message back to your computer. This is done using the debug.print command.
Type the following program into the editor window (or copy and paste it). The first and last lines should already be present, if they are just copy the middle line.
Sub main()
debug.print "Robocore test: Everything is working"
End Sub
Before we can download to the BasicX we need to set the chip preferences. This basically tells the chip what we want to be doing with each pin (input, output etc). To do this click on the project menu, followed by chip (or the F7 shortcut). We can leave everything as it is for now so just click on OK.
Now click on the Compile menu and select Compile and Run (keyboard shortcut F5) This will send the code to the Robocore. Going back to the main BasicX window, you should see the text ' Robocore test: Everything is working ' appear on the screen.
The Sub main() and End Sub commands tell the Robocore where the program begins and ends.
The debug.print command is very useful for telling us what the Robocore is doing, in the next case we will see more of its capabilities.
Now we will make the Robocore solve an equation for us. To do this we will need a variable. This is a value stored under a name in the memory of the Robocore. We will give this variable the name ' answer '.
Sub main()
Dim answerI As integer ' declaring
Dim answer As string ' variables
answerI = 5*5 ' doing the maths
answer=Cstr(answerI) 'convert to printable format
debug.print "5 times 5 is "; answer 'print answer
End Sub
Run the program as before and you should get the computer telling you the answer.
This program shows that you can print values from your program to the screen using debug.print just by adding a semi-colon and the variable name after the "text", this is very handy for example when testing sensors.
The program also uses comments, these have no effect on the program but allow us to add information about each line of the program. The computer ignores everything written on the line after an apostrophe (').
This program has also introduced us to variables, the topic for the next tutorial in the programming series.
Programming Variables
Variables are values stored in computer memory. We need to use them for almost every program to allow the Robocore to remember things. There are various different types of values, for example strings, which are text values, and integers, which are whole number values.
The computer needs you to tell it what type of value you want to use, otherwise it will get confused.
Each type of variable has its uses and limitations, which is why there are simple methods for switching between variable types. This was shown in the last tutorial, lets look at that program again line by line.
Sub main()
Dim answerI As integer ' declaring
Dim answer As string ' variables
The Dim lines declare the variables, that means they set them up for use in the program. The type of variable must be specified using the ' As ' keyword. Here we have an integer and a string.
answerI = 5*5 ' doing the math
Here the integer variable is used, this is because mathematical functions can be performed using it such as multiplication
answer=Cstr(answerI) 'convert to printable format
Using the CStr statement the integer value held in answerI is converted into a string and held in answer, this is a text variable allowing the answer to be printed to the screen using debug.print.
debug.print "5 times 5 is "; answer 'print answer
End Sub
Variables can have nearly any name, but you cannot use words that are otherwise used in the programming language, and it is best to keep them simple, either a single letter, or a word describing what the variable does.
Other types of variables are available. One of the types used commonly in robotics is the Boolean variable. This provides us with a true or false value.
For example a bump sensor will either be pressed or not pressed, so the Boolean variable for it will either be true (pressed) or false.
We set up Boolean variables using the following statement
Dim variable As Boolean.
variable = true
Boolean logic is commonly used with conditional statements, these are covered in the next section.
There is also the Single variable type, which allows floating point numbers
e.g 2.43 or -0.66, this is occasionally useful in robotics when more maths is necessary.
Programming Loops and Conditions
Until now our programming has been limited to running through the code once, and then the program stops functioning. This is clearly not suitable for a robot, which must be able to follow it's commands continuously for the length of time it is switched on. To do this we use loops.
We also want our robots to make decisions based on data from its sensors, for this we need conditional statements. Loops themselves contain conditional statements in order to tell them when to run and how long to continue.
There are several different loop commands, letting us control the function of the robot in a variety of ways.
Do Loops:
The simple Do loop can be used to enclose your whole program, when it reaches the end of the code it returns to the beginning and starts again. We can apply this to our program used in the introduction.
Sub main()
Dim answer As string
Dim answerI As integer
Do 'start of loop
answerI = 5*5
answer=Cstr(answerI)
debug.print "The answer is "; answer
Loop ' end of loop
End Sub
As you will see when you run this program, the statement is no longer displayed once, but continues to print as long as the program runs (you should press the reset button to stop it)
Now for something slightly more useful. We can add a conditional statement to the do loop by using a ' while ' statement. This lets us tell the Robocore when to stop looping.
Sub main()
Dim answerI As integer
Dim answer As string
Dim i As integer
i=0
Do While (i < 10)
answerI = 5*5
answer=Cstr(answerI)
debug.print "The answer is "; answer
i=i+1
loop
End Sub
Using the conditional statement ' while ' and a new integer variable i we have made the Robocore loop only 10 times (note that as 10 is not less than 10 the program will stop after the I = 9 loop)
For Loop:
We can set up the same loop using the for statement remove the Do while, Loop and i=i+1 lines and replace them with:
Do While For I = 1 to 10 Step 1
Loop Next
The loop runs from 1 to 10 stepping up 1 each loop, this removes the need for the line I=I+1.
Run the program to check that you have got it right.
Now for something a little more complex, instead of just doing one sum, we can automate the Robocore to do 10 different sums for us by changing the equation to
AnswerI = I*5
Now the Robocore takes the number of loops completed and multiplies it by five. When you run this program you should now get the answers 5,10,15 up to 50.
If Statement:
We can run a conditional statement without using a loop with an if statement. These are used as follows
Const switch as Byte = 5
Sub Main()
Do
if (GetPin(switch)=1) then
debug.print"switch pressed"
call sleep(0.15)
end if
Loop
End Sub
The best way to learn is to experiment, make your own program with what you've learnt and you'll soon feel more confident