July 06, 2003
API Command Line Interface
Well the ER1 is very functional from the robotic command center but there are a number of things that just need to be done via an external program. I found this out when I was unable to command the ER1 to rotate away from an object that the IR Sensor detected. I wanted to rotate until the sensor's reflection dropped below a thresh hold value. All of the "If" "Then" conditions of the Command Center are geared to trigger when the thresh hold is exceeded. This is the opposite of what I wanted to do.
Fortunately the folks at Evolution Robotics have added an API Command line interface that allows an external program to talk to the Command Center. I already used this API in an earlier post to do some simple remote control, so this seemed like a good opportunity to extend the remote control functionality I already coded. One thing I need to do is to process events that came back from ER1 to notify the external program when commands have finished. This was not part of the original remote control program.
Here is the pseudo code of the task I want to accomplish:
RotateAwayFromObject
loop
rotate 15 degrees away from the object
if irSensor<50 exit loop
end loop
Looks pretty simple doesn't it! Well submitting the commands to the command center is very easy but processing the events that come back from the command center is a little more difficult. Basically there are two events I had to process in the above pseudo code: When the rotate 15 degrees finishes and the IR sensor values. The ER1 documentation on the API is somewhat lacking but some documentation is a whole lot better than no documentation. I would like to see a complete description of all the possable events the ER1 can post for every command. Including error events. This would make the external program a little more robust if it could act on all events when needed. So for now I settled for the documentation on the "ir" and "move" command.
There is a command that turns on events and it is called "events" :) The interesting thing about ER1 events is that some only work when the command "events" is issued and some do not need the "events" command to send back information. In this case the "ir" command sends back the ir information without issuing "events". But the move commands do require an "events" command after the "move" command has been issued. Otherwise you will never see the "move done" event.
So I ended up with some code that looked a little ugly but it worked. I can always clean it up later. So here are some snippets of code that show a little about what I got working.
The IRSensor Class is used to parse the IR Sensor values and hold the results i an object.
public class IRSensor
{
int reflection=0;
int threshold=0;
int flashRate=0;
string command="";
public IRSensor()
{
}
public string Command
{
get{return this.command;}
set
{
this.command = value;
string[] temp = command.Split(' ');
if (temp.Length==4)
{
reflection = int.Parse(temp[1]);
threshold = int.Parse(temp[2]);
flashRate = int.Parse(temp[3]);
}
}
}
public int Reflection
{
get{return this.reflection;}
}
public int ThreshHold
{
get{return this.threshold;}
}
public int FlashRate
{
get{return this.flashRate;}
}
}
The RotateAwayFromObject method is the main method that tests for the object and issues the move command and repeats untill the object is clear
private void RotateAwayFromObject()
{
int lines=this.listBoxReceived.Items.Count;
this.listBoxReceived.Items.Clear();
command = CommandEnumerations.GetRightIRSensor;
this.telnet.SendMessage("ir 1");
// Wait for the command to complete
while(command!=0)
{
Application.DoEvents();
}
// Is the object cleared
if (this.ir1.Reflection<70)
{
}
else
{
// Move 15 degrees more away from the object
this.listBoxReceived.Items.Clear();
this.telnet.SendMessage("move 15 degrees");
command = CommandEnumerations.MotorDone;
this.telnet.SendMessage("events");
// Wait for the command to complete
while(command!=0)
{
Application.DoEvents();
}
// Recursive call to test for the object again
RotateAwayFromObject();
}
}
Well thats about it. The ability to extend the ER1 with the command line API is very nice and not too hard to do. I could easily convert this to be a stand alone console program and then call it from a behavior on the ER1 control center. Very cool!
Well here is the
Code if you want to see how I did it.
Posted by Mike Linnen at July 6, 2003 10:49 PM