Alles verbessert
This commit is contained in:
@@ -54,6 +54,15 @@ DRIVE_PIN_KEYS = {
|
||||
'rr': 'pin_drive_rr',
|
||||
}
|
||||
|
||||
DRIVE_NEUTRAL_KEYS = {
|
||||
'fl': 'drive_pwm_neutral_fl',
|
||||
'fr': 'drive_pwm_neutral_fr',
|
||||
'cl': 'drive_pwm_neutral_cl',
|
||||
'cr': 'drive_pwm_neutral_cr',
|
||||
'rl': 'drive_pwm_neutral_rl',
|
||||
'rr': 'drive_pwm_neutral_rr',
|
||||
}
|
||||
|
||||
ACTION_SETTINGS = {
|
||||
'steer_left': {
|
||||
'kind': 'steer',
|
||||
@@ -94,7 +103,10 @@ class PCA9685Direct:
|
||||
try:
|
||||
import smbus
|
||||
except ImportError as exc:
|
||||
raise RuntimeError('smbus ist nicht installiert.') from exc
|
||||
try:
|
||||
import smbus2 as smbus
|
||||
except ImportError as inner_exc:
|
||||
raise RuntimeError('smbus ist nicht installiert.') from inner_exc
|
||||
|
||||
self.address = address
|
||||
self.bus = smbus.SMBus(busnum)
|
||||
@@ -176,7 +188,10 @@ class MotorTester:
|
||||
self.config = _load_config(self.config_path)
|
||||
self.pwm.set_pwm_freq(50)
|
||||
self.steer_pwm_range = int(self.config['steer_pwm_range'])
|
||||
self.drive_pwm_neutral = int(self.config['drive_pwm_neutral'])
|
||||
self.drive_pwm_neutral = {
|
||||
wheel: int(self.config[DRIVE_NEUTRAL_KEYS[wheel]])
|
||||
for wheel in WHEELS
|
||||
}
|
||||
self.drive_pwm_range = int(self.config['drive_pwm_range'])
|
||||
|
||||
def _steer_pin(self, wheel):
|
||||
@@ -188,10 +203,12 @@ class MotorTester:
|
||||
def _drive_pin(self, wheel):
|
||||
return int(self.config[DRIVE_PIN_KEYS[wheel]])
|
||||
|
||||
def set_drive_neutral_pwm(self, pwm_value):
|
||||
duty_cycle = int(pwm_value)
|
||||
def _drive_neutral(self, wheel):
|
||||
return int(self.drive_pwm_neutral[wheel])
|
||||
|
||||
def set_drive_neutral_pwm(self, values):
|
||||
for wheel in WHEELS:
|
||||
self.pwm.set_pwm(self._drive_pin(wheel), 0, duty_cycle)
|
||||
self.pwm.set_pwm(self._drive_pin(wheel), 0, int(values[wheel]))
|
||||
time.sleep(0.03)
|
||||
|
||||
def center_all_steering(self):
|
||||
@@ -201,7 +218,7 @@ class MotorTester:
|
||||
|
||||
def stop_all_driving(self):
|
||||
for wheel in WHEELS:
|
||||
self.pwm.set_pwm(self._drive_pin(wheel), 0, self.drive_pwm_neutral)
|
||||
self.pwm.set_pwm(self._drive_pin(wheel), 0, self._drive_neutral(wheel))
|
||||
time.sleep(0.03)
|
||||
|
||||
def set_steering_angle(self, wheel, angle):
|
||||
@@ -237,12 +254,12 @@ class MotorTester:
|
||||
def test_driving(self, wheel, speed, duration):
|
||||
self.pwm.set_pwm(self._steer_pin(wheel), 0, self._steer_neutral(wheel))
|
||||
duty_cycle = int(
|
||||
self.drive_pwm_neutral +
|
||||
self._drive_neutral(wheel) +
|
||||
speed / 100.0 * self.drive_pwm_range * WHEEL_DIRECTIONS[wheel]
|
||||
)
|
||||
self.pwm.set_pwm(self._drive_pin(wheel), 0, duty_cycle)
|
||||
time.sleep(duration)
|
||||
self.pwm.set_pwm(self._drive_pin(wheel), 0, self.drive_pwm_neutral)
|
||||
self.pwm.set_pwm(self._drive_pin(wheel), 0, self._drive_neutral(wheel))
|
||||
|
||||
def execute(self, wheel, action):
|
||||
if wheel not in WHEELS:
|
||||
@@ -418,30 +435,61 @@ def save_steering_neutral_values(values, config_path=None):
|
||||
|
||||
def get_drive_neutral_value(config_path=None):
|
||||
tester = MotorTester(config_path=config_path)
|
||||
values = {}
|
||||
for wheel in WHEELS:
|
||||
key = DRIVE_NEUTRAL_KEYS[wheel]
|
||||
values[wheel] = {
|
||||
'wheel': wheel,
|
||||
'wheel_label': WHEEL_LABELS[wheel],
|
||||
'key': key,
|
||||
'value': int(tester.config[key]),
|
||||
'pin': tester._drive_pin(wheel),
|
||||
}
|
||||
return {
|
||||
'config_path': tester.config_path,
|
||||
'value': int(tester.config['drive_pwm_neutral']),
|
||||
'pins': {wheel: tester._drive_pin(wheel) for wheel in WHEELS},
|
||||
'values': values,
|
||||
}
|
||||
|
||||
|
||||
def preview_drive_neutral_value(pwm_value, config_path=None):
|
||||
def preview_drive_neutral_value(values, config_path=None):
|
||||
tester = MotorTester(config_path=config_path)
|
||||
value = int(pwm_value)
|
||||
preview_values = {}
|
||||
for wheel in WHEELS:
|
||||
if wheel not in values:
|
||||
raise ValueError(f'Fahr-Neutralwert fehlt: {DRIVE_NEUTRAL_KEYS[wheel]}')
|
||||
preview_values[wheel] = int(values[wheel])
|
||||
|
||||
tester.center_all_steering()
|
||||
tester.set_drive_neutral_pwm(value)
|
||||
tester.set_drive_neutral_pwm(preview_values)
|
||||
return {
|
||||
'value': value,
|
||||
'pins': {wheel: tester._drive_pin(wheel) for wheel in WHEELS},
|
||||
'values': {
|
||||
wheel: {
|
||||
'wheel': wheel,
|
||||
'wheel_label': WHEEL_LABELS[wheel],
|
||||
'key': DRIVE_NEUTRAL_KEYS[wheel],
|
||||
'value': preview_values[wheel],
|
||||
'pin': tester._drive_pin(wheel),
|
||||
}
|
||||
for wheel in WHEELS
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def save_drive_neutral_value(pwm_value, config_path=None):
|
||||
def save_drive_neutral_value(values, config_path=None):
|
||||
tester = MotorTester(config_path=config_path)
|
||||
value = int(pwm_value)
|
||||
_update_config_values(tester.config_path, {'drive_pwm_neutral': value})
|
||||
updates = {}
|
||||
for wheel in WHEELS:
|
||||
key = DRIVE_NEUTRAL_KEYS[wheel]
|
||||
if key not in values:
|
||||
raise ValueError(f'Fahr-Neutralwert fehlt: {key}')
|
||||
updates[key] = int(values[key])
|
||||
|
||||
_update_config_values(tester.config_path, updates)
|
||||
tester.config = _load_config(tester.config_path)
|
||||
tester.drive_pwm_neutral = int(tester.config['drive_pwm_neutral'])
|
||||
tester.drive_pwm_neutral = {
|
||||
wheel: int(tester.config[DRIVE_NEUTRAL_KEYS[wheel]])
|
||||
for wheel in WHEELS
|
||||
}
|
||||
tester.center_all_steering()
|
||||
tester.set_drive_neutral_pwm(tester.drive_pwm_neutral)
|
||||
return get_drive_neutral_value(config_path=tester.config_path)
|
||||
|
||||
Reference in New Issue
Block a user