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

Better Camera Implementation #59

Merged
merged 25 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6869285
libAprilTags.sh and data/camlocations file commits
cwosw Jan 11, 2025
d085aba
added golang dependancy
cwosw Jan 11, 2025
f8c45ed
fixed unrelated issues
cwosw Jan 11, 2025
a036fb9
Added camera scanner
clifflocked Jan 11, 2025
480141a
Merge pull request #57 from clifflocked/cameraIDs
cwosw Jan 11, 2025
7285c8b
Added camera ID alongside Serial #
clifflocked Jan 12, 2025
5b01f60
Merge pull request #58 from clifflocked/cameraIDs
cwosw Jan 12, 2025
8c97d62
Untested Full Implementation
cwosw Jan 13, 2025
4c6a94a
Merge branch 'cameraIDs' of https://github.com/Team766/apriltags_cuda…
cwosw Jan 13, 2025
09ff1c8
Added Offset Files
cwosw Jan 13, 2025
039a26a
added the new files
cwosw Jan 13, 2025
9d1c172
The game of fixes begins
cwosw Jan 14, 2025
91f01a0
removed nonexistant parameter
cwosw Jan 14, 2025
8f21727
Added more verbose errors
cwosw Jan 15, 2025
aafbe00
Fixed a lot more things
cwosw Jan 16, 2025
0fba751
UNTESTED kill handler
cwosw Jan 16, 2025
7452cbd
Merge branch 'main' of https://github.com/Team766/apriltags_cuda into…
cwosw Jan 18, 2025
df6a7ea
New commits to source
cwosw Jan 18, 2025
d1b3b82
not yet
cwosw Jan 18, 2025
b6eefce
added data/calibration back
cwosw Jan 18, 2025
41e82f6
Added real calibration files
cwosw Jan 18, 2025
4d3489e
serve on port 8080+camera_idx iff the user didn't specify a port
cgpadwick Jan 18, 2025
ffdb9d9
updated cal files
cwosw Jan 22, 2025
5af8829
Merge branch 'main' of https://github.com/Team766/apriltags_cuda into…
cwosw Jan 23, 2025
373bb8b
Merge branch 'cameraIDs' of https://github.com/Team766/apriltags_cuda…
cwosw Jan 23, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 61 additions & 6 deletions Installer/AprilTags/ApriltagsManager.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ function killIfRunning() {
PID=`libAprilTags.sh pid $1`
if [ $PID -ne 0 ]; then
kill -2 $PID
return 1
fi
return 0
}

# set clocks to max frequency (jetson only)
Expand All @@ -23,6 +25,9 @@ jetson_clocks || true
#open source for args
source /apps/AprilTags/args

# make sure that the bin dir is actually loaded
PATH="$PATH:/apps/bin"

if [[ $1 == "start" ]]; then
# check for lock
if [ -f "/apps/AprilTags/servicerunning" ]; then
Expand Down Expand Up @@ -51,17 +56,67 @@ if [[ $1 == "start" ]]; then
cd /apps/AprilTags

touch /apps/AprilTags/servicerunning

# abs path BECAUSE of the proc getting commands
/apps/AprilTags/Backend/ws_server $backend &

cams=`libAprilTags.sh camIDs`
if [[ $? -ne 0 ]]; then
echo "No cameras found or something. Try plugging some in, or else the cameras are not being seen."
exit 5
fi

# cd again just to be safe (that lib WILL cd)
cd /apps/AprilTags

# iterate
for cam in $cams; do
# set items off of the output cam
IFS=: read -r camID camIndex <<< "$cam"

# not yet
# get the offset file
#camLoc=$(libAprilTags.sh getCamLoc $camID)
#if [[ $? -ne 0 ]]; then
# echo "FAILED TO FIND CAMERA OFFSET ${camID}, EXITING..."
# exit 6
#fi

# ensure that the calibration file exists
if ! [[ -f /apps/AprilTags/data/calibration/calibrationmatrix_${camID}.json ]]; then
echo "FAILED TO FIND CAMERA CALIBRATION FILE ${camID}, EXITING..."
exit 6
fi

# set args
args=`$backend` # this actually is magical because of the weird eval thing
# https://stackoverflow.com/questions/5112663/bash-variable-reevaluation

# abs path BECAUSE of the proc getting commands
/apps/AprilTags/Backend/ws_server $args &
echo $! >> /apps/AprilTags/servicerunning # add every pid to a new line
done

exit

elif [[ $1 == "stop" ]]; then
rm /apps/AprilTags/servicerunning
if [[ -f /apps/AprilTags/servicerunning ]]; then
# read the file, kill if a pid is found inside
while read p; do
if [[ "$p" =~ ^-?[0-9]+$ ]]; then # regex to find int
# assume its a PID that belongs to ws_server if running
if ps -p $p > /dev/null
then
timeout -s INT 5s 'killHandler.sh ${p}' &
fi
fi
done < /apps/AprilTags/servicerunning
fi
# and add the rest of it
killIfRunning '/apps/AprilTags/Backend/ws_server'

