Nunchuk sur radio RC

Bonjour à tous.
Je ne suis pas un habitué de ce forum mais je viens vous demander de l’aide.

Je vous explique mon projet, qui lui même fait partie d’un projet beaucoup plus gros.
Je voudrais relier un nunchuk sur ma radiocommande afin de pouvoir piloter une nacelle. En gros, je piloterais mon quadricoptère et ma femme cadrera la vidéo.

J’ai une connaissance quasi nul en programmation, je suis capable de comprendre certaines choses mais pas d’écrire.

J’ai donc trouver du “tout fait” car le reste de mon projet m’occupe suffisamment pour avoir le temps d’apprendre à programmer.

Seulement ce code comporte quelques erreurs car apparement il y a eu quelques changements ces dernières années. Si quelqu’un avait la gentillesse de le corriger, cela me permettrait d’avancer pas mal.

Voici le code

// **********************************************************
// *************   Lay-Z Nunchuck RC Control Code   *********
// ***   Lay-Z Designed by Melih Karakelle on 2010-2012   ***
// **  an Arudino based Wii Nunchuck & MotionPlus Reader  ***
// **       This Source code licensed under GPL            **
// **********************************************************
// Version Number     : 1.00
// Latest Code Update : 2012-01-12
// Supported Hardware : Lay-Z boards (store.flytron.com)
// Project Forum      : http://forum.flytron.com
// **********************************************************
// # PROJECT DEVELOPERS # 
// Melih Karakelle (http://www.flytron.com) (forum nick name: Flytron)


// USAGE
// Use compatible trainer plug for your transmitter
// Connect (-) and (+) pins to supply pins of your transmitter
// Connect (S) pin to your transmitter's PPM-INPUT pin.
// Plug a Wii Nunchuck to Lay-Z
// Power on your transmitter and configure the trainer port as input and select the right (1-6) channels.
// Pull the trainer switch of your Tx for enabling trainer port.


#include <math.h>

#include "Wire.h"
#include "WiiChuck.h"


int ROLL;
int PITCH;
int JOYX;
int JOYY;
int BTNC;
int BTNZ;


#define MAXANGLE 90
#define MINANGLE -90
#define BLUE_LED 13

#define Channel_Count  6
#define PPM_Type  0
// 0 = NegativePPM  1=PositivePPM


WiiChuck chuck = WiiChuck();
int angleStart, currentAngle;
int tillerStart = 0;
double angle;


unsigned int Servo_Position[10] = {1500,1500,1500,1500,1500,1500,1500,1500,1500,1500}; //real servo position values
static unsigned char Servo_Number = 0;
unsigned int total_ppm_time=0;

void INIT_SERVO_DRIVER(void)
{
   TCCR1B   =   0x00;   //stop timer
   TCNT1H   =   0x00;   //setup
   TCNT1L   =   0x00;
   ICR1   =   40000;   // used for TOP, makes for 50 hz
   
   TCCR1A   =   0x02;   
   TCCR1B   =   0x1A; //start timer with 1/8 prescaler for 0.5us PPM resolution
   
   TIMSK1 = _BV (TOIE1);   
} 

ISR(TIMER1_OVF_vect)
  {
  unsigned int us; // this value is not real microseconds, we are using 0.5us resolution (2048 step), this is why the all values 2 times more than real microseconds.
  
  if (PPM_Type==0)
      PORTB &= ~_BV(1);
      else
      PORTB |= _BV(1);
  
  Servo_Number++; // jump to next servo
  if (Servo_Number>Channel_Count) // back to the first servo 
    {
    total_ppm_time = 0; // clear the total servo ppm time
    Servo_Number=0;
    } 

  if (Servo_Number == Channel_Count)  // Check the servo number. 
      {
        //Servos accepting 50hz ppm signal, this is why we are waiting for 20ms before second signal brust. 
        us = 40000 - total_ppm_time; //wait for total 20ms loop.  waiting time = 20.000us - total servo times

      }
      else
        us = 2 * Servo_Position[Servo_Number]; // read the servo timing from buffer
  
  total_ppm_time += us; // calculate total servo signal times.
  
  delayMicroseconds(300);
 
  if (PPM_Type==0)
      PORTB |= _BV(1);
      else
      PORTB &= ~_BV(1);
      
  TCNT1 = (40000 + 600) - us; // configure the timer interrupt for X micro seconds     
}


void setup() {
  Serial.begin(115200);
  chuck.begin();
  chuck.update();
  //chuck.calibrateJoy();
  
  pinMode(9,OUTPUT); //B1 pin is PPM Out
  
  INIT_SERVO_DRIVER();
}


