Better management of motor power settings in gpio.zerborg - no more offsets multiplied for max_power and hardcoded power coefficients, just pass the motor power in the configuration

This commit is contained in:
Fabio Manganiello 2018-04-13 15:12:31 +02:00
parent 0cfd67be51
commit b597097be3

View file

@ -22,36 +22,14 @@ class GpioZeroborgPlugin(Plugin):
_drive_thread = None _drive_thread = None
_can_run = False _can_run = False
_direction = None _direction = None
_power_offsets = {}
def __init__(self, v_in=8.4, v_out=6.0, def __init__(self, directions = {}):
power_offset_left_up=0.0, power_offset_right_up=0.0,
power_offset_left_down=0.0, power_offset_right_down=0.0,
power_offset_left_left=0.0, power_offset_left_right=0.0,
power_offset_right_left=0.0, power_offset_right_right=0.0):
import platypush.plugins.gpio.zeroborg.lib as ZeroBorg import platypush.plugins.gpio.zeroborg.lib as ZeroBorg
self.v_in = v_in self.directions = directions
self.v_out = v_out
self.max_power = v_out / float(v_in)
self.auto_mode = False self.auto_mode = False
self._direction = None self._direction = None
self._power_offsets = {
Direction.DIR_LEFT: {
Direction.DIR_UP: power_offset_left_up,
Direction.DIR_DOWN: power_offset_left_down,
Direction.DIR_LEFT: power_offset_left_left,
Direction.DIR_RIGHT: power_offset_left_right,
},
Direction.DIR_RIGHT: {
Direction.DIR_UP: power_offset_right_up,
Direction.DIR_DOWN: power_offset_right_down,
Direction.DIR_LEFT: power_offset_right_left,
Direction.DIR_RIGHT: power_offset_right_right,
},
}
self.zb = ZeroBorg.ZeroBorg() self.zb = ZeroBorg.ZeroBorg()
self.zb.Init() self.zb.Init()
@ -117,28 +95,19 @@ class GpioZeroborgPlugin(Plugin):
time.sleep(0.1) time.sleep(0.1)
if self._direction == Direction.DIR_UP.value: motor_1_power = motor_2_power = motor_3_power = motor_4_power = 0.0
left = 1.0 + self._power_offsets[Direction.DIR_LEFT][Direction.DIR_UP] if self._direction in self.directions:
right = 1.0 + self._power_offsets[Direction.DIR_RIGHT][Direction.DIR_UP] motor_1_power = self.directions[self._direction]['motor_1_power']
elif self._direction == Direction.DIR_DOWN.value: motor_2_power = self.directions[self._direction]['motor_2_power']
left = -1.0 - self._power_offsets[Direction.DIR_LEFT][Direction.DIR_DOWN] motor_3_power = self.directions[self._direction]['motor_3_power']
right = -1.0 - self._power_offsets[Direction.DIR_RIGHT][Direction.DIR_DOWN] motor_4_power = self.directions[self._direction]['motor_4_power']
elif self._direction == Direction.DIR_LEFT.value: elif self._direction:
left = -0.66 - self._power_offsets[Direction.DIR_LEFT][Direction.DIR_LEFT] logging.warning('Invalid direction {}, stopping motors'.format(self._direction))
right = 1.32 + self._power_offsets[Direction.DIR_RIGHT][Direction.DIR_LEFT]
elif self._direction == Direction.DIR_RIGHT.value:
left = 1.32 + self._power_offsets[Direction.DIR_LEFT][Direction.DIR_RIGHT]
right = -0.66 - self._power_offsets[Direction.DIR_RIGHT][Direction.DIR_RIGHT]
elif self._direction is not None:
logging.warning('Invalid direction: {}'.format(direction))
self.stop()
power = 0.75 # Still debugging the right power range self.zb.SetMotor1(motor_1_power)
self.zb.SetMotor2(motor_2_power)
self.zb.SetMotor1(power * left * self.max_power) self.zb.SetMotor3(motor_3_power)
self.zb.SetMotor2(power * left * self.max_power) self.zb.SetMotor4(motor_4_power)
self.zb.SetMotor3(power * -right * self.max_power)
self.zb.SetMotor4(power * -right * self.max_power)
self.auto_mode = False self.auto_mode = False