Servers by jDrones

LUA Manual Photo Coding Help?

I have some code to trigger Manual Photo using a Seagull #REC2 Ch3 which not working and ask for some help?

Taking a manual photo is 2 steps. Step 1 is Focus at pwm 1500 then Shutter Release at pwm 1800. Neutral is pwm 1175.

The code has the pwm defaulting to 1175 (neutral) and when a button is pressed the following lines are triggered.

Below line is for focus.

SRV_Channels:set_output_pwm_chan_timeout(chanShut, 1500, 2500)

Below line is Shutter Trigger.

SRV_Channels:set_output_pwm_chan_timeout(chanShut, 1800, 2500)

The problem is that focus line seems to get skipped. When the button is pressed the pwm goes from 1175 then to 1800 for 2500ms then back to 1175. I need it to stay at 1500 for the focus step.

If anyone can anyone help with this, I would appreciate it?

Hi, just from the top of my head, I think you need another function that simply set the pwm without timout. Not sure if there is one and if it has binding to lua, or maybe the the existing function can accept 0 or -1 for no timeout? Anyway just my 2c
HTH

Hi.
Thanks for your comments.
As I understand it there are two functions.
servo.set_output (function_number, PWM)
Which latches the pwm to a certain value and I have used this to set the neutral setting of 1175

set_output_pwm_chan_timeout(channel, pwm, timeout)
This sets the pwm at a certain value for a certain time before turning to the previous value (1175).
I have this function twice in a row, first to set the focus then to trigger the shutter.

In the script I have them listed as follows
SRV_Channels:set_output_pwm_chan_timeout(chanShut, 1500, 2500) – This sets the time for the camera Focus trigger 2500ms default.
SRV_Channels:set_output_pwm_chan_timeout(chanShut, 1800, 2500) – This sets the time for the camera shutter trigger 2500ms default.

What seems to be happening when looking at the mission planner status is that the pwm goes from the neutral pwm of 1175 to 1800 for 2500ms then back to 1175. That is the line commanding the pwm to go to 1500 for 2500ms is skipped.
That’s the bit I don’t understand.

Can you share your full script, very hard to say what is going on without the context.

1 Like

Hi Peter
Thanks for your offer of help.
Here is a link to the file and I have documented at the top the problem which I hope is clear?
https://drive.google.com/file/d/126VzXCSEQzINb3eFvtwCFZUlC1yAAN0Z/view?usp=sharing
The hardware works OK as I have tested it on a Taranis.
Cheers.
Peter

Your setting overriding the 1500 immediately. set_output_pwm_chan_timeout sets the pwm from now for the time out ms. They don’t add up, so you cant do override x then override y. You need to wait x ms then call it again.

In arduino land you would use a delay, but we dont get delays in lua, but you can call a function in x ms.

for example, something like this

SRV_Channels:set_output_pwm_chan_timeout(chanShut, 1500, 2500) -- This sets the time for the camera Focus trigger 2500ms. 
return shutter_trigger(), 2000

function shutter_trigger()
    SRV_Channels:set_output_pwm_chan_timeout(chanShut, 1800, 2500) -- This sets the time for the camera shutter trigger 2500ms.
    return update()
end

Hi Peter
Thanks for you help and a delay looks a good solution however I am getting a syntax error on the line.
return shutter_trigger(), 2000.
I’m not sure how this line works and ask for some help?
shutter_trigger() calls the function but how is it delayed 2000ms. Is it linked to ‘return’ or the function call? Does it wait 2000ms before or after calling the function?
Also in the function why is the line ‘return update()’ needed as it is not returning anything and what does ‘update()’ do.
I’m sure these are elementary questions but I would be appreciate any help!
Thanks
Peter

This works in the same way as you set your loop rate on line 120. It calls the given function in x ms.

update is your main loop, defined on line 56, after calling the new function you need to get back into your main loop.

1 Like

