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!

No comments:

Post a Comment