Two encoders and two steppers declared. Encoder is 64-bit again
This commit is contained in:
@@ -8,9 +8,8 @@ class MyEncoder
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
MyEncoder(TIM_TypeDef *_tim_base, uint8_t _indexPin, void irq(void));
|
MyEncoder(TIM_TypeDef *_tim_base, uint8_t _indexPin, void irq(void));
|
||||||
int32_t unwrapEncoder(uint16_t in);
|
int64_t unwrapEncoder(uint16_t in);
|
||||||
void indexPulse(void);
|
void indexPulse(void);
|
||||||
void init();
|
|
||||||
uint8_t indexHappened();
|
uint8_t indexHappened();
|
||||||
double currentPos();
|
double currentPos();
|
||||||
double frequency(uint64_t time);
|
double frequency(uint64_t time);
|
||||||
@@ -19,7 +18,7 @@ public:
|
|||||||
void setLatch(uint8_t latchEnable);
|
void setLatch(uint8_t latchEnable);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int32_t previousEncoderCounterValue = 0;
|
int64_t previousEncoderCounterValue = 0;
|
||||||
double PosScaleRes = 1.0;
|
double PosScaleRes = 1.0;
|
||||||
uint32_t CurPosScale = 1;
|
uint32_t CurPosScale = 1;
|
||||||
uint8_t oldLatchCEnable = 0;
|
uint8_t oldLatchCEnable = 0;
|
||||||
|
|||||||
@@ -5,12 +5,13 @@ MyEncoder::MyEncoder(TIM_TypeDef *_tim_base, uint8_t _indexPin, void irq(void))
|
|||||||
tim_base = _tim_base;
|
tim_base = _tim_base;
|
||||||
indexPin = _indexPin;
|
indexPin = _indexPin;
|
||||||
attachInterrupt(digitalPinToInterrupt(indexPin), irq, RISING); // When Index triggered
|
attachInterrupt(digitalPinToInterrupt(indexPin), irq, RISING); // When Index triggered
|
||||||
|
EncoderInit.SetCount(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define ONE_PERIOD 65536
|
#define ONE_PERIOD 65536
|
||||||
#define HALF_PERIOD 32768
|
#define HALF_PERIOD 32768
|
||||||
|
|
||||||
int32_t MyEncoder::unwrapEncoder(uint16_t in)
|
int64_t MyEncoder::unwrapEncoder(uint16_t in)
|
||||||
{
|
{
|
||||||
int32_t c32 = (int32_t)in - HALF_PERIOD; // remove half period to determine (+/-) sign of the wrap
|
int32_t c32 = (int32_t)in - HALF_PERIOD; // remove half period to determine (+/-) sign of the wrap
|
||||||
int32_t dif = (c32 - previousEncoderCounterValue); // core concept: prev + (current - prev) = current
|
int32_t dif = (c32 - previousEncoderCounterValue); // core concept: prev + (current - prev) = current
|
||||||
@@ -20,7 +21,7 @@ int32_t MyEncoder::unwrapEncoder(uint16_t in)
|
|||||||
if (dif < -HALF_PERIOD)
|
if (dif < -HALF_PERIOD)
|
||||||
mod_dif += ONE_PERIOD; // account for mod of negative number behavior in C
|
mod_dif += ONE_PERIOD; // account for mod of negative number behavior in C
|
||||||
|
|
||||||
int32_t unwrapped = previousEncoderCounterValue + mod_dif;
|
int64_t unwrapped = previousEncoderCounterValue + mod_dif;
|
||||||
previousEncoderCounterValue = unwrapped; // load previous value
|
previousEncoderCounterValue = unwrapped; // load previous value
|
||||||
|
|
||||||
return unwrapped + HALF_PERIOD; // remove the shift we applied at the beginning, and return
|
return unwrapped + HALF_PERIOD; // remove the shift we applied at the beginning, and return
|
||||||
@@ -37,11 +38,6 @@ void MyEncoder::indexPulse(void)
|
|||||||
pleaseZeroTheCounter = 0;
|
pleaseZeroTheCounter = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void MyEncoder::init()
|
|
||||||
{
|
|
||||||
// Set starting count value
|
|
||||||
EncoderInit.SetCount(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t MyEncoder::indexHappened()
|
uint8_t MyEncoder::indexHappened()
|
||||||
{
|
{
|
||||||
@@ -96,4 +92,3 @@ void MyEncoder::setLatch(uint8_t latchEnable)
|
|||||||
}
|
}
|
||||||
oldLatchCEnable = latchEnable;
|
oldLatchCEnable = latchEnable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,13 +10,20 @@ _Objects Obj;
|
|||||||
HardwareSerial Serial1(PA10, PA9);
|
HardwareSerial Serial1(PA10, PA9);
|
||||||
|
|
||||||
#include "MyEncoder.h"
|
#include "MyEncoder.h"
|
||||||
#define INDEX_PIN PA2
|
|
||||||
void indexPulseEncoderCB1(void);
|
void indexPulseEncoderCB1(void);
|
||||||
MyEncoder Encoder1(TIM2, INDEX_PIN, indexPulseEncoderCB1);
|
MyEncoder Encoder1(TIM2, PA2, indexPulseEncoderCB1);
|
||||||
void indexPulseEncoderCB1(void)
|
void indexPulseEncoderCB1(void)
|
||||||
{
|
{
|
||||||
Encoder1.indexPulse();
|
Encoder1.indexPulse();
|
||||||
}
|
}
|
||||||
|
#if 1
|
||||||
|
void indexPulseEncoderCB2(void);
|
||||||
|
MyEncoder Encoder2(TIM3, PB6, indexPulseEncoderCB2);
|
||||||
|
void indexPulseEncoderCB2(void)
|
||||||
|
{
|
||||||
|
Encoder2.indexPulse();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "StepGen.h"
|
#include "StepGen.h"
|
||||||
void timerCallbackStep1(void);
|
void timerCallbackStep1(void);
|
||||||
@@ -25,6 +32,14 @@ void timerCallbackStep1(void)
|
|||||||
{
|
{
|
||||||
Step1.timerCB();
|
Step1.timerCB();
|
||||||
}
|
}
|
||||||
|
#if 1
|
||||||
|
void timerCallbackStep2(void);
|
||||||
|
StepGen Step2(TIM8, 4, PC9, PC10, timerCallbackStep2);
|
||||||
|
void timerCallbackStep2(void)
|
||||||
|
{
|
||||||
|
Step2.timerCB();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void cb_set_outputs(void) // Master outputs gets here, slave inputs, first operation
|
void cb_set_outputs(void) // Master outputs gets here, slave inputs, first operation
|
||||||
{
|
{
|
||||||
@@ -85,7 +100,6 @@ void setup(void)
|
|||||||
rcc_config();
|
rcc_config();
|
||||||
|
|
||||||
Step1.setScale(500);
|
Step1.setScale(500);
|
||||||
Encoder1.init();
|
|
||||||
|
|
||||||
ecat_slv_init(&config);
|
ecat_slv_init(&config);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user