Arduino + Nunchuck (qualcosa non va nello script)


ciao tutti ne approfitto in questo post per presentarmi siccome non ho trovato una sezione predisposta (o non l'ho vista)  :smiley-mr-green:
comincio subito con il problema che ho, possiedo un arduino duemilanove e vorrei collegarci un nunchuck (non originale comprato su ebay: http://www.ebay.it/itm/220832743549?sspagename=strk:mewnx:it&_trksid=p3984.m1497.l2649), ho seguito questa guida http://siliconrepublic.blogspot.com/2010/08/arduino-based-human-interface-device.html
con questo codice:
code: [select]
#include "ps2dev.h" // emulate ps/2 device
#include <wire.h> // communicate on i2c port
#include <string.h>
#include <math.h>
#undef int
#include <stdio.h>

uint8_t outbuf[6]; // array store arduino input
int cnt = 0;
int ledpin = 13;

ps2dev keyboard(3,2); // ps2dev object (2:data, 3:clock)
int enabled =0; // pseudo variable state of "keyboard"

int joy_x_axis; //byte 1
int joy_y_axis; //byte 2
int accel_x_axis; //byte 3
int accel_y_axis; //byte 4
int accel_z_axis; //byte 5
int z_button = 0;
int c_button = 0;
int roll = 0;
int pitch = 0;

void ack() {
//acknowledge commands
while(keyboard.write(0xfa));
}


int keyboardcommand(int command) {
unsigned char val;
switch (command) {
case 0xff: //reset
ack();
//the while loop lets wait host ready
while(keyboard.write(0xaa)!=0);
break;
case 0xfe: //resend
ack();
break;
case 0xf6: //set defaults
//enter stream mode
ack();
break;
case 0xf5: //disable data reporting
//fm
enabled = 0;
ack();
break;
case 0xf4: //enable data reporting
//fm
enabled = 1;
ack();
break;
case 0xf3: //set typematic rate
ack();
keyboard.read(&val); //do nothing rate
ack();
break;
case 0xf2: //get device id
ack();
keyboard.write(0xab);
keyboard.write(0x83);
break;
case 0xf0: //set scan code set
ack();
keyboard.read(&val); //do nothing rate
ack();
break;
case 0xee: //echo
//ack();
keyboard.write(0xee);
break;
case 0xed: //set/reset leds
ack();
keyboard.read(&val); //do nothing rate
ack();
break;
}
}

void setup() {
wire.begin (); // join i2c bus address 0x52
nunchuck_init (); // send initilization handshake
delay(2000); //initialization time in case
// send keyboard start up
while(keyboard.write(0xaa)!=0);
}

void nunchuck_init ()
{
wire.begintransmission (0x52);// transmit device 0x52
wire.send (0x40); // sends memory address
wire.send (0x00); // sends sent zero.
wire.endtransmission (); // stop transmitting
}

void send_zero ()
{
wire.begintransmission (0x52);// transmit device 0x52
wire.send (0x00); // sends 1 byte
wire.endtransmission (); // stop transmitting
}

void loop() {
wire.requestfrom (0x52, 6); // request data nunchuck
while (wire.available ())
{
outbuf[cnt] = nunchuk_decode_byte (wire.receive ());
// receive byte integer
digitalwrite (ledpin, high); // sets led on
cnt++;
}

// if recieved 6 bytes, go process them
if (cnt >= 5)
{
process ();
}

unsigned char c;
//if host device wants send command:
if( (digitalread(3)==low) || (digitalread(2) == low)) {
while(keyboard.read(&c)) ;
keyboardcommand(c);
}
else{ //send keypresses accordingly using scancodes
if(joy_y_axis > 198){keyboard.write(0x1d);}
else{ keyboard.write(0xf0); keyboard.write(0x1d);}

if(joy_y_axis < 68){keyboard.write(0x1b);}
else{ keyboard.write(0xf0); keyboard.write(0x1b);}

if(joy_x_axis < 58){keyboard.write(0x1c);}
else{ keyboard.write(0xf0); keyboard.write(0x1c);}

if(joy_x_axis > 190){keyboard.write(0x23);}
else{ keyboard.write(0xf0); keyboard.write(0x23);}


if(pitch < 65){keyboard.write(0x43);}
else{ keyboard.write(0xf0); keyboard.write(0x43);}

if(pitch > 115){keyboard.write(0x42);}
else{ keyboard.write(0xf0); keyboard.write(0x42);}

if(roll > 125 ){keyboard.write(0x3b);}
else{ keyboard.write(0xf0); keyboard.write(0x3b);}

if(roll < 55){keyboard.write(0x4b);}
else{ keyboard.write(0xf0); keyboard.write(0x4b);}


if(!z_button){keyboard.write(0x29);}
else{ keyboard.write(0xf0); keyboard.write(0x29);}

if(!c_button){keyboard.write(0x12);}
else{ keyboard.write(0xf0); keyboard.write(0x12);}
}

cnt = 0;
send_zero (); // send request next bytes
delay (10);
}


void process (){
joy_x_axis = outbuf[0]; //byte 1
joy_y_axis = outbuf[1]; //byte 2
accel_x_axis = outbuf[2] * 2 * 2; //byte 3
accel_y_axis = outbuf[3] * 2 * 2; //byte 4
accel_z_axis = outbuf[4] * 2 * 2; //byte 5

z_button = 0;
c_button = 0;

roll = 0;
pitch = 0;
// byte outbuf[5] contains bits z , c buttons
// contains least significant bits accelerometer data
// have check each bit of byte outbuf[5]
if ((outbuf[5] >> 0) & 1) //'&1' see lsb after left shifts
{
z_button = 1; //bit 0
}
if ((outbuf[5] >> 1) & 1)
{
c_button = 1; //bit 1
}

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

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

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

//out here values 499, 518 , 216.0 might differ
//due slight differences nunchucks
roll = (atan2( (accel_z_axis-499), (accel_x_axis-515) )/3.14159*180);
pitch = (acos( (accel_y_axis-518)/216.0 )/3.14159*180);
}


// encode data format wiimote drivers except
// needed if use 1 of regular wiimote drivers
char nunchuk_decode_byte (char x)
{
x = (x ^ 0x17) + 0x17;
return x;
}



però mi restituisce questo errore:
code: [select]

sketch_dec17a.cpp:1:48: error: ps2dev.h: no such file or directory
sketch_dec17a:11: error: 'ps2dev' not name type
sketch_dec17a.cpp: in function 'void ack()':
sketch_dec17a:26: error: 'keyboard' not declared in scope
sketch_dec17a.cpp: in function 'int keyboardcommand(int)':
sketch_dec17a:36: error: 'keyboard' not declared in scope
sketch_dec17a:57: error: 'keyboard' not declared in scope
sketch_dec17a.cpp: in function 'void setup()':
sketch_dec17a:87: error: 'keyboard' not declared in scope
sketch_dec17a.cpp: in function 'void nunchuck_init()':
sketch_dec17a:93: error: 'class twowire' has no member named 'send'

as of arduino 1.0, wire.send() function renamed wire.write() consistency other libraries.

sketch_dec17a:94: error: 'class twowire' has no member named 'send'

as of arduino 1.0, wire.send() function renamed wire.write() consistency other libraries.

sketch_dec17a.cpp: in function 'void send_zero()':
sketch_dec17a:101: error: 'class twowire' has no member named 'send'

as of arduino 1.0, wire.send() function renamed wire.write() consistency other libraries.

sketch_dec17a.cpp: in function 'void loop()':
sketch_dec17a:109: error: 'class twowire' has no member named 'receive'

as of arduino 1.0, wire.receive() function renamed wire.read() consistency other libraries.

sketch_dec17a:124: error: 'keyboard' not declared in scope
sketch_dec17a:128: error: 'keyboard' not declared in scope
sketch_dec17a:129: error: 'keyboard' not declared in scope
sketch_dec17a:131: error: 'keyboard' not declared in scope
sketch_dec17a:132: error: 'keyboard' not declared in scope
sketch_dec17a:134: error: 'keyboard' not declared in scope
sketch_dec17a:135: error: 'keyboard' not declared in scope
sketch_dec17a:137: error: 'keyboard' not declared in scope
sketch_dec17a:138: error: 'keyboard' not declared in scope
sketch_dec17a:141: error: 'keyboard' not declared in scope
sketch_dec17a:142: error: 'keyboard' not declared in scope
sketch_dec17a:144: error: 'keyboard' not declared in scope
sketch_dec17a:145: error: 'keyboard' not declared in scope
sketch_dec17a:147: error: 'keyboard' not declared in scope
sketch_dec17a:148: error: 'keyboard' not declared in scope
sketch_dec17a:150: error: 'keyboard' not declared in scope
sketch_dec17a:151: error: 'keyboard' not declared in scope
sketch_dec17a:154: error: 'keyboard' not declared in scope
sketch_dec17a:155: error: 'keyboard' not declared in scope
sketch_dec17a:157: error: 'keyboard' not declared in scope
sketch_dec17a:158: error: 'keyboard' not declared in scope


ho provato altre guide anche con codice per nunchuck non originale ma niente da fare..
avete consigli ??
grazie per l'attenzione

ho provato anche con questo:


prova con il codice che ti allego, è sicuramente funzionante sia con nunchuk originali che cloni, collega il tutto seguendo lo schemino in testa al programma.

code: [select]

/*
_________
| 1 2 3 |
|       |
| 6 5 4 |
|_-----_|

•pin 1: verde - data (arduino analog pin 4)
•pin 2: (not connected)
•pin 3: rosso - 3.3v
•pin 4: giallo - clock (arduino analog pin 5)
•pin 5: (not connected)
•pin 6: bianco - gnd
*/

#include <wire.h>

void setup()
{
serial.begin(115000);
nunchuck_init(); // inizializza il nunchuck
}

void loop()
{
nunchuck_get_data();
nunchuck_print_data();
delay(100);
}

//
// nunchuck functions
//

static uint8_t nunchuck_buf[6];   //array utilizzato per immagazzinare dati in arrivo dal nunchuck,


void nunchuck_init()
{
wire.begin();                   
wire.begintransmission(0x52);    // trasmettiamo l'indirizzo della periferica 0x52
wire.send(0x40);        // trasmettiamo l'indirizzo della memoria
wire.send(0x00);        // trasmettiamo uno 0 perchè vogliamo leggere dati
wire.endtransmission();    // smettiamo di trasmettere
}

void nunchuck_send_request()
{
wire.begintransmission(0x52);    //trasmettiamo l'indirizzo del nunchuck
wire.send(0x00);        // trasmettiamo un byte
wire.endtransmission();    // smettiamo di trasmettere
}

// ricevere dati e immagazzinarli in un buffer
int nunchuck_get_data()
{
int cnt=0;
wire.requestfrom (0x52, 6);   
while (wire.available ()) {
// decodifichiamo byte che ci arrivano e li traformiamo in un intero
nunchuck_buf[cnt] = nunchuk_decode_byte(wire.receive());
cnt++;
}
nunchuck_send_request(); 

if (cnt >= 5) {
return 1;  //restituisce 1 se fallisce
}
return 0; //restituisce 0 se dati sono stati ricevuti in maniera corretta
}

// stampare dati arrivati
// dati di accelerazione sono lunghi 10 bit
// quindi ne leggiamo 8 poi aggiungiamo
// gli ultimi 2 bit.
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;
int accel_y_axis = nunchuck_buf[3] << 2;
int accel_z_axis = nunchuck_buf[4] << 2;

int z_button = 0;
int c_button = 0;

// byte nunchuck_buf[5] contains bits z , c buttons
// contains least significant bits accelerometer data
// have 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 += 1;
if ((nunchuck_buf[5] >> 3) & 1) accel_x_axis += 2;

if ((nunchuck_buf[5] >> 4) & 1) accel_y_axis += 1;
if ((nunchuck_buf[5] >> 5) & 1) accel_y_axis += 2;

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

serial.print(i,dec);
serial.print(",");

serial.print(accel_x_axis, dec);
serial.print(",");
serial.print(accel_y_axis, dec);
serial.print(",");
serial.print(accel_z_axis, dec);
serial.print(",");
serial.print(joy_x_axis,dec);
serial.print(",");
serial.print(joy_y_axis, dec);
serial.print(",");
serial.print(z_button, dec);
serial.print(",");
serial.print(c_button, dec);
serial.print(",");
serial.print(nunchuck_buf[5], bin);

serial.print("\r\n");  // newline
i++;
}

//  codifica nunchuck
char nunchuk_decode_byte (char x)
{
x = (x ^ 0x17) + 0x17;
return x;
}



e mi da questo errore :

code: [select]
sketch_dec17a.cpp: in function 'void nunchuck_init()':
sketch_dec17a:27: error: 'class twowire' has no member named 'send'

as of arduino 1.0, wire.send() function renamed wire.write() consistency other libraries.

sketch_dec17a:28: error: 'class twowire' has no member named 'send'

as of arduino 1.0, wire.send() function renamed wire.write() consistency other libraries.

sketch_dec17a.cpp: in function 'void nunchuck_send_request()':
sketch_dec17a:35: error: 'class twowire' has no member named 'send'

as of arduino 1.0, wire.send() function renamed wire.write() consistency other libraries.

sketch_dec17a.cpp: in function 'int nunchuck_get_data()':
sketch_dec17a:46: error: 'class twowire' has no member named 'receive'

as of arduino 1.0, wire.receive() function renamed wire.read() consistency other libraries.



Arduino Forum > International > Italiano > Generale (Moderator: leo72) > Arduino + Nunchuck (qualcosa non va nello script)


arduino

Comments

Popular posts from this blog

Joomla site hacked, cant see front and - Joomla! Forum - community, help and support

Christian Home School Programs - Joomla! Forum - community, help and support

Trouble with PF_OutFlag_I_USE_AUDIO and PF_CHECKOUT_LAYER_AUDIO