-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot_battery_reader.py
54 lines (53 loc) · 1.95 KB
/
robot_battery_reader.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
54
import RPi.GPIO as GPIO
import json
import ntcore
import time
from mfrc522 import SimpleMFRC522
ntInst = ntcore.NetworkTableInstance.getDefault()
ntInst.startClient4("Battery Reader")
ntInst.setServerTeam(5104)
ntInst.startDSClient()
batteryDatatable = ntInst.getTable("Battery")
readBattPub = batteryDatatable.getBooleanTopic("has_tag").publish()
tagIDPub = batteryDatatable.getIntegerTopic("tag_id").publish()
battIDPub = batteryDatatable.getIntegerTopic("data/id").publish()
yearPub = batteryDatatable.getIntegerTopic("data/year").publish()
capPub = batteryDatatable.getDoubleTopic("data/cap").publish()
typePub = batteryDatatable.getStringTopic("data/type").publish()
mfgPub = batteryDatatable.getStringTopic("data/mfg").publish()
try:
reader = SimpleMFRC522()
hasReadBattery = False
while(True):
readBattPub.setDefault(False)
tagIDPub.setDefault(0)
battIDPub.setDefault(0)
yearPub.setDefault(0)
capPub.setDefault(0.0)
typePub.setDefault("NONE")
mfgPub.setDefault("NONE")
try:
id, text = reader.read()
batteryData = json.loads(text)
print("SUCCESS!")
print("-------------------")
print("Tag ID: " + str(id))
print("Battery ID: "+ str(batteryData[0]))
print("Purchase Year: " + str(batteryData[1]))
print("Battery Capacity (Ah): " + str(batteryData[2]))
print("Type: " + str(batteryData[3]))
print("Manufacturer: " + str(batteryData[4]))
print("-------------------")
hasReadBattery = True
readBattPub.set(hasReadBattery)
tagIDPub.set(id)
battIDPub.set(batteryData[0])
yearPub.set(batteryData[1])
capPub.set(batteryData[2])
typePub.set(batteryData[3])
mfgPub.set(batteryData[4])
except:
print("READ ERROR, RETRYING")
time.sleep(2)
finally:
GPIO.cleanup()