MIRA
Domain JSONRPCServer - Documentation


Sending HTTP POST JSON-RPC commands

The webserver supports HTTP POST requests for JSON-RPC commands. Therefore the content-type of the request must be "application/json" A request using XMLHttpRequest would look like:

id = uid();
service = "/namespace/Service.setInt";
params = 1234;
request = "{\"jsonrpc\": \"2.0\", \"id\": " + id + ", \"method\": \"" + service + "\", \"params\": ["+ params + "]}";
xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", url, false);
xmlhttp.setRequestHeader("content-type","application/json");
xmlhttp.setRequestHeader("accept","application/json");
xmlhttp.send(request);

See http://www.jsonrpc.org/specification for the specification of JSON-RPC requests.

Navigation example

The navigation system is task based. Each task can consist of several subtasks that enable different behaviors in the navigation system. The most important subtasks are:

mira::navigation::PositionTask : makes the robot drive to the specified position mira::navigation::OrientationTask : makes the robot face into the specified direction at the target point mira::navigation::PreferredDirectionTask : allow or forbid driving forward/backward

For sending a robot navigation commands via RPC you need to create navigation tasks in JSON notation and pass it to the setTask function. You can use the following javascript method to send the robot to point x,y (in meter) facing phi (in degree) when reaching that point and driving only into the specified dir (-1 backward, 1 forward, 0 both)

function startDriving(x, y, phi, dir)
{
method = "/robot/navigation/Pilot.setTask";
param = {"SubTasks" : [
{
"@class" : "mira::navigation::PositionTask",
"MaxTolerance" : 0.2,
"MinTolerance" : 0.2,
"Position" : {"X" : x,"Y" : y}
},
{
"@class" : "mira::navigation::OrientationTask",
"Orientation" : phi,
"Tolerance" : 15.0
},
{
"@class" : "mira::navigation::PreferredDirectionTask",
"Direction" : dir,
"WrongDirectionCost" : 0.5
}
]};
callService(method, JSON.stringify(param));
};

The JSONRPC server comes with a jsonrpc.js file that includes the above callService method

If you know the name of the Framework (can be specified on the console via the fw-name command line option e.g. miracenter MyConfig.xml –fw-name MyFramework) you can also call methods of this framework. This allows to read from channels. The passed and returned parameters are always in JSON-format.

The following javascript method reads from the navigation status channel (not the extra " around the channels name) and returns true if the robot has reached the specified task:

function goalReached()
{
method = "/MyFramework.readChannel";
param = "\"/robot/navigation/PilotEvent\"";
var state = callService(method, param);
if (state.Value != "PlanAndDrive")
return true;
return false;
}

To get the planned path of the current navigation task you can query the /robot/navigation/Path channel. The datatype is std::vector<Point2f> and the path is continously replanned. So it does change when the robot moves or the local map information is updated and always starts at the robots current position and ends at the target position.

The following function retrieves the path:

function getPath()
{
method = "/MyFramework.readChannel";
param = "\"/robot/navigation/Path\"";
return callService(method, param).Value;
}

To query the position (pose) of the robot you need to get the correct transformation from the transform tree. The robots coordinate frame is "/robot/RobotFrame" and the world coordinate system is "/GlobalFrame". The following method returns the current robot position in the world:

function getRobotPosition()
{
method = "/MyFramework.getTransform2";
param = "\"/robot/RobotFrame\",\"/GlobalFrame\"";
pose = callService(method, param);
while (pose.Phi > 360)
pose.Phi -= 360;
while (pose.Phi < 0)
pose.Phi += 360;
return pose;
}