void loop() {
  digitalWrite(BLUE_LED, LOW);
  delay(15);
  digitalWrite(BLUE_LED, HIGH);
  chuck.update(); 

  ROLL = map(chuck.readRoll(), -30, 30, 1000, 2000);
  PITCH = map(chuck.readPitch(), 60, 120, 1000, 2000);
  JOYX = map(chuck.readJoyX(), -128, 128, 1200, 1800);
  JOYY = map(chuck.readJoyY(), -128, 128, 1200, 1800);
  BTNC = map(chuck.cPressed(), 0, 1, 1200, 1800);
  BTNZ = map(chuck.zPressed(), 0, 1, 1200, 1800); 

  Servo_Position[0] = ROLL;
  Servo_Position[1] = PITCH;
  Servo_Position[2] = JOYX;
  Servo_Position[3] = JOYY;
  Servo_Position[4] = BTNC;
  Servo_Position[5] = BTNZ;
  
 
  
  /*
  Serial.print((int)chuck.readAccelX()); 
  Serial.print(", ");  
  Serial.print((int)chuck.readAccelY()); 
  Serial.print(", ");  
  Serial.print((int)chuck.readAccelZ()); 
  */
  
  
  //uncomment these lines for reading the values by your serial monitor.
  /*  
  Serial.print(chuck.readRoll());
  Serial.print(", ");  
  Serial.print(chuck.readPitch());
  Serial.print(", "); 
  
  Serial.print((int)chuck.readJoyX()); 

  Serial.print(", ");  
  Serial.print((int)chuck.readJoyY()); 

  Serial.print(", ");  
  Serial.print((int)chuck.zPressed()); 
  
  Serial.print(", ");  
  Serial.print((int)chuck.cPressed()); 

  Serial.println();
  */
  


}

Et le 2eme onglet

/*
 * Nunchuck -- Use a Wii Nunchuck
 * Tim Hirzel http://www.growdown.com
 * 
 notes on Wii Nunchuck Behavior.
 This library provides an improved derivation of rotation angles from the nunchuck accelerometer data.
 The biggest different over existing libraries (that I know of ) is the full 360 degrees of Roll data
 from teh combination of the x and z axis accelerometer data using the math library atan2. 

 It is accurate with 360 degrees of roll (rotation around axis coming out of the c button, the front of the wii),
 and about 180 degrees of pitch (rotation about the axis coming out of the side of the wii).  (read more below)

 In terms of mapping the wii position to angles, its important to note that while the Nunchuck
 sense Pitch, and Roll, it does not sense Yaw, or the compass direction.  This creates an important
 disparity where the nunchuck only works within one hemisphere.  At a result, when the pitch values are 
 less than about 10, and greater than about 170, the Roll data gets very unstable.  essentially, the roll
 data flips over 180 degrees very quickly.   To understand this property better, rotate the wii around the
 axis of the joystick.  You see the sensor data stays constant (with noise).  Because of this, it cant know
 the difference between arriving upside via 180 degree Roll, or 180 degree pitch.  It just assumes its always
 180 roll.


 * 
 * This file is an adaptation of the code by these authors:
 * Tod E. Kurt, http://todbot.com/blog/
 *
 * The Wii Nunchuck reading code is taken from Windmeadow Labs
 * http://www.windmeadow.com/node/42
 */

#ifndef WiiChuck_h
#define WiiChuck_h

#include "WProgram.h" 
#include <Wire.h>
#include <math.h>


// these may need to be adjusted for each nunchuck for calibration
#define ZEROX 510  
#define ZEROY 490
#define ZEROZ 460
#define RADIUS 210  // probably pretty universal

#define DEFAULT_ZERO_JOY_X 124
#define DEFAULT_ZERO_JOY_Y 132



class WiiChuck {
    private:
        byte cnt;
        uint8_t status[6]; // array to store wiichuck output
        byte averageCounter;
        //int accelArray[3][AVERAGE_N];  // X,Y,Z
        int i;
        int total;
        uint8_t zeroJoyX;   // these are about where mine are
        uint8_t zeroJoyY; // use calibrateJoy when the stick is at zero to correct
        int lastJoyX;
        int lastJoyY;
        int angles[3];

        boolean lastZ, lastC;


    public:

        byte joyX;
        byte joyY;
        boolean buttonZ;
        boolean buttonC;
        void begin() 
        {
            Wire.begin();
            cnt = 0;
            averageCounter = 0;
            // instead of the common 0x40 -> 0x00 initialization, we
            // use 0xF0 -> 0x55 followed by 0xFB -> 0x00.
            // this lets us use 3rd party nunchucks (like cheap $4 ebay ones)
            // while still letting us use official oness.
            // only side effect is that we no longer need to decode bytes in _nunchuk_decode_byte
            // see http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1264805255
            //  
            Wire.beginTransmission(0x52); // device address
            Wire.send(0xF0);
            Wire.send(0x55);
            Wire.endTransmission();
            delay(1);
            Wire.beginTransmission(0x52);
            Wire.send(0xFB);
            Wire.send(0x00);
            Wire.endTransmission();
            update();            
            for (i = 0; i<3;i++) {
                angles[i] = 0;
            }
            zeroJoyX = DEFAULT_ZERO_JOY_X;
            zeroJoyY = DEFAULT_ZERO_JOY_Y;
        }


