Skip to content

Commit

Permalink
Migrate Focus Simulator (#2010)
Browse files Browse the repository at this point in the history
* Migrate Focus simulator

* avoid using single ENUM and use set / get method for values
  • Loading branch information
naheedsa authored Feb 29, 2024
1 parent c63d9d0 commit 97b1ebf
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 106 deletions.
127 changes: 64 additions & 63 deletions drivers/focuser/focus_simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void FocusSim::ISGetProperties(const char *dev)

INDI::Focuser::ISGetProperties(dev);

defineProperty(&ModeSP);
defineProperty(ModeSP);
loadConfig(true, "Mode");
}

Expand All @@ -80,28 +80,28 @@ bool FocusSim::initProperties()
{
INDI::Focuser::initProperties();

IUFillNumber(&SeeingN[0], "SIM_SEEING", "arcseconds", "%4.2f", 0, 60, 0, 3.5);
IUFillNumberVector(&SeeingNP, SeeingN, 1, getDeviceName(), "SEEING_SETTINGS", "Seeing", MAIN_CONTROL_TAB, IP_RW, 60,
IPS_IDLE);
SeeingNP[0].fill("SIM_SEEING", "arcseconds", "%4.2f", 0, 60, 0, 3.5);
SeeingNP.fill(getDeviceName(), "SEEING_SETTINGS", "Seeing", MAIN_CONTROL_TAB, IP_RW, 60,
IPS_IDLE);

IUFillNumber(&FWHMN[0], "SIM_FWHM", "arcseconds", "%4.2f", 0, 60, 0, 7.5);
IUFillNumberVector(&FWHMNP, FWHMN, 1, getDeviceName(), "FWHM", "FWHM", MAIN_CONTROL_TAB, IP_RO, 60, IPS_IDLE);
FWHMNP[0].fill("SIM_FWHM", "arcseconds", "%4.2f", 0, 60, 0, 7.5);
FWHMNP.fill(getDeviceName(), "FWHM", "FWHM", MAIN_CONTROL_TAB, IP_RO, 60, IPS_IDLE);

IUFillNumber(&TemperatureN[0], "TEMPERATURE", "Celsius", "%6.2f", -50., 70., 0., 0.);
IUFillNumberVector(&TemperatureNP, TemperatureN, 1, getDeviceName(), "FOCUS_TEMPERATURE", "Temperature",
TemperatureNP[0].fill("TEMPERATURE", "Celsius", "%6.2f", -50., 70., 0., 0.);
TemperatureNP.fill(getDeviceName(), "FOCUS_TEMPERATURE", "Temperature",
MAIN_CONTROL_TAB, IP_RW, 0, IPS_IDLE);

DelayNP[0].fill("DELAY_VALUE", "Value (uS)", "%.f", 0, 60000, 100, 100);
DelayNP.fill(getDeviceName(), "DELAY", "Delay", OPTIONS_TAB, IP_RW, 60, IPS_IDLE);

IUFillSwitch(&ModeS[MODE_ALL], "All", "All", ISS_ON);
IUFillSwitch(&ModeS[MODE_ABSOLUTE], "Absolute", "Absolute", ISS_OFF);
IUFillSwitch(&ModeS[MODE_RELATIVE], "Relative", "Relative", ISS_OFF);
IUFillSwitch(&ModeS[MODE_TIMER], "Timer", "Timer", ISS_OFF);
IUFillSwitchVector(&ModeSP, ModeS, MODE_COUNT, getDeviceName(), "Mode", "Mode", MAIN_CONTROL_TAB, IP_RW,
ISR_1OFMANY, 60, IPS_IDLE);
ModeSP[MODE_ALL].fill("All", "All", ISS_ON);
ModeSP[MODE_ABSOLUTE].fill("Absolute", "Absolute", ISS_OFF);
ModeSP[MODE_RELATIVE].fill("Relative", "Relative", ISS_OFF);
ModeSP[MODE_TIMER].fill("Timer", "Timer", ISS_OFF);
ModeSP.fill(getDeviceName(), "Mode", "Mode", MAIN_CONTROL_TAB, IP_RW,
ISR_1OFMANY, 60, IPS_IDLE);

initTicks = sqrt(FWHMN[0].value - SeeingN[0].value) / 0.75;
initTicks = sqrt(FWHMNP[0].getValue() - SeeingNP[0].getValue()) / 0.75;

FocusSpeedN[0].min = 1;
FocusSpeedN[0].max = 5;
Expand All @@ -124,16 +124,16 @@ bool FocusSim::updateProperties()

if (isConnected())
{
defineProperty(&SeeingNP);
defineProperty(&FWHMNP);
defineProperty(&TemperatureNP);
defineProperty(SeeingNP);
defineProperty(FWHMNP);
defineProperty(TemperatureNP);
defineProperty(DelayNP);
}
else
{
deleteProperty(SeeingNP.name);
deleteProperty(FWHMNP.name);
deleteProperty(TemperatureNP.name);
deleteProperty(SeeingNP.getName());
deleteProperty(FWHMNP.getName());
deleteProperty(TemperatureNP.getName());
deleteProperty(DelayNP);
}

Expand All @@ -148,39 +148,40 @@ bool FocusSim::ISNewSwitch(const char *dev, const char *name, ISState *states, c
if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
{
// Modes
if (strcmp(ModeSP.name, name) == 0)
if (ModeSP.isNameMatch(name))
{
IUUpdateSwitch(&ModeSP, states, names, n);
ModeSP.update(states, names, n);
uint32_t cap = 0;
int index = IUFindOnSwitchIndex(&ModeSP);
int index = ModeSP.findOnSwitchIndex();

switch (index)
{
case MODE_ALL:
cap = FOCUSER_CAN_ABS_MOVE | FOCUSER_CAN_REL_MOVE | FOCUSER_HAS_VARIABLE_SPEED;
break;

case MODE_ABSOLUTE:
cap = FOCUSER_CAN_ABS_MOVE;
break;

case MODE_RELATIVE:
cap = FOCUSER_CAN_REL_MOVE;
break;

case MODE_TIMER:
cap = FOCUSER_HAS_VARIABLE_SPEED;
break;

default:
ModeSP.s = IPS_ALERT;
IDSetSwitch(&ModeSP, "Unknown mode index %d", index);
return true;
case MODE_ALL:
cap = FOCUSER_CAN_ABS_MOVE | FOCUSER_CAN_REL_MOVE | FOCUSER_HAS_VARIABLE_SPEED;
break;

case MODE_ABSOLUTE:
cap = FOCUSER_CAN_ABS_MOVE;
break;

case MODE_RELATIVE:
cap = FOCUSER_CAN_REL_MOVE;
break;

case MODE_TIMER:
cap = FOCUSER_HAS_VARIABLE_SPEED;
break;

default:
ModeSP.setState(IPS_ALERT);
LOG_INFO("Unknown mode index");
ModeSP.apply();
return true;
}

FI::SetCapability(cap);
ModeSP.s = IPS_OK;
IDSetSwitch(&ModeSP, nullptr);
ModeSP.setState(IPS_OK);
ModeSP.apply();
return true;
}
}
Expand All @@ -197,19 +198,19 @@ bool FocusSim::ISNewNumber(const char *dev, const char *name, double values[], c
{
if (strcmp(name, "SEEING_SETTINGS") == 0)
{
SeeingNP.s = IPS_OK;
IUUpdateNumber(&SeeingNP, values, names, n);
SeeingNP.setState(IPS_OK);
SeeingNP.update(values, names, n);

IDSetNumber(&SeeingNP, nullptr);
SeeingNP.apply();
return true;
}

if (strcmp(name, "FOCUS_TEMPERATURE") == 0)
{
TemperatureNP.s = IPS_OK;
IUUpdateNumber(&TemperatureNP, values, names, n);
TemperatureNP.setState(IPS_OK);
TemperatureNP.update(values, names, n);

IDSetNumber(&TemperatureNP, nullptr);
TemperatureNP.apply();
return true;
}

Expand All @@ -234,7 +235,7 @@ bool FocusSim::ISNewNumber(const char *dev, const char *name, double values[], c
IPState FocusSim::MoveFocuser(FocusDirection dir, int speed, uint16_t duration)
{
double mid = (FocusAbsPosN[0].max - FocusAbsPosN[0].min) / 2;
int mode = IUFindOnSwitchIndex(&ModeSP);
int mode = ModeSP.findOnSwitchIndex();
double targetTicks = ((dir == FOCUS_INWARD) ? -1 : 1) * (speed * duration);

internalTicks += targetTicks;
Expand All @@ -254,21 +255,21 @@ IPState FocusSim::MoveFocuser(FocusDirection dir, int speed, uint16_t duration)

double ticks = initTicks + (internalTicks - mid) / 5000.0;

FWHMN[0].value = 0.5625 * ticks * ticks + SeeingN[0].value;
FWHMNP[0].setValue(0.5625 * ticks * ticks + SeeingNP[0].getValue());

LOGF_DEBUG("TIMER Current internal ticks: %g FWHM ticks: %g FWHM: %g", internalTicks, ticks,
FWHMN[0].value);
FWHMNP[0].getValue());

if (mode == MODE_ALL)
{
FocusAbsPosN[0].value = internalTicks;
IDSetNumber(&FocusAbsPosNP, nullptr);
}

if (FWHMN[0].value < SeeingN[0].value)
FWHMN[0].value = SeeingN[0].value;
if (FWHMNP[0].getValue() < SeeingNP[0].getValue())
FWHMNP[0].setValue(SeeingNP[0].getValue());

IDSetNumber(&FWHMNP, nullptr);
FWHMNP.apply();

return IPS_OK;
}
Expand All @@ -290,15 +291,15 @@ IPState FocusSim::MoveAbsFocuser(uint32_t targetTicks)

FocusAbsPosN[0].value = targetTicks;

FWHMN[0].value = 0.5625 * ticks * ticks + SeeingN[0].value;
FWHMNP[0].setValue(0.5625 * ticks * ticks + SeeingNP[0].getValue());

LOGF_DEBUG("ABS Current internal ticks: %g FWHM ticks: %g FWHM: %g", internalTicks, ticks,
FWHMN[0].value);
FWHMNP[0].getValue());

if (FWHMN[0].value < SeeingN[0].value)
FWHMN[0].value = SeeingN[0].value;
if (FWHMNP[0].getValue() < SeeingNP[0].getValue())
FWHMNP[0].setValue(SeeingNP[0].getValue());

IDSetNumber(&FWHMNP, nullptr);
FWHMNP.apply();

return IPS_OK;
}
Expand Down
83 changes: 40 additions & 43 deletions drivers/focuser/focus_simulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,61 +34,58 @@
*/
class FocusSim : public INDI::Focuser
{
public:
FocusSim();
virtual ~FocusSim() override = default;
public:
FocusSim();
virtual ~FocusSim() override = default;

const char *getDefaultName() override;
const char *getDefaultName() override;

void ISGetProperties(const char *dev) override;
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override;
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override;
void ISGetProperties(const char *dev) override;
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override;
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override;

protected:
protected:

bool initProperties() override;
bool updateProperties() override;
bool initProperties() override;
bool updateProperties() override;

bool Connect() override;
bool Disconnect() override;
bool Connect() override;
bool Disconnect() override;

virtual IPState MoveFocuser(FocusDirection dir, int speed, uint16_t duration) override;
virtual IPState MoveAbsFocuser(uint32_t targetTicks) override;
virtual IPState MoveRelFocuser(FocusDirection dir, uint32_t ticks) override;
virtual bool SetFocuserSpeed(int speed) override;
virtual IPState MoveFocuser(FocusDirection dir, int speed, uint16_t duration) override;
virtual IPState MoveAbsFocuser(uint32_t targetTicks) override;
virtual IPState MoveRelFocuser(FocusDirection dir, uint32_t ticks) override;
virtual bool SetFocuserSpeed(int speed) override;

virtual bool SetFocuserBacklash(int32_t steps) override;
virtual bool SetFocuserBacklashEnabled(bool enabled) override;
virtual bool SetFocuserBacklash(int32_t steps) override;
virtual bool SetFocuserBacklashEnabled(bool enabled) override;

virtual bool saveConfigItems(FILE *fp) override;
virtual bool saveConfigItems(FILE *fp) override;

private:
double internalTicks { 0 };
double initTicks { 0 };
private:
double internalTicks { 0 };
double initTicks { 0 };

// Seeing in arcseconds
INumberVectorProperty SeeingNP;
INumber SeeingN[1];
// Seeing in arcseconds
INDI::PropertyNumber SeeingNP {1};

// FWHM to be used by CCD driver to draw 'fuzzy' stars
INumberVectorProperty FWHMNP;
INumber FWHMN[1];
// FWHM to be used by CCD driver to draw 'fuzzy' stars
INDI::PropertyNumber FWHMNP {1};

// Temperature in celcius degrees
INumberVectorProperty TemperatureNP;
INumber TemperatureN[1];
// Temperature in celcius degrees
INDI::PropertyNumber TemperatureNP {1};

INDI::PropertyNumber DelayNP {1};
INDI::PropertyNumber DelayNP {1};

// Current mode of Focus simulator for testing purposes
enum
{
MODE_ALL,
MODE_ABSOLUTE,
MODE_RELATIVE,
MODE_TIMER,
MODE_COUNT
};
ISwitchVectorProperty ModeSP;
ISwitch ModeS[MODE_COUNT];
// Current mode of Focus simulator for testing purposes
enum
{
MODE_ALL,
MODE_ABSOLUTE,
MODE_RELATIVE,
MODE_TIMER,
MODE_COUNT
};
INDI::PropertySwitch ModeSP {4};
// ISwitch ModeS[MODE_COUNT];
};

0 comments on commit 97b1ebf

Please sign in to comment.