whitecatboard / Lua-RTOS-ESP32

Lua RTOS for ESP32
Other
1.2k stars 221 forks source link

stepper.startasync() and stepper.stopasync() #378

Closed ar055 closed 3 years ago

ar055 commented 3 years ago

commit e4d5059f1e0b5599bfa2b1f9ada4d6e23c353fbc

could you please post some lua code demonstrating how these functions work?

  1. I was unable to start stepper motors with the stepper.startasync () command.
  2. the stepper.stopasync () command stops all motors.

my code:

m1.stepper =  stepper.attach(m1.dir_pin, m1.step_pin, 200, 60, 6000, 100, 100)
m2.stepper =  stepper.attach(m2.dir_pin, m2.step_pin, 200, 60, 6000, 100, 100)

function run(motor)
  function _run()
    pio.pin.setlow(motor.en_pin)
    stepper.start(motor.stepper)
--    stepper.startasync(motor.stepper)  -- does not work
  end
  if motor.stepper:getdistance() ~= 0 then
    th = thread.start(_run)
  end
end

m1.stepper:move(1000)
m2.stepper:move(1000)

run(m1)
run(m2)
tmr.sleepms(1000)

stepper.stopasync(m1.stepper)  -- all steppers stopped
chowette commented 3 years ago

I do not reproduce your bug with only one motor. ( I have only one wired yet)

Your code look similar to mine, except that I have added the startasync()exactly to avoid an additional thread.

What I see is another bug :

m1.stepper =  stepper.attach(m1.dir_pin, m1.step_pin, 200, 60, 6000, 100, 100)
m1.stepper:move(1000)
pio.pin.setlow(m1.en_pin)
stepper.startasync(m1.stepper)
tmr.sleepms(1000)
stepper.stop(m1.stepper)  -- or stepper.stopasync(m1.stepper) 
-- here come the bug
stepper.start(m1.stepper) -- or stepper.startasync(m1.stepper)

Restarting a stepper after a call to stop() or stopasync() leave the stepper in an incoherent state. Note that calling the move() method reset this state.

m1.stepper:move(m1.stepper:getdistance())
stepper.start(m1.stepper) -- or stepper.startasync(m1.stepper)
chowette commented 3 years ago

I see where the bug from your sample code

stepper.stopasync(m1.stepper)  -- all steppers stopped

comes from : https://github.com/whitecatboard/Lua-RTOS-ESP32/blob/b524cdb84c111f8e612e73a823142178a33753e3/components/sys/drivers/stepper.c#L784

the mask parameter is never used. I will provide a patch soon

ar055 commented 3 years ago

idn't know that startasync starts the process itself, my mistake was pio.pin.sethigh (motor.en_pin) right after stepper.startasync(motor.stepper). confirm, stepper.startasiync() - it's work!

chowette commented 3 years ago

Yeah, the documentation is lacking ... But I don't know how to edit the wiki.

I did not see that you disabled the motors right after the startasync() , I am glad it works for you.

the0ne commented 3 years ago

Wiki is a separate repo that can be pulled, locally modified and pushed:

Any GitHub wiki can be cloned by appending wiki.git to the repo url, so the clone url for the repo https://myorg/myrepo/ is: git@github.com:myorg/myrepo.wiki.git (for ssh) or https://github.com/my/myrepo.wiki.git (for https).

You make edits, and commit and push your changes, like any normal repo. This wiki repo is distinct from any clone of the project repo (the repo without wiki.get appended).

chowette commented 3 years ago

Thank you @the0ne , I will give it a try.

chowette commented 3 years ago

see 3abf0ea5876f92405a80e3d6d0a47732a28c7349 in #377 for a fix to the stop() and stopasync() to stop only the stepper that where asked .