Skip to content

Commit

Permalink
Add delay for Run function of StepperDriver.
Browse files Browse the repository at this point in the history
  • Loading branch information
tdaira authored and deadprogram committed Nov 28, 2020
1 parent 879e89e commit 1528422
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions drivers/gpio/stepper_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,15 @@ func (s *StepperDriver) Run() (err error) {
s.moving = true
s.mutex.Unlock()

delay := s.getDelayPerStep()

go func() {
for {
if s.moving == false {
break
}
s.step()
time.Sleep(delay)
}
}()

Expand Down Expand Up @@ -203,8 +206,7 @@ func (s *StepperDriver) Move(stepsToMove int) error {
s.mutex.Unlock()

stepsLeft := int64(math.Abs(float64(stepsToMove)))
//Do not remove *1000 and change duration to time.Millisecond. It has been done for a reason
delay := time.Duration(60000*1000/(s.stepsPerRev*s.speed)) * time.Microsecond
delay := s.getDelayPerStep()

for stepsLeft > 0 {
if err := s.step(); err != nil {
Expand All @@ -218,6 +220,12 @@ func (s *StepperDriver) Move(stepsToMove int) error {
return nil
}

// getDelayPerStep gives the delay per step
func (s *StepperDriver) getDelayPerStep() time.Duration {
//Do not remove *1000 and change duration to time.Millisecond. It has been done for a reason
return time.Duration(60000*1000/(s.stepsPerRev*s.speed)) * time.Microsecond
}

// GetCurrentStep gives the current step of motor
func (s *StepperDriver) GetCurrentStep() int {
return s.stepNum
Expand Down

0 comments on commit 1528422

Please sign in to comment.