Two encoders and two steppers declared. Encoder is 64-bit again

This commit is contained in:
Hakan Bastedt
2024-01-11 21:10:27 +01:00
parent 6176166b3a
commit 7a63d27303
3 changed files with 23 additions and 15 deletions

View File

@@ -8,9 +8,8 @@ class MyEncoder
{
public:
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 init();
uint8_t indexHappened();
double currentPos();
double frequency(uint64_t time);
@@ -19,7 +18,7 @@ public:
void setLatch(uint8_t latchEnable);
private:
int32_t previousEncoderCounterValue = 0;
int64_t previousEncoderCounterValue = 0;
double PosScaleRes = 1.0;
uint32_t CurPosScale = 1;
uint8_t oldLatchCEnable = 0;

View File

@@ -5,12 +5,13 @@ MyEncoder::MyEncoder(TIM_TypeDef *_tim_base, uint8_t _indexPin, void irq(void))
tim_base = _tim_base;
indexPin = _indexPin;
attachInterrupt(digitalPinToInterrupt(indexPin), irq, RISING); // When Index triggered
EncoderInit.SetCount(0);
}
#define ONE_PERIOD 65536
#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 dif = (c32 - previousEncoderCounterValue); // core concept: prev + (current - prev) = current
@@ -20,7 +21,7 @@ int32_t MyEncoder::unwrapEncoder(uint16_t in)
if (dif < -HALF_PERIOD)
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
return unwrapped + HALF_PERIOD; // remove the shift we applied at the beginning, and return
@@ -37,11 +38,6 @@ void MyEncoder::indexPulse(void)
pleaseZeroTheCounter = 0;
}
}
void MyEncoder::init()
{
// Set starting count value
EncoderInit.SetCount(0);
}
uint8_t MyEncoder::indexHappened()
{
@@ -96,4 +92,3 @@ void MyEncoder::setLatch(uint8_t latchEnable)
}
oldLatchCEnable = latchEnable;
}

View File

@@ -10,13 +10,20 @@ _Objects Obj;
HardwareSerial Serial1(PA10, PA9);
#include "MyEncoder.h"
#define INDEX_PIN PA2
void indexPulseEncoderCB1(void);
MyEncoder Encoder1(TIM2, INDEX_PIN, indexPulseEncoderCB1);
MyEncoder Encoder1(TIM2, PA2, indexPulseEncoderCB1);
void indexPulseEncoderCB1(void)
{
Encoder1.indexPulse();
}
#if 1
void indexPulseEncoderCB2(void);
MyEncoder Encoder2(TIM3, PB6, indexPulseEncoderCB2);
void indexPulseEncoderCB2(void)
{
Encoder2.indexPulse();
}
#endif
#include "StepGen.h"
void timerCallbackStep1(void);
@@ -25,12 +32,20 @@ void timerCallbackStep1(void)
{
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
{
Encoder1.setLatch(Obj.IndexLatchEnable);
Encoder1.setScale(Obj.EncPosScale);
Step1.cmdPos(Obj.StepGenIn1.CommandedPosition);
}
@@ -85,7 +100,6 @@ void setup(void)
rcc_config();
Step1.setScale(500);
Encoder1.init();
ecat_slv_init(&config);
}