Sie sind auf Seite 1von 7

Controlling the robot from PC

Submitted by dolinay on July 28, 2008 - 9:39pm.

For controlling the robot from PC a Windows program was developed in C# including class which encapsulates the control of the robot. With this class it should be fairly easy to implement your own interface for controlling the robot if you know C#. For those not familiar with this programming language there is an option to write their own algorithms in easier way as will be described later. All that is needed is Microsoft Visual C# Express, which is free. [9].

Fig. 5.1 Application for controlling the robot from PC This program allows executing all the supported commands. Source code is provided in the Downloads section. http://1000projects.org/ece-project-topic-on-pc-controlled-temperature-andobstacle-sensing-robot-with-full-report.html Communication protocol Sending a command to the robot means writing single byte to serial port. The value of the byte to send can be calculated from the values of commands as defined in ServoDriver.H but in C# program you can use the RobotCommander class provided in the sample application. This class provides methods such as WalkStraight, TurnLeft, etc. Some commands require input parameter, such as speed for the walk command. Value of the parameter is also contained in the command byte. Each command including its parameter is only one byte wide. The lower 3 bits of the byte are reserved for parameter value. Higher 5 bits encode the command. The robot sends data over serial line only as an answer to commands Get sensor state and Get robot state. The data consist of 1 byte. The returned value is unique. For example, value 1 could mean left sensor is active, value 2 - left sensor is not active, value 3 - right sensor is active, and so on. This allows the PC program to send command to the robot asking for data and receive the answer asynchronously in a separate routine.

Description of the PC control application The included application Robot Control serves 2 purposes:

You can use it to experiment with the robot from PC including command at very low level such as setting position of servos. But you can also write your own algorithm for the robot in and run it from this application (with the Run Program button). You can use the application as an inspiration for your own program written in C# which can control the robot in any way you want. Using the RobotCommander class this is quite an easy task if you have some experience with C#. How to use the application
Connect the robot to your PC using serial cable Start the Robot Control application In the Com port area select the port to which you have connected the

robot (e.g. 1 if you connected the cable to COM1 port).


The baud rate should be 9600 In the Options area select speed of the robot. Medium will be a good

choice for first time.


Click Forward button to make the robot walk. It should start walking

within about 1 second after you clicked the button.


Click the Stop button to stop the robot. Try out the other commands To see sensor states check the Automatically refresh sensors box

in the Options area. When sensor is signaled the box at the top of the window should become checked. Click the Reset button to allow new activation of the sensor.
You can also experiment with the low level commands in the Manual

servo control area Clicking each button will move the servo into given position. See the Walking algorithm section for information about the symbols used for servo positions.

Run program button

When you click this button an autonomous algorithm will be started which controls the robot from PC. The default algorithm provided here is the same as the one which is hard coded into the MCU program. But you can change it easily if you open the project in Visual C# and edit RobotProgram.CS file. More details on this follow.

Writing your own movement algorithms There are two options for writing your own program to control the robot from PC: 1. 2. Write your own program in C# using the RobotCommander class Write just the algorithm and let the sample application execute

it using the Run Program button. Actually, there is a third option to write your own program in any language. You just need to be able to handle serial communication in this language and look up the codes which you need to send to the serial line in ServoDriver.H. For option 1 you need to know C#. You may want to have a look at the description of the classes available for controlling the robot below. For option 2 you dont need to know C# even though you will be actually writing a C# program. The sample application includes Run Program button which executes a method (function) into which you can write your own code. Obviously you need to have some understanding of programming (basic knowledge of C is a big plus), but you do not need to know what is a class, nor anything about object oriented programming (which scares some people). All you will be doing is writing simple sequence of commands.

Here is how to do it. I assume you have Microsoft Visual C# Express edition [9] installed and that you played a little with it so that you know how to open a project and build it.
Download and extract the source code for the sample application

RobotControl (see Download section)


Open the project in Visual C# Open the file RobotProgram.CS Scroll down a little in this file to locate this line: public void Move()

This is the function which will be executed when you click Run Program button in the main window. Into this function you can write your program to control the robot. See the sample program which is already there. It is in fact PC version of the simple autonomous algorithm programmed in the MCU of the robot. The names of the functions are understandable I hope, so just a couple of notes: When execution reaches the end of the Move function (the closing brace }), your program ends. To start it again you need to press the Run Program button again and it will start over from the first line of Move function. When you give the robot some command, e.g. GoStraight(); it will start walking and it will walk until you tell it to do something else (for example to stop). The same applies to turning and walking back) You can use loops (such as while or for) to prevent your program from ending. For example, most of the code of the sample program is in the following loop:
while ( !ShouldStop()) { CODE }

The function ShouldStop() returns true if user requested your program to stop clicked the Stop Program button which appears once the program is started. So,

the sample code makes the robot walk (and try to avoid obstacles) until you click the Stop button. To find out if there is any obstacle in front of the robot, use GetLeftSensor() and GetRightSensor() methods. Both return true if the sensor was signaled (touched something). Note that once the sensor touches something and becomes signaled, it will remain signaled until you reset it by calling ResetLeftSensor() or ResetRightSensor() method. This means that you will learn about an obstacle even if your program was busy doing something else in the very moment when the sensor touched it. But dont forget to reset the sensor otherwise it will all the time report obstacle even if there isnt any no longer. The sample program in fact does nothing more than commanding the robot to walk straight and then checking the sensors in a loop until one any of them is signaled. Then it responds using this simple logic: if left sensor was signaled, turn right a little, and then continue walking straight. If right sensor was signaled do the same but with left turn. If both sensors are signaled at the same time, walk back a little, and then turn left and then walk straight again. It is not very smart but its up to you to give your robot more IQ!

Description of the C# classes for robot control from PC Note that you do not need to read this unless you know C# and intend to write your own programs in C# for controlling Krabos. The C# interface for the robot is composed of 1 high level classs and some other helper classes which you do not need to use directly: RobotCommander this is the main class which provides high-level functions to control the robot (e.g. GoStraight) and to check for its state (e.g. GetLeftSensor). RoboCommandInfo This helper defines various symbolic names for robot speed, servo position etc. It also stores information about one command, such as

whether the command requires parameter and of what type this parameter is. You do not need to use this class except when using the symbolic names for speed etc. RobotCommands This class allows obtaining instance of RoboCommandInfo class for each available command and also sending data to serial port and translating received data to symbolic values. In the following text the RobotCommander class will be described in details:

Das könnte Ihnen auch gefallen