Contrôle de 2 servo-moteurs avec un joystick via un Arduino Uno

Tutoriel : https://arduinofactory.fr/joystick-arduino/

Matériels :

I/ Installation du pilote CH341.

Lien : https://github.com/juliagoda/CH341SER

1/ Création du répertoire de travail.

util01@station40:~$ mkdir -p LABY
util01@station40:~$ cd LABY/
util01@station40:~/LABY$ 

2/ Téléchargement du code source.

util01@station40:~/LABY$ git clone https://github.com/juliagoda/CH341SER.git
util01@station40:~/LABY$ cd CH341SER/
util01@station40:~/LABY/CH341SER$

3/ Compilation du pilote.

util01@station40:~/LABY/CH341SER$ make

4/ Chargement du pilote.

util01@station40:~/LABY/CH341SER$ sudo make load
modprobe usbserial
insmod ch34x.ko
util01@station40:~/LABY/CH341SER$ 

5/ Vérification.

util01@station40:~/LABY/CH341SER$ sudo dmesg
...
[15056.458681] usbcore: registered new interface driver usbserial_generic
[15056.458696] usbserial: USB Serial support registered for generic
[15056.461660] ch34x: loading out-of-tree module taints kernel.
[15056.461722] ch34x: module verification failed: signature and/or required key missing - tainting kernel
[15056.463777] usbcore: registered new interface driver ch34x
[15056.463796] usbserial: USB Serial support registered for ch34x
util01@station40:~/LABY/CH341SER$

II/ Connexion de l'Arduino sur le pc.

1/ Brancher l'Arduino à l'ordinateur via le câble USB.

2/ Vérification.

util01@station40:~/LABY/CH341SER$ sudo dmesg
...
[15388.697615] usb 2-1.8: new full-speed USB device number 5 using ehci-pci
[15388.809172] usb 2-1.8: New USB device found, idVendor=0843, idProduct=5740, bcdDevice= 1.00
[15388.809180] usb 2-1.8: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[15388.809182] usb 2-1.8: Product: USB SER
[15388.809183] usb 2-1.8: Manufacturer: FIREPHX
[15388.809185] usb 2-1.8: SerialNumber: FX2348N
[15388.922110] cdc_acm 2-1.8:1.0: ttyACM0: USB ACM device
[15388.922150] usbcore: registered new interface driver cdc_acm
[15388.922152] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapters
util01@station40:~/LABY/CH341SER$

III/ Code Arduino.

// https://arduinofactory.fr/joystick-arduino/

#include <Servo.h> // Utilisation de la bibliothèque Servo.h.
//Broche de la carte Arduino
#define pinServo 9  // Broche pour le signal du servomoteur X.
#define pinServo1 10  
const int SW_pin = 2; // Pin digital pour indiquer la postion du bouton poussoir
const int X_pin = 0; // Pin analogique pour la coordonnée X
const int Y_pin = 1; // Pin analogique pour la coordonnée Y
unsigned short X,Y; // Variables pour les valeurs du joystick.
Servo Sx;        // Déclaration du Servo.
Servo Sy;

void setup() {
  pinMode(SW_pin, INPUT); // Configure SW comme une entré
  digitalWrite(SW_pin, HIGH); // Met de la tension dans SW pour dire au joystock que c'est une entré

  pinMode(pinServo, OUTPUT); // Broche servo moteur en sortie.
  Sx.attach(pinServo); // Fait la correspondance entre la broche du servomoteur et le pin
  Sx.write(90); // Met le servomoteur en position initiale.

  pinMode(pinServo1, OUTPUT); // Broche servo moteur en sortie.
  Sy.attach(pinServo1); // Fait la correspondance entre la broche du servomoteur et le pin
  Sy.write(90); // Met le servomoteur en position initiale.

  Serial.begin(9600); // Configure le moniteur série

}

void loop() {
  X = analogRead(X_pin); // Lecture des valeurs du joystick.
  Sx.write(rotation_servo(X)); // Envoi des signaux de rotation.

  Y = analogRead(Y_pin); // Lecture des valeurs du joystick.
  Sy.write(rotation_servo(Y)); // Envoi des signaux de rotation.

  delay(500);
}

byte rotation_servo(unsigned short n) // Fonction de correspondance.
{
  return (byte)(-0.175953 * n + 180);  
}

IV/ Vidéo d'exemple.