uStepper S
uStepperEncoder.cpp
Go to the documentation of this file.
1 /********************************************************************************************
2 * File: uStepperEncoder.cpp *
3 * Version: 1.0.1 *
4 * Date: May 14th, 2019 *
5 * Author: Thomas Hørring Olsen *
6 * *
7 *********************************************************************************************
8 * (C) 2019 *
9 * *
10 * uStepper ApS *
11 * www.ustepper.com *
12 * administration@ustepper.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 uStepper ApS nor the author, can be held responsible for any damage *
20 * caused by the use of the code contained in this file ! *
21 * *
22 ********************************************************************************************/
32 #include <uStepperS.h>
33 /* At initialition setup the SPI hardware protocal to communicate with SSI interface */
34 extern uStepperS * pointer;
36 {
37  /* Prepare Hardware SPI communication */
38 
39  /*
40  * SPE = 1: SPI enabled
41  * MSTR = 1: Master
42  * SPR0 = 1 & SPR1 = 0: fOSC/16 = 1Mhz
43  * CPOL = 1: Idle at HIGH
44  * CPHA = 0: Sample at leading edge
45  */
46  // SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<CPOL);
47 }
48 
50 {
51  this->pointer = _pointer;
52 
53  angle = 0;
54 
55  /* Set the interrupt mode to 14 with a prescaler of 1 */
56  TCCR1A = (1 << WGM11);
57  TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 << CS10);
58 
59  /* Reset Timer1 and set compare interrupt each: 62.5 ns * 16000 = 1 milliseconds */
60  TCNT1 = 0;
61  ICR1 = 16000;
62 
63  TIFR1 = 0;
64 
65  /* Enable Timer1 compare interrupt */
66  TIMSK1 = (1 << OCIE1A);
67 
68  /* As long as we only use SSI, the MOSI_ENC/DIN (NSL) should be pulled LOW */
69  PORTC &= ~(1 << MOSI_ENC);
70 
71  /* Enable global interrupts */
72  sei();
73 }
74 
76 {
77  cli();
78 
79  this->encoderOffset = this->captureAngle();
80  this->oldAngle = 0;
81  this->angle = 0;
82  this->angleMoved = 0;
83  this->revolutions = 0;
84  pointer->driver.setHome();
85  this->encoderFilter.posError = 0.0;
86  this->encoderFilter.posEst = 0.0;
87  this->encoderFilter.velIntegrator = 0.0;
88  this->encoderFilter.velEst = 0.0;
89  sei();
90 }
91 
93 {
94  uint8_t status;
95 
96  //this->captureAngle();
97 
98  status = this->getStatus();
99 
100  if((status & 0xE0) != 0x80)
101  {
102  return 0;
103  }
104 
105  return 1;
106 }
107 
109 {
110  pointer->setSPIMode(2);
111 
112  uint16_t value = 0;
113  uint8_t stats = 0;
114 
115  chipSelect(true); // Set CS HIGH
116 
117  /* Write dummy and read the incoming 8 bits */
118  value = pointer->SPI(0x00);
119  value <<= 8;
120 
121  /* Write dummy and read the incoming 8 bits */
122  value |= pointer->SPI(0x00);
123 
124  /* Write dummy and read the incoming 8 bits */
125  this->status = pointer->SPI(0x00);
126 
127  chipSelect(false); // Set CS LOW
128 
129 
130  return value;
131 }
132 
134 {
135  return (float)angle * 0.005493164; //360/65536
136 }
137 
139 {
140  return angle;
141 }
142 
143 
145 {
146  return this->angleMoved * 0.005493164; //360/65536
147 }
148 
150 {
151  return angleMoved;
152 }
153 
154 
156 {
157  return this->status;
158 }
159 
161 {
163 }
164 
166 {
168 }
169 
171 {
172  if(state)
173  PORTD |= (1 << CS_ENCODER); // Set CS HIGH
174  else
175  PORTD &= ~(1 << CS_ENCODER); // Set CS LOW
176 }
uStepperEncoder(void)
Constructor of uStepperEncoder class.
#define ENCODERDATATOSTEP
volatile posFilter_t encoderFilter
#define MOSI_ENC
Definition: uStepperS.h:197
float posEst
Definition: uStepperS.h:176
float getSpeed(void)
Measure the current speed of the motor.
int32_t getAngleMovedRaw(void)
Returns the angle moved from reference position in raw encoder readings.
#define ENCODERDATATOREVOLUTIONS
uStepperEncoder encoder
Definition: uStepperS.h:265
Prototype of class for accessing all features of the uStepper S in a single object.
Definition: uStepperS.h:252
float posError
Definition: uStepperS.h:175
float getAngleMoved(void)
Returns the angle moved from reference position in degrees.
uStepperDriver driver
Definition: uStepperS.h:262
float velEst
Definition: uStepperS.h:178
void setHome(void)
Resets the internal position counter of the motor driver.
volatile int32_t angleMoved
uint16_t captureAngle(void)
Capture the current shaft angle.
volatile uint16_t oldAngle
#define CS_ENCODER
Definition: uStepperS.h:194
float velIntegrator
Definition: uStepperS.h:177
uint16_t getAngleRaw(void)
Return the current shaft angle in raw encoder readings.
void setHome(void)
Define new reference(home) position.
volatile uint16_t angle
volatile int16_t revolutions
float getAngle(void)
Return the current shaft angle in degrees.
void init(uStepperS *_pointer)
Initiation of the encoder.
uStepperS * pointer
uint16_t encoderOffset
void chipSelect(bool state)
Set the output level of the chip select pin.
float getRPM(void)
Measure the current speed of the motor.
bool detectMagnet(void)
detect magnet
uint8_t getStatus(void)
Get encoder status.
Function prototypes and definitions for the uStepper S library.