This repository has been archived by the owner on Jul 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
LoadCell.py
53 lines (41 loc) · 1.57 KB
/
LoadCell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from Phidget22.Devices.VoltageRatioInput import *
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from libraries.PhidgetHelperFunctions import *
from robocluster import Device
import config
log = config.getLogger()
DATA_INTERVAL = 100
SERIAL_NUMBER = 532721
LoadDevice = Device('LoadCell', 'rover', network=config.network)
def on_Voltage(ph, voltageRatio):
log.info(voltageRatio)
if ph.getChannel() == 0:
weight = 8316008.31601 * float(voltageRatio) - 585.072765073
else:
weight = 385728.061716 * float(voltageRatio) + 95.2285438766
log.info('channel: {} weight: {}'.format(ph.getChannel(), weight))
@LoadDevice.task
async def send_reading():
await LoadDevice.publish('load_cell_weight', [ph.getChannel(), weight])
def onErrorHandler(ph, errorCode, errorString):
log.error(errorString)
def onAttachHandler(ph):
ph.setDataInterval(DATA_INTERVAL)
ph.setVoltageRatioChangeTrigger(0.0)
if(ph.getChannelSubclass() == ChannelSubclass.PHIDCHSUBCLASS_VOLTAGERATIOINPUT_SENSOR_PORT):
ph.setSensorType(VoltageRatioSensorType.SENSOR_TYPE_VOLTAGERATIO)
channels = []
for i in range(2):
channel = VoltageRatioInput()
channel.setDeviceSerialNumber(SERIAL_NUMBER)
channel.setChannel(i)
channel.setIsHubPortDevice(False)
channel.setHubPort(-1)
channel.setOnAttachHandler(onAttachHandler)
channel.setOnErrorHandler(onErrorHandler)
channel.setOnVoltageRatioChangeHandler(on_Voltage)
channel.openWaitForAttachment(5000)
channels.append(channel)
LoadDevice.start()
LoadDevice.wait()