Basic test with puls sequence

This commit is contained in:
Hakan Bastedt
2024-01-01 22:42:12 +01:00
parent f99d9bab77
commit 64d62a954a
2 changed files with 51 additions and 26 deletions

View File

@@ -9,5 +9,6 @@
"unordered_map": "cpp", "unordered_map": "cpp",
"vector": "cpp", "vector": "cpp",
"system_error": "cpp" "system_error": "cpp"
} },
"C_Cpp.errorSquiggles": "disabled"
} }

View File

@@ -105,18 +105,35 @@ static esc_cfg_t config =
#define STEPPER_DIR PA12 #define STEPPER_DIR PA12
#define STEPPER_STEP_PIN PA11 #define STEPPER_STEP_PIN PA11
HardwareTimer *MyTim; HardwareTimer *MyTim;
volatile uint32_t stepCount = 0; volatile uint32_t stepCount = 0, stepPulses = 0;
volatile int32_t actualPosition = 0;
volatile int32_t requestedPosition;
volatile uint32_t pulsesToGo = 0;
volatile byte forwardDirection = 0; // 1 if going forward
volatile uint32_t syncCounts = 0;
void TimerStep_CB(void) void TimerStep_CB(void)
{ {
stepCount++; if (forwardDirection)
digitalWrite(STEPPER_STEP_PIN, !digitalRead(STEPPER_STEP_PIN)); stepCount++;
if (stepCount >= 10) else
stepCount--;
//digitalWrite(STEPPER_STEP_PIN, !digitalRead(STEPPER_STEP_PIN));
if (stepCount == stepPulses)
{ {
MyTim->pause(); MyTim->pause();
} }
} }
void makePulses(uint32_t period /* in usecs */, uint32_t pulses /* nr of pulses to do*/)
{
MyTim->setOverflow(period / pulses, MICROSEC_FORMAT);
stepCount = 0;
stepPulses = pulses;
MyTim->resume();
}
void setup(void) void setup(void)
{ {
Serial1.begin(115200); Serial1.begin(115200);
@@ -124,19 +141,38 @@ void setup(void)
TIM_TypeDef *Instance = TIM1; TIM_TypeDef *Instance = TIM1;
MyTim = new HardwareTimer(Instance); MyTim = new HardwareTimer(Instance);
MyTim->setMode(4, TIMER_OUTPUT_COMPARE_PWM2, STEPPER_STEP_PIN); MyTim->setMode(4, TIMER_OUTPUT_COMPARE_PWM2, STEPPER_STEP_PIN);
MyTim->setOverflow(5000, HERTZ_FORMAT);
MyTim->setCaptureCompare(4, 50, PERCENT_COMPARE_FORMAT); // 50% MyTim->setCaptureCompare(4, 50, PERCENT_COMPARE_FORMAT); // 50%
MyTim->attachInterrupt(TimerStep_CB); MyTim->attachInterrupt(TimerStep_CB);
MyTim->resume(); stepCount = 0;
#if 1
while (1) while (1)
{ {
HAL_Delay(5); // Update the actual position
stepCount = 0; actualPosition += pulsesToGo;
MyTim->resume(); Obj.StepGenOut1.ActualPosition = actualPosition;
// Get new end position
// requestedPosition = Obj.StepGenIn1.CommandedPosition;
requestedPosition = syncCounts % 2 ? 4 : -4;
// Get the diff and the direction
pulsesToGo = requestedPosition - actualPosition;
syncCounts++;
forwardDirection = pulsesToGo > 0 ? 1 : 0;
forwardDirection = 1;
// Set direction pin
Obj.DiffT = forwardDirection;
digitalWrite(STEPPER_DIR, forwardDirection); // I think one should really wait a bit when changed
makePulses(1000, syncCounts % 2 ? 2 : 4);
// Make the pulses using hardware timer
// if (pulsesToGo != 0)
// makePulses(sync0CycleTime / 1000, pulsesToGo);
delayMicroseconds(1000);
} }
#endif
// Set starting count value // Set starting count value
EncoderInit.SetCount(Tim2, 0); EncoderInit.SetCount(Tim2, 0);
// EncoderInit.SetCount(Tim3, 0); // EncoderInit.SetCount(Tim3, 0);
@@ -185,16 +221,8 @@ void indexPulse(void)
} }
} }
volatile int32_t actualPosition = 0;
volatile int32_t requestedPosition;
volatile uint32_t pulsesToGo = 0;
volatile byte forwardDirection = 0; // 1 if going forward
volatile uint32_t syncCounts = 0;
void sync0Handler(void) void sync0Handler(void)
{ {
#if 0
// Update the actual position // Update the actual position
actualPosition += pulsesToGo; actualPosition += pulsesToGo;
Obj.StepGenOut1.ActualPosition = actualPosition; Obj.StepGenOut1.ActualPosition = actualPosition;
@@ -202,18 +230,14 @@ void sync0Handler(void)
requestedPosition = Obj.StepGenIn1.CommandedPosition; requestedPosition = Obj.StepGenIn1.CommandedPosition;
// Get the diff and the direction // Get the diff and the direction
pulsesToGo = requestedPosition - actualPosition; pulsesToGo = requestedPosition - actualPosition;
#else
syncCounts++; syncCounts++;
pulsesToGo = syncCounts % 2 ? 4 : 8;
#endif
// forwardDirection = pulsesToGo > 0 ? 1 : 0;
forwardDirection = syncCounts % 2 ? 1 : 0; forwardDirection = syncCounts % 2 ? 1 : 0;
// Set direction pin // Set direction pin
digitalWrite(STEPPER_DIR, forwardDirection); // I think one should really wait a bit when changed
Obj.DiffT = forwardDirection; Obj.DiffT = forwardDirection;
digitalWrite(STEPPER_DIR, forwardDirection); // I think one should really wait a bit when changed
// Make the pulses using hardware timer // Make the pulses using hardware timer
// makePulses(sync0CycleTime / 1000, pulsesToGo); makePulses(sync0CycleTime / 1000, pulsesToGo);
} }
void ESC_interrupt_enable(uint32_t mask) void ESC_interrupt_enable(uint32_t mask)