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.5 *
4 * date: August 11th, 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 ********************************************************************************************/
160 #ifndef _USTEPPER_H_
161 #define _USTEPPER_H_
162 
163 #ifndef __AVR_ATmega328P__
164 #error !!This library only supports the ATmega328p MCU!!
165 #endif
166 
167 #include <inttypes.h>
168 #include <avr/io.h>
169 #include <Arduino.h>
170 
171 #define FULL 1
172 #define HALF 2
173 #define QUARTER 4
174 #define EIGHT 8
175 #define SIXTEEN 16
176 
177 #define NORMAL 0
178 #define DROPIN 1
179 
180 #define STOP 1
181 #define ACCEL 2
182 #define CRUISE 4
183 #define DECEL 8
184 #define INITDECEL 16
185 #define INTFREQ 28200.0f
186 #define CW 0
187 #define CCW 1
188 #define HARD 1
189 #define SOFT 0
191 #define ENCODERADDR 0x36
193 #define ANGLE 0x0E
194 #define STATUS 0x0B
195 #define AGC 0x1A
196 #define MAGNITUDE 0x1B
198 #define ENCODERINTFREQ 1000.0
199 #define ENCODERSPEEDCONSTANT ENCODERINTFREQ/10.0/360.0
201 #define R 4700.0
203 #define I2CFREE 0
205 #define READ 1
207 #define WRITE 0
209 #define START 0x08
211 #define REPSTART 0x10
213 #define TXADDRACK 0x18
215 #define TXDATAACK 0x28
217 #define RXADDRACK 0x40
219 #define ACK 1
221 #define NACK 0
239 #define A 0.001295752996237
240 #define B 0.000237488365866
241 #define C 0.000000083423218
243 #define ALPHA 0.85
244 #define BETA (1.0 - ALPHA)
245 
246 
247 extern "C" void interrupt0(void);
248 extern "C" void interrupt1(void);
249 extern "C" void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked,used));
250 extern "C" void TIMER1_COMPA_vect(void) __attribute__ ((signal,used));
251 
252 class float2
253 {
254  public:
255  float2(void);
256  float getFloatValue(void);
257  uint64_t getRawValue(void);
258  void setValue(float val);
259  float2 & operator=(const float &value);
260  bool operator==(const float2 &value);
261  bool operator!=(const float2 &value);
262  bool operator>=(const float2 &value);
263  bool operator<=(const float2 &value);
264  bool operator<=(const float &value);
265  bool operator<(const float2 &value);
266  bool operator>(const float2 &value);
267  float2 & operator*=(const float2 &value);
268  float2 & operator-=(const float2 &value);
269  float2 & operator+=(const float2 &value);
270  float2 & operator+=(const float &value);
271  float2 & operator/=(const float2 &value);
272  const float2 operator+(const float2 &value);
273  const float2 operator-(const float2 &value);
274  const float2 operator*(const float2 &value);
275  const float2 operator/(const float2 &value);
276  uint64_t value;
277 
278  private:
279  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal,used));
280 
281 };
282 
293 {
294 public:
295 
308  uStepperTemp(void);
309 
320  float getTemp(void);
321 private:
322 };
323 
332 {
333 public:
339  uStepperEncoder(void);
340 
349  float getAngle(void);
350 
360  float getSpeed(void);
361 
370  uint16_t getStrength(void);
371 
380  uint8_t getAgc(void);
381 
392  uint8_t detectMagnet(void);
393 
407  float getAngleMoved(void);
408 
415  void setup(void);
416 
423  void setHome(void);
424 
425 private:
426 
427  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal,used));
428 
430  volatile float oldAngle;
431  volatile float angle;
432  volatile float curSpeed;
433  volatile float angleMoved;
434 };
435 
443 class uStepper
444 {
445 private:
446  //Address offset: 0
447  uint16_t cruiseDelay;
448  //Address offset: 2
450  //Address offset: 10
451  uint8_t state;
452  //Address offset: 11
453  uint32_t accelSteps;
454  //Address offset: 15
455  uint32_t decelSteps;
456  //Address offset: 19
457  uint32_t initialDecelSteps;
458  //Address offset: 23
459  uint32_t cruiseSteps;
460  //Address offset: 27
461  uint32_t currentStep;
462  //Address offset: 31
463  uint32_t totalSteps;
464  //Address offset: 35
465  bool continous;
466  //Address offset: 36
467  bool hold;
468  //Address offset: 37
469  bool direction;
470  //Address offset: 38
471  int64_t stepsSinceReset;
472  //Address offset: 46
474  //Address offset: 54
475  uint16_t delay;
476  //Address offset: 56
477  bool dropIn;
478  //Address offset: 57
479  float velocity;
480  //Address offset: 61
481  float acceleration;
482  volatile uint16_t faultStepDelay;
483  volatile float tolerance;
484  volatile float stepResolution;
485 
486 
487  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked,used));
488  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal,used));
489 
490 
498  void startTimer(void);
499 
500 
508  void stopTimer(void);
509 
510 
518  void enableMotor(void);
519 
520 
527  void disableMotor(void);
528 
529 public:
544  uStepper(float accel, float vel);
545 
546 
554  uStepper(void);
555 
556 
567  void setMaxAcceleration(float accel);
568 
569 
578  float getMaxAcceleration(void);
579 
580 
591  void setMaxVelocity(float vel);
592 
593 
603  float getMaxVelocity(void);
604 
605 
616  void runContinous(bool dir);
617 
618 
632  void moveSteps(uint32_t steps, bool dir, bool holdMode);
633 
634 
645  void hardStop(bool holdMode);
646 
647 
658  void softStop(bool holdMode);
659 
660 
680  void setup(bool mode = NORMAL, uint8_t microStepping = SIXTEEN, float faultSpeed = 3000.0, uint32_t faultTolerance = 20);
681 
682 
692  bool getCurrentDirection(void);
693 
694 
704  bool getMotorState(void);
705 
706 
720  int64_t getStepsSinceReset(void);
721 
731  void pwmD8(float duty);
732 
742  void pwmD3(float duty);
743 };
744 
759 {
760  private:
761  uint8_t status;
772  void cmd(uint8_t cmd);
773 
774  public:
775 
783  i2cMaster(void);
784 
795  bool readByte(bool ack, uint8_t *data);
796 
810  bool read(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
811 
824  bool start(uint8_t addr, bool RW);
825 
838  bool restart(uint8_t addr, bool RW);
839 
850  bool writeByte(uint8_t data);
851 
865  bool write(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
866 
875  bool stop(void);
876 
885  uint8_t getStatus(void);
886 
893  void begin(void);
894 };
895 
896 extern i2cMaster I2C;
898 #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:1217
uint32_t currentStep
Definition: uStepper.h:461
Prototype of class for the temperature sensor.
Definition: uStepper.h:292
uint16_t getStrength(void)
Measure the strength of the magnet.
Definition: uStepper.cpp:737
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:1270
float2 multiplier
Definition: uStepper.h:449
float acceleration
Definition: uStepper.h:481
bool continous
Definition: uStepper.h:465
void pwmD8(float duty)
Generate PWM signal on digital output 8.
Definition: uStepper.cpp:1292
Prototype of class for accessing all features of the uStepper in a single object. ...
Definition: uStepper.h:443
i2cMaster I2C
Definition: uStepper.cpp:73
volatile float curSpeed
Definition: uStepper.h:432
void enableMotor(void)
Enables the stepper driver output stage.
Definition: uStepper.cpp:1255
float getMaxAcceleration(void)
Get the value of the maximum motor acceleration.
Definition: uStepper.cpp:830
float getTemp(void)
Request a reading of current temperature.
Definition: uStepper.cpp:637
uint32_t cruiseSteps
Definition: uStepper.h:459
uint32_t totalSteps
Definition: uStepper.h:463
#define C
Definition: uStepper.h:241
bool getCurrentDirection(void)
Returns the direction the motor is currently configured to rotate.
Definition: uStepper.cpp:1265
float getAngleMoved(void)
Measure the angle moved from reference position.
Definition: uStepper.cpp:657
uint32_t accelSteps
Definition: uStepper.h:453
void hardStop(bool holdMode)
Stop the motor without deceleration.
Definition: uStepper.cpp:1138
void setMaxAcceleration(float accel)
Set the maximum acceleration of the stepper motor.
Definition: uStepper.cpp:810
void startTimer(void)
Starts timer for stepper algorithm.
Definition: uStepper.cpp:1241
void setMaxVelocity(float vel)
Sets the maximum rotational velocity of the motor.
Definition: uStepper.cpp:860
uStepperTemp temp
Definition: uStepper.h:530
void moveSteps(uint32_t steps, bool dir, bool holdMode)
Make the motor perform a predefined number of steps.
Definition: uStepper.cpp:985
float getMaxVelocity(void)
Returns the maximum rotational velocity of the motor.
Definition: uStepper.cpp:893
void pwmD3(float duty)
Generate PWM signal on digital output 3.
Definition: uStepper.cpp:1311
void softStop(bool holdMode)
Stop the motor with deceleration.
Definition: uStepper.cpp:1169
uint8_t status
Definition: uStepper.h:761
uint8_t detectMagnet(void)
Detect if magnet is present and within range.
Definition: uStepper.cpp:755
int64_t stepsSinceReset
Definition: uStepper.h:471
uStepperEncoder encoder
Definition: uStepper.h:531
void stopTimer(void)
Stops the timer for the stepper algorithm.
Definition: uStepper.cpp:1250
uint8_t getAgc(void)
Read the current AGC value of the encoder chip.
Definition: uStepper.cpp:746
void runContinous(bool dir)
Make the motor rotate continuously.
Definition: uStepper.cpp:898
Prototype of class for accessing the TWI (I2C) interface of the AVR (master mode only).
Definition: uStepper.h:758
Prototype of class for the AS5600 encoder.
Definition: uStepper.h:331
void setHome(void)
Define new reference(home) position.
Definition: uStepper.cpp:719
uint16_t cruiseDelay
Definition: uStepper.h:447
float getAngle(void)
Measure the current shaft angle.
Definition: uStepper.cpp:732
volatile float oldAngle
Definition: uStepper.h:430
float velocity
Definition: uStepper.h:479
float2 exactDelay
Definition: uStepper.h:473
volatile float angleMoved
Definition: uStepper.h:433
uint8_t state
Definition: uStepper.h:451
uStepperTemp(void)
Constructor.
Definition: uStepper.cpp:632
uint16_t delay
Definition: uStepper.h:475
uint32_t decelSteps
Definition: uStepper.h:455
bool direction
Definition: uStepper.h:469
bool hold
Definition: uStepper.h:467
float encoderOffset
Definition: uStepper.h:429
void disableMotor(void)
Disables the stepper driver output stage.
Definition: uStepper.cpp:1260
int64_t getStepsSinceReset(void)
Get the number of steps applied since reset.
Definition: uStepper.cpp:1280
void setup(void)
Setup the encoder.
Definition: uStepper.cpp:700
uint32_t initialDecelSteps
Definition: uStepper.h:457