Bonjour,
Je cherche à asservir en position un moteur pas à pas avec un encodeur rotatif optique afin de pouvoir enrouler un câble lorsqu'il passe sous une roue détectant son mouvement. Le système d'enroulement est actionné par un moteur pas à pas réducté et j'ai placé un encodeur sur la roue.
Je voudrais maintenant que lorsque la roue tourne, le moteur pas à pas tourne aussi.
J'ai pas mal recherché sur internet et ça dépasse un peu mes compétences actuelles car le programme utilise des interruptions pour lire l'encodeur, et je ne maitrise pas bien.
J'ai récupéré ce programme que j'ai un peu modifié mais il ne fonctionne pas (rien ne se passe) : des idées ? merci
#include <AccelStepper.h>
#include <MultiStepper.h>
//https://www.brainy-bits.com/stepper-motor-rotary-encoder-p1/
#include "Stepper.h"
#define STEPS 32 // Number of steps for one revolution of Internal shaft
// 2048 steps for one revolution of External shaft
volatile boolean TurnDetected; // need volatile for Interrupts
volatile boolean rotationdirection; // CW or CCW rotation
const int PinCLK = 2; // Generating interrupts using CLK signal
const int PinDT = 3; // Reading DT signal
//const int PinSW=4; // Reading Push Button switch
int RotaryPosition = 0; // To store Stepper Motor Position
int PrevPosition; // Previous Rotary position Value to check accuracy
int StepsToTake; // How much to move Stepper
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
//Stepper small_stepper(STEPS, 8, 10, 9, 11);
AccelStepper Moteur(AccelStepper::DRIVER, 7, 8); // step, dir
// Interrupt routine runs if CLK goes from HIGH to LOW
void isr () {
delay(4); // delay for Debouncing
if (digitalRead(PinCLK))
rotationdirection = digitalRead(PinDT);
else
rotationdirection = !digitalRead(PinDT);
TurnDetected = true;
}
void setup () {
pinMode(PinCLK, INPUT);
pinMode(PinDT, INPUT);
//pinMode(PinSW,INPUT);
//digitalWrite(PinSW, HIGH); // Pull-Up resistor for switch
attachInterrupt (0, isr, FALLING); // interrupt 0 always connected to pin 2 on Arduino UNO
digitalWrite(13, LOW); //Activation du drivers M1
}
void loop () {
Moteur.setSpeed(2000);
Moteur.setMaxSpeed(2000);
// if (!(digitalRead(PinSW))) { // check if button is pressed
// if (RotaryPosition == 0) { // check if button was already pressed
// } else {
// small_stepper.step(-(RotaryPosition*50));
// RotaryPosition=0; // Reset position to ZERO
// }
// }
// Runs if rotation was detected
if (TurnDetected) {
PrevPosition = RotaryPosition; // Save previous position in variable
if (rotationdirection) {
RotaryPosition = RotaryPosition - 1;
} // decrase Position by 1
else {
RotaryPosition = RotaryPosition + 1;
} // increase Position by 1
TurnDetected = false; // do NOT repeat IF loop until new rotation detected
// Which direction to move Stepper motor
if ((PrevPosition + 1) == RotaryPosition) { // Move motor CW
StepsToTake = 50;
// small_stepper.step(StepsToTake);
Moteur.move(StepsToTake);
Moteur.run();
}
if ((RotaryPosition + 1) == PrevPosition) { // Move motor CCW
StepsToTake = -50;
// small_stepper.step(StepsToTake);
Moteur.move(StepsToTake);
Moteur.run();
}
}
}