1

我在电脑俱乐部和孩子们一起探索 Sphero。我们开发简单的 orbBasic 程序来做有趣的事情。我确实放弃了移动设备上的 orbBasic 应用程序,因为它几乎无法用于编程(字体很小,很难在移动设备上编辑)。我找到了 Sphero Mac SDK,我们现在使用它的 orbBasicLoader 从 Mac 上传程序。但是当我们的程序变大时,我们发现它们不会以这种方式加载到 Sphero,可能是因为它们必须拆分为两块 Sphero 内存,而 Mac SDK 无法正确处理。它由设备上的 orbBasic 应用程序处理,因此是可能的。

如何使用 Mac SDK 将大型 orbBasic 程序上传到 Sphero?

这是我们的一个程序 - Sphero 的类似蛇的游戏,用 orbBasic 编写,遇到了这个问题。在 4*4 米的空间中(球体在程序开始时位于空间的中心)有隐藏的“食物”,您的目标是通过使用 Sphero 的亮度作为指导将 Sphero 靠近“食物”来“吃食物”。你在吃完 5 个“食物”后结束游戏。双击 Sphero 重新启动。

10 locate 0,0
20 E=0
30 X=200-rnd 400
40 Y=200-rnd 400
50 C=xpos-X
60 D=ypos-Y
70 L=sqrt(C*C+D*D)
80 if L>200 then L=200
90 O=255-L
100 RGB O,O,O
110 if L<10 then goto 140
120 delay 100
130 goto 50
140 E=E+1
150 RGB 0,E*51,0
160 delay 1000
170 if E<5 then goto 30
180 LEDC 1+rnd 7
190 delay 100
200 if dshake > 0 then goto 10 
210 goto 180
4

1 回答 1

1

我想到了!

您必须手动将片段上传到 Sphero,并且片段不应接近 253 的限制。片段的最大大小为 200 效果很好。是很简单的。

下面是从程序中分离片段的代码:

-(void)setProgram:(NSString *)inProgram {
    NSArray *lines = [inProgram componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

    if( fragments != nil ) {
        [fragments release];
    }

    char buffer[1024]= {0};

    fragments = [[NSMutableArray alloc] initWithCapacity:2];

    NSMutableData *data = [[NSMutableData alloc] init];
    for( int i = 0; i<[lines count]; i++ ) {
        NSString *currentLine = [lines objectAtIndex:i];

        //strip comments - lines starting with apostrophe
        if( [currentLine hasPrefix:@"'"] ) continue;

        if( (data.length + [currentLine lengthOfBytesUsingEncoding:NSASCIIStringEncoding]) > 200 ) {
            [fragments addObject:data];
            [data release];
            data = [[NSMutableData alloc] init];
       }

        if( [currentLine getCString:buffer maxLength:253 encoding:NSASCIIStringEncoding] ) {
            [data appendBytes:buffer length:strlen(buffer)];
            [data appendBytes:"\r" length:1];
        }
    }

    [fragments addObject:data];
    [data release];
}

这是发送代码:

每次从 Sphero 收到的响应成功直到加载设置为 YES 时,都必须调用函数。如果您尝试一次发送所有片段 - 将返回响应代码 -2 并且程序不会加载。

-(void)loadToSphero {
    if( loaded == YES ) {
        [self abort];
        [self erase];
        curFragment = 0;
    }


    if( curFragment < [fragments count] ) {
        RKOrbBasicAppendFragmentCommand *appendCmd = [[RKOrbBasicAppendFragmentCommand alloc] initWithStorageType:RKOrbBasicStorageTypeTemporary fragment:[fragments objectAtIndex:curFragment]];
        [appendCmd sendCommand];
        [appendCmd release];
        curFragment++;
    }
    else {
        loaded = YES;
   }
}

这是其他程序命令:

-(void)execute {
     if( loaded == YES ) {
        RKOrbBasicExecuteCommand *execCmd = [[RKOrbBasicExecuteCommand alloc] initWithStorageArea:RKOrbBasicStorageTypeTemporary startLine:10];
        [execCmd sendCommand];
        [execCmd release];
     }
}

-(void)erase {
     RKOrbBasicEraseStorageCommand *eraseCmd = [[RKOrbBasicEraseStorageCommand alloc] initWithStorageType:RKOrbBasicStorageTypeTemporary];
     [eraseCmd sendCommand];
     [eraseCmd release];
     loaded=NO;
}

-(void)abort {
     RKOrbBasicAbortCommand *abortCmd = [[RKOrbBasicAbortCommand alloc] init];
     [abortCmd sendCommand];
     [abortCmd release];
}
于 2014-04-20T05:56:44.410 回答