ColdFire for Processing (2)
■SilentCのソース
adinit(void)
{
InitAd(0x70);
}
disp(char ch)
{
PrNum(GetAd(ch));
}
udpsend(char *str)
{
char *soc,*buf1,*buf2;
long ip;
ip=GetIP(str);
if(ip==0)return;
buf1=MemoryAlloc(15);
buf2=MemoryAlloc(5);
soc=CreateSocket(0);
InitAd(0x70);
#stop 0
for(;;){
GetDigit(GetAd(4),buf1);StrCat(buf1," ");
GetDigit(GetAd(5),buf2);StrCat(buf1,buf2);StrCat(buf1," ");
GetDigit(GetAd(6),buf2);StrCat(buf1,buf2);
SendTo(soc,ip,30049,buf1,StrLen(buf1));
if(Getc(0)=='q')break;
}
CloseSocket(soc);
MemoryFree(buf1);
MemoryFree(buf2);
}
"udpsend(IP)"で実行、"q"で終了は雑誌記事のサンプルと一緒。
■Processingのソース
import hypermedia.net.*;
UDP udp;
int x,y,z,gx,gy,gz;
color boxFill;
void setup()
{
size(400, 300, P3D);
frameRate(10);
udp = new UDP(this, 30049);
//udp.log(true);
udp.listen(true);
PFont nfont = loadFont("ArialMT-48.vlw");
textFont(nfont, 20);
}
void draw()
{
background(255);
pushMatrix();
translate(width/2, height/2, 0);
// 0G 補正
gx = 1900-x;
gy = 1900-z;
gz = y-2120;
// 座標変換 一部変更(080819)
if(gx==0&&gy==0||gy==0&&gz==0){
rotateX( 0 );
rotateZ( 0 );
}else{
rotateX( PI - asin( gz / sqrt(gy*gy+gz*gz) ) );
rotateZ( PI - asin( gx / sqrt(gx*gx+gy*gy) ) );
}
stroke(255, 100);
boxFill = color(abs(0.05*x), abs(0.05*y), abs(0.05*z), 100);
fill(boxFill);
box(300, 10, 80);
popMatrix();
fill(150);
text("X:" + gx + " Y:" + gy + " Z:" + gz, 0, height);
}
void receive( byte[] data)
{
String message = new String(data);
//print(message + "\n");
String xyz[] = splitTokens(message);
x = int(xyz[0]);
y = int(xyz[1]);
z = int(xyz[2]);
}
■気がついたこと
0Gのときの値が結構ずれているので、
軸ごとに適当な校正を行った。
ローパスの必要性がある。
ColdFireにやらせるか、P55でやるか。
移動平均などで良さそう。
基板がひっくりかえった時について考慮されていない。
表面が上を向いている場合と下を向いている場合で、
rotate部分の計算を分けなきゃならないか?
傾き角度が大きくなると回ってしまう。
もっと良い方法がないかを調べてから対処。
"SilentC"は簡素で楽しい。
SMTP,POP,SNTPなどのプロトコルの理解に役立った。
取っ掛かりとして最適。
一冊買い増し。
■参考文献
http://ubaa.net/shared/processing/udp/examples/udp/udp.pde
Interface 2008年 9月号 / CQ出版社
実例で学ぶゲームAIプログラミング / オライリー・ジャパン



