Skip to content

Commit

Permalink
Gain range shifting resistor variable
Browse files Browse the repository at this point in the history
Adds handling functions, a getter/setter, and updated gain calculations
for added a resistor in series with the Pot to shift the gain range.

There's a critical fix not yet released but in the master branch.
PlatformIO with Arduino-ESP8266>3.0 needs it for the library to
function.
  • Loading branch information
jclds139 committed Nov 16, 2021
1 parent 1f645d8 commit b04e793
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
21 changes: 16 additions & 5 deletions geoscope/ADCModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ volatile uint32_t adcReadableTime_cycles = 0;
#define potSSpin 5 // digital pot slave select pin

MCP41xxx* gainPot = NULL;
double gainShiftRatio = 0;


const SPISettings adcConfig = SPISettings(0.8e6, MSBFIRST, SPI_MODE0);
Expand Down Expand Up @@ -93,21 +94,22 @@ void adcPoll() {
void changeAmplifierGain(float val) {
amplifierGain = val;
static uint16_t potValue = 0;
static const unsigned int potSteps = UINT8_MAX;
static const uint16_t potSteps = UINT8_MAX;

if (val <= 1) {
if (val <= 1.0) {
gainPot->shutdown();
//minimum gain (1) comes by shutting down the
//minimum gain (1) comes by shutting down the pot
amplifierGain = 1;
}
else {

// calculate the nearest gain resistor value
potValue = round(potSteps-(potSteps/val));
potValue = round(potSteps * (1-(1+gainShiftRatio)/val));
if (potValue >= potSteps) {
potValue = potSteps-1;
}
amplifierGain = 1 + potValue/(potSteps-potValue);
double gainDivisor = potSteps-potValue;
amplifierGain = 1.0 + potValue/gainDivisor + potSteps*gainShiftRatio/gainDivisor;
//correct the stored gain value to the actual set value

gainPot->write(potValue);
Expand All @@ -123,10 +125,19 @@ void gainLoad() {
else
amplifierGain = storage.readString().toFloat();
storage.close();

storage = LittleFS.open("/config/rPotRatio", "r");
if (storage)
gainShiftRatio = storage.readString().toFloat();
storage.close();
}

void gainSave() {
File storage = LittleFS.open("/config/gain", "w");
storage.printf("%.3f\n", amplifierGain);
storage.close();

storage = LittleFS.open("/config/rPotRatio", "w");
storage.printf("%f\n", gainShiftRatio);
storage.close();
}
1 change: 1 addition & 0 deletions geoscope/ADCModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define ADC_HOLD_TIME_CYCLES 8 //from MCP3201 datasheet: t_SUCS >= 100ns

extern MCP41xxx* gainPot;
extern double gainShiftRatio;

void adcSetup();
void adcPoll();
Expand Down
23 changes: 9 additions & 14 deletions geoscope/MQTTService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void mqttSetup() {
mqttConnect();
}
gainLoad();
mqttNotify("Current gain: " + String(amplifierGain));
mqttNotify("Current gain: " + String(amplifierGain, 3));
}

void mqttShutdown() {
Expand All @@ -71,12 +71,6 @@ void mqttShutdown() {
}

void mqttConnect() {
if (MQTT_BROKER_TIMEOUT && attemptTimeout && millis() > *attemptTimeout) {
//initiates self reset if we're requiring Broker for operation (can be turned off during testing),
// and there's a set attemptTimeout
mqttNotify("Device Self Reset");
forceReset();
}
// Attempt to connect
mqttclient.disconnect();
if (mqttclient.connect(("GEOSCOPE-" + clientId).c_str()))
Expand All @@ -88,9 +82,11 @@ void mqttConnect() {
mqttclient.subscribe("geoscope/restart",LWMQTT_QOS1);
mqttclient.subscribe(CONFIG_TOPIC_PREFIX + "#", LWMQTT_QOS1);
}
else
{
minYield(20);
else if (MQTT_BROKER_TIMEOUT && attemptTimeout && millis() > *attemptTimeout) {
//initiates self reset if we're requiring Broker for operation (can be turned off during testing),
// and there's a set attemptTimeout
mqttNotify("Device Self Reset");
forceReset();
}
}

Expand Down Expand Up @@ -119,7 +115,7 @@ void mqttSend() {
payload += String(rawBuffer[buffer_row][i]) + ",";
}
payload.remove(payload.length()-1); //trash that last ','
payload += "],\"gain\":" + String(amplifierGain) + "}";
payload += "],\"gain\":" + String(amplifierGain, 3) + "}";

if (!mqttclient.connected()) {
if (!attemptTimeout)
Expand All @@ -145,9 +141,8 @@ void mqttOnMessage(String & topic, String & in_payload) {
}
else if (topic.equalsIgnoreCase("geoscope/restart")) {
samplingDisable();
mqttNotify("Restart");
mqttclient.clearWill();
minYield(10);
mqttNotify("MQTT Initiated Restart");
mqttShutdown();
forceReset();
}
else if (topic.equalsIgnoreCase("geoscope/hb")) {
Expand Down
21 changes: 20 additions & 1 deletion geoscope/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,21 @@ bool adc_gain(Commander &cmd) {

}

bool adc_ratio(Commander &cmd) {
float payload;
if (cmd.getFloat(payload)) {
gainShiftRatio = payload;
changeAmplifierGain(amplifierGain);
mqttReportGain(amplifierGain);
}
else {
cmd.rewind();
cmd.print(F("Gain Shift Ratio (R16/R_pot): "));
cmd.printf("%f\n", gainShiftRatio);
}
return 0;
}

bool adc_dump(Commander &cmd) {
cmd.println(F("Dumping Raw Data to Terminal. Press any key to stop."));
minYield(2000); //wait a couple seconds for the user to read the info
Expand All @@ -401,7 +416,11 @@ bool adc_dump(Commander &cmd) {
}

const commandList_t adcCommands[] = {
{"gain", adc_gain, "Get/Set Amplifier gain (min. 1, max ~834, rounds to nearest valid value)"},
{"gain", adc_gain, "Get/Set Amplifier gain (min. 1, max >80, rounds to nearest valid value)"},
{"ratio", adc_ratio, "Get/Set Amplifier gain shifting ration (R16/R_pot)"},
{"shift", adc_ratio, "Alias for `ratio`"},
{"resistor", adc_ratio, "Alias for `ratio`"},
{"res", adc_ratio, "Alias for `ratio`"},
{"dump", adc_dump, "Start streaming raw ADC data"},
{"exit", sub_exit, "Return to main prompt"}
};
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ framework = arduino
lib_deps =
256dpi/MQTT
Commander
TelnetStream
https://github.com/jandrassy/TelnetStream
ESPWebDAV
ESP8266WebServer
https://github.com/akaJes/AsyncPing
Expand Down

0 comments on commit b04e793

Please sign in to comment.