Page 1 of 1

Lua Script Delay Function

Posted: Tue May 02, 2017 3:36 pm
by boggie1688
My new project for the RCP3 is to get the GoPro script nice and polished.

The script provided in the wiki is great! Thank you to the author!

I was trying to spruce up the script a bit more, but having it set the following:
1. Switch to camera mode
2. Set orientation to up
3. Set desired resolution
4. Set desired Field of View

This way, I don't have to remember to setup the camera before hand.

It was easy enough to create functions for each of those camera settings, however I think the script is executing too quickly. It fires commands rapidly at the GoPro, and while the first command is recognized, I think the subsequent ones are missed.

Is there a way to have the Lua script delay a given number of seconds after firing the first command?

Posted: Thu May 04, 2017 4:24 pm
by brentp
Try the sleep(ms) command - this will stall the lua script for the specified time.

Great on the GoPro script!

We're working to implement this capability in the firmware. Need to check again but, is 'switch to camera mode' the command that some of the newer GoPro's need to enable remote control? This will be one of the things we need to do (I believe) when we implement it in the official firmware.

Posted: Thu May 04, 2017 4:36 pm
by boggie1688
Brent,

I have a GoPro Hero3, not sure about the newer units.

I was using the camera mode to make sure that the GoPro switches to video recording and not photo/time lapse burst. I could have used the GoPro between Autox events, and want to make sure that I don't take a photo when I'm actually trying to record a video.

I'll post a copy of what I put together when I finish.

Posted: Fri May 05, 2017 3:58 am
by boggie1688
setTickRate(50)


function onTick()
checkGoPro()
getSteering(1)
if getGpsSpeed() >= 1 then
startLogging()
end
end

steerId = addChannel("Steering", 50, 0, -450, 450, "Deg.")

function getSteering(chan)
id, ext, data = rxCAN(chan,100)
if id == 194 then
processSteering(data)
end
end

function processSteering(data)
local steer = 0
if data[2] < 128 then
steer = -1*((data[2]*256)+data[1])
else
steer = (((data[2]-128)*256)+data[1])
end
setChannel(steerId, (steer*.0436))
end


--Specify your GoPro wifi password here
goproPwd = "blahblah"

--Specify your GoPro SSID here
goproSsid = "RCP3WIFI"

--Speed threshold to start recording
goproStart = 5

--Speed threshold to stop recording
goproStop = 1

--How fast we check, in Hz
tickRate = 10

--Set this to 1 to log communications between RCP & WiFi
debug = 1
-----------------------------
--DO NOT EDIT BELOW
-----------------------------
--the serial port where the WiFi is connected
port = 4
--indicates wifiStatus
--0 = not init, 1 = init sent, 2 = got IP, 3 = ready
wifiStatus = 0
lastInitTime = 0
initTimeout = 20000

function logMsg(msg)
println('[GoProWiFi] ' ..msg)
end

function sendCrlf()
writeCSer(port, 13)
writeCSer(port, 10)
end

function sendRaw(val)
for i=1, #val do
local c = string.sub(val, i, i)
writeCSer(port, string.byte(c))
end
end

function sendAt(val)
if debug == 1 then logMsg('send: ' ..val) end
sendRaw(val)
sendCrlf()
end

function toInt(val)
return string.sub(val, 1, -3)
end

