Skip to content

Commit

Permalink
Add esccontrol
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderBurtasov committed Sep 10, 2022
1 parent 9cab983 commit ec244f5
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions sources/micropython/esccontrol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import machine
import servo
import _thread
import time

class EscControl:
def __init__(self, pinNum, sleepInterval, rotationSpeed):
self.servo = servo.Servo(pinNum)
self.sleepInterval = sleepInterval
self.rotationSpeed = rotationSpeed * 10 / 90
self.lock = _thread.allocate_lock()
self.currentValue = 0
self.targetValue = self.currentValue
self.multiplier = 1
self.lastTickMs = time.ticks_ms()
self.needQuit = False
self.setup()
self.execThread = _thread.start_new_thread(self.runFunc, ())

def targetValueToAngle(self, value):
tmp = round(value * 9 + 90)
# print('value to write: ', tmp)
return tmp

def setTargetValue(self, value):
self.lock.acquire()

if self.hasTransition(self.currentValue, value):
print('Zero;')
self.servo.writeAngle(90)
time.sleep(0.1)
self.servo.writeAngle(80)
time.sleep(0.1)
self.servo.writeAngle(90)
time.sleep(0.1)
self.currentValue = 0

self.targetValue = value
self.multiplier = 1 if self.targetValue >= self.currentValue else -1
self.lastTickMs = time.ticks_ms()
self.lock.release()

def stop(self):
self.lock.acquire()
self.needQuit = True
self.lock.release()

def join(self):
self.execThread.join()

def runFunc(self):
while True:
self.lock.acquire()
if True == self.needQuit:
self.lock.release()
return

if self.currentValue != self.targetValue:
(tmpTicsMs, calculatedValue) = EscControl.calculateNextStep(self.currentValue, self.rotationSpeed,
self.multiplier, self.lastTickMs)
valueToWrite = 0
if self.multiplier > 0:
valueToWrite = calculatedValue if calculatedValue <= self.targetValue else self.targetValue
else:
valueToWrite = calculatedValue if calculatedValue > self.targetValue else self.targetValue

self.servo.writeAngle(self.targetValueToAngle(valueToWrite))
self.currentValue = valueToWrite
self.lastTickMs = tmpTicsMs

self.lock.release()
time.sleep(self.sleepInterval)

def setup(self):
self.servo.writeAngle(0)
time.sleep(0.1)
self.servo.writeAngle(180)
time.sleep(0.1)
self.servo.writeAngle(90)
time.sleep(0.1)
self.currentValue = 0
self.targetValue = 0
print('arm done')

@staticmethod
def hasTransition(fromValue, toValue):
if fromValue == 0:
return True;
return fromValue * toValue < 0

@staticmethod
def calculateNextStep(currentValue, speedMax, multiplier, lastTickMs):
currentTickMs = time.ticks_ms()
timeDelta = currentTickMs - lastTickMs
delta = speedMax * timeDelta / 1000
return (currentTickMs, currentValue + delta * multiplier)

0 comments on commit ec244f5

Please sign in to comment.