Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 3 months ago. This question has been closed. Reason: Duplicate question
atan2
- include<math.h>
- include "mbed.h"
- include <I2CConfig.h>ヘッダーの読み込み
- include <WiiNunchuckReader.h>ヘッダーの読み込み
DigitalOut myled(LED1); PwmOut pa8(PA_8); Serial pc(USBTX,USBRX);パソコンの画面に数値出力するためのUSB通信(使う時はpwmout PA_2,PB_4使用不可(Sirial2とpwmが同じピン使ってる)) WiiNunchuckReader C1(PB_7,PB_6);ヌンチャクと通信するピンの設定(SDA,SCL)←I2C int X,Y,Z; double t,pa,pb,pc,pd,pe,rb,rc,rd,re,r; PwmOut a(PA_4),b(PB_4),c(PB_5),d(PA_8),e(PA_2); int main(){ pc.baud(921600);パソコンとの通信速度設定 X=0; Y=228; Z=0; pa=1500; pb=1500; pc=1500; pd=2200; pe=1500; a.period_ms(20); b.period_ms(20); c.period_ms(20); d.period_ms(20); e.period_ms(20); a.pulsewidth_us(pa); b.pulsewidth_us(pb); c.pulsewidth_us(pc); d.pulsewidth_us(pd); e.pulsewidth_us(pe); while(1) {wait(0.0001); C1.RequestRead();値の読み取り int x= C1.getJoyX();左右方向のスティック値読み取り(0~255?) int y= C1.getJoyY();上下方向のスティック値読み取り int C= C1.getButtonC();Cボタン値読み取り(1 or 0) int z= C1.getButtonZ();Zボタン値読み取り
↑読み取り後にx,y,c,z定義すること
pc.printf("(x,y,C,z)=(%d,%d,%x,%x)\n\r",x,y,c,z);値をteratermに表示(\n\rは改行後左端へ移動) wait(0.5);wait挟まないと数値読み取りづらい
あとは読み取ったx,y,z,cの値で条件分岐させればよい
t=1;座標
X=X+(x-113)*0.06*t;
Y=Y+(y-115)*0.06*t;
if(z==1&&C==0){Z++;}
if(C==1&&z==0){Z;}
手の開閉
if(C==1&&z==1){pa;}
a.pulsewidth_us(pa);
演算
r=pow(X*X+Y*Y,0.5)-110;
rc=acos((Z*Z+r*r-6994)/6930);
rd=1.5708-asin((b*sin(rc))/pow(r*r+Z*Z,0.5))-atan(z/r);
rb=rc+rd-1.5708;
re=atan2(Y,X);
}
}