Buenas a todo el foro, en primer lugar gracias de antemano, y enhorabuena por todos los proyectos, y ahora voy con el mio...
estoy montado una cámara giroscópica para grabar en la moto cuando vamos al circuito, y he montado un sistema con un servo y el acelerómetro del nunchuck de la wii,
el funcionamiento en bueno pero el alulo de giro del servo es mayor que el angulo de giro del acelerómetro por lo que la cámara no queda recta...
donde podría cambiar el ángulo del giro del servo en el siguiente código... GRACIAS otra vez...
#include <Wire.h>
#include <string.h>
#include <stdio.h>
uint8_t outbuf[6];
uint8_t ctrlr_type[6];
int cnt = 0;
int ledPin = 13;
int servoPin = 7;
int servoPin2 = 6;
int pulseWidth = 0;
int pulseWidth2 = 0;
long lastPulse = 0;
long lastPulse2 = 0;
int z_button = 0;
int c_button = 0;
int refreshTime = 20;
int minPulse = 1000;
int minPulse2 = 500;
int dtime=10;
#define pwbuffsize 10
long pwbuff[pwbuffsize];
long pwbuffpos = 0;
long pwbuff2[pwbuffsize];
long pwbuffpos2 = 0;
void setup()
{
// beginSerial (19200);
Wire.begin ();
nunchuck_init ();
pinMode(servoPin, OUTPUT);
pinMode(servoPin2, OUTPUT);
pulseWidth = minPulse;
pulseWidth2 = minPulse2;
Serial.print ("Finished setup\n");
}
void nunchuck_init()
{
byte cnt;
Wire.begin();
// init controller PARA NUNCHAKU NO ORIGINAL
//_________________________________________
delay(1);
Wire.beginTransmission(0x52); // device address
Wire.send(0xF0); // 1st initialisation register
Wire.send(0x55); // 1st initialisation value
Wire.endTransmission();
delay(1);
Wire.beginTransmission(0x52);
Wire.send(0xFB); // 2nd initialisation register
Wire.send(0x00); // 2nd initialisation value
Wire.endTransmission();
delay(1);
// read the extension type from the register block
Wire.beginTransmission(0x52);
Wire.send(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.receive(); // 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.send(0xF0); // crypto key command register
Wire.send(0xAA); // sends crypto enable notice
Wire.endTransmission();
delay(1);
Wire.beginTransmission(0x52);
Wire.send(0x40); // crypto key data address
for (cnt = 0; cnt < 6; cnt++) {
Wire.send(0x00); // sends 1st key block (zeros)
}
Wire.endTransmission();
Wire.beginTransmission(0x52);
Wire.send(0x40); // sends memory address
for (cnt = 6; cnt < 12; cnt++) {
Wire.send(0x00); // sends 2nd key block (zeros)
}
Wire.endTransmission();
Wire.beginTransmission(0x52);
Wire.send(0x40); // sends memory address
for (cnt = 12; cnt < 16; cnt++) {
Wire.send(0x00); // sends 3rd key block (zeros)
}
Wire.endTransmission();
delay(1);
// end device init
}
void send_zero()
{
Wire.beginTransmission (0x52);
Wire.send (0x00);
Wire.endTransmission ();
}
int t = 0;
void loop()
{
t++;
long last = millis();
if( t == 1) {
t = 0;
Wire.requestFrom (0x52, 6);
while (Wire.available ()) {
outbuf[cnt] = nunchuk_decode_byte (Wire.receive ());
digitalWrite (ledPin, HIGH);
cnt++;
}
if (cnt >= 5) {
// printNunchuckData();
int z_button = 0;
int c_button = 0;
if ((outbuf[5] >> 0) & 1)
z_button = 1;
if ((outbuf[5] >> 1) & 1)
c_button = 1;
switch (c_button) {
case 1:
switch (z_button) {
case 0:
break;
case 1:
muovi();
break;
}
break;
case 0:
switch (z_button) {
case 0:
delay(10000);
break;
case 1:
delay(3000);
break;
}
break;
}
}
cnt = 0;
send_zero();
} // if(t==)
updateServo();
delay(dtime);
}
void updateServo() {
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH);
delayMicroseconds(pulseWidth);
digitalWrite(servoPin, LOW);
digitalWrite(servoPin2, HIGH);
delayMicroseconds(pulseWidth2);
digitalWrite(servoPin2, LOW);
lastPulse = millis();
}
}
int i=0;
void printNunchuckData()
{
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;
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;
Serial.print (i,DEC);
Serial.print ("\t");
Serial.print ("X: ");
Serial.print (joy_x_axis, DEC);
Serial.print ("\t");
Serial.print ("Y: ");
Serial.print (joy_y_axis, DEC);
Serial.print ("\t");
Serial.print ("AccX: ");
Serial.print (accel_x_axis, DEC);
Serial.print ("\t");
Serial.print ("AccY: ");
Serial.print (accel_y_axis, DEC);
Serial.print ("\t");
Serial.print ("AccZ: ");
Serial.print (accel_z_axis, DEC);
Serial.print ("\t");
Serial.print (z_button, DEC);
Serial.print (" ");
Serial.print (c_button, DEC);
Serial.print ("\r\n");
i++;
}
char nunchuk_decode_byte (char x)
{
x = (x ^ 0x17) + 0x17;
return x;
}
void muovi (){
float tilt = (700 - outbuf[3]22);
float tilt2 = outbuf[2]22;
tilt = (tilt);
pulseWidth = (tilt * 5) + minPulse;
tilt2 = (tilt2-288);
pulseWidth2 = (tilt2 * 5) + minPulse2;
pwbuff[pwbuffpos] = pulseWidth;
pwbuff2[pwbuffpos2] = pulseWidth2;
if( ++pwbuffpos == pwbuffsize ) pwbuffpos = 0;
if( ++pwbuffpos2 == pwbuffsize ) pwbuffpos2 = 0;
pulseWidth=0;
pulseWidth2=0;
for( int p=0; p<pwbuffsize; p++ ){
pulseWidth += pwbuff[p];
pulseWidth2 += pwbuff2[p];
}
pulseWidth /= pwbuffsize;
pulseWidth2 /= pwbuffsize;
}