Skip to content

Commit

Permalink
Work on state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
jesskelly committed Mar 4, 2020
1 parent 7642282 commit be02afb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
18 changes: 15 additions & 3 deletions Measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class ADXL345:

def __init__(self, address=0x53):
self.address = address
self.setBandwidthRate(BW_RATE_25HZ)
self.setBandwidthRate(BW_RATE_400HZ)
self.setRange(RANGE_4G)
self.enableMeasurement()

Expand Down Expand Up @@ -145,7 +145,7 @@ def getAxes(self, gforce=False):
# Try to catch the Interrupt and close the file saving the results
try:
# Interval to update measures
timeInterval = 0.05
timeInterval = 0.020
dimDer = 2
dimStd = 20
dimFilter = 5
Expand Down Expand Up @@ -188,7 +188,19 @@ def getAxes(self, gforce=False):
values.update(utils.calculateSTD())
values.update(utils.calculateDerivative())

m.runOneStep(values)
state=m.runOneStep(values)
if(state=="Still_state"):
values["event"]=10
elif(state=="Moving_state"):
values["event"]=20
elif(state=="Bumping_state"):
values["event"]=30
elif(state=="Holding_state"):
values["event"]=40
elif(state=="Crash_state"):
values["event"]=50



# Save the information on the file
writer.writerow(values.values())
Expand Down
20 changes: 10 additions & 10 deletions plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ def plotData(data, message='Data', s=None, e=None):

for key in data.columns.values:
if(key!="time"):
if(key!="event"):
if(key=="current"):
dataG.append(go.Scatter(y=data[key][s:e],
x=data["time"][s:e],
yaxis='y2',
name=key))
else:
dataG.append(go.Scatter(y=data[key][s:e],
x=data["time"][s:e],
name=key))
#if(key!="event"):
if(key=="current"):
dataG.append(go.Scatter(y=data[key][s:e],
x=data["time"][s:e],
yaxis='y2',
name=key))
else:
dataG.append(go.Scatter(y=data[key][s:e],
x=data["time"][s:e],
name=key))

layout = dict(
title=message,
Expand Down
23 changes: 12 additions & 11 deletions safetySM.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,26 @@ def conditionAnd(op,data, names, values):

def still_state_transitions(self, data):
#if data["Std_norm"] >0.01 or abs(data["Der_norm"])>0.04 or data["Std_current"] >1 or abs(data["Der_current"])>10:
if self.conditionOr(">",data, ["Std_norm","Der_norm","Std_current","Der_current"], [0.01,0.04,1,10]):
#if self.conditionAnd(">",data, ["Std_norm","Der_norm","Std_current","Der_current"], [0.01,0.04,1,10]):
if self.conditionAnd(">",data, ["Std_norm","Std_current"], [0.01,1]):
newState = "Moving_state"
else:
newState = "Still_state"
return newState

def moving_state_transitions(self, data):
if self.conditionOr(">",data, ["Std_norm","Der_norm"], [0.1,1.1]):
if self.conditionOr(">",data, ["Std_norm","Der_norm"], [0.5,1.5]):
newState = "Crash_state"
elif self.conditionOr(">",data, ["Std_current","Der_current"], [10,30]):
newState = "Holding_state"
elif self.conditionOr(">",data, ["Std_norm","Der_norm","Std_current","Der_current"], [0.1,0.5,4,20]):
newState = "Bumping_state"
elif self.conditionOr("<",data, ["Std_norm","Der_norm","Std_current","Der_current"], [0.01,0.04,1,10]):
#elif self.conditionAnd(">",data, ["Std_current","Der_current"], [20,30]):
# newState = "Holding_state"
#elif self.conditionAnd(">",data, ["Std_norm","Der_norm","Std_current","Der_current"], [0.1,0.5,4,20]):
# newState = "Bumping_state"
elif self.conditionAnd("<",data, ["Std_norm","Std_current"], [0.01,1]):
newState = "Still_state"
else:
newState = "Moving_state"
return newState

def bumping_state_transitions(self, data):
if self.conditionOr("<",data, ["Std_norm","Der_norm","Std_current","Der_current"], [0.1,0.5,4,20]):
newState = "Moving_state"
Expand All @@ -66,14 +67,14 @@ def bumping_state_transitions(self, data):
return newState

def holding_state_transitions(self, data):
if self.conditionOr("<",data, ["Std_norm","Der_norm","Std_current","Der_current"], [0.1,0.5,10,30]):
if self.conditionAnd("<",data, ["Std_current","Der_current"], [20,30]):
newState = "Moving_state"
elif self.conditionOr("<",data, ["Std_norm","Der_norm","Std_current","Der_current"], [0.01,0.04,1,10]):
newState = "Still_state"
else:
newState = "Holding_state"
return newState

def crash_state_transitions(self, data):
if self.conditionOr("<",data, ["Std_norm","Der_norm","Std_current","Der_current"], [0.01,0.04,1,10]):
newState = "Still_state"
Expand All @@ -82,7 +83,7 @@ def crash_state_transitions(self, data):
return newState

def runOneStep(self,data):
self.m.runOneStep(data)
return self.m.runOneStep(data)


if __name__== "__main__":
Expand Down
1 change: 1 addition & 0 deletions statemachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ def runOneStep(self, cargo):
else:
print("State: ", newState)
self.handler = self.handlers[newState.upper()]
return newState

0 comments on commit be02afb

Please sign in to comment.