Orbiter-Forum  

Go Back   Orbiter-Forum > Orbiter Space Flight Simulator > Orbiter Web Forum > Simpit Forum
Register Blogs Orbinauts List Social Groups FAQ Projects Mark Forums Read

Simpit Forum Share & discuss simpit projects for Orbiter here!

Reply
 
Thread Tools
Old 08-27-2010, 08:43 PM   #1
widdernix
Orbinaut
Default Talking to serial devices

What is the lack of Orbiter compared to flight simulators? We miss a possibility to display values, light led's and so on.
With LUA we can do this! We can now talk to a serial device and this is how to do it (works under XP and Windows 7).

What do we need.
1. Orbiter (of course )
2. A free COM port (including USB to serial converters)
3. A serial device awaiting our commands, i.e. microcontroller like Arduino (I'm using a PIC16F877).

First initialize the COM port. I tried to do this within my LUA script. It worked, but it puts Orbiter in background and I have to manualy put it back in front. This sometimes results in graphical errors. So the best is to do this in a DOS box before starting Orbiter. I'm using COM2 in this example (do not use COM numbers above 9):

mode COM2:57600,n,8,1

This sets COM2 to use 57600 Baud, no parity, 8 databit, 1 stopbit.

Next step is to create a LUA script which is sending data to the serial device. We can do this in Notepad. In this example i am sending the RCS status periodicaly to the serial device, assuming a serial target device, which is setting its ports/leds according to the received value.

And this is the script:

-- begin of script
local debug = false -- if you don't want a endless loop set debug to true
local schl = 50 -- and schl to the number of loops you want
local mycom = "COM2" -- put in your COM port here
local mybaud = "57600" -- your baud rate
local simspd = 0.2 -- wait time between serial datagrams
local status1, old_status1 -- define variables for LED status

old_status1 = -1

v = vessel.get_focusinterface() -- get a vessel handle

function serout(_sout) -- write stuff to serial port
wserial:write(_sout) -- fills the serial buffer
wserial:flush() -- send the serial buffer
end

wserial=io.open(mycom,"w+b") -- this opens the COM in binary mode

repeat
status1 = 0
if v:get_rcsmode() == RCSMODE.OFF then -- checking RCS mode
status1 = status1 + 2 -- LED 2 on
else
status1 = status1 + 1 -- LED 1 on
end
if v:get_rcsmode() == RCSMODE.ROT then
status1 = status1 + 8 -- LED 4 on
end
if v:get_rcsmode() == RCSMODE.LIN then
status1 = status1 + 4 -- LED 3 on
end

if status1 ~= old_status1 then -- to reduce traffic, send value only if it has changed
old_status1 = status1 -- save values
sendit=string.char(status1)
serout(sendit)
end

proc.wait_sysdt(simspd) -- wait a little bit, otherwise we would stress the serial port (and Orbiter)

if debug then
schl = schl - 1
end

until schl<=1 -- repeat that

wserial:close(mycom) -- this closes the COM
-- end of script

Now save "myscript.lua" in the Orbiter\Script directory.
Run the mode command as described before. Run Orbiter with a scenario at your choice. Open either the LUA console or the LUA mfd and start the script run('myscript'). And don't forget to switch on your serial device.

LUA isn't talking much about an error in scripts. It says only "execution error". In this case try to reduce steps in the script and use term.out commands.

I think you can do similar things also with the printer port (i.e. LPT1). Try it, I don't have any on my PC.

Have fun
widdernix

---------- Post added at 08:43 PM ---------- Previous post was at 05:48 PM ----------

To give you an overview what's possible, I added my current script.
sd.txt
rename it to .lua

Last edited by widdernix; 08-27-2010 at 08:48 PM.
widdernix is offline   Reply With Quote
Old 09-06-2010, 08:16 PM   #2
JDat
Orbinaut
Default

Awesome! Great job! I will test it in near future. What abut to control vessel from microcontroller? I am familiar with MCU programming and 6 years ago wrote some small code with LUA. Seems that I need to read SDK an start my experiments. Can You write sample code in LUA to receive serial port commands and turn on/off Main Engine?
JDat is offline   Reply With Quote
Old 09-08-2010, 10:20 AM   #3
widdernix
Orbinaut
Default

This seems to be tricky. I need a little bit time to check that.
widdernix is offline   Reply With Quote
Old 09-10-2010, 05:21 PM   #4
widdernix
Orbinaut
Default

I can't get a solution for "Serial in". Opening the COM in read mode freezes Orbiter because LUA is waiting for incoming bytes. Maybe somebody has a better solution? I suggest using projects like MJoy16 in combination with SV Mapper to control the ship. It's easy to make a MJoy16 if you are familliar with electronics.

Here you can see Orbiter in action. I'm docked at ISS. All Instruments are working with feedback of Orbiter.

Click image for larger version

Name:	Instruments.jpg
Views:	54
Size:	328.6 KB
ID:	5704
widdernix is offline   Reply With Quote
Thanked by:
Old 09-10-2010, 07:23 PM   #5
cinder1992
Random failhurricane.
 
cinder1992's Avatar

Default

mind if I use this so people can use REAL hardware with UVGCO?

Edit: this actually answers your question JDat
cinder1992 is offline   Reply With Quote
Old 09-29-2010, 06:47 PM   #6
widdernix
Orbinaut
Default

Hi JDat. I found a solution for serial input. Library "luars232" is needed. It's part of the luaforwindows tools. Search and install this great toolbox it has tons of usefull LUA extensions. Copy the luars232.DLL to your Orbiter main folder.
Modified script example:

Code:
rs232 = require("luars232")

local debug = false -- if you don't want a endless loop set debug to true
local simspd = 0.2 -- wait time between serial datagrams
local status1, old_status1 -- define variables for LED status
old_status1 = -1
local debug = false  -- if you don't want a endless loop set debug to true
local schl = 50      -- and schl to the number of loops you want

local port_name = "COM2"   -- put in your COM port here
local e, p = rs232.open(port_name)
if e ~= rs232.RS232_ERR_NOERROR then
    -- handle error
	term.out('',string.format("can't open serial port '%s', error: '%s'\n", port_name, rs232.error_tostring(e)))
    return
end
-- set port settings
assert(p:set_baud_rate(rs232.RS232_BAUD_57600) == rs232.RS232_ERR_NOERROR)
assert(p:set_data_bits(rs232.RS232_DATA_8) == rs232.RS232_ERR_NOERROR)
assert(p:set_parity(rs232.RS232_PARITY_NONE) == rs232.RS232_ERR_NOERROR)
assert(p:set_stop_bits(rs232.RS232_STOP_1) == rs232.RS232_ERR_NOERROR)
assert(p:set_flow_control(rs232.RS232_FLOW_OFF)  == rs232.RS232_ERR_NOERROR)

v = vessel.get_focusinterface() -- get a vessel handle

function serout(_sout) -- write stuff to serial port
  err, len_written = p:write(_sout)
end

function serin() -- read stuff from serial port
  -- read with timeout
  local read_len = 100 -- read one byte
  local timeout = 100 -- in miliseconds
  local err, data_read, size = p:read(read_len, timeout)
  assert(e == rs232.RS232_ERR_NOERROR)
  return data_read
end

repeat
  status1 = 0
  if v:get_rcsmode() == RCSMODE.OFF then -- checking RCS mode
    status1 = status1 + 2 -- LED 2 on
  else
    status1 = status1 + 1 -- LED 1 on
  end
  if v:get_rcsmode() == RCSMODE.ROT then
    status1 = status1 + 8 -- LED 4 on
  end
  if v:get_rcsmode() == RCSMODE.LIN then
    status1 = status1 + 4 -- LED 3 on
  end

  if status1 ~= old_status1 then -- to reduce traffic, send value only if it has changed
    old_status1 = status1 -- save values
    sendit=string.char(status1)
    serout(sendit)
  end

  proc.wait_sysdt(simspd) -- wait a little bit, otherwise we would stress the serial port (and Orbiter)
  
  serdata = serin()
  -- do something here
  
  if debug then schl = schl - 1  end

until schl<=1 -- repeat that
assert(p:close() == rs232.RS232_ERR_NOERROR)  -- this closes the COM
Of course the script has to be modified to your needs. Due to my LUA and programming skills it's a very basic script which surely can be done better.

@cinder1992: I thought your just using hardware, heard of your famous beer and peanuts container...
widdernix is offline   Reply With Quote
Old 09-29-2010, 07:37 PM   #7
Wishbone
Clueless developer
 
Wishbone's Avatar
Default

What about interfacing to 1553? now that would be positively funny
Wishbone is offline   Reply With Quote
Reply

  Orbiter-Forum > Orbiter Space Flight Simulator > Orbiter Web Forum > Simpit Forum


Thread Tools

Posting Rules
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Jump


All times are GMT. The time now is 04:39 AM.

Quick Links Need Help?


About Us | Rules & Guidelines | TOS Policy | Privacy Policy

Orbiter-Forum is hosted at Orbithangar.com
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright 2007 - 2017, Orbiter-Forum.com. All rights reserved.