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

A camera control script that automates running camera-ctl #100

Open
wants to merge 20 commits into
base: master
Choose a base branch
from

Conversation

akseidel
Copy link

@akseidel akseidel commented Feb 8, 2021

This is a shell script with accompanying instruction readme that performs all tasks required to start camera-ctl in a Terminal session.

This script when executed in a Terminal window finishes up with "camera-ctl" running in a screen session. It automates the serial connection after detecting the tty serial device presence, logs in as root and runs "camera-ctl".

Although the showmewebcam readme describes "camera-ctl" in debugging it is actually an essential utility required to be running when showmewebcam is being used in dynamic situations. This script allows ordinary users to easily invoke "camera-ctl".

Shell script with accompanying instruction readme that performs all tasks required to start camera-ctl in a Termainl session.
@htruong
Copy link
Contributor

htruong commented Feb 8, 2021

Thanks for sending in the patch! It seems like the shellcheck did not pass, do you mind revising so that it passes Shellcheck?

Changes required to eliminate shellcheck errors
@akseidel
Copy link
Author

akseidel commented Feb 8, 2021

Thanks for letting me know about shellcheck. I installed it for the IDE I am using and I believe all the bad code is removed. The script was tested on my end.

@dgsiegel
Copy link
Contributor

dgsiegel commented Feb 8, 2021

Thanks for all your work here. I can definitely see the benefit here. I am just wondering, if this repo is actually the best place for this script. First, this repo is mostly about building the image and not working with an installed showmewebcam instance (except swmc-expect). Secondly, it does seem highly custom to MacOS, does not work in Linux in its current state and is mostly useless for Windows.

If the goal is to easily run camera-ctl, we could also set up PAM to autologin a user, or even to start camera-ctl automatically when logging in. In any case, the portname should stay the same at all times, and so you can easily work with your shell history or add an alias.

In any case I would be very happy to link to your script, add further details on the wiki etc.

@htruong would be happy to hear your opinion as well.

# edit this next line to be the correct text pattern for your computer.
portnamepat="tty.usbmodem" #typical for showmewebcam on Appl OS X
#portnamepat="ttyACM" #probably typical for showmewebcam on Linux
#portnamepat="tty."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should auto discover this, based on the platform you are on.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will work on that. I have a Raspberry Pi 4 I can use for testing the Linux side.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto discover is now implemented.

clear
# terminates all the ort screen sessions.
screen -ls | grep Attached | cut -d. -f1 | awk '{print $1}' | xargs kill
screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs kill
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why two times?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my first shell script, so I have to dive deep to understand even the smallest aspects. Based on tests, what I had for what is supposed to handle both Attached and Detached in one line did not work. The single lines did. So I used those and moved on. There are many brute force parts of this script that I assume must have much better solutions.

#portnamepat="tty."
# The client is an application you want this script to also run.
runtheclient="true"
clientname="Photo Booth"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MacOs specific, should be based on the platform, a parameter or optional

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is MacOS specific. The intent is for educator to make edits, which is one reason these are brought to the script top. The intended users (school setting as webcam attached to microscope camera attachment) would not know what a parameter is. I understand the point. Perhaps there is something that could be done.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In combination with the auto discover implementation the script sets these per OS.

#portnamepat="ttyACM" #probably typical for showmewebcam on Linux
#portnamepat="tty."
# The client is an application you want this script to also run.
runtheclient="true"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better as a parameter

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the intended situation running the client is almost 100% wanted and in MacOS it would be difficult to have multiple instances. This variable is almost an ort from the early development process. An issue with parameters is that they result in what is viewed as unnecessary complication by the intended user.

@@ -0,0 +1,146 @@
#!/bin/bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#!/bin/sh or #!/usr/bin/env bash are better for platform compatibility.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will check this out.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script has been entirely refactored for sh.

if [ "$runtheclient" = "true" ]; then
# -g causes application to open in background
# https://scriptingosx.com/2017/02/the-macos-open-command/
open -g -a "$clientname"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Linux, this would probably be xdg-open

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Hopefully I'll get the platform checking to work.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In combination with auto discover the application Webcamoid is run for Linux situations. This has been tested on a Raspberry Pi 4 running a Raspbian system.

