Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Fix trigger threshold calculation for all scopes #88

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/digitizer_block_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,16 @@ namespace gr {
return count;
}

double
digitizer_block_impl::get_aichan_range(const std::string &id) const
{
if (id != "AUX")
return 1.; // AUX triger input has a fixed Range of +/- 1V

auto idx = convert_to_aichan_idx(id);
return d_channel_settings[idx].range;
}

void
digitizer_block_impl::set_aichan_range(const std::string &id, double range, double range_offset)
{
Expand Down
2 changes: 2 additions & 0 deletions lib/digitizer_block_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ namespace gr {
*/
int get_enabled_aichan_count() const;

double get_aichan_range(const std::string &id) const;

void set_aichan_range(const std::string &id, double range, double range_offset = 0) override;

void set_aichan_trigger(const std::string &id, trigger_direction_t direction, double threshold) override;
Expand Down
28 changes: 21 additions & 7 deletions lib/picoscope_3000a_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,32 @@ namespace gr {
};

int16_t
convert_voltage_to_ps3000a_raw_logic_value(double value)
convert_voltage_to_ps3000a_raw_logic_level(double voltage)
{
double max_logical_voltage = 5.0;

if (value > max_logical_voltage)
if (fabs(voltage) > fabs(max_logical_voltage))
{
std::ostringstream message;
message << "Exception in " << __FILE__ << ":" << __LINE__ << ": max logical level is: " << max_logical_voltage;
throw std::invalid_argument(message.str());
message << "Exception in " << __FILE__ << ":" << __LINE__ << ": Voltage '" << voltage <<
"' exceed maximum (+/-" << max_logical_voltage << "V)";
GR_LOG_ERROR(d_logger, message.str());
}

return (int16_t) ((value / max_logical_voltage) * (double)PS3000A_MAX_LOGIC_LEVEL);
return (int16_t) ((voltage / max_logical_voltage) * (double)PS3000A_MAX_LOGIC_LEVEL);
}

int16_t
convert_voltage_to_ps3000a_raw_adc_count(double voltage, float channel_range, int16_t PS3000A_MAX_VALUE)
{
if (fabs(voltage) > double (fabs(channel_range))) {
std::ostringstream message;
message << "Exception in " << __FILE__ << ":" << __LINE__ << ": Voltage '" << voltage <<
"' exceed maximum channel range (+/-" << channel_range << "V)";
GR_LOG_ERROR(d_logger, message.str());
}

return (int16_t) ((voltage / channel_range) * (double)PS3000A_MAX_VALUE);
}

PS3000A_CHANNEL
Expand Down Expand Up @@ -558,7 +572,7 @@ namespace gr {
d_handle,
static_cast<PS3000A_DIGITAL_PORT>(PS3000A_DIGITAL_PORT0 + port),
d_port_settings[port].enabled,
convert_voltage_to_ps3000a_raw_logic_value(d_port_settings[port].logic_level));
convert_voltage_to_ps3000a_raw_logic_level(d_port_settings[port].logic_level));
if(status != PICO_OK) {
GR_LOG_ERROR(d_logger, "ps3000aSetDigitalPort (port " + std::to_string(port)
+ "): " + ps3000a_get_error_message(status));
Expand All @@ -573,7 +587,7 @@ namespace gr {
status = ps3000aSetSimpleTrigger(d_handle,
true, // enable
convert_to_ps3000a_channel(d_trigger_settings.source),
convert_voltage_to_ps3000a_raw_logic_value(d_trigger_settings.threshold),
convert_voltage_to_ps3000a_raw_adc_count(d_trigger_settings.threshold, get_aichan_range(d_trigger_settings.source), d_max_value),
convert_to_ps3000a_threshold_direction(d_trigger_settings.direction),
0, // delay
-1); // auto trigger
Expand Down
18 changes: 8 additions & 10 deletions lib/picoscope_4000a_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -287,18 +287,16 @@ namespace gr {
};

int16_t
convert_voltage_to_ps4000a_raw_logic_value(double value)
convert_voltage_to_ps4000a_raw_adc_count(double voltage, float channel_range, int16_t PS4000A_MAX_VALUE)
{
double max_logical_voltage = 5.0;

if (value > max_logical_voltage)
{
if (fabs(voltage) > double (fabs(channel_range))) {
std::ostringstream message;
message << "Exception in " << __FILE__ << ":" << __LINE__ << ": max logical level is: " << max_logical_voltage;
throw std::invalid_argument(message.str());
message << "Exception in " << __FILE__ << ":" << __LINE__ << ": Voltage '" << voltage <<
"' exceed maximum channel range (+/-" << channel_range << "V)";
GR_LOG_ERROR(d_logger, message.str());
}
// Note max channel value not provided with PicoScope API, we use ext max value
return (int16_t) ((value / max_logical_voltage) * (double)PS4000A_EXT_MAX_VALUE);

return (int16_t) ((voltage / channel_range) * (double)PS4000A_MAX_VALUE);
}

PS4000A_CHANNEL
Expand Down Expand Up @@ -505,7 +503,7 @@ namespace gr {
status = ps4000aSetSimpleTrigger(d_handle,
true, // enable
convert_to_ps4000a_channel(d_trigger_settings.source),
convert_voltage_to_ps4000a_raw_logic_value(d_trigger_settings.threshold),
convert_voltage_to_ps4000a_raw_adc_count(d_trigger_settings.threshold, get_aichan_range(d_trigger_settings.source), d_max_value),
convert_to_ps4000a_threshold_direction(d_trigger_settings.direction),
0, // delay
-1); // auto trigger
Expand Down
22 changes: 2 additions & 20 deletions lib/picoscope_6000_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ namespace gr {
};

int16_t
convert_voltage_to_ps6000_raw_logic_value(double voltage, float channel_range)
convert_voltage_to_ps6000_raw_adc_count(double voltage, float channel_range)
{
if (fabs(voltage) > double (fabs(channel_range))) {
std::ostringstream message;
Expand Down Expand Up @@ -534,28 +534,10 @@ namespace gr {
if (d_trigger_settings.is_enabled()
&& d_acquisition_mode == acquisition_mode_t::RAPID_BLOCK)
{
float channel_range;
if (d_trigger_settings.source == "A")
channel_range = d_channel_settings[0].range;
else if (d_trigger_settings.source == "B")
channel_range = d_channel_settings[1].range;
else if (d_trigger_settings.source == "C")
channel_range = d_channel_settings[2].range;
else if (d_trigger_settings.source == "D")
channel_range = d_channel_settings[3].range;
else if (d_trigger_settings.source == "AUX")
channel_range = 1.; // AUX can only be used for triggering, has a fixed Range of +/-1V according to Programmers guideline
else {
std::ostringstream message;
message << "Exception in " << __FILE__ << ":" << __LINE__ << ": Invalid Channel Name: " << d_trigger_settings.source;
GR_LOG_ERROR(d_logger, message.str());
return make_pico_6000_error_code(status);
}

status = ps6000SetSimpleTrigger(d_handle,
true, // enable
convert_to_ps6000_channel(d_trigger_settings.source),
convert_voltage_to_ps6000_raw_logic_value(d_trigger_settings.threshold, channel_range),
convert_voltage_to_ps6000_raw_adc_count(d_trigger_settings.threshold, get_aichan_range(d_trigger_settings.source)),
convert_to_ps6000_threshold_direction(d_trigger_settings.direction),
0, // delay
-1); // auto trigger
Expand Down