1
0
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:
Andrey Pohilko 2017-09-22 19:16:44 +03:00
parent b518e42d53
commit 4fffe1fb5f
4 changed files with 49 additions and 24 deletions

View File

@ -228,7 +228,7 @@ hub.button.subscribe(callback)
### 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
from pylgbst import MoveHub
@ -238,9 +238,9 @@ def callback(value):
print("Voltage: %s" % value)
hub = MoveHub()
hub.battery.subscribe(callback)
hub.voltage.subscribe(callback)
time.sleep(1)
print ("Value: " % hub.battery.last_value)
print ("Value: " % hub.voltage.last_value)
```
### General Notes

View File

@ -12,8 +12,8 @@ PORT_A = 0x37
PORT_B = 0x38
PORT_AB = 0x39
PORT_TILT_SENSOR = 0x3A
PORT_SOMETHING1 = 0x3B
PORT_BATTERY = 0x3C # subscribing on this port showed correlation with power voltage
PORT_AMPERAGE = 0x3B
PORT_VOLTAGE = 0x3C
PORTS = {
PORT_A: "A",
@ -23,8 +23,8 @@ PORTS = {
PORT_D: "D",
PORT_LED: "LED",
PORT_TILT_SENSOR: "TILT_SENSOR",
PORT_SOMETHING1: "UNK1",
PORT_BATTERY: "BATTERY",
PORT_AMPERAGE: "AMPERAGE",
PORT_VOLTAGE: "VOLTAGE",
}
# PACKET TYPES
@ -42,8 +42,8 @@ MSG_SENSOR_DATA = 0x45
MSG_SENSOR_SUBSCRIBE_ACK = 0x47
# DEVICE TYPES
DEV_UNKNOWN1 = 0x15 # one of them is button? onboard temperature? maybe another kind of voltage, they have same params
DEV_BATTERY = 0x14
DEV_AMPERAGE = 0x15 # one of them is button? onboard temperature? maybe another kind of voltage, they have same params
DEV_VOLTAGE = 0x14
DEV_DCS = 0x25
DEV_IMOTOR = 0x26
DEV_MOTOR = 0x27
@ -56,8 +56,8 @@ DEVICE_TYPES = {
DEV_MOTOR: "MOTOR",
DEV_TILT_SENSOR: "TILT_SENSOR",
DEV_LED: "LED",
DEV_UNKNOWN1: "UNKNOWN #1",
DEV_BATTERY: "BATTERY",
DEV_AMPERAGE: "AMPERAGE",
DEV_VOLTAGE: "VOLTAGE",
}
# NOTIFICATIONS

View File

@ -4,7 +4,8 @@ from struct import pack
from pylgbst.comms import BLEConnection, str2hex, get_byte
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')
@ -19,7 +20,7 @@ class MoveHub(object):
:type led: LED
:type tilt_sensor: TiltSensor
:type button: Button
:type battery: Battery
:type amperage: Voltage
:type color_distance_sensor: pylgbst.peripherals.ColorDistanceSensor
:type motor_external: EncodedMotor
:type port_C: Peripheral
@ -39,7 +40,8 @@ class MoveHub(object):
# shorthand fields
self.button = Button(self)
self.led = None
self.battery = None
self.amperage = None
self.voltage = None
self.motor_A = None
self.motor_B = None
self.motor_AB = None
@ -59,7 +61,7 @@ class MoveHub(object):
builtin_devices = ()
for num in range(0, 60):
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:
return
log.debug("Waiting for builtin devices to appear: %s", builtin_devices)
@ -148,8 +150,10 @@ class MoveHub(object):
self.devices[port] = LED(self, port)
elif dev_type == DEV_TILT_SENSOR:
self.devices[port] = TiltSensor(self, port)
elif dev_type == DEV_BATTERY:
self.devices[port] = Battery(self, port)
elif dev_type == DEV_AMPERAGE:
self.devices[port] = Amperage(self, port)
elif dev_type == DEV_VOLTAGE:
self.devices[port] = Voltage(self, port)
else:
log.debug("Unhandled peripheral type 0x%x on port 0x%x", dev_type, port)
self.devices[port] = Peripheral(self, port)
@ -168,8 +172,10 @@ class MoveHub(object):
self.led = self.devices[port]
elif port == PORT_TILT_SENSOR:
self.tilt_sensor = self.devices[port]
elif port == PORT_BATTERY:
self.battery = self.devices[port]
elif port == PORT_AMPERAGE:
self.amperage = self.devices[port]
elif port == PORT_VOLTAGE:
self.voltage = self.devices[port]
else:
log.debug("Unhandled port: %s", PORTS[port])

View File

@ -377,25 +377,44 @@ class ColorDistanceSensor(Peripheral):
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
MODE2 = 0x01 # give more frequent notifications, maybe different voltage (cpu vs board?)
def __init__(self, parent, port):
super(Battery, self).__init__(parent, port)
super(Voltage, self).__init__(parent, port)
self.last_value = None
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?
# 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
# good 7.5v ~= 3892
# liion 5v ~= 2100
def handle_port_data(self, data):
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)