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.2 *
4 * date: August 3rd, 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 ********************************************************************************************/
151 #ifndef _USTEPPER_H_
152 #define _USTEPPER_H_
153 
154 #ifndef __AVR_ATmega328P__
155 #error !!This library only supports the ATmega328p MCU!!
156 #endif
157 
158 #include <inttypes.h>
159 #include <avr/io.h>
160 #include <Arduino.h>
161 
162 #define FULL 1
163 #define HALF 2
164 #define QUARTER 4
165 #define EIGHT 8
166 #define SIXTEEN 16
167 
168 #define NORMAL 0
169 #define DROPIN 1
170 
171 #define STOP 1
172 #define ACCEL 2
173 #define CRUISE 4
174 #define DECEL 8
175 #define INITDECEL 16
176 #define INTFREQ 28200.0f
177 #define CW 0
178 #define CCW 1
179 #define HARD 1
180 #define SOFT 0
182 #define ENCODERADDR 0x36
184 #define ANGLE 0x0E
185 #define STATUS 0x0B
186 #define AGC 0x1A
187 #define MAGNITUDE 0x1B
189 #define ENCODERINTFREQ 1000.0
190 #define ENCODERSPEEDCONSTANT ENCODERINTFREQ/10.0/360.0
192 #define R 4700.0
194 #define I2CFREE 0
196 #define READ 1
198 #define WRITE 0
200 #define START 0x08
202 #define REPSTART 0x10
204 #define TXADDRACK 0x18
206 #define TXDATAACK 0x28
208 #define RXADDRACK 0x40
210 #define ACK 1
212 #define NACK 0
230 #define A 0.001295752996237
231 #define B 0.000237488365866
232 #define C 0.000000083423218
234 #define ALPHA 0.85
235 #define BETA (1.0 - ALPHA)
236 
237 
238 extern "C" void INT0_vect(void) __attribute__ ((signal));
239 extern "C" void INT1_vect(void) __attribute__ ((signal));
240 extern "C" void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked));
241 extern "C" void TIMER1_COMPA_vect(void) __attribute__ ((signal));
242 
243 class float2
244 {
245  public:
246  float2(void);
247  float getFloatValue(void);
248  uint64_t getRawValue(void);
249  void setValue(float val);
250  float2 & operator=(const float &value);
251  bool operator==(const float2 &value);
252  bool operator!=(const float2 &value);
253  bool operator>=(const float2 &value);
254  bool operator<=(const float2 &value);
255  bool operator<=(const float &value);
256  bool operator<(const float2 &value);
257  bool operator>(const float2 &value);
258  float2 & operator*=(const float2 &value);
259  float2 & operator-=(const float2 &value);
260  float2 & operator+=(const float2 &value);
261  float2 & operator+=(const float &value);
262  float2 & operator/=(const float2 &value);
263  const float2 operator+(const float2 &value);
264  const float2 operator-(const float2 &value);
265  const float2 operator*(const float2 &value);
266  const float2 operator/(const float2 &value);
267  uint64_t value;
268 
269  private:
270  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal));
271 
272 };
273 
284 {
285 public:
286 
299  uStepperTemp(void);
300 
311  float getTemp(void);
312 private:
313 };
314 
323 {
324 public:
330  uStepperEncoder(void);
331 
340  float getAngle(void);
341 
351  float getSpeed(void);
352 
361  uint16_t getStrength(void);
362 
371  uint8_t getAgc(void);
372 
383  uint8_t detectMagnet(void);
384 
398  float getAngleMoved(void);
399 
406  void setup(void);
407 
414  void setHome(void);
415 
416 private:
417 
418  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal));
419 
421  volatile float oldAngle;
422  volatile float angle;
423  volatile float curSpeed;
424  volatile float angleMoved;
425 };
426 
434 class uStepper
435 {
436 private:
437  //Address offset: 0
438  uint16_t cruiseDelay;
439  //Address offset: 2
441  //Address offset: 10
442  uint8_t state;
443  //Address offset: 11
444  uint32_t accelSteps;
445  //Address offset: 15
446  uint32_t decelSteps;
447  //Address offset: 19
448  uint32_t initialDecelSteps;
449  //Address offset: 23
450  uint32_t cruiseSteps;
451  //Address offset: 27
452  uint32_t currentStep;
453  //Address offset: 31
454  uint32_t totalSteps;
455  //Address offset: 35
456  bool continous;
457  //Address offset: 36
458  bool hold;
459  //Address offset: 37
460  bool direction;
461  //Address offset: 38
462  int64_t stepsSinceReset;
463  //Address offset: 46
465  //Address offset: 54
466  uint16_t delay;
467  //Address offset: 56
468  bool dropIn;
469  //Address offset: 57
470  float velocity;
471  //Address offset: 61
472  float acceleration;
473  volatile uint16_t faultStepDelay;
474  volatile float tolerance;
475  volatile float stepResolution;
476 
477 
478  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked));
479  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal));
480 
481 
489  void startTimer(void);
490 
491 
499  void stopTimer(void);
500 
501 
509  void enableMotor(void);
510 
511 
518  void disableMotor(void);
519 
520 public:
535  uStepper(float accel, float vel);
536 
537 
545  uStepper(void);
546 
547 
558  void setMaxAcceleration(float accel);
559 
560 
569  float getMaxAcceleration(void);
570 
571 
582  void setMaxVelocity(float vel);
583 
584 
594  float getMaxVelocity(void);
595 
596 
607  void runContinous(bool dir);
608 
609 
623  void moveSteps(uint32_t steps, bool dir, bool holdMode);
624 
625 
636  void hardStop(bool holdMode);
637 
638 
649  void softStop(bool holdMode);
650 
651 
660  void setup(bool mode = NORMAL, uint8_t microStepping = SIXTEEN, float faultSpeed = 3000.0, uint32_t faultTolerance = 20);
661 
662 
672  bool getCurrentDirection(void);
673 
674 
684  bool getMotorState(void);
685 
686 
700  int64_t getStepsSinceReset(void);
701 };
702 
717 {
718  private:
719  uint8_t status;
730  void cmd(uint8_t cmd);
731 
732  public:
733 
741  i2cMaster(void);
742 
753  bool readByte(bool ack, uint8_t *data);
754 
768  bool read(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
769 
782  bool start(uint8_t addr, bool RW);
783 
796  bool restart(uint8_t addr, bool RW);
797 
808  bool writeByte(uint8_t data);
809 
823  bool write(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
824 
833  bool stop(void);
834 
843  uint8_t getStatus(void);
844 
851  void begin(void);
852 };
853 
854 extern i2cMaster I2C;
856 #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:1216
uint32_t currentStep
Definition: uStepper.h:452
Prototype of class for the temperature sensor.
Definition: uStepper.h:283
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:1271
float2 multiplier
Definition: uStepper.h:440
float acceleration
Definition: uStepper.h:472
bool continous
Definition: uStepper.h:456
Prototype of class for accessing all features of the uStepper in a single object. ...
Definition: uStepper.h:434
i2cMaster I2C
Definition: uStepper.cpp:73
volatile float curSpeed
Definition: uStepper.h:423
void enableMotor(void)
Enables the stepper driver output stage.
Definition: uStepper.cpp:1256
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:450
uint32_t totalSteps
Definition: uStepper.h:454
#define C
Definition: uStepper.h:232
bool getCurrentDirection(void)
Returns the direction the motor is currently configured to rotate.
Definition: uStepper.cpp:1266
float getAngleMoved(void)
Measure the angle moved from reference position.
Definition: uStepper.cpp:657
uint32_t accelSteps
Definition: uStepper.h:444
void hardStop(bool holdMode)
Stop the motor without deceleration.
Definition: uStepper.cpp:1137
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:1242
void setMaxVelocity(float vel)
Sets the maximum rotational velocity of the motor.
Definition: uStepper.cpp:860
uStepperTemp temp
Definition: uStepper.h:521
void moveSteps(uint32_t steps, bool dir, bool holdMode)
Make the motor perform a predefined number of steps.
Definition: uStepper.cpp:984
float getMaxVelocity(void)
Returns the maximum rotational velocity of the motor.
Definition: uStepper.cpp:893
void softStop(bool holdMode)
Stop the motor with deceleration.
Definition: uStepper.cpp:1168
uint8_t status
Definition: uStepper.h:719
uint8_t detectMagnet(void)
Detect if magnet is present and within range.
Definition: uStepper.cpp:755
int64_t stepsSinceReset
Definition: uStepper.h:462
uStepperEncoder encoder
Definition: uStepper.h:522
void stopTimer(void)
Stops the timer for the stepper algorithm.
Definition: uStepper.cpp:1251
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:716
Prototype of class for the AS5600 encoder.
Definition: uStepper.h:322
void setHome(void)
Define new reference(home) position.
Definition: uStepper.cpp:719
uint16_t cruiseDelay
Definition: uStepper.h:438
float getAngle(void)
Measure the current shaft angle.
Definition: uStepper.cpp:732
volatile float oldAngle
Definition: uStepper.h:421
float velocity
Definition: uStepper.h:470
float2 exactDelay
Definition: uStepper.h:464
volatile float angleMoved
Definition: uStepper.h:424
uint8_t state
Definition: uStepper.h:442
uStepperTemp(void)
Constructor.
Definition: uStepper.cpp:632
uint16_t delay
Definition: uStepper.h:466
uint32_t decelSteps
Definition: uStepper.h:446
bool direction
Definition: uStepper.h:460
bool hold
Definition: uStepper.h:458
float encoderOffset
Definition: uStepper.h:420
void disableMotor(void)
Disables the stepper driver output stage.
Definition: uStepper.cpp:1261
int64_t getStepsSinceReset(void)
Get the number of steps applied since reset.
Definition: uStepper.cpp:1281
void setup(void)
Setup the encoder.
Definition: uStepper.cpp:700
uint32_t initialDecelSteps
Definition: uStepper.h:448