Hello,World! BLOG

マルチエンジニアSEAGELLの開発日記

Wiiヌンチャクでゴム銃

Wiiヌンチャクを使用してサーボモーターをコントロールしてみました。


Wiiヌンチャクでコントロール


使用したパーツ


ソースコードArduino

※コードはコピペしたり急いで書いたのでのかなりアバウトです。

#include <Wire.h>
#include <Servo.h>

struct _S_NUNCHAKU {
  int joy_x;          
  int joy_y;         
  int accel_x;        
  int accel_y;       
  int accel_z;     
  boolean button_z;
  boolean button_c;
};
typedef _S_NUNCHAKU S_NUNCHAKU;

S_NUNCHAKU g_nunchaku;
Servo g_mservo_x;
Servo g_mservo_y;
Servo g_mservo_b;

uint8_t outbuf[6];		

int cnt = 0;
int led_pin = 5;
int back_x = 90;
int back_y = 90;

void setup()
{
  pinMode(led_pin, OUTPUT);
  Serial.begin (9600);  
  Wire.begin ();		
  nunchack_init (); 
  g_mservo_x.attach(2);
  g_mservo_y.attach(3);
  g_mservo_b.attach(4);
  g_mservo_x.write(90);
  g_mservo_y.write(90);
  g_mservo_b.write(90);
}

void loop()
{
  Wire.requestFrom (0x52, 6);
  while (Wire.available ()) {
    outbuf[cnt] = nunchuk_decode_byte (Wire.read ());	
    cnt++;
  }
  if (5 <= cnt) {
    getNunchakuData();
    showDirection();
  }
  cnt = 0;
  send_zero();
  delay (1);
}

void nunchack_init()
{
  Wire.beginTransmission (0x52);	
  Wire.write (0x40);		
  Wire.write (0x00);		
  Wire.endTransmission ();	
  memset(&g_nunchaku, 0x00, sizeof(S_NUNCHAKU));
}

void getNunchakuData() 
{  
  g_nunchaku.joy_x = outbuf[0];
  g_nunchaku.joy_y = outbuf[1];
  g_nunchaku.accel_x = outbuf[2]; 
  g_nunchaku.accel_y = outbuf[3];
  g_nunchaku.accel_z = outbuf[4];

  ((outbuf[5] >> 0) & 1) ? g_nunchaku.button_z = false : g_nunchaku.button_z = true;
  ((outbuf[5] >> 1) & 1) ? g_nunchaku.button_c = false : g_nunchaku.button_c = true;

  if ((outbuf[5] >> 2) & 1) g_nunchaku.accel_x += 2;
  if ((outbuf[5] >> 3) & 1) g_nunchaku.accel_x += 1;

  if ((outbuf[5] >> 4) & 1) g_nunchaku.accel_y += 2;
  if ((outbuf[5] >> 5) & 1) g_nunchaku.accel_y += 1;

  if ((outbuf[5] >> 6) & 1) g_nunchaku.accel_z += 2;
  if ((outbuf[5] >> 7) & 1) g_nunchaku.accel_z += 1;
}

void showDirection() 
{  
  int noffset = 0;
  
  if (g_nunchaku.button_z) {  
    if ((g_nunchaku.accel_x < back_x - noffset) || (back_x + noffset < g_nunchaku.accel_x)) {
      g_mservo_x.write(181 - map(g_nunchaku.accel_x, 70, 190, 1, 180));
      back_x = g_nunchaku.accel_x;
    }
    
    if ((g_nunchaku.accel_y < back_y - noffset) || (back_y + noffset < g_nunchaku.accel_y)) {
      g_mservo_y.write(map(g_nunchaku.accel_y, 70, 190, 40, 160));
      back_y = g_nunchaku.accel_y;
    }
    
  } else {
    g_mservo_x.write(181 - map(g_nunchaku.joy_x, 0, 255, 1, 180));
    g_mservo_y.write(190 - map(g_nunchaku.joy_y, 0, 255, 20, 170));
  }
  
  if (g_nunchaku.button_c) {
    g_mservo_b.write(90);
    digitalWrite(led_pin, HIGH);
  } else {
    g_mservo_b.write(1);  
    digitalWrite(led_pin, LOW);
  }
}

void send_zero ()
{
  Wire.beginTransmission (0x52);
  Wire.write (0x00);		
  Wire.endTransmission ();	
}

char nunchuk_decode_byte (char x)
{
  x = (x ^ 0x17) + 0x17;
  return x;
}
PAGE TOP ▲

Copyright © 2016 SysES. All Rights Reserved.