Question

I am a beginner at working with Sphero and Android, and I am going through the examples in the Sphero SDK. I have successfully merged the ButtonDrive and the Collisions sample projects so I fx. can get Sphero to roll in a direction and stop when it bumps into something. But the code for setting up asynchronous data listening perplexes me, and I am unable to add the code needed to use Locator functions such as getLocatorData. Can anyone provide me with the code I need to implement so I can call both methods 'collisionData.hasImpactXAxis()' and 'locatorData.getPositionX()'

Was it helpful?

Solution

See the Locator sample at https://github.com/orbotix/Sphero-Android-SDK for more details, but the common practice is to...

private CollisionDetectedAsyncData collisionData;
private LocatorData lastLocation; 

// setup general AsyncDataListener 
private final DeviceMessenger.AsyncDataListener mDataListener = new DeviceMessenger.AsyncDataListener() {
    @Override
    public void onDataReceived(DeviceAsyncData data) {

        if(data instanceof DeviceSensorsAsyncData){ 
            //get the frames in the response
            List<DeviceSensorsData> data_list = ((DeviceSensorsAsyncData)data).getAsyncData();
            if(data_list != null){

                // Iterate over each frame, however we set data streaming as only one frame
                for(DeviceSensorsData datum : data_list){
                    LocatorData locatorData = datum.getLocatorData();
                    if( locatorData != null ) {
                        lastLocation = locatorData;  
                    }
                }
            }
        }

        if (data instanceof CollisionDetectedAsyncData) {
            collisionData = (CollisionDetectedAsyncData) data;
        }

        // handle new data from collisions or locations
    }
};

private configureStreamingAndCollisions(){
    // hook up Locator
    final long mask = SetDataStreamingCommand.DATA_STREAMING_MASK_LOCATOR_ALL;
    final int divisor = 4; //100Hz
    final int packet_frames = 1;
    final int response_count = 0;
    SetDataStreamingCommand.sendCommand((Robot)mRobot, divisor, packet_frames, mask, response_count);
    DeviceMessenger.getInstance().addAsyncDataListener((Robot)mRobot, mDataListener);

    // hook up Collision Listener
    DeviceMessenger.getInstance().addAsyncDataListener((Robot)mRobot,mCollisionListener);
    ConfigureCollisionDetectionCommand.sendCommand(mRobot, ConfigureCollisionDetectionCommand.DEFAULT_DETECTION_METHOD,
                            45, 45, 100, 100, 100);
}

Note that you will need class variables to keep track of the lastLocation or lastCollision...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top