#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_DEPRECATE #pragma warning(disable : 4115) #include "stdafx.h" #include "phidget21.h" #include #include #include #include #include #include #include #include #include #include #include #include "def.h" #include "temp.h" #include "Dome.h" #include "com_port.h" #include "audine.h" #include "ExternDef.h" /* Dome coordinates are 0 at meridian and horizon. Telescope coordinates are 0 (hour angle) at meridian and equator. X, Y, Z coordinates are +West/East, +South/North, +Up/Down. ha = hour angle +West, ra = right ascension +East, dec = declination +N */ char BarCodeRxData[BARCODE_RX_CHAR_SIZE], BarCodeComPort[] = COM_BARCODE; char PoluluRxData[POLULU_CHAR_SIZE], PoluluTxData[POLULU_CHAR_SIZE], PoluluComPort[] = COM_POLULU; int dome_west_req, dome_east_req, DomeStallCnt = 0, DomeMarkerFound = 0, DomeParkMarkerFound = 0, dome_mode = PARK, p_dome_mode = -1, DomePowerMsg = 0, dome_power = 0, DomeBattCharge = 0, IOComAip[4], BarCodeInitStatus, BarCodeRxStatus, BarCodeTxStatus, BarCodeHandle = -1, BarCodeRxByteRead, SlitInMotion = 0, SlitInProx = 0, DomeEncoder, DomeParkMarkerSearch = 0, BarCodeStatusFail = COM_FAIL_MAX, SlitMotorOn = 0, PoluluInitStatus, PoluluRxStatus, PoluluTxStatus, PoluluHandle = -1, PoluluRxByteRead, PoluluTxByteWritten, PoluluStatusFail = COM_FAIL_MAX; t_PoluluError PoluluError = {0}; float slit_win = MIN_SLIT, dome_enable_tim = 0.f, DomeStalledTimer = 0.f, dome_pos = DOME_PARK, p_dome_pos = DOME_PARK, dome_vel = 0.f, dome_err, dome_pos_req = DOME_PARK, dome_park_dir, azimuth = DOME_PARK, altitude, MotionMsgTime = 0.f, DomeMotorCur12, DomeMotorCur34, test_posn = DOME_PARK, test_posn_ = DOME_PARK, test_vel = 4.f/60.f, DomeBattVolt = 12.0f, DomeBattCurr = 0.0f, DomeBattCurr_ = 0.0f, DomeBattCurrOff = 0.f, DomeBattCurrExcTm = 0.f, SlitSpeed = 0.f, DomeMotionBarCodeTimer = 0.f, DomeVelocityTimer = 0.f, DomeMotionTimer = 0.f, DomeBattVolt__ = 12.0f, DomeBattCurr__ = 0.f, DomeBarCodePrevious = DOME_PARK; extern int slit_state, slit_state_req, slit_power, TempIRCloud, UtilPower; extern float vtime, dtime, dmtime, itime, jtime, AscomDomeAzimuth, Ha, Dec, TempIRSky, TempIRSky_f, DomeTemp; extern double dt[]; void DomeControl() { ..... // Polulu motor controller if (PoluluHandle >= 0) { static int TxStep = 0, RxStep = 0, Var = 0, LastCmdStatus = 0, ErrorStatus = 0, Direction = POLULU_MOTOR_STOP, StatusMsg = 0; static float ReqSpeed, Vin = 0.f, Temp = 0.f, SlowPollingTimer = POLULU_POLLING_TIMER, slit_win = MIN_SLIT; float TargetSpeed; TargetSpeed = limitc((dome_err * 0.1f), -1.0f, 1.0f) * (POLULU_MAX_MOTOR_CMD - POLULU_MIN_MOTOR_CMD); ReqSpeed += limita((TargetSpeed - ReqSpeed), POLULU_ACCELERATION); if (dome_err > slit_win) { dome_power = 1; Direction = POLULU_MOTOR_FORWARD; slit_win = MIN_SLIT * 0.1f; SlowPollingTimer = POLULU_POLLING_TIMER; } else if (dome_err < -slit_win) { dome_power = 1; Direction = POLULU_MOTOR_REVERSE; slit_win = MIN_SLIT * 0.1f; SlowPollingTimer = POLULU_POLLING_TIMER; } else { dome_power = (SlowPollingTimer > 0.f); Direction = POLULU_MOTOR_STOP; slit_win = MIN_SLIT * 1.0f; if (SlowPollingTimer > 0.f) SlowPollingTimer -= (float)dt[0]; StatusMsg = 0; } PoluluTxStatus = Win32GetComTxStatus(PoluluHandle, &PoluluTxByteWritten); if (PoluluTxStatus == COM_SUCCESS) { static int SlowPollingCounter = 0; switch (TxStep) { case 0: sprintf(PoluluTxData, "GO\r"); break; case 1: case 2: case 4: case 5: case 7: case 8: if (Direction != POLULU_MOTOR_STOP) sprintf(PoluluTxData, "%c%d\r", Direction, (int)(absf(ReqSpeed) + POLULU_MIN_MOTOR_CMD)); else sprintf(PoluluTxData, "%c\r", POLULU_MOTOR_STOP); break; case 3: sprintf(PoluluTxData, "D0\r" ); Var = POLULU_VARIABLE_STAT; break; case 6: sprintf(PoluluTxData, "D23\r"); Var = POLULU_VARIABLE_VIN; break; case 9: sprintf(PoluluTxData, "D24\r"); Var = POLULU_VARIABLE_TEMP; break; default: PoluluTxData[0] = 0; if (SlowPollingTimer <= 0.f) { if (SlowPollingCounter++ < 1000) break; SlowPollingCounter = 0; TxStep = -1; } else TxStep = -1; break; } TxStep++; PoluluStatusFail = 0; PoluluTxStatus = Win32ComTx(PoluluHandle, PoluluTxData, strlen(PoluluTxData), &PoluluTxByteWritten); } PoluluRxStatus = Win32ComRx(PoluluHandle, PoluluRxData, POLULU_CHAR_SIZE, &PoluluRxByteRead); if ((PoluluRxStatus == COM_SUCCESS) && PoluluRxByteRead) { int ch, n = 0, ReplyComplete; static int Value = 0, ReplyValue = 0; while (PoluluRxByteRead--) { ch = PoluluRxData[n++]; ReplyComplete = 0; switch (ch) { case POLULU_OK: LastCmdStatus = ch; break; case POLULU_LAST_CMD_ERR: LastCmdStatus = ch; break; case POLULU_LAST_CMD_INV: LastCmdStatus = ch; break; case CR: break; case LF: ReplyComplete = 1; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': Value = Value * 10 + (ch - '0'); ReplyValue++; break; default: break; } if (ReplyComplete) { if (ReplyValue) { switch (Var) { case POLULU_VARIABLE_STAT: ErrorStatus = Value; if (ErrorStatus && !StatusMsg && dome_power) { StatusMsg = 1; sprintf(strRT, "Dome drive fault - status %d", ErrorStatus); Log(strRT, LOG); } break; case POLULU_VARIABLE_VIN: Vin = (float)Value * 0.001f; break; case POLULU_VARIABLE_TEMP: Temp = (float)Value * 0.1f; break; } Value = ReplyValue = 0; } } } PoluluStatusFail = 0; DomeDriveVoltage = Vin; DomeDriveTemp = Temp; } else if (PoluluRxStatus < 0) PoluluStatusFail++; else PoluluStatusFail = 0; } .... void DomeInit(int Connect) { // Barcode reader serial port setup ComPortHandler(&BarCodeHandle, BarCodeComPort, "BarCode", &BarCodeInitStatus, &BarCodeStatusFail, Connect, COM_BUFFERING, 9600); ComPortHandler(&PoluluHandle, PoluluComPort, "Polulu", &PoluluInitStatus, &PoluluStatusFail, Connect, COM_BUFFERING, 9600); DomeFlatVoltMsg = 0; } ...