Skip to content

Commit

Permalink
refactoring of volt. and cur. limit check
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Apr 22, 2020
1 parent b0a69dd commit c259b2c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
25 changes: 18 additions & 7 deletions src/eez/modules/psu/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,8 @@ void Channel::tick(uint32_t tick_usec) {
}

if (!io_pins::isInhibited()) {
setCvMode(channelInterface->isCcMode(subchannelIndex));
setCcMode(channelInterface->isCvMode(subchannelIndex));
setCvMode(channelInterface->isCvMode(subchannelIndex));
setCcMode(channelInterface->isCcMode(subchannelIndex));
}

// update history values
Expand Down Expand Up @@ -1450,6 +1450,17 @@ void Channel::setPowerLimit(float limit) {
}
}

bool Channel::isVoltageLimitExceeded(float u) {
return channel_dispatcher::getValuePrecision(*this, UNIT_VOLT, u) >
channel_dispatcher::getValuePrecision(*this, UNIT_VOLT, channel_dispatcher::getULimit(*this));
}

bool Channel::isCurrentLimitExceeded(float i) {
return channel_dispatcher::getValuePrecision(*this, UNIT_AMPER, i) >
channel_dispatcher::getValuePrecision(*this, UNIT_AMPER, channel_dispatcher::getILimit(*this));
}


