Robot Output

Returning data from the robot

There are two kinds of messages that the robot can send:

  • Command Responses: robot responses to commands
  • Async Messages: robot generated async messages.
Both RKRobotBase and RKConvenienceRobot classes contain addResponseObserver: and removeResponseObserver: selectors to register a custom RKResponseObserver implementation to handle responses and async messages.
Both RKRobotBase and RKConvenienceRobot types contain addResponseObserver(observer: RKResponseObserver!) and removeResponseObserver(observer: RKResponseObserver!) methods to register a custom RKResponseObserver implementation to handle responses and async messages.

Register for robot responses

Register for responses with the RKResponseObserver protocol.
All handle... selectors are optional and will only be fired if implemented.

[_robot addResponseObserver:self]; // register with the robot for responses.
robot.addResponseObserver(self)
        mRobot.addResponseListener(new ResponseListener() {
            public void handleResponse(DeviceResponse response, Robot robot) {

            }

            public void handleAsyncMessage(AsyncMessage asyncMessage, Robot robot) {

            }

            public void handleStringResponse(String stringResponse, Robot robot) {
                // almost always NOOP
            }
        });

}
// Coming Soon

Command Responses

Responses are robot responses to commands that return data much like a standard web request/response cycle.

Below handles a response from a RKReadOdometerCommand.

- (void)handleResponse:(RKDeviceResponse *)response forRobot:(id<RKRobotBase>)robot {
    // handle syncronous responses from 'get' command requests
    if([response isKindOfClass:[RKReadOdometerResponse class]]){
        RKReadOdometerResponse* ror = (RKReadOdometerResponse*) response;
        uint32_t distance = ror.distanceInCentimeters;
    }
}
func handleResponse(response: RKDeviceResponse!, forRobot robot: RKRobotBase!) {
    if let ror = response as? RKReadOdometerResponse {
        let distance = ror.distanceInCentimeters
    }
}

Below handles a response from a GetOdometerCommand.

public void handleResponse(DeviceResponse response, Robot robot) {
    if( response instanceof GetOdometerResponse ) {
        GetOdometerResponse odometerResponse = (GetOdometerResponse) response;
        int lifetimeDistanceRolled = odometerResponse.getDistanceInCentimeters();
    }
}
// Coming Soon

Robot Async Messages

Async messages are messages generated by logic on the robot either by sensors, or developer defined Macro Emits and Oval Async notifications. Push notifications are analogous to robot async-messages.

  • Sensor Data
  • Collision Detected
  • Self Level Complete
  • Sleep Will/Did Occur
  • Power State Change
  • Macro Emit
  • Oval Async (developer defined)

The example snippit below shows setup and handling of Collision Async messages.

Example: Enable collision detection

// enable collision detection with RKConvenienceRobot
[_robot enableCollisions:YES];
robot.enableCollisions(true)
// OR customize collision detection with RKRobotBase & sendCommand:
[_robot sendCommand:[[RKConfigureCollisionDetectionCommand alloc]
                                initForMethod:RKCollisionDetectionMethod3
                                xThreshold:50 xSpeedThreshold:30 yThreshold:200 ySpeedThreshold:0 postTimeDeadZone:.2]];
}
robot.sendCommand(RKConfigureCollisionDetectionCommand(forMethod: .Method3, xThreshold: 50, xSpeedThreshold: 30, yThreshold: 200, ySpeedThreshold: 0, postTimeDeadZone: 0.2))
mRobot.enableCollisions( true );
// Coming Soon - enable collision detection

Handle Async Messages on collision

-(void)handleAsyncMessage:(RKAsyncMessage *)message forRobot:(id<RKRobotBase>)robot {
    if([message isKindOfClass:[RKCollisionDetectedAsyncData class]]){
        // handleCollisionDetected
    }
}
func handleAsyncMessage(message: RKAsyncMessage!, forRobot robot: RKRobotBase!) {
    if let collisionMessage = message as? RKCollisionDetectedAsyncData {
        // handleCollisionDetected
    }
}
@Override
public void handleAsyncMessage( AsyncMessage asyncMessage, Robot robot ) {
    if( asyncMessage instanceof CollisionDetectedAsyncData ) {
        //Collision occurred.
    }
}
// Coming Soon - handle Collisions