Go Down

Topic: wii motion and nunchuk :~ (Read 2 times) previous topic - next topic

AlexRodriguez

buenas, he podido hacer funcionar los dispositivos por separados,
pero juntos no he podido.ahi le dejos los codes.desde ya muchas gracias por su ayuda.

Code: [Select]

/////Code nunchuk

#include <Wire.h> // Necesario para trabajar con el bus I2C
#include <utility\twi.h> // Necesario para la constante CPU_FREQ

void setup()
{
 Serial.begin(9600); // Inicializamos el puerto serie a una velocidad baja para que se puedan ver los datos
 Wire.begin(); // Inicializamos la librería wire para trabajar con I2C
 TWBR = ((CPU_FREQ / 400000L) - 16) / 2; // Configuramos el I2C para el modo Fast (400Kb/s)

 // Inicializamos el nunchuk sin encriptación  
 Wire.beginTransmission(0x52);
 Wire.send(0xF0);
 Wire.send(0x55);
 Wire.endTransmission();

 Wire.beginTransmission(0x52);
 Wire.send(0xFB);
 Wire.send(0x00);
 Wire.endTransmission();
}

void loop ()
{
 uint8_t buffer[6];

 // Pedimos que se actualicen los datos
 Wire.beginTransmission(0x52);
 Wire.send(0);
 Wire.endTransmission();

 // Recogemos y guardamos los datos
 Wire.requestFrom(0x52, 6);
 for(int indice = 0; indice < 6; indice++)
 {
   buffer[indice] = Wire.receive();
 }

 // Imprimimos los datos en el puerto serie
 //Serial.print("JX:");
 Serial.print(buffer[0], DEC);

 Serial.print(",");
 Serial.print(buffer[1], DEC);

 Serial.print(",");
 Serial.print((buffer[2] << 2) | ((buffer[5] & 0x0c) >> 2));

 Serial.print(",");
 Serial.print((buffer[3] << 2) | ((buffer[5] & 0x30) >> 4));

 Serial.print(",");
 Serial.print((buffer[4] << 2) | ((buffer[5] & 0xc0) >> 6));

 Serial.print(",");
 Serial.print((buffer[5] >> 1) & 1);

 Serial.print(",");
 Serial.println(buffer[5] & 1);
}




Code: [Select]

/// wii motion

#include <Wire.h>
byte data[6]; //six data bytes
int yaw, pitch, roll; //three axes
int yaw0, pitch0, roll0; //calibration zeroes

void wmpOn(){
Wire.beginTransmission(0x53); //WM+ starts out deactivated at address 0x53
Wire.send(0xfe); //send 0x04 to address 0xFE to activate WM+
Wire.send(0x04);
Wire.endTransmission(); //WM+ jumps to address 0x52 and is now active
}

void wmpSendZero(){
Wire.beginTransmission(0x52); //now at address 0x52
Wire.send(0x00); //send zero to signal we want info
Wire.endTransmission();
}

void calibrateZeroes(){
for (int i=0;i<10;i++){
wmpSendZero();
Wire.requestFrom(0x52,6);
for (int i=0;i<6;i++){
data[i]=Wire.receive();
}
yaw0+=(((data[3]>>2)<<8)+data[0])/10; //average 10 readings
pitch0+=(((data[4]>>2)<<8)+data[1])/10;
roll0+=(((data[5]>>2)<<8)+data[2])/10;
}
Serial.print("Yaw0:");
Serial.print(yaw0);
Serial.print(" Pitch0:");
Serial.print(pitch0);
Serial.print(" Roll0:");
Serial.println(roll0);
}

void receiveData(){
wmpSendZero(); //send zero before each request (same as nunchuck)
Wire.requestFrom(0x52,6); //request the six bytes from the WM+
for (int i=0;i<6;i++){
data[i]=Wire.receive();
}
yaw=((data[3]>>2)<<8)+data[0]-yaw0;
pitch=((data[4]>>2)<<8)+data[1]-pitch0;
roll=((data[5]>>2)<<8)+data[2]-roll0;
}
//see http://wiibrew.org/wiki/Wiimote/Extension_Controllers#Wii_Motion_Plus
//for info on what each byte represents
void setup(){
Serial.begin(115200);
Serial.println("WM+ tester");
Wire.begin();
wmpOn(); //turn WM+ on
calibrateZeroes(); //calibrate zeroes
delay(1000);
}

void loop(){
receiveData(); //receive data and calculate yaw pitch and roll
Serial.print("yaw:");//see diagram on randomhacksofboredom.blogspot.com
Serial.print(yaw); //for info on which axis is which
Serial.print(" pitch:");
Serial.print(pitch);
Serial.print(" roll:");
Serial.println(roll);
delay(100);
}







