Two python scripts have been created:Activity.pywhich acquires and stores accelerometer data andUpdateDB.pywhich contains the functions to access the Database.
if (Gx<-1.5 or Gy<-1.5 or Gz<-1.5 or Gx>1.5 or Gy>1.5 or Gz>1.5):
state = 0
elif (Ax>0.1 and Ax<0.9 and Ay>-1.2 and Ay<-0.4 and Az<0.7 and Az>-0.1):
state = 1
elif (Ax>0.6 and Ax<1.4 and Ay>-0.4 and Ay<0.4 and Az<0.0 and Az>-0.8):
state = 2
elif (Ax>0.1 and Ax<0.9 and Ay>0.4 and Ay<1.2 and Az<0.7 and Az>-0.1):
state = 3
elif (Ax<0.4 and Ax>-0.4 and Ay>-0.4 and Ay<0.4 and Az<1.4 and Az>0.6):
state = 4
elif (Ax>-0.9 and Ax<-0.1 and Ay>-1.2 and Ay<-0.4 and Az<0.1 and Az>-0.7):
state = 5
elif (Ax>-1.3 and Ax<-0.5 and Ay>-0.4 and Ay<0.4 and Az<0.8 and Az>0.0):
state = 6
elif (Ax>-0.9 and Ax<-0.1 and Ay>0.4 and Ay<1.2 and Az<0.1 and Az>-0.7):
state = 7
elif (Ax<0.4 and Ax>-0.4 and Ay>-0.4 and Ay<0.4 and Az<-0.6 and Az>-1.4):
state = 8
else:
state = 0
return state
Functions for reading battery data:
def read_voltage():
read = Battery
_bus.read_word_data(Battery
_Address, 2)
swapped = struct.unpack("H", read))[0]
voltage = swapped * 78.125 /1000000
return voltage
def read_capacity():
read = Battery
_bus.read_word_data(Battery
_Address, 4)
swapped = struct.unpack("H", read))[0]
capacity = swapped/256
return capacity
Initialising accelerometer and necessary variables:
note: a 15 second sleep is required to avoid errors by running the script at system startup.
MPU_Init()
cur_state = 0
old_state = 0
cur_capacity = 0
old_capacity = 0
sleep(15)
This cycle checks every second the accelerometer and battery parameters and, in case of variations, updates the database. At midnight or in case of changed date, the function that calculates the summary of the day is called.
while True:
cur_state = read_state()
cur_datetime = datetime.now()
last_datetime = mdb.check_last_date()
if cur_datetime.day==last_datetime.day and cur_datetime.month==last_datetime.month and cur_datetime.year==last_datetime.year: