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.3.0 *
4 * date: May 7th, 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 ********************************************************************************************/
134 #ifndef _USTEPPER_H_
135 #define _USTEPPER_H_
136 
137 #ifndef __AVR_ATmega328P__
138 #error !!This library only supports the ATmega328p MCU!!
139 #endif
140 
141 #include <inttypes.h>
142 #include <avr/io.h>
143 
144 #define STOP 1
145 #define ACCEL 2
146 #define CRUISE 4
147 #define DECEL 8
148 #define INITDECEL 16
149 #define INTFREQ 32830.0f
150 #define CW 0
151 #define CCW 1
152 #define HARD 1
153 #define SOFT 0
155 #define ENCODERADDR 0x36
157 #define ANGLE 0x0E
158 #define STATUS 0x0B
159 #define AGC 0x1A
160 #define MAGNITUDE 0x1B
162 #define ENCODERINTFREQ 244.3
163 #define ENCODERSPEEDCONSTANT ENCODERINTFREQ/360.0
165 #define R 4700.0
167 #define I2CFREE 0
169 #define READ 1
171 #define WRITE 0
173 #define START 0x08
175 #define REPSTART 0x10
177 #define TXADDRACK 0x18
179 #define TXDATAACK 0x28
181 #define RXADDRACK 0x40
183 #define ACK 1
185 #define NACK 0
203 #define A 0.001295752996237
204 #define B 0.000237488365866
205 #define C 0.000000083423218
212 extern "C" void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked));
213 
219 extern "C" void TIMER1_COMPA_vect(void) __attribute__ ((signal));
220 
231 {
232 public:
233 
246  uStepperTemp(void);
247 
258  float getTemp(void);
259 private:
260 };
261 
270 {
271 public:
277  uStepperEncoder(void);
278 
287  float getAngle(void);
288 
298  float getSpeed(void);
299 
308  uint16_t getStrength(void);
309 
318  uint8_t getAgc(void);
319 
330  uint8_t detectMagnet(void);
331 
345  float getAngleMoved(void);
346 
353  void setup(void);
354 
361  void setHome(void);
362 
363 private:
364 
365  friend void TIMER1_COMPA_vect(void) __attribute__ ((signal));
366 
368  volatile float oldAngle;
369  volatile float curSpeed;
370  volatile float angleMoved;
371 };
372 
380 class uStepper
381 {
382 private:
383  //Address offset: 0
384  uint16_t cruiseDelay;
385  //Address offset: 2
386  float velocity;
387  //Address offset: 6
388  float acceleration;
389  //Address offset: 10
390  uint16_t delay;
391  //Address offset: 12
392  float multiplier;
393  //Address offset: 16
395  //Address offset: 20
396  uint8_t state;
397  //Address offset: 21
398  uint32_t accelSteps;
399  //Address offset: 25
400  uint32_t decelSteps;
401  //Address offset: 29
402  uint32_t initialDecelSteps;
403  //Address offset: 33
404  uint32_t cruiseSteps;
405  //Address offset: 37
406  uint32_t currentStep;
407  //Address offset: 41
408  uint32_t totalSteps;
409  //Address offset: 45
410  bool continous;
411  //Address offset: 46
412  bool hold;
413  //Address offset: 47
414  bool direction;
415  //Address offset: 48
416  int64_t stepsSinceReset;
417  //Address offset: 56
418  float exactDelay;
420  friend void TIMER2_COMPA_vect(void) __attribute__ ((signal,naked));
421 
422 
430  void startTimer(void);
431 
432 
440  void stopTimer(void);
441 
442 
450  void enableMotor(void);
451 
452 
459  void disableMotor(void);
460 
461 public:
476  uStepper(float accel, float vel);
477 
478 
486  uStepper(void);
487 
488 
499  void setMaxAcceleration(float accel);
500 
501 
510  float getMaxAcceleration(void);
511 
512 
523  void setMaxVelocity(float vel);
524 
525 
535  float getMaxVelocity(void);
536 
537 
548  void runContinous(bool dir);
549 
550 
564  void moveSteps(uint32_t steps, bool dir, bool holdMode);
565 
566 
577  void hardStop(bool holdMode);
578 
579 
590  void softStop(bool holdMode);
591 
592 
601  void setup(void);
602 
603 
613  bool getCurrentDirection(void);
614 
615 
625  bool getMotorState(void);
626 
627 
641  int64_t getStepsSinceReset(void);
642 };
643 
658 {
659  private:
660  uint8_t status;
671  void cmd(uint8_t cmd);
672 
673  public:
674 
682  i2cMaster(void);
683 
694  bool readByte(bool ack, uint8_t *data);
695 
709  bool read(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
710 
723  bool start(uint8_t addr, bool RW);
724 
737  bool restart(uint8_t addr, bool RW);
738 
749  bool writeByte(uint8_t data);
750 
764  bool write(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data);
765 
774  bool stop(void);
775 
784  uint8_t getStatus(void);
785 
792  void begin(void);
793 };
794 
795 extern i2cMaster I2C;
797 #endif
uStepperEncoder(void)
Constructor.
Definition: uStepper.cpp:171
uint32_t currentStep
Definition: uStepper.h:406
Prototype of class for the temperature sensor.
Definition: uStepper.h:230
uint16_t getStrength(void)
Measure the strength of the magnet.
Definition: uStepper.cpp:251
float getSpeed(void)
Measure the current speed of the motor.
Definition: uStepper.cpp:207
bool getMotorState(void)
Get the current state of the motor.
Definition: uStepper.cpp:712
float acceleration
Definition: uStepper.h:388
bool continous
Definition: uStepper.h:410
Prototype of class for accessing all features of the uStepper in a single object. ...
Definition: uStepper.h:380
i2cMaster I2C
Definition: uStepper.cpp:82
volatile float curSpeed
Definition: uStepper.h:369
void enableMotor(void)
Enables the stepper driver output stage.
Definition: uStepper.cpp:697
float getMaxAcceleration(void)
Get the value of the maximum motor acceleration.
Definition: uStepper.cpp:342
float exactDelay
Definition: uStepper.h:418
float exactDelayDecel
Definition: uStepper.h:394
uint32_t cruiseSteps
Definition: uStepper.h:404
uint32_t totalSteps
Definition: uStepper.h:408
bool getCurrentDirection(void)
Returns the direction the motor is currently configured to rotate.
Definition: uStepper.cpp:707
float getAngleMoved(void)
Measure the angle moved from reference position.
Definition: uStepper.cpp:176
uint32_t accelSteps
Definition: uStepper.h:398
void hardStop(bool holdMode)
Stop the motor without deceleration.
Definition: uStepper.cpp:603
void setMaxAcceleration(float accel)
Set the maximum acceleration of the stepper motor.
Definition: uStepper.cpp:323
void startTimer(void)
Starts timer for stepper algorithm.
Definition: uStepper.cpp:683
void setMaxVelocity(float vel)
Sets the maximum rotational velocity of the motor.
Definition: uStepper.cpp:347
uStepperTemp temp
Definition: uStepper.h:462
void moveSteps(uint32_t steps, bool dir, bool holdMode)
Make the motor perform a predefined number of steps.
Definition: uStepper.cpp:458
float getMaxVelocity(void)
Returns the maximum rotational velocity of the motor.
Definition: uStepper.cpp:380
float multiplier
Definition: uStepper.h:392
void softStop(bool holdMode)
Stop the motor with deceleration.
Definition: uStepper.cpp:629
uint8_t status
Definition: uStepper.h:660
uint8_t detectMagnet(void)
Detect if magnet is present and within range.
Definition: uStepper.cpp:269
int64_t stepsSinceReset
Definition: uStepper.h:416
uStepperEncoder encoder
Definition: uStepper.h:463
void stopTimer(void)
Stops the timer for the stepper algorithm.
Definition: uStepper.cpp:692
uint8_t getAgc(void)
Read the current AGC value of the encoder chip.
Definition: uStepper.cpp:260
void runContinous(bool dir)
Make the motor rotate continuously.
Definition: uStepper.cpp:385
Prototype of class for accessing the TWI (I2C) interface of the AVR (master mode only).
Definition: uStepper.h:657
Prototype of class for the AS5600 encoder.
Definition: uStepper.h:269
void setHome(void)
Define new reference(home) position.
Definition: uStepper.cpp:229
uint16_t cruiseDelay
Definition: uStepper.h:384
float getAngle(void)
Measure the current shaft angle.
Definition: uStepper.cpp:239
volatile float oldAngle
Definition: uStepper.h:368
float velocity
Definition: uStepper.h:386
volatile float angleMoved
Definition: uStepper.h:370
void setup(void)
Initializes the different parts of the uStepper object.
Definition: uStepper.cpp:672
uint8_t state
Definition: uStepper.h:396
uint16_t delay
Definition: uStepper.h:390
uint32_t decelSteps
Definition: uStepper.h:400
bool direction
Definition: uStepper.h:414
bool hold
Definition: uStepper.h:412
float encoderOffset
Definition: uStepper.h:367
void disableMotor(void)
Disables the stepper driver output stage.
Definition: uStepper.cpp:702
int64_t getStepsSinceReset(void)
Get the number of steps applied since reset.
Definition: uStepper.cpp:722
void setup(void)
Setup the encoder.
Definition: uStepper.cpp:212
uint32_t initialDecelSteps
Definition: uStepper.h:402