uStepper
uStepper.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * File: uStepper.h *
3 * Version: 1.0.0 *
4 * date: October 13th, 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 ********************************************************************************************/
163 #ifndef _USTEPPER_H_
164 #define _USTEPPER_H_
165 
166 #ifndef __AVR_ATmega328P__
167 #error !!This library only supports the ATmega328p MCU!!
168 #endif
169 
170 #include <inttypes.h>
171 #include <avr/io.h>
172 #include <Arduino.h>
173 #include <uStepperServo.h>
174 
175 #define FULL 1
176 #define HALF 2
177 #define QUARTER 4
178 #define EIGHT 8
179 #define SIXTEEN 16
181 #define NORMAL 0
182 #define DROPIN 1
183 #define PID 2
185 #define STOP 1
186 #define ACCEL 2
187 #define CRUISE 4
188 #define DECEL 8
189 #define INITDECEL 16
190 #define INTFREQ 28200.0f
191 #define INTPERIOD 1000000.0/INTFREQ
193 #define INTPIDDELAYCONSTANT 0.028199994
194 #define CW 0
195 #define CCW 1
196 #define HARD 1
197 #define SOFT 0
199 #define ENCODERINTFREQ 1000.0
200 #define ENCODERSPEEDCONSTANT ENCODERINTFREQ/10.0/4096.0
202 #define ENCODERADDR 0x36
204 #define ANGLE 0x0E
205 #define STATUS 0x0B
206 #define AGC 0x1A
207 #define MAGNITUDE 0x1B
209 #define R 4700.0
211 #define I2CFREE 0
213 #define READ 1
215 #define WRITE 0
217 #define START 0x08
219 #define REPSTART 0x10
221 #define TXADDRACK 0x18
223 #define TXDATAACK 0x28
225 #define RXADDRACK 0x40
227 #define ACK 1
229 #define NACK 0
248 #define A 0.001295752996237
249 #define B 0.000237488365866
250 #define C 0.000000083423218
258 extern "C" void interrupt0(void);
259 
266 extern "C" void interrupt1(void);
267 
278 extern "C" void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked,used));
279 
288 extern "C" void TIMER1_COMPA_vect(void) __attribute__ ((signal,used));
289 
290 class float2
291 {
292  public:
293  float2(void);
294  float getFloatValue(void);
295  uint64_t getRawValue(void);
296  void setValue(float val);
297  float2 & operator=(const float &value);
298  bool operator==(const float2 &value);
299  bool operator!=(const float2 &value);
300  bool operator>=(const float2 &value);
301  bool operator<=(const float2 &value);
302  bool operator<=(const float &value);
303  bool operator<(const float2 &value);
304  bool operator>(const float2 &value);
305  float2 & operator*=(const float2 &value);
306  float2 & operator-=(const float2 &value);
307  float2 & operator+=(const float2 &value);
308  float2 & operator+=(const float &value);
309  float2 & operator/=(const float2 &value);
310  const float2 operator+(const float2 &value);
311  const float2 operator-(const float2 &value);
312  const float2 operator*(const float2 &value);
313  const float2 operator/(const float2 &value);
314  uint64_t value;
315 
316  private:
317  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal,used));
318 
319 };
320 
331 {
332 public:
333 
346  uStepperTemp(void);
347 
360  float getTemp(void);
361 private:
362 };
363 
374 {
375 public:
376  volatile int32_t angleMoved;
377  uint16_t encoderOffset;
378  volatile float oldAngle;
379  volatile uint16_t angle;
380  volatile float curSpeed;
386  uStepperEncoder(void);
387 
400  float getAngle(void);
401 
412  float getSpeed(void);
413 
421  uint16_t getStrength(void);
422 
432  uint8_t getAgc(void);
433 
444  uint8_t detectMagnet(void);
445 
462  float getAngleMoved(void);
463 
472  void setup(uint8_t mode);
473 
480  void setHome(void);
481 
482 private:
483 
484  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal,used));
485 };
486 
495 class uStepper
496 {
497 private:
498  //Address offset: 0
499  uint16_t cruiseDelay;
502  //Address offset: 2
506  //Address offset: 10
507  uint8_t state;
510  //Address offset: 11
511  uint32_t accelSteps;
513  //Address offset: 15
514  uint32_t decelSteps;
516  //Address offset: 19
517  uint32_t initialDecelSteps;
519  //Address offset: 23
520  uint32_t cruiseSteps;
522  //Address offset: 27
523  uint32_t currentStep;
525  //Address offset: 31
526  uint32_t totalSteps;
528  //Address offset: 35
529  bool continous;
532  //Address offset: 36
533  bool hold;
537  //Address offset: 37
538  bool direction;
540  //Address offset: 38
541  int64_t stepsSinceReset;
546  //Address offset: 46
550  //Address offset: 54
551  uint16_t delay;
558  //Address offset: 56
559  bool dropIn;
560  //Address offset: 57
561  float velocity;
565  //Address offset: 61
566  float acceleration;
572  //address offset: 65
573  volatile float tolerance;
575  //address offset: 69
576  volatile float hysteresis;
578  //address offset: 73
579  volatile float stepConversion;
581  //address offset: 77
582  volatile uint16_t counter;
584  //address offset: 79
585  volatile int32_t stepCnt;
587  //address offset: 83
588  volatile int32_t control;
590  //address offset: 87
591  volatile uint32_t speedValue[2];
593  //address offset: 95
594  float pTerm;
596  //address offset: 99
597  float iTerm;
598  //address offset: 103
599  float dTerm;
601  //address offset: 107
602  uint8_t mode;
606  float angleToStep;
610  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked,used));
611  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal,used));
612  friend void interrupt1(void);
613 
622  void startTimer(void);
623 
624 
633  void stopTimer(void);
634 
635 
645  void enableMotor(void);
646 
647 
655  void disableMotor(void);
656 
661  void pidDropIn(void);
662 
667  void pid(void);
668 
669 public:
670  uStepperTemp temp;
671  uStepperEncoder encoder;
687  uStepper(float accel, float vel);
688 
689 
698  uStepper(void);
699 
700 
712  void setMaxAcceleration(float accel);
713 
714 
723  float getMaxAcceleration(void);
724 
725 
736  void setMaxVelocity(float vel);
737 
738 
748  float getMaxVelocity(void);
749 
750 
763  void runContinous(bool dir);
764 
765 
783  void moveSteps(uint32_t steps, bool dir, bool holdMode);
784 
785 
799  void hardStop(bool holdMode);
800 
801 
816  void softStop(bool holdMode);
817 
818 
852  void setup( uint8_t mode = NORMAL,
853  uint8_t microStepping = SIXTEEN,
854  float faultTolerance = 10.0,
855  float faultHysteresis = 5.0,
856  float pTerm = 1.0,
857  float iTerm = 0.02,
858  float dterm = 0.006);
859 
870  bool getCurrentDirection(void);
871 
872 
882  bool getMotorState(void);
883 
884 
906  int64_t getStepsSinceReset(void);
907 
919  void pwmD8(float duty);
920 
932  void pwmD3(float duty);
933 
943  void updateSetPoint(float setPoint);
944 };
945 
968 {
969  private:
970  uint8_t status;
981  void cmd(uint8_t cmd);
982 
983  public:
984 
994  i2cMaster(void);
995 
1013  bool readByte(bool ack, uint8_t *data);
1014 
1036  bool read(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
1037 
1055  bool start(uint8_t addr, bool RW);
1056 
1074  bool restart(uint8_t addr, bool RW);
1075 
1086  bool writeByte(uint8_t data);
1087 
1108  bool write(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
1109 
1120  bool stop(void);
1121 
1130  uint8_t getStatus(void);
1131 
1139  void begin(void);
1140 };
1141 
1142 extern i2cMaster I2C;
1144 #endif
uint32_t currentStep
Definition: uStepper.h:523
Prototype of class for the temperature sensor.
Definition: uStepper.h:330
void TIMER1_COMPA_vect(void) __attribute__((signal
Measures angle and speed of motor.
Definition: uStepper.cpp:395
volatile uint16_t counter
Definition: uStepper.h:582
float angleToStep
Definition: uStepper.h:606
volatile int32_t stepCnt
Definition: uStepper.h:585
float2 multiplier
Definition: uStepper.h:503
float acceleration
Definition: uStepper.h:566
bool continous
Definition: uStepper.h:529
Prototype of class for accessing all features of the uStepper in a single object. ...
Definition: uStepper.h:495
void TIMER2_COMPA_vect(void) __attribute__((signal
Used to apply step pulses to the motor.
Definition: uStepper.cpp:127
volatile float curSpeed
Definition: uStepper.h:380
bool dropIn
Definition: uStepper.h:559
float getTemp(void)
Request a reading of current temperature.
Definition: uStepper.cpp:791
uint32_t cruiseSteps
Definition: uStepper.h:520
uint32_t totalSteps
Definition: uStepper.h:526
#define C
Definition: uStepper.h:250
uint32_t accelSteps
Definition: uStepper.h:511
float iTerm
Definition: uStepper.h:597
uint8_t mode
Definition: uStepper.h:602
volatile int32_t angleMoved
Definition: uStepper.h:376
int64_t stepsSinceReset
Definition: uStepper.h:541
volatile float tolerance
Definition: uStepper.h:573
float pTerm
Definition: uStepper.h:594
Prototype of class for accessing the TWI (I2C) interface of the AVR (master mode only).
Definition: uStepper.h:967
volatile int32_t control
Definition: uStepper.h:588
Prototype of class for the AS5600 encoder.
Definition: uStepper.h:373
volatile float hysteresis
Definition: uStepper.h:576
#define NORMAL
Definition: uStepper.h:181
void interrupt1(void)
Used by dropin feature to take in enable signal.
Definition: uStepper.cpp:81
volatile float oldAngle
Definition: uStepper.h:378
uint16_t encoderOffset
Definition: uStepper.h:377
#define SIXTEEN
Definition: uStepper.h:179
float velocity
Definition: uStepper.h:561
float2 exactDelay
Definition: uStepper.h:547
float dTerm
Definition: uStepper.h:599
Function prototypes and definitions for the uStepper Servo library.
uint8_t state
Definition: uStepper.h:507
uStepperTemp(void)
Constructor.
Definition: uStepper.cpp:786
volatile float stepConversion
Definition: uStepper.h:579
uint16_t delay
Definition: uStepper.h:551
uint32_t decelSteps
Definition: uStepper.h:514
bool direction
Definition: uStepper.h:538
bool hold
Definition: uStepper.h:533
uint32_t initialDecelSteps
Definition: uStepper.h:517