For this week, we had to make (one of) our board to communicate with a terminal (PC or anything else)
Source files :
Week 11 - light barriere python code
Stellarium communication C++ Code
Here is the assignements where I had to use these skills :
I had two project for this week exercise
The first one is a derivative of the Week 11 exercise, modified to communicate with a PC/Mac.
The second idea was to make something usefull for my final project, i.e. : make the Stellarium software and the motherboard of my telescope to communicate (Stellarium has a Telescope Control Plugin witch can send commands to compatible telescopes)
As you may remember, during the Week11, I made a sort of light barrier (in fact, this exercise could have covered input programming, output programming and interface application as well).
What I've done is that I modified the python program to trigger a sound when someone pass the fablab door (this sound can be anything - from a traditionnal bell sound to an extract of some metal band song
First, I set two variables (one for entering, one for leaving) at the begining of the code
c_enter = 73 #(char "I" for "In") c_leave = 79 #(char "O" for "Out")
I used the VLC python wrapper to control... VLC from the python script.
I didn't used a lot of function, only two :
if (ord(char) == c_enter): p = vlc.MediaPlayer("some_sound.mp3") # # we check if a sound isn't already playing, to avoid sound-flood # if not (p.is_playing()): p.play()
Nothing complicated here, does what it's supposed to do
After reading the page about the telescope plugin of Stellarium, it appears that he sends only the "slew" command (the one making the telescope to move). So implementing the LX200 version of this command in the main board should be easy. I still need to know what is exactly sent anyway.
So I ended up looking at the C++ code of the TelescopeCtrl plugin, and it sends only commands related to the direct positionning of the telescope (Set/Get Right-ascendant, Set/Get Declination and GoTo) - thats OK for me, less code to analyse and implement, given that my final project is not aimed to replicate a complete commercial motorized telescope like MEADE©™ or Celestron©™.
And guess what ? It's a success... Well, sort of.
I can successfully make Stellarium communicate with a little Arduino sketch (used only for the tests), but it seems that Stellarium don't send all the commands.
Here are the commented parts of the source code :
Variables declaration, "cmd" will be the input buffer arbitrary set to 14 bytes (the maximum command size sent by stellarium + 1)
char cmd[14]; int count = 0; boolean trameReceived, commaAlreadyReceived = false;
a little recursive blink function, could have been done in a few different ways, but recursivity fun() { echo "is fun ! "; fun(); }
void blink_led(byte howmuch, int howlong) { if (howmuch > 0) { digitalWrite(13, HIGH); delay(howlong); digitalWrite(13, LOW); delay(howlong); blink_led(howmuch - 1, howlong); } }
The main loop :
void loop() { char inByte; while (Serial.available() == 0) {} // wait for data availables on serial inByte = Serial.read(); if (inByte == ':' && !commaAlreadyReceived) { // Begining of a command (some commands contains comma, hence the bool) commaAlreadyReceived = true; count = 0; } if (inByte == '#') { // end of command received // So we can say that a complete frame is in cmd[] trameReceived = true; commaAlreadyReceived = false; } cmd[count] = inByte; count++; // If more than the maximum size of cmd[] is received, flush the serial and continue // May be this is not necessary if (count > sizeof(cmd)) { Serial.flush(); } // If a complete trame is in the buffer, we can parse her if (trameReceived) { trameReceived = false; count = 0; // Set Right Ascension if (cmd[0] == ':' && cmd[1] == 'S' && cmd[2] == 'r') { Serial.print(1); blink_led(1, 300); } // Set declination if (cmd[0] == ':' && cmd[1] == 'S' && cmd[2] == 'd') { Serial.print(1); blink_led(2, 300); } // Run a SLEW (GoTo) if (cmd[0] == ':' && cmd[1] == 'M' && cmd[2] == 'S') { Serial.print(0); blink_led(3, 100); } // Those two commands are correctly received and processed // Get Declination (totally random hardcoded coordinates for testing purposes) if (cmd[0] == ':' && cmd[1] == 'G' && cmd[2] == 'D') { Serial.print("s03*30’23#"); blink_led(5, 100); } // Get Right Ascension // (totally random hardcoded coordinates for testing purposes) if (cmd[0] == ':' && cmd[1] == 'G' && cmd[2] == 'R') { Serial.print("05:54:03#"); blink_led(5, 100); } } }
As said above, right now, only the two last commands are sent regularly by Stellarium and successfully parsed (led blinks).
But if I type ":SrHH:MM:SS#" in the serial monitor, the LED blinks, too. This proves the code is working.
It seems that Stellarium - for an unknown reason - don't send the commands when I click on the "Goto" button, but he still regularly polls the "telescope" for his actual axis positions, so basically, my board is communicating with a software on my PC (witch I have analyzed his code source instead of coding)
For future work on this project, I found this repo witch seems interesting and will certainly help me
As always when I code something, devils are in the details. Some semicolon forgotted, bad logic in IF statements, misdeclaration of variables... I call those type of errors the "time-waster-errors"