bool Channel::isVoltageBalanced() {
if (!isInstalled()) {
return false;
Expand Down Expand Up @@ -1527,12 +1538,12 @@ void Channel::setCurrentRangeSelectionMode(CurrentRangeSelectionMode mode) {
flags.currentRangeSelectionMode = mode;

if (flags.currentRangeSelectionMode == CURRENT_RANGE_SELECTION_ALWAYS_LOW) {
if (i.set > 0.05f) {
i.set = 0.05f;
float limit = 0.05f;
if (i.set > limit) {
i.set = limit;
}

if (i.limit > 0.05f) {
i.limit = 0.05f;
if (i.limit > limit) {
i.limit = limit;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/eez/modules/psu/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,10 @@ class Channel {
/// Change power limit, it will adjust U_SET or I_SET if necessary.
void setPowerLimit(float limit);

bool isVoltageLimitExceeded(float u);

bool isCurrentLimitExceeded(float i);

bool isPowerLimitExceeded(float u, float i) {
return channelInterface->isPowerLimitExceeded(subchannelIndex, u, i);
}
Expand Down
9 changes: 5 additions & 4 deletions src/eez/modules/psu/list_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,13 @@ int checkLimits(int iChannel) {
current = roundedValue;
}

if (voltage > channel_dispatcher::getULimit(channel)) {

if (channel.isVoltageLimitExceeded(voltage)) {
g_errorChannelIndex = channel.channelIndex;
return SCPI_ERROR_VOLTAGE_LIMIT_EXCEEDED;
}

if (current > channel_dispatcher::getILimit(channel)) {
if (channel.isCurrentLimitExceeded(current)) {
g_errorChannelIndex = channel.channelIndex;
return SCPI_ERROR_CURRENT_LIMIT_EXCEEDED;
}
Expand Down Expand Up @@ -547,14 +548,14 @@ int maxListsSize(Channel &channel) {

bool setListValue(Channel &channel, int16_t it, int *err) {
float voltage = channel_dispatcher::roundChannelValue(channel, UNIT_VOLT, g_channelsLists[channel.channelIndex].voltageList[it % g_channelsLists[channel.channelIndex].voltageListLength]);
if (voltage > channel_dispatcher::getULimit(channel)) {
if (channel.isVoltageLimitExceeded(voltage)) {
g_errorChannelIndex = channel.channelIndex;
*err = SCPI_ERROR_VOLTAGE_LIMIT_EXCEEDED;
return false;
}

float current = channel_dispatcher::roundChannelValue(channel, UNIT_AMPER, g_channelsLists[channel.channelIndex].currentList[it % g_channelsLists[channel.channelIndex].currentListLength]);
if (current > channel_dispatcher::getILimit(channel)) {
if (channel.isCurrentLimitExceeded(current)) {
g_errorChannelIndex = channel.channelIndex;
*err = SCPI_ERROR_CURRENT_LIMIT_EXCEEDED;
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/eez/modules/psu/scpi/appl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ scpi_result_t scpi_cmd_apply(scpi_t *context) {
call_set_current = true;
}

if (voltage > channel_dispatcher::getULimit(*channel)) {
if (channel->isVoltageLimitExceeded(voltage)) {
SCPI_ErrorPush(context, SCPI_ERROR_VOLTAGE_LIMIT_EXCEEDED);
return SCPI_RES_ERR;
}

if (call_set_current && current > channel_dispatcher::getILimit(*channel)) {
if (call_set_current && channel->isCurrentLimitExceeded(current)) {
SCPI_ErrorPush(context, SCPI_ERROR_CURRENT_LIMIT_EXCEEDED);
return SCPI_RES_ERR;
}
Expand Down
8 changes: 4 additions & 4 deletions src/eez/modules/psu/scpi/sour.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ scpi_result_t scpi_cmd_sourceCurrentLevelImmediateAmplitude(scpi_t *context) {
return SCPI_RES_ERR;
}

if (current > channel_dispatcher::getILimit(*channel)) {
if (channel->isCurrentLimitExceeded(current)) {
SCPI_ErrorPush(context, SCPI_ERROR_CURRENT_LIMIT_EXCEEDED);
return SCPI_RES_ERR;
}
Expand Down Expand Up @@ -205,7 +205,7 @@ scpi_result_t scpi_cmd_sourceVoltageLevelImmediateAmplitude(scpi_t *context) {
return SCPI_RES_ERR;
}

if (voltage > channel_dispatcher::getULimit(*channel)) {
if (channel->isVoltageLimitExceeded(voltage)) {
SCPI_ErrorPush(context, SCPI_ERROR_VOLTAGE_LIMIT_EXCEEDED);
return SCPI_RES_ERR;
}
Expand Down Expand Up @@ -955,7 +955,7 @@ scpi_result_t scpi_cmd_sourceListCurrentLevel(scpi_t *context) {
return SCPI_RES_ERR;
}

if (current > channel_dispatcher::getIMaxLimit(*channel)) {
if (channel->isCurrentLimitExceeded(current)) {
SCPI_ErrorPush(context, SCPI_ERROR_CURRENT_LIMIT_EXCEEDED);
return SCPI_RES_ERR;
}
Expand Down Expand Up @@ -1086,7 +1086,7 @@ scpi_result_t scpi_cmd_sourceListVoltageLevel(scpi_t *context) {
return SCPI_RES_ERR;
}

if (voltage > channel_dispatcher::getUMaxLimit(*channel)) {
if (channel->isVoltageLimitExceeded(voltage)) {
SCPI_ErrorPush(context, SCPI_ERROR_VOLTAGE_LIMIT_EXCEEDED);
return SCPI_RES_ERR;
}
Expand Down
4 changes: 2 additions & 2 deletions src/eez/modules/psu/trigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ int checkTrigger() {
return err;
}
} else {
if (channel.u.triggerLevel > channel_dispatcher::getULimit(channel)) {
if (channel.isVoltageLimitExceeded(channel.u.triggerLevel)) {
g_errorChannelIndex = channel.channelIndex;
return SCPI_ERROR_VOLTAGE_LIMIT_EXCEEDED;
}

if (channel.i.triggerLevel > channel_dispatcher::getILimit(channel)) {
if (channel.isCurrentLimitExceeded(channel.i.triggerLevel)) {
g_errorChannelIndex = channel.channelIndex;
return SCPI_ERROR_CURRENT_LIMIT_EXCEEDED;
}
Expand Down

0 comments on commit c259b2c

Please sign in to comment.