More encapsulation of timer info.
This commit is contained in:
@@ -7,12 +7,12 @@
|
|||||||
class MyEncoder
|
class MyEncoder
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
MyEncoder(uint8_t _indexPin, void irq(void));
|
MyEncoder(TIM_TypeDef *_tim_base, uint8_t _indexPin, void irq(void));
|
||||||
int32_t unwrapEncoder(uint16_t in);
|
int32_t unwrapEncoder(uint16_t in);
|
||||||
void indexPulse(void);
|
void indexPulse(void);
|
||||||
void init(TIM_TypeDef *_tim_base);
|
void init();
|
||||||
uint8_t indexHappened();
|
uint8_t indexHappened();
|
||||||
double currentPos(volatile uint32_t cnt);
|
double currentPos();
|
||||||
double frequency(uint64_t time);
|
double frequency(uint64_t time);
|
||||||
uint8_t getIndexState();
|
uint8_t getIndexState();
|
||||||
void setScale(double scale);
|
void setScale(double scale);
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
#include "MyENcoder.h"
|
#include "MyENcoder.h"
|
||||||
|
|
||||||
MyEncoder::MyEncoder(uint8_t _indexPin, void irq(void))
|
MyEncoder::MyEncoder(TIM_TypeDef *_tim_base, uint8_t _indexPin, void irq(void))
|
||||||
{
|
{
|
||||||
|
tim_base = _tim_base;
|
||||||
indexPin = _indexPin;
|
indexPin = _indexPin;
|
||||||
attachInterrupt(digitalPinToInterrupt(indexPin), irq, RISING); // When Index triggered
|
attachInterrupt(digitalPinToInterrupt(indexPin), irq, RISING); // When Index triggered
|
||||||
}
|
}
|
||||||
@@ -29,21 +30,17 @@ void MyEncoder::indexPulse(void)
|
|||||||
{
|
{
|
||||||
if (pleaseZeroTheCounter)
|
if (pleaseZeroTheCounter)
|
||||||
{
|
{
|
||||||
TIM2->CNT = 0;
|
tim_base->CNT = 0;
|
||||||
indexPulseFired = 1;
|
indexPulseFired = 1;
|
||||||
Pos.clear();
|
Pos.clear();
|
||||||
TDelta.clear();
|
TDelta.clear();
|
||||||
pleaseZeroTheCounter = 0;
|
pleaseZeroTheCounter = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void MyEncoder::init(TIM_TypeDef *_tim_base)
|
void MyEncoder::init()
|
||||||
{
|
{
|
||||||
EncoderInit.tim_base = _tim_base;
|
|
||||||
// Set starting count value
|
// Set starting count value
|
||||||
EncoderInit.SetCount(0);
|
EncoderInit.SetCount(0);
|
||||||
// EncoderInit.SetCount(Tim3, 0);
|
|
||||||
// EncoderInit.SetCount(Tim4, 0);
|
|
||||||
// EncoderInit.SetCount(Tim8, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t MyEncoder::indexHappened()
|
uint8_t MyEncoder::indexHappened()
|
||||||
@@ -57,7 +54,7 @@ uint8_t MyEncoder::indexHappened()
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
double MyEncoder::currentPos(volatile uint32_t cnt)
|
double MyEncoder::currentPos()
|
||||||
{
|
{
|
||||||
curPos = unwrapEncoder(tim_base->CNT) * PosScaleRes;
|
curPos = unwrapEncoder(tim_base->CNT) * PosScaleRes;
|
||||||
return curPos;
|
return curPos;
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ HardwareSerial Serial1(PA10, PA9);
|
|||||||
#include "MyEncoder.h"
|
#include "MyEncoder.h"
|
||||||
#define INDEX_PIN PA2
|
#define INDEX_PIN PA2
|
||||||
void indexPulseEncoderCB1(void);
|
void indexPulseEncoderCB1(void);
|
||||||
MyEncoder Encoder1(INDEX_PIN, indexPulseEncoderCB1);
|
MyEncoder Encoder1(TIM2, INDEX_PIN, indexPulseEncoderCB1);
|
||||||
void indexPulseEncoderCB1(void)
|
void indexPulseEncoderCB1(void)
|
||||||
{
|
{
|
||||||
Encoder1.indexPulse();
|
Encoder1.indexPulse();
|
||||||
@@ -30,6 +30,7 @@ void cb_set_outputs(void) // Master outputs gets here, slave inputs, first opera
|
|||||||
{
|
{
|
||||||
Encoder1.setLatch(Obj.IndexLatchEnable);
|
Encoder1.setLatch(Obj.IndexLatchEnable);
|
||||||
Encoder1.setScale(Obj.EncPosScale);
|
Encoder1.setScale(Obj.EncPosScale);
|
||||||
|
|
||||||
Step1.cmdPos(Obj.StepGenIn1.CommandedPosition);
|
Step1.cmdPos(Obj.StepGenIn1.CommandedPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,11 +42,12 @@ void handleStepper(void)
|
|||||||
void cb_get_inputs(void) // Set Master inputs, slave outputs, last operation
|
void cb_get_inputs(void) // Set Master inputs, slave outputs, last operation
|
||||||
{
|
{
|
||||||
Obj.IndexStatus = Encoder1.indexHappened();
|
Obj.IndexStatus = Encoder1.indexHappened();
|
||||||
Obj.EncPos = Encoder1.currentPos(TIM2->CNT);
|
Obj.EncPos = Encoder1.currentPos();
|
||||||
Obj.EncFrequency = Encoder1.frequency(ESCvar.Time);
|
Obj.EncFrequency = Encoder1.frequency(ESCvar.Time);
|
||||||
Obj.IndexByte = Encoder1.getIndexState();
|
Obj.IndexByte = Encoder1.getIndexState();
|
||||||
if (Obj.IndexByte)
|
if (Obj.IndexByte)
|
||||||
Serial1.printf("IS 1\n");
|
Serial1.printf("IS 1\n");
|
||||||
|
|
||||||
Obj.StepGenOut1.ActualPosition = Step1.actPos();
|
Obj.StepGenOut1.ActualPosition = Step1.actPos();
|
||||||
Obj.DiffT = 10000 * Step1.reqPos();
|
Obj.DiffT = 10000 * Step1.reqPos();
|
||||||
}
|
}
|
||||||
@@ -83,7 +85,7 @@ void setup(void)
|
|||||||
rcc_config();
|
rcc_config();
|
||||||
|
|
||||||
Step1.setScale(500);
|
Step1.setScale(500);
|
||||||
Encoder1.init(TIM2);
|
Encoder1.init();
|
||||||
|
|
||||||
ecat_slv_init(&config);
|
ecat_slv_init(&config);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user