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.4 *
4 * date: August 8th, 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 ********************************************************************************************/
156 #ifndef _USTEPPER_H_
157 #define _USTEPPER_H_
158 
159 #ifndef __AVR_ATmega328P__
160 #error !!This library only supports the ATmega328p MCU!!
161 #endif
162 
163 #include <inttypes.h>
164 #include <avr/io.h>
165 #include <Arduino.h>
166 
167 #define FULL 1
168 #define HALF 2
169 #define QUARTER 4
170 #define EIGHT 8
171 #define SIXTEEN 16
172 
173 #define NORMAL 0
174 #define DROPIN 1
175 
176 #define STOP 1
177 #define ACCEL 2
178 #define CRUISE 4
179 #define DECEL 8
180 #define INITDECEL 16
181 #define INTFREQ 28200.0f
182 #define CW 0
183 #define CCW 1
184 #define HARD 1
185 #define SOFT 0
187 #define ENCODERADDR 0x36
189 #define ANGLE 0x0E
190 #define STATUS 0x0B
191 #define AGC 0x1A
192 #define MAGNITUDE 0x1B
194 #define ENCODERINTFREQ 1000.0
195 #define ENCODERSPEEDCONSTANT ENCODERINTFREQ/10.0/360.0
197 #define R 4700.0
199 #define I2CFREE 0
201 #define READ 1
203 #define WRITE 0
205 #define START 0x08
207 #define REPSTART 0x10
209 #define TXADDRACK 0x18
211 #define TXDATAACK 0x28
213 #define RXADDRACK 0x40
215 #define ACK 1
217 #define NACK 0
235 #define A 0.001295752996237
236 #define B 0.000237488365866
237 #define C 0.000000083423218
239 #define ALPHA 0.85
240 #define BETA (1.0 - ALPHA)
241 
242 
243 extern "C" void INT0_vect(void) __attribute__ ((signal,used));
244 extern "C" void INT1_vect(void) __attribute__ ((signal,used));
245 extern "C" void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked,used));
246 extern "C" void TIMER1_COMPA_vect(void) __attribute__ ((signal,used));
247 
248 class float2
249 {
250  public:
251  float2(void);
252  float getFloatValue(void);
253  uint64_t getRawValue(void);
254  void setValue(float val);
255  float2 & operator=(const float &value);
256  bool operator==(const float2 &value);
257  bool operator!=(const float2 &value);
258  bool operator>=(const float2 &value);
259  bool operator<=(const float2 &value);
260  bool operator<=(const float &value);
261  bool operator<(const float2 &value);
262  bool operator>(const float2 &value);
263  float2 & operator*=(const float2 &value);
264  float2 & operator-=(const float2 &value);
265  float2 & operator+=(const float2 &value);
266  float2 & operator+=(const float &value);
267  float2 & operator/=(const float2 &value);
268  const float2 operator+(const float2 &value);
269  const float2 operator-(const float2 &value);
270  const float2 operator*(const float2 &value);
271  const float2 operator/(const float2 &value);
272  uint64_t value;
273 
274  private:
275  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal,used));
276 
277 };
278 
289 {
290 public:
291 
304  uStepperTemp(void);
305 
316  float getTemp(void);
317 private:
318 };
319 
328 {
329 public:
335  uStepperEncoder(void);
336 
345  float getAngle(void);
346 
356  float getSpeed(void);
357 
366  uint16_t getStrength(void);
367 
376  uint8_t getAgc(void);
377 
388  uint8_t detectMagnet(void);
389 
403  float getAngleMoved(void);
404 
411  void setup(void);
412 
419  void setHome(void);
420 
421 private:
422 
423  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal,used));
424 
426  volatile float oldAngle;
427  volatile float angle;
428  volatile float curSpeed;
429  volatile float angleMoved;
430 };
431 
439 class uStepper
440 {
441 private:
442  //Address offset: 0
443  uint16_t cruiseDelay;
444  //Address offset: 2
446  //Address offset: 10
447  uint8_t state;
448  //Address offset: 11
449  uint32_t accelSteps;
450  //Address offset: 15
451  uint32_t decelSteps;
452  //Address offset: 19
453  uint32_t initialDecelSteps;
454  //Address offset: 23
455  uint32_t cruiseSteps;
456  //Address offset: 27
457  uint32_t currentStep;
458  //Address offset: 31
459  uint32_t totalSteps;
460  //Address offset: 35
461  bool continous;
462  //Address offset: 36
463  bool hold;
464  //Address offset: 37
465  bool direction;
466  //Address offset: 38
467  int64_t stepsSinceReset;
468  //Address offset: 46
470  //Address offset: 54
471  uint16_t delay;
472  //Address offset: 56
473  bool dropIn;
474  //Address offset: 57
475  float velocity;
476  //Address offset: 61
477  float acceleration;
478  volatile uint16_t faultStepDelay;
479  volatile float tolerance;
480  volatile float stepResolution;
481 
482 
483  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked,used));
484  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal,used));
485 
486 
494  void startTimer(void);
495 
496 
504  void stopTimer(void);
505 
506 
514  void enableMotor(void);
515 
516 
523  void disableMotor(void);
524 
525 public:
540  uStepper(float accel, float vel);
541 
542 
550  uStepper(void);
551 
552 
563  void setMaxAcceleration(float accel);
564 
565 
574  float getMaxAcceleration(void);
575 
576 
587  void setMaxVelocity(float vel);
588 
589 
599  float getMaxVelocity(void);
600 
601 
612  void runContinous(bool dir);
613 
614 
628  void moveSteps(uint32_t steps, bool dir, bool holdMode);
629 
630 
641  void hardStop(bool holdMode);
642 
643 
654  void softStop(bool holdMode);
655 
656 
676  void setup(bool mode = NORMAL, uint8_t microStepping = SIXTEEN, float faultSpeed = 3000.0, uint32_t faultTolerance = 20);
677 
678 
688  bool getCurrentDirection(void);
689 
690 
700  bool getMotorState(void);
701 
702 
716  int64_t getStepsSinceReset(void);
717 };
718 
733 {
734  private:
735  uint8_t status;
746  void cmd(uint8_t cmd);
747 
748  public:
749 
757  i2cMaster(void);
758 
769  bool readByte(bool ack, uint8_t *data);
770 
784  bool read(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
785 
798  bool start(uint8_t addr, bool RW);
799 
812  bool restart(uint8_t addr, bool RW);
813 
824  bool writeByte(uint8_t data);
825 
839  bool write(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
840 
849  bool stop(void);
850 
859  uint8_t getStatus(void);
860 
867  void begin(void);
868 };
869 
870 extern i2cMaster I2C;
872 #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:457
Prototype of class for the temperature sensor.
Definition: uStepper.h:288
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:445
float acceleration
Definition: uStepper.h:477
bool continous
Definition: uStepper.h:461
Prototype of class for accessing all features of the uStepper in a single object. ...
Definition: uStepper.h:439
i2cMaster I2C
Definition: uStepper.cpp:73
volatile float curSpeed
Definition: uStepper.h:428
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:455
uint32_t totalSteps
Definition: uStepper.h:459
#define C
Definition: uStepper.h:237
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:449
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:526
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:735
uint8_t detectMagnet(void)
Detect if magnet is present and within range.
Definition: uStepper.cpp:755
int64_t stepsSinceReset
Definition: uStepper.h:467
uStepperEncoder encoder
Definition: uStepper.h:527
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:732
Prototype of class for the AS5600 encoder.
Definition: uStepper.h:327
void setHome(void)
Define new reference(home) position.
Definition: uStepper.cpp:719
uint16_t cruiseDelay
Definition: uStepper.h:443
float getAngle(void)
Measure the current shaft angle.
Definition: uStepper.cpp:732
volatile float oldAngle
Definition: uStepper.h:426
float velocity
Definition: uStepper.h:475
float2 exactDelay
Definition: uStepper.h:469
volatile float angleMoved
Definition: uStepper.h:429
uint8_t state
Definition: uStepper.h:447
uStepperTemp(void)
Constructor.
Definition: uStepper.cpp:632
uint16_t delay
Definition: uStepper.h:471
uint32_t decelSteps
Definition: uStepper.h:451
bool direction
Definition: uStepper.h:465
bool hold
Definition: uStepper.h:463
float encoderOffset
Definition: uStepper.h:425
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:453