# this is broken, and I am too lazy to fix it
#killIfRunning '/apps/AprilTags/Backend/ws_server'
#while killIfRunning '/apps/AprilTags/Backend/ws_server'; do
# continue # just run the initial condition, a backup if the service orphaned something
#done

rm /apps/AprilTags/servicerunning
else
echo "input ${1} not understood"
exit 3
Expand Down
4 changes: 3 additions & 1 deletion Installer/AprilTags/args
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Please be careful editing this file!
backend="-camera_idx 0 -cal_file data/calibrationmatrix.json"
# It looks odd and it is structured in two seperate ways, but it is fine, write inside the echo for backend
# ${camIndex} is the video index of the camera, ${camID} is the id of the camera (by serial), and ${camLoc} is the location of the camera on the robot (as in tr for example to be top right)
backend='eval echo "-camera_idx ${camIndex} -cal_file /apps/AprilTags/data/calibration/calibrationmatrix_${camID}.json -camera_name cam${camID}"'
# manager
autormlockfile="true"
2 changes: 1 addition & 1 deletion Installer/AprilTagsPipeline.service
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ExecStart=/apps/AprilTags/ApriltagsManager.sh start
RemainAfterExit=true
ExecStop=/apps/AprilTags/ApriltagsManager.sh stop
StandardOutput=journal
Restart=always
#Restart=always

