uStepper
uStepper.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * File: uStepper.h *
3 * Version: 1.3.0 *
4 * date: January 10th, 2018 *
5 * Author: Thomas Hørring Olsen *
6 * *
7 *********************************************************************************************
8 * (C) 2018 *
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 ********************************************************************************************/
199 #ifndef _USTEPPER_H_
200 #define _USTEPPER_H_
201 
202 #ifndef __AVR_ATmega328P__
203 #error !!This library only supports the ATmega328p MCU!!
204 #endif
205 
206 #include <inttypes.h>
207 #include <avr/io.h>
208 #include <avr/delay.h>
209 #include <Arduino.h>
210 #include <uStepperServo.h>
211 
213 #define FULL 1
214 
215 #define HALF 2
216 
217 #define QUARTER 4
218 
219 #define EIGHT 8
220 
221 #define SIXTEEN 16
222 
223 #define NORMAL 0
224 
225 #define PWM 1
226 
227 #define DROPIN 1
228 
229 #define PID 2
230 
232 #define STOP 1
233 
234 #define ACCEL 2
235 
236 #define CRUISE 4
237 
238 #define DECEL 8
239 
240 #define INITDECEL 16
241 
242 #define INTFREQ 28200.0f
243 
244 #define INTPERIOD 1000000.0/INTFREQ
245 
246 #define INTPIDDELAYCONSTANT 0.028199994
247 
248 #define CW 0
249 
250 #define CCW 1
251 
252 #define HARD 1
253 
254 #define SOFT 0
255 
257 #define ENCODERINTFREQ 1000.0
258 
259 #define ENCODERSPEEDCONSTANT ENCODERINTFREQ/10.0/4096.0
260 
261 #define ENCODERADDR 0x36
262 
263 #define ANGLE 0x0E
264 
265 #define STATUS 0x0B
266 
267 #define AGC 0x1A
268 
269 #define MAGNITUDE 0x1B
270 
272 #define R 4700.0
273 
275 #define I2CFREE 0
276 
278 #define READ 1
279 
281 #define WRITE 0
282 
284 #define START 0x08
285 
287 #define REPSTART 0x10
288 
290 #define TXADDRACK 0x18
291 
293 #define TXDATAACK 0x28
294 
296 #define RXADDRACK 0x40
297 
299 #define ACK 1
300 
302 #define NACK 0
303 
321 #define A 0.001295752996237
322 
323 #define B 0.000237488365866
324 
325 #define C 0.000000083423218
326 
333 extern "C" void interrupt0(void);
334 
341 extern "C" void interrupt1(void);
342 
353 extern "C" void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked,used));
354 
363 extern "C" void TIMER1_COMPA_vect(void) __attribute__ ((signal,used));
364 
365 class float2
366 {
367  public:
368  float2(void);
369  float getFloatValue(void);
370  uint64_t getRawValue(void);
371  void setValue(float val);
372  float2 & operator=(const float &value);
373  bool operator==(const float2 &value);
374  bool operator!=(const float2 &value);
375  bool operator>=(const float2 &value);
376  bool operator<=(const float2 &value);
377  bool operator<=(const float &value);
378  bool operator<(const float2 &value);
379  bool operator>(const float2 &value);
380  float2 & operator*=(const float2 &value);
381  float2 & operator-=(const float2 &value);
382  float2 & operator+=(const float2 &value);
383  float2 & operator+=(const float &value);
384  float2 & operator/=(const float2 &value);
385  const float2 operator+(const float2 &value);
386  const float2 operator-(const float2 &value);
387  const float2 operator*(const float2 &value);
388  const float2 operator/(const float2 &value);
389  uint64_t value;
390 
391  private:
392  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal,used));
393 
394 };
395 
406 {
407 public:
408 
421  uStepperTemp(void);
422 
435  float getTemp(void);
436 private:
437 };
438 
449 {
450 public:
453  volatile int32_t angleMoved;
454 
456  uint16_t encoderOffset;
457 
461  volatile uint16_t oldAngle;
462 
465  volatile uint16_t angle;
466 
470  volatile int16_t revolutions;
471 
474  volatile float curSpeed;
480  uStepperEncoder(void);
481 
494  float getAngle(void);
495 
506  float getSpeed(void);
507 
515  uint16_t getStrength(void);
516 
526  uint8_t getAgc(void);
527 
538  uint8_t detectMagnet(void);
539 
556  float getAngleMoved(void);
557 
566  void setup(uint8_t mode);
567 
574  void setHome(void);
575 
576 private:
577 
578  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal,used));
579 };
580 
589 class uStepper
590 {
591 private:
592  //Address offset: 0
594  uint16_t cruiseDelay;
595 
596  //Address offset: 2
601 
602  //Address offset: 10
606  uint8_t state;
607 
608  //Address offset: 11
611  uint32_t accelSteps;
612 
613  //Address offset: 15
616  uint32_t decelSteps;
617 
618  //Address offset: 19
621  uint32_t initialDecelSteps;
622 
623  //Address offset: 23
626  uint32_t cruiseSteps;
627 
628  //Address offset: 27
631  uint32_t currentStep;
632 
633  //Address offset: 31
636  uint32_t totalSteps;
637 
638  //Address offset: 35
642  bool continous;
643 
644  //Address offset: 36
649  bool hold;
650 
651  //Address offset: 37
654  bool direction;
655 
656  //Address offset: 38
662  volatile int32_t stepsSinceReset;
663 
666  int32_t dummy;
667 
668  //Address offset: 46
673 
674  //Address offset: 54
682  uint16_t delay;
683 
684  //Address offset: 56
686  bool dropIn;
687 
688  //Address offset: 57
693  float velocity;
694 
695  //Address offset: 61
702  float acceleration;
703 
704  //address offset: 65
707  volatile float tolerance;
708 
709  //address offset: 69
712  volatile float hysteresis;
713 
714  //address offset: 73
717  volatile float stepConversion;
718 
719  //address offset: 77
722  volatile uint16_t counter;
723 
724  //address offset: 79
727  volatile int32_t stepCnt;
728 
729  //address offset: 83
732  volatile int32_t control;
733 
734  //address offset: 87
737  volatile uint32_t speedValue[2];
738 
739  //address offset: 95
742  float pTerm;
743 
744  //address offset: 99
746  float iTerm;
747 
748  //address offset: 103
751  float dTerm;
752 
753  //address offset: 107
756  uint8_t mode;
757 
760  float angleToStep;
761 
764  volatile bool stall;
765 
766  volatile bool invertDir;
767 
768  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked,used));
769  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal,used));
770  friend void interrupt1(void);
771  friend void uStepperEncoder::setHome(void);
772 
781  void startTimer(void);
782 
783 
792  void stopTimer(void);
793 
794 
804  void enableMotor(void);
805 
806 
814  void disableMotor(void);
815 
820  void pidDropIn(void);
821 
826  void pid(void);
827 
828 public:
833 
848  uStepper(float accel, float vel);
849 
850 
859  uStepper(void);
860 
861 
873  void setMaxAcceleration(float accel);
874 
875 
884  float getMaxAcceleration(void);
885 
886 
897  void setMaxVelocity(float vel);
898 
899 
909  float getMaxVelocity(void);
910 
911 
924  void runContinous(bool dir);
925 
926 
944  void moveSteps(int32_t steps, bool dir, bool holdMode);
945 
946 
960  void hardStop(bool holdMode);
961 
962 
977  void softStop(bool holdMode);
978 
979 
1016  void setup( uint8_t mode = NORMAL,
1017  uint8_t microStepping = SIXTEEN,
1018  float faultTolerance = 10.0,
1019  float faultHysteresis = 5.0,
1020  float pTerm = 1.0,
1021  float iTerm = 0.02,
1022  float dterm = 0.006,
1023  bool setHome = true);
1024 
1035  bool getCurrentDirection(void);
1036 
1037 
1047  bool getMotorState(void);
1048 
1049 
1071  int32_t getStepsSinceReset(void);
1072 
1084  void pwmD8(double duty);
1085 
1095  void setCurrent(double current);
1096 
1105  void pwmD8(int mode);
1106 
1118  void pwmD3(double duty);
1119 
1120 
1129  void pwmD3(int mode);
1130 
1140  void updateSetPoint(float setPoint);
1141 
1153  float moveToEnd(bool dir);
1154 
1165  void moveToAngle(float angle, bool holdMode);
1166 
1176  void moveAngle(float angle, bool holdMode);
1177 
1184  bool isStalled(void);
1185 
1186  bool detectStall(float diff, bool running);
1187 };
1188 
1211 {
1212  private:
1214  uint8_t status;
1215 
1216 
1225  bool cmd(uint8_t cmd);
1226 
1227  public:
1228 
1238  i2cMaster(void);
1239 
1257  bool readByte(bool ack, uint8_t *data);
1258 
1280  bool read(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
1281 
1299  bool start(uint8_t addr, bool RW);
1300 
1318  bool restart(uint8_t addr, bool RW);
1319 
1330  bool writeByte(uint8_t data);
1331 
1352  bool write(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
1353 
1364  bool stop(void);
1365 
1374  uint8_t getStatus(void);
1375 
1383  void begin(void);
1384 };
1386 extern i2cMaster I2C;
1387 
1388 #endif
void interrupt0(void)
Used by dropin feature to take in step pulses.
Definition: uStepper.cpp:115
uint32_t currentStep
Definition: uStepper.h:631
Prototype of class for the temperature sensor.
Definition: uStepper.h:405
void TIMER1_COMPA_vect(void) __attribute__((signal
Measures angle and speed of motor.
Definition: uStepper.cpp:271
volatile uint16_t counter
Definition: uStepper.h:722
float angleToStep
Definition: uStepper.h:760
friend void TIMER2_COMPA_vect(void) __attribute__((signal
Used to apply step pulses to the motor.
Definition: uStepper.cpp:128
volatile int32_t stepCnt
Definition: uStepper.h:727
float2 multiplier
Definition: uStepper.h:600
float acceleration
Definition: uStepper.h:702
bool continous
Definition: uStepper.h:642
Prototype of class for accessing all features of the uStepper in a single object. ...
Definition: uStepper.h:589
i2cMaster I2C
Definition: uStepper.cpp:78
void TIMER2_COMPA_vect(void) __attribute__((signal
Used to apply step pulses to the motor.
Definition: uStepper.cpp:128
volatile float curSpeed
Definition: uStepper.h:474
bool dropIn
Definition: uStepper.h:686
uint32_t cruiseSteps
Definition: uStepper.h:626
uint32_t totalSteps
Definition: uStepper.h:636
uint32_t accelSteps
Definition: uStepper.h:611
float iTerm
Definition: uStepper.h:746
uStepperTemp temp
Definition: uStepper.h:830
uint8_t status
Definition: uStepper.h:1214
volatile int32_t stepsSinceReset
Definition: uStepper.h:662
uint8_t mode
Definition: uStepper.h:756
volatile int32_t angleMoved
Definition: uStepper.h:453
uStepperEncoder encoder
Definition: uStepper.h:832
volatile float tolerance
Definition: uStepper.h:707
volatile uint16_t oldAngle
Definition: uStepper.h:461
float pTerm
Definition: uStepper.h:742
Prototype of class for accessing the TWI (I2C) interface of the AVR (master mode only).
Definition: uStepper.h:1210
volatile int32_t control
Definition: uStepper.h:732
Prototype of class for the AS5600 encoder.
Definition: uStepper.h:448
volatile float hysteresis
Definition: uStepper.h:712
void setHome(void)
Define new reference(home) position.
Definition: uStepper.cpp:717
volatile uint16_t angle
Definition: uStepper.h:465
volatile int16_t revolutions
Definition: uStepper.h:470
uint16_t cruiseDelay
Definition: uStepper.h:594
#define NORMAL
Definition: uStepper.h:223
void interrupt1(void)
Used by dropin feature to take in enable signal.
Definition: uStepper.cpp:82
uint16_t encoderOffset
Definition: uStepper.h:456
#define SIXTEEN
Definition: uStepper.h:221
float velocity
Definition: uStepper.h:693
float2 exactDelay
Definition: uStepper.h:672
float dTerm
Definition: uStepper.h:751
Function prototypes and definitions for the uStepper Servo library.
uint8_t state
Definition: uStepper.h:606
volatile float stepConversion
Definition: uStepper.h:717
uint16_t delay
Definition: uStepper.h:682
uint32_t decelSteps
Definition: uStepper.h:616
bool direction
Definition: uStepper.h:654
int32_t dummy
Definition: uStepper.h:666
bool hold
Definition: uStepper.h:649
uint32_t initialDecelSteps
Definition: uStepper.h:621
volatile bool stall
Definition: uStepper.h:764