我是使用 Sphero 和 Android 的初学者,我正在浏览 Sphero SDK 中的示例。我已经成功合并了 ButtonDrive 和 Collisions 示例项目,所以我进行了修复。可以让 Sphero 向一个方向滚动并在撞到某物时停止。但是设置异步数据监听的代码让我很困惑,我无法添加使用 Locator 函数(如 getLocatorData)所需的代码。谁能给我提供我需要实现的代码,这样我就可以调用这两种方法“collisionData.hasImpactXAxis()”和“locatorData.getPositionX()”
问问题
452 次
1 回答
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...
于 2013-06-04T00:11:52.320 回答