# Position Tracking with Embedded Systems

## Introduction

For this project an embedded system that can track a users position is needed. This system will be used to track their position to determine if a user is doing the exercises correctly.

## Objectives

- Design an embedded system that can track user position and sent the data to an Android App.
- Recieve the data from the embedded system in the Android App and sync the data to the current task for the user.

## Research and Analysis

### Choosing the sensor

For this project we have chosen LDR's as our primary sensor. The LDR's will be placed on the ground in a board and the user will stand on top of the board. The LDR's will be used to track the user's position. The LDR's will be connected to the ESP32 microcontroller and the data will be processed to determine the user's position.

We have chosen this sensor since it's one of the easiest and cheapest solutions to our problem. Other sensors like pressure sensors, accelerometers, and Wii Balance Board are either too expensive, not the most optimal for the task, or hard to integrate with other systems.

### Alternative Solutions

There are other sensors that can be used for position tracking, such as pressure sensors, Wii Balance Board or accelerometers. However, these sensors are either too expensive, not the most optimal for the task or hard to integrate with other systems. 

Example of other sensors that can be used for position tracking:

Wii Balance Board:
    - Description: The Wii Balance Board is a balance board that can measure a user's weight and center of balance. 
    - Pros: Low-cost.
    - Cons: Very hard to intergrate with other systems.
    - Cost: ~ 20 euros (https://www.amazon.nl/Nintendo-Wii-Balance-Board-Wii/dp/B0013E9HP6)

Pressure sensors:
    - Description: Pressure sensors can be used to measure the force applied by the user on the ground. By measuring the pressure distribution, the user's position can be determined.
    - Pros: High accuracy, can measure force applied by the user.
    - Cons: Expensive, will require additional hardware for data transfer.
    - Cost: ~ 33 euros (https://www.antratek.nl/flexiforce-a401-sensor-25lbs?gad_source=1&gclid=CjwKCAjwupGyBhBBEiwA0UcqaMMrIXGafsF2oE-15JaTPT5tDhfCyDHz2D2gSghyPvg11okv_QIFThoCw5oQAvD_BwE)

Accelerometers:
    - Description: Accelerometers can be used to measure the user's acceleration and orientation. By integrating the acceleration data, the user's position can be determined.
    - Pros: Can measure acceleration and orientation, cheap.
    - Cons: Will require additional hardware for data transfer.
    - Cost: ~ 5 euros (https://www.amazon.nl/versnellingsmeter-gyroscoop-versnellingssensor-converter-gegevensuitgang/dp/B07BVXN2GP/ref=asc_df_B07BVXN2GP/?tag=nlshogostdde-21&linkCode=df0&hvadid=430548884871&hvpos=&hvnetw=g&hvrand=5187253011954678898&hvpone=&hvptwo=&hvqmt=&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=1010543&hvtargid=pla-928293154057&psc=1&mcid=43bf111afa7b3ba593f4a49321683352)

### Wii Balance Board

The use of a Wii Balance Board was considered for this project. The Wii Balance Board is a balance board that can measure a user's weight and center of balance. The implementation of the board is very similar to the LDR's with an ESP32. The board also had bluetooth which makes it possible to connect to the Android App. However, the Wii Balance Board is very hard to integrate with other systems. The board is not designed to be used with other systems and the documentation is very limited. This makes it very hard to use the board for this project.

#### Problems with the Wii Balance Board

- The Wii Balance Board is not designed to be used with other systems. The documentation is very limited and there is no official SDK available.
- The Wii Balance Board does not use UUID's for the services and characteristics. This makes it very hard to connect to the board and read the data.
- When trying to connect to the Wii Balance Board, the board would show up as a device but would ask for a PIN code. The PIN code is not documented anywhere and there is no way to connect to the board without it.

`Bluetooth pairing must be initiated by the host by sending a "Require Authentication" HCI command to its bluetooth device. The bluetooth device will ask the host for a link key, which must be rejected so it will ask for a PIN-Code. The PIN-Code is the binary bluetooth address of the wiimote backwards.`

When trying the solution above the board would reject the PIN code and would not connect to the host.

#### Sources

The following sources have been used to research the Wii Balance Board:

- [Wii Balance Board](https://wiibrew.org/wiki/Wii_Balance_Board#Bluetooth_Communication)
- [Wii Mote](https://wiibrew.org/wiki/Wiimote#Bluetooth_Communication)

Publicly available projects have been researched aswell nearly all of them dind't use java to connect to the Wii Balance Board. The projects that did use java to connect to the Wii Balance Board were either outdated or didn't work.

## System Design

### Hardware

The hardware of the system will consist of the following components:
    - LDR: The sensor that will be used to track the user's position based on the light intensity.
    - ESP32: The microcontroller that will process the data from the LDR.
    - Pepper: The controller that will recieve the processed data from the ESP32 and will sync the data to the current task for the user.
    
#### Connection diagram

![Connection diagram](../diagrams/assets/positionTracking.png)

## Implementation

### Prototyping

A prototype of the app has been created to test the connection between the ESP32 and the Android App. The app can discover BLE devices and connect to them. The app currently is not able to continously read the data from the ESP32. It can however read the data once.

### Testing and Validation

The prototype of the 'Tracking board' has been tested. The LDR's have were connected to the ESP32 on a breadboard. The ESP32 will emit itself as a GATT server and the Android App will look for the hosts name and connect to it. The Android App will then read the data from the ESP32 once.

## Conclusion

The LDR's with the ESP32 are a good solution for the problem. The LDR's are cheap and easy to use. The ESP32 is a good microcontroller to process the data from the LDR's. The Android App can connect to the ESP32 and read the data from it. The next step is to make the Android App continously read the data from the ESP32 and display it to the user/use it in the app for exercises.

## References

[Bluetooth Discovery](https://developer.android.com/develop/connectivity/bluetooth/find-bluetooth-devices)
[GATT Server Connection](https://developer.android.com/develop/connectivity/bluetooth/ble/connect-gatt-server)
[ESP32 GATT Server](https://github.com/nkolban/esp32-snippets/tree/master/cpp_utils/tests/BLETests/Arduino/BLE_server)