dknathalage / SIT329-Project

0 stars 3 forks source link

Design the algorithm to infer position from the sensor readings #5

Open dknathalage opened 2 years ago

nadavfedida commented 2 years ago

This is the Position matrix that is decided on from another issue.

numberOfSensors = 4
numberOfsamples = 100
Inputs =  np.random.randint(0, 4095, size=(numberOfSensors, numberOfsamples))

Matrix = [('TopTube',[True,False,True,True]),
            ('Pantani',[True,False,False,False]),
            ('Froome',[True,False,True,False]),
            ('Elbows',[False,True,False,False])]

threshold = [2000, 2000, 2000, 2000]

And this is the code where all the sensor's input are taken in, then calculate the position by cross-referencing the above table

state = Position[0]
connected = 0
if  __name__ == '__main__':
    try:
          if connected == 0:
              client = mqtt.Client() 
              client.on_connect = on_connect 
              client.connect("test.mosquitto.org", 1883, 60)
              #client.loop_forever()
              connected = 1
              # start timer to check each state duration   
              start = time.time()
          for i in range(numberOfsamples):
              Dictionary = [False , False , False , False ]
              for j in range(len(Inputs)):
                  if Inputs[j][i] >= threshold[j]:
                      Dictionary[j] = True

              print(Dictionary)
              for i ,v  in enumerate(Matrix):
                  if Dictionary == v[1]:
                      POS = Matrix[i][0]
                      print(POS, 'MATCH >> Sensor: ', i, '\n')
                      end = time.time()
                      endTime = end - start
                      if state != POS:
                          publishState(POS,endTime)
                          start = end
                          state = POS
              time.sleep(0.5)

            publishState("End of Session",0)
            time.sleep(3)

    except KeyboardInterrupt:
        print("Measurement stopped by User")
        publishState("End of Session",0)
        time.sleep(3)

The last part is where each state is already calculated, the function below is called to send the body position to the subscribes MQTT clients

def publishState( matrix,  timerLapsed):
    posDict = {"positon": None, "time":0 }

    temp = " %.2f" % timerLapsed
    posDict.update({"positon":matrix, "time":temp })

    toSend = json.dumps(posDict)
    print(f'Sending {matrix} with time {temp}')
    publish.single("PositionCheck", toSend , hostname="test.mosquitto.org")