Communications between Python and Stellarium (Stellarium Telescope Protocol)

 

In this post, I’m going to explain a way to simulate a telescope connection to Stellarium with Python, so that we can both receive coordinates pointed with it, and send coordinates to display the field of view indicator on the screen.

The coordinates that Stellarium sends are equatorial coordinates.

In the page of the Stellarium project we can find information about how to control a telescope with Stellarium. I opted for the client-server over TCP/IP option, so we’re going to implement a “Stellarium Telescope Protocol” server with Python.

We only need to manage an asynchronous connection, so we’ll use a simple socket with the asyncore Python library.

This protocol uses only two types of messages, one for server→client direction and other for client→server, with the following structure:

Scheme for server→client message

LENGTH (2 bytes, integer): length of the message
TYPE (2 bytes, integer): 0
TIME (8 bytes, integer): current time on the server computer in microseconds
    since 1970.01.01 UT. Currently unused.
RA (4 bytes, unsigned integer): right ascension of the telescope (J2000)
    a value of 0x100000000 = 0x0 means 24h=0h,
    a value of 0x80000000 means 12h
DEC (4 bytes, signed integer): declination of the telescope (J2000)
    a value of -0x40000000 means -90degrees,
    a value of 0x0 means 0degrees,
    a value of 0x40000000 means 90degrees
STATUS (4 bytes, signed integer): status of the telescope, currently unused.
    status=0 means ok, status
Scheme for client→server message

LENGTH (2 bytes, integer): length of the message
TYPE (2 bytes, integer): 0
TIME (8 bytes, integer): current time on the server computer in microseconds
    since 1970.01.01 UT. Currently unused.
RA (4 bytes, unsigned integer): right ascension of the telescope (J2000)
    a value of 0x100000000 = 0x0 means 24h=0h,
    a value of 0x80000000 means 12h
DEC (4 bytes, signed integer): declination of the telescope (J2000)
    a value of -0x40000000 means -90degrees,
    a value of 0x0 means 0degrees,
    a value of 0x40000000 means 90degrees

 

The values are transmitted in little-endian format, and we’ll use ConstBitStream (code.google.com) to manage it.

The following code snippet displays the received coordinates in the terminal, and then returns them back to Stellarium to show the field of view indicator on the screen.

 

We’ve extended the QtCore.QThread class because we’ll use this code (with some modifications) with a Qt user interface as a module. Also you can see that some aux functions are used to some unit conversions, for example coords.rad_2_stellarium_protocol and coords.hourStr_2_rad. These functions are implemented in another file “coords.py” that is imported as a module with “import coords”:

 

We just have to place both files in the same directory, (telescope_server.py and coords.py), give execution permissions (chmod +x telescope_server.py) and execute:

chmod +x telescope_server.py 
./telescope_server.py 
telescope_server.py: run - INFO: Telescope_Server running.

Now the Stellarium configuration. First we need to configure the telescope connection:

Configuration window > Plug-ins > Telescope Control > Configure Telescope

 

Stellarium configuration.

Now we set the connection mode, name, IP and port:

  • Connection mode: External Software or a remote computer
  • Name: Virtual Telescope
  • IP: localhost
  • Port: 10001

Once connected, and assuming that we only have one configured device, we press “Ctrl + 1” to send the coordinates from Stellarium (the number 1 indicates the first configured device). Then we should see in console the received coordinates (among other debugging data):