Hi Peter
Thanks again for your help!
I have included the camera shutter function but I am getting a syntax error which I can’t resolve and ask if you could have a look to see where it is?
Here is a link to the revised file.
https://drive.google.com/file/d/14GL0cXyvh77g5xKmdDeAx9RXULpIaiHa/view?usp=sharing

There error is.
Compilation error on line 80:
…Testing\For Forum Question\Seagul v40c Shut REC Zoom.lua:80: ‘end’ expected (to close ‘if’ at line 75) near ‘gcs’

All the if statements seem closed??
Any help would be appreciated!
Peter

you return on line 78, the stuff on lines 80 and 81 will never be used. When come back out of shutter_fire() it will be at the start of update not where you called it.

Hi Peter
Thanks and I have spent some time trying your suggestions but I am not convinced adding a function plus adding a delay on a return line works delaying two consecutive
SRV_Channels:set_output_pwm_chan_timeout(chan, 1330, 2500) lines?
It works for the second one but not the first.

Here a simple version with delays everywhere, but the Shutter Focus delay is skipped

If anyone can help it would be appreciated.
I would like to achieve is to focus for 2500ms and then trigger the shutter for 2500ms.

The code an attempt to adapt the #REC script posted by MadRC for the #REC2

https://drive.google.com/file/d/105TsQgiL3lzgTDNr3Prz6OGgOxRF3zDx/view?usp=sharing

– Seagull #REC2 Manual Photo
– This tests using a Herelink to trigger Seagull #REC2 Ch3 Manual Photo
– Herelink setup
– Set a button to Toggle channel 14 from 1100 to 1990
#REC2 Manual photo pwm settings
– Neutral 1175
– Focus 1500 for 1 second
– Shutter 1800 100ms

local ShutterTrigger = false – ShutterTrigger is used in the short press action to check the Current state for short press input and is set by the script on loop.
local laststateShutter = false – Last state is used log the last position of channel 14 input and is set by the script on loop.
local SERVO_FUNCTION_SHUTTER = 95 – 94 is the Script 2 servo output in Ardupilot. Shutter
local chanShut = SRV_Channels:find_channel(SERVO_FUNCTION_SHUTTER) – sets chanShut to output servo action

gcs:send_text(0, “Seagull camera manual trigger Script for Herelink running”) – This sends a message to the ground station to say the script is running.

function shutter_focus()
SRV_Channels:set_output_pwm_chan_timeout(chanShut, 1500, 2500) – This sets the time for the camera Focus trigger 2500ms.
gcs:send_text(0, “Shutter Focus”)
return shutter_fire(), 2500
end

function shutter_fire()
SRV_Channels:set_output_pwm_chan_timeout(chanShut, 1800, 2500) – This sets the time for the camera shutter trigger 2500ms.
gcs:send_text(0, “Shutter Trigger”)
laststateShutter = ShutterTrigger
return update(), 2500
end

function update() – This is the loop section of the script.

pwmVShutter = rc:get_pwm(14) – gets pwm of ch 14, if less than or equal to 1500 then it sets ShutterTrigger to false
if pwmVShutter <= 1500 then
ShutterTrigger = false

elseif pwmVShutter >= 1501 then -- gets pwm of ch 14, if more than or equal to 1501 then it sets ShutterTrigger to true
    ShutterTrigger = true
end 

SRV_Channels:set_output_pwm(SERVO_FUNCTION_SHUTTER, 1175) -- Neutral for Manaul Focus

if not (ShutterTrigger == laststateShutter )  then -- This sets the output to 1300 momentary to trigger the camera shutter if the state on ch 14 changes ie has an input to trigger the shutter.
  return shutter_focus(), 2500 -- waits the amount set in the time before calling function 
end

return update, 500 – This loops the script every 500ms.

end

return update(), 3000 – This sets a delay to start the script on boot.

my fault, my example was bad, remove the brackets on the function when calling return.

Manual Photo Ver 01.lua (2.3 KB)

1 Like

Works!! Finally got there.
Thanks!

Servers by jDrones