Go to the documentation of this file.
76 SPCR1 = (1<<SPE1)|(1<<MSTR1);
97 uint8_t noninverted = 0;
105 startAngle -= distance/2.0;
120 startAngle += distance/2.0;
135 startAngle -= distance/2.0;
149 if(inverted > noninverted)
156 uint16_t stepsPerRevolution,
198 digitalWrite(2,HIGH);
199 digitalWrite(3,HIGH);
200 digitalWrite(4,HIGH);
211 tempSettings.
invert = invert;
290 steps = (int32_t)( (abs(diff) *
angleToStep) + 0.5);
341 return ( stats >> 13 );
347 registerContent &= ~(3UL << 20);
376 uint32_t velocity = abs(velocityDir);
387 SPCR1 &= ~(1<<CPHA1);
402 while(!( SPSR1 & (1 << SPIF1) ));
444 if( current <= 100.0 && current >= 0.0){
458 if( current <= 100.0 && current >= 0.0){
686 static float integral;
687 static bool integralReset = 0;
688 static float errorOld, differential = 0.0;
692 u = error*this->
pTerm;
709 integral += error*this->
iTerm;
711 if(integral > 200000.0)
715 else if(integral < -200000.0)
717 integral = -200000.0;
720 if(error > -10 && error < 10)
736 differential += 0.1*((error - errorOld)*this->
dTerm);
773 if(cmd->charAt(2) ==
';')
775 Serial.println(
"COMMAND NOT ACCEPTED");
784 if(cmd->substring(0,2) == String(
"P="))
788 if(cmd->charAt(i) >=
'0' && cmd->charAt(i) <=
'9')
790 value.concat(cmd->charAt(i));
792 else if(cmd->charAt(i) ==
'.')
794 value.concat(cmd->charAt(i));
798 else if(cmd->charAt(i) ==
';')
804 Serial.println(
"COMMAND NOT ACCEPTED");
811 if(cmd->charAt(i) >=
'0' && cmd->charAt(i) <=
'9')
813 value.concat(cmd->charAt(i));
815 else if(cmd->charAt(i) ==
';')
817 Serial.print(
"COMMAND ACCEPTED. P = ");
818 Serial.println(value.toFloat(),4);
826 Serial.println(
"COMMAND NOT ACCEPTED");
836 else if(cmd->substring(0,2) == String(
"I="))
840 if(cmd->charAt(i) >=
'0' && cmd->charAt(i) <=
'9')
842 value.concat(cmd->charAt(i));
844 else if(cmd->charAt(i) ==
'.')
846 value.concat(cmd->charAt(i));
850 else if(cmd->charAt(i) ==
';')
856 Serial.println(
"COMMAND NOT ACCEPTED");
863 if(cmd->charAt(i) >=
'0' && cmd->charAt(i) <=
'9')
865 value.concat(cmd->charAt(i));
867 else if(cmd->charAt(i) ==
';')
869 Serial.print(
"COMMAND ACCEPTED. I = ");
870 Serial.println(value.toFloat(),4);
878 Serial.println(
"COMMAND NOT ACCEPTED");
888 else if(cmd->substring(0,2) == String(
"D="))
892 if(cmd->charAt(i) >=
'0' && cmd->charAt(i) <=
'9')
894 value.concat(cmd->charAt(i));
896 else if(cmd->charAt(i) ==
'.')
898 value.concat(cmd->charAt(i));
902 else if(cmd->charAt(i) ==
';')
908 Serial.println(
"COMMAND NOT ACCEPTED");
915 if(cmd->charAt(i) >=
'0' && cmd->charAt(i) <=
'9')
917 value.concat(cmd->charAt(i));
919 else if(cmd->charAt(i) ==
';')
921 Serial.print(
"COMMAND ACCEPTED. D = ");
922 Serial.println(value.toFloat(),4);
930 Serial.println(
"COMMAND NOT ACCEPTED");
940 else if(cmd->substring(0,6) == String(
"invert"))
942 if(cmd->charAt(6) !=
';')
944 Serial.println(
"COMMAND NOT ACCEPTED");
949 Serial.println(F(
"Direction normal!"));
957 Serial.println(F(
"Direction inverted!"));
969 else if(cmd->substring(0,5) == String(
"error"))
971 if(cmd->charAt(5) !=
';')
973 Serial.println(
"COMMAND NOT ACCEPTED");
976 Serial.print(F(
"Current Error: "));
978 Serial.println(F(
" Steps"));
985 else if(cmd->substring(0,7) == String(
"current"))
987 if(cmd->charAt(7) !=
';')
989 Serial.println(
"COMMAND NOT ACCEPTED");
992 Serial.print(F(
"Run Current: "));
994 Serial.println(F(
" %"));
995 Serial.print(F(
"Hold Current: "));
997 Serial.println(F(
" %"));
1004 else if(cmd->substring(0,10) == String(
"parameters"))
1006 if(cmd->charAt(10) !=
';')
1008 Serial.println(
"COMMAND NOT ACCEPTED");
1011 Serial.print(F(
"P: "));
1013 Serial.print(F(
", "));
1014 Serial.print(F(
"I: "));
1016 Serial.print(F(
", "));
1017 Serial.print(F(
"D: "));
1025 else if(cmd->substring(0,4) == String(
"help"))
1027 if(cmd->charAt(4) !=
';')
1029 Serial.println(
"COMMAND NOT ACCEPTED");
1039 else if(cmd->substring(0,11) == String(
"runCurrent="))
1043 if(cmd->charAt(i) >=
'0' && cmd->charAt(i) <=
'9')
1045 value.concat(cmd->charAt(i));
1047 else if(cmd->charAt(i) ==
'.')
1049 value.concat(cmd->charAt(i));
1053 else if(cmd->charAt(i) ==
';')
1059 Serial.println(
"COMMAND NOT ACCEPTED");
1066 if(cmd->charAt(i) >=
'0' && cmd->charAt(i) <=
'9')
1068 value.concat(cmd->charAt(i));
1070 else if(cmd->charAt(i) ==
';')
1072 if(value.toFloat() >= 0.0 && value.toFloat() <= 100.0)
1074 i = (uint8_t)value.toFloat();
1079 Serial.println(
"COMMAND NOT ACCEPTED");
1085 Serial.println(
"COMMAND NOT ACCEPTED");
1090 Serial.print(
"COMMAND ACCEPTED. runCurrent = ");
1092 Serial.println(F(
" %"));
1102 else if(cmd->substring(0,12) == String(
"holdCurrent="))
1106 if(cmd->charAt(i) >=
'0' && cmd->charAt(i) <=
'9')
1108 value.concat(cmd->charAt(i));
1110 else if(cmd->charAt(i) ==
'.')
1112 value.concat(cmd->charAt(i));
1116 else if(cmd->charAt(i) ==
';')
1122 Serial.println(
"COMMAND NOT ACCEPTED");
1129 if(cmd->charAt(i) >=
'0' && cmd->charAt(i) <=
'9')
1131 value.concat(cmd->charAt(i));
1133 else if(cmd->charAt(i) ==
';')
1135 if(value.toFloat() >= 0.0 && value.toFloat() <= 100.0)
1137 i = (uint8_t)value.toFloat();
1142 Serial.println(
"COMMAND NOT ACCEPTED");
1148 Serial.println(
"COMMAND NOT ACCEPTED");
1153 Serial.print(
"COMMAND ACCEPTED. holdCurrent = ");
1155 Serial.println(F(
" %"));
1167 Serial.println(
"COMMAND NOT ACCEPTED");
1175 static String stringInput;
1176 static uint32_t t = millis();
1180 while(!Serial.available())
1183 if((millis() - t) >= 500)
1185 stringInput.remove(0);
1190 stringInput += (char)Serial.read();
1191 if(stringInput.lastIndexOf(
';') > -1)
1194 stringInput.remove(0);
1201 Serial.println(F(
"uStepper S Dropin !"));
1202 Serial.println(F(
""));
1203 Serial.println(F(
"Usage:"));
1204 Serial.println(F(
"Show this command list: 'help;'"));
1205 Serial.println(F(
"Get PID Parameters: 'parameters;'"));
1206 Serial.println(F(
"Set Proportional constant: 'P=10.002;'"));
1207 Serial.println(F(
"Set Integral constant: 'I=10.002;'"));
1208 Serial.println(F(
"Set Differential constant: 'D=10.002;'"));
1209 Serial.println(F(
"Invert Direction: 'invert;'"));
1210 Serial.println(F(
"Get Current PID Error: 'error;'"));
1211 Serial.println(F(
"Get Run/Hold Current Settings: 'current;'"));
1212 Serial.println(F(
"Set Run Current (percent): 'runCurrent=50.0;'"));
1213 Serial.println(F(
"Set Hold Current (percent): 'holdCurrent=50.0;'"));
1214 Serial.println(F(
""));
1215 Serial.println(F(
""));
1222 EEPROM.get(0,tempSettings);
1249 uint8_t checksum = 0xAA;
1250 uint8_t *p = (uint8_t*)settings;
1252 for(i=0; i < 15; i++)
volatile posFilter_t externalStepInputFilter
Prototype of class for accessing all features of the uStepper S in a single object.
void init(uStepperS *_pointer)
Initiation of the motor driver.
void disableStallguard(void)
void moveSteps(int32_t steps)
Make the motor perform a predefined number of steps.
void enableStallguard(int8_t threshold=4, bool stopOnStall=false)
Enable TMC5130 StallGuard.
bool getMotorState(uint8_t statusType=POSITION_REACHED)
Get the current motor driver state.
void setAcceleration(uint32_t acceleration)
Set motor acceleration.
int32_t readRegister(uint8_t address)
Reads a register from the motor driver.
void setBrakeMode(uint8_t mode, float brakeCurrent=25.0)
void readMotorStatus(void)
volatile int32_t angleMoved
float getAngleMoved(bool filtered=true)
Returns the angle moved from reference position in degrees.
Struct to store dropin settings.
void setVelocity(uint32_t velocity)
Set motor velocity.
void setSPIMode(uint8_t mode)
uint16_t captureAngle(void)
Capture the current shaft angle.
#define ACCELERATIONCONVERSION
void disableStallguard(void)
Disables the builtin stallguard offered from TMC5130, and reenables StealthChop.
void runContinous(bool dir)
Make the motor rotate continuously.
void disablePid(void)
This method disables the PID until calling enablePid.
bool isStalled(void)
This method returns a bool variable indicating wether the motor is stalled or not....
float stepsPerSecondToRPM
void dropinCli()
This method is used to tune Drop-in parameters. After tuning uStepper S, the parameters are saved in ...
volatile posFilter_t encoderFilter
volatile float controlThreshold
void stop(bool mode=HARD)
Stop the motor.
void updateCurrent(void)
Writes the current setting registers of the motor driver
void filterSpeedPos(posFilter_t *filter, int32_t steps)
float getPidError(void)
This method returns the current PID error.
#define ENCODERDATATOSTEP
void setRPM(float rpm)
Set the velocity in rpm.
void disableClosedLoop(void)
This method disables the closed loop mode until calling enableClosedLoop.
volatile bool pidDisabled
void parseCommand(String *cmd)
This method is used for the dropinCli to take in user commands.
float moveToEnd(bool dir, float rpm=40.0, int8_t threshold=4)
Moves the motor to its physical limit, without limit switch.
void setIntegral(float I)
This method is used to change the PID integral parameter I.
float RPMToStepsPerSecond
void clearStall(void)
Clear the stallguard, reenabling the motor to return to its previous operation.
void init(void)
Internal function to prepare the uStepperS in the constructor.
bool loadDropinSettings(void)
void setPosition(int32_t position)
Set the motor position.
void setDifferential(float D)
This method is used to change the PID differential parameter D.
void interrupt1(void)
Used by dropin feature to take in enable signal.
void setMaxVelocity(float velocity)
Set the maximum velocity of the stepper motor.
uint8_t dropinSettingsCalcChecksum(dropinCliSettings_t *settings)
void enableClosedLoop(void)
This method reenables the closed loop mode after being disabled.
void setup(uint8_t mode=NORMAL, uint16_t stepsPerRevolution=200, float pTerm=10.0, float iTerm=0.0, float dTerm=0.0, uint16_t dropinStepSize=16, bool setHome=true, uint8_t invert=0, uint8_t runCurrent=50, uint8_t holdCurrent=30)
Initializes the different parts of the uStepper S object.
friend void interrupt0(void)
Used by dropin feature to take in step pulses.
void init(uStepperS *_pointer)
Initiation of the encoder.
void enableStallguard(int8_t threshold, bool stopOnStall)
int32_t getPosition(void)
Returns the current position of the motor driver.
void saveDropinSettings(void)
void setMaxAcceleration(float acceleration)
Set the maximum acceleration of the stepper motor.
float angleMoved(void)
Get the angle moved from reference position in degrees.
#define VELOCITYCONVERSION
void invertDropinDir(bool invert)
This method is used to invert the drop-in direction pin interpretation.
void moveAngle(float angle)
Makes the motor rotate a specific angle relative to the current position.
void setHome(float initialAngle=0)
Define new reference(home) position.
dropinCliSettings_t dropinSettings
uint8_t SPI(uint8_t data)
Struct for encoder velocity estimator.
void setDirection(bool direction)
void checkOrientation(float distance=10)
This method is used to check the orientation of the motor connector.
#define VELOCITY_MODE_POS
void setHoldCurrent(double current)
Set motor hold current.
uStepperS()
Constructor of uStepper class.
void moveToAngle(float angle)
Makes the motor rotate a specific angle relative to the current position.
void dropinPrintHelp()
This method is used to print the dropinCli menu explainer:
void setControlThreshold(float threshold)
This method sets the control threshold for the closed loop position control in microsteps - i....
volatile float currentPidError
void setProportional(float P)
This method is used to change the PID proportional parameter P.
void enablePid(void)
This method reenables the PID after being disabled.
bool invertPidDropinDirection
void setShaftDirection(bool direction)
Set motor driver direction.
void setDeceleration(uint32_t deceleration)
Set motor deceleration.
void setMaxDeceleration(float deceleration)
Set the maximum deceleration of the stepper motor.
void TIMER1_COMPA_vect(void)
void setCurrent(double current)
Set motor output current.
void setRampMode(uint8_t mode)
Set motor driver to position mode or velocity mode.
int32_t writeRegister(uint8_t address, uint32_t datagram)
Write a register of the motor driver.
void interrupt0(void)
Used by dropin feature to take in step pulses.