uStepper S
uStepperS.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * File: uStepperS.h *
3 * Version: 2.0.0 *
4 * Date: March 30th, 2020 *
5 * Author: Thomas Hørring Olsen *
6 * *
7 *********************************************************************************************
8 * (C) 2020 *
9 * *
10 * uStepper ApS *
11 * www.ustepper.com *
12 * administration@ustepper.com *
13 * *
14 * The code contained in this file is released under the following open source license: *
15 * *
16 * Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International *
17 * *
18 * The code in this file is provided without warranty of any kind - use at own risk! *
19 * neither uStepper ApS nor the author, can be held responsible for any damage *
20 * caused by the use of the code contained in this file ! *
21 * *
22 ********************************************************************************************/
133 #ifndef _USTEPPER_S_H_
134 #define _USTEPPER_S_H_
135 
136 #ifndef ARDUINO_AVR_USTEPPER_S
137  #error !!This library only supports the uStepper S board!!
138 #endif
139 
140 #ifndef __AVR_ATmega328PB__
141  #error !!This library only supports the ATmega328PB MCU!!
142 #endif
143 
144 #include <avr/io.h>
145 #include <avr/interrupt.h>
146 #include <Arduino.h>
147 #include <EEPROM.h>
148 #include <inttypes.h>
149 #include <uStepperServo.h>
150 
151 #define FREEWHEELBRAKE 0
152 #define COOLBRAKE 1
153 #define HARDBRAKE 2
155 #define CW 1
156 #define CCW 0
158 #define POSITION_REACHED 0x20
159 #define VELOCITY_REACHED 0x10
160 #define STANDSTILL 0x08
161 #define STALLGUARD2 0x04
167 typedef union
168 {
169  float f;
170  uint8_t bytes[4];
171 }floatBytes_t;
172 
181 typedef struct
182 {
186  uint8_t invert;
187  uint8_t holdCurrent;
188  uint8_t runCurrent;
189  uint8_t checksum;
191 
197 typedef struct
198 {
199  float posError = 0.0;
200  float posEst = 0.0;
201  float velIntegrator = 0.0;
202  float velEst = 0.0;
203 }posFilter_t;
204 
205 
206 class uStepperS;
207 #include <uStepperEncoder.h>
208 #include <uStepperDriver.h>
209 
210 #define HARD 0
211 #define SOFT 1
213 #define DRV_ENN PD4
214 #define SD_MODE PD5
215 #define SPI_MODE PD6
217 #define CS_DRIVER PE2
218 #define CS_ENCODER PD7
220 #define MOSI1 PE3
221 #define MOSI_ENC PC2
222 #define MISO1 PC0
223 #define SCK1 PC1
226 #define NORMAL 0
227 #define DROPIN 1
228 #define CLOSEDLOOP 2
229 #define PID CLOSEDLOOP
231 #define CLOCKFREQ 16000000.0
237 #define ENCODERINTFREQ 1000
238 #define ENCODERINTPERIOD 1.0/ENCODERINTFREQ
239 #define PULSEFILTERKP 120.0
240 #define PULSEFILTERKI 1900.0*ENCODERINTPERIOD
247 extern "C" void TIMER1_COMPA_vect(void) __attribute__ ((signal,used));
248 
255 void interrupt0(void);
256 
263 void interrupt1(void);
264 
265 
273 class uStepperS
274 {
275 
276 friend class uStepperDriver;
277 friend class uStepperEncoder;
278 friend void interrupt0(void);
279 friend void TIMER1_COMPA_vect(void) __attribute__ ((signal,used));
280 public:
281 
284 
287 
291  uStepperS();
292 
296  uStepperS(float acceleration, float velocity);
297 
301  void init( void );
302 
338  void setup( uint8_t mode = NORMAL,
339  uint16_t stepsPerRevolution = 200,
340  float pTerm = 10.0,
341  float iTerm = 0.0,
342  float dTerm = 0.0,
343  uint16_t dropinStepSize = 16,
344  bool setHome = true,
345  uint8_t invert = 0,
346  uint8_t runCurrent = 50,
347  uint8_t holdCurrent = 30);
348 
357  void setRPM( float rpm );
358 
367  void setMaxAcceleration ( float acceleration );
368 
377  void setMaxDeceleration ( float deceleration );
378 
387  void setMaxVelocity ( float velocity );
388 
396  void setCurrent( double current );
397 
405  void setHoldCurrent( double current );
406 
421  void moveSteps( int32_t steps );
422 
438  void moveAngle( float angle );
439 
455  void moveToAngle( float angle );
456 
465  void runContinous( bool dir );
466 
472  float angleMoved( void );
473 
489  bool getMotorState(uint8_t statusType = POSITION_REACHED);
490 
501  void stop( bool mode = HARD );
502 
512  void enableStallguard( int8_t threshold = 4, bool stopOnStall = false);
513 
517  void disableStallguard( void );
518 
522  void clearStall( void );
523 
530  bool isStalled(void);
531 
532 
545  bool isStalled( int8_t threshold );
546 
559  void setBrakeMode( uint8_t mode, float brakeCurrent = 25.0 );
560 
565  void enablePid(void);
566 
571  void disablePid(void);
572 
577  void enableClosedLoop(void);
578 
583  void disableClosedLoop(void);
584 
589  void setControlThreshold(float threshold);
590 
606  float moveToEnd(bool dir, float rpm = 40.0, int8_t threshold = 4);
607 
612  float getPidError(void);
613 
620  void setProportional(float P);
621 
628  void setIntegral(float I);
629 
636  void setDifferential(float D);
637 
644  void invertDropinDir(bool invert);
645 
661  void dropinCli();
662 
669  void parseCommand(String *cmd);
670 
687  void dropinPrintHelp();
688 
696  void checkOrientation(float distance = 10);
697 
698 private:
699 
704  float maxVelocity;
705 
714  float angleToStep;
715 
716  uint16_t microSteps;
717  uint16_t fullSteps;
718  uint16_t dropinStepSize;
719 
720  int32_t stepCnt;
721 
723  float RPMToStepsPerSecond;
724 
726 
727  float currentPidSpeed;
730  volatile uint8_t mode;
731  float pTerm;
733  float iTerm;
734 
735  float dTerm;
736  bool brake;
737  volatile bool pidDisabled;
739  volatile float controlThreshold = 10;
742  volatile bool stall;
743  // SPI functions
744 
745  volatile int32_t pidPositionStepsIssued = 0;
746  volatile float currentPidError;
747 
749  int8_t stallThreshold = 4;
750 
752  bool stallStop = false;
753 
755  bool stallEnabled = false;
756 
757  uint8_t SPI( uint8_t data );
758 
759  void setSPIMode( uint8_t mode );
760 
761  void chipSelect( uint8_t pin , bool state );
762 
763  void filterSpeedPos(posFilter_t *filter, int32_t steps);
764 
765  float pid(float error);
766 
768  bool loadDropinSettings(void);
769  void saveDropinSettings(void);
771 };
772 
773 
774 
775 #endif
uStepperS::externalStepInputFilter
volatile posFilter_t externalStepInputFilter
Definition: uStepperS.h:724
uStepperS
Prototype of class for accessing all features of the uStepper S in a single object.
Definition: uStepperS.h:272
uStepperS::dropinStepSize
uint16_t dropinStepSize
Definition: uStepperS.h:717
uStepperDriver
Prototype of class for the TMC5130 Driver.
Definition: uStepperDriver.h:149
uStepperS::stall
volatile bool stall
Definition: uStepperS.h:741
uStepperS::moveSteps
void moveSteps(int32_t steps)
Make the motor perform a predefined number of steps.
Definition: uStepperS.cpp:251
uStepperS::enableStallguard
void enableStallguard(int8_t threshold=4, bool stopOnStall=false)
Enable TMC5130 StallGuard.
Definition: uStepperS.cpp:301
uStepperS::getMotorState
bool getMotorState(uint8_t statusType=POSITION_REACHED)
Get the current motor driver state.
Definition: uStepperS.cpp:82
uStepperEncoder::angle
volatile uint16_t angle
Definition: uStepperEncoder.h:207
uStepperS::stallEnabled
bool stallEnabled
Definition: uStepperS.h:754
uStepperS::setBrakeMode
void setBrakeMode(uint8_t mode, float brakeCurrent=25.0)
Definition: uStepperS.cpp:343
uStepperS::TIMER1_COMPA_vect
friend void TIMER1_COMPA_vect(void) __attribute__((signal
Interrupt routine for critical tasks.
Definition: uStepperS.cpp:574
uStepperS::chipSelect
void chipSelect(uint8_t pin, bool state)
uStepperS::dTerm
float dTerm
Definition: uStepperS.h:734
uStepperS::rpmToVelocity
float rpmToVelocity
Definition: uStepperS.h:712
dropinCliSettings_t
Struct to store dropin settings.
Definition: uStepperS.h:180
uStepperS::setSPIMode
void setSPIMode(uint8_t mode)
Definition: uStepperS.cpp:381
uStepperS::disableStallguard
void disableStallguard(void)
Disables the builtin stallguard offered from TMC5130, and reenables StealthChop.
Definition: uStepperS.cpp:312
uStepperS::runContinous
void runContinous(bool dir)
Make the motor rotate continuously.
Definition: uStepperS.cpp:468
floatBytes_t
Union to easily split a float into its binary representation.
Definition: uStepperS.h:166
uStepperS::disablePid
void disablePid(void)
This method disables the PID until calling enablePid.
Definition: uStepperS.cpp:628
uStepperS::fullSteps
uint16_t fullSteps
Definition: uStepperS.h:716
uStepperS::pTerm
float pTerm
Definition: uStepperS.h:730
uStepperS::maxAcceleration
float maxAcceleration
Definition: uStepperS.h:709
uStepperS::brake
bool brake
Definition: uStepperS.h:735
uStepperS::isStalled
bool isStalled(void)
This method returns a bool variable indicating wether the motor is stalled or not....
Definition: uStepperS.cpp:324
uStepperS::stepsPerSecondToRPM
float stepsPerSecondToRPM
Definition: uStepperS.h:721
uStepperS::dropinCli
void dropinCli()
This method is used to tune Drop-in parameters. After tuning uStepper S, the parameters are saved in ...
Definition: uStepperS.cpp:1172
uStepperDriver.h
Function prototypes and definitions for the TMC5130 motor driver.
uStepperS::stepCnt
int32_t stepCnt
Definition: uStepperS.h:719
uStepperS::controlThreshold
volatile float controlThreshold
Definition: uStepperS.h:738
uStepperS::stop
void stop(bool mode=HARD)
Stop the motor.
Definition: uStepperS.cpp:486
uStepperS::encoder
uStepperEncoder encoder
Definition: uStepperS.h:285
interrupt1
void interrupt1(void)
Used by dropin feature to take in enable signal.
Definition: uStepperS.cpp:527
uStepperS::filterSpeedPos
void filterSpeedPos(posFilter_t *filter, int32_t steps)
Definition: uStepperS.cpp:503
uStepperS::getPidError
float getPidError(void)
This method returns the current PID error.
Definition: uStepperS.cpp:676
NORMAL
#define NORMAL
Definition: uStepperS.h:225
uStepperS::setRPM
void setRPM(float rpm)
Set the velocity in rpm.
Definition: uStepperS.cpp:364
interrupt0
void interrupt0(void)
Used by dropin feature to take in step pulses.
Definition: uStepperS.cpp:539
uStepperS::disableClosedLoop
void disableClosedLoop(void)
This method disables the closed loop mode until calling enableClosedLoop.
Definition: uStepperS.cpp:640
uStepperS::microSteps
uint16_t microSteps
Definition: uStepperS.h:715
uStepperS::iTerm
float iTerm
Definition: uStepperS.h:732
uStepperS::pidDisabled
volatile bool pidDisabled
Definition: uStepperS.h:736
uStepperS::parseCommand
void parseCommand(String *cmd)
This method is used for the dropinCli to take in user commands.
Definition: uStepperS.cpp:767
uStepperS::moveToEnd
float moveToEnd(bool dir, float rpm=40.0, int8_t threshold=4)
Moves the motor to its physical limit, without limit switch.
Definition: uStepperS.cpp:645
uStepperS::setIntegral
void setIntegral(float I)
This method is used to change the PID integral parameter I.
Definition: uStepperS.cpp:752
uStepperS::RPMToStepsPerSecond
float RPMToStepsPerSecond
Definition: uStepperS.h:722
uStepperS::clearStall
void clearStall(void)
Clear the stallguard, reenabling the motor to return to its previous operation.
Definition: uStepperS.cpp:319
uStepperServo.h
Function prototypes and definitions for the uStepper Servo library.
uStepperS::init
void init(void)
Internal function to prepare the uStepperS in the constructor.
Definition: uStepperS.cpp:57
uStepperS::loadDropinSettings
bool loadDropinSettings(void)
Definition: uStepperS.cpp:1217
uStepperS::setDifferential
void setDifferential(float D)
This method is used to change the PID differential parameter D.
Definition: uStepperS.cpp:757
uStepperS::setMaxVelocity
void setMaxVelocity(float velocity)
Set the maximum velocity of the stepper motor.
Definition: uStepperS.cpp:407
uStepperS::dropinSettingsCalcChecksum
uint8_t dropinSettingsCalcChecksum(dropinCliSettings_t *settings)
Definition: uStepperS.cpp:1245
uStepperS::enableClosedLoop
void enableClosedLoop(void)
This method reenables the closed loop mode after being disabled.
Definition: uStepperS.cpp:635
uStepperS::setup
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.
Definition: uStepperS.cpp:154
uStepperS::interrupt0
friend void interrupt0(void)
Used by dropin feature to take in step pulses.
Definition: uStepperS.cpp:539
uStepperS::pid
float pid(float error)
Definition: uStepperS.cpp:681
uStepperS::mode
volatile uint8_t mode
Definition: uStepperS.h:729
uStepperS::angleToStep
float angleToStep
Definition: uStepperS.h:713
uStepperS::stallStop
bool stallStop
Definition: uStepperS.h:751
uStepperS::maxVelocity
float maxVelocity
Definition: uStepperS.h:703
uStepperEncoder
Prototype of class for the AEAT8800-Q24 encoder.
Definition: uStepperEncoder.h:49
uStepperS::saveDropinSettings
void saveDropinSettings(void)
Definition: uStepperS.cpp:1238
uStepperS::setMaxAcceleration
void setMaxAcceleration(float acceleration)
Set the maximum acceleration of the stepper motor.
Definition: uStepperS.cpp:418
uStepperS::angleMoved
float angleMoved(void)
Get the angle moved from reference position in degrees.
Definition: uStepperS.cpp:481
uStepperS::driver
uStepperDriver driver
Definition: uStepperS.h:282
uStepperS::invertDropinDir
void invertDropinDir(bool invert)
This method is used to invert the drop-in direction pin interpretation.
Definition: uStepperS.cpp:762
uStepperS::stallThreshold
int8_t stallThreshold
Definition: uStepperS.h:748
uStepperEncoder.h
Function prototypes and definitions for the AEAT8800-Q24 Encoder.
uStepperS::currentPidSpeed
float currentPidSpeed
Definition: uStepperS.h:726
uStepperS::moveAngle
void moveAngle(float angle)
Makes the motor rotate a specific angle relative to the current position.
Definition: uStepperS.cpp:266
uStepperEncoder::setHome
void setHome(float initialAngle=0)
Define new reference(home) position.
Definition: uStepperEncoder.cpp:80
uStepperS::dropinSettings
dropinCliSettings_t dropinSettings
Definition: uStepperS.h:766
uStepperS::SPI
uint8_t SPI(uint8_t data)
Definition: uStepperS.cpp:396
posFilter_t
Struct for encoder velocity estimator.
Definition: uStepperS.h:196
uStepperS::checkOrientation
void checkOrientation(float distance=10)
This method is used to check the orientation of the motor connector.
Definition: uStepperS.cpp:92
uStepperS::pidPositionStepsIssued
volatile int32_t pidPositionStepsIssued
Definition: uStepperS.h:744
uStepperS::setHoldCurrent
void setHoldCurrent(double current)
Set motor hold current.
Definition: uStepperS.cpp:454
uStepperS::uStepperS
uStepperS()
Constructor of uStepper class.
Definition: uStepperS.cpp:34
uStepperS::moveToAngle
void moveToAngle(float angle)
Makes the motor rotate a specific angle relative to the current position.
Definition: uStepperS.cpp:283
uStepperS::dropinPrintHelp
void dropinPrintHelp()
This method is used to print the dropinCli menu explainer:
Definition: uStepperS.cpp:1198
uStepperS::setControlThreshold
void setControlThreshold(float threshold)
This method sets the control threshold for the closed loop position control in microsteps - i....
Definition: uStepperS.cpp:617
uStepperS::currentPidError
volatile float currentPidError
Definition: uStepperS.h:745
POSITION_REACHED
#define POSITION_REACHED
Definition: uStepperS.h:157
uStepperS::used
friend void used
Definition: uStepperS.h:278
uStepperS::maxDeceleration
float maxDeceleration
Definition: uStepperS.h:710
uStepperS::setProportional
void setProportional(float P)
This method is used to change the PID proportional parameter P.
Definition: uStepperS.cpp:747
uStepperS::enablePid
void enablePid(void)
This method reenables the PID after being disabled.
Definition: uStepperS.cpp:621
uStepperS::invertPidDropinDirection
bool invertPidDropinDirection
Definition: uStepperS.h:711
HARD
#define HARD
Definition: uStepperS.h:209
uStepperS::setMaxDeceleration
void setMaxDeceleration(float deceleration)
Set the maximum deceleration of the stepper motor.
Definition: uStepperS.cpp:430
uStepperS::setCurrent
void setCurrent(double current)
Set motor output current.
Definition: uStepperS.cpp:441