uStepper
 All Classes Files Functions Variables Macros Pages
uStepper.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * File: uStepper.h *
3 * Version: 0.4.1 *
4 * date: July 31th, 2016 *
5 * Author: Thomas Hørring Olsen *
6 * *
7 *********************************************************************************************
8 * (C) 2016 *
9 * *
10 * ON Development IVS *
11 * www.on-development.com *
12 * administration@on-development.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 ON Development IVS nor the author, can be held responsible for any damage *
20 * caused by the use of the code contained in this file ! *
21 * *
22 ********************************************************************************************/
148 #ifndef _USTEPPER_H_
149 #define _USTEPPER_H_
150 
151 #ifndef __AVR_ATmega328P__
152 #error !!This library only supports the ATmega328p MCU!!
153 #endif
154 
155 #include <inttypes.h>
156 #include <avr/io.h>
157 #include <Arduino.h>
158 
159 #define FULL 1
160 #define HALF 2
161 #define QUARTER 4
162 #define EIGHT 8
163 #define SIXTEEN 16
164 
165 #define NORMAL 0
166 #define DROPIN 1
167 
168 #define STOP 1
169 #define ACCEL 2
170 #define CRUISE 4
171 #define DECEL 8
172 #define INITDECEL 16
173 #define INTFREQ 28200.0f
174 #define CW 0
175 #define CCW 1
176 #define HARD 1
177 #define SOFT 0
179 #define ENCODERADDR 0x36
181 #define ANGLE 0x0E
182 #define STATUS 0x0B
183 #define AGC 0x1A
184 #define MAGNITUDE 0x1B
186 #define ENCODERINTFREQ 1000.0
187 #define ENCODERSPEEDCONSTANT ENCODERINTFREQ/10.0/360.0
189 #define R 4700.0
191 #define I2CFREE 0
193 #define READ 1
195 #define WRITE 0
197 #define START 0x08
199 #define REPSTART 0x10
201 #define TXADDRACK 0x18
203 #define TXDATAACK 0x28
205 #define RXADDRACK 0x40
207 #define ACK 1
209 #define NACK 0
227 #define A 0.001295752996237
228 #define B 0.000237488365866
229 #define C 0.000000083423218
231 #define ALPHA 0.85
232 #define BETA (1.0 - ALPHA)
233 
234 
235 extern "C" void INT0_vect(void) __attribute__ ((signal));
236 extern "C" void INT1_vect(void) __attribute__ ((signal));
237 extern "C" void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked));
238 extern "C" void TIMER1_COMPA_vect(void) __attribute__ ((signal));
239 
240 class float2
241 {
242  public:
243  float2(void);
244  float getFloatValue(void);
245  uint64_t getRawValue(void);
246  void setValue(float val);
247  float2 & operator=(const float &value);
248  bool operator==(const float2 &value);
249  bool operator!=(const float2 &value);
250  bool operator>=(const float2 &value);
251  bool operator<=(const float2 &value);
252  bool operator<=(const float &value);
253  bool operator<(const float2 &value);
254  bool operator>(const float2 &value);
255  float2 & operator*=(const float2 &value);
256  float2 & operator-=(const float2 &value);
257  float2 & operator+=(const float2 &value);
258  float2 & operator+=(const float &value);
259  float2 & operator/=(const float2 &value);
260  const float2 operator+(const float2 &value);
261  const float2 operator-(const float2 &value);
262  const float2 operator*(const float2 &value);
263  const float2 operator/(const float2 &value);
264  uint64_t value;
265 
266  private:
267  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal));
268 
269 };
270 
281 {
282 public:
283 
296  uStepperTemp(void);
297 
308  float getTemp(void);
309 private:
310 };
311 
320 {
321 public:
327  uStepperEncoder(void);
328 
337  float getAngle(void);
338 
348  float getSpeed(void);
349 
358  uint16_t getStrength(void);
359 
368  uint8_t getAgc(void);
369 
380  uint8_t detectMagnet(void);
381 
395  float getAngleMoved(void);
396 
403  void setup(void);
404 
411  void setHome(void);
412 
413 private:
414 
415  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal));
416 
418  volatile float oldAngle;
419  volatile float angle;
420  volatile float curSpeed;
421  volatile float angleMoved;
422 };
423 
431 class uStepper
432 {
433 private:
434  //Address offset: 0
435  uint16_t cruiseDelay;
436  //Address offset: 2
438  //Address offset: 10
439  uint8_t state;
440  //Address offset: 11
441  uint32_t accelSteps;
442  //Address offset: 15
443  uint32_t decelSteps;
444  //Address offset: 19
445  uint32_t initialDecelSteps;
446  //Address offset: 23
447  uint32_t cruiseSteps;
448  //Address offset: 27
449  uint32_t currentStep;
450  //Address offset: 31
451  uint32_t totalSteps;
452  //Address offset: 35
453  bool continous;
454  //Address offset: 36
455  bool hold;
456  //Address offset: 37
457  bool direction;
458  //Address offset: 38
459  int64_t stepsSinceReset;
460  //Address offset: 46
462  //Address offset: 54
463  uint16_t delay;
464  //Address offset: 56
465  bool dropIn;
466  //Address offset: 57
467  float velocity;
468  //Address offset: 61
469  float acceleration;
470  volatile uint16_t faultStepDelay;
471  volatile float tolerance;
472  volatile float stepResolution;
473 
474 
475  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked));
476  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal));
477 
478 
486  void startTimer(void);
487 
488 
496  void stopTimer(void);
497 
498 
506  void enableMotor(void);
507 
508 
515  void disableMotor(void);
516 
517 public:
532  uStepper(float accel, float vel);
533 
534 
542  uStepper(void);
543 
544 
555  void setMaxAcceleration(float accel);
556 
557 
566  float getMaxAcceleration(void);
567 
568 
579  void setMaxVelocity(float vel);
580 
581 
591  float getMaxVelocity(void);
592 
593 
604  void runContinous(bool dir);
605 
606 
620  void moveSteps(uint32_t steps, bool dir, bool holdMode);
621 
622 
633  void hardStop(bool holdMode);
634 
635 
646  void softStop(bool holdMode);
647 
648 
657  void setup(bool mode = NORMAL, uint8_t microStepping = SIXTEEN, float faultSpeed = 3000.0, uint32_t faultTolerance = 20);
658 
659 
669  bool getCurrentDirection(void);
670 
671 
681  bool getMotorState(void);
682 
683 
697  int64_t getStepsSinceReset(void);
698 };
699 
714 {
715  private:
716  uint8_t status;
727  void cmd(uint8_t cmd);
728 
729  public:
730 
738  i2cMaster(void);
739 
750  bool readByte(bool ack, uint8_t *data);
751 
765  bool read(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
766 
779  bool start(uint8_t addr, bool RW);
780 
793  bool restart(uint8_t addr, bool RW);
794 
805  bool writeByte(uint8_t data);
806 
820  bool write(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
821 
830  bool stop(void);
831 
840  uint8_t getStatus(void);
841 
848  void begin(void);
849 };
850 
851 extern i2cMaster I2C;
853 #endif
uStepperEncoder(void)
Constructor.
Definition: uStepper.cpp:652
void setup(bool mode=NORMAL, uint8_t microStepping=SIXTEEN, float faultSpeed=3000.0, uint32_t faultTolerance=20)
Initializes the different parts of the uStepper object.
Definition: uStepper.cpp:1212
uint32_t currentStep
Definition: uStepper.h:449
Prototype of class for the temperature sensor.
Definition: uStepper.h:280
uint16_t getStrength(void)
Measure the strength of the magnet.
Definition: uStepper.cpp:734
float getSpeed(void)
Measure the current speed of the motor.
Definition: uStepper.cpp:695
bool getMotorState(void)
Get the current state of the motor.
Definition: uStepper.cpp:1267
float2 multiplier
Definition: uStepper.h:437
float acceleration
Definition: uStepper.h:469
bool continous
Definition: uStepper.h:453
Prototype of class for accessing all features of the uStepper in a single object. ...
Definition: uStepper.h:431
i2cMaster I2C
Definition: uStepper.cpp:73
volatile float curSpeed
Definition: uStepper.h:420
void enableMotor(void)
Enables the stepper driver output stage.
Definition: uStepper.cpp:1252
float getMaxAcceleration(void)
Get the value of the maximum motor acceleration.
Definition: uStepper.cpp:827
float getTemp(void)
Request a reading of current temperature.
Definition: uStepper.cpp:637
uint32_t cruiseSteps
Definition: uStepper.h:447
uint32_t totalSteps
Definition: uStepper.h:451
#define C
Definition: uStepper.h:229
bool getCurrentDirection(void)
Returns the direction the motor is currently configured to rotate.
Definition: uStepper.cpp:1262
float getAngleMoved(void)
Measure the angle moved from reference position.
Definition: uStepper.cpp:657
uint32_t accelSteps
Definition: uStepper.h:441
void hardStop(bool holdMode)
Stop the motor without deceleration.
Definition: uStepper.cpp:1133
void setMaxAcceleration(float accel)
Set the maximum acceleration of the stepper motor.
Definition: uStepper.cpp:807
void startTimer(void)
Starts timer for stepper algorithm.
Definition: uStepper.cpp:1238
void setMaxVelocity(float vel)
Sets the maximum rotational velocity of the motor.
Definition: uStepper.cpp:857
uStepperTemp temp
Definition: uStepper.h:518
void moveSteps(uint32_t steps, bool dir, bool holdMode)
Make the motor perform a predefined number of steps.
Definition: uStepper.cpp:980
float getMaxVelocity(void)
Returns the maximum rotational velocity of the motor.
Definition: uStepper.cpp:890
void softStop(bool holdMode)
Stop the motor with deceleration.
Definition: uStepper.cpp:1164
uint8_t status
Definition: uStepper.h:716
uint8_t detectMagnet(void)
Detect if magnet is present and within range.
Definition: uStepper.cpp:752
int64_t stepsSinceReset
Definition: uStepper.h:459
uStepperEncoder encoder
Definition: uStepper.h:519
void stopTimer(void)
Stops the timer for the stepper algorithm.
Definition: uStepper.cpp:1247
uint8_t getAgc(void)
Read the current AGC value of the encoder chip.
Definition: uStepper.cpp:743
void runContinous(bool dir)
Make the motor rotate continuously.
Definition: uStepper.cpp:895
Prototype of class for accessing the TWI (I2C) interface of the AVR (master mode only).
Definition: uStepper.h:713
Prototype of class for the AS5600 encoder.
Definition: uStepper.h:319
void setHome(void)
Define new reference(home) position.
Definition: uStepper.cpp:720
uint16_t cruiseDelay
Definition: uStepper.h:435
float getAngle(void)
Measure the current shaft angle.
Definition: uStepper.cpp:729
volatile float oldAngle
Definition: uStepper.h:418
float velocity
Definition: uStepper.h:467
float2 exactDelay
Definition: uStepper.h:461
volatile float angleMoved
Definition: uStepper.h:421
uint8_t state
Definition: uStepper.h:439
uStepperTemp(void)
Constructor.
Definition: uStepper.cpp:632
uint16_t delay
Definition: uStepper.h:463
uint32_t decelSteps
Definition: uStepper.h:443
bool direction
Definition: uStepper.h:457
bool hold
Definition: uStepper.h:455
float encoderOffset
Definition: uStepper.h:417
void disableMotor(void)
Disables the stepper driver output stage.
Definition: uStepper.cpp:1257
int64_t getStepsSinceReset(void)
Get the number of steps applied since reset.
Definition: uStepper.cpp:1277
void setup(void)
Setup the encoder.
Definition: uStepper.cpp:700
uint32_t initialDecelSteps
Definition: uStepper.h:445