- Labornetzteil AliExpress         
Ergebnis 1 bis 10 von 49

Thema: Wii Nunchuck an Arduino

Hybrid-Darstellung

Vorheriger Beitrag Vorheriger Beitrag   Nächster Beitrag Nächster Beitrag
  1. #1
    Erfahrener Benutzer Robotik Einstein
    Registriert seit
    11.12.2007
    Ort
    weit weg von nahe Bonn
    Alter
    40
    Beiträge
    3.416
    es gibt da wohl 2 Varianten

    versuchs mal mit dem workaround den ich im arduino forum gefunden habe (Zusammengefasst übersetzt: In der init muss eine andere Sequenz gesendet werden)

    https://forum.arduino.cc/index.php?topic=45924.msg333160#msg333160
    Es gibt 10 Sorten von Menschen: Die einen können binär zählen, die anderen
    nicht.

  2. #2
    HaWe
    Gast
    leider auch nicht, gibt auch nur:
    2 joy:255,255 acc:258,258,258 but:1,1
    3 joy:255,255 acc:258,258,258 but:1,1
    4 joy:255,255 acc:258,258,258 but:1,1
    5 joy:255,255 acc:258,258,258 but:1,1
    6 joy:255,255 acc:258,258,258 but:1,1
    7 joy:255,255 acc:258,258,258 but:1,1
    8 joy:255,255 acc:258,258,258 but:1,1
    9 joy:255,255 acc:258,258,258 but:1,1
    10 joy:255,255 acc:258,258,258 but:1,1
    11 joy:255,255 acc:258,258,258 but:1,1
    12 joy:255,255 acc:258,258,258 but:1,1
    13 joy:255,255 acc:258,258,258 but:1,1
    14 joy:255,255 acc:258,258,258 but:1,1
    15 joy:255,255 acc:258,258,258 but:1,1

    und zwischendurch mal völlig unmotoviert, ohne mein Zutun:

    113 joy:255,255 acc:255,255,170 but:0,0
    114 joy:0,0 acc:0,0,0 but:0,0
    115 joy:0,0 acc:0,1,164 but:0,0
    116 joy:0,0 acc:0,0,0 but:0,0
    117 joy:0,3 acc:0,0,0 but:0,0
    118 joy:0,0 acc:0,0,0 but:0,0
    119 joy:0,0 acc:3,258,258 but:1,1
    120 joy:255,255 acc:258,258,258 but:1,1

    Code:
    #include <Wire.h>;
    
    
    void setup(){
       Serial.begin(115200);
    
       //nunchuck_setpowerpins();
       nunchuck_init();
    
       Serial.print("Nunchuck ready\n");
    
    }
    
     
    
    void loop(){
       nunchuck_get_data();
       nunchuck_print_data();
       delay(1000);
    }
    
    //=========================================================================//
    //Do not modify!!!!!!!!
    //=========================================================================//
    
    //
    // Nunchuck functions
    //
    
    
    static uint8_t nunchuck_buf[6]; // array to store nunchuck data,
    
    // Uses port C (analog in) pins as power & ground for Nunchuck
    static void nunchuck_setpowerpins() {
      /*
       #define pwrpin PC3
       #define gndpin PC2
       DDRC |= _BV(pwrpin) | _BV(gndpin);
       PORTC &=~ _BV(gndpin);
       PORTC |= _BV(pwrpin);
       */
       delay(100); // wait for things to stabilize
    }
    
    
    // initialize the I2C system, join the I2C bus,
    // and tell the nunchuck we're talking to it
    
    void nunchuck_init()
    {  /*
       Wire.begin(); // join I2C bus as master
       Wire.beginTransmission(0x52); // transmit to device 0x52
       Wire.write(0x40); // sends memory address
       Wire.write(0x00); // sends sent a zero.
       Wire.endTransmission(); // stop transmitting
       */
       
       byte cnt;
       uint8_t ctrlr_type[6];
    
       Wire.begin();
               
    // init controller
    delay(1);
    Wire.beginTransmission(0x52);      // device address
    Wire.write(0xF0);                    // 1st initialisation register
    Wire.write(0x55);                    // 1st initialisation value
    Wire.endTransmission();
    delay(1);
    Wire.beginTransmission(0x52);
    Wire.write(0xFB);                    // 2nd initialisation register
    Wire.write(0x00);                    // 2nd initialisation value
    Wire.endTransmission();
    delay(1);
               
    // read the extension type from the register block        
    Wire.beginTransmission(0x52);
    Wire.write(0xFA);                    // extension type register
    Wire.endTransmission();
    Wire.beginTransmission(0x52);
    Wire.requestFrom(0x52, 6);               // request data from controller
    for (cnt = 0; cnt < 6; cnt++) {
       if (Wire.available()) {
           ctrlr_type[cnt] = Wire.read(); // Should be 0x0000 A420 0101 for Classic Controller, 0x0000 A420 0000 for nunchuck
       }
    }
    Wire.endTransmission();
    delay(1); 
               
    // send the crypto key (zeros), in 3 blocks of 6, 6 & 4.
    Wire.beginTransmission(0x52);
    Wire.write(0xF0);                    // crypto key command register
    Wire.write(0xAA);                    // sends crypto enable notice
    Wire.endTransmission();
    delay(1);
    Wire.beginTransmission(0x52);
    Wire.write(0x40);                    // crypto key data address
    for (cnt = 0; cnt < 6; cnt++) {
       Wire.write(0x00);                    // sends 1st key block (zeros)
    }
    Wire.endTransmission();
    Wire.beginTransmission(0x52);
    Wire.write(0x40);                    // sends memory address
    for (cnt = 6; cnt < 12; cnt++) {
       Wire.write(0x00);                    // sends 2nd key block (zeros)
    }
    Wire.endTransmission();
    Wire.beginTransmission(0x52);
    Wire.write(0x40);                    // sends memory address
    for (cnt = 12; cnt < 16; cnt++) {
       Wire.write(0x00);                    // sends 3rd key block (zeros)
    }
    Wire.endTransmission();
    delay(1);
    // end device init
    }
    
    
    // Send a request for data to the nunchuck
    // was "send_zero()"
    void nunchuck_send_request() {
       Wire.beginTransmission(0x52); // transmit to device 0x52
       Wire.write(0x00); // sends one byte
       Wire.endTransmission(); // stop transmitting
    }
    
    
    // Receive data back from the nunchuck,
    // returns 1 on successful read. returns 0 on failure
    int nunchuck_get_data() {
    
       int cnt=0;
    
       Wire.requestFrom (0x52, 6); // request data from nunchuck   
       while (Wire.available ()) {   
       // receive byte as an integer   
       nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.read());   
       cnt++;      
    }
    
    nunchuck_send_request(); // send request for next data payload
    // If we recieved the 6 bytes, then go print them
       if (cnt >= 5) {
         return 1; // success
       }
       return 0; //failure
    }
    
    
    // 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 nunchuck_print_data() {
       static int i=0;
       int joy_x_axis = nunchuck_buf[0];
       int joy_y_axis = nunchuck_buf[1];
       int accel_x_axis = nunchuck_buf[2]; // * 2 * 2;
       int accel_y_axis = nunchuck_buf[3]; // * 2 * 2;
       int accel_z_axis = nunchuck_buf[4]; // * 2 * 2;
       
       int z_button = 0;
       int c_button = 0;
             
       // byte nunchuck_buf[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 ((nunchuck_buf[5] >> 0) & 1)
       z_button = 1;
       
       if ((nunchuck_buf[5] >> 1) & 1)
       c_button = 1;
       
       if ((nunchuck_buf[5] >> 2) & 1)
       accel_x_axis += 2;
       
       if ((nunchuck_buf[5] >> 3) & 1)
       accel_x_axis += 1;
       
       if ((nunchuck_buf[5] >> 4) & 1)
       accel_y_axis += 2;
       
       if ((nunchuck_buf[5] >> 5) & 1)
       accel_y_axis += 1;
    
       if ((nunchuck_buf[5] >> 6) & 1)
       accel_z_axis += 2;
    
       if ((nunchuck_buf[5] >> 7) & 1)
       accel_z_axis += 1;
    
       Serial.print(i,DEC);
       Serial.print("\t");
       Serial.print("joy:");
       Serial.print(joy_x_axis,DEC);
       Serial.print(",");
       Serial.print(joy_y_axis, DEC);
       Serial.print(" \t");
       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("\t");
       Serial.print("but:");
       Serial.print(z_button, DEC);
       Serial.print(",");
       Serial.print(c_button, DEC);
       Serial.print("\r\n"); // newline
       i++;
    
    }
    
    
    // Encode data to format that most wiimote drivers except
    // only needed if you use one of the regular wiimote drivers
    
    char nunchuk_decode_byte (char x) {
       x = (x ^ 0x17) + 0x17;
       return x;
    }
    
    
    // returns zbutton state: 1=pressed, 0=notpressed
    int nunchuck_zbutton() {
       return ((nunchuck_buf[5] >> 0) & 1) ? 0 : 1; // voodoo
    }
    
    
    // returns zbutton state: 1=pressed, 0=notpressed
    int nunchuck_cbutton()  {
       return ((nunchuck_buf[5] >> 1) & 1) ? 0 : 1; // voodoo
    }
    
    
    // returns value of x-axis joystick
    
    int nunchuck_joyx()  {
       return nunchuck_buf[0];
    }
    
    
    // returns value of y-axis joystick
    int nunchuck_joyy()  {
       return nunchuck_buf[1];
    }
    
    
    // returns value of x-axis accelerometer
    int nunchuck_accelx()  {
       return nunchuck_buf[2]; // FIXME: this leaves out 2-bits of the data
    }
    
    
    // returns value of y-axis accelerometer
    int nunchuck_accely()  {
       return nunchuck_buf[3]; // FIXME: this leaves out 2-bits of the data
    }
    
    
    // returns value of z-axis accelerometer
    int nunchuck_accelz()  {
       return nunchuck_buf[4]; // FIXME: this leaves out 2-bits of the data
    }
    Geändert von HaWe (29.03.2019 um 13:04 Uhr)

  3. #3
    Erfahrener Benutzer Robotik Einstein
    Registriert seit
    11.12.2007
    Ort
    weit weg von nahe Bonn
    Alter
    40
    Beiträge
    3.416
    puuhh da findet sich irgendwie nicht viel nützliches drüber

    sicher dass du nicht eventuell data und clock vertauscht hast?!

    als schuss ins blaue: tausch sonst einfach mal die data und clock pins, mehr als datensalat passiert da nicht

    edit: quark was red ich da ... der scanner hats ja erkannt XD

    https://www.hackster.io/infusion/usi...arduino-597254

    das ist leider ein englischer post aber der beschäftigt sich recht detailliert mit dem ding, vielleicht kannst du damit dein programm nachverfolgen und debuggen!?
    Es gibt 10 Sorten von Menschen: Die einen können binär zählen, die anderen
    nicht.

  4. #4
    HaWe
    Gast
    nee, I2C läuft ja, auch per i2c-scanner erkannt
    (getestet: umgestöpselt gehts gar nicht)

    das Programm von dem Post https://www.hackster.io/infusion/usi...arduino-597254 habe ich ja auch erfolglos ausprobiert

    PS, ich nutze so einen Adapter:
    https://www.ebay.de/sch/i.html?_from...02990&_sacat=0
    Geändert von HaWe (29.03.2019 um 16:11 Uhr)

  5. #5
    Erfahrener Benutzer Roboter Genie
    Registriert seit
    05.11.2007
    Beiträge
    1.076
    Hallo HaWe,
    schau mal bitte hier rein:
    https://allaboutmcu.wordpress.com/20...is-is-a-draft/

    Auszug:

    This will not work with the Wii nunchuck, even though we know its write address(which is 52 by the way, A4 in hex).
    For the communication to work – after sending the write adress for the first time – “0x40” must be sent before 0.

    StartI2C();
    IdleI2C();
    MasterWriteI2C(0xA4);
    MasterWriteI2C(0x40);
    MasterWriteI2C(0x00);
    StopI2C();

    And again for the knockoff versions of the nunchuck it’s different once more,
    there are not only one but TWO handshakes this time around, the cheeky bastards:

    StartI2C();
    IdleI2C();
    MasterWriteI2C(0xA4);
    MasterWriteI2C(0xF0);
    MasterWriteI2C(0x55);
    StopI2C();

    StartI2C();
    IdleI2C();
    MasterWriteI2C(0xA4);
    MasterWriteI2C(0xFB);
    MasterWriteI2C(0x00);
    StopI2C();

    Siro

  6. #6
    HaWe
    Gast
    Zitat Zitat von Siro Beitrag anzeigen
    Hallo HaWe,
    schau mal bitte hier rein:
    https://allaboutmcu.wordpress.com/20...is-is-a-draft/

    Auszug:

    This will not work with the Wii nunchuck, even though we know its write address(which is 52 by the way, A4 in hex).
    For the communication to work – after sending the write adress for the first time – “0x40” must be sent before 0.

    StartI2C();
    IdleI2C();
    MasterWriteI2C(0xA4);
    MasterWriteI2C(0x40);
    MasterWriteI2C(0x00);
    StopI2C();

    And again for the knockoff versions of the nunchuck it’s different once more,
    there are not only one but TWO handshakes this time around, the cheeky bastards:

    StartI2C();
    IdleI2C();
    MasterWriteI2C(0xA4);
    MasterWriteI2C(0xF0);
    MasterWriteI2C(0x55);
    StopI2C();

    StartI2C();
    IdleI2C();
    MasterWriteI2C(0xA4);
    MasterWriteI2C(0xFB);
    MasterWriteI2C(0x00);
    StopI2C();

    Siro
    das kann doch nicht richtig sein - die Nunchuk adresse ist 0x52, nicht 0xA4 und nicht (DEC) 52 !
    0x52 wird ja auch von meinem I2C Scanner erkannt...!

    Code:
    Scanning at 400k
    
    0  ** 01 02 03 04 05 06 07 -- -- -- -- -- -- -- -- 
    1  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    2  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    3  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    4  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    5  -- -- 52 -- -- -- -- -- -- -- -- -- -- -- -- -- 
    6  -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
    7  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ** 
    Scan result for i2cclock 400k
    found: 9 devices
    das einzige, wirklich seltsame ist, dass außer x52 immer auch x01...x07 erkannt werden...
    (x68 ist ein zusätzlicher MPU6050 zu Testzwecken)

  7. #7
    Erfahrener Benutzer Robotik Einstein
    Registriert seit
    07.03.2011
    Beiträge
    1.899
    Zitat Zitat von HaWe Beitrag anzeigen
    hallo,
    ich versuche erfolglos, einen Wii Nunchuck an Arduino (3.3V ARM Cortex) auszulesen. Ich habe 2 Nunchucks ausprobiert, beide verhalten sich identisch.
    Vom I2C Scanner wird jeder einzelne bei Adresse 0x52 erkannt, sowohl bei 100kHz als auch bei 400kHz. Pullups sind jew. 4.7k, an den Nunchucks oder I2C Bus liegt es also nicht. (Auch alternativ testweise angeschlossener MCP6050 funktioniert auf i2c)

    Spannungsversorgung über 3.3V und GND Buchse.

    Seltsamerweise aber liefert der i2c-Scanner auch gleichzeitig I2C Devices an x01 x02 x03 x04 x05 x06 x07 (zusätzlich zu x52), die verschwinden, wenn er abgesteckt ist.

    Es sind original-Nunchucks aus einem Wii-Set (IIRC).
    Ich benutze zwar kein Arduino, hab aber schon einen Nunchuk (Clone) verschiedentlich zum Laufen gebracht. Wenn man einen funktionierenden I2C Master hat, ist das kein Hexenwerk. Ist halt ein I2C Device wie tausend andere auch. Ich zeig den Code mal in Portionen, damit ich dazwischen ein paar Erläuterungen machen kann.

    Code:
    #include "I2cMaster.h"
    
    #define NUNCHUK_ADDR 0x52
    
    typedef struct {
        int x;
        int y;
        int acc_x;
        int acc_y;
        int acc_z;
        int c_button;
        int z_button;
    } nunchuk_data_t;
    
    int NunchukInit(void);
    int NunchukStart(void);
    int NunchukRead(nunchuk_data_t *data);
    Die Adresse ist natürlich 0x52, nur die Arduino-Jünger popeln da immer noch das R/W-Bit mit rein, weil die wohl die I2C-Spec nie gelesen haben oder weil ihnen die Aufteilung eines Bytes in verschiedene Bitfelder intellektuell zu anspruchsvoll ist. Hier wird nur die Struktur für das Ergebnis beschrieben. Das packt man gerne in ein Headerfile.
    Code:
    int NunchukInit(void) {
        I2cStart();
        if(I2cWriteAddress(NUNCHUK_ADDR, I2C_WRITE)){
            I2cStop();
            return -1;
        }
        if(I2cWriteByte(0x40)){
            I2cStop();
            return -1;
        }
        if(I2cWriteByte(0x00)){
            I2cStop();
            return -1;
        }
        I2cStop();
        return 0;
    }
    Das ist meine Init-Funktion. Hier werden dann die Funktionen aus meiner I2C lib verwendet. I2CStart() bzw I2CStop() erzeugt die entsprechenden Zustände auf dem Bus. I2cWriteAddress() setzt aus der Adresse und R/W das erste Byte des I2C Telegramms zusammen. Die Funktion liefert ACK/NAK zurück, wie es vom Bus kommt. Kommt NAK, wird die Sache abgebrochen. Bei I2cWriteAddress() heißt das typischerweise, es gibt kein Device auf dieser Adresse. So testen die I2C Scanner. Dann wird in das Nunchuk-Register mit der Adresse 0x40 eine 0 geschrieben, um den Nunchuk zu starten. Das findet man in Beispielen im Netz.

    Das erste Byte nach dem Adressbyte bei einem Write setzt bei I2C typischerweise den Registerpointer im Device. Hier wird er auf 0 gesetzt.

    Code:
    int NunchukStart(void) {
        // set register address
        I2cStart();
        if(I2cWriteAddress(NUNCHUK_ADDR, I2C_WRITE)){
            I2cStop();
            return -1;       // nunchuk didn't respond
        }
        if(I2cWriteByte(0x00)){
            I2cStop();
            return -1;
        }
        I2cStop();
    
    }
    Die interessanten Register im Nunchuk liegen ab Adresse 0, daher muß NunchukStart() vor jedem Auslesen ausgeführt werden. Beim Interpretieren der gelesenen Daten gibt es ein Problem. Die Datenbytes sind "verschlüsselt". Es ist aber eine wirklich simple Funktion, die man auch im Netz findet und bei mir NunchukDecode() heißt.

    Code:
    int NunchukDecode(int data){
        return ((data ^ 0x17) + 0x17) & 0xff;
    }
    
    int NunchukRead(nunchuk_data_t *data) {
        int temp;
    
        I2cStart();
        if (I2cWriteAddress(NUNCHUK_ADDR, I2C_READ)) {
            I2cStop();
            return -1;       // nunchuk didn't respond
        }
        data->x = NunchukDecode(I2cReadByte(I2C_ACK));
        data->x -= 128;             // center the value
        data->y = NunchukDecode(I2cReadByte(I2C_ACK));
        data->y -= 128;
        data->acc_x = NunchukDecode(I2cReadByte(I2C_ACK)) << 2;
        data->acc_y = NunchukDecode(I2cReadByte(I2C_ACK)) << 2;
        data->acc_z = NunchukDecode(I2cReadByte(I2C_ACK)) << 2;
        temp = NunchukDecode(I2cReadByte(I2C_NACK));
        I2cStop();
    // decode the last byte
        if(temp & 0x01)
            data->z_button = 0;
        else
            data->z_button = 1;
        if(temp & 0x02)
            data->c_button = 0;
        else
            data->c_button = 1;
    
        data->acc_x |= (temp >> 2) & 0x03;
        data->acc_y |= (temp >> 4) & 0x03;
        data->acc_z |= (temp >> 6) & 0x03;
    
        return 0;
    }
    Hier wird ein Byte nach dem anderen eingelesen, dekodiert und in die Struktur gepackt. Zum Schluß wird das letzte Byte gelesen und die Bits ausgewertet. Wenn ich mich recht erinnere, ist der Nunchuk pingelig, wenn das letzte Byte nicht mit NAK quittiert wird. Er bleibt dann hängen und blockiert den Bus.

    Hier noch ein Kommentar zu einer der gezeigten Arduino-lib. Dort wird nie ACK ausgewertet sondern immer weiter gemacht. Das führt dazu, daß selbst wenn der BUS leer ist, immer 0xff bzw 255 gelesen wird. Die Pullups sorgen für eine 1 auf dem Bus. Wenn ich also lese: "ich bekomme immer 255" oder auch andere Zweierpotenzen, die sich aus Umrechnungen ergeben können, weiß ich, daß der Bus klemmt oder das Device nicht angesprochen wird ohne in den Code zu schauen oder das Scope anzumachen.

    MfG Klebwax
    Strom fließt auch durch krumme Drähte !

Ähnliche Themen

  1. Wii Nunchuck I2C an Raspberry Pi
    Von wassy92x im Forum Allgemeines zum Thema Roboter / Modellbau
    Antworten: 0
    Letzter Beitrag: 27.01.2015, 15:24
  2. Nunchuck auslesen
    Von Snaper im Forum Sensoren / Sensorik
    Antworten: 12
    Letzter Beitrag: 28.02.2012, 09:45
  3. Wii Motion Plus - Nunchuck Passthrough Mode
    Von Che Guevara im Forum Basic-Programmierung (Bascom-Compiler)
    Antworten: 4
    Letzter Beitrag: 19.10.2011, 19:36
  4. Nunchuck mit Gyroskopen
    Von RobertM im Forum Microcontroller allgemeine Fragen/Andere Microcontroller
    Antworten: 1
    Letzter Beitrag: 21.12.2008, 16:40
  5. Nunchuck über i2c Ausleseprobleme (wohl Nunchukseitig)
    Von ustech im Forum AVR Hardwarethemen
    Antworten: 6
    Letzter Beitrag: 05.10.2008, 23:56

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •  

LiFePO4 Speicher Test