Difference between revisions of "Nunchuck"
Jump to navigation
Jump to search
Line 32: | Line 32: | ||
Note : remplacer beginSerial par Serial.begin dans la fonction setup(). |
Note : remplacer beginSerial par Serial.begin dans la fonction setup(). |
||
+ | |||
+ | |||
+ | Wiichuck NMEA Sentence |
||
+ | <pre> |
||
+ | /* |
||
+ | * WiiChuckNMEA |
||
+ | * |
||
+ | * output wii nunckuck values in a NMEA 0183 formatted message |
||
+ | * |
||
+ | * $WICHK,JOYX,JOYY,ACCX,ACCY,ACCZ,ZBUT,CBUT*CHECKSUM[CR][LF] |
||
+ | * |
||
+ | * TODO Calcutate checksum (always 00) |
||
+ | */ |
||
+ | |||
+ | #include <Wire.h> |
||
+ | #include "nunchuck_funcs.h" |
||
+ | |||
+ | int loop_cnt=0; |
||
+ | |||
+ | byte joyx,joyy,accx,accy,accz,zbut,cbut; |
||
+ | //const int LEDPIN = 13; |
||
+ | const int BAUD = 19200; |
||
+ | |||
+ | void setup() |
||
+ | { |
||
+ | Serial.begin(BAUD); |
||
+ | nunchuck_setpowerpins(); |
||
+ | nunchuck_init(); // send the initilization handshake |
||
+ | loop_cnt = 0; |
||
+ | } |
||
+ | |||
+ | void loop() |
||
+ | { |
||
+ | // if( loop_cnt > 100 ) { // every 100 msecs get new data |
||
+ | |||
+ | nunchuck_get_data(); |
||
+ | |||
+ | joyx = nunchuck_joyx(); |
||
+ | joyy = nunchuck_joyy(); |
||
+ | accx = nunchuck_accelx(); // ranges from approx 70 - 182 |
||
+ | accy = nunchuck_accely(); // ranges from approx 65 - 173 |
||
+ | accz = nunchuck_accelz(); // ranges from approx 65 - 173 |
||
+ | zbut = nunchuck_zbutton(); |
||
+ | cbut = nunchuck_cbutton(); |
||
+ | |||
+ | // FOR DEBUG nunchuck_print_data(); |
||
+ | |||
+ | Serial.print("$WICHK,"); |
||
+ | Serial.print(joyx,DEC); |
||
+ | Serial.print(","); |
||
+ | Serial.print(joyy,DEC); |
||
+ | Serial.print(","); |
||
+ | Serial.print((byte)accx,DEC); |
||
+ | Serial.print(","); |
||
+ | Serial.print((byte)accx,DEC); |
||
+ | Serial.print(","); |
||
+ | Serial.print((byte)accz,DEC); |
||
+ | Serial.print(","); |
||
+ | Serial.print((byte)zbut,DEC); |
||
+ | Serial.print(","); |
||
+ | Serial.print((byte)cbut,DEC); |
||
+ | Serial.print("*"); |
||
+ | Serial.print("00"); |
||
+ | Serial.print("\r\n"); |
||
+ | // } |
||
+ | loop_cnt++; |
||
+ | delay(10); |
||
+ | } |
||
+ | |||
+ | |||
+ | byte calcChecksumBuf(byte checksum, byte buf[], int i, int len){ |
||
+ | for(;i<len; i++){ |
||
+ | checksum=calcChecksum(checksum,buf[i]); |
||
+ | } |
||
+ | return checksum; |
||
+ | } |
||
+ | |||
+ | |||
+ | byte calcChecksum(byte checksum, byte c){ |
||
+ | if ( c == '$' ) return 0; |
||
+ | if ( c != '\r' && c != '\n' && c != '*' ) { |
||
+ | checksum ^= c; |
||
+ | } |
||
+ | return checksum; |
||
+ | } |
||
+ | </pre> |
||
==Liens== |
==Liens== |
Latest revision as of 03:11, 7 November 2012
Le Nunchuk est une manette d'extension (via le port I2C) de la WiiMote. Il inclut un joystick analogique, 2 boutons et un accéléromètre 3 axes.
Remarque : le WiiChuck Adapter évite de "détruire" le connecteur de Nunchuk
Utilisation du Nunchuk avec l'Arduino
Comment relier le nunchuk :
The Nunchuck uses the I2C communication bus for the output signals. The color codes I had to use for the nunchuck I used were: * Red – Power (+3.3 V strictly) * White – Ground * Green – Analog In 4 * Black – Analog In 5
Relier le nunchuck à l'arduino
Ce lien contient également un programme qui permet de récupérer les valeurs pour les différents contrôles (accéléromètre, joystick et boutons).
Exemple de code Arduino
Utilisation pour piloter un(/deux) servo moteur(s) Media:NunchuckServo.pde.txt
Note : remplacer beginSerial par Serial.begin dans la fonction setup().
Wiichuck NMEA Sentence
/* * WiiChuckNMEA * * output wii nunckuck values in a NMEA 0183 formatted message * * $WICHK,JOYX,JOYY,ACCX,ACCY,ACCZ,ZBUT,CBUT*CHECKSUM[CR][LF] * * TODO Calcutate checksum (always 00) */ #include <Wire.h> #include "nunchuck_funcs.h" int loop_cnt=0; byte joyx,joyy,accx,accy,accz,zbut,cbut; //const int LEDPIN = 13; const int BAUD = 19200; void setup() { Serial.begin(BAUD); nunchuck_setpowerpins(); nunchuck_init(); // send the initilization handshake loop_cnt = 0; } void loop() { // if( loop_cnt > 100 ) { // every 100 msecs get new data nunchuck_get_data(); joyx = nunchuck_joyx(); joyy = nunchuck_joyy(); accx = nunchuck_accelx(); // ranges from approx 70 - 182 accy = nunchuck_accely(); // ranges from approx 65 - 173 accz = nunchuck_accelz(); // ranges from approx 65 - 173 zbut = nunchuck_zbutton(); cbut = nunchuck_cbutton(); // FOR DEBUG nunchuck_print_data(); Serial.print("$WICHK,"); Serial.print(joyx,DEC); Serial.print(","); Serial.print(joyy,DEC); Serial.print(","); Serial.print((byte)accx,DEC); Serial.print(","); Serial.print((byte)accx,DEC); Serial.print(","); Serial.print((byte)accz,DEC); Serial.print(","); Serial.print((byte)zbut,DEC); Serial.print(","); Serial.print((byte)cbut,DEC); Serial.print("*"); Serial.print("00"); Serial.print("\r\n"); // } loop_cnt++; delay(10); } byte calcChecksumBuf(byte checksum, byte buf[], int i, int len){ for(;i<len; i++){ checksum=calcChecksum(checksum,buf[i]); } return checksum; } byte calcChecksum(byte checksum, byte c){ if ( c == '$' ) return 0; if ( c != '\r' && c != '\n' && c != '*' ) { checksum ^= c; } return checksum; }