        void calibrateJoy() {
            zeroJoyX = joyX;
            zeroJoyY = joyY;
        }

        void update() {

            Wire.requestFrom (0x52, 6); // request data from nunchuck
            while (Wire.available ()) {
                // receive byte as an integer
                status[cnt] = _nunchuk_decode_byte (Wire.receive()); //
                cnt++;
            }
            if (cnt > 5) {
                lastZ = buttonZ;
                lastC = buttonC;
                lastJoyX = readJoyX();
                lastJoyY = readJoyY();
                //averageCounter ++;
                //if (averageCounter >= AVERAGE_N)
                //    averageCounter = 0;

                cnt = 0;
                joyX = (status[0]);
                joyY = (status[1]);
                for (i = 0; i < 3; i++) 
                    //accelArray[i][averageCounter] = ((int)status[i+2] << 2) + ((status[5] & (B00000011 << ((i+1)*2) ) >> ((i+1)*2))); 
                    angles[i] = (status[i+2] << 2) + ((status[5] & (B00000011 << ((i+1)*2) ) >> ((i+1)*2))); 

                //accelYArray[averageCounter] = ((int)status[3] << 2) + ((status[5] & B00110000) >> 4); 
                //accelZArray[averageCounter] = ((int)status[4] << 2) + ((status[5] & B11000000) >> 6); 

                buttonZ = !( status[5] & B00000001);
                buttonC = !((status[5] & B00000010) >> 1);
                _send_zero(); // send the request for next bytes

            }
        }


    // UNCOMMENT FOR DEBUGGING
    //byte * getStatus() {
    //    return status;
    //}

    float readAccelX() {
       // total = 0; // accelArray[xyz][averageCounter] * FAST_WEIGHT;
        return (float)angles[0] - ZEROX;
    }
    float readAccelY() {
        // total = 0; // accelArray[xyz][averageCounter] * FAST_WEIGHT;
        return (float)angles[1] - ZEROY;
    }
    float readAccelZ() {
        // total = 0; // accelArray[xyz][averageCounter] * FAST_WEIGHT;
        return (float)angles[2] - ZEROZ;
    }

    boolean zPressed() {
        return (buttonZ);// && ! lastZ);
    }
    boolean cPressed() {
        return (buttonC);// && ! lastC);
    }

    // for using the joystick like a directional button
    boolean rightJoy(int thresh=60) {
        return (readJoyX() > thresh and lastJoyX <= thresh);
    }

    // for using the joystick like a directional button
    boolean leftJoy(int thresh=60) {
        return (readJoyX() < -thresh and lastJoyX >= -thresh);
    }


    int readJoyX() {
        return (int) joyX - zeroJoyX;
    }

    int readJoyY() {
        return (int)joyY - zeroJoyY;
    }


    // R, the radius, generally hovers around 210 (at least it does with mine)
   // int R() {
   //     return sqrt(readAccelX() * readAccelX() +readAccelY() * readAccelY() + readAccelZ() * readAccelZ());  
   // }


    // returns roll degrees
    int readRoll() {
        return (int)(atan2(readAccelX(),readAccelZ())/ M_PI * 180.0);
    }

    // returns pitch in degrees
    int readPitch() {        
        return (int) (acos(readAccelY()/RADIUS)/ M_PI * 180.0);  // optionally swap 'RADIUS' for 'R()'
    }

    private:
        byte _nunchuk_decode_byte (byte x)
        {
            //decode is only necessary with certain initializations 
            //x = (x ^ 0x17) + 0x17;
            return x;
        }

        void _send_zero()
        {
            Wire.beginTransmission (0x52); // transmit to device 0x52
            Wire.send (0x00); // sends one byte
            Wire.endTransmission (); // stop transmitting
        }

};


#endif

Je vous remercie d’avance.

Malgré mes connaissances à la ramasse, je cherche quand même, donc déjà un soucis avec le Wprogram.h qui est devenu Arduino.h, mais lorsque je le change, le fichier qui se trouve dans le dossier qui s'appelle Wiichuck.pde se transforme en WiiChuck.ino.

Après je sais qu'il y a aussi des erreurs avec les commandes wire.***** qui ont changées, mais le premier soucis m'empêche déjà d'avancer.