Skip to content

Commit

Permalink
feat: improved module format
Browse files Browse the repository at this point in the history
  • Loading branch information
Paradoxdruid committed Oct 29, 2022
1 parent cabb223 commit ed8078a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 47 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@ This repository has some scripts to interact with the serial data from the LD06,

In one shell, start the test server: `python -m pyLIDAR.example_server`

In another, start pyLIDAR, specifying the "test" port: `python -m pyLIDAR.pyLIDAR -p test --save --graph`
In another, start pyLIDAR, specifying the "test" port: `python -m pyLIDAR -p test --save --graph`

## Usage with LD06 LIDAR

In a shell, start pyLIDAR with appropriate port: `python -m pyLIDAR.pyLIDAR -p COM4 --save`
In a shell, start pyLIDAR with appropriate port: `python -m pyLIDAR -p COM4 --save`

### Sample Output
### Command Line Options

`-p PORT` or `--port PORT` specifies LD06 port (use `-p test` to connect to test server)

`-s` or `--save` save collected data to a timestamped JSON file

`-g` or `--graph` display live-updating graph of data

### Data format

Data is saved as a list of arrays in JSON format.

## Sample Output

![Sample Output](/assets/sample_output.png?raw=true "Sample Output")
45 changes: 45 additions & 0 deletions pyLIDAR/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Use signal handling to break infinite loops
import signal
import sys
from types import FrameType
from typing import Optional

import serial

from pyLIDAR.pyLIDAR import main


def signal_handler(signal: int, frame: Optional[FrameType]) -> None:
print("\nprogram exiting gracefully")
sys.exit(0)


signal.signal(signal.SIGINT, signal_handler)

# Set up argument parsing
import argparse

parser = argparse.ArgumentParser(description="Collect LIDAR data")
parser.add_argument("-p", "--port", required=True, help="serial port")
parser.add_argument("-g", "--graph", action="store_true", help="graph data")
parser.add_argument("-s", "--save", action="store_true", help="save data")

args = parser.parse_args()

port: str = args.port
plot: bool = args.graph
save: bool = args.save

print("Starting LIDAR data collection")

if port == "test": # Use local server for testing
with serial.serial_for_url("socket://127.0.0.1:8000", timeout=1) as ser:
print("Entered")
main(ser, plot, save)
print("Exited")
else:
# Open serial port connection to LD06 LIDAR
with serial.Serial(port, 230400, timeout=1) as ser:
print("Entered")
main(ser, plot, save)
print("Exited")
45 changes: 1 addition & 44 deletions pyLIDAR/pyLIDAR.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import json
import os
from dataclasses import asdict
from types import FrameType
from typing import List, Optional
from typing import List

import serial

Expand Down Expand Up @@ -76,45 +75,3 @@ def main(ser: serial.Serial, plot: bool = False, save: bool = False) -> None:
array = []
array.append(asdict(data))
json.dump(array, outfile)


# Code that runs when file is directly executed, i.e.: `python pyLIDAR.py -p COM4 -s`
if __name__ == "__main__":

# Use signal handling to break infinite loops
import signal
import sys

def signal_handler(signal: int, frame: Optional[FrameType]) -> None:
print("\nprogram exiting gracefully")
sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

# Set up argument parsing
import argparse

parser = argparse.ArgumentParser(description="Collect LIDAR data")
parser.add_argument("-p", "--port", required=True, help="serial port")
parser.add_argument("-g", "--graph", action="store_true", help="graph data")
parser.add_argument("-s", "--save", action="store_true", help="save data")

args = parser.parse_args()

port: str = args.port
plot: bool = args.graph
save: bool = args.save

print("Starting LIDAR data collection")

if port == "test": # Use local server for testing
with serial.serial_for_url("socket://127.0.0.1:8000", timeout=1) as ser:
print("Entered")
main(ser, plot, save)
print("Exited")
else:
# Open serial port connection to LD06 LIDAR
with serial.Serial(port, 230400, timeout=1) as ser:
print("Entered")
main(ser, plot, save)
print("Exited")

0 comments on commit ed8078a

Please sign in to comment.