Subscribe To Robotics | IntroDuction | History | Home


Friends Dont Forget To check the archieve at the left end of the page



Make Your Own Robot Tutoials



Simple Beetle Bot | Wired Robot | Combat Robot | Solar Engine |



Beam Symet | Photopopper | Beam Trimet | Line Follower |

Latest Updates
Driver Less Car | I-Sobot | MotherBoard | MicroController | Artificial Brain |

Camera Sensors Hardware | Remote Control Working

Google

Wednesday, February 13, 2008

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

1 comment:

Sumit said...

i have been going through ur articles
they are amazing and provide a whole lot of information and that to good ones
but dont u think people like me who r just staring with robotics would be able to understand all
i am specially talking about this particular article
it would be really helpful if u could start right from the scratch , basics, imagining the person has got no idea abt all the codes and softwares u r talking about

Just an advice
keep up the good work