From 1d805b1ced3d1c07a6105e6280dc2316f6425020 Mon Sep 17 00:00:00 2001 From: Vsevolod Merenkov Date: Fri, 27 Sep 2019 01:09:05 +0300 Subject: [PATCH 1/7] Fix for serial terminal line break symbol #bugfix --- firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h index fbd802b..88c0b39 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h @@ -18,7 +18,8 @@ void identifyMarkers() { char x = Serial.read(); // char y = Wire.read(); - if (x == endMarker) { + if (x == '\n' || x == '\r') + { serialIncoming = true; inputBuffer[bytesRecvd] = 0; parseData(); From e029364cdcea0ae07c5702dc48b4dc2b74cbba6d Mon Sep 17 00:00:00 2001 From: Vsevolod Merenkov Date: Mon, 30 Sep 2019 02:56:24 +0300 Subject: [PATCH 2/7] Reverted voltmeter code #bugfix --- .../src/Pyr0_Piezo_Sensor_V2.x.x.cpp | 20 +------------------ .../src/pP_function.h | 15 ++++++++++++++ 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/Pyr0_Piezo_Sensor_V2.x.x.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/Pyr0_Piezo_Sensor_V2.x.x.cpp index 2cd9fa0..5c2813b 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/Pyr0_Piezo_Sensor_V2.x.x.cpp +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/Pyr0_Piezo_Sensor_V2.x.x.cpp @@ -82,9 +82,6 @@ update the voltMeterConstant variable in pP_config.h with the correct value ------------------------------------------------------------*/ -// Debug output toggle. Uncomment to enable -#define DEBUG true - /* Debug output verbose mode will continuously output sensor readings rather than waiting for user input */ //#define VERBOSE true @@ -115,20 +112,6 @@ void setup() { Serial.begin(9600); attachInterrupt(digitalPinToInterrupt(Z_TRG), pulse, FALLING); - - // Atmega's Secret Voltmeter setup: - // set the reference to Vcc and the measurement to the internal 1.1V reference - #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) - ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); - #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) - ADMUX = _BV(MUX5) | _BV(MUX0); - #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) - ADMUX = _BV(MUX3) | _BV(MUX2); - #else - ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); - #endif - - delay(2); // Wait for vref to settle Serial.println("Initializing Pyr0-Piezo Sensor..."); @@ -166,11 +149,10 @@ void loop() { VComp = analogRead(VCOMP_SENSE_PIN); VFol = analogRead(V_FOLLOW_PIN); - // Voltage Follower adjustment VLast = VOld - Vin; if (VLast > Hyst || VLast < -Hyst) { + // Voltage Follower adjustment adjustFollow(); - // Voltage Comparator adjustment adjustComp(); // Alert the user that auto-calibration is ongoing diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.h index 4c35da6..a1ef24d 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.h @@ -28,6 +28,21 @@ void pulse() { long readVcc() { // Read 1.1V reference against AVcc + + // Atmega's Secret Voltmeter setup: + // set the reference to Vcc and the measurement to the internal 1.1V reference +#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); +#elif defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) + ADMUX = _BV(MUX5) | _BV(MUX0); +#elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) + ADMUX = _BV(MUX3) | _BV(MUX2); +#else + ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); +#endif + + delay(2); // Wait for vref to settle + ADCSRA |= _BV(ADSC); // Start conversion while (bit_is_set(ADCSRA,ADSC)); // measuring From 6976b3908d76636679a2723dab972102adaba23f Mon Sep 17 00:00:00 2001 From: Vsevolod Merenkov Date: Mon, 30 Sep 2019 03:02:14 +0300 Subject: [PATCH 3/7] Removed old debug output define and sensor trigger debug output, modified VComp and VFol output to mV instead of 10-bit number #changelog --- .../AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.h | 3 --- firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.h index a1ef24d..2c0979c 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.h @@ -17,9 +17,6 @@ void digitalWriteFast(uint8_t pin, uint8_t x) { void pulse() { digitalWriteFast(TRG_OUT, LOW); sensorHReading = 1; - #ifdef DEBUG - Serial.println("Trig!"); - #endif delay(TRG_DUR); digitalWriteFast(TRG_OUT, HIGH); } diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h index 88c0b39..5bff6c2 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h @@ -193,11 +193,11 @@ void serialPrintState() Serial.print(","); Serial.print("\"VComp\":"); - Serial.print(VComp); + Serial.print((long) VComp * Vin / 1023); Serial.print(","); Serial.print("\"VFol\":"); - Serial.print(VFol); + Serial.print((long) VFol * Vin / 1023); Serial.print(","); Serial.print("\"Err\":"); From 8d5fb81219f9ea9ae8dca7b5eee8c9b6f7a8ae1d Mon Sep 17 00:00:00 2001 From: Vsevolod Merenkov Date: Mon, 30 Sep 2019 03:04:06 +0300 Subject: [PATCH 4/7] Fixed serial command for voltmeter constant, added EEPROM support for it #bugfix --- .../src/pP_config.cpp | 14 ++++++++ .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h | 5 +-- .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h | 36 +++++++++---------- .../src/pP_volatile.h | 2 +- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp index 47a5288..516788d 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp @@ -8,6 +8,8 @@ int LOOP_DUR = LOOP_DUR_DEFAULT; // duration of time between ADC checks and othe int TRG_DUR = TRG_DUR_DEFAULT; // duration of the Z-axis pulse sent, in ms int Hyst = HYST_DEFAULT; // Hysteresis value for ADC measurements int Debug = 0; +long voltMeterConstant = VM_CONST_DEFAULT; +uint8_t pP_i2c_address = 0xa0; void resetEEPROM() { resetConfig(); @@ -65,6 +67,17 @@ void restoreConfig() { } else { Hyst = temp; } + + long longTemp; + EEPROM.get(VM_CONST_DEFAULT, longTemp); + if (longTemp < 1000000L || longTemp > 1200000L) + { + resetEEPROM(); + } + else + { + voltMeterConstant = longTemp; + } } void resetConfig() { @@ -74,4 +87,5 @@ void resetConfig() { LOOP_DUR = LOOP_DUR_DEFAULT; TRG_DUR = TRG_DUR_DEFAULT; Hyst = HYST_DEFAULT; + voltMeterConstant = VM_CONST_DEFAULT; } \ No newline at end of file diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h index 657cbf0..95fa6ba 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h @@ -48,14 +48,15 @@ #endif #define VM_CONST_ADDRESS 28 +#define VM_CONST_DEFAULT 1125300L #if !(defined(voltMeterConstant)) - extern long voltMeterConstant = 1125300L; // For fine tuning input voltage sense + extern long voltMeterConstant; // For fine tuning input voltage sense #endif #ifdef I2C_INPUT #define I2C_SLAVE_ADDRESS 24 #if !(defined(pP_i2c_address)) - extern byte pP_i2c_address = 0xa0; // I2C Bus Address + extern uint8_t pP_i2c_address; // I2C Bus Address #endif #endif diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h index 5bff6c2..c584fdd 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h @@ -8,7 +8,7 @@ void parseData() { strcpy(serialMessageIn, strtokIndx); // copy it to serialMessageIn strtokIndx = strtok(NULL, " "); // this continues where the previous call left off - serialInt = atoi(strtokIndx); // convert this part to an integer + serialLong = atol(strtokIndx); // convert this part to an integer } /*------------------------------------------------*/ @@ -60,9 +60,9 @@ void identifyMarkers() { void updateGainFactor() { - if (serialInt >= 0) + if (serialLong >= 0) { - GAIN_FACTOR = serialInt; + GAIN_FACTOR = serialLong; adjustGain(); EEPROM.put(GAIN_FACTOR_ADDRESS, GAIN_FACTOR); } @@ -71,9 +71,9 @@ void updateGainFactor() /*------------------------------------------------*/ void updateVFol() { - if (serialInt >= 0) + if (serialLong >= 0) { - followerThrs = serialInt; + followerThrs = serialLong; adjustFollow(); EEPROM.put(FOLLOWER_THRESHOLD_ADDRESS, followerThrs); } @@ -81,9 +81,9 @@ void updateVFol() { /*------------------------------------------------*/ void updateVComp() { - if (serialInt >= 0) + if (serialLong >= 0) { - compThrs = serialInt; + compThrs = serialLong; adjustComp(); EEPROM.put(COMP_THRESHOLD_ADDRESS, compThrs); } @@ -93,36 +93,36 @@ void updateVComp() { void updateLoopDuration() { - if (serialInt >= 0) + if (serialLong >= 0) { - LOOP_DUR = serialInt; + LOOP_DUR = serialLong; EEPROM.put(LOOP_DUR_ADDRESS, LOOP_DUR); } } /*------------------------------------------------*/ void updateTrigDuration() { - if (serialInt >= 0) + if (serialLong >= 0) { - TRG_DUR = serialInt; + TRG_DUR = serialLong; EEPROM.put(TRG_DUR_ADDRESS, TRG_DUR); } } /*------------------------------------------------*/ void updateHysteresis() { - if (serialInt >= 0) + if (serialLong >= 0) { - Hyst = serialInt; + Hyst = serialLong; EEPROM.put(HYST_ADDRESS, Hyst); } } /*------------------------------------------------*/ void updateConstant() { - if (serialInt >= 0) + if (serialLong >= 0) { - voltMeterConstant = (long) serialInt; + voltMeterConstant = (long) serialLong; EEPROM.put(VM_CONST_ADDRESS, voltMeterConstant); } } @@ -130,9 +130,9 @@ void updateConstant() { /*------------------------------------------------*/ void updateDebug() { - if (serialInt > 0) { + if (serialLong > 0) { Debug = 1; - } else if (serialInt == 0) { + } else if (serialLong == 0) { Debug = 0; } } @@ -218,7 +218,7 @@ void updateParams() { updateVComp(); } else if (strcmp(serialMessageIn, "LOOP_D") == 0) { - updateTrigDuration(); + updateLoopDuration(); } else if (strcmp(serialMessageIn, "TRG_D") == 0) { updateTrigDuration(); diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h index 1ed74a9..9d12576 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h @@ -33,7 +33,7 @@ char inputBuffer[buffSize]; byte bytesRecvd = 0; bool serialIncoming = false; char serialMessageIn[buffSize] = {0}; -int serialInt = 0; +long serialLong = 0; //#define LOW 0 //#define HIGH 1 From 51d734986b97ea8e070ab87eac70d871d057cfce Mon Sep 17 00:00:00 2001 From: Vsevolod Merenkov Date: Mon, 30 Sep 2019 03:06:15 +0300 Subject: [PATCH 5/7] Fixed internal oscillator frequency configuration on ATmega88P, added configuration for ATmega328PB #changelog --- .../Pyr0_Piezo_Sensor_v2.x.x/platformio.ini | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/platformio.ini b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/platformio.ini index d4b1918..e6e49ba 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/platformio.ini +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/platformio.ini @@ -22,5 +22,23 @@ upload_flags = upload_port = COM4 upload_speed = 19200 -board_build.f_cpu = 1000000L +board_build.f_cpu = 8000000L +board_fuses.lfuse = "0xE2" +board_fuses.hfuse = "0xDF" +board_fuses.efuse = "0xF9" +[env:ATmega328PB] +platform = atmelavr +board = ATmega328PB +framework = arduino +upload_protocol = stk500v1 +; each flag in a new line +upload_flags = + -P$UPLOAD_PORT + -b$UPLOAD_SPEED + +; edit these lines +upload_port = COM4 +upload_speed = 19200 + +board_build.f_cpu = 8000000L \ No newline at end of file From ecf28d89725a1c5c2d572805ca99c240a1882842 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Wed, 2 Oct 2019 18:46:41 -0700 Subject: [PATCH 6/7] Code formatting unified --- .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h | 39 +++++++------------ 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h index c584fdd..e6aae55 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h @@ -18,8 +18,7 @@ void identifyMarkers() { char x = Serial.read(); // char y = Wire.read(); - if (x == '\n' || x == '\r') - { + if (x == '\n' || x == '\r') { serialIncoming = true; inputBuffer[bytesRecvd] = 0; parseData(); @@ -27,8 +26,7 @@ void identifyMarkers() { } else { inputBuffer[bytesRecvd] = x; bytesRecvd++; - if (bytesRecvd == buffSize) - { + if (bytesRecvd == buffSize) { bytesRecvd = buffSize - 1; } } @@ -60,8 +58,7 @@ void identifyMarkers() { void updateGainFactor() { - if (serialLong >= 0) - { + if (serialLong >= 0) { GAIN_FACTOR = serialLong; adjustGain(); EEPROM.put(GAIN_FACTOR_ADDRESS, GAIN_FACTOR); @@ -71,8 +68,7 @@ void updateGainFactor() /*------------------------------------------------*/ void updateVFol() { - if (serialLong >= 0) - { + if (serialLong >= 0) { followerThrs = serialLong; adjustFollow(); EEPROM.put(FOLLOWER_THRESHOLD_ADDRESS, followerThrs); @@ -81,8 +77,7 @@ void updateVFol() { /*------------------------------------------------*/ void updateVComp() { - if (serialLong >= 0) - { + if (serialLong >= 0) { compThrs = serialLong; adjustComp(); EEPROM.put(COMP_THRESHOLD_ADDRESS, compThrs); @@ -91,10 +86,8 @@ void updateVComp() { /*------------------------------------------------*/ -void updateLoopDuration() -{ - if (serialLong >= 0) - { +void updateLoopDuration() { + if (serialLong >= 0) { LOOP_DUR = serialLong; EEPROM.put(LOOP_DUR_ADDRESS, LOOP_DUR); } @@ -102,8 +95,7 @@ void updateLoopDuration() /*------------------------------------------------*/ void updateTrigDuration() { - if (serialLong >= 0) - { + if (serialLong >= 0) { TRG_DUR = serialLong; EEPROM.put(TRG_DUR_ADDRESS, TRG_DUR); } @@ -111,8 +103,7 @@ void updateTrigDuration() { /*------------------------------------------------*/ void updateHysteresis() { - if (serialLong >= 0) - { + if (serialLong >= 0) { Hyst = serialLong; EEPROM.put(HYST_ADDRESS, Hyst); } @@ -120,8 +111,7 @@ void updateHysteresis() { /*------------------------------------------------*/ void updateConstant() { - if (serialLong >= 0) - { + if (serialLong >= 0) { voltMeterConstant = (long) serialLong; EEPROM.put(VM_CONST_ADDRESS, voltMeterConstant); } @@ -139,12 +129,10 @@ void updateDebug() { /*------------------------------------------------*/ -void serialPrintConfig() -{ +void serialPrintConfig() { Serial.print("GAIN_F "); Serial.print(GAIN_FACTOR); - switch (GAIN_FACTOR) - { + switch (GAIN_FACTOR) { case 0: Serial.println(" 3x"); break; @@ -184,8 +172,7 @@ void serialPrintConfig() Serial.println(voltMeterConstant); } -void serialPrintState() -{ +void serialPrintState() { Serial.print("{"); Serial.print("\"Vcc\":"); From 9126cd71a4b0a0570fe8f3b9295334bb94ac8ca3 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Wed, 2 Oct 2019 18:51:59 -0700 Subject: [PATCH 7/7] code formatting --- .../AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp index 516788d..3b4fced 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp @@ -70,12 +70,9 @@ void restoreConfig() { long longTemp; EEPROM.get(VM_CONST_DEFAULT, longTemp); - if (longTemp < 1000000L || longTemp > 1200000L) - { + if (longTemp < 1000000L || longTemp > 1200000L) { resetEEPROM(); - } - else - { + } else { voltMeterConstant = longTemp; } }