Basic Commands

Robots objects in all supported Sphero SDKs have a sendCommand function. This function abstracts the creation of the binary packet to be sent, queues the packet, and marshals it through the Bluetooth Stack.

All examples assume a valid robot connection and class variable. See Connections

Aim & Setting Heading

As described in Heading & Aiming an application will need to be able to Aim the robot before allowing your user to attempt to navigate.

UI Aim Component
Custom Aiming

To implement a custom aim component, the following commands will need to be used.

  1. Turn on Back (Aim) LED
  2. Rotate with zero velocity
  3. Set Heading
// turn on the blue taillight
[_robot sendCommand:[RKBackLEDOutputCommand commandWithBrightness:1.0]];
...
// rotate the robot
[_robot sendCommand:[RKRollCommand commandWithHeading:0 andVelocity:0.0];
...
// when user is done aiming - set the heading
[_robot sendCommand:[RKSetHeadingCommand command]];
robot.driveWithHeading(180, andVelocity: 0)
robot.setZeroHeading()
mRobot.drive( 180, 0 );
mRobot.setZeroHeading();

Driving

Driving forward

[_robot sendCommand:[RKRollCommand commandWithHeading:0 andVelocity:1.0];
robot.sendCommand(RKRollCommand(heading: 0, velocity: 1.0))
// Here h is the heading in degrees and v is a value between 0.0f and 1.0f
mRobot.sendCommand( new RollCommand( h, v, RollCommand.State.GO ) );
// Unity

Stopping

[_robot sendCommand:[RKRollCommand commandWithStopAtHeading:0]];
RKRollCommand.commandWithStopAtHeading(0)
mRobot.sendCommand(new RollCommand(RollCommand.getCurrentHeading(), 0.0f, RollCommand.State.STOP));
// Unity

Changing Color

Set white at 50% brightness

[_robot sendCommand:[RKRGBLEDOutputCommand commandWithRed:.5 green:.5 blue:.5]];
// The valid color values here are 0.0f to 1.0f.
robot.sendCommand(RKRGBLEDOutputCommand(red: 0.5, green: 0.5, blue: 0.5))
mRobot.sendCommand( new RGBLEDOutputCommand( 0.5f, 0.5f, 0.5f ) );
// unity

Convenience Robot Function

The RKConvenienceRobot class contains the method - [RKConvenienceRobot setLEDWithRed:(float)redVal Green:(float)greenVal Blue:(float)blueVal. We can set the RGB LED with this method. The valid values here are 0.0f to 1.0f.

@property (strong, nonatomic) RKConvenienceRobot *robot; // Assume this is set when the robot connects

{...}

- (void)blink:(BOOL)lit {
    if (lit) {
        [_robot setLEDWithRed:0.0f Green:0.0f Blue:0.0f];
    }
    else {
        [_robot setLEDWithRed:0.0f Green:0.0f Blue:1.0f];
    }

    [self performSelector:@selector(blink:) withObject:!lit afterDelay:0.5];
}

The RKConvenienceRobot class contains the method setLEDWithRed(redVal: Float, green: Float, blue: Float). We can set the RGB LED with this method. The valid values here are 0.0 to 1.0.

var robot: RKConvenienceRobot

{...}

func blink(lit: Bool) {
    if (lit) {
        robot.setLEDWithRed(0.0, green: 0.0, blue: 0.0)
    } else {
        robot.setLEDWithRed(0.0, green: 0.0, blue: 1.0)
    }

    var delay = Int64(0.5 * Float(NSEC_PER_SEC))
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay), dispatch_get_main_queue(), { () -> Void in
        self.blink(!lit);
    })
}

The ConvenienceRobot class contains the method setLED( float red, float green, float blue ). We can set the RGB LED with this method. The valid values here are 0.0 to 1.0.

private ConvenienceRobot mRobot;

{...}

private void blink( final boolean lit ) {
    if( lit ) {
        mRobot.setLed( 0.0f, 0.0f, 0.0f );
    } else {
        mRobot.setLed( 0.0f, 0.0f, 1.0f );
    }

    final Handler handler = new Handler();
    handler.postDelayed( new Runnable() {
        public void run() {
            blink( !lit );
        }
    }, 2000 );
}
// coming soon

Convenience Robot Send Command

The RKConvenienceRobot class contains the method - [RKConvenienceRobot sendCommand:(RKDeviceCommand *)command]. We can make a RKRGBLEDOutputCommand and send it with this method.

@property (strong, nonatomic) RKConvenienceRobot *robot; // Assume this is set when the robot connects

{...}

- (void)blink:(BOOL)lit {
    if (lit) {
        [_robot sendCommand:[RKRGBLEDOutputCommand commandWithRed:0.0f
                                                            green:0.0f
                                                             blue:0.0f]];
    }
    else {
        [_robot sendCommand:[RKRGBLEDOutputCommand commandWithRed:0.0f
                                                            green:0.0f
                                                             blue:1.0f]];
    }

    [self performSelector:@selector(blink:) withObject:!lit afterDelay:0.5];
}

The RKConvenienceRobot class contains the method sendCommand(commad: RKDeviceCommand). We can make a RKRGBLEDOutputCommand and send it with this method.

var robot: RKConvenienceRobot

{...}

func blink(lit: Bool) {
    if (lit) {
        robot.sendCommand(RKRGBLEDOutputCommand(red: 0.0, green: 0.0, blue: 0.0))
    } else {
        robot.sendCommand(RKRGBLEDOutputCommand(red: 0.0, green: 0.0, blue: 1.0))
    }

    var delay = Int64(0.5 * Float(NSEC_PER_SEC))
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay), dispatch_get_main_queue(), { () -> Void in
        self.blink(!lit);
    })
}

