mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
Changed battery stuff
This commit is contained in:
parent
b518e42d53
commit
4fffe1fb5f
@ -228,7 +228,7 @@ hub.button.subscribe(callback)
|
|||||||
|
|
||||||
### Power Voltage & Battery
|
### Power Voltage & Battery
|
||||||
|
|
||||||
`MoveHub` class has field `battery` to subscribe to battery voltage status. Callback accepts single parameter with current value. The range of values is unknown, it's 2-byte integer. Every time data is received, value is also written into `last_value` field of Battery object.
|
`MoveHub` class has field `voltage` to subscribe to battery voltage status. Callback accepts single parameter with current value. The range of values is float between `0` and `1.0`. Every time data is received, value is also written into `last_value` field of `Voltage` object. Values less than `0.2` are known as lowest values, when unit turns off.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from pylgbst import MoveHub
|
from pylgbst import MoveHub
|
||||||
@ -238,9 +238,9 @@ def callback(value):
|
|||||||
print("Voltage: %s" % value)
|
print("Voltage: %s" % value)
|
||||||
|
|
||||||
hub = MoveHub()
|
hub = MoveHub()
|
||||||
hub.battery.subscribe(callback)
|
hub.voltage.subscribe(callback)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
print ("Value: " % hub.battery.last_value)
|
print ("Value: " % hub.voltage.last_value)
|
||||||
```
|
```
|
||||||
|
|
||||||
### General Notes
|
### General Notes
|
||||||
|
@ -12,8 +12,8 @@ PORT_A = 0x37
|
|||||||
PORT_B = 0x38
|
PORT_B = 0x38
|
||||||
PORT_AB = 0x39
|
PORT_AB = 0x39
|
||||||
PORT_TILT_SENSOR = 0x3A
|
PORT_TILT_SENSOR = 0x3A
|
||||||
PORT_SOMETHING1 = 0x3B
|
PORT_AMPERAGE = 0x3B
|
||||||
PORT_BATTERY = 0x3C # subscribing on this port showed correlation with power voltage
|
PORT_VOLTAGE = 0x3C
|
||||||
|
|
||||||
PORTS = {
|
PORTS = {
|
||||||
PORT_A: "A",
|
PORT_A: "A",
|
||||||
@ -23,8 +23,8 @@ PORTS = {
|
|||||||
PORT_D: "D",
|
PORT_D: "D",
|
||||||
PORT_LED: "LED",
|
PORT_LED: "LED",
|
||||||
PORT_TILT_SENSOR: "TILT_SENSOR",
|
PORT_TILT_SENSOR: "TILT_SENSOR",
|
||||||
PORT_SOMETHING1: "UNK1",
|
PORT_AMPERAGE: "AMPERAGE",
|
||||||
PORT_BATTERY: "BATTERY",
|
PORT_VOLTAGE: "VOLTAGE",
|
||||||
}
|
}
|
||||||
|
|
||||||
# PACKET TYPES
|
# PACKET TYPES
|
||||||
@ -42,8 +42,8 @@ MSG_SENSOR_DATA = 0x45
|
|||||||
MSG_SENSOR_SUBSCRIBE_ACK = 0x47
|
MSG_SENSOR_SUBSCRIBE_ACK = 0x47
|
||||||
|
|
||||||
# DEVICE TYPES
|
# DEVICE TYPES
|
||||||
DEV_UNKNOWN1 = 0x15 # one of them is button? onboard temperature? maybe another kind of voltage, they have same params
|
DEV_AMPERAGE = 0x15 # one of them is button? onboard temperature? maybe another kind of voltage, they have same params
|
||||||
DEV_BATTERY = 0x14
|
DEV_VOLTAGE = 0x14
|
||||||
DEV_DCS = 0x25
|
DEV_DCS = 0x25
|
||||||
DEV_IMOTOR = 0x26
|
DEV_IMOTOR = 0x26
|
||||||
DEV_MOTOR = 0x27
|
DEV_MOTOR = 0x27
|
||||||
@ -56,8 +56,8 @@ DEVICE_TYPES = {
|
|||||||
DEV_MOTOR: "MOTOR",
|
DEV_MOTOR: "MOTOR",
|
||||||
DEV_TILT_SENSOR: "TILT_SENSOR",
|
DEV_TILT_SENSOR: "TILT_SENSOR",
|
||||||
DEV_LED: "LED",
|
DEV_LED: "LED",
|
||||||
DEV_UNKNOWN1: "UNKNOWN #1",
|
DEV_AMPERAGE: "AMPERAGE",
|
||||||
DEV_BATTERY: "BATTERY",
|
DEV_VOLTAGE: "VOLTAGE",
|
||||||
}
|
}
|
||||||
|
|
||||||
# NOTIFICATIONS
|
# NOTIFICATIONS
|
||||||
|
@ -4,7 +4,8 @@ from struct import pack
|
|||||||
|
|
||||||
from pylgbst.comms import BLEConnection, str2hex, get_byte
|
from pylgbst.comms import BLEConnection, str2hex, get_byte
|
||||||
from pylgbst.constants import *
|
from pylgbst.constants import *
|
||||||
from pylgbst.peripherals import Button, EncodedMotor, ColorDistanceSensor, LED, TiltSensor, Battery, Peripheral
|
from pylgbst.peripherals import Button, EncodedMotor, ColorDistanceSensor, LED, TiltSensor, Voltage, Peripheral, \
|
||||||
|
Amperage
|
||||||
|
|
||||||
log = logging.getLogger('movehub')
|
log = logging.getLogger('movehub')
|
||||||
|
|
||||||
@ -19,7 +20,7 @@ class MoveHub(object):
|
|||||||
:type led: LED
|
:type led: LED
|
||||||
:type tilt_sensor: TiltSensor
|
:type tilt_sensor: TiltSensor
|
||||||
:type button: Button
|
:type button: Button
|
||||||
:type battery: Battery
|
:type amperage: Voltage
|
||||||
:type color_distance_sensor: pylgbst.peripherals.ColorDistanceSensor
|
:type color_distance_sensor: pylgbst.peripherals.ColorDistanceSensor
|
||||||
:type motor_external: EncodedMotor
|
:type motor_external: EncodedMotor
|
||||||
:type port_C: Peripheral
|
:type port_C: Peripheral
|
||||||
@ -39,7 +40,8 @@ class MoveHub(object):
|
|||||||
# shorthand fields
|
# shorthand fields
|
||||||
self.button = Button(self)
|
self.button = Button(self)
|
||||||
self.led = None
|
self.led = None
|
||||||
self.battery = None
|
self.amperage = None
|
||||||
|
self.voltage = None
|
||||||
self.motor_A = None
|
self.motor_A = None
|
||||||
self.motor_B = None
|
self.motor_B = None
|
||||||
self.motor_AB = None
|
self.motor_AB = None
|
||||||
@ -59,7 +61,7 @@ class MoveHub(object):
|
|||||||
builtin_devices = ()
|
builtin_devices = ()
|
||||||
for num in range(0, 60):
|
for num in range(0, 60):
|
||||||
builtin_devices = (self.led, self.motor_A, self.motor_B,
|
builtin_devices = (self.led, self.motor_A, self.motor_B,
|
||||||
self.motor_AB, self.tilt_sensor, self.button, self.battery)
|
self.motor_AB, self.tilt_sensor, self.button, self.amperage, self.voltage)
|
||||||
if None not in builtin_devices:
|
if None not in builtin_devices:
|
||||||
return
|
return
|
||||||
log.debug("Waiting for builtin devices to appear: %s", builtin_devices)
|
log.debug("Waiting for builtin devices to appear: %s", builtin_devices)
|
||||||
@ -148,8 +150,10 @@ class MoveHub(object):
|
|||||||
self.devices[port] = LED(self, port)
|
self.devices[port] = LED(self, port)
|
||||||
elif dev_type == DEV_TILT_SENSOR:
|
elif dev_type == DEV_TILT_SENSOR:
|
||||||
self.devices[port] = TiltSensor(self, port)
|
self.devices[port] = TiltSensor(self, port)
|
||||||
elif dev_type == DEV_BATTERY:
|
elif dev_type == DEV_AMPERAGE:
|
||||||
self.devices[port] = Battery(self, port)
|
self.devices[port] = Amperage(self, port)
|
||||||
|
elif dev_type == DEV_VOLTAGE:
|
||||||
|
self.devices[port] = Voltage(self, port)
|
||||||
else:
|
else:
|
||||||
log.debug("Unhandled peripheral type 0x%x on port 0x%x", dev_type, port)
|
log.debug("Unhandled peripheral type 0x%x on port 0x%x", dev_type, port)
|
||||||
self.devices[port] = Peripheral(self, port)
|
self.devices[port] = Peripheral(self, port)
|
||||||
@ -168,8 +172,10 @@ class MoveHub(object):
|
|||||||
self.led = self.devices[port]
|
self.led = self.devices[port]
|
||||||
elif port == PORT_TILT_SENSOR:
|
elif port == PORT_TILT_SENSOR:
|
||||||
self.tilt_sensor = self.devices[port]
|
self.tilt_sensor = self.devices[port]
|
||||||
elif port == PORT_BATTERY:
|
elif port == PORT_AMPERAGE:
|
||||||
self.battery = self.devices[port]
|
self.amperage = self.devices[port]
|
||||||
|
elif port == PORT_VOLTAGE:
|
||||||
|
self.voltage = self.devices[port]
|
||||||
else:
|
else:
|
||||||
log.debug("Unhandled port: %s", PORTS[port])
|
log.debug("Unhandled port: %s", PORTS[port])
|
||||||
|
|
||||||
|
@ -377,25 +377,44 @@ class ColorDistanceSensor(Peripheral):
|
|||||||
log.debug("Unhandled data in mode %s: %s", self._port_subscription_mode, str2hex(data))
|
log.debug("Unhandled data in mode %s: %s", self._port_subscription_mode, str2hex(data))
|
||||||
|
|
||||||
|
|
||||||
class Battery(Peripheral):
|
class Voltage(Peripheral):
|
||||||
MODE1 = 0x00 # give less frequent notifications
|
MODE1 = 0x00 # give less frequent notifications
|
||||||
MODE2 = 0x01 # give more frequent notifications, maybe different voltage (cpu vs board?)
|
MODE2 = 0x01 # give more frequent notifications, maybe different voltage (cpu vs board?)
|
||||||
|
|
||||||
def __init__(self, parent, port):
|
def __init__(self, parent, port):
|
||||||
super(Battery, self).__init__(parent, port)
|
super(Voltage, self).__init__(parent, port)
|
||||||
self.last_value = None
|
self.last_value = None
|
||||||
|
|
||||||
def subscribe(self, callback, mode=MODE1, granularity=1, async=False):
|
def subscribe(self, callback, mode=MODE1, granularity=1, async=False):
|
||||||
super(Battery, self).subscribe(callback, mode, granularity)
|
super(Voltage, self).subscribe(callback, mode, granularity)
|
||||||
|
|
||||||
# we know only voltage subscription from it. is it really battery or just onboard voltage?
|
# we know only voltage subscription from it. is it really battery or just onboard voltage?
|
||||||
# device has turned off on 1b0e000600453ba800 - 168d
|
# device has turned off on 1b0e00060045 3b a800 - 168 dec / 1b0e00060045 3c 0803 / 1b0e000600453c 0703
|
||||||
# moderate 9v ~= 3840
|
# moderate 9v ~= 3840
|
||||||
# good 7.5v ~= 3892
|
# good 7.5v ~= 3892
|
||||||
# liion 5v ~= 2100
|
# liion 5v ~= 2100
|
||||||
def handle_port_data(self, data):
|
def handle_port_data(self, data):
|
||||||
val = unpack("<H", data[4:6])[0]
|
val = unpack("<H", data[4:6])[0]
|
||||||
self.last_value = val
|
self.last_value = val / 4096.0
|
||||||
|
if self.last_value < 0.2:
|
||||||
|
logging.warning("Battery low! %s%%", int(100 * self.last_value))
|
||||||
|
self._notify_subscribers(self.last_value)
|
||||||
|
|
||||||
|
|
||||||
|
class Amperage(Peripheral):
|
||||||
|
MODE1 = 0x00 # give less frequent notifications
|
||||||
|
MODE2 = 0x01 # give more frequent notifications, maybe different voltage (cpu vs board?)
|
||||||
|
|
||||||
|
def __init__(self, parent, port):
|
||||||
|
super(Amperage, self).__init__(parent, port)
|
||||||
|
self.last_value = None
|
||||||
|
|
||||||
|
def subscribe(self, callback, mode=MODE1, granularity=1, async=False):
|
||||||
|
super(Amperage, self).subscribe(callback, mode, granularity)
|
||||||
|
|
||||||
|
def handle_port_data(self, data):
|
||||||
|
val = unpack("<H", data[4:6])[0]
|
||||||
|
self.last_value = val / 4096.0
|
||||||
self._notify_subscribers(self.last_value)
|
self._notify_subscribers(self.last_value)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user