同时获取定位器和碰撞数据

时间:2013-06-02 18:39:21

标签: sphero-api

我是使用Sphero和Android的初学者,我正在浏览Sphero SDK中的示例。我已经成功合并了ButtonDrive和Collisions示例项目,所以我fx。可以让Sphero向一个方向滚动,并在碰到某个东西时停下来。但是设置异步数据监听的代码让我困惑,我无法添加使用定位器函数(如getLocatorData)所需的代码。 任何人都可以提供我需要实现的代码,所以我可以调用两个方法'collisionData.hasImpactXAxis()'和'locatorData.getPositionX()'

1 个答案:

答案 0 :(得分:0)

有关详细信息,请参阅https://github.com/orbotix/Sphero-Android-SDK处的定位器示例,但通常的做法是......

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);
}

请注意,您需要类变量来跟踪lastLocation或lastCollision ......