AlexRodriguez

Me encontre con este codigo para hacerlo funcionar juntos , pero la verdad que no me tira nada.


Code: [Select]

//Code for Arduino based communication with Wii Motion Plus and Nunchuck.
//Nunchuck is plugged into WM+ extension port so it DOES NOT require
//additional expansion board.
//Code, discussion, links, and references at:
//http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1248889032/0
//influenced this code, primarily contributions by
//Knuckles904, Ed Simmons, Adrian Carter, and duckhead
//Additionally, information at
//http://wiibrew.org/wiki/Wiimote/Extension_Controllers#Wii_Motion_Plus
//was very helpful.
//This code provides bases and/or example for more specific implementations

// Created by automatik (auto11)
// http://voidbot.net

//code and set up instructions at:
//http://voidbot.net/nunchchuk-and-wii-motion-plus-6-DOF.html

#include <Wire.h>

//Nunchuck
uint8_t outbuf[6];           //nunchuck buffer for data
int cnt = 0;                 //nunchuck buffer counter

//Wii Motion Plus
byte data[6];                //buffer for WM+ data
int yaw, pitch, roll;        //three gyroscope axes
int yaw0, pitch0, roll0;     //calibration zeroes
boolean yaw_v, pitch_v, roll_v; //calibration zeroes

void setup ()
{
  Serial.begin (115200);
  Wire.begin();             // join i2c bus as a master
  delay(100);

//instead of averaging data , I used data
//based on experiments from my particular WM+
  yaw0=8163;
  pitch0=7859;
  roll0=8536;
}

void loop ()
{
// Data communciation is sensative hence
// bunch of timed delays. These are just
// what works on my setup so YMMV. If you
// think data is incorect increase vakues
// for delays.

wmpActive();                 //  activate WM+
  delay(50);
extPort();                   //write 0x00 to 0x(4)A400fb
  delay(180);
  send_zero();
DataFlush();
  delay(30);
  send_zero();
wmpRead();                   // read WM+ data ( gyro )
wmpDeact();                  // deactivate WMP
  delay(50);
  send_zero();
  delay(70);
nunchuckRead(); // read Nunchuck data ( accelerometer data and buttons )

//  delay(250);

//Serial.println("===============================================");
}

//common

//Request data
void send_zero ()
{
  Wire.beginTransmission (0x52);
  Wire.send (0x00); // send zero
  Wire.endTransmission ();
}

void DataFlush()
{
  int h=1;
  Wire.requestFrom(0x52,100);
  while (Wire.available())
  {
    Wire.receive();
    h++;
  }
}

//ext port
//write 0x00 to 0x(4)A400fb
void extPort()
{
Wire.beginTransmission(0x52);
Wire.send(0xfb);
Wire.send(0x00);
Wire.endTransmission();
}

//Nunchuck

void nunchuckRead()
{
  Wire.requestFrom (0x52, 6); // request data from nunchuck
  while (Wire.available ())
    {
      outbuf[cnt] = Wire.receive (); // receive nunchuck data
      cnt++;
    }

    send_zero (); // send the request for next bytes
   
    // If we recieved the 6 bytes, then print them
    if (cnt >= 5)
    {
nunchuckAssembData();
    }
    cnt = 0; //(NunChuck)
}


// Assemble and print the input data we have recieved
// accel data is 10 bits long
// so we read 8 bits, then we have to add
// on the last 2 bits.  That is why I
// multiply them by 2 * 2
void nunchuckAssembData()
{
  int joy_x_axis = outbuf[0];
  int joy_y_axis = outbuf[1];
  int accel_x_axis = outbuf[2] * 2 * 2;
  int accel_y_axis = outbuf[3] * 2 * 2;
  int accel_z_axis = outbuf[4] * 2 * 2;

  int z_button = 0;
  int c_button = 0;

// byte outbuf[5] contains bits for z and c buttons
// it also contains the least significant bits for the accelerometer data
// so we have to check each bit of byte outbuf[5]
  if ((outbuf[5] >> 0) & 1)
    {
      z_button = 1;
    }
  if ((outbuf[5] >> 1) & 1)
    {
      c_button = 1;
    }

  if ((outbuf[5] >> 2) & 1)
    {
      accel_x_axis += 2;
    }
  if ((outbuf[5] >> 3) & 1)
    {
      accel_x_axis += 1;
    }

  if ((outbuf[5] >> 4) & 1)
    {
      accel_y_axis += 2;
    }
  if ((outbuf[5] >> 5) & 1)
    {
      accel_y_axis += 1;
    }

  if ((outbuf[5] >> 6) & 1)
    {
      accel_z_axis += 2;
    }
  if ((outbuf[5] >> 7) & 1)
    {
      accel_z_axis += 1;
    }

//Print Nunchuck Data
/*
  Serial.print (joy_x_axis, DEC);
  Serial.print ("\t");

  Serial.print (joy_y_axis, DEC);
  Serial.print ("\t");

  Serial.print (accel_x_axis, DEC);
  Serial.print ("\t");

  Serial.print (accel_y_axis, DEC);
  Serial.print ("\t");

  Serial.print (accel_z_axis, DEC);
  Serial.print ("\t");

  Serial.print (z_button, DEC);
  Serial.print ("\t");

  Serial.print (c_button, DEC);
  Serial.print ("\t");

  Serial.print ("\r\n");
*/
      //A ENVOYER AU PORT SERIE
      Serial.print("|acc=");
      Serial.print(accel_x_axis, DEC);
      Serial.print(",");
      Serial.print(accel_y_axis, DEC);
      Serial.print(",");
      Serial.print(accel_z_axis, DEC);
      Serial.print("|but=");
      Serial.print(z_button, DEC);
      Serial.print(",");
      Serial.print(c_button, DEC);
      Serial.print("|joy:");
      Serial.print(joy_x_axis,DEC);
      Serial.print(",");
      Serial.println(joy_y_axis, DEC);
}