function httpGet(url)
sendAt('AT+CIPSTART="TCP","10.5.5.9",80')
sleep(500)
local crlf = string.char(13) ..string.char(10)
local get = 'GET ' ..url ..' HTTP/1.0' ..crlf ..crlf
sendAt('AT+CIPSEND=' ..toInt(#get))
sleep(100)
sendRaw(get)
sleep(100)
sendAt("AT+CIPCLOSE")
end

function sendGoProShutter(cmd)
httpGet('/bacpac/SH?t=' ..goproPwd ..'&p=%' ..cmd)
end

function startGoPro()
logMsg('start GoPro')
sendGoProShutter('01')
end

function stopGoPro()
logMsg('stop GoPro')
sendGoProShutter('00')
end

function initWiFi()
logMsg('initializing')
sendAt('AT+RST')
sleep(2000)
sendAt('AT+CWMODE_CUR=1')
sleep(1000)
sendAt('AT+CWJAP_CUR="' ..goproSsid ..'","' ..goproPwd ..'"')
wifiStatus = 1
end

function processIncoming()
local line = readSer(port, 100)
if line ~= '' and debug == 1 then print(line) end
if string.match(line, 'WIFI GOT IP') then
wifiStatus = 2
end
if wifiStatus == 2 and string.match(line, 'OK') then
wifiStatus = 3
sleep(2000)
if power == nil then
power = 0
end
if power == 0 then
logMsg('Powering up GoPro')
sleep(2000)
sendPowerUp('01')
sleep(1000)
logMsg('GoPro On')
power = 1
end
if cameramode == nil then
cameramode = 0
end
if cameramode == 0 then
logMsg('Switching to Camera Mode')
sleep(1000)
modeCameraGoPro('00')
sleep(1000)
logMsg('Camera Mode Set')
cameramode = 1
end
if orientation == nil then
orientation = 0
end
if orientation == 0 then
logMsg('Setting Orientation Up')
sleep(1000)
orientationupGoPro('00')
sleep(1000)
logMsg('Orientation is Up')
orientation = 1
end
if resolution == nil then
resolution = 0
end
if resolution == 0 then
logMsg('Setting Resolution')
sleep(1000)
setresolGoPro('06')
sleep(1000)
logMsg('Resolution Set')
resolution = 1
end
if fov == nil then
fov = 0
end
if fov == 0 then
logMsg('Setting FOV')
sleep(1000)
setfovGoPro('01')
sleep(1000)
logMsg('FOV Set')
fov = 1
end
end
end


function sendPowerUp(cmd)
httpGet('/bacpac/PW?t=' ..goproPwd ..'&p=%' ..cmd)
end

function modeCameraGoPro(cmd)
httpGet('/camera/CM?t=' ..goproPwd ..'&p=%' ..cmd)
end

function orientationupGoPro(cmd)
httpGet('/camera/UP?t=' ..goproPwd ..'&p=%' ..cmd)
end

function setresolGoPro(cmd)
httpGet('/camera/VR?t=' ..goproPwd ..'&p=%' ..cmd)
end

function setfovGoPro(cmd)
httpGet('/camera/FV?t=' ..goproPwd ..'&p=%' ..cmd)
end

function checkGoPro()
if wifiStatus == 0 then
initWiFi()
lastInitTime = getUptime()
return
end
if wifiStatus == 1 and getUptime() > lastInitTime + initTimeout then
logMsg('could not connect to GoPro')
wifiStatus = 0
end
processIncoming()
if wifiStatus ~= 3 then
return
end
trigger = getGpsSpeed()
if recording == 0 and trigger > goproStart then
startGoPro()
recording = 1
end
if recording == 1 and trigger < goproStop then
stopGoPro()
recording = 0
end
end
Excuse the messy and long code.

Currently, I have an issue where the GoPro doesn't turn on when I power up the datalogger. However, if I clear the script and run it from the software, all the commands execute just fine.

I tried polling the log as I turn the car on, but the log doesn't poll until I clear and run the script. Not exactly sure what is going on here. Otherwise the script works perfectly, turns the gopro on and sets all my parameters too!

Posted: Fri May 05, 2017 4:04 am
by brentp
Hrm, the lua script should run upon power up. Might want to try sprinkling some println() logging messages in there to prove it's working.

Perhaps there's a race condition where the wifi module isn't yet ready to accept commands from the Lua script when it's first powered up. Some of those debug logging messages will help shed light on it.

Posted: Fri May 05, 2017 4:10 am
by boggie1688
Stupid me!

I had my laptop plugged in via USB! I though I was turning the RCP3 of when I turned the car off, but my laptop was keeping the unit powered!!!

:oops: :lol:

Posted: Fri May 05, 2017 4:13 am
by brentp
No worries, I do that *all the time* :)

Posted: Fri May 05, 2017 4:25 am
by boggie1688
https://youtu.be/3E4tweXkLZI

Yes!

Probably could be tweaked to speed it up but still not bad.

Posted: Fri May 05, 2017 4:40 am
by brentp
Alright, that's great! powering it up is definitely a bonus.

It has no detrimental effect if the camera is already powered on, and all of those commands are set?

I've pointed another fellow to this thread to confirm if the command to set camera mode is the same as what you're doing here; apparently there are newer GoPros that need additional commands before video can start recording; perhaps you've solved that here.

Posted: Fri May 05, 2017 5:10 am
by boggie1688
As far as I could tell, running the command twice didn't adversely affect the GoPro.

I reference this website for the httpGet commands.
http://hobbyrobo.com/gopro-url-commands/

I also came across this when hunting for commands. He appears to have figured out all the commands for all the GoPro generations.
https://github.com/KonradIT/goprowifihack

Here is one specific to the Hero 5.
https://github.com/KonradIT/goprowifiha ... ommands.md

It appears the Hero4 and Hero 5 share the same Wifi Commands:
https://github.com/KonradIT/goprowifiha ... ommands.md


I'll play around with it again tomorrow and see if I can speed it up a bit. I also want to have the RCP3 turn the GoPro off when I turn the car off. Going to see if there is a way to have that work.[/url]

Posted: Mon Jul 17, 2017 3:28 am
by jeffmcaffer
I just posted a Wake on LAN snippet in another thread. Check out viewtopic.php?t=5398.

I've been working with a Session but the same sort of thing should work for other cameras needing WoL.