Stepper contin. rot.
This commit is contained in:
parent
ee47fe4999
commit
ce6c408a9f
@ -17,7 +17,8 @@ Pin (ESP32)| Function
|
|||||||
36,39,34 | Button-Input
|
36,39,34 | Button-Input
|
||||||
32,33,13 | Button-LEDs
|
32,33,13 | Button-LEDs
|
||||||
16 | Housing-LEDs
|
16 | Housing-LEDs
|
||||||
2,4,5 | Free GPIO
|
2 | Piezo-Buzzer
|
||||||
|
4,5 | Free GPIO
|
||||||
35 | Free GPInput
|
35 | Free GPInput
|
||||||
|
|
||||||
## Reference
|
## Reference
|
||||||
|
@ -5,7 +5,7 @@ import uasyncio as asyncio
|
|||||||
stepping = [[1,0,1,0],[1,0,0,0],[1,0,0,1],[0,0,0,1],[0,1,0,1],[0,1,0,0],[0,1,1,0],[0,0,1,0]]
|
stepping = [[1,0,1,0],[1,0,0,0],[1,0,0,1],[0,0,0,1],[0,1,0,1],[0,1,0,0],[0,1,1,0],[0,0,1,0]]
|
||||||
|
|
||||||
class Stepper:
|
class Stepper:
|
||||||
def __init__(self,pins,stepsPerRev=400,stepDurationMs=50,inverted=False):
|
def __init__(self,pins,stepsPerRev=400,stepDurationMs=10,inverted=False):
|
||||||
self._pins = [Pin(p, Pin.OUT) for p in pins]
|
self._pins = [Pin(p, Pin.OUT) for p in pins]
|
||||||
self.stepnum = 0
|
self.stepnum = 0
|
||||||
self.rotDirection = 1
|
self.rotDirection = 1
|
||||||
@ -30,16 +30,25 @@ class Stepper:
|
|||||||
self._pins[3].value(level[3])
|
self._pins[3].value(level[3])
|
||||||
|
|
||||||
def getStepsToTarget(self):
|
def getStepsToTarget(self):
|
||||||
return ((self._rotTarget - self.stepnum)*self.rotDirection)%self.stepsPerRev
|
#return ((self._rotTarget - self.stepnum)*self.rotDirection)%self.stepsPerRev
|
||||||
|
return ((self._rotTarget - self.stepnum)*self.rotDirection)
|
||||||
|
|
||||||
def rotateTo(self, target,duration=0,direction=0):
|
def rotateTo(self, target,duration=0,direction=0,normalise = True):
|
||||||
"""sets the rotation target and (if direction == 0) calculates if
|
"""sets the rotation target and (if direction == 0) calculates if
|
||||||
it should rotate left or right"""
|
it should rotate left or right"""
|
||||||
self._rotTarget = int(target*self.stepsPerRev) % self.stepsPerRev
|
self._rotTarget = int(target*self.stepsPerRev)
|
||||||
|
if normalise:
|
||||||
|
rotTargetNorm = self._rotTarget%self.stepsPerRev
|
||||||
|
stepsNorm = self.stepnum%self.stepsPerRev
|
||||||
|
self._rotTarget = self.stepnum+rotTargetNorm-stepsNorm
|
||||||
|
if (self._rotTarget-self.stepnum) > self.stepsPerRev/2:
|
||||||
|
self._rotTarget-=self.stepsPerRev
|
||||||
|
elif(self._rotTarget-self.stepnum) < -self.stepsPerRev/2:
|
||||||
|
self._rotTarget+=self.stepsPerRev
|
||||||
|
|
||||||
if not self.isAtTarget():
|
if not self.isAtTarget():
|
||||||
if (direction == 0):
|
if (direction == 0):
|
||||||
self.rotDirection = 1 if (self._rotTarget - self.stepnum)%self.stepsPerRev \
|
self.rotDirection = 1 if (self._rotTarget > self.stepnum) else -1
|
||||||
< self.stepsPerRev/2 else -1
|
|
||||||
else:
|
else:
|
||||||
self.rotDirection = direction
|
self.rotDirection = direction
|
||||||
self._speedMs = max(int(self.stepDurationMs),duration*1000/self.getStepsToTarget())
|
self._speedMs = max(int(self.stepDurationMs),duration*1000/self.getStepsToTarget())
|
||||||
@ -53,7 +62,7 @@ class Stepper:
|
|||||||
self.disablePower()
|
self.disablePower()
|
||||||
|
|
||||||
def _update(self):
|
def _update(self):
|
||||||
self.stepnum = (self.stepnum + self.rotDirection) % self.stepsPerRev
|
self.stepnum = (self.stepnum + self.rotDirection)
|
||||||
self._setPins(stepping[(self.stepnum%8) if not self.inverted else ((8-self.stepnum)%8)])
|
self._setPins(stepping[(self.stepnum%8) if not self.inverted else ((8-self.stepnum)%8)])
|
||||||
|
|
||||||
def disablePower(self):
|
def disablePower(self):
|
||||||
@ -66,4 +75,4 @@ class Stepper:
|
|||||||
return (self._rotTarget / self.stepsPerRev)
|
return (self._rotTarget / self.stepsPerRev)
|
||||||
|
|
||||||
def isAtTarget(self):
|
def isAtTarget(self):
|
||||||
return (self._rotTarget%self.stepsPerRev == self.stepnum%self.stepsPerRev)
|
return (self._rotTarget == self.stepnum)
|
||||||
|
29
main.py
29
main.py
@ -5,14 +5,24 @@ import Button
|
|||||||
import uasyncio as asyncio
|
import uasyncio as asyncio
|
||||||
import StepperL298M
|
import StepperL298M
|
||||||
import StepperClock
|
import StepperClock
|
||||||
|
import neopixel
|
||||||
|
|
||||||
#Async
|
#Async
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
#Initialise the time module with the RTC
|
#Initialise the time module with the RTC
|
||||||
dsRtc = DS3231.DS3231(machine.I2C(sda = machine.Pin(17), scl=machine.Pin(18)))
|
try:
|
||||||
rtc = machine.RTC()
|
dsRtc = DS3231.DS3231(machine.I2C(sda = machine.Pin(17), scl=machine.Pin(18)))
|
||||||
rtc.init(dsRtc.DateTime()+[0])
|
rtc = machine.RTC()
|
||||||
|
rtc.init(dsRtc.DateTime()+[0])
|
||||||
|
except:
|
||||||
|
print("Error on DS3231-Inititalisation")
|
||||||
|
|
||||||
|
housingLEDs = neopixel.NeoPixel(machine.Pin(16, machine.Pin.OUT),5)
|
||||||
|
housingLEDs.fill((0,0,0))
|
||||||
|
housingLEDs.write()
|
||||||
|
|
||||||
|
Buzzer = machine.PWM(machine.Pin(2), freq=10000, duty=0)
|
||||||
|
|
||||||
#Initalise the Motors
|
#Initalise the Motors
|
||||||
stepDurationMs = 8
|
stepDurationMs = 8
|
||||||
@ -42,9 +52,14 @@ def rightButtonClickUp():
|
|||||||
rightButtonLED.off()
|
rightButtonLED.off()
|
||||||
def rightButtonClick():
|
def rightButtonClick():
|
||||||
if(clock.isRunning()):
|
if(clock.isRunning()):
|
||||||
stepperhour.stepnum = (stepperhour.stepnum+8)%stepperhour.stepsPerRev
|
stepperminu.stepnum = (stepperminu.stepnum+8)%stepperminu.stepsPerRev
|
||||||
else:
|
else:
|
||||||
stepperhour.rotateTo(stepperhour.getOrientation()+0.25)
|
stepperminu.rotateTo(stepperminu.getOrientation()-0.25)
|
||||||
|
|
||||||
|
#if(clock.isRunning()):
|
||||||
|
# stepperhour.stepnum = (stepperhour.stepnum+8)%stepperhour.stepsPerRev
|
||||||
|
#else:
|
||||||
|
# stepperhour.rotateTo(stepperhour.getOrientation()+0.25)
|
||||||
rightButton = Button.Button(pin=34,onPushDown=rightButtonClickDown,onPushUp=rightButtonClickUp,onClick=rightButtonClick,inverted=True)
|
rightButton = Button.Button(pin=34,onPushDown=rightButtonClickDown,onPushUp=rightButtonClickUp,onClick=rightButtonClick,inverted=True)
|
||||||
|
|
||||||
def middleButtonClickDown():
|
def middleButtonClickDown():
|
||||||
@ -57,8 +72,12 @@ def middleButtonClick():
|
|||||||
clock.stop()
|
clock.stop()
|
||||||
stepperminu.rotateTo(0)
|
stepperminu.rotateTo(0)
|
||||||
stepperhour.rotateTo(0)
|
stepperhour.rotateTo(0)
|
||||||
|
housingLEDs.fill((255,0,255))
|
||||||
|
housingLEDs.write()
|
||||||
else:
|
else:
|
||||||
clock.start()
|
clock.start()
|
||||||
|
housingLEDs.fill((0,0,0))
|
||||||
|
housingLEDs.write()
|
||||||
middleButton = Button.Button(pin=39,onPushDown=middleButtonClickDown,onPushUp=middleButtonClickUp,onClick=middleButtonClick,inverted=True)
|
middleButton = Button.Button(pin=39,onPushDown=middleButtonClickDown,onPushUp=middleButtonClickUp,onClick=middleButtonClick,inverted=True)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user