//Wii Motion Plus

//Activate Wii Motion Plus
void wmpActive(){
  Wire.beginTransmission(0x53);
  Wire.send(0xfe);
  Wire.send(0x04);
  Wire.endTransmission();
}

//Deactivate Wii Motion Plus
void wmpDeact(){
Wire.beginTransmission(0x52);
Wire.send(0xf0);
Wire.send(0x55);
Wire.endTransmission();
}


void wmpRead()
{
  Wire.requestFrom(0x52,6); //request data from WM+
  for (int i=0;i<6;i++)
  {
    data[i]=Wire.receive();
  }
  wmpAssembData (); //assemble WM+ data
}

//assemble Wii Motion Plus data bits
//and get difference from averge values
void wmpAssembData ()
{
yaw=((data[3]>>2)<<8)+data[0];//-yaw0;
roll=((data[4]>>2)<<8)+data[1];//-roll0;
pitch=((data[5]>>2)<<8)+data[2];//-pitch0;


// get difference from avg values
// comment this out if you want to
// get raw data values
//yaw -= yaw0;
//roll -= roll0;
//pitch -= pitch0;

//scaling  mode data
yaw_v = data[3] & 2;
roll_v = data[4] & 2;
pitch_v = data[3] & 1;

// print WM+ data 
wmpPrintData ();
}

// print Wii Motion Plus data
void wmpPrintData ()
{
/*
Serial.print("yaw:");
Serial.print(yaw);
Serial.print("\t");
Serial.print("pitch:");
Serial.print(pitch);
Serial.print("\t");
Serial.print("roll:");
Serial.println(roll);

Serial.print("yaw_v:");
Serial.print(yaw_v);
Serial.print("\t");
Serial.print("pitch_v:");
Serial.print(pitch_v);
Serial.print("\t");
Serial.print("roll_v:");
Serial.println(roll_v);
*/
      //A ENVOYER AU PORT SERIE
      Serial.print("Yaw,pitch,roll=");
      Serial.print(yaw);
      Serial.print(",");
      Serial.print(pitch);
      Serial.print(",");
      Serial.print(roll);
}








creo que el problema esta en la velocidad  ""I2C para el modo Fast (400Kb/s)"", igualmente he probado a dostintas veloccidad, pero no me tira nada. solo puros ceros. :smiley-eek:

:~

SrDonGato

Si el código está mas que probado lo que debes tener mal es el montaje.

Conectas el nunchuk al motion ?  o conectas ambos al mismo bus ?

el problema que tienes probablemente sea de montaje

un saludo

AlexRodriguez

s, lo tengo conectado el nunchuk al motion.


con este codigo me funciona, pero no lo entiendo, esta
muy abanzado el codigo. es por eso que lo quiero el de arriba.
ahi le lo dejo haber si alguien lo decodifica y ve que le estaria faltado el de arriba.gracias!!

http://code.google.com/p/multiwii/source/browse/tags/MultiWiiV1_0/MultiWiiV1_0.pde?spec=svn18&r=18

David Cuartielles

Alex,

parece que hemos cometido algun tipo de error en las respuestas a mensajes en el foro. He eliminado los tres ultimos mensajes de este hilo para evitar confusiones. Si hay alguna cuestion referente a dichos mensajes, solo hazmelo saber: d.cuartielles [at] arduino [punto] cc

Si te ha causado algun tipo de problema, lo siento,

/d

Go Up