Hello,
I am on a project that involves stepper motors. I send commands to the serial port through LabVIEW. My commands are :
-Go to position (moteur X1)
-Go to position (moteur Y1)
-Home (moteur X1)
-Home (moteur Y1)
All the controls work correctly except for the Y1 engine home. The problematic part of the code is:
//Moteur Y1 :
//Case 1 : Go To Position
//Case 2 : Home
switch (State_Y1)
{
case 1 : // Go To Position
stepper_Y1.run();
if (is_run_Y1==0)
{
State_Y1 = 0;
}
break;
case 2 : // Home
stepper_Y1.run();
Val_FinDeCourseY1 = digitalRead(FinDeCourseY1);
if (Val_FinDeCourseY1==0)
{
stepper_Y1.stop();
stepper_Y1.setCurrentPosition(0);
State_Y1 = 0;
}
break;
}
In fact, the motor Y1 goes directly into the condition (Val_FinDeCourseY1 == 0), it turns one step (approximately), stops and takes this new position as position 0.
I have done unit tests on the sensors and they are working fine.
The code worked fine on an arduino board but I changed to a tensy which has more computing power.
Thing noted, when I set a delay of 1ms after reading the limit switch Y1, the command works but it slowed down with the 1ms delay ...
I have been on the problem for several hours and cannot see my error.
The code in its entirety:
Thank you
/****************************************************************************************
** This is example LINX firmware for use with the PJRC Teensy 4.0 with the serial
** interface enabled.
**
** For more information see: www.labviewmakerhub.com/linx
** For support visit the forums at: www.labviewmakerhub.com/forums/linx
**
** Written By Sam Kristoff
**
** BSD2 License.
****************************************************************************************/
//Include All Peripheral Libraries Used By LINX
#include <SPI.h>
#include <Wire.h>
#include <EEPROM.h>
#include <Servo.h>
#include <stdlib.h>
#include <AccelStepper.h>
#include <LinxPjrcTeensy40.h> //Include Device Specific Header From Sketch>>Import Library (In This Case LinxChipkitMax32.h)
#include <LinxSerialListener.h> //Also Include Desired LINX Listener From Sketch>>Import Library (In This Case LinxSerialListener.h)
AccelStepper stepper_Y1 = AccelStepper( 1, 11, 12); // Déclaration de la fonction "AccelStepper" pour le moteur Y1
AccelStepper stepper_X1 = AccelStepper( 1, 8, 9); // Déclaration de la fonction "AccelStepper" pour le moteur X1
int Motor_Y1(); //Initialisation de la fonction qui permet de bouger le moteur Y1 d'une position à une autre
int Motor_X1(); //Initialisation de la fonction qui permet de bouger le moteur X1 d'une position à une autre
int Motor_Y1_Home(); //Initialisation de la fonction qui permet de faire le home du moteur Y1
int Motor_X1_Home(); //Initialisation de la fonction qui permet de faire le home du moteur X1
byte State_Y1 = 0; //Etat du moteur Y1 (0=idle, 1=GoToPosition, 2=Home)
byte State_X1 = 0; //Etat du moteur X1 (0=idle, 1=GoToPosition, 2=Home)
int FinDeCourseY1 = 7; //Assignation de la broche 79 (fin de course Y1 à la constante "FinDeCourseY1")
int FinDeCourseX1 = 3; //Assignation de la broche 78 (fin de course X1 à la constante "FinDeCourseX1")
bool Val_FinDeCourseY1; //Déclaration du booleen Val_FinDeCourseY1
bool Val_FinDeCourseX1; //Déclaration du booleen Val_FinDeCourseX1
LinxPjrcTeensy40* LinxDevice; //Create A Pointer To The LINX Device Object We Instantiate In Setup()
//Initialize LINX Device And Listener
void setup()
{
//Instantiate The LINX Device
LinxDevice = new LinxPjrcTeensy40();
//The LINXT Listener Is Pre Instantiated, Call Start And Pass A Pointer To The LINX Device And The UART Channel To Listen On
LinxSerialConnection.Start(LinxDevice, 0);
//LINX custom command
LinxSerialConnection.AttachCustomCommand(0, Motor_Y1); //Custom command numero 0 (Fonction GotoPosition for Y1 motor)
LinxSerialConnection.AttachCustomCommand(1, Motor_X1); //Custom command numero 1 (Fonction GotoPosition for X1 motor)
LinxSerialConnection.AttachCustomCommand(2, Motor_Y1_Home); //Custom command numero 2 (Fonction Home for Y1 motor)
LinxSerialConnection.AttachCustomCommand(3, Motor_X1_Home); //Custom command numero 3 (Fonction Home for X1 motor)
pinMode(FinDeCourseY1, INPUT); // Assignation de FinDeCourseY1 comme étant une entrée
pinMode(FinDeCourseX1, INPUT); // Assignation de FinDeCourseX1 comme étant une entrée
stepper_Y1.setMinPulseWidth(15);
stepper_X1.setMinPulseWidth(15);
}
void loop()
{
//Listen For New Packets From LabVIEW
LinxSerialConnection.CheckForCommands();
bool is_run_X1 = stepper_X1.isRunning(); //Permet de savoir si le moteur X1 est en mouvement
bool is_run_Y1 = stepper_Y1.isRunning(); //Permet de savoir si le moteur Y1 est en mouvemen
//Moteur X1 :
//Case 1 : Go To Position
//Case 2 : Home
switch (State_X1)
{
case 1 : // Go To Position
stepper_X1.run();
if (is_run_X1==0)
{
State_X1 = 0;
}
break;
case 2 : // Home
stepper_X1.run();
Val_FinDeCourseX1 = digitalRead(FinDeCourseX1);
if (Val_FinDeCourseX1==0)
{
stepper_X1.stop();
stepper_X1.setCurrentPosition(0);
State_X1 = 0;
}
break;
}
//Moteur Y1 :
//Case 1 : Go To Position
//Case 2 : Home
switch (State_Y1)
{
case 1 : // Go To Position
stepper_Y1.run();
if (is_run_Y1==0)
{
State_Y1 = 0;
}
break;
case 2 : // Home
stepper_Y1.run();
Val_FinDeCourseY1 = digitalRead(FinDeCourseY1);
if (Val_FinDeCourseY1==0)
{
stepper_Y1.stop();
stepper_Y1.setCurrentPosition(0);
State_Y1 = 0;
}
break;
}
}
int Motor_Y1(unsigned char numInputBytes, unsigned char* input, unsigned char* numResponseBytes, unsigned char* response) // Moteur Y1 Commande Go To Position
{
//Convertion of the char Array input to unsigned interger (Step to move)
unsigned int Step_To_Move_Y1 = atoi((char*)input);
// Set the maximum speed and acceleration:
stepper_Y1.setMaxSpeed(18000);
stepper_Y1.setAcceleration(3500);
// Set the target position:
stepper_Y1.moveTo(-Step_To_Move_Y1);
//Change l'etat du moteur Y1 à 1 (GoToPosition)
State_Y1 = 1;
return 0;
}
int Motor_X1(unsigned char numInputBytes, unsigned char* input, unsigned char* numResponseBytes, unsigned char* response) // Moteur X1 Commande Go To Position
{
//Convertion of the char Array input to unsigned interger (Step to move)
unsigned int Step_To_Move_X1 = atoi((char*)input);
// Set the maximum speed and acceleration:
stepper_X1.setMaxSpeed(3500);
stepper_X1.setAcceleration(1500);
// Set the target position:
stepper_X1.moveTo(Step_To_Move_X1);
//Change l'etat du moteur X1 à 1 (GoToPosition)
State_X1 = 1;
return 0;
}
int Motor_Y1_Home(unsigned char numInputBytes, unsigned char* input, unsigned char* numResponseBytes, unsigned char* response) // Moteur Y1 Commande Home
{
// Set the maximum speed and acceleration:
stepper_Y1.setMaxSpeed(1000);
stepper_Y1.setAcceleration(500);
// Set the target position:
stepper_Y1.moveTo(50000);
//Change l'etat du moteur Y1 à 2 (Home)
State_Y1 = 2;
return 0;
}
int Motor_X1_Home(unsigned char numInputBytes, unsigned char* input, unsigned char* numResponseBytes, unsigned char* response) // Moteur X1 Commande Home
{
// Set the maximum speed and acceleration:
stepper_X1.setMaxSpeed(1000);
stepper_X1.setAcceleration(500);
// Set the target position:
stepper_X1.moveTo(-25000);
//Change l'etat du moteur X1 à 2 (Home)
State_X1 = 2;
return 0;
}