Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Friday, June 1, 2012

Controling Devices With JSON

RTS2 has an XMLRPC which also supports JSON. This make it very easy to poll and control devices external of the driver. Here is a simple example of controlling the Tban fan controller based on temperature difference between inside the dome and the outside ambient temperature.




from rts2json import jsonProxy, createJsonServer 
import socket
import xmlrpclib
import json
import httplib

#connect to server
createJsonServer('localhost:8889','','')

#get temps from devices
externalTemp = jsonProxy().getValue('S0','TEMP_AMB')
internalTemp = jsonProxy().getValue('S2','DS1')

#calc the difference
diffTemp = internalTemp - externalTemp

#set the fan speed based on rules
if diffTemp < 0:
    jsonProxy().setValue('S2','MFanCon',0)

elif diffTemp > 7:
    jsonProxy().setValue('S2','MFanCon',3)

elif diffTemp > 4:
    jsonProxy().setValue('S2','MFanCon',2)

elif diffTemp > 0:
    jsonProxy().setValue('S2','MFanCon',1)

The rts2json is in the RTS2 repo. Simply connect via a JSON proxy and get and set values of devices. The script is set to a 15 minute cron and adjust fans speeds accordingly.

Wednesday, February 8, 2012

First light....

Last Monday we finally got first images running through RTS2. The only missing part of the image acquisition is automatic focusing. Focuser still needs testing which we will probably get to end of this week.

Our Proof

Though this is quite a milestone there is still loads of work left to do. There are several software pieces that need work before BORAT can really achieve a primitive autonomous state.

Monday, July 18, 2011

Fixing AstroHaven Enterprises dome driver

Timing is everything and especially so when it comes to computers.

When issuing a command the dome controller will run the command exactly for one second. So when writing the driver the code to open and close the leafs were dead simple. Or so I thought so......


cmdSent=0;
response = '\x00';

while(response != POLL_B_OPENED && cmdSent < MAX_COMMANDS)
{
sconn->writePort(CMD_B_OPEN);
sconn->readPort(response);
cmdSent++;
}

if(cmdSent < MAX_COMMANDS)
{
return 1;
}
else
{
return -1;
}


This code simply sends the command and keeps sending till it gets the wanted response. There is a upper limit set in the header file. The variable cmdSent is incrementing to the limit. The limit is just in case something might happen we can report a fail to open or close. A question might come up why we can't simply hard code a number?

Two reason, first is the controller and motors isn't exact. I can't say opening the dome will take X amount of seconds because with age the time to open and close will change. Second this driver will work for all three sizes of AHE domes with same controller. (Heard AHE now ships with different controller) Make it versatile as much as possible.

The problem started when converting the AHE driver from a custom serial driver to the standard RTS2 ConnSerial driver. So simply replaced write and read with the functions of the ConnSerial and tested. Everything worked except the opening and closing.

I worked on this problem for several hours. Mainly it took so long because it was super late and debugging abilities lessen with lack of sleep. :) Can you spot the problem? Hint: readPort() is non blocking......

Originally the serial read function of the custom serial was blocking. So when I replaced with the new code forgot it was non blocking. When this code runs it will reach the upper limit before the leaf started to close or open thus throwing an error and quitting. Quick fix is to sleep giving time for dome controller to move.


while(response != POLL_B_OPENED && cmdSent < MAX_COMMANDS)
{
sconn->writePort(CMD_B_OPEN);
sleep(1); //simple fix here
sconn->readPort(response);
cmdSent++;
}


Lesson learned, never assume anything and RTFM!