Script / UI Sync Problems

Discussions on extending SharpCap using the built in Python scripting functionality
Post Reply
CarlH
Posts: 6
Joined: Tue Apr 03, 2018 11:16 pm

Script / UI Sync Problems

Post by CarlH » Thu Aug 27, 2020 2:37 am

This is my first attemp to create an atomated script that records R-G-B videos using a filter wheel in a loop until I stop the script. The first red filter recording works fine, but when starting with the green filter the script seems to start running wild: the current filter and the time left display in SharpCap user interface are not updated / displayed anymore, and the next recording does not stop after 30 seconds. Finally when I stop the script the recording continues until I manually press the stop record button in the SharpCap user interface.

Are there additional commands needed to get the script posted below to get running in a controlled manner and to get the user interface correctly displaying the current status?

I am running SharpCap 3.2.6292 and using a ZWO ASI 1600 Mono with an Atik EFW2 filterwheel.

import time
import clr
clr.AddReference("SharpCap")
from System import TimeSpan
from SharpCap.UI import CaptureLimitType
SharpCap.TargetName='Mars'
SharpCap.SelectedCamera.CaptureConfig.CaptureLimitType = CaptureLimitType.TimeLimited

while True:

# RED
SharpCap.SelectedCamera.Controls.Exposure.ExposureMs=10
SharpCap.SelectedCamera.Controls.Gain.Value=240
SharpCap.SelectedCamera.CaptureConfig.CaptureLimitTime = TimeSpan.FromSeconds(30)
SharpCap.Wheels.SelectedWheel.Position = 2
SharpCap.SelectedCamera.PrepareToCapture()
SharpCap.SelectedCamera.RunCapture()
SharpCap.ShowNotification('Recording with Red Filter')
while True:
if not SharpCap.SelectedCamera.Capturing:
break
time.sleep(0.5)

# SAME CODE FOR GREEN AS FOR RED (only different exposure and filter pos)

# SAME CODE FOR BLUE AS FOR RED (only different exposure and filter pos)

Thanks, Carl

User avatar
admin
Site Admin
Posts: 5210
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Script / UI Sync Problems

Post by admin » Thu Aug 27, 2020 6:10 pm

Hi,

I can't see anything obviously wrong. I'd advise that you update to the latest version of SharpCap as the one you are using is quite out of date now (there have been some scripting fixes, but nothing that would obviously tie in with what you are seeing).

If you still have problems with the latest version then please can you post the full script including indentation (use the code block button above the message post window – it looks like this </>).

Thanks, Robin

CarlH
Posts: 6
Joined: Tue Apr 03, 2018 11:16 pm

Re: Script / UI Sync Problems

Post by CarlH » Thu Aug 27, 2020 6:30 pm

Hi Robin

thanks for the help. I've just installed the newest SharpCap version. The behaviour is the same:

1. Red filter is recorded fine, 30s, and Time Left is displayed on the status bar. Only the R filter box on the Camera Control Panel is not highlighted, it still shows L, even if R is beeing used for recording. After the 30s the SER-file is correctly stored, and it used the R filter as it should.

2. Green filter starts recording, but Time Left is not displayed in the status bar, and the recording continuous indefinitelly.

For reference, the scripting window shows this information:
IronPython 2.7.9 (2.7.9.0) on .NET 4.0.30319.42000 (32-bit)

Here is the complete code:

Code: Select all

import time
import clr
clr.AddReference("SharpCap")
from System import TimeSpan
from SharpCap.UI import CaptureLimitType
SharpCap.TargetName='Mars'
SharpCap.SelectedCamera.CaptureConfig.CaptureLimitType = CaptureLimitType.TimeLimited

while True:
  
   # RED
   SharpCap.SelectedCamera.Controls.Exposure.ExposureMs=10
   SharpCap.SelectedCamera.Controls.Gain.Value=240
   SharpCap.SelectedCamera.CaptureConfig.CaptureLimitTime = TimeSpan.FromSeconds(30)
   SharpCap.Wheels.SelectedWheel.Position = 2
   SharpCap.SelectedCamera.PrepareToCapture()
   SharpCap.SelectedCamera.RunCapture()
   SharpCap.ShowNotification('Recording with Red Filter')
   while True:
      if not SharpCap.SelectedCamera.Capturing:
         break
      time.sleep(0.5)

   # GREEN
   SharpCap.SelectedCamera.Controls.Exposure.ExposureMs=12
   SharpCap.SelectedCamera.Controls.Gain.Value=240
   SharpCap.SelectedCamera.CaptureConfig.CaptureLimitTime = TimeSpan.FromSeconds(30)
   SharpCap.Wheels.SelectedWheel.Position = 3
   SharpCap.SelectedCamera.PrepareToCapture()
   SharpCap.SelectedCamera.RunCapture()
   SharpCap.ShowNotification('Recording with Green Filter')
   while True:
      if not SharpCap.SelectedCamera.Capturing:
         break
      time.sleep(0.5)

   # BLUE
   SharpCap.SelectedCamera.Controls.Exposure.ExposureMs=15
   SharpCap.SelectedCamera.Controls.Gain.Value=240
   SharpCap.SelectedCamera.CaptureConfig.CaptureLimitTime = TimeSpan.FromSeconds(30)
   SharpCap.Wheels.SelectedWheel.Position = 4
   SharpCap.SelectedCamera.PrepareToCapture()
   SharpCap.SelectedCamera.RunCapture()
   SharpCap.ShowNotification('Recording with Blue Filter')
   while True:
      if not SharpCap.SelectedCamera.Capturing :
         break
      time.sleep(0.5)
