There are currently 0 users and 3 guests online.
Krabos - walking robot
- 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. .
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.
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
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:
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 don’t 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  installed and that you played a little with it so that you know how to open a project and build it.
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())
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 don’t forget to reset the sensor otherwise it will all the time report obstacle even if there isn’t 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 it’s 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:
Defines the current speed of the robot. With this speed the robot moves when you call GoStraigh() or any other of the movement functions.
Gets the state of the sensor. True means sensor is signalled. It will send the appropriate command to the robot to get fresh value and return the result. If timeout occurs during the communication (500 ms), it will return last value or false if no value was ever read.
Setting the property to false resets the sensor, making it ready to signal another obstacle. Setting it to true throws exception!
Gets the state in which the robot is now. State is read from the robot using the appropriate command.
Sets position of the specified servo. The position is one of the values defined in RobotCommandInfo.ServoPosition enumeration. Possible values are A, B and Center.
Creates instance of the RobotCommander class. Input argument is number of the seriál port (COM) to use for communication with the robot, e.g. 1 for COM1, 2 for COM2, etc.
Note that it will attempt to open the specified port and throw ArgumentException if it fails.
Creates instance of the RobotCommander class using given SerialPort object. The port must be already opened before it is passed to this constructor!
These methods set the robot moving in the required way. They return true is the command has been sent to robot successfully, false if not.
The robot moves with the speed specified in the Speed property, Medium speed by defuult.
Stops the robot. The robot will stop with all servos in Center position.
Puts the robot into autonomous mode walking forward. It will respond to sensor inputs by turning etc.
Same as using the xSensorSignaled properties. Methods return false if there is error in communication as well as if the sensor is not signalled
bool QuerySensor(RobotCommandInfo.SensorCommandData sensor)
Call this method to ask the robot to report state of its sensors without waiting for the answer. You must handle the SerialPort DataReceived event to receive the answer asynchronously and then use TranslateReceivedByteToSensor method to decode received data.
RobotCommandInfo.SensorCommandData sensor – the sensor which state robot should report.
Call this method to ask the robot to report its state without waiting for the answer. You must handle the SerialPort DataReceived event to receive the answer asynchronously and then use TranslateReceivedByteToState method to decode received data.
bool TranslateReceivedByteToSensor(byte data,
out RobotCommandInfo.SensorCommandData sensor,
out bool active)
This method takes one byte of data received from serial line and translates it into information about sensor state. It can be used to receive answers from the robot asynchronously (after sending command using QuerySensor method).
byte data – data which the robot sent.
RobotCommandInfo.SensorCommandData sensor – which sensor state is in the data (if any).
bool active – state of the sensor, true means signalled, false nonsignalled.
Returns: true if the byte of data contained information about sensor state, false if it did not.
bool TranslateReceivedByteToState(byte data,
out RobotCommandInfo.RobotState state)
This method takes one byte of data received from serial line and translates it into information about robot state. It can be used to receive answers from the robot asynchronously (after sending command using QueryState method).
byte data – data which the robot sent.
RobotCommandInfo.RobotState state – receives state of the robot which was decoded from the data (if available)
Returns: true if the byte of data contained information about robot state, false if it did not.