-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial scanner implementation * network table support * Add NT battery name interface to robot code * Initial implementation of network tables * Updated conda env to include pyntcore * Cleaned up code * Added battery name prefix * Fixed comment * Lowered timeout value --------- Co-authored-by: Jaden Chen <[email protected]> Co-authored-by: JadenChen1123 <[email protected]> Co-authored-by: Jonah <[email protected]> Co-authored-by: Leo Huang <[email protected]> Co-authored-by: izvit <[email protected]>
- Loading branch information
1 parent
180af7a
commit 0ce425e
Showing
4 changed files
with
136 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
channels: | ||
- defaults | ||
dependencies: | ||
- python=3.12 | ||
- pip | ||
- pyserial | ||
- pip: | ||
- pyntcore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
""" Script to interface with battery scanner | ||
""" | ||
import argparse | ||
from time import sleep | ||
import serial | ||
import array | ||
import ntcore | ||
import logging | ||
import re | ||
|
||
#------------------ | ||
#-- Parameters | ||
#------------------ | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
timeout = 1 | ||
prefix = array.array('B', [0x02, 0x00, 0x00, 0x01, 0x00, 0x33, 0x31]) | ||
scan_command = array.array('B',[0x7e, 0x00, 0x08, 0x01, 0x00, 0x02, 0x01, 0xab, 0xcd]) | ||
name_length = 8 | ||
response_length = len(prefix) + name_length | ||
|
||
#------------------ | ||
#-- Parse command line arguments | ||
#------------------ | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--dev", default="COM8", type=str, help="Path to the camera device") | ||
args = parser.parse_args() | ||
|
||
|
||
#------------------ | ||
#-- Setup NetworkTables | ||
#------------------ | ||
client = ntcore.NetworkTableInstance.getDefault() | ||
client.startClient4(f'battery-scanner') | ||
client.setServerTeam(6328) | ||
|
||
pub = client.getStringTopic("/battery_name").publish() | ||
|
||
#------------------ | ||
#-- Read battery name | ||
#------------------ | ||
name = None | ||
try: | ||
with serial.Serial(args.dev, 9600, timeout=timeout) as ser: | ||
|
||
logging.info("Scanning for battery ID") | ||
|
||
while True: | ||
ser.write(scan_command) | ||
response = ser.read(response_length) | ||
|
||
if len(response) == response_length: | ||
if response.startswith(prefix): | ||
name_bytes = response[-8:] | ||
name_str = name_bytes.decode("utf-8") | ||
if re.match('^[a-zA-Z0-9_\\-]+$', name_str): | ||
name = name_str | ||
logging.info(f"Battery ID: {name}") | ||
break | ||
else: | ||
logging.warning("Battery ID doesn't match expected pattern") | ||
|
||
except Exception as e: | ||
logging.error(e) | ||
|
||
#------------------ | ||
#-- Publish name | ||
#------------------ | ||
if name is not None: | ||
pub.set(f"BAT-{name}") | ||
else: | ||
logging.error("Unable to read battery code") | ||
|
||
|
||
#------------------ | ||
#-- Switch to idle state | ||
#------------------ | ||
logging.info("Switching to idle mode") | ||
while True: | ||
sleep(100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 0 additions & 84 deletions
84
src/main/java/org/littletonrobotics/frc2024/util/BatteryTracker.java
This file was deleted.
Oops, something went wrong.