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.0 *
4 * date: July 30th, 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 ********************************************************************************************/
144 #ifndef _USTEPPER_H_
145 #define _USTEPPER_H_
146 
147 #ifndef __AVR_ATmega328P__
148 #error !!This library only supports the ATmega328p MCU!!
149 #endif
150 
151 #include <inttypes.h>
152 #include <avr/io.h>
153 #include <Arduino.h>
154 
155 #define FULL 1
156 #define HALF 2
157 #define QUARTER 4
158 #define EIGHT 8
159 #define SIXTEEN 16
160 
161 #define NORMAL 0
162 #define DROPIN 1
163 
164 #define STOP 1
165 #define ACCEL 2
166 #define CRUISE 4
167 #define DECEL 8
168 #define INITDECEL 16
169 #define INTFREQ 28200.0f
170 #define CW 0
171 #define CCW 1
172 #define HARD 1
173 #define SOFT 0
175 #define ENCODERADDR 0x36
177 #define ANGLE 0x0E
178 #define STATUS 0x0B
179 #define AGC 0x1A
180 #define MAGNITUDE 0x1B
182 #define ENCODERINTFREQ 1000.0
183 #define ENCODERSPEEDCONSTANT ENCODERINTFREQ/10.0/360.0
185 #define R 4700.0
187 #define I2CFREE 0
189 #define READ 1
191 #define WRITE 0
193 #define START 0x08
195 #define REPSTART 0x10
197 #define TXADDRACK 0x18
199 #define TXDATAACK 0x28
201 #define RXADDRACK 0x40
203 #define ACK 1
205 #define NACK 0
223 #define A 0.001295752996237
224 #define B 0.000237488365866
225 #define C 0.000000083423218
227 #define ALPHA 0.85
228 #define BETA (1.0 - ALPHA)
229 
230 
231 extern "C" void INT0_vect(void) __attribute__ ((signal));
232 extern "C" void INT1_vect(void) __attribute__ ((signal));
233 extern "C" void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked));
234 extern "C" void TIMER1_COMPA_vect(void) __attribute__ ((signal));
235 
236 class float2
237 {
238  public:
239  float2(void);
240  float getFloatValue(void);
241  uint64_t getRawValue(void);
242  void setValue(float val);
243  float2 & operator=(const float &value);
244  bool operator==(const float2 &value);
245  bool operator!=(const float2 &value);
246  bool operator>=(const float2 &value);
247  bool operator<=(const float2 &value);
248  bool operator<=(const float &value);
249  bool operator<(const float2 &value);
250  bool operator>(const float2 &value);
251  float2 & operator*=(const float2 &value);
252  float2 & operator-=(const float2 &value);
253  float2 & operator+=(const float2 &value);
254  float2 & operator+=(const float &value);
255  float2 & operator/=(const float2 &value);
256  const float2 operator+(const float2 &value);
257  const float2 operator-(const float2 &value);
258  const float2 operator*(const float2 &value);
259  const float2 operator/(const float2 &value);
260  uint64_t value;
261 
262  private:
263  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal));
264 
265 };
266 
277 {
278 public:
279 
292  uStepperTemp(void);
293 
304  float getTemp(void);
305 private:
306 };
307 
316 {
317 public:
323  uStepperEncoder(void);
324 
333  float getAngle(void);
334 
344  float getSpeed(void);
345 
354  uint16_t getStrength(void);
355 
364  uint8_t getAgc(void);
365 
376  uint8_t detectMagnet(void);
377 
391  float getAngleMoved(void);
392 
399  void setup(void);
400 
407  void setHome(void);
408 
409 private:
410 
411  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal));
412 
414  volatile float oldAngle;
415  volatile float angle;
416  volatile float curSpeed;
417  volatile float angleMoved;
418 };
419 
427 class uStepper
428 {
429 private:
430  //Address offset: 0
431  uint16_t cruiseDelay;
432  //Address offset: 2
434  //Address offset: 10
435  uint8_t state;
436  //Address offset: 11
437  uint32_t accelSteps;
438  //Address offset: 15
439  uint32_t decelSteps;
440  //Address offset: 19
441  uint32_t initialDecelSteps;
442  //Address offset: 23
443  uint32_t cruiseSteps;
444  //Address offset: 27
445  uint32_t currentStep;
446  //Address offset: 31
447  uint32_t totalSteps;
448  //Address offset: 35
449  bool continous;
450  //Address offset: 36
451  bool hold;
452  //Address offset: 37
453  bool direction;
454  //Address offset: 38
455  int64_t stepsSinceReset;
456  //Address offset: 46
458  //Address offset: 54
459  uint16_t delay;
460  //Address offset: 56
461  bool dropIn;
462  //Address offset: 57
463  float velocity;
464  //Address offset: 61
465  float acceleration;
466  volatile uint16_t faultStepDelay;
467  volatile float tolerance;
468  volatile float stepResolution;
469 
470 
471  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked));
472  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal));
473 
474 
482  void startTimer(void);
483 
484 
492  void stopTimer(void);
493 
494 
502  void enableMotor(void);
503 
504 
511  void disableMotor(void);
512 
513 public:
528  uStepper(float accel, float vel);
529 
530 
538  uStepper(void);
539 
540 
551  void setMaxAcceleration(float accel);
552 
553 
562  float getMaxAcceleration(void);
563 
564 
575  void setMaxVelocity(float vel);
576 
577 
587  float getMaxVelocity(void);
588 
589 
600  void runContinous(bool dir);
601 
602 
616  void moveSteps(uint32_t steps, bool dir, bool holdMode);
617 
618 
629  void hardStop(bool holdMode);
630 
631 
642  void softStop(bool holdMode);
643 
644 
653  void setup(bool mode = NORMAL, uint8_t microStepping = SIXTEEN, float faultSpeed = 3000.0, uint32_t faultTolerance = 20);
654 
655 
665  bool getCurrentDirection(void);
666 
667 
677  bool getMotorState(void);
678 
679 
693  int64_t getStepsSinceReset(void);
694 };
695 
710 {
711  private:
712  uint8_t status;
723  void cmd(uint8_t cmd);
724 
725  public:
726 
734  i2cMaster(void);
735 
746  bool readByte(bool ack, uint8_t *data);
747 
761  bool read(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
762 
775  bool start(uint8_t addr, bool RW);
776 
789  bool restart(uint8_t addr, bool RW);
790 
801  bool writeByte(uint8_t data);
802 
816  bool write(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
817 
826  bool stop(void);
827 
836  uint8_t getStatus(void);
837 
844  void begin(void);
845 };
846 
847 extern i2cMaster I2C;
849 #endif
uStepperEncoder(void)
Constructor.
Definition: uStepper.cpp:651
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:1208
uint32_t currentStep
Definition: uStepper.h:445
Prototype of class for the temperature sensor.
Definition: uStepper.h:276
uint16_t getStrength(void)
Measure the strength of the magnet.
Definition: uStepper.cpp:730
float getSpeed(void)
Measure the current speed of the motor.
Definition: uStepper.cpp:694
bool getMotorState(void)
Get the current state of the motor.
Definition: uStepper.cpp:1263
float2 multiplier
Definition: uStepper.h:433
float acceleration
Definition: uStepper.h:465
bool continous
Definition: uStepper.h:449
Prototype of class for accessing all features of the uStepper in a single object. ...
Definition: uStepper.h:427
i2cMaster I2C
Definition: uStepper.cpp:73
volatile float curSpeed
Definition: uStepper.h:416
void enableMotor(void)
Enables the stepper driver output stage.
Definition: uStepper.cpp:1248
float getMaxAcceleration(void)
Get the value of the maximum motor acceleration.
Definition: uStepper.cpp:823
float getTemp(void)
Request a reading of current temperature.
Definition: uStepper.cpp:636
uint32_t cruiseSteps
Definition: uStepper.h:443
uint32_t totalSteps
Definition: uStepper.h:447
#define C
Definition: uStepper.h:225
bool getCurrentDirection(void)
Returns the direction the motor is currently configured to rotate.
Definition: uStepper.cpp:1258
float getAngleMoved(void)
Measure the angle moved from reference position.
Definition: uStepper.cpp:656
uint32_t accelSteps
Definition: uStepper.h:437
void hardStop(bool holdMode)
Stop the motor without deceleration.
Definition: uStepper.cpp:1129
void setMaxAcceleration(float accel)
Set the maximum acceleration of the stepper motor.
Definition: uStepper.cpp:803
void startTimer(void)
Starts timer for stepper algorithm.
Definition: uStepper.cpp:1234
void setMaxVelocity(float vel)
Sets the maximum rotational velocity of the motor.
Definition: uStepper.cpp:853
uStepperTemp temp
Definition: uStepper.h:514
void moveSteps(uint32_t steps, bool dir, bool holdMode)
Make the motor perform a predefined number of steps.
Definition: uStepper.cpp:976
float getMaxVelocity(void)
Returns the maximum rotational velocity of the motor.
Definition: uStepper.cpp:886
void softStop(bool holdMode)
Stop the motor with deceleration.
Definition: uStepper.cpp:1160
uint8_t status
Definition: uStepper.h:712
uint8_t detectMagnet(void)
Detect if magnet is present and within range.
Definition: uStepper.cpp:748
int64_t stepsSinceReset
Definition: uStepper.h:455
uStepperEncoder encoder
Definition: uStepper.h:515
void stopTimer(void)
Stops the timer for the stepper algorithm.
Definition: uStepper.cpp:1243
uint8_t getAgc(void)
Read the current AGC value of the encoder chip.
Definition: uStepper.cpp:739
void runContinous(bool dir)
Make the motor rotate continuously.
Definition: uStepper.cpp:891
Prototype of class for accessing the TWI (I2C) interface of the AVR (master mode only).
Definition: uStepper.h:709
Prototype of class for the AS5600 encoder.
Definition: uStepper.h:315
void setHome(void)
Define new reference(home) position.
Definition: uStepper.cpp:716
uint16_t cruiseDelay
Definition: uStepper.h:431
float getAngle(void)
Measure the current shaft angle.
Definition: uStepper.cpp:725
volatile float oldAngle
Definition: uStepper.h:414
float velocity
Definition: uStepper.h:463
float2 exactDelay
Definition: uStepper.h:457
volatile float angleMoved
Definition: uStepper.h:417
uint8_t state
Definition: uStepper.h:435
uStepperTemp(void)
Constructor.
Definition: uStepper.cpp:631
uint16_t delay
Definition: uStepper.h:459
uint32_t decelSteps
Definition: uStepper.h:439
bool direction
Definition: uStepper.h:453
bool hold
Definition: uStepper.h:451
float encoderOffset
Definition: uStepper.h:413
void disableMotor(void)
Disables the stepper driver output stage.
Definition: uStepper.cpp:1253
int64_t getStepsSinceReset(void)
Get the number of steps applied since reset.
Definition: uStepper.cpp:1273
void setup(void)
Setup the encoder.
Definition: uStepper.cpp:699
uint32_t initialDecelSteps
Definition: uStepper.h:441