./telescope_server.py
telescope_server.py: run - INFO: Telescope_Server running. 
telescope_server.py: handle_accept - DEBUG: Connected: ('127.0.0.1', 37023) 
telescope_server.py: handle_read - DEBUG: Size: 20, Type: 0, Time: 1342975913635132, RA: 1932791525 (e50e3473), DEC: 17943536 (f0cb1101) 
coords.py: rad_2_stellarium_protocol - DEBUG: (hours, degrees): (10.800277, 1.503900) -> (10h48m1.0s, 1º30'14'')

The script returns back the coordinates to Stellarium so that it displays the field of view indicator:

Virtual Telescope connection with Stellarium

And we already have in our script the equatorial coordinates of any celestial object. Use it wisely 😉

Cheers!

 
Tagged . Bookmark the permalink.

14 Responses to Communications between Python and Stellarium (Stellarium Telescope Protocol)

  1. Bhanesh Awasthi says:

    Hello,
    I am an engineering student in Germany.Your posted project is really interested and well explained.
    I am trying to interface my own created mount with stellarium using serial communication and I succeed to send RA and DEC to the stellarium and my scope is visible on the stellarium. My next task is to slew telescope reticle to the selected target. When I select the target and press Ctrl+1 and I check in the telescope debug file:-
    sendCommand(Lx200CommandGetRa)
    16386, 13:47:07.789861Z: Lx200Connection::sendCommand(Lx200CommandGetDec)
    16386, 13:47:09.073935Z: Lx200Connection::sendCommand(Lx200CommandStopSlew)
    16386, 13:47:09.074935Z: Lx200Connection::sendCommand(Lx200CommandSetSelectedRa(18:55:25))
    16386, 13:47:09.074935Z: Lx200Connection::sendCommand(Lx200CommandSetSelectedDec(-24:18:25))
    16386, 13:47:09.074935Z: Lx200Connection::sendCommand(Lx200CommandGotoSelected)

    My confusion is that How can I send the RA and DEC of the selected target to my main computer i.e. Raspberry Pi for further coordinate conversion?
    My final task is to get the RA and DEC of the selected target in Pi and convert the equatorial coordinates according to the steps required by the stepper motor to move to the selected target.

    It would be very helpful for me if you can give some suggestion.

    Thanking in advance for your kind cooperation.

    • JuanRa says:

      Hi Bhanesh,

      I’m not sure if I fully understand your issue, but anyway I’ll give it a try 🙂

      If you can already send coordinates to Stellarium that means you have already the socket connected to it, so that’s the half of the way.

      The function that receives the coordinates from Stellarium is “handle_read” in the “Telescope_Channel” class (see the code above).

      In this function is where you can do the coordinate transformations. In my project I did that in Arduino, since my goal was to make the device as self-governed as possible.

      By the way, here you have the full code of the project. Maybe it can be useful:

      https://github.com/juanrmn/Arduino-Telescope-Control

      In the testing folder, there is a script for debugging Stellarium communications. I think that if you run it and play a bit with the code you’ll finally realise how it works.

      I hope this helps you…

      Regards.

  2. Pingback: Help converting python::bitstream usage to PHP equivalent | DL-UAT

  3. rickbassham says:

    Thanks for the code. I modifed it and am using it to automatically solve images taken by my camera, and then update Stellarium with my telescope’s current location.

  4. Fouad says:

    how to run python file please ?

    • JuanRa says:

      I assumed that the file has exec permissions in the post… I’m going to fix that, thanks 🙂

      Meanwhile, to run a python file you can do:

      Or you can add exec permission to the file and then run it:

       

  5. Alex Petrov says:

    Hello. I try to reproduce your project and I’ve some issues with converting coordinates from Horizontal to Equatorial. I am embarrassed by this particular line (in getECoords func.):

    (*ar) = atan2(EVC[1], EVC[0]) + (_k*(t-_t0));

    atan2 returns value in the range [-pi:pi] and you add the time. What the point? Now the value neither in radians nor in degrees. Can you explain more detailed this.

    Thanks a lot.

  6. ErnestoCD says:

    How do i syncronise Stellarium with the python script? i hope it can be done becouse i want to make it work without human action. thanks for everything, your code was ery usefull

  7. Pingback: Using SenseHat for azimuth and altitude | w01f359

  8. Yeb Havinga says:

    Hi. Thank you very much for your work and explanations. I want to use your telescope server as a submodule of a project of my own and therefore copied the code on a github project. Please let me know if you object to this, and I will remove the subproject.

    • JuanRa says:

      Hi Yeb, of course you can use the code for whatever you want. It’s good to know that this can be useful for someone. Also I`ve seen you mentioned me on Github, that’s enough for me 🙂

      Cheers

  9. Hector Santini says:

    Saludos JuanRa:

    Do you have a complete version where everything runs on Raspberry Pi-3 (No Arduino)? The Pi-3 have 4 USB’s and an integrated Wifi.

    Gracias,

    Hector

  10. R. says:

    Hi,

     

    THank you for this great work, I was wondering if you knew how to draw a trajectory in stellarium. For example, let’s say i have the coordinates of my object and also the exposure time. Do you know if I can trace the trajectory of that object during the exposure time?

     

    Thanks!

     

    R.

    • JuanRa says:

      Hi,

      I’m sorry but I don’t know how to do that. It’s been a long time since I was programming this, but as far as I can remember I don’t think this is even possible. The Stellarium protocol was quite simple. Anyway as I said it was a long time ago, so perhaps now the protocol has new functions for that. It’s an useful option indeed.

Leave a Reply

Your email address will not be published. Required fields are marked *