Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull Request For Research Purposes #166

Open
wants to merge 37 commits into
base: runner_dev_jeremy
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
bd85aa8
Update README.md
TimotheeeNiven Nov 12, 2024
5b637a6
Resolved merge conflicts and applied stashed changes
TimotheeeNiven Dec 4, 2024
ad3b601
Runner Fixes
TimothyNiven Dec 5, 2024
ccd5782
Add/update everything in the runner folder
TimothyNiven Dec 5, 2024
0e96be6
Updates to IC and KWS in reference submission/Clarity in Runner
TimothyNiven Dec 6, 2024
bbc1df9
Updates to IC and KWS in reference submission/Clarity in Runner
TimothyNiven Dec 6, 2024
c8a3c3e
Runner with new Results error fixed and working AUC
TimothyNiven Dec 10, 2024
5a5b565
Runner with new Results error fixed and working AUC
TimothyNiven Dec 10, 2024
cf6c1ee
Working AUC, results error present
TimothyNiven Dec 10, 2024
e90d24c
Working Performance test, Energy still in trial
TimothyNiven Dec 17, 2024
efef41a
Fixes for multiclass AUC
TimothyNiven Dec 19, 2024
4ab47db
Old Runner style for AD
TimothyNiven Jan 2, 2025
b4c5ec3
AD Completed
TimothyNiven Jan 7, 2025
8567940
AD Completed
TimothyNiven Jan 7, 2025
d7003f3
AD Completed
TimothyNiven Jan 7, 2025
55c3e2a
AD Fully integrated
TimothyNiven Jan 9, 2025
361ae60
AD tested with everything else
TimothyNiven Jan 10, 2025
fc192be
Create data_acquisition.py
TimotheeeNiven Jan 17, 2025
7cee438
Create data_analysis.py
TimotheeeNiven Jan 17, 2025
c5f278e
Create LPM01A.py
TimotheeeNiven Jan 17, 2025
f77bcdb
Add files via upload
TimotheeeNiven Jan 17, 2025
f29a492
Add files via upload
TimotheeeNiven Jan 17, 2025
60005e3
Create README.md
TimotheeeNiven Jan 17, 2025
64288df
Update README.md
TimotheeeNiven Jan 17, 2025
e5b9d38
Update README.md
TimotheeeNiven Jan 17, 2025
aa10b65
added .metadata to gitignore and removed it from tracking
jeremy-syn Jan 20, 2025
8e48030
Streaming ww dev (#168)
jeremy-syn Jan 20, 2025
c0bcb36
Stop tracking .metadata and add to .gitignore
TimothyNiven Jan 21, 2025
844740e
Stop tracking .metadata and add to .gitignore
TimothyNiven Jan 21, 2025
e47d4e9
Updated Runner, needs work on AUC for AD
TimothyNiven Jan 23, 2025
e2d48cf
Merge branch 'master' of https://github.com/TimotheeeNiven/tiny
TimothyNiven Jan 23, 2025
fdfa9d3
Sync reference_submissions folder with upstream
TimothyNiven Jan 23, 2025
418d240
Fix reference_submissions
TimothyNiven Jan 23, 2025
b8c0305
Update script.py
TimotheeeNiven Jan 24, 2025
05b1d3b
Update main.py
TimotheeeNiven Jan 24, 2025
bec5ff6
AUC Updated
TimotheeeNiven Jan 24, 2025
464088e
Update serial_device.py
TimotheeeNiven Jan 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Binary file modified .gitignore
Binary file not shown.
74 changes: 74 additions & 0 deletions LMP01A-CODE/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# LPM01A-lib
## These files were obtained from https://github.com/lazicdanilo/LPM01A-lib

## Description

This is a Python library for the [X-NUCLEO-LPM01A](https://www.st.com/en/evaluation-tools/x-nucleo-lpm01a.html) STM32 Power shield, Nucleo expansion board for power consumption measurement.

For more information about the X-NUCLEO-LPM01A communication protocol please refer to the [UM2269 Getting started with PowerShield firmware](https://www.st.com/resource/en/user_manual/dm00418905-getting-started-with-powershield-firmware-stmicroelectronics.pdf)

## Prerequisites

### Data Acquisition

- [PySerial](https://pypi.org/project/pyserial/)

### Data Analysis

- [Click](https://pypi.org/project/click/)
- [Pandas](https://pypi.org/project/pandas/)
- [Matplotlib](https://pypi.org/project/matplotlib/)

## Usage

### LPM01A

```python
from src.LPM01A import LPM01A

lpm = LPM01A("/dev/ttyACM0", 3864000)
lpm.init_device(mode="ascii", voltage=3300, freq=5000, duration=0)
lpm.start_capture()
lpm.read_and_parse_data()
```

See [data_acquisition.py](data_acquisition.py) for a complete example.

### DataAnalysis

```python
from src.DataAnalysis import DataAnalysis

da = DataAnalysis("example.csv", 10_000, 30_000)
print(f"Average current consumption {da.calculate_average_current()} Ah")
da.plot_current_vs_timestamp()
```

See [data_analysis.py](data_analysis.py) for a complete example.

### data_analysis.py

```bash
# Calculates average current consumption between 3ms and 3.7ms and plots data
./data_analysis.py example.csv -s 3_000_000 -e 3_700_000 -p
```

![Usage example](assets/pics/data_analysis_usage_example.gif)

## Running Your own device and storing data
If you run this command in your terminal
```bash
python -m data_acquisition.py
```
This will collect data until a keyboard interrupt (ctrl+c for windows) is enacted

Below will plot the data, remove the -s and -e to plot the fully collected data

```bash
# Calculates average current consumption between 1ms and 4ms and plots data
./data_analysis.py csv_folder_name/your.csv -s 1_000_000 -e 4_000_000 -p
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions LMP01A-CODE/data_acquisition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/env python3

from src.LPM01A import LPM01A

try:
lpm = LPM01A(port="COM19", baud_rate=3864000, print_info_every_ms=10_000)
lpm.init_device(mode="ascii", voltage=3300, freq=1000, duration=0)
lpm.start_capture()
lpm.read_and_parse_data()
except KeyboardInterrupt:
print("KeyboardInterrupt detected. Exiting...")
lpm.stop_capture()
lpm.deinit_capture()
exit(0)
115 changes: 115 additions & 0 deletions LMP01A-CODE/data_analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/bin/env python3

import click
import datetime as dt
from src.DataAnalysis import DataAnalysis, CacheData
from src.UnitConversions import UnitConversions


@click.command()
@click.option(
"-s",
"--start-timestamp-us",
default=0,
type=int,
help="Start timestamp in us for filtering data. Default is 0.",
)
@click.option(
"-e",
"--end-timestamp-us",
default=2**64,
type=int,
help="End timestamp in us for filtering data. Default is 2^64.",
)
@click.argument("csv-file", type=click.Path(exists=True))
@click.option("-p", "--plot", is_flag=True, help="Plot current vs timestamp")
@click.option(
"-d", "--dont-calculate", is_flag=True, help="Don't calculate average current"
)
@click.option(
"-c",
"--no-cache",
is_flag=True,
help="Don't use cached values from the CSV file nor write cache data to the CSV file.",
)
@click.help_option("-h", "--help")
def main(
start_timestamp_us: int,
end_timestamp_us: int,
csv_file: str,
plot: bool,
dont_calculate: bool,
no_cache: bool,
):
"""Calculate average current consumption from a CSV file between start_timestamp_us and end_timestamp_us.
If -p/--plot flag is used data will be plotted.

This script will also cache the calculated data to the CSV file in form of a comments (starting with '#')
if the whole file is used (option -s and -e are not used).

Example usage:

python data_analysis.py example.csv -s 3_000_000 -e 3_700_000 -p
"""

da = DataAnalysis(
csv_file, start_timestamp_us, end_timestamp_us, try_cache=(not no_cache)
)
uc = UnitConversions()

cache_data = None

# We can only read/write cache data if the whole file is used (option -s and -e are not used)
if start_timestamp_us != 0 or end_timestamp_us != 2**64:
no_cache = True

if not dont_calculate:
if not no_cache:
cache_data = da.get_csv_cache_data()

if cache_data == None:
time_window_s = da.get_time_slice()
num_values = da.get_number_of_used_values()
average_current_Ah = da.calculate_average_current()
else:
print("============= Using cached data =============")
time_window_s = cache_data.time_window_s
num_values = cache_data.num_values
average_current_Ah = cache_data.avg_current_Ah

print(
f"Selected time window: {time_window_s} s ({uc.s_to_ms(time_window_s)} ms)\n"
f"Number of values: {num_values}\n"
f"Average current consumption:\n"
f" - {average_current_Ah} Ah\n"
f" - {uc.A_to_mA(average_current_Ah)} mAh\n"
f" - {uc.A_to_uA(average_current_Ah)} uAh"
)

if not no_cache and cache_data == None and not dont_calculate:
print("============= Writing cache data =============")
cd = CacheData()
cd.date = dt.datetime.now().strftime("%d-%m-%Y")
cd.time = dt.datetime.now().strftime("%H:%M:%S")
cd.time_window_s = time_window_s
cd.time_window_ms = uc.s_to_ms(time_window_s)
cd.num_values = num_values
cd.avg_current_Ah = average_current_Ah
cd.avg_current_mAh = uc.A_to_mA(average_current_Ah)
da.write_csv_cache_data(cd)

if plot:
# Plot current vs timestamp
da.plot_current_vs_timestamp(
"Timestamp (ms)",
"Current (uA)",
"Current vs Timestamp",
)


if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("KeyboardInterrupt detected. Exiting...")
exit(0)
Loading
Loading