def _move(self, angle): done = False speed = self.speed while not done: wishedToSet = self.angleToPotential(angle) currentVal = self.pot.readRawValue() currentAngle = self.potentialToAngle(currentVal) print(F"to set:{angle}* current: {currentAngle}*") # print(F"to set:{wishedToSet} current: {currentVal}") if abs(currentVal - wishedToSet) < 20: self.move360Servo('stop', 0) done = True return angleDifference = abs(currentAngle - angle) if angleDifference < 100: newSpeed = mapValueToIntRange(angleDifference, 0, 100, 1, 100) print('slowing down', newSpeed) speed = 0.1 if currentVal > wishedToSet: self.move360Servo('forward', speed) elif currentVal < wishedToSet: self.move360Servo('backward', speed) else: self.move360Servo('stop', 0) done = True time.sleep(0.05)
def readPotentiometers(potentiometers, scr): i = 0 for p in potentiometers: val = None if p['adc'] == 0: val = adc0.read_adc(p['pin'], GAIN) else: val = adc1.read_adc(p['pin'], GAIN) rawVal = val valInRange = mapValueToIntRange(val, 0, 26208, 0, 4096) if valInRange > p['max']: p['max'] = valInRange if valInRange < p['min']: p['min'] = valInRange scr.addstr(i, 0, F"{p['name']} min: {p['min']} max: {p['max']} val: {valInRange}") i += 1
def readPercentValue(self, value): mapValueToIntRange(value, self.minVal, self.maxVal, 0, 100)
def angleToPercent(self, angle): return mapValueToIntRange(angle, 0, self.max_angle, 0, 100)
def potentialToAngle(self, potential): return mapValueToIntRange(potential, self.pot.minVal, self.pot.maxVal, 0, self.max_angle)
def angleToPotential(self, angle): return mapValueToIntRange(angle, 0, self.max_angle, self.pot.minVal, self.pot.maxVal)
def getValueFromAngle(self, angle): return mapValueToIntRange(angle, 0, 360, 0, 4095)