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.3 *
4 * date: August 5th, 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 ********************************************************************************************/
153 #ifndef _USTEPPER_H_
154 #define _USTEPPER_H_
155 
156 #ifndef __AVR_ATmega328P__
157 #error !!This library only supports the ATmega328p MCU!!
158 #endif
159 
160 #include <inttypes.h>
161 #include <avr/io.h>
162 #include <Arduino.h>
163 
164 #define FULL 1
165 #define HALF 2
166 #define QUARTER 4
167 #define EIGHT 8
168 #define SIXTEEN 16
169 
170 #define NORMAL 0
171 #define DROPIN 1
172 
173 #define STOP 1
174 #define ACCEL 2
175 #define CRUISE 4
176 #define DECEL 8
177 #define INITDECEL 16
178 #define INTFREQ 28200.0f
179 #define CW 0
180 #define CCW 1
181 #define HARD 1
182 #define SOFT 0
184 #define ENCODERADDR 0x36
186 #define ANGLE 0x0E
187 #define STATUS 0x0B
188 #define AGC 0x1A
189 #define MAGNITUDE 0x1B
191 #define ENCODERINTFREQ 1000.0
192 #define ENCODERSPEEDCONSTANT ENCODERINTFREQ/10.0/360.0
194 #define R 4700.0
196 #define I2CFREE 0
198 #define READ 1
200 #define WRITE 0
202 #define START 0x08
204 #define REPSTART 0x10
206 #define TXADDRACK 0x18
208 #define TXDATAACK 0x28
210 #define RXADDRACK 0x40
212 #define ACK 1
214 #define NACK 0
232 #define A 0.001295752996237
233 #define B 0.000237488365866
234 #define C 0.000000083423218
236 #define ALPHA 0.85
237 #define BETA (1.0 - ALPHA)
238 
239 
240 extern "C" void INT0_vect(void) __attribute__ ((signal));
241 extern "C" void INT1_vect(void) __attribute__ ((signal));
242 extern "C" void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked));
243 extern "C" void TIMER1_COMPA_vect(void) __attribute__ ((signal));
244 
245 class float2
246 {
247  public:
248  float2(void);
249  float getFloatValue(void);
250  uint64_t getRawValue(void);
251  void setValue(float val);
252  float2 & operator=(const float &value);
253  bool operator==(const float2 &value);
254  bool operator!=(const float2 &value);
255  bool operator>=(const float2 &value);
256  bool operator<=(const float2 &value);
257  bool operator<=(const float &value);
258  bool operator<(const float2 &value);
259  bool operator>(const float2 &value);
260  float2 & operator*=(const float2 &value);
261  float2 & operator-=(const float2 &value);
262  float2 & operator+=(const float2 &value);
263  float2 & operator+=(const float &value);
264  float2 & operator/=(const float2 &value);
265  const float2 operator+(const float2 &value);
266  const float2 operator-(const float2 &value);
267  const float2 operator*(const float2 &value);
268  const float2 operator/(const float2 &value);
269  uint64_t value;
270 
271  private:
272  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal));
273 
274 };
275 
286 {
287 public:
288 
301  uStepperTemp(void);
302 
313  float getTemp(void);
314 private:
315 };
316 
325 {
326 public:
332  uStepperEncoder(void);
333 
342  float getAngle(void);
343 
353  float getSpeed(void);
354 
363  uint16_t getStrength(void);
364 
373  uint8_t getAgc(void);
374 
385  uint8_t detectMagnet(void);
386 
400  float getAngleMoved(void);
401 
408  void setup(void);
409 
416  void setHome(void);
417 
418 private:
419 
420  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal));
421 
423  volatile float oldAngle;
424  volatile float angle;
425  volatile float curSpeed;
426  volatile float angleMoved;
427 };
428 
436 class uStepper
437 {
438 private:
439  //Address offset: 0
440  uint16_t cruiseDelay;
441  //Address offset: 2
443  //Address offset: 10
444  uint8_t state;
445  //Address offset: 11
446  uint32_t accelSteps;
447  //Address offset: 15
448  uint32_t decelSteps;
449  //Address offset: 19
450  uint32_t initialDecelSteps;
451  //Address offset: 23
452  uint32_t cruiseSteps;
453  //Address offset: 27
454  uint32_t currentStep;
455  //Address offset: 31
456  uint32_t totalSteps;
457  //Address offset: 35
458  bool continous;
459  //Address offset: 36
460  bool hold;
461  //Address offset: 37
462  bool direction;
463  //Address offset: 38
464  int64_t stepsSinceReset;
465  //Address offset: 46
467  //Address offset: 54
468  uint16_t delay;
469  //Address offset: 56
470  bool dropIn;
471  //Address offset: 57
472  float velocity;
473  //Address offset: 61
474  float acceleration;
475  volatile uint16_t faultStepDelay;
476  volatile float tolerance;
477  volatile float stepResolution;
478 
479 
480  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked));
481  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal));
482 
483 
491  void startTimer(void);
492 
493 
501  void stopTimer(void);
502 
503 
511  void enableMotor(void);
512 
513 
520  void disableMotor(void);
521 
522 public:
537  uStepper(float accel, float vel);
538 
539 
547  uStepper(void);
548 
549 
560  void setMaxAcceleration(float accel);
561 
562 
571  float getMaxAcceleration(void);
572 
573 
584  void setMaxVelocity(float vel);
585 
586 
596  float getMaxVelocity(void);
597 
598 
609  void runContinous(bool dir);
610 
611 
625  void moveSteps(uint32_t steps, bool dir, bool holdMode);
626 
627 
638  void hardStop(bool holdMode);
639 
640 
651  void softStop(bool holdMode);
652 
653 
662  void setup(bool mode = NORMAL, uint8_t microStepping = SIXTEEN, float faultSpeed = 3000.0, uint32_t faultTolerance = 20);
663 
664 
674  bool getCurrentDirection(void);
675 
676 
686  bool getMotorState(void);
687 
688 
702  int64_t getStepsSinceReset(void);
703 };
704 
719 {
720  private:
721  uint8_t status;
732  void cmd(uint8_t cmd);
733 
734  public:
735 
743  i2cMaster(void);
744 
755  bool readByte(bool ack, uint8_t *data);
756 
770  bool read(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
771 
784  bool start(uint8_t addr, bool RW);
785 
798  bool restart(uint8_t addr, bool RW);
799 
810  bool writeByte(uint8_t data);
811 
825  bool write(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
826 
835  bool stop(void);
836 
845  uint8_t getStatus(void);
846 
853  void begin(void);
854 };
855 
856 extern i2cMaster I2C;
858 #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:454
Prototype of class for the temperature sensor.
Definition: uStepper.h:285
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:1272
float2 multiplier
Definition: uStepper.h:442
float acceleration
Definition: uStepper.h:474
bool continous
Definition: uStepper.h:458
Prototype of class for accessing all features of the uStepper in a single object. ...
Definition: uStepper.h:436
i2cMaster I2C
Definition: uStepper.cpp:73
volatile float curSpeed
Definition: uStepper.h:425
void enableMotor(void)
Enables the stepper driver output stage.
Definition: uStepper.cpp:1257
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:452
uint32_t totalSteps
Definition: uStepper.h:456
#define C
Definition: uStepper.h:234
bool getCurrentDirection(void)
Returns the direction the motor is currently configured to rotate.
Definition: uStepper.cpp:1267
float getAngleMoved(void)
Measure the angle moved from reference position.
Definition: uStepper.cpp:657
uint32_t accelSteps
Definition: uStepper.h:446
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:1243
void setMaxVelocity(float vel)
Sets the maximum rotational velocity of the motor.
Definition: uStepper.cpp:860
uStepperTemp temp
Definition: uStepper.h:523
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 softStop(bool holdMode)
Stop the motor with deceleration.
Definition: uStepper.cpp:1169
uint8_t status
Definition: uStepper.h:721
uint8_t detectMagnet(void)
Detect if magnet is present and within range.
Definition: uStepper.cpp:755
int64_t stepsSinceReset
Definition: uStepper.h:464
uStepperEncoder encoder
Definition: uStepper.h:524
void stopTimer(void)
Stops the timer for the stepper algorithm.
Definition: uStepper.cpp:1252
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:718
Prototype of class for the AS5600 encoder.
Definition: uStepper.h:324
void setHome(void)
Define new reference(home) position.
Definition: uStepper.cpp:719
uint16_t cruiseDelay
Definition: uStepper.h:440
float getAngle(void)
Measure the current shaft angle.
Definition: uStepper.cpp:732
volatile float oldAngle
Definition: uStepper.h:423
float velocity
Definition: uStepper.h:472
float2 exactDelay
Definition: uStepper.h:466
volatile float angleMoved
Definition: uStepper.h:426
uint8_t state
Definition: uStepper.h:444
uStepperTemp(void)
Constructor.
Definition: uStepper.cpp:632
uint16_t delay
Definition: uStepper.h:468
uint32_t decelSteps
Definition: uStepper.h:448
bool direction
Definition: uStepper.h:462
bool hold
Definition: uStepper.h:460
float encoderOffset
Definition: uStepper.h:422
void disableMotor(void)
Disables the stepper driver output stage.
Definition: uStepper.cpp:1262
int64_t getStepsSinceReset(void)
Get the number of steps applied since reset.
Definition: uStepper.cpp:1282
void setup(void)
Setup the encoder.
Definition: uStepper.cpp:700
uint32_t initialDecelSteps
Definition: uStepper.h:450