I could be wrong but xdg-open wants an image file for an argument, so I did not find a way to use it. In the script situation an application needs to be run, For the Linux situation I found running a detached screen running Webcamoid as a solution. The difference between the Apple and Linux situations is this detached screen existence that gets blown away the next time the script is run. Webcamoid closes and reopens if it was already running whereas an already running PhotoBooth just stays.

# trick is used to get around that.
screen -dmS spawner
screen -S spawner -X screen screen -dR thispicam "$piusbwebcamport" 115200
sleep 0.3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the delays? You can just read the output and wait for that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to read the output. I hardly understand screens. Can you provide a reference link to start me on the right track to read screen output? The sleeps were necessary. Sometimes the Pi Zero was not ready in time. I thought it was lucky sleep would take decimal numbers instead of only integers. Perhaps by reading the screen output it might also be possible to detect the unrefreshed state camera-ctl can be in when the Pi Zero already had it running.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my tests with a Raspberry Pi 4, an even longer sleep is required, particularly this first one immediately after starting the screen connection.

# a detached target screen does not work without the target screen having
# been attached at some point. Here the nest screen into a spawner screen
# trick is used to get around that.
screen -dmS spawner
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One might need sudo to access the device with screen.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what you mean. I can tell you it took a lot of testing to determine what was going on. Apparently one cannot stuff to a detached screen that has not been attached yet. Looking online knowing that observed behaviour led to this solution. The visible, ie attached, initial screen session blocks the script until that screen is terminated. Therefore the succeeding stuffs to provide root, root, camera-ctl do not occur until the first screen is terminated. Starting the initial screen detached allowed the succeeding code to proceed, but the stuffs vanish.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on the access mode of the device, you need sudo to access it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I know what you are talking about. I would never see that on my Pi 4 where I run as root. The script now checks for root user and advises running the script with sudo. It also checks for a screen command.

@htruong
Copy link
Contributor

htruong commented Feb 9, 2021

Yeah I agree with @dgsiegel.

I also understand the intention of @akseidel though, as you seem to have a pretty nice set up with your microscope. Perhaps having this example script in a separate sample directory for specific example use-cases would be OK for me too.

@dgsiegel
Copy link
Contributor

dgsiegel commented Feb 9, 2021

Perhaps having this example script in a separate sample directory for specific example use-cases would be OK for me too.

Sounds good. We could put the swmc-expect script there as well.

@akseidel
Copy link
Author

akseidel commented Feb 9, 2021 via email

changes to conform with sh,
started to add and make changes per initial comments
more sh conforming
Added images for readme & added edited readme
testing in linux generated extra output when zargs kill had nothing to do
changes per linux and discoveries made experimenting with linux on RPI 4
Linux implementation as determined by working with Raspbian on a Raspberry Pi 4 and the application Webcamoid.
@kbingham
Copy link

I'm curious about this, is it to be able to configure the camera when it's running? Wouldn't that be handled by setting controls on the host which are interpreted by uvc-gadget and then set accordingly?

Or is this more specific?

The camera should be configurable without using the serial console.

@dgsiegel
Copy link
Contributor

The camera should be configurable without using the serial console.

Yes, you're right, see #93

However this is not fully functional right now, as it needs kernel/uvc patches and probably further changes in uvc-gadget.

changes to arguments and how they are processed.
Added sleep after client startup to avoid communication clash with USB serial connection startup.
ort cleanup
-m command parameter disables the automatic logging in to Pi Zero
Added missing comment regarding -m in the argument reaction function.
added help parameter
regarding newly added -m and -h
Description and instructions regarding the garbled login after a fresh boot.
@dgsiegel
Copy link
Contributor

#109 might be interesting to you, as it enables auto login through the serial console

@akseidel
Copy link
Author

Yes the auto login looks promising. I was planning to eliminate the greeting line myself as the very first change to make if I ever managed to venture into building it.

- detect missing screen command
- detect user not root in linux
- tested with showmewebcam autlog buiild
- detect missing client application
- other minor
updates per 1.8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants