It is often needed to persist some robot data beyond reboots. The GoPiGo3 API uses the file gpg3_config.json for this purpose.
For each of my robots, I use the same concept and have a package to encapsulate and marshal access to my robot’s file.
# file: daveDataJson.py
#
# Serialize data values to /home/pi/GoPi5Go/daveData.json
# (running ./daveDataJson.py will create the file)
#
# Methods:
# saveData(dataname, datavalue, logit=False) # adds datanaem:datavalue to rosbotData.json file
# getData(dataname=None) # either returns dictionary with all values, or just value of passed name
# delData(dataname) # delete item from rosbotData.json
# printData() # prints contents of rosbotData.json
#
"""
Example from Carl:
lastDocking : ---- Docking 4573 completed at 8.1 v after 2.6 h playtime
chargeCycles : 4573
lastDismount : ---- Dismount 4573 at 10.9 v after 2.5 h recharge
dockingState : 1
chargingState : 1
chargeConditioning : 0
lastDismountTime : 2024-04-15 06:47:03
lastRechargeDuration : 2.5
newBatterySetDate : 2023-03-21
newBatterySetAtDocking : 3652
newBatterySetAtLifeHours : 36810.9
newBatterySetDesc : 8x Eneloop White 2000 mAh NiMH AA cells
lastDockingTime : 2024-04-15 04:15:21
lastPlaytimeDuration : 2.6
"""
...
and a utility for command line editing the datafile
pi@GoPi5Go:~/GoPi5Go/ros2ws $ ../utils/mod_daveDataJson.py
daveData.json contents:
lastDocking : ---- GoPi5Go-Dave ROS 2 Docking 1024 : success at battery 10.1v after 2.8 h playtime
chargeCycles : 1024
lastDismount : ---- GoPi5Go-Dave ROS 2 Undocking, Charge Current 99 mA 12.1v after 2.6 h charging
dockingState : undocked
chargingState : discharging
lastDismountTime : 2024-09-18 09:09:12
lastRechargeDuration : 2.6
newBatteryDate : 2024-04-19
newBatteryAtDocking : 416
newBatteryAtLifeHours : 694
newBatteryDesc : TalentCell YB1203000-USB 12v 3000mAh
lastDockingTime : 2024-09-18 06:33:20
lastPlaytimeDuration : 2.8
key: lastDocking : ---- GoPi5Go-Dave ROS 2 Docking 1024 : success at battery 10.1v after 2.8 h playtime
Keep value "---- GoPi5Go-Dave ROS 2 Docking 1024 : success at battery 10.1v after 2.8 h playtime " y/n? y
key: chargeCycles : 1024
Keep value "1024" y/n? n
Enter New Value (without quotes): 1024
Use value "1024" y/n? y
Saved chargeCycles: 1024
...
To use it:
import daveDataJson
...
chargeCycles = int(daveDataJson.getData('chargeCycles')) + 1
daveDataJson.saveData('chargeCycles', chargeCycles)
...
Ideally, error handling should be used…
I probably should have named it “robotDataJson.py” and the file “~/robotData.json” so it could have been reused more easily.