Cheers

Carl

User avatar
admin
Site Admin
Posts: 5210
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Script / UI Sync Problems

Post by admin » Thu Aug 27, 2020 6:32 pm

Hi Carl,

does it help if you put this line in each colour block rather than just having it at the start of the file?

Code: Select all

SharpCap.SelectedCamera.CaptureConfig.CaptureLimitType = CaptureLimitType.TimeLimited
I'm just wondering if that gets reset when the capture finishes?

Cheers, Robin

CarlH
Posts: 6
Joined: Tue Apr 03, 2018 11:16 pm

Re: Script / UI Sync Problems

Post by CarlH » Thu Aug 27, 2020 7:07 pm

Hi Robin,

adding the CaptureLimitType line to each filter does not help. But adding a SharpCap.SelectedCamera.StopCapture() after every capture ended helps (even if it looks a little strange in the message bar). Maybe capture end and file save do not happen instantaneously, so that a timing problem occurs.

Now the only other real problem left is how to perform a StopCapture when the script is manually stopped. Is there a way to achieve this?

And is there a way to tell the filter bar control to be refreshed with the current filter?

Thanks

Carl

CarlH
Posts: 6
Joined: Tue Apr 03, 2018 11:16 pm

Re: Script / UI Sync Problems

Post by CarlH » Thu Aug 27, 2020 7:55 pm

Hi Robin,

replacing

Code: Select all

SharpCap.Wheels.SelectedWheel.Position = 1
by

Code: Select all

SharpCap.SelectedCamera.Controls.FilterWheel.Value='R'
solves the problem with the filter control box display.

Cheers

Carl

CarlH
Posts: 6
Joined: Tue Apr 03, 2018 11:16 pm

Re: Script / UI Sync Problems

Post by CarlH » Thu Aug 27, 2020 8:26 pm

Hi Robin,

I also managed to stop the last recording when the script is stopped:

Code: Select all

try:
    # script code

finally:
   if SharpCap.SelectedCamera.Capturing:
      SharpCap.SelectedCamera.StopCapture()
Cheers

Carl

CarlH
Posts: 6
Joined: Tue Apr 03, 2018 11:16 pm

Re: Script / UI Sync Problems

Post by CarlH » Thu Aug 27, 2020 9:34 pm

Here is the final code for reference, in case somebody is interested. The script stops fine in the middle of a recording, but not during a filter move - in this case an error appears and the recording continues, which can only be stopped by running and stopping the script again.

Code: Select all

import time
import clr
clr.AddReference("SharpCap")
from System import TimeSpan
from SharpCap.UI import CaptureLimitType
SharpCap.TargetName='Mars'

def Rec(exposureInMs, gain, durationInSecs, filterName):
	SharpCap.SelectedCamera.Controls.Exposure.ExposureMs=exposureInMs
	SharpCap.SelectedCamera.Controls.Gain.Value=gain
	SharpCap.SelectedCamera.CaptureConfig.CaptureLimitType = CaptureLimitType.TimeLimited
	SharpCap.SelectedCamera.CaptureConfig.CaptureLimitTime = TimeSpan.FromSeconds(durationInSecs)
	SharpCap.ShowNotification('Moving filter wheel to ' + filterName)
	SharpCap.SelectedCamera.Controls.FilterWheel.Value=filterName	
	while SharpCap.SelectedCamera.Controls.FilterWheel.Moving:
		time.sleep(0.1)
	SharpCap.SelectedCamera.PrepareToCapture()
	SharpCap.ShowNotification('Capturing with ' + filterName + ' filter', 0, False, 4)
	SharpCap.SelectedCamera.RunCapture()
	while SharpCap.SelectedCamera.Capturing:
		time.sleep(0.1)
	SharpCap.SelectedCamera.StopCapture()

try:
	while True:
		Rec(10, 240, 30, 'R')
		Rec(12, 240, 30, 'G')
		Rec(15, 240, 30, 'B')
		
finally:
	if SharpCap.SelectedCamera.Capturing:
		SharpCap.SelectedCamera.StopCapture()
Carl

User avatar
admin
Site Admin
Posts: 5210
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Script / UI Sync Problems

Post by admin » Fri Aug 28, 2020 6:18 pm

Hi,

good to hear that you got it working :-) I suspect you are right that there is a timing issue if you try to start one capture too soon after the finish of the previous one.

Cheers, Robin

Post Reply