[Install]
WantedBy=multi-user.target
20 changes: 13 additions & 7 deletions Installer/bin/AprilTags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function help() {
echo "-h to print this menu"
echo "-v to print verbose"
echo "--validate or -V {option} to validate something"
echo "--update or -u to update the running frontend/backend from the cuda root"
echo "--update or -u to update the running backend from the cuda root"
echo "ex. -V lockfile"
echo
echo "if you try to run two different commands in one command, one might exit before the other"
Expand Down Expand Up @@ -79,33 +79,33 @@ if [ ! -z ${valitem+x} ]; then
# lockfile is there
printV "lockfile present, checking if backend is being run"
if [ $PID -ne 0 ]; then
# echo "lockfile=true;running=true;"
echo "lockfile=true;running=true;"
printV "The service appears to be running and stable."
else
rm /apps/AprilTags/servicerunning
if [ $? -ne 0 ]; then
# echo "lockfile=rm;running=false;"
echo "lockfile=rm;running=false;"
printV "Lockfile found with the service not running, it could not be deleted, feel free to delete it."
else
# echo "lockfile=false;runnning=false;"
echo "lockfile=false;runnning=false;"
printV "Lockfile found with no services running, it is now deleted."
fi
fi
else
# lockfile missing, check if anything is runing
printV "lockfile is not there"
if [ $PID -eq 0 ]; then
# echo "lockfile=false;running=false;"
echo "lockfile=false;running=false;"
printV "The AprilTags service is offline and the lockfile is not there."
else
printV "The lockfile is missing and at least one of the services is running."
touch /apps/AprilTags/servicerunning
if [ $? -ne 0 ]; then
# echo "lockfile=add;running=true;"
echo "lockfile=add;running=true;"
printV "The lockfile couldn't be created. please make it, or stop the processes!"
printV "Go to the ApriltagsManager.sh script and run it with 'stop'"
else
# echo "lockfile=true;running=true;"
echo "lockfile=true;running=true;"
printV "Service is online, the lockfile is now in place."
fi
fi
Expand Down Expand Up @@ -137,6 +137,7 @@ elif [ $update == "t" ]; then
printV "copying backend..."
cp -R build/* /apps/AprilTags/Backend/

printV "copying data..."
# remove the existing data dir if it exists then replace it
if [ -d /apps/AprilTags/data ]; then
rm -rf /apps/AprilTags/data
Expand All @@ -145,6 +146,11 @@ elif [ $update == "t" ]; then
cp -R data/ /apps/AprilTags/data/
fi

printV "copying public server files..."
if [ -d public ]; then
cp -R public/ /apps/AprilTags/public
fi

printV "The files were sucessfully coppied"

# restart the service
Expand Down
11 changes: 11 additions & 0 deletions Installer/bin/camerascanner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Camerascanner
A helper function by Segen to read the serial number of a camera and the index 0 id of the camera as the /dev/video device. It is structured as ${serial}:${videoid}.

## building
```bash
go build -o scanner_${arch} main
```
To just run the file:
```bash
go run main
```
3 changes: 3 additions & 0 deletions Installer/bin/camerascanner/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module main

go 1.19
37 changes: 37 additions & 0 deletions Installer/bin/camerascanner/scanner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"fmt"
"os/exec"
"strings"
//"log"
)

func main() {
out, err := exec.Command("ls", "-l", "/dev/v4l/by-id/").Output()
if err != nil {}

devices := strings.Split(string(out), "lrwxr")
devices = devices[1:]

var sNums []string
serial := ""

for _, e := range devices {
start := strings.Index(e, "Camera") + 5
for j := 2; j < 100; j++ {
if string(e[strings.Index(e, "index") + 5]) == "1" {
break
}
if string(e[start + j]) == "-" {
sNums = append(sNums, serial + ":" + string(e[len(e) - 2]))
serial = ""
break
} else {
serial += string(e[start + j])
}
}
}
fmt.Print(sNums)

}
Binary file added Installer/bin/camerascanner/scanner_aarch64
Binary file not shown.
Binary file added Installer/bin/camerascanner/scanner_x86_64
Binary file not shown.
17 changes: 17 additions & 0 deletions Installer/bin/killHandler.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#! /bin/bash

if [[ "$1" =~ ^-?[0-9]+$ ]]; then # regex to find int
# assume its a PID that belongs to ws_server if running
if ps -p $1 > /dev/null
then
killpid=$1
fi
fi

# stop if unset
if [ -z ${killpid+x} ]; then exit 1; fi

# catch: kill -9
trap 'kill -9 ${killpid}' INT

kill -2 ${killpid}
49 changes: 49 additions & 0 deletions Installer/bin/libAprilTags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,55 @@ function getProcPID() {
echo 0
}

echoerr() { echo "$@" 1>&2; } # bypass capture by var assigning because std::err

if [[ $1 == "pid" ]]; then
getProcPID $2
elif [[ $1 == "camIDs" ]]; then
cd "${0%/*}"/camerascanner
# run the go file in the most efficent way possible
arch=$(uname -m)
if [[ -f "scanner_${arch}" ]]; then
ret=$("./scanner_${arch}")
else
if [[ $arch == "x86_64" ]]; then
# x86 can run aarch64 binaries for some reason, so do that
ret=$("./scanner_aarch64")

else
echoerr "running go file directly"
ret=$(go run main)
fi
fi

if [[ "${ret,,}" == *"err"* ]] || [[ "${ret,,}" == *"fault"* ]]; then
# the majority of time that this control path gets reached will be because Go tries to run main, but then lacks some variables and errors out
echoerr "The Go code probably ran into an error. Look at the logs above to see it, otherwise make sure you have cameras plugged in."
exit 1
elif ! [[ "${ret:0:1}" == "[" ]] || ! [[ "${ret: -1}" == "]" ]]; then
# There might be some novel way that this control path can be reached, for the most part, this will not get here
echoerr "This is some novel error that I do not know how got here. Make sure you have cameras plugged in."
exit 1
fi
# all good
echo "${ret:1: -1}"
exit 0
elif [[ $1 == "getCamLoc" ]]; then
camlocfile="/apps/AprilTags/data/cameralocations"
camid=$2
if ! [[ -f camlocfile ]]; then
echo "cam locations file not found."
exit 1 # maybe stop using the same number
fi
# must have the file, assume that the file is correct
set -e # just in case
locfile=$(tail -n +1 camlocfile)
for line in locfile; do
if [[ "$2" == "${line[0]}" ]]; then
echo line[1]
exit 0
fi
done
echo "id not found"
exit 1 # again, same code
fi
4 changes: 2 additions & 2 deletions Installer/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ systemctl enable AprilTagsPipeline.service
# add path modification in /etc/profile.d/ if not already there
# why two? idk. it just apparently should have both?
if [ ! -f /etc/profile.d/AddAppsToPaf.sh ]; then
cp AddAppsToPaf.sh /etc/profile.d/AddAppsToPaf.sh
cp AddAppsToPath.sh /etc/profile.d/AddAppsToPath.sh
fi

# now onto the AprilTags specific items
cp -R AprilTags/ /apps/
cp bin/* /apps/bin/
cp -R bin/* /apps/bin/

echo "The AprilTags service has been installed and enabled. Run 'AprilTags.sh --update all' in the Cuda project root to sync it to be run."
echo "HINT: program not found error? run 'export PATH=\$PATH:/apps/bin' in your session"
Expand Down
5 changes: 2 additions & 3 deletions Installer/uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ function isDirEmpty() {
if [[ $fullrm == "t" ]]; then
if [ -z "$( ls -A '/apps/bin/' )" ]; then
rm -d /apps/bin
rm /etc/profile.d/AddAppsToPaf.sh
rm /etc/profile.d/AddAppsToPaf.csh
rm /etc/profile.d/AddAppsToPath.sh
fi

if [ -z "$( ls -A '/apps/' )" ]; then
Expand All @@ -54,4 +53,4 @@ if [[ $fullrm == "t" ]]; then
echo "full uninstall is as completed as it can be!"
fi

echo "The AprilTags service has been uninstalled from your system!"
echo "The AprilTags service has been uninstalled from your system!"
Loading
Loading