Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix step motor over heating after stop,and auto reboot in new version of firmware. #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ With this module you can drive a stepper motor in 3 lines like this
```lua
stepper = require ('stepper')
stepper.init({5,6,7,8})
stepper.rotate(stepper.FORWARD,2500,5,0,function () print('Rotation done.') end)
stepper.rotate(stepper.FORWARD,2500,5,function () print('Rotation done.') end)
```
As you can see, your code trigger rotate and let the module finish rotation and trigger callback.

Expand Down Expand Up @@ -63,7 +63,7 @@ Note : this is a async method. It'll trigger rotation and return immediately.

##### Signature :
```lua
rotate( direction, desired_steps, interval, timer_to_use, callback)
rotate( direction, desired_steps, interval, callback)
```

##### Parameters :
Expand All @@ -73,7 +73,6 @@ rotate( direction, desired_steps, interval, timer_to_use, callback)
| **direction** | _stepper.FORWARD or stepper.REVERSE_ |
| **desired_steps** | _number between 0 to infinity - 2500 is default which is roughly half a rotation_ |
| **interval** | _time delay in milliseconds between steps, smaller self number is, faster the motor rotates . 5 is default_ |
| **timer_to_use** | _which nodemcu timer to use, 0 is default_ |
| **callback** | _callback to invoke on completion of given rotation |


Expand All @@ -82,9 +81,9 @@ rotate( direction, desired_steps, interval, timer_to_use, callback)
```lua
direction = stepper.FORWARD
desired_steps = 2500
interval = 5
interval = 1
timer_to_use = 0
stepper.rotate(direction,desired_steps,interval,timer_to_use,function ()
stepper.rotate(direction,desired_steps,interval,function ()
print('Rotation done. inside callback.')
-- do some thing useful
end)
Expand All @@ -94,5 +93,5 @@ stepper.rotate(direction,desired_steps,interval,timer_to_use,function ()
```lua
stepper = require ('stepper')
stepper.init({5,6,7,8})
stepper.rotate(stepper.FORWARD,2500,5,0,function () print('Rotation done.') end)
stepper.rotate(stepper.FORWARD,2500,5,function () print('Rotation done.') end)
```
15 changes: 9 additions & 6 deletions stepper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ do
-- motor configuration data
---------------------------------------------------------------------------------------
local motor_params = {}
local motor_timer = tmr.create()

-- nodemcu pin numbers on which to motor is connected
motor_params.pins = {5,6,7,8}
Expand All @@ -47,7 +48,6 @@ do
motor_params.step_interval = 5 -- milliseconds decides the speed. smaller the interval, higher the speed.
motor_params.desired_steps = 2500
motor_params.direction = FORWARD
motor_params.timer_to_use = 0
motor_params.callback = nil


Expand Down Expand Up @@ -84,7 +84,10 @@ do
--increment the counter and check if there are steps to execute
step_counter = step_counter + 1
if step_counter > motor_params.desired_steps then
tmr.stop(motor_params.timer_to_use)
motor_timer:stop()
for index,mcu_pin in ipairs(motor_params.pins) do
gpio.write(mcu_pin, gpio.LOW)
end
node.task.post(2, motor_params.callback) -- node.task.HIGH_PRIORITY = 2
else
updatePhaseForNextStep();
Expand All @@ -108,7 +111,7 @@ do
-- D8 < ------ > IN4
local init = function ( pins )
if not pins or not interval then
print('Init params missing !!! initializing with defaults')
--print('Init params missing !!! initializing with defaults')
local motor_pins = motor_params.pins;
for i,pin in ipairs( motor_pins ) do
gpio.mode(pin, gpio.OUTPUT)
Expand All @@ -128,17 +131,17 @@ do
-- direction = stepper.FORWARD or stepper.REVERSE
-- desired_steps = number between 0 to infinity - 2500 is default
-- interval = time delay in milliseconds between steps, smaller self number is, faster the motor rotates . 5 is default
local rotate = function ( direction, desired_steps, interval, timer_to_use, callback)
local rotate = function ( direction, desired_steps, interval, callback)
motor_params.step_interval = interval -- milliseconds decides the speed. smaller the interval, higher the speed.
motor_params.desired_steps = desired_steps
motor_params.direction = direction
motor_params.timer_to_use = timer_to_use
motor_params.callback = callback

step_counter = 0
phase = 1

tmr.alarm(motor_params.timer_to_use, motor_params.step_interval, REPEATING_TIMER, single_step)
--tmr.alarm(motor_params.timer_to_use, motor_params.step_interval, REPEATING_TIMER, single_step)
motor_timer:alarm(motor_params.step_interval, REPEATING_TIMER, single_step)
end

stepper = {
Expand Down
5 changes: 2 additions & 3 deletions stepper_test.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
stepper = require ('stepper')
stepper.init()
desired_steps = 2500
interval = 5
timer_to_use = 0
interval = 1
print('stepper.rotate() - start')
stepper.rotate(stepper.FORWARD,desired_steps,interval,timer_to_use,function ()
stepper.rotate(stepper.FORWARD,desired_steps,interval,function ()
print('Rotation done. inside callback.')
end)