The ConvenienceRobot class contains the method sendCommand( DeviceCommand command ). We can make a RGBLEDOutputCommand and send it with this method.

private ConvenienceRobot mRobot;

{...}

private void blink( final boolean lit ) {
    if( lit ) {
        mRobot.sendCommand( new RGBLEDOutputCommand( 0.0f, 0.0f, 0.0f ) );
    } else {
        mRobot.sendCommand( new RGBLEDOutputCommand( 0.0f, 0.0f, 1.0f ) );
    }

    final Handler handler = new Handler();
    handler.postDelayed( new Runnable() {
        public void run() {
            blink( !lit );
        }
    }, 2000 );
}
// js

Robot Send Command

The id<RKRobotBase> object (the one we get from the - (void)handleRobotStateChangeNotification:(RKRobotChangedStateNotification )n method or by using - [RKConvenienceRobot robot]) contains the method - [id<RKRobotBase> sendCommand:(RKDeviceCommand )command]. We can make an RKRGBLEDOutputCommand and send it with this method.

@property (strong, nonatomic) id<RKRobotBase> robot; // Assume this is set when the robot connects

{...}

- (void)blink:(BOOL)lit {
    if (lit) {
        [_robot sendCommand:[RKRGBLEDOutputCommand commandWithRed:0.0f
                                                            green:0.0f
                                                             blue:0.0f]];
    }
    else {
        [_robot sendCommand:[RKRGBLEDOutputCommand commandWithRed:0.0f
                                                            green:0.0f
                                                             blue:1.0f]];
    }

    [self performSelector:@selector(blink:) withObject:!lit afterDelay:0.5];
}

The RKRobotBase object (the one we get from the handleRobotStateChangeNotification(n: RKRobotChangedStateNotification) method or by using the robot property) contains the method sendCommand(command: RKDeviceCommand). We can make an RKRGBLEDOutputCommand and send it with this method.

var robot: RKRobotBase

{...}

func blink(lit: Bool) {
    if (lit) {
        robot.sendCommand(RKRGBLEDOutputCommand(red: 0.0, green: 0.0, blue: 0.0))
    } else {
        robot.sendCommand(RKRGBLEDOutputCommand(red: 0.0, green: 0.0, blue: 1.0))
    }

    var delay = Int64(0.5 * Float(NSEC_PER_SEC))
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay), dispatch_get_main_queue(), { () -> Void in
        self.blink(!lit);
    })
}

The Robot object (the one we can get from the handleRobotChangedState method) contains the method sendCommand( DeviceCommand command). We can make an RGBLEDOutputCommand and send it with this method.

private Robot mRobot;

{...}

private void blink( final boolean lit ) {
    if( lit ) {
        mRobot.sendCommand( new RGBLEDOutputCommand( 0.0f, 0.0f, 0.0f ) );
    } else {
        mRobot.sendCommand( new RGBLEDOutputCommand( 0.0f, 0.0f, 1.0f ) );
    }

    final Handler handler = new Handler();
    handler.postDelayed( new Runnable() {
        public void run() {
            blink( !lit );
        }
    }, 2000 );
}
// js