uStepper S
uStepperS.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * File: uStepperS.h *
3 * Version: 1.0.1 *
4 * Date: May 14th, 2019 *
5 * Author: Thomas Hørring Olsen *
6 * *
7 *********************************************************************************************
8 * (C) 2019 *
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 ********************************************************************************************/
119 #ifndef _USTEPPER_S_H_
120 #define _USTEPPER_S_H_
121 
122 #ifndef __AVR_ATmega328PB__
123  #error !!This library only supports the ATmega328PB MCU!!
124 #endif
125 #include <avr/io.h>
126 #include <avr/interrupt.h>
127 #include <Arduino.h>
128 #include <EEPROM.h>
129 #include <inttypes.h>
130 #include <uStepperServo.h>
131 #define CW 1
132 #define CCW 0
134 #define POSITION_REACHED 0x20
135 #define VELOCITY_REACHED 0x10
136 #define STANDSTILL 0x08
137 #define STALLGUARD2 0x04
143 typedef union
144 {
145  float f;
146  uint8_t bytes[4];
147 }floatBytes_t;
148 
157 typedef struct
158 {
162  uint8_t invert;
163  uint8_t holdCurrent;
164  uint8_t runCurrent;
165  uint8_t checksum;
167 
173 typedef struct
174 {
175  float posError = 0.0;
176  float posEst = 0.0;
177  float velIntegrator = 0.0;
178  float velEst = 0.0;
179 }posFilter_t;
180 
181 
182 class uStepperS;
183 #include <uStepperEncoder.h>
184 #include <uStepperDriver.h>
185 
186 #define HARD 0
187 #define SOFT 1
189 #define DRV_ENN PD4
190 #define SD_MODE PD5
191 #define SPI_MODE PD6
193 #define CS_DRIVER PE2
194 #define CS_ENCODER PD7
196 #define MOSI1 PE3
197 #define MOSI_ENC PC2
198 #define MISO1 PC0
199 #define SCK1 PC1
202 #define NORMAL 0
203 
204 #define DROPIN 1
205 
206 #define PID 2
207 
208 #define CLOCKFREQ 16000000.0
211 #define ENCODERINTFREQ 1000.0
212 
213 #define ENCODERINTPERIOD 1.0/ENCODERINTFREQ
214 
215 #define ENCODERSPEEDCONSTANT ENCODERINTFREQ/65535.0
216 
217 #define PULSEFILTERKP 120.0
218 
219 #define PULSEFILTERKI 1900.0*ENCODERINTPERIOD
220 
226 extern "C" void TIMER1_COMPA_vect(void) __attribute__ ((signal,used));
227 
234 void interrupt0(void);
235 
242 void interrupt1(void);
243 
244 
253 {
254 
255 friend class uStepperDriver;
256 friend class uStepperEncoder;
257 friend void interrupt0(void);
258 friend void TIMER1_COMPA_vect(void) __attribute__ ((signal,used));
259 public:
260 
263 
266 
270  uStepperS();
271 
275  uStepperS(float acceleration, float velocity);
276 
280  void init( void );
281 
317  void setup( uint8_t mode = NORMAL,
318  uint16_t stepsPerRevolution = 200,
319  float pTerm = 10.0,
320  float iTerm = 0.0,
321  float dTerm = 0.0,
322  uint16_t dropinStepSize = 16,
323  bool setHome = true,
324  uint8_t invert = 0,
325  uint8_t runCurrent = 50,
326  uint8_t holdCurrent = 30);
327 
328 
337  void setRPM( float rpm );
338 
347  void setMaxAcceleration ( float acceleration );
348 
357  void setMaxDeceleration ( float deceleration );
358 
367  void setMaxVelocity ( float velocity );
368 
376  void setCurrent( double current );
377 
385  void setHoldCurrent( double current );
386 
401  void moveSteps( int32_t steps );
402 
418  void moveAngle( float angle );
419 
435  void moveToAngle( float angle );
436 
445  void runContinous( bool dir );
446 
452  float angleMoved( void );
453 
469  bool uStepperS::getMotorState(uint8_t statusType = POSITION_REACHED);
470 
481  void stop( bool mode = HARD );
482 
491  bool isStalled(float stallSensitivity = 0.992);
492 
493  void brakeMotor(bool brake);
494 
499  void enablePid(void);
500 
505  void disablePid(void);
506 
520  float moveToEnd(bool dir, float stallSensitivity = 0.992);
521 
526  float getPidError(void);
527 
534  void setProportional(float P);
535 
542  void setIntegral(float I);
543 
550  void setDifferential(float D);
551 
558  void invertDropinDir(bool invert);
559 
575  void dropinCli();
576 
583  void parseCommand(String *cmd);
584 
601  void dropinPrintHelp();
602 
603 private:
604 
609  float maxVelocity;
610 
616  float maxDeceleration;
617  bool invertPidDropinDirection;
618  float rpmToVelocity;
619  float angleToStep;
620 
621  uint16_t microSteps;
622  uint16_t fullSteps;
623  uint16_t dropinStepSize;
624 
625  int32_t stepCnt;
626 
627  float stepsPerSecondToRPM;
628  float RPMToStepsPerSecond;
629 
630  volatile posFilter_t externalStepInputFilter;
631 
632  float currentPidSpeed;
635  uint8_t mode;
636  float pTerm;
638  float iTerm;
639 
640  float dTerm;
641  bool brake;
642  volatile bool pidDisabled;
645  volatile bool stall;
646  // SPI functions
647 
648  volatile int32_t pidPositionStepsIssued = 0;
649  volatile float currentPidError;
650  float stallSensitivity = 0.992;
651  uint8_t SPI( uint8_t data );
652 
653  void setSPIMode( uint8_t mode );
654 
655  void chipSelect( uint8_t pin , bool state );
656 
657  void filterSpeedPos(posFilter_t *filter, int32_t steps);
658 
659  float pid(float error);
660  bool detectStall(int32_t stepsMoved);
661  dropinCliSettings_t dropinSettings;
662  bool loadDropinSettings(void);
663  void saveDropinSettings(void);
664  uint8_t dropinSettingsCalcChecksum(dropinCliSettings_t *settings);
665 };
666 
667 
668 
669 #endif
floatBytes_t D
Definition: uStepperS.h:161
void dropinCli()
This method is used to tune Drop-in parameters. After tuning uStepper S, the parameters are saved in ...
Definition: uStepperS.cpp:1114
floatBytes_t P
Definition: uStepperS.h:159
volatile bool stall
Definition: uStepperS.h:645
void moveAngle(float angle)
Moves the motor rotate a specific angle relative to the current position.
Definition: uStepperS.cpp:221
uStepperEncoder encoder
Definition: uStepperS.h:265
void interrupt0(void)
Used by dropin feature to take in step pulses.
Definition: uStepperS.cpp:483
friend void TIMER1_COMPA_vect(void) __attribute__((signal
Interrupt routine for critical tasks.
Definition: uStepperS.cpp:518
Prototype of class for accessing all features of the uStepper S in a single object.
Definition: uStepperS.h:252
void setHoldCurrent(double current)
Set motor hold current.
Definition: uStepperS.cpp:405
Struct to store dropin settings.
Definition: uStepperS.h:157
void dropinPrintHelp()
This method is used to print the dropinCli menu explainer:
Definition: uStepperS.cpp:1140
Function prototypes and definitions for the TMC5130 motor driver.
void setMaxVelocity(float velocity)
Set the maximum velocity of the stepper motor.
Definition: uStepperS.cpp:358
uStepperDriver driver
Definition: uStepperS.h:262
void interrupt1(void)
Used by dropin feature to take in enable signal.
Definition: uStepperS.cpp:471
#define NORMAL
Definition: uStepperS.h:202
Struct for encoder velocity estimator.
Definition: uStepperS.h:173
float getPidError(void)
This method returns the current PID error.
Definition: uStepperS.cpp:618
#define POSITION_REACHED
Definition: uStepperS.h:134
void runContinous(bool dir)
Make the motor rotate continuously.
Definition: uStepperS.cpp:419
floatBytes_t I
Definition: uStepperS.h:160
void parseCommand(String *cmd)
This method is used for the dropinCli to take in user commands.
Definition: uStepperS.cpp:709
Prototype of class for the TMC5130 Driver.
#define HARD
Definition: uStepperS.h:186
uint8_t mode
Definition: uStepperS.h:635
float maxAcceleration
Definition: uStepperS.h:615
Prototype of class for the AEAT8800-Q24 encoder.
void setHome(void)
Define new reference(home) position.
void setMaxDeceleration(float deceleration)
Set the maximum deceleration of the stepper motor.
Definition: uStepperS.cpp:381
volatile uint16_t angle
uStepperS()
Constructor of uStepper class.
Definition: uStepperS.cpp:35
void moveToAngle(float angle)
Moves the motor rotate a specific angle relative to the current position.
Definition: uStepperS.cpp:238
void TIMER1_COMPA_vect(void) __attribute__((signal
Interrupt routine for critical tasks.
Definition: uStepperS.cpp:518
Union to easily split a float into its binary representation.
Definition: uStepperS.h:143
void enablePid(void)
This method enables the PID after being disabled (disablePid).
Definition: uStepperS.cpp:582
void setRPM(float rpm)
Set the velocity in rpm.
Definition: uStepperS.cpp:315
void stop(bool mode=HARD)
Stop the motor.
Definition: uStepperS.cpp:437
void moveSteps(int32_t steps)
Make the motor perform a predefined number of steps.
Definition: uStepperS.cpp:206
void init(void)
Internal function to prepare the uStepperS in the constructor.
Definition: uStepperS.cpp:58
void chipSelect(bool state)
Set the output level of the chip select pin.
bool isStalled(float stallSensitivity=0.992)
This method returns a bool variable indicating wether the motor is stalled or not.
Definition: uStepperS.cpp:293
void setIntegral(float I)
This method is used to change the PID integral parameter I.
Definition: uStepperS.cpp:694
float iTerm
Definition: uStepperS.h:638
Function prototypes and definitions for the AEAT8800-Q24 Encoder.
float maxVelocity
Definition: uStepperS.h:609
Function prototypes and definitions for the uStepper Servo library.
void setProportional(float P)
This method is used to change the PID proportional parameter P.
Definition: uStepperS.cpp:689
void invertDropinDir(bool invert)
This method is used to invert the drop-in direction pin interpretation.
Definition: uStepperS.cpp:704
float moveToEnd(bool dir, float stallSensitivity=0.992)
Moves the motor to its physical limit, without limit switch.
Definition: uStepperS.cpp:596
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:92
void disablePid(void)
This method disables the PID until calling enablePid.
Definition: uStepperS.cpp:589
void setDifferential(float D)
This method is used to change the PID differential parameter D.
Definition: uStepperS.cpp:699
float angleMoved(void)
Get the angle moved from reference position in degrees.
Definition: uStepperS.cpp:432
void setMaxAcceleration(float acceleration)
Set the maximum acceleration of the stepper motor.
Definition: uStepperS.cpp:369
void setCurrent(double current)
Set motor output current.
Definition: uStepperS.cpp:392
bool getMotorState(uint8_t statusType=POSITION_REACHED)
Get the current motor driver state.
Definition: uStepperS.cpp:82
friend void interrupt0(void)
Used by dropin feature to take in step pulses.
Definition: uStepperS.cpp:483