Mise à jour de 'PWMDriver.py'

This commit is contained in:
Gzod01 2023-05-27 11:43:41 +02:00
parent ce5fc4c445
commit 9ef67cb37d

View file

@ -3,14 +3,14 @@ from Adafruit_PCA9685 import PCA9685
class PWMDriver: class PWMDriver:
def __init__(self): def __init__(self):
self.driver = PCA9685() self.driver = PCA9685()
self.driver.set_pwm_freq(50) self.driver.set_pwm_freq(50)
self.servoProfiles = { self.servoProfiles = {
'default': {'range': 180, 'min': 65, 'max': 530}, 'default': {'range': 180, 'min': 65, 'max': 530},
'lidar': {'range': 180, 'min': 75, 'max': 510}, 'lidar': {'range': 180, 'min': 75, 'max': 510},
'china': {'range': 180, 'min': 75, 'max': 510}, 'china': {'range': 180, 'min': 75, 'max': 510},
'flag': {'range': 180, 'min': 100, 'max': 480}, 'flag': {'range': 180, 'min': 100, 'max': 480},
'rev': {'range': 270, 'min': 95, 'max': 600}, # 552 'rev': {'range': 270, 'min': 95, 'max': 600}, # 552
} }
#définir la fréquence du signal de sortie à 50hz #définir la fréquence du signal de sortie à 50hz
@ -26,29 +26,26 @@ class PWMDriver:
ini_esc(LESC[1]) ini_esc(LESC[1])
ini_esc(LESC[2]) ini_esc(LESC[2])
ini_esc(LESC[3]) ini_esc(LESC[3])
def setAngle(self, slot, angle, profileName = 'default'):
def setAngle(self, slot, angle, profileName = 'default'): profile = None
profile = None if type(profileName) is dict:
if type(profileName) is dict: profile = profileName
profile = profileName if profile == None:
if profile == None: if profileName not in self.servoProfiles:
if profileName not in self.servoProfiles: profileName = 'default'
profileName = 'default' profile = self.servoProfiles[profileName]
profile = self.servoProfiles[profileName] if 'range' not in profile:
if 'range' not in profile: profile['range'] = 180
profile['range'] = 180 if angle < 0 or angle > profile['range']:
if angle < 0 or angle > profile['range']: print('PWMDriver: Invalid range passed ' + angle + ' but range is ' + profile['range'])
print('PWMDriver: Invalid range passed ' + angle + ' but range is ' + profile['range']) if slot > 15 or slot < 0:
if slot > 15 or slot < 0: print('> ERR!!! Invalid slot passed to setAngle!')
print('> ERR!!! Invalid slot passed to setAngle!') return
return pulse = int(self.mappyt(angle, 0, profile['range'], profile['min'], profile['max']))
pulse = int(self.mappyt(angle, 0, profile['range'], profile['min'], profile['max'])) #print('setting slot', slot, 'to angle', angle, 'with profile', profileName, profile, pulse)
#print('setting slot', slot, 'to angle', angle, 'with profile', profileName, profile, pulse) self.driver.set_pwm(slot, 0, pulse)
self.driver.set_pwm(slot, 0, pulse) def setPwm(self, slot, off, on):
#self.driver.set_pwm(slot, off, on)
def setPwm(self, slot, off, on): self.driver.set_pwm(slot, off,on)
#self.driver.set_pwm(slot, off, on) def mappyt(self, x, inMin, inMax, outMin, outMax):
self.driver.set_pwm(slot, off,on) return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin
def mappyt(self, x, inMin, inMax, outMin, outMax):
return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin