Write Location to file Scripting Issues

Hey,

Trying to get a drone to log coordinates into a .txt or .csv file while in flight. Not getting any success. I’m using a slightly modified version of the example script here. I have modified it to save to SD only and to log ahrs.get_position() instead of roll, etc (I’ll paste my script at the bottom). I’ve enabled SCR_ENABLE and increased SCR_HEAP_SIZE to 65k up from 44k.

I have the .txt file created in the same directory as the script, and tried stepping back to just writing “Hello World” but can’t get anything to print. One issue I had but got around was having my scripts show up in mission planner as file_name.lua.txt but that was fixed by saving as filename.LUA at which point the system seems to recognize it as the correct format. As I understand it the scripts run on startup. Is the SD card what I’m accessing from mission planner when connected by USB? Accessing the SD card on this chip requires dismantling the drone.

I was wondering if anyone had pointers that could help me get this working. Thanks!

 -- example of logging to a file on the SD card and to data flash
local file_name = "LOG_DATA.txt"
local file

-- the data
local coords = ahrs.get_position()

local function write_to_file()

  if not file then
    error("Could not open file")
  end

  -- write data
  -- separate with comas and add a carriage return
  file:write(tostring(millis()) .. ", " .. coords .. "\n")

  -- make sure file is upto date
  file:flush()

end


function update()
  
  coords = ahrs:get_position()

  -- write to then new file the SD card
  write_to_file()

  return update, 3000 -- reschedules the loop
end

-- make a file
-- note that this appends to the same the file each time, you will end up with a very big file
-- you may want to make a new file each time using a unique name
file = io.open(file_name, "a")
if not file then
  error("Could not make file")
end

-- write the CSV header
file:write('Time Stamp(ms), coordinates\n')
file:write("Hello World\n")
file:flush()

return update, 10000

I don’t think you can access the get_position method like that.
Did you look at the wiki?
Also I don’t think you can write coords like below since it contains Location.

Thanks for the tips! I’ve definitely identified a few syntax and object access type errors in my LUA script.

In order to try to eliminate my own code writing errors I switched to just trying to get a Sample Script to work . Still I can’t get anything to print to file from the unchanged sample script which makes me think its more than just syntax errors. I’ve double checked SCR_ENABLE. I can’t figure out what I’m missing.

Today while researching ways to test my scripts I discovered REPL within Mission Planner. Unfortunately it crashes as soon as I attempt to connect. Seems to be a common issue but couldn’t find a solution. REPL is less of a concern, I just need a simple script that logs lat/long every few seconds lol. Any ideas?