start:arduino:joystick
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| start:arduino:joystick [2024/04/02 10:57] – [Voyons l'étalonnage du potentiomètre de l'axe X.] gerardadmin | start:arduino:joystick [2024/04/02 12:55] (Version actuelle) – [Utilisation de la manette Joystick avec Arduino] gerardadmin | ||
|---|---|---|---|
| Ligne 99: | Ligne 99: | ||
| ===== Comment connecter le module joystick avec Arduino UNO ===== | ===== Comment connecter le module joystick avec Arduino UNO ===== | ||
| - | {{ : | + | {{ : |
| + | |||
| + | -1- Connectez l'une des broches GND de l' | ||
| + | |||
| + | J' | ||
| + | |||
| + | -2- La broche SEL peut également être étiquetée SW. Il s'agit de l' | ||
| + | |||
| + | N' | ||
| + | |||
| + | -3- Connectez la broche d' | ||
| + | |||
| + | -4- Connectez la broche analogique A0 | ||
| + | |||
| + | Connectez la broche d' | ||
| + | |||
| + | -5- Connectez la broche du capteur étiquetée VCC à la broche Arduino 5 V. | ||
| + | |||
| + | |||
| + | =====Programme Arduino contrôle servo avec joystick===== | ||
| + | |||
| + | |||
| + | <code c joystick001.ino> | ||
| + | #define pinX A1 | ||
| + | |||
| + | #include " | ||
| + | Servo servo1; | ||
| + | |||
| + | void setup() { | ||
| + | | ||
| + | |||
| + | | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | int X = analogRead(pinX); | ||
| + | |||
| + | X = map(X, 0, 1023, 0, 180); | ||
| + | |||
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | =====Utilisation de la manette Joystick avec Arduino===== | ||
| + | |||
| + | <code c joystick002.ino> | ||
| + | #include < | ||
| + | //—– Adressage matériel —– | ||
| + | // En cas de non fonctionnement, | ||
| + | // commentaire et retirez le commentaire à la ligne 9. | ||
| + | LiquidCrystal_I2C lcd(0x27, 20, 4); | ||
| + | |||
| + | int VRx = A0; | ||
| + | int VRy = A1; | ||
| + | int SW = 2; | ||
| + | |||
| + | int xPosition = 0; | ||
| + | int yPosition = 0; | ||
| + | int SW_state = 0; | ||
| + | int mapX = 0; | ||
| + | int mapY = 0; | ||
| + | |||
| + | void setup() { | ||
| + | lcd.init(); // initialisation de l’afficheur | ||
| + | pinMode(VRx, | ||
| + | pinMode(VRy, | ||
| + | pinMode(SW, INPUT_PULLUP); | ||
| + | |||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | lcd.backlight(); | ||
| + | lcd.clear(); | ||
| + | xPosition = analogRead(VRx); | ||
| + | yPosition = analogRead(VRy); | ||
| + | SW_state = digitalRead(SW); | ||
| + | mapX = map(xPosition, | ||
| + | mapY = map(yPosition, | ||
| + | |||
| + | if ((mapX> | ||
| + | lcd.print(" | ||
| + | if ((mapX> | ||
| + | lcd.print(" | ||
| + | if ((mapX< | ||
| + | lcd.print(" | ||
| + | if ((mapX> | ||
| + | lcd.print(" | ||
| + | if (SW_state==0) | ||
| + | lcd.print(" | ||
| + | delay(1000); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Contrôleur de joystick pour robot MeArm ===== | ||
| + | |||
| + | <code c joystick003.ino> | ||
| + | /* meArm analog joysticks version 1.3.1 - UtilStudio.com Dec 2018 | ||
| + | Uses two analogue joysticks and four servos. | ||
| + | |||
| + | In version 1.3 was improved | ||
| + | Some bugs was removed. | ||
| + | |||
| + | First joystick | ||
| + | | ||
| + | |||
| + | | ||
| + | | ||
| + | Press button for 2 seconds | ||
| + | |||
| + | | ||
| + | | ||
| + | GND | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | 9 | ||
| + | 6 | ||
| + | | ||
| + | */ | ||
| + | # | ||
| + | |||
| + | bool repeatePlaying = false; /* Repeatedly is running recorded | ||
| + | int delayBetweenCycles = 2000; /* Delay between cycles */ | ||
| + | |||
| + | int basePin = 11; /* Base servo */ | ||
| + | int shoulderPin = 10; /* Shoulder servo */ | ||
| + | int elbowPin = 9; /* Elbow servo */ | ||
| + | int gripperPin = 6; /* Gripper | ||
| + | |||
| + | int xdirPin = 0; /* Base - joystick1*/ | ||
| + | int ydirPin = 1; /* Shoulder - joystick1 */ | ||
| + | int zdirPin = 3; /* Elbow - joystick2 | ||
| + | int gdirPin = 2; /* Gripper - joystick2 */ | ||
| + | |||
| + | //int pinRecord = A4; /* Button record - backward compatibility */ | ||
| + | //int pinPlay = A5; / | ||
| + | int pinRecord = PD2; /* Button record | ||
| + | int pinPlay = PD3; /* Button play - recommended (A5 is deprecated, will by used for additional joystick) */ | ||
| + | int pinLedRecord = PD4; /* LED - indicates recording | ||
| + | |||
| + | bool useInternalPullUpResistors | ||
| + | |||
| + | const int buffSize = 512; /* Size of recording buffer */ | ||
| + | |||
| + | int startBase = 90; | ||
| + | int startShoulder = 90; | ||
| + | int startElbow = 90; | ||
| + | int startGripper | ||
| + | |||
| + | int posBase = 90; | ||
| + | int posShoulder = 90; | ||
| + | int posElbow = 90; | ||
| + | int posGripper = 0; | ||
| + | |||
| + | int lastBase = 90; | ||
| + | int lastShoulder = 90; | ||
| + | int lastElbow | ||
| + | int lastGripper = 90; | ||
| + | |||
| + | int minBase = 0; | ||
| + | int maxBase = 150; | ||
| + | int minShoulder = 0; | ||
| + | int maxShoulder = 150; | ||
| + | int minElbow = 0; | ||
| + | int maxElbow | ||
| + | int minGripper = 0; | ||
| + | int maxGripper = 150; | ||
| + | |||
| + | const int countServo | ||
| + | int buff[buffSize]; | ||
| + | int buffAdd[countServo]; | ||
| + | int recPos = 0; | ||
| + | int playPos = 0; | ||
| + | |||
| + | int buttonRecord = HIGH; | ||
| + | int buttonPlay = HIGH; | ||
| + | |||
| + | int buttonRecordLast = LOW; | ||
| + | int buttonPlayLast = LOW; | ||
| + | |||
| + | bool record = false; | ||
| + | bool play = false; | ||
| + | bool debug = false; | ||
| + | |||
| + | String command = " | ||
| + | int printPos = 0; | ||
| + | |||
| + | int buttonPlayDelay = 20; | ||
| + | int buttonPlayCount = 0; | ||
| + | |||
| + | bool ledLight = false; | ||
| + | |||
| + | Servo servoBase; | ||
| + | Servo servoShoulder; | ||
| + | Servo servoElbow; | ||
| + | Servo servoGripper; | ||
| + | |||
| + | void setup() { | ||
| + | Serial.begin(9600); | ||
| + | |||
| + | if (useInternalPullUpResistors) | ||
| + | pinMode(pinRecord, | ||
| + | pinMode(pinPlay, | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | pinMode(pinRecord, | ||
| + | pinMode(pinPlay, | ||
| + | } | ||
| + | |||
| + | pinMode(xdirPin, | ||
| + | pinMode(ydirPin, | ||
| + | pinMode(zdirPin, | ||
| + | pinMode(gdirPin, | ||
| + | |||
| + | pinMode(pinLedRecord, | ||
| + | |||
| + | servoBase.attach(basePin); | ||
| + | servoShoulder.attach(shoulderPin); | ||
| + | servoElbow.attach(elbowPin); | ||
| + | servoGripper.attach(gripperPin); | ||
| + | |||
| + | StartPosition(); | ||
| + | |||
| + | digitalWrite(pinLedRecord, | ||
| + | delay(1000); | ||
| + | digitalWrite(pinLedRecord, | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | |||
| + | buttonRecord = digitalRead(pinRecord); | ||
| + | buttonPlay = digitalRead(pinPlay); | ||
| + | |||
| + | // Serial.print(buttonRecord); | ||
| + | // Serial.print(" | ||
| + | // Serial.println(buttonPlay); | ||
| + | // for testing purposes | ||
| + | |||
| + | if (buttonPlay == LOW) | ||
| + | { | ||
| + | buttonPlayCount++; | ||
| + | |||
| + | if (buttonPlayCount >= buttonPlayDelay) | ||
| + | { | ||
| + | repeatePlaying = true; | ||
| + | } | ||
| + | } | ||
| + | else buttonPlayCount = 0; | ||
| + | |||
| + | if (buttonPlay != buttonPlayLast) | ||
| + | { | ||
| + | if (record) | ||
| + | { | ||
| + | record = false; | ||
| + | } | ||
| + | |||
| + | if (buttonPlay == LOW) | ||
| + | { | ||
| + | play = !play; | ||
| + | repeatePlaying = false; | ||
| + | |||
| + | if (play) | ||
| + | { | ||
| + | StartPosition(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | if (buttonRecord != buttonRecordLast) | ||
| + | { | ||
| + | if (buttonRecord == LOW) | ||
| + | { | ||
| + | record = !record; | ||
| + | |||
| + | if (record) | ||
| + | { | ||
| + | play = false; | ||
| + | repeatePlaying = false; | ||
| + | recPos = 0; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | if (debug) PrintBuffer(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | buttonPlayLast = buttonPlay; | ||
| + | buttonRecordLast = buttonRecord; | ||
| + | |||
| + | float dx = map(analogRead(xdirPin), | ||
| + | float dy = map(analogRead(ydirPin), | ||
| + | float dz = map(analogRead(zdirPin), | ||
| + | float dg = map(analogRead(gdirPin), | ||
| + | |||
| + | if (abs(dx) | ||
| + | if (abs(dy) < 1.5) dy = 0; | ||
| + | if (abs(dz) < 1.5) dz = 0; | ||
| + | if (abs(dg) < 1.5) dg = 0; | ||
| + | |||
| + | posBase += dx; | ||
| + | posShoulder += dy; | ||
| + | posElbow += dz; | ||
| + | posGripper += dg; | ||
| + | |||
| + | if (play) | ||
| + | { | ||
| + | if (playPos | ||
| + | playPos = 0; | ||
| + | |||
| + | if (repeatePlaying) | ||
| + | { | ||
| + | delay(delayBetweenCycles); | ||
| + | StartPosition(); | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | play = false; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | bool endOfData = false; | ||
| + | |||
| + | while (!endOfData) | ||
| + | { | ||
| + | if (playPos >= buffSize - 1) break; | ||
| + | if (playPos >= recPos) break; | ||
| + | |||
| + | int data = buff[playPos]; | ||
| + | int angle = data & 0xFFF; | ||
| + | int servoNumber = data & 0x3000; | ||
| + | endOfData | ||
| + | |||
| + | switch (servoNumber) | ||
| + | { | ||
| + | case 0x0000: | ||
| + | posBase = angle; | ||
| + | break; | ||
| + | |||
| + | case 0x1000: | ||
| + | posShoulder | ||
| + | break; | ||
| + | |||
| + | case 0x2000: | ||
| + | posElbow = angle; | ||
| + | break; | ||
| + | |||
| + | case 0x3000: | ||
| + | posGripper = angle; | ||
| + | dg = posGripper - lastGripper; | ||
| + | break; | ||
| + | } | ||
| + | |||
| + | playPos++; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | if (posBase > maxBase) posBase = maxBase; | ||
| + | if (posShoulder > maxShoulder) posShoulder = maxShoulder; | ||
| + | if (posElbow > maxElbow) posElbow = maxElbow; | ||
| + | if (posGripper > maxGripper) posGripper = maxGripper; | ||
| + | |||
| + | if (posBase < minBase) posBase = minBase; | ||
| + | if (posShoulder < minShoulder) | ||
| + | if (posElbow < minElbow) posElbow = minElbow; | ||
| + | if (posGripper < minGripper) posGripper = minGripper; | ||
| + | |||
| + | servoBase.write(posBase); | ||
| + | servoShoulder.write(posShoulder); | ||
| + | servoElbow.write(posElbow); | ||
| + | |||
| + | bool waitGripper = false; | ||
| + | if (dg < 0) { | ||
| + | posGripper = minGripper; | ||
| + | waitGripper | ||
| + | } | ||
| + | else if (dg > 0) { | ||
| + | posGripper = maxGripper; | ||
| + | waitGripper | ||
| + | } | ||
| + | |||
| + | servoGripper.write(posGripper); | ||
| + | if (play && waitGripper) | ||
| + | { | ||
| + | delay(1000); | ||
| + | } | ||
| + | |||
| + | if ((lastBase != posBase) | (lastShoulder | ||
| + | { | ||
| + | if (record) | ||
| + | { | ||
| + | if (recPos < buffSize - countServo) | ||
| + | { | ||
| + | int buffPos = 0; | ||
| + | |||
| + | if (lastBase != posBase) | ||
| + | { | ||
| + | buffAdd[buffPos] = posBase; | ||
| + | buffPos++; | ||
| + | } | ||
| + | |||
| + | if (lastShoulder != posShoulder) | ||
| + | { | ||
| + | buffAdd[buffPos] | ||
| + | buffPos++; | ||
| + | } | ||
| + | |||
| + | if (lastElbow | ||
| + | { | ||
| + | buffAdd[buffPos] = posElbow | 0x2000; | ||
| + | buffPos++; | ||
| + | } | ||
| + | |||
| + | if (lastGripper != posGripper) | ||
| + | { | ||
| + | buffAdd[buffPos] = posGripper | 0x3000; | ||
| + | buffPos++; | ||
| + | } | ||
| + | |||
| + | buffAdd[buffPos - 1] = buffAdd[buffPos - 1] | 0x4000; | ||
| + | |||
| + | for (int i = 0; i < buffPos; i++) | ||
| + | { | ||
| + | buff[recPos | ||
| + | } | ||
| + | |||
| + | recPos += buffPos; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | command = " | ||
| + | printPos = 0; | ||
| + | |||
| + | if (play) | ||
| + | { | ||
| + | command = " | ||
| + | printPos = playPos; | ||
| + | } | ||
| + | else if (record) | ||
| + | { | ||
| + | command = " | ||
| + | printPos = recPos; | ||
| + | } | ||
| + | |||
| + | Serial.print(command); | ||
| + | Serial.print(" | ||
| + | Serial.print(printPos); | ||
| + | Serial.print(" | ||
| + | Serial.print(posBase); | ||
| + | Serial.print(" | ||
| + | Serial.print(posShoulder); | ||
| + | Serial.print(" | ||
| + | Serial.print(posElbow); | ||
| + | Serial.print(" | ||
| + | Serial.print(posGripper); | ||
| + | Serial.print(" | ||
| + | Serial.print(record); | ||
| + | Serial.print(" | ||
| + | Serial.print(play); | ||
| + | Serial.println(); | ||
| + | } | ||
| + | |||
| + | lastBase = posBase; | ||
| + | lastShoulder = posShoulder; | ||
| + | lastElbow = posElbow; | ||
| + | lastGripper = posGripper; | ||
| + | |||
| + | if ( repeatePlaying) | ||
| + | { | ||
| + | ledLight = !ledLight; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | if (ledLight) | ||
| + | { | ||
| + | ledLight = false; | ||
| + | } | ||
| + | |||
| + | if (record) | ||
| + | { | ||
| + | ledLight | ||
| + | } | ||
| + | }; | ||
| + | |||
| + | digitalWrite(pinLedRecord, | ||
| + | delay(50); | ||
| + | } | ||
| + | |||
| + | void PrintBuffer() | ||
| + | { | ||
| + | for (int i = 0; i < recPos; i++) | ||
| + | { | ||
| + | int data = buff[i]; | ||
| + | int angle = data & 0xFFF; | ||
| + | int servoNumber = data & 0x3000; | ||
| + | bool endOfData = data & 0x4000; | ||
| + | |||
| + | Serial.print(" | ||
| + | Serial.print(servoNumber); | ||
| + | Serial.print(" | ||
| + | Serial.print(angle); | ||
| + | Serial.print(" | ||
| + | Serial.print(endOfData); | ||
| + | Serial.print(" | ||
| + | Serial.print(data, | ||
| + | Serial.println(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | void StartPosition() | ||
| + | { | ||
| + | int angleBase = servoBase.read(); | ||
| + | int angleShoulder = servoShoulder.read(); | ||
| + | int angleElbow = servoElbow.read(); | ||
| + | int angleGripper = servoGripper.read(); | ||
| + | |||
| + | Serial.print(angleBase); | ||
| + | Serial.print(" | ||
| + | Serial.print(angleShoulder); | ||
| + | Serial.print(" | ||
| + | Serial.print(angleElbow); | ||
| + | Serial.print(" | ||
| + | Serial.print(angleGripper); | ||
| + | Serial.println(" | ||
| + | |||
| + | posBase = startBase; | ||
| + | posShoulder = startShoulder; | ||
| + | posElbow = startElbow; | ||
| + | posGripper = startGripper; | ||
| + | |||
| + | servoBase.write(posBase); | ||
| + | servoShoulder.write(posShoulder); | ||
| + | servoElbow.write(posElbow); | ||
| + | servoGripper.write(posGripper); | ||
| + | } | ||
| + | </ | ||
/home/chanteri/www/fablab37110/data/attic/start/arduino/joystick.1712048273.txt.gz · Dernière modification : de gerardadmin
